-
Notifications
You must be signed in to change notification settings - Fork 14
/
hash-table.sml
executable file
·381 lines (336 loc) · 12.9 KB
/
hash-table.sml
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
functor HashTable (structure Key : HASHABLE)
:> HASH_TABLE where type key = Key.t
=
struct
type init = int
type key = Key.t
datatype 'a entry =
Nil
| Cons of word * key * 'a * 'a entry ref
datatype 'a bucket =
Zero
| One of key * 'a
| Many of 'a entry ref (* invariant: length >=2 *)
datatype 'a pretable =
T of { residents : int ref, (* current number of residents *)
size : word,
thresh : int, (* number of residents at which to resize *)
arr : 'a bucket array }
type 'a table = 'a pretable ref
exception Absent
fun resizeLoad n = n div 4 * 3
fun initial sz =
if sz <= 0 then
raise (Fail "illegal size")
else
T { residents = ref 0,
size = Word.fromInt sz,
thresh = resizeLoad sz,
arr = Array.array (sz, Zero) }
fun table sz = ref (initial sz)
fun reset table sz =
table := initial sz
fun size (ref (T { residents, ... })) = !residents
fun findEntry lr hash key =
(case !lr of
Nil =>
lr
| Cons (hash', key', _, rest) =>
if hash = hash' andalso Key.eq (key, key') then
lr
else
findEntry rest hash key)
fun resize (table as ref (T { residents, size, thresh, arr, ... })) =
if !residents < thresh then
()
else
let
val newsize = 2 * Word.toInt size
val newsize' = Word.fromInt newsize
val arr' = Array.array (newsize, Zero)
fun moveInto hash key datum =
let
val n = Word.toInt (Word.mod (hash, newsize'))
in
(case Array.sub (arr', n) of
Zero =>
Array.update (arr', n, One (key, datum))
| One (key', datum') =>
Array.update (arr', n,
Many (ref (Cons (hash, key, datum,
ref (Cons (Key.hash key', key', datum',
ref Nil))))))
| Many lr =>
Array.update (arr', n, Many (ref (Cons (hash, key, datum, lr)))))
end
fun moveList lr =
(case !lr of
Nil => ()
| Cons (hash, key, datum, rest) =>
(
moveInto hash key datum;
moveList rest
))
fun move bucket =
(case bucket of
Zero => ()
| One (key, datum) =>
moveInto (Key.hash key) key datum
| Many lr =>
moveList lr)
in
(* Move entries to new array. *)
Array.app move arr;
table := T { residents = residents,
size = newsize',
thresh = resizeLoad newsize,
arr = arr' }
end
fun member (ref (T { size, arr, ... })) key =
let
val hash = Key.hash key
val n = Word.toInt (Word.mod (hash, size))
in
(case Array.sub (arr, n) of
Zero => false
| One (key', _) =>
Key.eq (key, key')
| Many lr =>
(case !(findEntry lr hash key) of
Nil => false
| Cons _ => true))
end
fun insert (table as ref (T { residents, size, arr, ... })) key datum =
let
val hash = Key.hash key
val n = Word.toInt (Word.mod (hash, size))
in
(case Array.sub (arr, n) of
Zero =>
(
Array.update (arr, n,
One (key, datum));
residents := !residents + 1;
resize table
)
| One (key', datum') =>
if Key.eq (key, key') then
Array.update (arr, n, One (key, datum))
else
(
Array.update (arr, n,
Many (ref (Cons (hash, key, datum,
ref (Cons (Key.hash key', key', datum',
ref Nil))))));
residents := !residents + 1;
resize table
)
| Many lr =>
(case findEntry lr hash key of
ref Nil =>
(
Array.update (arr, n,
Many (ref (Cons (hash, key, datum, lr))));
residents := !residents + 1;
resize table
)
| lr' as ref (Cons (_, _, _, rest)) =>
lr' := Cons (hash, key, datum, rest)))
end
fun remove (ref (T { residents, size, arr, ... })) key =
let
val hash = Key.hash key
val n = Word.toInt (Word.mod (hash, size))
in
(case Array.sub (arr, n) of
Zero => ()
| One (key', _) =>
if Key.eq (key, key') then
(
Array.update (arr, n, Zero);
residents := !residents - 1
)
else
()
| Many lr =>
(case findEntry lr hash key of
ref Nil => ()
| lr' as ref (Cons (_, _, _, rest)) =>
(
lr' := !rest;
(case !lr of
Cons (_, key', datum', ref Nil) =>
Array.update (arr, n, One (key', datum'))
| Nil =>
raise (Fail "invariant")
| _ => ());
residents := !residents - 1
)))
end
fun lookup (ref (T { size, arr, ... })) key =
let
val hash = Key.hash key
val n = Word.toInt (Word.mod (hash, size))
in
(case Array.sub (arr, n) of
Zero =>
raise Absent
| One (key', datum) =>
if Key.eq (key, key') then
datum
else
raise Absent
| Many lr =>
(case findEntry lr hash key of
ref Nil =>
raise Absent
| ref (Cons (_, _, datum, _)) =>
datum))
end
fun find table key =
(SOME (lookup table key)
handle Absent => NONE)
fun operate' (table as ref (T { residents, size, arr, ... })) key absentf presentf =
let
val hash = Key.hash key
val n = Word.toInt (Word.mod (hash, size))
in
(case Array.sub (arr, n) of
Zero =>
let
val datumo = absentf ()
in
(case datumo of
NONE => ()
| SOME datum =>
(
Array.update (arr, n, One (key, datum));
residents := !residents + 1;
resize table
));
(NONE, datumo)
end
| One (key', datum') =>
if Key.eq (key, key') then
let
val datumo = presentf datum'
in
(case datumo of
NONE =>
(
Array.update (arr, n, Zero);
residents := !residents - 1
)
| SOME datum =>
Array.update (arr, n, One (key, datum)));
(SOME datum', datumo)
end
else
let
val datumo = absentf ()
in
(case datumo of
NONE =>
()
| SOME datum =>
(
Array.update (arr, n,
Many (ref (Cons (hash, key, datum,
ref (Cons (Key.hash key', key', datum',
ref Nil))))));
residents := !residents + 1;
resize table
));
(NONE, datumo)
end
| Many lr =>
(case findEntry lr hash key of
ref Nil =>
let
val datumo = absentf ()
in
(case datumo of
NONE => ()
| SOME datum =>
(
Array.update (arr, n,
Many (ref (Cons (hash, key, datum, lr))));
residents := !residents + 1;
resize table
));
(NONE, datumo)
end
| lr' as ref (Cons (_, _, datum', rest)) =>
let
val datumo = presentf datum'
in
(case datumo of
NONE =>
(
lr' := !rest;
(case !lr of
Cons (_, key'', datum'', ref Nil) =>
Array.update (arr, n, One (key'', datum''))
| Nil =>
raise (Fail "invariant")
| _ => ());
residents := !residents - 1
)
| SOME datum =>
lr' := Cons (hash, key, datum, rest));
(SOME datum', datumo)
end))
end
fun operate table key absentf presentf =
let
val (x, y) = operate' table key (SOME o absentf) (SOME o presentf)
in
(x, valOf y)
end
fun insertMerge table key x f =
let
val _ = operate' table key (fn () => SOME x) (SOME o f)
in
()
end
fun lookupOrInsert table key datumf =
let
val (x, y) = operate' table key (SOME o datumf) (fn x => SOME x)
in
valOf y
end
fun lookupOrInsert' table key datumf =
let
val (x, y) = operate' table key (SOME o datumf) (fn x => SOME x)
in
(valOf y, isSome x)
end
fun foldEntry f x entry =
(case entry of
Nil => x
| Cons (_, key, datum, ref next) =>
foldEntry f (f (key, datum, x)) next)
fun fold f x (ref (T { arr, ... })) =
Array.foldl
(fn (Zero, acc) => acc
| (One (key, datum), acc) => f (key, datum, acc)
| (Many (ref l), acc) => foldEntry f acc l)
x
arr
fun toList table =
fold (fn (key, datum, l) => (key, datum) :: l) [] table
fun appEntry f entry =
(case entry of
Nil => ()
| Cons (_, key, datum, ref next) =>
(
f (key, datum);
appEntry f next
))
fun app f (ref (T { arr, ... })) =
Array.app
(fn Zero => ()
| One (key, datum) => f (key, datum)
| Many (ref l) => appEntry f l)
arr
end