-
Notifications
You must be signed in to change notification settings - Fork 4
/
myhashmap.py
81 lines (69 loc) · 1.96 KB
/
myhashmap.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
class MyHashMap:
INITIAL_SIZE = 1 << 4 # 16
MAXIMUM_CAPACITY = 1 << 30
def __init__(self):
self.hashTable = [None] * self.INITIAL_SIZE
@classmethod
def initializeWithCapacity(cls, capacity):
tableSize = cls.tableSizeFor(capacity)
instance = cls()
instance.hashTable = [None] * tableSize
return instance
class Entry:
def __init__(self, k, v):
self.key = k
self.value = v
self.next = None
@staticmethod
def tableSizeFor(cap):
n = cap - 1
n |= n >> 1
n |= n >> 2
n |= n >> 4
n |= n >> 8
n |= n >> 16
if n < 0:
return 1
else:
if n >= MyHashMap.MAXIMUM_CAPACITY:
return MyHashMap.MAXIMUM_CAPACITY
else:
return n + 1
def put(self, key, value):
hashCode = key % len(self.hashTable)
node = self.hashTable[hashCode]
if node is None:
newNode = self.Entry(key, value)
self.hashTable[hashCode] = newNode
else:
previousNode = node
while node:
if node.key == key:
node.value = value
return
previousNode = node
node = node.next
newNode = self.Entry(key, value)
previousNode.next = newNode
def get(self, key):
hashCode = key % len(self.hashTable)
node = self.hashTable[hashCode]
while node:
if node.key == key:
return node.value
node = node.next
return None
if __name__ == '__main__':
map = MyHashMap.initializeWithCapacity(7)
map.put(1, "hi")
map.put(2, "my")
map.put(3, "name")
map.put(4, "is")
map.put(5, "John")
map.put(6, "how")
map.put(7, "are")
map.put(8, "you")
map.put(9, "friends")
map.put(10, "?")
value = map.get(8)
print(value)