-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtable.mojo
340 lines (238 loc) · 9.61 KB
/
hashtable.mojo
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
from math import abs
from time import now
from list_iterator import ListIterator
alias NOT_FOUND_ERROR = "Key not found"
@always_inline
fn hash_fn(key: String) -> Int:
var hash = 2166136261
for i in range(len(key)):
hash ^= ord(key[i])
hash *= 16777619
return abs(hash)
@value
@register_passable("trivial")
struct Item[T: AnyType]:
var key: StringRef
var value: T
var expire: Int # expire in ms
fn __init__(key: StringRef, value: T) -> Self:
return Self {key: key, value: value, expire: -1}
fn __eq__(self, other: None) -> Bool:
return False
fn __eq__(self, other: Item[Bool]) -> Bool:
return self.key == other.key and rebind[Bool](self.value) == other.value
fn __eq__(self, other: Item[Int]) -> Bool:
return self.key == other.key and rebind[Int](self.value) == other.value
fn __eq__(self, other: Item[Float32]) -> Bool:
return self.key == other.key and rebind[Float32](self.value) == other.value
fn __eq__(self, other: Item[Float64]) -> Bool:
return self.key == other.key and rebind[Float64](self.value) == other.value
fn __eq__(self, other: Item[StringRef]) -> Bool:
return self.key == other.key and rebind[StringRef](self.value) == other.value
fn __ne__(self, other: None) -> Bool:
return True
fn __ne__(self, other: Item[Bool]) -> Bool:
return self.__eq__(other) == False
fn __ne__(self, other: Item[Int]) -> Bool:
return self.__eq__(other) == False
fn __ne__(self, other: Item[Float32]) -> Bool:
return self.__eq__(other) == False
fn __ne__(self, other: Item[Float64]) -> Bool:
return self.__eq__(other) == False
fn __ne__(self, other: Item[StringRef]) -> Bool:
return self.__eq__(other) == False
fn set_value(inout self: Self, value: T):
self.value = value
fn set_expire(inout self: Self, key: StringRef, expire: Int):
self.expire = expire
fn is_expired(self: Self) -> Bool:
return self.expire != -1 and self.expire < (now() // 1_000_000)
fn value_to_string(self: Self) raises -> String:
if T == Bool:
return String(rebind[Bool](self.value))
elif T == Float32:
return String(rebind[Float32](self.value))
elif T == Float64:
return String(rebind[Float64](self.value))
elif T == Int:
return String(rebind[Int](self.value))
elif T == StringRef:
return '"' + String(rebind[StringRef](self.value)) + '"'
else:
return "???"
@value
@register_passable("trivial")
struct Array[T: AnyType]:
var data: Pointer[T]
var size: Int
var cap: Int
fn __init__(size: Int) -> Self:
let cap = size * 2
let data = Pointer[T].alloc(cap)
return Self {data: data, size: size, cap: cap}
fn __init__[
*Ts: AnyType
](inout self, owned other_list: ListLiteral[Ts]) raises -> Self:
let other_list_len = len(other_list)
let size = 0
let cap = other_list_len * 2
let data = Pointer[T].alloc(self.cap)
let src = Pointer.address_of(other_list).bitcast[T]()
for i in range(other_list_len):
self.push_back(src.load(i))
return Self {data: data, size: size, cap: cap}
fn __getitem__(borrowed self: Self, i: Int) raises -> T:
if i > self.size:
raise Error("Index out of bounds")
return self.data.load(i)
fn __setitem__(borrowed self: Self, i: Int, item: T) raises:
if i > self.size:
raise Error("Index out of bounds")
self.data.store(i, item)
fn __ne__(self: Self, other: None) -> Bool:
return False
fn __ne__(self: Self, other: Array[Bool]) -> Bool:
return not rebind[Bool](self.data.load()) == other.data.load()
fn __ne__(self: Self, other: Array[Int]) -> Bool:
return not rebind[Int](self.data.load()) == other.data.load()
fn __ne__(self: Self, other: Array[StringRef]) -> Bool:
return not rebind[StringRef](self.data.load()) == other.data.load()
fn __ne__(self: Self, other: Array[Float32]) -> Bool:
return not rebind[Float32](self.data.load()) == other.data.load()
fn __ne__(self: Self, other: Array[Float64]) -> Bool:
return not rebind[Float64](self.data.load()) == other.data.load()
fn __iter__(self) -> ListIterator[T]:
return ListIterator[T](self.data, self.size)
fn __len__(borrowed self) -> Int:
return self.size
fn push_back(inout self: Self, item: T) raises:
if self.size >= self.cap:
self.resize(self.size + 1)
self.__setitem__(self.size, item)
self.size += 1
fn append(inout self: Self, item: T) raises:
self.push_back(item)
fn resize(inout self: Self, new_size: Int):
let new_cap = new_size * 2
let new_data = Pointer[T].alloc(new_cap)
for i in range(new_size):
new_data.store(i, self.data.load(i))
self.data.free()
self.data = new_data
self.size = new_size
self.cap = new_cap
fn remove_at(inout self, index: Int) raises:
if index >= self.size:
raise Error("Index out of bounds")
for i in range(index, self.size - 1):
self[i] = self[i + 1]
self.size -= 1
@value
@register_passable("trivial")
struct HashTable[T: AnyType]:
var size: Int
var data: Array[Array[Item[T]]]
var count: Int
fn __init__(size: Int) raises -> Self:
let table = Array[Array[Item[T]]](size)
for i in range(size):
table[i] = Array[Item[T]](0)
return Self {size: size, data: table, count: 0}
fn hash_function(self, key: StringRef) -> Int:
return hash_fn(key) % self.size
fn set(inout self: Self, key: StringRef, value: T) raises:
let hash_index = self.hash_function(key)
for i in range(self.data[hash_index].size):
if self.data[hash_index][i].key == key:
self.data[hash_index][i].set_value(value)
self.count += 1
if self.count > self.size:
self.resize()
return
let item = Item[T](key, value)
self.data[hash_index].data.store(self.data[hash_index].size, item)
self.data[hash_index].resize(self.data[hash_index].size + 1)
self.count += 1
if self.count > self.size:
self.resize()
fn contains(self: Self, key: StringRef) -> Bool:
# XXX: This doesn't work for some reason...
# but count should be 1 and it's 0
# if self.count == 0:
# return False
let hash_index = self.hash_function(key)
try:
for i in range(self.data[hash_index].size):
if self.data[hash_index][i].key == key:
return True
except:
pass
return False
@always_inline
fn get_item(inout self: Self, key: StringRef) raises -> Item[T]:
let hash_index = self.hash_function(key)
for i in range(self.data[hash_index].size):
if self.data[hash_index][i].key == key:
return self.data[hash_index][i]
raise Error(NOT_FOUND_ERROR)
fn get(self: Self, key: StringRef) raises -> T:
let hash_index = self.hash_function(key)
for i in range(self.data[hash_index].size):
if self.data[hash_index][i].key == key:
return rebind[T](self.data[hash_index][i].value)
raise Error(NOT_FOUND_ERROR)
fn __getitem__(self: Self, key: StringRef) raises -> T:
return self.get(key)
fn __setitem__(inout self: Self, key: StringRef, value: T) raises:
self.set(key, value)
fn delete(inout self: Self, key: StringRef) raises -> Bool:
let hash_index = self.hash_function(key)
for i in range(self.data[hash_index].size):
let item = self.data[hash_index][i]
if item.key == key:
self.data[hash_index].remove_at(i)
self.count -= 1
return True
return False
fn resize(inout self: Self) raises:
let old_table = self.data.data
self.size *= 2
self.data = Array[Array[Item[T]]](self.size)
for i in range(self.size):
let bucket = old_table[i]
if bucket != None:
for j in range(bucket.size):
let item = bucket[j]
self.set(item.key, item.value)
fn set_expire(inout self: Self, key: StringRef, expires: Int) raises:
let hash_index = self.hash_function(key)
for i in range(self.data[hash_index].size):
if self.data[hash_index][i].key == key:
self.data[hash_index][i].set_expire(key, expires)
return
raise Error(NOT_FOUND_ERROR)
fn to_string(inout self: Self) raises -> String:
var res: String = ""
# use later for multiple levels (a hash table inside of a hash table)
let indent = " "
res += "\n{\n"
res += self._to_string_attrs()
res += "}"
return res
fn _to_string_attrs(self: Self) raises -> String:
var res: String = ""
let indent = " "
for i in range(self.size):
let bucket = self.data[i]
for j in range(bucket.size):
let item = bucket[j]
res += indent
res += '"'
res += item.key
res += '"'
res += ": "
res += item.value_to_string()
res += ","
if j < bucket.size - 1:
res += "\n"
return res