-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashmap.py
167 lines (136 loc) · 4.9 KB
/
hashmap.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# Implementation of the Map ADT using closed hashing and a probe with
# double hashing.
from myarray import myArray
# Storage class for holding the key/value pairs.
class _MapEntry( object ):
def __init__( self, key, value ):
self.key = key
self.value = value
class HashMap:
UNUSED = None
EMPTY = _MapEntry( None, None )
def __init__( self ):
self._table = myArray( 7 )
self._count = 0
self._maxCount = len( self._table ) - len( self._table ) // 3
self._keylist = list()
def __len__( self ):
return self._count
def __contains__( self, key ):
slot = self._findSlot( key, False )
return slot is not None
# Adds a new entry to the map if the key does not exist. Otherwise, the
# new value replaces the current value associated with the key.
def add( self, key, value ):
if key in self:
slot = self._findSlot( key , False )
self._table[slot].value = value
return False
else:
slot = self._findSlot( key, True )
#print slot
self._table[slot] = _MapEntry( key, value )
self._count += 1
self._keylist.append(key)
if self._count == self._maxCount:
self._rehash()
return True
# Returns the value associated with the key.
def valueOf( self, key ):
slot = self._findSlot( key, False )
assert slot is not None, "Invalid map key"
return self._table[slot].value
# Remove the entry associated with the key.
def remove( self, key ):
slot = self._findSlot( key, False )
assert slot is not None, "Invalid map key"
self._table[slot] = self.EMPTY
self._count -= 1
self._keylist.remove(key)
# Returns an iterator for traversing the keys in the map.
def __iter__( self ):
return _HashTableIterator( self._keylist )
#pass
# Finds the slot containing the key or where the key can be added.
# forInsert indicates if the search is for an insertion, which locates
# the slot into which the new key can be added.
def _findSlot( self, key, forInsert ):
slot = self._hash1( key )
#print 'slot', slot
step = self._hash2( key )
M = len( self._table )
#print '..', self._table[slot]
while self._table[slot] is not self.UNUSED:
if forInsert and \
(self._table[slot] is self.UNUSED or self._table[slot] is self.EMPTY):
#print 'insert here'
return slot
elif not forInsert and \
(self._table[slot] is not self.EMPTY and self._table[slot].key == key):
#print 'find it'
return slot
else:
#print 'continue probe'
slot = (slot + step) % M
if forInsert:
return slot
else:
return None
# Rebuilds the hash table.
def _rehash( self ):
oriTable = self._table
newSize = len(self._table) * 2 + 1
self._table = myArray( newSize )
self._count = 0
self._maxCount = newSize - newSize // 3
for entry in oriTable:
if entry is not self.UNUSED and entry is not self.EMPTY:
slot = self._findSlot( entry.key, True )
self._table[slot] = entry
self._count += 1
# The main hash function for mapping keys to table
def _hash1( self, key ):
return abs( hash(key) ) % len(self._table)
# The second hash function used with double hashing probes.
def _hash2( self, key ):
return 1 + abs( hash(key)) % (len(self._table) - 2)
class _HashTableIterator:
def __init__( self, theArray):
self._arrayRef = theArray
self._curNdx = 0
def __iter__( self ):
return self
def next( self ):
if self._curNdx < len( self._arrayRef ):
entry = self._arrayRef[ self._curNdx ]
#print entry
self._curNdx += 1
return entry
else:
raise StopIteration
if __name__ == '__main__':
mydict = HashMap()
mydict.add( 45, 100 )
mydict.add( 23, 20 )
mydict.add( 2, 86 )
mydict.add( 64, 90 )
print 'M: %d' % len(mydict)
print 'N: %d' % len(mydict._table)
for key in mydict:
print key, mydict.valueOf(key)
print '==================='
mydict.add( 123, 10)
mydict.add( 73, 38)
print 'M: %d' % len(mydict)
print 'N: %d' % len(mydict._table)
#print mydict.valueOf( 'James' )
for key in mydict:
print key, mydict.valueOf(key)
print '==================='
mydict.remove( 2 )
#print mydict.valueOf( 'Ryuka' )
print 'M: %d' % len(mydict)
print 'N: %d' % len(mydict._table)
for key in mydict:
print key, mydict.valueOf(key)
print '==================='