-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathdict-list.sml
executable file
·332 lines (268 loc) · 9.42 KB
/
dict-list.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
functor ListDict (structure Key : ORDERED)
:> DICT where type key = Key.t
=
struct
type key = Key.t
type 'a dict = (key * 'a) list
exception Absent
val empty = []
val isEmpty = List.null
val size = List.length
fun singleton key x = [(key, x)]
fun insert l key x =
(case l of
[] => [(key, x)]
| (key', y) :: rest =>
(case Key.compare (key, key') of
LESS =>
(key, x) :: l
| EQUAL =>
(key, x) :: rest
| GREATER =>
(key', y) :: insert rest key x))
fun insert' l key x =
(case l of
[] =>
([(key, x)], false)
| (key', y) :: rest =>
(case Key.compare (key, key') of
LESS =>
((key, x) :: l, false)
| EQUAL =>
((key, x) :: rest, true)
| GREATER =>
let
val (d, present) = insert' rest key x
in
((key', y) :: d, present)
end))
fun remove l key =
(case l of
[] => []
| (key', y) :: rest =>
(case Key.compare (key, key') of
LESS => l
| EQUAL => rest
| GREATER =>
(key', y) :: remove rest key))
fun remove' l key =
(case l of
[] => ([], false)
| (key', y) :: rest =>
(case Key.compare (key, key') of
LESS => (l, false)
| EQUAL => (rest, true)
| GREATER =>
let
val (d, present) = remove' rest key
in
((key', y) :: d, present)
end))
fun operate' l key absentf presentf =
(case l of
[] =>
(case absentf () of
NONE =>
(NONE, NONE, [])
| z as SOME x =>
(NONE, z, [(key, x)]))
| (key', y) :: rest =>
(case Key.compare (key, key') of
LESS =>
(case absentf () of
NONE =>
(NONE, NONE, l)
| z as SOME x =>
(NONE, z, (key, x) :: l))
| EQUAL =>
(case presentf y of
NONE =>
(SOME y, NONE, rest)
| z as SOME x =>
(SOME y, z, (key, x) :: rest))
| GREATER =>
let
val (ante, post, rest') = operate' rest key absentf presentf
in
(ante, post, (key', y) :: rest')
end))
fun operate dict key absentf presentf =
let
val (x, y, d) = operate' dict key (SOME o absentf) (SOME o presentf)
in
(x, valOf y, d)
end
fun insertMerge dict key x f =
let
val (_, _, z) =
operate' dict key (fn () => SOME x) (SOME o f)
in
z
end
fun find l key =
(case l of
[] =>
NONE
| (key', x) :: rest =>
(case Key.compare (key, key') of
LESS =>
NONE
| EQUAL =>
SOME x
| GREATER =>
find rest key))
fun lookup l key =
(case l of
[] =>
raise Absent
| (key', x) :: rest =>
(case Key.compare (key, key') of
LESS =>
raise Absent
| EQUAL =>
x
| GREATER =>
lookup rest key))
fun member l key =
(case l of
[] =>
false
| (key', _) :: rest =>
(case Key.compare (key, key') of
LESS =>
false
| EQUAL =>
true
| GREATER =>
member rest key))
fun toList l = l
fun domain l = List.map (fn (key, _) => key) l
fun map f l = List.map (fn (key, x) => (key, f x)) l
fun map' f l = List.map (fn (kx as (key, x)) => (key, f kx)) l
fun foldl f base l = List.foldl (fn ((key, x), y) => f (key, x, y)) base l
fun foldr f base l = List.foldr (fn ((key, x), y) => f (key, x, y)) base l
val app = List.app
fun union l1 l2 merge =
(case (l1, l2) of
(nil, _) => l2
| (_, nil) => l1
| ((key1, x) :: rest1, (key2, y) :: rest2) =>
(case Key.compare (key1, key2) of
LESS =>
(key1, x) :: union rest1 l2 merge
| EQUAL =>
(key1, merge (key1, x, y)) :: union rest1 rest2 merge
| GREATER =>
(key2, y) :: union l1 rest2 merge))
fun split key acc l =
(case l of
nil =>
(acc, NONE, nil)
| (item as (key', _)) :: rest =>
(case Key.compare (key, key') of
LESS =>
(acc, NONE, l)
| EQUAL =>
(acc, SOME item, rest)
| GREATER =>
split key (item :: acc) rest))
fun partition l key =
(case split key nil l of
(leftr, NONE, right) => (rev leftr, NONE, right)
| (leftr, SOME (_, x), right) => (rev leftr, SOME x, right))
fun partitionlt l key =
(case split key nil l of
(leftr, NONE, right) => (rev leftr, right)
| (leftr, SOME item, right) => (rev leftr, item :: right))
fun partitiongt l key =
(case split key nil l of
(leftr, NONE, right) => (rev leftr, right)
| (leftr, SOME item, right) => (rev (item :: leftr), right))
fun rangeii tree left right =
let
val (_, tree') = partitionlt tree left
val (tree'', _) = partitiongt tree' right
in
tree''
end
fun rangeie tree left right =
let
val (_, tree') = partitionlt tree left
val (tree'', _) = partitionlt tree' right
in
tree''
end
fun rangeei tree left right =
let
val (_, tree') = partitiongt tree left
val (tree'', _) = partitiongt tree' right
in
tree''
end
fun rangeee tree left right =
let
val (_, tree') = partitiongt tree left
val (tree'', _) = partitionlt tree' right
in
tree''
end
fun least l =
(case l of
nil => raise Absent
| (key, x) :: _ => (key, x))
fun greatest l =
(case l of
nil => raise Absent
| [(key, x)] => (key, x)
| _ :: t => greatest t)
fun leastGt l key =
(case l of
nil => raise Absent
| (key', x) :: rest =>
(case Key.compare (key', key) of
GREATER => (key', x)
| _ => leastGt rest key))
fun leastGeq l key =
(case l of
nil => raise Absent
| (key', x) :: rest =>
(case Key.compare (key', key) of
LESS => leastGeq rest key
| _ => (key', x)))
fun greatestLt l key =
let
fun loop prev l =
(case l of
nil => prev
| (key', x) :: rest =>
(case Key.compare (key', key) of
LESS =>
loop (key', x) rest
| _ => prev))
in
(case l of
nil => raise Absent
| (key', x) :: rest =>
(case Key.compare (key', key) of
LESS =>
loop (key', x) rest
| _ => raise Absent))
end
fun greatestLeq l key =
let
fun loop prev l =
(case l of
nil => prev
| (key', x) :: rest =>
(case Key.compare (key', key) of
GREATER => prev
| _ => loop (key', x) rest))
in
(case l of
nil => raise Absent
| (key', x) :: rest =>
(case Key.compare (key', key) of
GREATER => raise Absent
| _ => loop (key', x) rest))
end
end