forked from logicheap/DataStructures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearProbeHashMap.py
More file actions
95 lines (76 loc) · 2.66 KB
/
LinearProbeHashMap.py
File metadata and controls
95 lines (76 loc) · 2.66 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
class Item:
def __init__(self, k, v):
self._key = k
self._value = v
class LinearProbeHashMap:
def __init__(self, capacity=11):
#Creates a list of size CAPACITY and initialize it with None
self._table = [None]*capacity
#Keeping the count of total entries in table
self._n = 0
def _hash_function(self, key):
return key%len(self._table)
def __len__(self):
return self._n
def _find_bucket(self, key, hashed_key):
while True:
if self._table[hashed_key] is not None:
if self._table[hashed_key]._key == key:
return hashed_key, True
hashed_key = (hashed_key + 1)%len(self._table)
else:
return hashed_key, False
def __getitem__(self, key):
hashed_key = self._hash_function(key)
hashed_key, found = self._find_bucket(key, hashed_key)
if found is False:
raise KeyError("key error")
return self._table[hashed_key]._value
def __setitem__(self, key, value):
hashed_key = self._hash_function(key)
hashed_key, found = self._find_bucket(key, hashed_key)
self._table[hashed_key] = Item(key, value)
if found is False:
self._n += 1
if self._n > len(self._table)//2:
self._resize(2*len(self._table)-1)
def _resize(self, capacity):
old = []
for bucket in self._table:
if bucket is not None:
old.append(bucket)
self._table = capacity*[None]
self._n = 0
for items in old:
self[items._key] = items._value
def print(self):
for i in range(len(self._table)):
if self._table[i] is not None:
print("..............................")
print("At " + str(i))
print(str(self._table[i]._key) + " " + str(self._table[i]._value))
testHashMap = LinearProbeHashMap()
print("size of table is "+ str(len(testHashMap._table)))
#Testing set function
testHashMap[10] = "John"
testHashMap[21] = "Ram"
testHashMap[24] = "Aman"
#Testing resize function
testHashMap[22] = "Jyoti"
testHashMap[23] = "Kate"
testHashMap[3] = "Phoebe"
testHashMap[4] = "Jyotishi"
#testHashMap[56] = "Kate"
#testHashMap[34] = "Phoebe"
#testHashMap[18] = "Jyoti"
#testHashMap[23] = "Kate"
#testHashMap[39] = "Phoebe"
#testHashMap[13] = "Jyoti"
#testHashMap[29] = "Kate"
#testHashMap[37] = "Phoebe"
#Testing get function
print("value for key 21 is: " + testHashMap[21])
#print("value for key 25 is: " + testHashMap[25])
testHashMap.print()
#Check size of table
print("size of table is "+ str(len(testHashMap._table)))