-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhashtable.go
308 lines (193 loc) · 4.6 KB
/
hashtable.go
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
package main
type inserter func(int,int)
type selector int
func forEachKey(hash func(*,int) uint64, keys [], do func(*, int)) {
var void uint64
if len(keys)>0 {
var voidt [];
voidt = make([],1)
void = hash(&voidt[0], -1)
voidt = nil
}
for i := range keys {
var val *;
val = &keys[i]
if hash(val,-1) == void {
continue
}
do (val, i)
}
}
func slect(hash func(*, int) uint64, key *, table []) selector {
// next we look if the slot is occupied
var slot = hash(key, len(table))
var location *;
location = &table[slot]
var hloc = hash(location, -1)
// if it's occupied by a key
var hkey = hash(key, -1)
if hloc == hkey {
return selector(int(slot))
}
return -1
}
// Fast Search when you know the key exists
// If unsure, use Select
func fetch(hash func(*, int) uint64, key *, table []) selector {
return selector(int(hash(key, len(table))))
}
func (f selector) from(values []) * {
if f == -1 {
return nil
}
return &values[f]
}
func hashfun(key *[8]byte, size int) (o uint64) {
o |= uint64(key[0])
o |= uint64(key[1]) << 8
o |= uint64(key[2]) << 16
o |= uint64(key[3]) << 24
o |= uint64(key[4]) << 32
o |= uint64(key[5]) << 40
o |= uint64(key[6]) << 48
o |= uint64(key[7]) << 56
return o % uint64(size)
}
func (f inserter) into(hash func(*, int) uint64, key *, table *[]) {
const grow_by = 15
if len(*table) == 0 {
(*table) = make([], 1)
var loc *;
loc = &(*table)[0]
*loc = *key
f(1,-1)
f(0,-2)
f(0,-4)
return
}
// next we look if the slot is occupied
var slot = hash(key, len(*table))
var location *;
location = &(*table)[slot]
var hloc = hash(location, -1)
if hash(key, -1) == hloc {
f(int(slot),-4)
return
}
// we check if the slot is empty
var empty [];
empty = make([], 1)
var void *;
void = &empty[0]
var hvoid = hash(void, -1)
empty = nil
if hvoid == hloc {
*location = *key
f(int(slot),-4)
return
}
var oldsize = len(*table)
var newsize = len(*table)+grow_by
var keys [];
keys = make([], newsize)
f(newsize, -1)
for i := 0; i < oldsize; i++ {
var each *;
each = &(*table)[i]
var heach = hash(each, -1)
if hvoid == heach {
continue
}
heach = hash(each, newsize)
var newkey *;
newkey = &keys[heach]
var hnewkey = hash(newkey, -1)
if hvoid != hnewkey {
newsize += grow_by
keys = make([], newsize)
f(newsize, -1)
i = -1
continue
}
*newkey = *each
f(int(heach), i)
}
*table = keys
keys = nil
f(0,-2)
f.into(hash, key, table)
}
func insert(value *, values *[]) inserter {
var oldvalues []
_ = oldvalues
const resizeValues = -1 // save the old and make a new values array
const deleteValues = -2 // clean the old (saved) values array
const emptyElement = -3 // puts an empty element to slot in values array
const addedElement = -4 // puts the inserted value to a slot in values
return inserter( func(dst int, src int) {
var dstp *;
var srcp *;
switch (src) {
case resizeValues:
if oldvalues == nil {
oldvalues = *values
}
*values = make([], dst)
return
case deleteValues:
oldvalues = nil
return
case emptyElement:
var void [];
void = make([], 1)
_ = void
srcp = &void[0]
dstp = &(*values)[dst]
case addedElement:
srcp = value
dstp = &(*values)[dst]
default:
srcp = &oldvalues[src]
dstp = &(*values)[dst]
}
*dstp = *srcp
})
}
func main() {
var keys [][8]byte
var values [][2]uintptr
_ = values
var test = [2]uintptr{1337,7331}
insert(&test, &values).into(hashfun, &[8]byte{2,6,7,8,3,2,1,0}, &keys)
insert(&test, &values).into(hashfun, &[8]byte{2,6,7,8,3,2,1,0}, &keys)
insert(&test, &values).into(hashfun, &[8]byte{2,6,7,8,3,2,1,1}, &keys)
insert(&test, &values).into(hashfun, &[8]byte{2,6,7,8,3,2,1,2}, &keys)
insert(&test, &values).into(hashfun, &[8]byte{2,6,7,8,3,2,1,3}, &keys)
insert(&test, &values).into(hashfun, &[8]byte{2,6,7,8,3,2,1,4}, &keys)
insert(&test, &values).into(hashfun, &[8]byte{1,3,8,5,4,9,5,3}, &keys)
for i := 0; i < 250; i++ {
var oldsize = len(keys)
insert(&test, &values).into(hashfun, &[8]byte{1,3,8,5,4,9,5,byte(i)}, &keys)
if oldsize != len(keys) {
print("Reallocated to size ");
println(len(keys))
}
}
println("Here.")
forEachKey(hashfun, keys, func(key *[8]byte, i int) {
print(i)
print(" ")
print(values[i][0])
print(" ")
})
println("")
println("Searching.")
var a = slect(hashfun, &[8]byte{1,3,8,5,4,9,5,20}, keys).from(values)
var b *[2]uintptr = a
print((*b)[0])
println((*b)[1])
_ = a
if nil == slect(hashfun, &[8]byte{0,0,0,0,1,0,0,1}, keys).from(values) {
println("not found")
}
}