Last active
March 24, 2019 14:00
-
-
Save AdamGold/f91b08e693058b3228558affe07a4d75 to your computer and use it in GitHub Desktop.
filled_entries.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __init__(self, size=10): | |
self.entries = [None for _ in range(size * 2)] | |
self.size = size | |
self.actual_size = size * 2 | |
self.filled_entries = 0 | |
def __setitem__(self, key: str, value: Any) -> int: | |
"""allow insertion of items with object[key] = value""" | |
if self.filled_entries == self.size: | |
# no more room | |
raise KeyError | |
entry = Entry(key, value) | |
index = entry.hash % self.actual_size | |
existing_entry = self.entries[index] | |
while existing_entry and not entry.compare_hash(existing_entry): | |
# don't overlap existing values | |
index = (index + 1) % self.actual_size | |
existing_entry = self.entries[index] | |
self.entries[index] = entry | |
self.filled_entries += 1 | |
return index |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment