class MutableHashMap[K = Unsubscripted, V = Unsubscripted]
Build one with MutableHashMap.empty(), make(*entries) or
from_iterable(entries). Updates return the map itself.
Functions#
__init__#
__init__() -> Nonedef __init__(self) -> None:
self._entries = {}
# Reentrant: ``modify_at`` goes through ``set`` and ``remove``, and an
# ``f`` may touch the map.
self._lock = threading.RLock()empty#
empty() -> MutableHashMap[K, V]Spelled MutableHashMap[str, int].empty().
Returns MutableHashMap[K, V]
@classmethod
def empty(cls) -> MutableHashMap[K, V]:
"""Spelled ``MutableHashMap[str, int].empty()``."""
return MutableHashMap()make#
make[K2, V2](*entries: tuple[K2, V2] = ()) -> MutableHashMap[K2, V2]
make(*entries: tuple[K, V] = ()) -> MutableHashMap[K, V]| Parameter | Type | Description |
|---|---|---|
entries | = () |
@overload
@classmethod
def make[K2, V2](
cls: type[MutableHashMap[Unsubscripted, Unsubscripted]], *entries: tuple[K2, V2]
) -> MutableHashMap[K2, V2]: ...
@overload
@classmethod
def make(cls, *entries: tuple[K, V]) -> MutableHashMap[K, V]: ...
@classmethod
def make[K2, V2](cls, *entries: tuple[K2, V2]) -> MutableHashMap[K2, V2]:
return MutableHashMap.from_iterable(entries)from_iterable#
from_iterable[K2, V2](entries: list[tuple[K2, V2]]) -> MutableHashMap[K2, V2]
from_iterable[K2, V2](entries: Iterable[tuple[K2, V2]]) -> MutableHashMap[K2, V2]
from_iterable(entries: Iterable[tuple[K, V]]) -> MutableHashMap[K, V]| Parameter | Type | Description |
|---|---|---|
entries |
@overload
@classmethod
def from_iterable[K2, V2](
cls: type[MutableHashMap[Unsubscripted, Unsubscripted]], entries: list[tuple[K2, V2]]
) -> MutableHashMap[K2, V2]: ...
@overload
@classmethod
def from_iterable[K2, V2](
cls: type[MutableHashMap[Unsubscripted, Unsubscripted]], entries: Iterable[tuple[K2, V2]]
) -> MutableHashMap[K2, V2]: ...
@overload
@classmethod
def from_iterable(cls, entries: Iterable[tuple[K, V]]) -> MutableHashMap[K, V]: ...
@classmethod
def from_iterable[K2, V2](cls, entries: Iterable[tuple[K2, V2]]) -> MutableHashMap[K2, V2]:
out = MutableHashMap[K2, V2]()
for key, value in entries:
out.set(key, value)
return outis_empty#
is_empty() -> boolReturns bool
def is_empty(self) -> bool:
return not self._entriesget#
get(key) -> Option[V]| Parameter | Type | Description |
|---|---|---|
key | object |
Returns Option[V]
def get(self, key: object) -> Option[V]:
found = self._entries.get(equal_key(key))
return None if found is None else Some(found[1])has#
has(key) -> bool| Parameter | Type | Description |
|---|---|---|
key | object |
Returns bool
def has(self, key: object) -> bool:
return equal_key(key) in self._entrieskeys#
keys() -> Iterator[K]Returns Iterator[K]
def keys(self) -> Iterator[K]:
return iter([k for k, _ in self._entries.values()])values#
values() -> Iterator[V]Returns Iterator[V]
def values(self) -> Iterator[V]:
return iter([v for _, v in self._entries.values()])set#
set(key, value) -> MutableHashMap[K, V]| Parameter | Type | Description |
|---|---|---|
key | K | |
value | V |
Returns MutableHashMap[K, V]
def set(self, key: K, value: V) -> MutableHashMap[K, V]:
k = equal_key(key)
with self._lock:
found = self._entries.get(k)
self._entries[k] = (key if found is None else found[0], value)
return selfmodify#
modify(key, f) -> MutableHashMap[K, V]Replace key's value with f(value); nothing without key.
| Parameter | Type | Description |
|---|---|---|
key | K | |
f | Callable[[V], V] |
Returns MutableHashMap[K, V]
def modify(self, key: K, f: Callable[[V], V]) -> MutableHashMap[K, V]:
"""Replace ``key``'s value with ``f(value)``; nothing without ``key``."""
k = equal_key(key)
with self._lock:
found = self._entries.get(k)
if found is not None:
self._entries[k] = (found[0], f(found[1]))
return selfmodify_at#
modify_at(key, f) -> MutableHashMap[K, V]Set or remove key by what f makes of its current value.
| Parameter | Type | Description |
|---|---|---|
key | K | |
f | Callable[[Option[V]], Option[V]] |
Returns MutableHashMap[K, V]
def modify_at(self, key: K, f: Callable[[Option[V]], Option[V]]) -> MutableHashMap[K, V]:
"""Set or remove ``key`` by what ``f`` makes of its current value."""
with self._lock:
match f(self.get(key)):
case Some(value):
return self.set(key, value)
case None:
return self.remove(key)remove#
remove(key) -> MutableHashMap[K, V]| Parameter | Type | Description |
|---|---|---|
key | K |
Returns MutableHashMap[K, V]
def remove(self, key: K) -> MutableHashMap[K, V]:
self._entries.pop(equal_key(key), None)
return selfclear#
clear() -> MutableHashMap[K, V]Returns MutableHashMap[K, V]
def clear(self) -> MutableHashMap[K, V]:
self._entries.clear()
return selffor_each#
for_each(f) -> None| Parameter | Type | Description |
|---|---|---|
f | Callable[[V, K], object] |
def for_each(self, f: Callable[[V, K], object]) -> None:
for k, v in list(self._entries.values()):
f(v, k)