MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / _try_set

Method _try_set

data_structures/hashing/hash_map.py:69–88  ·  view source on GitHub ↗

Try to add value to the bucket. If bucket is empty or key is the same, does insert and return True. If bucket has another key that means that we need to check next bucket.

(self, ind: int, key: KEY, val: VAL)

Source from the content-addressed store, hash-verified

67 return (ind + 1) % len(self._buckets)
68
69 def _try_set(self, ind: int, key: KEY, val: VAL) -> bool:
70 """
71 Try to add value to the bucket.
72
73 If bucket is empty or key is the same, does insert and return True.
74
75 If bucket has another key that means that we need to check next bucket.
76 """
77 stored = self._buckets[ind]
78 if not stored:
79 # A falsy item means that bucket was never used (None)
80 # or was deleted (_deleted).
81 self._buckets[ind] = _Item(key, val)
82 self._len += 1
83 return True
84 elif stored.key == key:
85 stored.val = val
86 return True
87 else:
88 return False
89
90 def _is_full(self) -> bool:
91 """

Callers 1

_add_itemMethod · 0.95

Calls 1

_ItemClass · 0.85

Tested by

no test coverage detected