-
Notifications
You must be signed in to change notification settings - Fork 14
/
hash-table-dataless.sml
executable file
·326 lines (284 loc) · 10.3 KB
/
hash-table-dataless.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
functor DatalessHashTable (structure Key : HASHABLE)
:>
DATALESS_HASH_TABLE
where type key = Key.t
=
struct
type init = int
type key = Key.t
datatype entry =
Nil
| Cons of word * key * entry ref
datatype bucket =
Zero
| One of key
| Many of entry ref (* invariant: length >=2 *)
type table =
{ residents : int ref, (* current number of residents *)
size : word,
thresh : int, (* number of residents at which to resize *)
arr : bucket array } ref
exception Absent
fun resizeLoad n = n div 4 * 3
fun initial sz =
if sz <= 0 then
raise (Fail "illegal size")
else
{ 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 { residents, ... } : table) = !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 { residents, size, thresh, arr, ... } : table) =
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 =
let
val n = Word.toInt (hash mod newsize')
in
(case Array.sub (arr', n) of
Zero =>
Array.update (arr', n, One key)
| One key' =>
Array.update (arr', n,
Many (ref (Cons (hash, key,
ref (Cons (Key.hash key', key',
ref Nil))))))
| Many lr =>
Array.update (arr', n, Many (ref (Cons (hash, key, lr)))))
end
fun moveList lr =
(case !lr of
Nil => ()
| Cons (hash, key, rest) =>
(
moveInto hash key;
moveList rest
))
fun move bucket =
(case bucket of
Zero => ()
| One key =>
moveInto (Key.hash key) key
| Many lr =>
moveList lr)
in
(* Move entries to new array. *)
Array.app move arr;
table := { residents = residents,
size = newsize',
thresh = resizeLoad newsize,
arr = arr' }
end
fun member (ref { size, arr, ... } : table) key =
let
val hash = Key.hash key
val n = Word.toInt (hash mod 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 swap (table as ref { residents, size, arr, ... } : table) key =
let
val hash = Key.hash key
val n = Word.toInt (hash mod size)
in
(case Array.sub (arr, n) of
Zero =>
(
Array.update (arr, n, One key);
residents := !residents + 1;
resize table;
NONE
)
| One key' =>
if Key.eq (key, key') then
(
Array.update (arr, n, One key);
SOME key'
)
else
(
Array.update (arr, n,
Many (ref (Cons (hash, key,
ref (Cons (Key.hash key', key',
ref Nil))))));
residents := !residents + 1;
resize table;
NONE
)
| Many lr =>
(case findEntry lr hash key of
ref Nil =>
(
Array.update (arr, n,
Many (ref (Cons (hash, key, lr))));
residents := !residents + 1;
resize table;
NONE
)
| lr' as ref (Cons (_, key', rest)) =>
(
lr' := Cons (hash, key, rest);
SOME key'
)))
end
fun insert table key = (swap table key; ())
fun remove (table as ref { residents, size, arr, ... } : table) key =
let
val hash = Key.hash key
val n = Word.toInt (hash mod 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', ref Nil) =>
Array.update (arr, n, One key')
| Nil =>
raise (Fail "invariant")
| _ => ());
residents := !residents - 1
)))
end
fun lookup (table as ref { size, arr, ... } : table) key =
let
val hash = Key.hash key
val n = Word.toInt (hash mod size)
in
(case Array.sub (arr, n) of
Zero =>
raise Absent
| One key' =>
if Key.eq (key, key') then
key'
else
raise Absent
| Many lr =>
(case findEntry lr hash key of
ref Nil =>
raise Absent
| ref (Cons (_, key', _)) =>
key'))
end
fun find table key =
(SOME (lookup table key)
handle Absent => NONE)
fun lookupOrInsert' (table as ref { residents, size, arr, ... } : table) key =
let
val hash = Key.hash key
val n = Word.toInt (hash mod size)
in
(case Array.sub (arr, n) of
Zero =>
(
Array.update (arr, n, One key);
residents := !residents + 1;
resize table;
(key, false)
)
| One key' =>
if Key.eq (key, key') then
(key', true)
else
(
Array.update (arr, n,
Many (ref (Cons (hash, key,
ref (Cons (Key.hash key', key',
ref Nil))))));
residents := !residents + 1;
resize table;
(key, false)
)
| Many lr =>
(case findEntry lr hash key of
ref Nil =>
(
Array.update (arr, n,
Many (ref (Cons (hash, key, lr))));
residents := !residents + 1;
resize table;
(key, false)
)
| lr' as ref (Cons (_, key', rest)) =>
(key', true)))
end
fun lookupOrInsert table key = #1 (lookupOrInsert' table key)
fun foldEntry f x entry =
(case entry of
Nil => x
| Cons (_, key, ref next) =>
foldEntry f (f (key, x)) next)
fun fold f x (ref { arr, ... } : table) =
Array.foldl
(fn (Zero, acc) => acc
| (One key, acc) => f (key, acc)
| (Many (ref l), acc) => foldEntry f acc l)
x
arr
fun foldEntryLazy f rem entry =
(case entry of
Nil => Susp.force rem
| Cons (_, key, ref next) =>
f (key,
Susp.delay (fn () => foldEntryLazy f rem next)))
fun foldLazy f x (ref { arr, ... } : table) =
ArrayUtil.foldlLazy
(fn (Zero, rem) => Susp.force rem
| (One key, rem) => f (key, rem)
| (Many (ref l), rem) => foldEntryLazy f rem l)
x
arr
fun toList table =
fold (fn (key, l) => key :: l) [] table
fun appEntry f entry =
(case entry of
Nil => ()
| Cons (_, key, ref next) =>
(
f key;
appEntry f next
))
fun app f (ref { arr, ... } : table) =
Array.app
(fn Zero => ()
| One key => f key
| Many (ref l) => appEntry f l)
arr
end