-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashClassTimed.py
More file actions
76 lines (59 loc) · 1.95 KB
/
Copy pathHashClassTimed.py
File metadata and controls
76 lines (59 loc) · 1.95 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
import random
import time
import string
#The string class is needed for the next function
def rword(k):
return ''.join(random.choice(string.ascii_lowercase) for i in range(k))
class HashTable:
def __init__(self,size):
self.slots = [None] * size
self.data = [None] * size
def store(self,item,data):
hashvalue = self.hashfunction(item,len(self.slots))
if self.slots[hashvalue] == None:
self.slots[hashvalue] = item
self.data[hashvalue] = data
else:
nextslot = self.rehash(hashvalue,len(self.slots))
while self.slots[nextslot] != None:
nextslot = self.rehash(nextslot,len(self.slots))
self.slots[nextslot]=item
self.data[nextslot]=data
def search(self,item):
start=time.clock()
startslot = self.hashfunction(item,len(self.slots))
data = None
stop = False
found = False
position = startslot
while self.slots[position] != None and \
not found and not stop:
if self.slots[position] == item:
found = True
data = self.data[position]
else:
position=self.rehash(position,len(self.slots))
if position == startslot:
stop = True
end=time.clock()
return data,end-start
#very useful overloaded get and set "magic" methods
def __getitem__(self,item):
return self.search(item)
def __setitem__(self,item,data):
self.store(item,data)
#def hashfunction(self,item,size):
# return item%size
def hashfunction(self,astring,size):
sum = 0
for pos in range(len(astring)):
sum = sum + ord(astring[pos])
return sum%size
def rehash(self,oldhash,size):
return (oldhash+1)%size
def __str__(self):
print( self.slots)
print (self.data)
H = HashTable(5591)
for i in range(1000):
H[rword(5)]=rword(12)