Selfie
Loading...
Searching...
No Matches
selfie_lib.ArrayMap.ArrayMap Class Reference
Inheritance diagram for selfie_lib.ArrayMap.ArrayMap:

Public Member Functions

 __init__ (self)
 
"ArrayMap[K, V]" empty (cls)
 
ListBackedSet[Kkeys (self)
 
_ArrayMapEntries[K, Vitems (self)
 
V __getitem__ (self, K key)
 
Iterator[K__iter__ (self)
 
int __len__ (self)
 
"ArrayMap[K, V]" plus (self, K key, V value)
 
"ArrayMap[K, V]" minus_sorted_indices (self, list[int] indices)
 
"ArrayMap[K, V]" plus_or_noop (self, K key, V value)
 
"ArrayMap[K, V]" plus_or_noop_or_replace (self, K key, V value)
 
 __str__ (self)
 
 __repr__ (self)
 

Protected Member Functions

int _binary_search_key (self, K key)
 

Detailed Description

Definition at line 147 of file ArrayMap.py.

Constructor & Destructor Documentation

◆ __init__()

selfie_lib.ArrayMap.ArrayMap.__init__ (   self)

Definition at line 151 of file ArrayMap.py.

151 def __init__(self):
152 raise NotImplementedError("Use ArrayMap.empty() or other class methods instead")
153

Member Function Documentation

◆ __getitem__()

V selfie_lib.ArrayMap.ArrayMap.__getitem__ (   self,
K  key 
)

Definition at line 173 of file ArrayMap.py.

173 def __getitem__(self, key: K) -> V:
174 index = self._binary_search_key(key)
175 if index >= 0:
176 return self.__data[2 * index + 1] # type: ignore
177 raise KeyError(key)
178

◆ __iter__()

Iterator[K] selfie_lib.ArrayMap.ArrayMap.__iter__ (   self)

Definition at line 179 of file ArrayMap.py.

179 def __iter__(self) -> Iterator[K]:
180 return (self.__data[i] for i in range(0, len(self.__data), 2)) # type: ignore
181

◆ __len__()

int selfie_lib.ArrayMap.ArrayMap.__len__ (   self)

Definition at line 182 of file ArrayMap.py.

182 def __len__(self) -> int:
183 return len(self.__data) // 2
184

◆ __repr__()

selfie_lib.ArrayMap.ArrayMap.__repr__ (   self)

Definition at line 237 of file ArrayMap.py.

237 def __repr__(self):
238 return self.__str__()

◆ __str__()

selfie_lib.ArrayMap.ArrayMap.__str__ (   self)

Definition at line 234 of file ArrayMap.py.

234 def __str__(self):
235 return "{" + ", ".join(f"{k}: {v}" for k, v in self.items()) + "}"
236

◆ _binary_search_key()

int selfie_lib.ArrayMap.ArrayMap._binary_search_key (   self,
K  key 
)
protected

Definition at line 185 of file ArrayMap.py.

185 def _binary_search_key(self, key: K) -> int:
186 return _binary_search(self.__keys, key)
187

◆ empty()

"ArrayMap[K, V]" selfie_lib.ArrayMap.ArrayMap.empty (   cls)

Definition at line 162 of file ArrayMap.py.

162 def empty(cls) -> "ArrayMap[K, V]":
163 if not hasattr(cls, "__EMPTY"):
164 cls.__EMPTY = cls.__create([])
165 return cls.__EMPTY
166

◆ items()

_ArrayMapEntries[K, V] selfie_lib.ArrayMap.ArrayMap.items (   self)

Definition at line 170 of file ArrayMap.py.

170 def items(self) -> _ArrayMapEntries[K, V]: # type: ignore
171 return _ArrayMapEntries(self.__data)
172

◆ keys()

ListBackedSet[K] selfie_lib.ArrayMap.ArrayMap.keys (   self)

Definition at line 167 of file ArrayMap.py.

167 def keys(self) -> ListBackedSet[K]: # type: ignore
168 return self.__keys
169

◆ minus_sorted_indices()

"ArrayMap[K, V]" selfie_lib.ArrayMap.ArrayMap.minus_sorted_indices (   self,
list[int]  indices 
)

Definition at line 198 of file ArrayMap.py.

198 def minus_sorted_indices(self, indices: list[int]) -> "ArrayMap[K, V]":
199 new_data = self.__data[:]
200 adjusted_indices = [i * 2 for i in indices] + [i * 2 + 1 for i in indices]
201 adjusted_indices.sort(reverse=True)
202 for index in adjusted_indices:
203 del new_data[index]
204 return ArrayMap.__create(new_data)
205

◆ plus()

"ArrayMap[K, V]" selfie_lib.ArrayMap.ArrayMap.plus (   self,
K  key,
V  value 
)

Definition at line 188 of file ArrayMap.py.

188 def plus(self, key: K, value: V) -> "ArrayMap[K, V]":
189 index = self._binary_search_key(key)
190 if index >= 0:
191 raise KeyError
192 insert_at = -(index + 1)
193 new_data = self.__data[:]
194 new_data.insert(insert_at * 2, key)
195 new_data.insert(insert_at * 2 + 1, value)
196 return ArrayMap.__create(new_data)
197

◆ plus_or_noop()

"ArrayMap[K, V]" selfie_lib.ArrayMap.ArrayMap.plus_or_noop (   self,
K  key,
V  value 
)

Definition at line 206 of file ArrayMap.py.

206 def plus_or_noop(self, key: K, value: V) -> "ArrayMap[K, V]":
207 index = self._binary_search_key(key)
208 if index >= 0:
209 return self
210 else:
211 # Insert new key-value pair
212 insert_at = -(index + 1)
213 new_data = self.__data[:]
214 new_data.insert(insert_at * 2, key)
215 new_data.insert(insert_at * 2 + 1, value)
216 return ArrayMap.__create(new_data)
217

◆ plus_or_noop_or_replace()

"ArrayMap[K, V]" selfie_lib.ArrayMap.ArrayMap.plus_or_noop_or_replace (   self,
K  key,
V  value 
)

Definition at line 218 of file ArrayMap.py.

218 def plus_or_noop_or_replace(self, key: K, value: V) -> "ArrayMap[K, V]":
219 index = self._binary_search_key(key)
220 if index >= 0:
221 if (self.__data[2 * index + 1]) == value:
222 return self
223 # Replace existing value
224 new_data = self.__data[:]
225 new_data[2 * index + 1] = value # Update the value at the correct position
226 else:
227 # Insert new key-value pair
228 insert_at = -(index + 1)
229 new_data = self.__data[:]
230 new_data.insert(insert_at * 2, key)
231 new_data.insert(insert_at * 2 + 1, value)
232 return ArrayMap.__create(new_data)
233

The documentation for this class was generated from the following file: