-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic_hash_table.py
175 lines (134 loc) · 5.86 KB
/
dynamic_hash_table.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
168
169
170
171
172
173
174
175
from hash_table import HashSet, HashMap
from prime_generator import get_next_size
class DynamicHashSet(HashSet):
def __init__(self, collision_type, params):
super().__init__(collision_type, params)
def rehash(self):
p=get_next_size()
if self.collision_type=="Linear":
new_set=[-1 for i in range(p)]
c=(self.params[0],self.params[1],p)
self.params=c
for key in self.hash_set:
if(key!=-1):
s=self.get_slot(key)
t=p
if(new_set[s]==-1):
new_set[s]=key
elif(new_set[s]==key):
pass
else:
a=(s+1)%t
while(a!=s):
if(new_set[a]==key):
break
elif(new_set[a]==-1):
new_set[a]=key#exception raising add at last if table size not enough
break
a=(a+1)%t
self.hash_set=new_set
elif self.collision_type=="Double":
new_set=[-1 for i in range(p)]
c=(self.params[0],self.params[1],self.params[2],p)
self.params=c
for key in self.hash_set:
if(key!=-1):
s=self.get_slot(key)
t=self.params[-1]
if(new_set[s]==-1):
new_set[s]=key
elif(new_set[s]==key):
pass
else:
h2=self.hash2(key)
a=(s+h2)%t
while(a!=s):
if(new_set[a]==key):
break
elif(new_set[a]==-1):
new_set[a]=key
break
a=(a+h2)%t
self.hash_set=new_set
elif (self.collision_type=="Chain"):
new_set=[[] for i in range(p)]
c=(self.params[0],self.params[1],p)
self.params=c
for i in self.hash_set:
for key in i:
s=self.get_slot(key)
new_set[s].append(key)
self.hash_set=new_set
def insert(self, x):
# YOU DO NOT NEED TO MODIFY THIS
super().insert(x)
if self.get_load() >= 0.5:
self.rehash()
class DynamicHashMap(HashMap):
def __init__(self, collision_type, params):
super().__init__(collision_type, params)
def rehash(self):
# IMPLEMENT THIS FUNCTION
p=get_next_size()
if(self.collision_type=="Linear"):
new_map=[(-1,-1) for i in range(p)]
c=(self.params[0],self.params[1],p)
self.params=c
for x in self.hash_map:
key=x[0]
if(key!=-1):
s=self.get_slot(key)
t=self.params[-1]
if(new_map[s][0]==key):
pass
elif(new_map[s][0]==-1):
new_map[s]=x
else:
a=(s+1)%t
while(a!=s):
if(new_map[a][0]==x[0]):
break
elif(new_map[a][0]==-1):
new_map[a]=x#exception raising add at last if table size not enough
break
a=(a+1)%t
self.hash_map=new_map
elif self.collision_type=="Double":
new_map=[(-1,-1) for i in range(p)]
c=(self.params[0],self.params[1],self.params[2],p)
self.params=c
for x in self.hash_map:
key=x[0]
if(key!=-1):
s=self.get_slot(key)
t=self.params[-1]
if([s][0]==key):
pass
elif(new_map[s][0]==-1):
new_map[s]=x
else:
h2=self.hash2(key)
a=(s+h2)%t
while(a!=s):
if(new_map[a][0]==key):
break
elif(new_map[a][0]==-1):
new_map[a]=x
break
a=(a+h2)%t
self.hash_map=new_map
elif (self.collision_type=="Chain"):
new_map=[[] for i in range(p)]
c=(self.params[0],self.params[1],p)
self.params=c
for i in self.hash_map:
for j in i:
key=j[0]
s=self.get_slot(key)
new_map[s].append(j)
self.hash_map=new_map
def insert(self, key):
# YOU DO NOT NEED TO MODIFY THIS
super().insert(key)
if self.get_load() >= 0.5:
self.rehash()