MCPcopy
hub / github.com/keon/algorithms / put

Method put

algorithms/data_structures/hash_table.py:42–68  ·  view source on GitHub ↗

Insert or update a key-value pair. Args: key: The key to insert. value: The value associated with the key. Raises: ValueError: If the table is full.

(self, key: int, value: object)

Source from the content-addressed store, hash-verified

40 self._values: list[object] = [self._empty] * size
41
42 def put(self, key: int, value: object) -> None:
43 """Insert or update a key-value pair.
44
45 Args:
46 key: The key to insert.
47 value: The value associated with the key.
48
49 Raises:
50 ValueError: If the table is full.
51 """
52 initial_hash = hash_ = self.hash(key)
53
54 while True:
55 if self._keys[hash_] is self._empty or self._keys[hash_] is self._deleted:
56 self._keys[hash_] = key
57 self._values[hash_] = value
58 self._len += 1
59 return
60 elif self._keys[hash_] == key:
61 self._keys[hash_] = key
62 self._values[hash_] = value
63 return
64
65 hash_ = self._rehash(hash_)
66
67 if initial_hash == hash_:
68 raise ValueError("Table is full")
69
70 def get(self, key: int) -> object | None:
71 """Retrieve the value for a given key.

Calls 2

hashMethod · 0.95
_rehashMethod · 0.95