-
Notifications
You must be signed in to change notification settings - Fork 0
/
hast_table_incl_chaining.py
64 lines (41 loc) · 1.58 KB
/
hast_table_incl_chaining.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class HashItem:
def __init__(self, key, value):
self.key = key
self.value = value
class ChainedHashTable:
def __init__(self):
self.size = 256
self.slots = [None for i in range(self.size)]
self.count = 0
self.load_balance = 0
def _hash(self, key):
hash_value = 0
multiplier = 1
for character in key:
hash_value += multiplier * ord(character)
return hash_value % self.size
def set(self, key, value):
item = HashItem(key, value)
hash_key = self._hash(item.key)
while self.slots[hash_key] is not None:
# Check list of slots with existing keys
for item in self.slots[hash_key]:
if item.key == key:
return
# Append key to slots if not found already
self.slots[hash_key].append(item)
break
if self.slots[hash_key] is None:
# Add list with item to empty slot
self.slots[hash_key] = [item]
self.count += 1
# Log current Load Balance on hash table
self.load_balance = round(len([element for element in self.slots if element is not None]) / self.size, 2)
print(f'Load Balance Currently at {self.load_balance}')
def get(self, key):
hash_key = self._hash(key)
# Check list of slots with existing keys
for item in self.slots[hash_key]:
if item.key == key:
return item.key
return None