|
| 1 | +from collections.abc import Set, Iterator, Mapping |
| 2 | +from typing import List, TypeVar, Union |
| 3 | +from abc import abstractmethod, ABC |
| 4 | + |
| 5 | +T = TypeVar("T") |
| 6 | +V = TypeVar("V") |
| 7 | +K = TypeVar("K") |
| 8 | + |
| 9 | + |
| 10 | +class ListBackedSet(Set[T], ABC): |
| 11 | + @abstractmethod |
| 12 | + def __len__(self) -> int: |
| 13 | + ... |
| 14 | + |
| 15 | + @abstractmethod |
| 16 | + def __getitem__(self, index: Union[int, slice]) -> Union[T, List[T]]: |
| 17 | + ... |
| 18 | + |
| 19 | + def __contains__(self, item: object) -> bool: |
| 20 | + for i in range(len(self)): |
| 21 | + if self[i] == item: |
| 22 | + return True |
| 23 | + return False |
| 24 | + |
| 25 | + |
| 26 | +class ArraySet(ListBackedSet[K]): |
| 27 | + __data: List[K] |
| 28 | + |
| 29 | + def __init__(self, data: List[K]): |
| 30 | + raise NotImplementedError("Use ArraySet.empty() instead") |
| 31 | + |
| 32 | + @classmethod |
| 33 | + def __create(cls, data: List[K]) -> "ArraySet[K]": |
| 34 | + # Create a new instance without calling __init__ |
| 35 | + instance = super().__new__(cls) |
| 36 | + instance.__data = data |
| 37 | + return instance |
| 38 | + |
| 39 | + def __iter__(self) -> Iterator[K]: |
| 40 | + return iter(self.__data) |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def empty(cls) -> "ArraySet[K]": |
| 44 | + if not hasattr(cls, "__EMPTY"): |
| 45 | + cls.__EMPTY = cls([]) |
| 46 | + return cls.__EMPTY |
| 47 | + |
| 48 | + def __len__(self) -> int: |
| 49 | + return len(self.__data) |
| 50 | + |
| 51 | + def __getitem__(self, index: Union[int, slice]) -> Union[K, List[K]]: |
| 52 | + if isinstance(index, int): |
| 53 | + return self.__data[index] |
| 54 | + elif isinstance(index, slice): |
| 55 | + return self.__data[index] |
| 56 | + else: |
| 57 | + raise TypeError("Invalid argument type.") |
| 58 | + |
| 59 | + def plusOrThis(self, element: K) -> "ArraySet[K]": |
| 60 | + # TODO: use binary search, and also special sort order for strings |
| 61 | + if element in self.__data: |
| 62 | + return self |
| 63 | + else: |
| 64 | + new_data = self.__data[:] |
| 65 | + new_data.append(element) |
| 66 | + new_data.sort() # type: ignore[reportOperatorIssue] |
| 67 | + return ArraySet.__create(new_data) |
| 68 | + |
| 69 | + |
| 70 | +class ArrayMap(Mapping[K, V]): |
| 71 | + def __init__(self, data: list): |
| 72 | + # TODO: hide this constructor as done in ArraySet |
| 73 | + self.__data = data |
| 74 | + |
| 75 | + @classmethod |
| 76 | + def empty(cls) -> "ArrayMap[K, V]": |
| 77 | + if not hasattr(cls, "__EMPTY"): |
| 78 | + cls.__EMPTY = cls([]) |
| 79 | + return cls.__EMPTY |
| 80 | + |
| 81 | + def __getitem__(self, key: K) -> V: |
| 82 | + index = self.__binary_search_key(key) |
| 83 | + if index >= 0: |
| 84 | + return self.__data[2 * index + 1] |
| 85 | + raise KeyError(key) |
| 86 | + |
| 87 | + def __iter__(self) -> Iterator[K]: |
| 88 | + return (self.__data[i] for i in range(0, len(self.__data), 2)) |
| 89 | + |
| 90 | + def __len__(self) -> int: |
| 91 | + return len(self.__data) // 2 |
| 92 | + |
| 93 | + def __binary_search_key(self, key: K) -> int: |
| 94 | + # TODO: special sort order for strings |
| 95 | + low, high = 0, (len(self.__data) // 2) - 1 |
| 96 | + while low <= high: |
| 97 | + mid = (low + high) // 2 |
| 98 | + mid_key = self.__data[2 * mid] |
| 99 | + if mid_key < key: |
| 100 | + low = mid + 1 |
| 101 | + elif mid_key > key: |
| 102 | + high = mid - 1 |
| 103 | + else: |
| 104 | + return mid |
| 105 | + return -(low + 1) |
| 106 | + |
| 107 | + def plus(self, key: K, value: V) -> "ArrayMap[K, V]": |
| 108 | + index = self.__binary_search_key(key) |
| 109 | + if index >= 0: |
| 110 | + raise ValueError("Key already exists") |
| 111 | + insert_at = -(index + 1) |
| 112 | + new_data = self.__data[:] |
| 113 | + new_data[insert_at * 2 : insert_at * 2] = [key, value] |
| 114 | + return ArrayMap(new_data) |
| 115 | + |
| 116 | + def minus_sorted_indices(self, indicesToRemove: List[int]) -> "ArrayMap[K, V]": |
| 117 | + if not indicesToRemove: |
| 118 | + return self |
| 119 | + newData = [] |
| 120 | + for i in range(0, len(self.__data), 2): |
| 121 | + if i // 2 not in indicesToRemove: |
| 122 | + newData.extend(self.__data[i : i + 2]) |
| 123 | + return ArrayMap(newData) |
0 commit comments