-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
hash map #12678
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
hash map #12678
Changes from 4 commits
2bee459
454eded
2c99889
6354304
875caab
cff17ec
79dbf28
cf14f87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,7 @@ | |
VAL = TypeVar("VAL") | ||
|
||
|
||
@dataclass(frozen=True, slots=True) | ||
@dataclass(slots=True) | ||
class _Item(Generic[KEY, VAL]): | ||
key: KEY | ||
val: VAL | ||
|
@@ -72,16 +72,16 @@ def _try_set(self, ind: int, key: KEY, val: VAL) -> bool: | |
|
||
If bucket is empty or key is the same, does insert and return True. | ||
|
||
If bucket has another key or deleted placeholder, | ||
that means that we need to check next bucket. | ||
If bucket has another key that means that we need to check next bucket. | ||
""" | ||
stored = self._buckets[ind] | ||
if not stored: | ||
# A falsy item means that is None (bucket never used) or _deleted. | ||
self._buckets[ind] = _Item(key, val) | ||
self._len += 1 | ||
return True | ||
elif stored.key == key: | ||
self._buckets[ind] = _Item(key, val) | ||
stored.val = val | ||
return True | ||
else: | ||
return False | ||
|
@@ -228,6 +228,25 @@ def __delitem__(self, key: KEY) -> None: | |
Traceback (most recent call last): | ||
... | ||
KeyError: 4 | ||
|
||
# Test resize down when sparse | ||
## Setup: resize up | ||
>>> hm = HashMap(100, capacity_factor=0.75) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we go with size 4? In that case, we could avoid the loop. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like this? # Test resize down when sparse
## Setup: resize up
>>> hm = HashMap(4, capacity_factor=0.75)
>>> len(hm._buckets)
4
>>> hm[0] = 0
>>> hm[1] = 1
>>> hm[2] = 2
>>> len(hm._buckets)
4
>>> hm[3] = 3
>>> len(hm._buckets)
8
## Resize down
>>> del hm[3]
>>> len(hm._buckets)
8
>>> del hm[2]
>>> len(hm._buckets)
4 I've checked that it passes, but I don't know which one I prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd prefer to keep tests as linear as possible. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's wait for the 3d opinion. |
||
>>> len(hm._buckets) | ||
100 | ||
>>> for i in range(75): | ||
... hm[i] = i | ||
>>> len(hm._buckets) | ||
100 | ||
>>> hm[75] = 75 | ||
>>> len(hm._buckets) | ||
200 | ||
|
||
## Resize down | ||
>>> del hm[75] | ||
>>> del hm[74] | ||
>>> len(hm._buckets) | ||
100 | ||
""" | ||
for ind in self._iterate_buckets(key): | ||
item = self._buckets[ind] | ||
|
Uh oh!
There was an error while loading. Please reload this page.