-
Notifications
You must be signed in to change notification settings - Fork 14
/
dict-hash.sml
51 lines (41 loc) · 1.3 KB
/
dict-hash.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
(* For types where we'd like to minimize the number of comparisons. *)
functor HashDict (structure Key : HASHABLE
structure Dict : DICT where type key = Key.t
structure WordDict : DICT where type key = Word.word)
:> MINI_DICT where type key = Key.t
=
struct
type key = Dict.key
type 'a dict = 'a Dict.dict WordDict.dict
val empty = WordDict.empty
fun insert wd key x =
let
val (_, _, wd') =
WordDict.operate wd (Key.hash key)
(fn () => Dict.singleton key x)
(fn kd => Dict.insert kd key x)
in
wd'
end
fun remove wd key =
let
val (_, _, wd') =
WordDict.operate' wd (Key.hash key)
(fn () => NONE)
(fn kd =>
let
val kd' = Dict.remove kd key
in
if Dict.isEmpty kd' then
NONE
else
SOME kd'
end)
in
wd'
end
fun find wd key =
(case WordDict.find wd (Key.hash key) of
NONE => NONE
| SOME kd => Dict.find kd key)
end