39348
|
1 |
(* ========================================================================= *)
|
|
2 |
(* FINITE MAPS WITH A FIXED KEY TYPE *)
|
39349
|
3 |
(* Copyright (c) 2004 Joe Hurd, distributed under the BSD License *)
|
39348
|
4 |
(* ========================================================================= *)
|
|
5 |
|
|
6 |
functor KeyMap (Key : Ordered) :> KeyMap where type key = Key.t =
|
|
7 |
struct
|
|
8 |
|
|
9 |
(* ------------------------------------------------------------------------- *)
|
|
10 |
(* Importing from the input signature. *)
|
|
11 |
(* ------------------------------------------------------------------------- *)
|
|
12 |
|
|
13 |
type key = Key.t;
|
|
14 |
|
|
15 |
val compareKey = Key.compare;
|
|
16 |
|
|
17 |
(* ------------------------------------------------------------------------- *)
|
|
18 |
(* Importing useful functionality. *)
|
|
19 |
(* ------------------------------------------------------------------------- *)
|
|
20 |
|
|
21 |
exception Bug = Useful.Bug;
|
|
22 |
|
|
23 |
exception Error = Useful.Error;
|
|
24 |
|
|
25 |
val pointerEqual = Portable.pointerEqual;
|
|
26 |
|
|
27 |
val K = Useful.K;
|
|
28 |
|
|
29 |
val randomInt = Portable.randomInt;
|
|
30 |
|
|
31 |
val randomWord = Portable.randomWord;
|
|
32 |
|
|
33 |
(* ------------------------------------------------------------------------- *)
|
|
34 |
(* Converting a comparison function to an equality function. *)
|
|
35 |
(* ------------------------------------------------------------------------- *)
|
|
36 |
|
|
37 |
fun equalKey key1 key2 = compareKey (key1,key2) = EQUAL;
|
|
38 |
|
|
39 |
(* ------------------------------------------------------------------------- *)
|
|
40 |
(* Priorities. *)
|
|
41 |
(* ------------------------------------------------------------------------- *)
|
|
42 |
|
|
43 |
type priority = Word.word;
|
|
44 |
|
|
45 |
val randomPriority = randomWord;
|
|
46 |
|
|
47 |
val comparePriority = Word.compare;
|
|
48 |
|
|
49 |
(* ------------------------------------------------------------------------- *)
|
|
50 |
(* Priority search trees. *)
|
|
51 |
(* ------------------------------------------------------------------------- *)
|
|
52 |
|
|
53 |
datatype 'value tree =
|
|
54 |
E
|
|
55 |
| T of 'value node
|
|
56 |
|
|
57 |
and 'value node =
|
|
58 |
Node of
|
|
59 |
{size : int,
|
|
60 |
priority : priority,
|
|
61 |
left : 'value tree,
|
|
62 |
key : key,
|
|
63 |
value : 'value,
|
|
64 |
right : 'value tree};
|
|
65 |
|
|
66 |
fun lowerPriorityNode node1 node2 =
|
|
67 |
let
|
|
68 |
val Node {priority = p1, ...} = node1
|
|
69 |
and Node {priority = p2, ...} = node2
|
|
70 |
in
|
|
71 |
comparePriority (p1,p2) = LESS
|
|
72 |
end;
|
|
73 |
|
|
74 |
(* ------------------------------------------------------------------------- *)
|
|
75 |
(* Tree debugging functions. *)
|
|
76 |
(* ------------------------------------------------------------------------- *)
|
|
77 |
|
|
78 |
(*BasicDebug
|
|
79 |
local
|
|
80 |
fun checkSizes tree =
|
|
81 |
case tree of
|
|
82 |
E => 0
|
|
83 |
| T (Node {size,left,right,...}) =>
|
|
84 |
let
|
|
85 |
val l = checkSizes left
|
|
86 |
and r = checkSizes right
|
|
87 |
|
|
88 |
val () = if l + 1 + r = size then () else raise Bug "wrong size"
|
|
89 |
in
|
|
90 |
size
|
|
91 |
end;
|
|
92 |
|
|
93 |
fun checkSorted x tree =
|
|
94 |
case tree of
|
|
95 |
E => x
|
|
96 |
| T (Node {left,key,right,...}) =>
|
|
97 |
let
|
|
98 |
val x = checkSorted x left
|
|
99 |
|
|
100 |
val () =
|
|
101 |
case x of
|
|
102 |
NONE => ()
|
|
103 |
| SOME k =>
|
|
104 |
case compareKey (k,key) of
|
|
105 |
LESS => ()
|
|
106 |
| EQUAL => raise Bug "duplicate keys"
|
|
107 |
| GREATER => raise Bug "unsorted"
|
|
108 |
|
|
109 |
val x = SOME key
|
|
110 |
in
|
|
111 |
checkSorted x right
|
|
112 |
end;
|
|
113 |
|
|
114 |
fun checkPriorities tree =
|
|
115 |
case tree of
|
|
116 |
E => NONE
|
|
117 |
| T node =>
|
|
118 |
let
|
|
119 |
val Node {left,right,...} = node
|
|
120 |
|
|
121 |
val () =
|
|
122 |
case checkPriorities left of
|
|
123 |
NONE => ()
|
|
124 |
| SOME lnode =>
|
|
125 |
if not (lowerPriorityNode node lnode) then ()
|
|
126 |
else raise Bug "left child has greater priority"
|
|
127 |
|
|
128 |
val () =
|
|
129 |
case checkPriorities right of
|
|
130 |
NONE => ()
|
|
131 |
| SOME rnode =>
|
|
132 |
if not (lowerPriorityNode node rnode) then ()
|
|
133 |
else raise Bug "right child has greater priority"
|
|
134 |
in
|
|
135 |
SOME node
|
|
136 |
end;
|
|
137 |
in
|
|
138 |
fun treeCheckInvariants tree =
|
|
139 |
let
|
|
140 |
val _ = checkSizes tree
|
|
141 |
|
|
142 |
val _ = checkSorted NONE tree
|
|
143 |
|
|
144 |
val _ = checkPriorities tree
|
|
145 |
in
|
|
146 |
tree
|
|
147 |
end
|
|
148 |
handle Error err => raise Bug err;
|
|
149 |
end;
|
|
150 |
*)
|
|
151 |
|
|
152 |
(* ------------------------------------------------------------------------- *)
|
|
153 |
(* Tree operations. *)
|
|
154 |
(* ------------------------------------------------------------------------- *)
|
|
155 |
|
|
156 |
fun treeNew () = E;
|
|
157 |
|
|
158 |
fun nodeSize (Node {size = x, ...}) = x;
|
|
159 |
|
|
160 |
fun treeSize tree =
|
|
161 |
case tree of
|
|
162 |
E => 0
|
|
163 |
| T x => nodeSize x;
|
|
164 |
|
|
165 |
fun mkNode priority left key value right =
|
|
166 |
let
|
|
167 |
val size = treeSize left + 1 + treeSize right
|
|
168 |
in
|
|
169 |
Node
|
|
170 |
{size = size,
|
|
171 |
priority = priority,
|
|
172 |
left = left,
|
|
173 |
key = key,
|
|
174 |
value = value,
|
|
175 |
right = right}
|
|
176 |
end;
|
|
177 |
|
|
178 |
fun mkTree priority left key value right =
|
|
179 |
let
|
|
180 |
val node = mkNode priority left key value right
|
|
181 |
in
|
|
182 |
T node
|
|
183 |
end;
|
|
184 |
|
|
185 |
(* ------------------------------------------------------------------------- *)
|
|
186 |
(* Extracting the left and right spines of a tree. *)
|
|
187 |
(* ------------------------------------------------------------------------- *)
|
|
188 |
|
|
189 |
fun treeLeftSpine acc tree =
|
|
190 |
case tree of
|
|
191 |
E => acc
|
|
192 |
| T node => nodeLeftSpine acc node
|
|
193 |
|
|
194 |
and nodeLeftSpine acc node =
|
|
195 |
let
|
|
196 |
val Node {left,...} = node
|
|
197 |
in
|
|
198 |
treeLeftSpine (node :: acc) left
|
|
199 |
end;
|
|
200 |
|
|
201 |
fun treeRightSpine acc tree =
|
|
202 |
case tree of
|
|
203 |
E => acc
|
|
204 |
| T node => nodeRightSpine acc node
|
|
205 |
|
|
206 |
and nodeRightSpine acc node =
|
|
207 |
let
|
|
208 |
val Node {right,...} = node
|
|
209 |
in
|
|
210 |
treeRightSpine (node :: acc) right
|
|
211 |
end;
|
|
212 |
|
|
213 |
(* ------------------------------------------------------------------------- *)
|
|
214 |
(* Singleton trees. *)
|
|
215 |
(* ------------------------------------------------------------------------- *)
|
|
216 |
|
|
217 |
fun mkNodeSingleton priority key value =
|
|
218 |
let
|
|
219 |
val size = 1
|
|
220 |
and left = E
|
|
221 |
and right = E
|
|
222 |
in
|
|
223 |
Node
|
|
224 |
{size = size,
|
|
225 |
priority = priority,
|
|
226 |
left = left,
|
|
227 |
key = key,
|
|
228 |
value = value,
|
|
229 |
right = right}
|
|
230 |
end;
|
|
231 |
|
|
232 |
fun nodeSingleton (key,value) =
|
|
233 |
let
|
|
234 |
val priority = randomPriority ()
|
|
235 |
in
|
|
236 |
mkNodeSingleton priority key value
|
|
237 |
end;
|
|
238 |
|
|
239 |
fun treeSingleton key_value =
|
|
240 |
let
|
|
241 |
val node = nodeSingleton key_value
|
|
242 |
in
|
|
243 |
T node
|
|
244 |
end;
|
|
245 |
|
|
246 |
(* ------------------------------------------------------------------------- *)
|
|
247 |
(* Appending two trees, where every element of the first tree is less than *)
|
|
248 |
(* every element of the second tree. *)
|
|
249 |
(* ------------------------------------------------------------------------- *)
|
|
250 |
|
|
251 |
fun treeAppend tree1 tree2 =
|
|
252 |
case tree1 of
|
|
253 |
E => tree2
|
|
254 |
| T node1 =>
|
|
255 |
case tree2 of
|
|
256 |
E => tree1
|
|
257 |
| T node2 =>
|
|
258 |
if lowerPriorityNode node1 node2 then
|
|
259 |
let
|
|
260 |
val Node {priority,left,key,value,right,...} = node2
|
|
261 |
|
|
262 |
val left = treeAppend tree1 left
|
|
263 |
in
|
|
264 |
mkTree priority left key value right
|
|
265 |
end
|
|
266 |
else
|
|
267 |
let
|
|
268 |
val Node {priority,left,key,value,right,...} = node1
|
|
269 |
|
|
270 |
val right = treeAppend right tree2
|
|
271 |
in
|
|
272 |
mkTree priority left key value right
|
|
273 |
end;
|
|
274 |
|
|
275 |
(* ------------------------------------------------------------------------- *)
|
|
276 |
(* Appending two trees and a node, where every element of the first tree is *)
|
|
277 |
(* less than the node, which in turn is less than every element of the *)
|
|
278 |
(* second tree. *)
|
|
279 |
(* ------------------------------------------------------------------------- *)
|
|
280 |
|
|
281 |
fun treeCombine left node right =
|
|
282 |
let
|
|
283 |
val left_node = treeAppend left (T node)
|
|
284 |
in
|
|
285 |
treeAppend left_node right
|
|
286 |
end;
|
|
287 |
|
|
288 |
(* ------------------------------------------------------------------------- *)
|
|
289 |
(* Searching a tree for a value. *)
|
|
290 |
(* ------------------------------------------------------------------------- *)
|
|
291 |
|
|
292 |
fun treePeek pkey tree =
|
|
293 |
case tree of
|
|
294 |
E => NONE
|
|
295 |
| T node => nodePeek pkey node
|
|
296 |
|
|
297 |
and nodePeek pkey node =
|
|
298 |
let
|
|
299 |
val Node {left,key,value,right,...} = node
|
|
300 |
in
|
|
301 |
case compareKey (pkey,key) of
|
|
302 |
LESS => treePeek pkey left
|
|
303 |
| EQUAL => SOME value
|
|
304 |
| GREATER => treePeek pkey right
|
|
305 |
end;
|
|
306 |
|
|
307 |
(* ------------------------------------------------------------------------- *)
|
|
308 |
(* Tree paths. *)
|
|
309 |
(* ------------------------------------------------------------------------- *)
|
|
310 |
|
|
311 |
(* Generating a path by searching a tree for a key/value pair *)
|
|
312 |
|
|
313 |
fun treePeekPath pkey path tree =
|
|
314 |
case tree of
|
|
315 |
E => (path,NONE)
|
|
316 |
| T node => nodePeekPath pkey path node
|
|
317 |
|
|
318 |
and nodePeekPath pkey path node =
|
|
319 |
let
|
|
320 |
val Node {left,key,right,...} = node
|
|
321 |
in
|
|
322 |
case compareKey (pkey,key) of
|
|
323 |
LESS => treePeekPath pkey ((true,node) :: path) left
|
|
324 |
| EQUAL => (path, SOME node)
|
|
325 |
| GREATER => treePeekPath pkey ((false,node) :: path) right
|
|
326 |
end;
|
|
327 |
|
|
328 |
(* A path splits a tree into left/right components *)
|
|
329 |
|
|
330 |
fun addSidePath ((wentLeft,node),(leftTree,rightTree)) =
|
|
331 |
let
|
|
332 |
val Node {priority,left,key,value,right,...} = node
|
|
333 |
in
|
|
334 |
if wentLeft then (leftTree, mkTree priority rightTree key value right)
|
|
335 |
else (mkTree priority left key value leftTree, rightTree)
|
|
336 |
end;
|
|
337 |
|
|
338 |
fun addSidesPath left_right = List.foldl addSidePath left_right;
|
|
339 |
|
|
340 |
fun mkSidesPath path = addSidesPath (E,E) path;
|
|
341 |
|
|
342 |
(* Updating the subtree at a path *)
|
|
343 |
|
|
344 |
local
|
|
345 |
fun updateTree ((wentLeft,node),tree) =
|
|
346 |
let
|
|
347 |
val Node {priority,left,key,value,right,...} = node
|
|
348 |
in
|
|
349 |
if wentLeft then mkTree priority tree key value right
|
|
350 |
else mkTree priority left key value tree
|
|
351 |
end;
|
|
352 |
in
|
|
353 |
fun updateTreePath tree = List.foldl updateTree tree;
|
|
354 |
end;
|
|
355 |
|
|
356 |
(* Inserting a new node at a path position *)
|
|
357 |
|
|
358 |
fun insertNodePath node =
|
|
359 |
let
|
|
360 |
fun insert left_right path =
|
|
361 |
case path of
|
|
362 |
[] =>
|
|
363 |
let
|
|
364 |
val (left,right) = left_right
|
|
365 |
in
|
|
366 |
treeCombine left node right
|
|
367 |
end
|
|
368 |
| (step as (_,snode)) :: rest =>
|
|
369 |
if lowerPriorityNode snode node then
|
|
370 |
let
|
|
371 |
val left_right = addSidePath (step,left_right)
|
|
372 |
in
|
|
373 |
insert left_right rest
|
|
374 |
end
|
|
375 |
else
|
|
376 |
let
|
|
377 |
val (left,right) = left_right
|
|
378 |
|
|
379 |
val tree = treeCombine left node right
|
|
380 |
in
|
|
381 |
updateTreePath tree path
|
|
382 |
end
|
|
383 |
in
|
|
384 |
insert (E,E)
|
|
385 |
end;
|
|
386 |
|
|
387 |
(* ------------------------------------------------------------------------- *)
|
|
388 |
(* Using a key to split a node into three components: the keys comparing *)
|
|
389 |
(* less than the supplied key, an optional equal key, and the keys comparing *)
|
|
390 |
(* greater. *)
|
|
391 |
(* ------------------------------------------------------------------------- *)
|
|
392 |
|
|
393 |
fun nodePartition pkey node =
|
|
394 |
let
|
|
395 |
val (path,pnode) = nodePeekPath pkey [] node
|
|
396 |
in
|
|
397 |
case pnode of
|
|
398 |
NONE =>
|
|
399 |
let
|
|
400 |
val (left,right) = mkSidesPath path
|
|
401 |
in
|
|
402 |
(left,NONE,right)
|
|
403 |
end
|
|
404 |
| SOME node =>
|
|
405 |
let
|
|
406 |
val Node {left,key,value,right,...} = node
|
|
407 |
|
|
408 |
val (left,right) = addSidesPath (left,right) path
|
|
409 |
in
|
|
410 |
(left, SOME (key,value), right)
|
|
411 |
end
|
|
412 |
end;
|
|
413 |
|
|
414 |
(* ------------------------------------------------------------------------- *)
|
|
415 |
(* Searching a tree for a key/value pair. *)
|
|
416 |
(* ------------------------------------------------------------------------- *)
|
|
417 |
|
|
418 |
fun treePeekKey pkey tree =
|
|
419 |
case tree of
|
|
420 |
E => NONE
|
|
421 |
| T node => nodePeekKey pkey node
|
|
422 |
|
|
423 |
and nodePeekKey pkey node =
|
|
424 |
let
|
|
425 |
val Node {left,key,value,right,...} = node
|
|
426 |
in
|
|
427 |
case compareKey (pkey,key) of
|
|
428 |
LESS => treePeekKey pkey left
|
|
429 |
| EQUAL => SOME (key,value)
|
|
430 |
| GREATER => treePeekKey pkey right
|
|
431 |
end;
|
|
432 |
|
|
433 |
(* ------------------------------------------------------------------------- *)
|
|
434 |
(* Inserting new key/values into the tree. *)
|
|
435 |
(* ------------------------------------------------------------------------- *)
|
|
436 |
|
|
437 |
fun treeInsert key_value tree =
|
|
438 |
let
|
|
439 |
val (key,value) = key_value
|
|
440 |
|
|
441 |
val (path,inode) = treePeekPath key [] tree
|
|
442 |
in
|
|
443 |
case inode of
|
|
444 |
NONE =>
|
|
445 |
let
|
|
446 |
val node = nodeSingleton (key,value)
|
|
447 |
in
|
|
448 |
insertNodePath node path
|
|
449 |
end
|
|
450 |
| SOME node =>
|
|
451 |
let
|
|
452 |
val Node {size,priority,left,right,...} = node
|
|
453 |
|
|
454 |
val node =
|
|
455 |
Node
|
|
456 |
{size = size,
|
|
457 |
priority = priority,
|
|
458 |
left = left,
|
|
459 |
key = key,
|
|
460 |
value = value,
|
|
461 |
right = right}
|
|
462 |
in
|
|
463 |
updateTreePath (T node) path
|
|
464 |
end
|
|
465 |
end;
|
|
466 |
|
|
467 |
(* ------------------------------------------------------------------------- *)
|
|
468 |
(* Deleting key/value pairs: it raises an exception if the supplied key is *)
|
|
469 |
(* not present. *)
|
|
470 |
(* ------------------------------------------------------------------------- *)
|
|
471 |
|
|
472 |
fun treeDelete dkey tree =
|
|
473 |
case tree of
|
|
474 |
E => raise Bug "KeyMap.delete: element not found"
|
|
475 |
| T node => nodeDelete dkey node
|
|
476 |
|
|
477 |
and nodeDelete dkey node =
|
|
478 |
let
|
|
479 |
val Node {size,priority,left,key,value,right} = node
|
|
480 |
in
|
|
481 |
case compareKey (dkey,key) of
|
|
482 |
LESS =>
|
|
483 |
let
|
|
484 |
val size = size - 1
|
|
485 |
and left = treeDelete dkey left
|
|
486 |
|
|
487 |
val node =
|
|
488 |
Node
|
|
489 |
{size = size,
|
|
490 |
priority = priority,
|
|
491 |
left = left,
|
|
492 |
key = key,
|
|
493 |
value = value,
|
|
494 |
right = right}
|
|
495 |
in
|
|
496 |
T node
|
|
497 |
end
|
|
498 |
| EQUAL => treeAppend left right
|
|
499 |
| GREATER =>
|
|
500 |
let
|
|
501 |
val size = size - 1
|
|
502 |
and right = treeDelete dkey right
|
|
503 |
|
|
504 |
val node =
|
|
505 |
Node
|
|
506 |
{size = size,
|
|
507 |
priority = priority,
|
|
508 |
left = left,
|
|
509 |
key = key,
|
|
510 |
value = value,
|
|
511 |
right = right}
|
|
512 |
in
|
|
513 |
T node
|
|
514 |
end
|
|
515 |
end;
|
|
516 |
|
|
517 |
(* ------------------------------------------------------------------------- *)
|
|
518 |
(* Partial map is the basic operation for preserving tree structure. *)
|
|
519 |
(* It applies its argument function to the elements *in order*. *)
|
|
520 |
(* ------------------------------------------------------------------------- *)
|
|
521 |
|
|
522 |
fun treeMapPartial f tree =
|
|
523 |
case tree of
|
|
524 |
E => E
|
|
525 |
| T node => nodeMapPartial f node
|
|
526 |
|
|
527 |
and nodeMapPartial f (Node {priority,left,key,value,right,...}) =
|
|
528 |
let
|
|
529 |
val left = treeMapPartial f left
|
|
530 |
and vo = f (key,value)
|
|
531 |
and right = treeMapPartial f right
|
|
532 |
in
|
|
533 |
case vo of
|
|
534 |
NONE => treeAppend left right
|
|
535 |
| SOME value => mkTree priority left key value right
|
|
536 |
end;
|
|
537 |
|
|
538 |
(* ------------------------------------------------------------------------- *)
|
|
539 |
(* Mapping tree values. *)
|
|
540 |
(* ------------------------------------------------------------------------- *)
|
|
541 |
|
|
542 |
fun treeMap f tree =
|
|
543 |
case tree of
|
|
544 |
E => E
|
|
545 |
| T node => T (nodeMap f node)
|
|
546 |
|
|
547 |
and nodeMap f node =
|
|
548 |
let
|
|
549 |
val Node {size,priority,left,key,value,right} = node
|
|
550 |
|
|
551 |
val left = treeMap f left
|
|
552 |
and value = f (key,value)
|
|
553 |
and right = treeMap f right
|
|
554 |
in
|
|
555 |
Node
|
|
556 |
{size = size,
|
|
557 |
priority = priority,
|
|
558 |
left = left,
|
|
559 |
key = key,
|
|
560 |
value = value,
|
|
561 |
right = right}
|
|
562 |
end;
|
|
563 |
|
|
564 |
(* ------------------------------------------------------------------------- *)
|
|
565 |
(* Merge is the basic operation for joining two trees. Note that the merged *)
|
|
566 |
(* key is always the one from the second map. *)
|
|
567 |
(* ------------------------------------------------------------------------- *)
|
|
568 |
|
|
569 |
fun treeMerge f1 f2 fb tree1 tree2 =
|
|
570 |
case tree1 of
|
|
571 |
E => treeMapPartial f2 tree2
|
|
572 |
| T node1 =>
|
|
573 |
case tree2 of
|
|
574 |
E => treeMapPartial f1 tree1
|
|
575 |
| T node2 => nodeMerge f1 f2 fb node1 node2
|
|
576 |
|
|
577 |
and nodeMerge f1 f2 fb node1 node2 =
|
|
578 |
let
|
|
579 |
val Node {priority,left,key,value,right,...} = node2
|
|
580 |
|
|
581 |
val (l,kvo,r) = nodePartition key node1
|
|
582 |
|
|
583 |
val left = treeMerge f1 f2 fb l left
|
|
584 |
and right = treeMerge f1 f2 fb r right
|
|
585 |
|
|
586 |
val vo =
|
|
587 |
case kvo of
|
|
588 |
NONE => f2 (key,value)
|
|
589 |
| SOME kv => fb (kv,(key,value))
|
|
590 |
in
|
|
591 |
case vo of
|
|
592 |
NONE => treeAppend left right
|
|
593 |
| SOME value =>
|
|
594 |
let
|
|
595 |
val node = mkNodeSingleton priority key value
|
|
596 |
in
|
|
597 |
treeCombine left node right
|
|
598 |
end
|
|
599 |
end;
|
|
600 |
|
|
601 |
(* ------------------------------------------------------------------------- *)
|
|
602 |
(* A union operation on trees. *)
|
|
603 |
(* ------------------------------------------------------------------------- *)
|
|
604 |
|
|
605 |
fun treeUnion f f2 tree1 tree2 =
|
|
606 |
case tree1 of
|
|
607 |
E => tree2
|
|
608 |
| T node1 =>
|
|
609 |
case tree2 of
|
|
610 |
E => tree1
|
|
611 |
| T node2 => nodeUnion f f2 node1 node2
|
|
612 |
|
|
613 |
and nodeUnion f f2 node1 node2 =
|
|
614 |
if pointerEqual (node1,node2) then nodeMapPartial f2 node1
|
|
615 |
else
|
|
616 |
let
|
|
617 |
val Node {priority,left,key,value,right,...} = node2
|
|
618 |
|
|
619 |
val (l,kvo,r) = nodePartition key node1
|
|
620 |
|
|
621 |
val left = treeUnion f f2 l left
|
|
622 |
and right = treeUnion f f2 r right
|
|
623 |
|
|
624 |
val vo =
|
|
625 |
case kvo of
|
|
626 |
NONE => SOME value
|
|
627 |
| SOME kv => f (kv,(key,value))
|
|
628 |
in
|
|
629 |
case vo of
|
|
630 |
NONE => treeAppend left right
|
|
631 |
| SOME value =>
|
|
632 |
let
|
|
633 |
val node = mkNodeSingleton priority key value
|
|
634 |
in
|
|
635 |
treeCombine left node right
|
|
636 |
end
|
|
637 |
end;
|
|
638 |
|
|
639 |
(* ------------------------------------------------------------------------- *)
|
|
640 |
(* An intersect operation on trees. *)
|
|
641 |
(* ------------------------------------------------------------------------- *)
|
|
642 |
|
|
643 |
fun treeIntersect f t1 t2 =
|
|
644 |
case t1 of
|
|
645 |
E => E
|
|
646 |
| T n1 =>
|
|
647 |
case t2 of
|
|
648 |
E => E
|
|
649 |
| T n2 => nodeIntersect f n1 n2
|
|
650 |
|
|
651 |
and nodeIntersect f n1 n2 =
|
|
652 |
let
|
|
653 |
val Node {priority,left,key,value,right,...} = n2
|
|
654 |
|
|
655 |
val (l,kvo,r) = nodePartition key n1
|
|
656 |
|
|
657 |
val left = treeIntersect f l left
|
|
658 |
and right = treeIntersect f r right
|
|
659 |
|
|
660 |
val vo =
|
|
661 |
case kvo of
|
|
662 |
NONE => NONE
|
|
663 |
| SOME kv => f (kv,(key,value))
|
|
664 |
in
|
|
665 |
case vo of
|
|
666 |
NONE => treeAppend left right
|
|
667 |
| SOME value => mkTree priority left key value right
|
|
668 |
end;
|
|
669 |
|
|
670 |
(* ------------------------------------------------------------------------- *)
|
|
671 |
(* A union operation on trees which simply chooses the second value. *)
|
|
672 |
(* ------------------------------------------------------------------------- *)
|
|
673 |
|
|
674 |
fun treeUnionDomain tree1 tree2 =
|
|
675 |
case tree1 of
|
|
676 |
E => tree2
|
|
677 |
| T node1 =>
|
|
678 |
case tree2 of
|
|
679 |
E => tree1
|
|
680 |
| T node2 =>
|
|
681 |
if pointerEqual (node1,node2) then tree2
|
|
682 |
else nodeUnionDomain node1 node2
|
|
683 |
|
|
684 |
and nodeUnionDomain node1 node2 =
|
|
685 |
let
|
|
686 |
val Node {priority,left,key,value,right,...} = node2
|
|
687 |
|
|
688 |
val (l,_,r) = nodePartition key node1
|
|
689 |
|
|
690 |
val left = treeUnionDomain l left
|
|
691 |
and right = treeUnionDomain r right
|
|
692 |
|
|
693 |
val node = mkNodeSingleton priority key value
|
|
694 |
in
|
|
695 |
treeCombine left node right
|
|
696 |
end;
|
|
697 |
|
|
698 |
(* ------------------------------------------------------------------------- *)
|
|
699 |
(* An intersect operation on trees which simply chooses the second value. *)
|
|
700 |
(* ------------------------------------------------------------------------- *)
|
|
701 |
|
|
702 |
fun treeIntersectDomain tree1 tree2 =
|
|
703 |
case tree1 of
|
|
704 |
E => E
|
|
705 |
| T node1 =>
|
|
706 |
case tree2 of
|
|
707 |
E => E
|
|
708 |
| T node2 =>
|
|
709 |
if pointerEqual (node1,node2) then tree2
|
|
710 |
else nodeIntersectDomain node1 node2
|
|
711 |
|
|
712 |
and nodeIntersectDomain node1 node2 =
|
|
713 |
let
|
|
714 |
val Node {priority,left,key,value,right,...} = node2
|
|
715 |
|
|
716 |
val (l,kvo,r) = nodePartition key node1
|
|
717 |
|
|
718 |
val left = treeIntersectDomain l left
|
|
719 |
and right = treeIntersectDomain r right
|
|
720 |
in
|
|
721 |
if Option.isSome kvo then mkTree priority left key value right
|
|
722 |
else treeAppend left right
|
|
723 |
end;
|
|
724 |
|
|
725 |
(* ------------------------------------------------------------------------- *)
|
|
726 |
(* A difference operation on trees. *)
|
|
727 |
(* ------------------------------------------------------------------------- *)
|
|
728 |
|
|
729 |
fun treeDifferenceDomain t1 t2 =
|
|
730 |
case t1 of
|
|
731 |
E => E
|
|
732 |
| T n1 =>
|
|
733 |
case t2 of
|
|
734 |
E => t1
|
|
735 |
| T n2 => nodeDifferenceDomain n1 n2
|
|
736 |
|
|
737 |
and nodeDifferenceDomain n1 n2 =
|
|
738 |
if pointerEqual (n1,n2) then E
|
|
739 |
else
|
|
740 |
let
|
|
741 |
val Node {priority,left,key,value,right,...} = n1
|
|
742 |
|
|
743 |
val (l,kvo,r) = nodePartition key n2
|
|
744 |
|
|
745 |
val left = treeDifferenceDomain left l
|
|
746 |
and right = treeDifferenceDomain right r
|
|
747 |
in
|
|
748 |
if Option.isSome kvo then treeAppend left right
|
|
749 |
else mkTree priority left key value right
|
|
750 |
end;
|
|
751 |
|
|
752 |
(* ------------------------------------------------------------------------- *)
|
|
753 |
(* A subset operation on trees. *)
|
|
754 |
(* ------------------------------------------------------------------------- *)
|
|
755 |
|
|
756 |
fun treeSubsetDomain tree1 tree2 =
|
|
757 |
case tree1 of
|
|
758 |
E => true
|
|
759 |
| T node1 =>
|
|
760 |
case tree2 of
|
|
761 |
E => false
|
|
762 |
| T node2 => nodeSubsetDomain node1 node2
|
|
763 |
|
|
764 |
and nodeSubsetDomain node1 node2 =
|
|
765 |
pointerEqual (node1,node2) orelse
|
|
766 |
let
|
|
767 |
val Node {size,left,key,right,...} = node1
|
|
768 |
in
|
|
769 |
size <= nodeSize node2 andalso
|
|
770 |
let
|
|
771 |
val (l,kvo,r) = nodePartition key node2
|
|
772 |
in
|
|
773 |
Option.isSome kvo andalso
|
|
774 |
treeSubsetDomain left l andalso
|
|
775 |
treeSubsetDomain right r
|
|
776 |
end
|
|
777 |
end;
|
|
778 |
|
|
779 |
(* ------------------------------------------------------------------------- *)
|
|
780 |
(* Picking an arbitrary key/value pair from a tree. *)
|
|
781 |
(* ------------------------------------------------------------------------- *)
|
|
782 |
|
|
783 |
fun nodePick node =
|
|
784 |
let
|
|
785 |
val Node {key,value,...} = node
|
|
786 |
in
|
|
787 |
(key,value)
|
|
788 |
end;
|
|
789 |
|
|
790 |
fun treePick tree =
|
|
791 |
case tree of
|
|
792 |
E => raise Bug "KeyMap.treePick"
|
|
793 |
| T node => nodePick node;
|
|
794 |
|
|
795 |
(* ------------------------------------------------------------------------- *)
|
|
796 |
(* Removing an arbitrary key/value pair from a tree. *)
|
|
797 |
(* ------------------------------------------------------------------------- *)
|
|
798 |
|
|
799 |
fun nodeDeletePick node =
|
|
800 |
let
|
|
801 |
val Node {left,key,value,right,...} = node
|
|
802 |
in
|
|
803 |
((key,value), treeAppend left right)
|
|
804 |
end;
|
|
805 |
|
|
806 |
fun treeDeletePick tree =
|
|
807 |
case tree of
|
|
808 |
E => raise Bug "KeyMap.treeDeletePick"
|
|
809 |
| T node => nodeDeletePick node;
|
|
810 |
|
|
811 |
(* ------------------------------------------------------------------------- *)
|
|
812 |
(* Finding the nth smallest key/value (counting from 0). *)
|
|
813 |
(* ------------------------------------------------------------------------- *)
|
|
814 |
|
|
815 |
fun treeNth n tree =
|
|
816 |
case tree of
|
|
817 |
E => raise Bug "KeyMap.treeNth"
|
|
818 |
| T node => nodeNth n node
|
|
819 |
|
|
820 |
and nodeNth n node =
|
|
821 |
let
|
|
822 |
val Node {left,key,value,right,...} = node
|
|
823 |
|
|
824 |
val k = treeSize left
|
|
825 |
in
|
|
826 |
if n = k then (key,value)
|
|
827 |
else if n < k then treeNth n left
|
|
828 |
else treeNth (n - (k + 1)) right
|
|
829 |
end;
|
|
830 |
|
|
831 |
(* ------------------------------------------------------------------------- *)
|
|
832 |
(* Removing the nth smallest key/value (counting from 0). *)
|
|
833 |
(* ------------------------------------------------------------------------- *)
|
|
834 |
|
|
835 |
fun treeDeleteNth n tree =
|
|
836 |
case tree of
|
|
837 |
E => raise Bug "KeyMap.treeDeleteNth"
|
|
838 |
| T node => nodeDeleteNth n node
|
|
839 |
|
|
840 |
and nodeDeleteNth n node =
|
|
841 |
let
|
|
842 |
val Node {size,priority,left,key,value,right} = node
|
|
843 |
|
|
844 |
val k = treeSize left
|
|
845 |
in
|
|
846 |
if n = k then ((key,value), treeAppend left right)
|
|
847 |
else if n < k then
|
|
848 |
let
|
|
849 |
val (key_value,left) = treeDeleteNth n left
|
|
850 |
|
|
851 |
val size = size - 1
|
|
852 |
|
|
853 |
val node =
|
|
854 |
Node
|
|
855 |
{size = size,
|
|
856 |
priority = priority,
|
|
857 |
left = left,
|
|
858 |
key = key,
|
|
859 |
value = value,
|
|
860 |
right = right}
|
|
861 |
in
|
|
862 |
(key_value, T node)
|
|
863 |
end
|
|
864 |
else
|
|
865 |
let
|
|
866 |
val n = n - (k + 1)
|
|
867 |
|
|
868 |
val (key_value,right) = treeDeleteNth n right
|
|
869 |
|
|
870 |
val size = size - 1
|
|
871 |
|
|
872 |
val node =
|
|
873 |
Node
|
|
874 |
{size = size,
|
|
875 |
priority = priority,
|
|
876 |
left = left,
|
|
877 |
key = key,
|
|
878 |
value = value,
|
|
879 |
right = right}
|
|
880 |
in
|
|
881 |
(key_value, T node)
|
|
882 |
end
|
|
883 |
end;
|
|
884 |
|
|
885 |
(* ------------------------------------------------------------------------- *)
|
|
886 |
(* Iterators. *)
|
|
887 |
(* ------------------------------------------------------------------------- *)
|
|
888 |
|
|
889 |
datatype 'value iterator =
|
|
890 |
LR of (key * 'value) * 'value tree * 'value node list
|
|
891 |
| RL of (key * 'value) * 'value tree * 'value node list;
|
|
892 |
|
|
893 |
fun fromSpineLR nodes =
|
|
894 |
case nodes of
|
|
895 |
[] => NONE
|
|
896 |
| Node {key,value,right,...} :: nodes =>
|
|
897 |
SOME (LR ((key,value),right,nodes));
|
|
898 |
|
|
899 |
fun fromSpineRL nodes =
|
|
900 |
case nodes of
|
|
901 |
[] => NONE
|
|
902 |
| Node {key,value,left,...} :: nodes =>
|
|
903 |
SOME (RL ((key,value),left,nodes));
|
|
904 |
|
|
905 |
fun addLR nodes tree = fromSpineLR (treeLeftSpine nodes tree);
|
|
906 |
|
|
907 |
fun addRL nodes tree = fromSpineRL (treeRightSpine nodes tree);
|
|
908 |
|
|
909 |
fun treeMkIterator tree = addLR [] tree;
|
|
910 |
|
|
911 |
fun treeMkRevIterator tree = addRL [] tree;
|
|
912 |
|
|
913 |
fun readIterator iter =
|
|
914 |
case iter of
|
|
915 |
LR (key_value,_,_) => key_value
|
|
916 |
| RL (key_value,_,_) => key_value;
|
|
917 |
|
|
918 |
fun advanceIterator iter =
|
|
919 |
case iter of
|
|
920 |
LR (_,tree,nodes) => addLR nodes tree
|
|
921 |
| RL (_,tree,nodes) => addRL nodes tree;
|
|
922 |
|
|
923 |
fun foldIterator f acc io =
|
|
924 |
case io of
|
|
925 |
NONE => acc
|
|
926 |
| SOME iter =>
|
|
927 |
let
|
|
928 |
val (key,value) = readIterator iter
|
|
929 |
in
|
|
930 |
foldIterator f (f (key,value,acc)) (advanceIterator iter)
|
|
931 |
end;
|
|
932 |
|
|
933 |
fun findIterator pred io =
|
|
934 |
case io of
|
|
935 |
NONE => NONE
|
|
936 |
| SOME iter =>
|
|
937 |
let
|
|
938 |
val key_value = readIterator iter
|
|
939 |
in
|
|
940 |
if pred key_value then SOME key_value
|
|
941 |
else findIterator pred (advanceIterator iter)
|
|
942 |
end;
|
|
943 |
|
|
944 |
fun firstIterator f io =
|
|
945 |
case io of
|
|
946 |
NONE => NONE
|
|
947 |
| SOME iter =>
|
|
948 |
let
|
|
949 |
val key_value = readIterator iter
|
|
950 |
in
|
|
951 |
case f key_value of
|
|
952 |
NONE => firstIterator f (advanceIterator iter)
|
|
953 |
| s => s
|
|
954 |
end;
|
|
955 |
|
|
956 |
fun compareIterator compareValue io1 io2 =
|
|
957 |
case (io1,io2) of
|
|
958 |
(NONE,NONE) => EQUAL
|
|
959 |
| (NONE, SOME _) => LESS
|
|
960 |
| (SOME _, NONE) => GREATER
|
|
961 |
| (SOME i1, SOME i2) =>
|
|
962 |
let
|
|
963 |
val (k1,v1) = readIterator i1
|
|
964 |
and (k2,v2) = readIterator i2
|
|
965 |
in
|
|
966 |
case compareKey (k1,k2) of
|
|
967 |
LESS => LESS
|
|
968 |
| EQUAL =>
|
|
969 |
(case compareValue (v1,v2) of
|
|
970 |
LESS => LESS
|
|
971 |
| EQUAL =>
|
|
972 |
let
|
|
973 |
val io1 = advanceIterator i1
|
|
974 |
and io2 = advanceIterator i2
|
|
975 |
in
|
|
976 |
compareIterator compareValue io1 io2
|
|
977 |
end
|
|
978 |
| GREATER => GREATER)
|
|
979 |
| GREATER => GREATER
|
|
980 |
end;
|
|
981 |
|
|
982 |
fun equalIterator equalValue io1 io2 =
|
|
983 |
case (io1,io2) of
|
|
984 |
(NONE,NONE) => true
|
|
985 |
| (NONE, SOME _) => false
|
|
986 |
| (SOME _, NONE) => false
|
|
987 |
| (SOME i1, SOME i2) =>
|
|
988 |
let
|
|
989 |
val (k1,v1) = readIterator i1
|
|
990 |
and (k2,v2) = readIterator i2
|
|
991 |
in
|
|
992 |
equalKey k1 k2 andalso
|
|
993 |
equalValue v1 v2 andalso
|
|
994 |
let
|
|
995 |
val io1 = advanceIterator i1
|
|
996 |
and io2 = advanceIterator i2
|
|
997 |
in
|
|
998 |
equalIterator equalValue io1 io2
|
|
999 |
end
|
|
1000 |
end;
|
|
1001 |
|
|
1002 |
(* ------------------------------------------------------------------------- *)
|
|
1003 |
(* A type of finite maps. *)
|
|
1004 |
(* ------------------------------------------------------------------------- *)
|
|
1005 |
|
|
1006 |
datatype 'value map =
|
|
1007 |
Map of 'value tree;
|
|
1008 |
|
|
1009 |
(* ------------------------------------------------------------------------- *)
|
|
1010 |
(* Map debugging functions. *)
|
|
1011 |
(* ------------------------------------------------------------------------- *)
|
|
1012 |
|
|
1013 |
(*BasicDebug
|
|
1014 |
fun checkInvariants s m =
|
|
1015 |
let
|
|
1016 |
val Map tree = m
|
|
1017 |
|
|
1018 |
val _ = treeCheckInvariants tree
|
|
1019 |
in
|
|
1020 |
m
|
|
1021 |
end
|
|
1022 |
handle Bug bug => raise Bug (s ^ "\n" ^ "KeyMap.checkInvariants: " ^ bug);
|
|
1023 |
*)
|
|
1024 |
|
|
1025 |
(* ------------------------------------------------------------------------- *)
|
|
1026 |
(* Constructors. *)
|
|
1027 |
(* ------------------------------------------------------------------------- *)
|
|
1028 |
|
|
1029 |
fun new () =
|
|
1030 |
let
|
|
1031 |
val tree = treeNew ()
|
|
1032 |
in
|
|
1033 |
Map tree
|
|
1034 |
end;
|
|
1035 |
|
|
1036 |
fun singleton key_value =
|
|
1037 |
let
|
|
1038 |
val tree = treeSingleton key_value
|
|
1039 |
in
|
|
1040 |
Map tree
|
|
1041 |
end;
|
|
1042 |
|
|
1043 |
(* ------------------------------------------------------------------------- *)
|
|
1044 |
(* Map size. *)
|
|
1045 |
(* ------------------------------------------------------------------------- *)
|
|
1046 |
|
|
1047 |
fun size (Map tree) = treeSize tree;
|
|
1048 |
|
|
1049 |
fun null m = size m = 0;
|
|
1050 |
|
|
1051 |
(* ------------------------------------------------------------------------- *)
|
|
1052 |
(* Querying. *)
|
|
1053 |
(* ------------------------------------------------------------------------- *)
|
|
1054 |
|
|
1055 |
fun peekKey (Map tree) key = treePeekKey key tree;
|
|
1056 |
|
|
1057 |
fun peek (Map tree) key = treePeek key tree;
|
|
1058 |
|
|
1059 |
fun inDomain key m = Option.isSome (peek m key);
|
|
1060 |
|
|
1061 |
fun get m key =
|
|
1062 |
case peek m key of
|
|
1063 |
NONE => raise Error "KeyMap.get: element not found"
|
|
1064 |
| SOME value => value;
|
|
1065 |
|
|
1066 |
fun pick (Map tree) = treePick tree;
|
|
1067 |
|
|
1068 |
fun nth (Map tree) n = treeNth n tree;
|
|
1069 |
|
|
1070 |
fun random m =
|
|
1071 |
let
|
|
1072 |
val n = size m
|
|
1073 |
in
|
|
1074 |
if n = 0 then raise Bug "KeyMap.random: empty"
|
|
1075 |
else nth m (randomInt n)
|
|
1076 |
end;
|
|
1077 |
|
|
1078 |
(* ------------------------------------------------------------------------- *)
|
|
1079 |
(* Adding. *)
|
|
1080 |
(* ------------------------------------------------------------------------- *)
|
|
1081 |
|
|
1082 |
fun insert (Map tree) key_value =
|
|
1083 |
let
|
|
1084 |
val tree = treeInsert key_value tree
|
|
1085 |
in
|
|
1086 |
Map tree
|
|
1087 |
end;
|
|
1088 |
|
|
1089 |
(*BasicDebug
|
|
1090 |
val insert = fn m => fn kv =>
|
|
1091 |
checkInvariants "KeyMap.insert: result"
|
|
1092 |
(insert (checkInvariants "KeyMap.insert: input" m) kv);
|
|
1093 |
*)
|
|
1094 |
|
|
1095 |
fun insertList m =
|
|
1096 |
let
|
|
1097 |
fun ins (key_value,acc) = insert acc key_value
|
|
1098 |
in
|
|
1099 |
List.foldl ins m
|
|
1100 |
end;
|
|
1101 |
|
|
1102 |
(* ------------------------------------------------------------------------- *)
|
|
1103 |
(* Removing. *)
|
|
1104 |
(* ------------------------------------------------------------------------- *)
|
|
1105 |
|
|
1106 |
fun delete (Map tree) dkey =
|
|
1107 |
let
|
|
1108 |
val tree = treeDelete dkey tree
|
|
1109 |
in
|
|
1110 |
Map tree
|
|
1111 |
end;
|
|
1112 |
|
|
1113 |
(*BasicDebug
|
|
1114 |
val delete = fn m => fn k =>
|
|
1115 |
checkInvariants "KeyMap.delete: result"
|
|
1116 |
(delete (checkInvariants "KeyMap.delete: input" m) k);
|
|
1117 |
*)
|
|
1118 |
|
|
1119 |
fun remove m key = if inDomain key m then delete m key else m;
|
|
1120 |
|
|
1121 |
fun deletePick (Map tree) =
|
|
1122 |
let
|
|
1123 |
val (key_value,tree) = treeDeletePick tree
|
|
1124 |
in
|
|
1125 |
(key_value, Map tree)
|
|
1126 |
end;
|
|
1127 |
|
|
1128 |
(*BasicDebug
|
|
1129 |
val deletePick = fn m =>
|
|
1130 |
let
|
|
1131 |
val (kv,m) = deletePick (checkInvariants "KeyMap.deletePick: input" m)
|
|
1132 |
in
|
|
1133 |
(kv, checkInvariants "KeyMap.deletePick: result" m)
|
|
1134 |
end;
|
|
1135 |
*)
|
|
1136 |
|
|
1137 |
fun deleteNth (Map tree) n =
|
|
1138 |
let
|
|
1139 |
val (key_value,tree) = treeDeleteNth n tree
|
|
1140 |
in
|
|
1141 |
(key_value, Map tree)
|
|
1142 |
end;
|
|
1143 |
|
|
1144 |
(*BasicDebug
|
|
1145 |
val deleteNth = fn m => fn n =>
|
|
1146 |
let
|
|
1147 |
val (kv,m) = deleteNth (checkInvariants "KeyMap.deleteNth: input" m) n
|
|
1148 |
in
|
|
1149 |
(kv, checkInvariants "KeyMap.deleteNth: result" m)
|
|
1150 |
end;
|
|
1151 |
*)
|
|
1152 |
|
|
1153 |
fun deleteRandom m =
|
|
1154 |
let
|
|
1155 |
val n = size m
|
|
1156 |
in
|
|
1157 |
if n = 0 then raise Bug "KeyMap.deleteRandom: empty"
|
|
1158 |
else deleteNth m (randomInt n)
|
|
1159 |
end;
|
|
1160 |
|
|
1161 |
(* ------------------------------------------------------------------------- *)
|
|
1162 |
(* Joining (all join operations prefer keys in the second map). *)
|
|
1163 |
(* ------------------------------------------------------------------------- *)
|
|
1164 |
|
|
1165 |
fun merge {first,second,both} (Map tree1) (Map tree2) =
|
|
1166 |
let
|
|
1167 |
val tree = treeMerge first second both tree1 tree2
|
|
1168 |
in
|
|
1169 |
Map tree
|
|
1170 |
end;
|
|
1171 |
|
|
1172 |
(*BasicDebug
|
|
1173 |
val merge = fn f => fn m1 => fn m2 =>
|
|
1174 |
checkInvariants "KeyMap.merge: result"
|
|
1175 |
(merge f
|
|
1176 |
(checkInvariants "KeyMap.merge: input 1" m1)
|
|
1177 |
(checkInvariants "KeyMap.merge: input 2" m2));
|
|
1178 |
*)
|
|
1179 |
|
|
1180 |
fun union f (Map tree1) (Map tree2) =
|
|
1181 |
let
|
|
1182 |
fun f2 kv = f (kv,kv)
|
|
1183 |
|
|
1184 |
val tree = treeUnion f f2 tree1 tree2
|
|
1185 |
in
|
|
1186 |
Map tree
|
|
1187 |
end;
|
|
1188 |
|
|
1189 |
(*BasicDebug
|
|
1190 |
val union = fn f => fn m1 => fn m2 =>
|
|
1191 |
checkInvariants "KeyMap.union: result"
|
|
1192 |
(union f
|
|
1193 |
(checkInvariants "KeyMap.union: input 1" m1)
|
|
1194 |
(checkInvariants "KeyMap.union: input 2" m2));
|
|
1195 |
*)
|
|
1196 |
|
|
1197 |
fun intersect f (Map tree1) (Map tree2) =
|
|
1198 |
let
|
|
1199 |
val tree = treeIntersect f tree1 tree2
|
|
1200 |
in
|
|
1201 |
Map tree
|
|
1202 |
end;
|
|
1203 |
|
|
1204 |
(*BasicDebug
|
|
1205 |
val intersect = fn f => fn m1 => fn m2 =>
|
|
1206 |
checkInvariants "KeyMap.intersect: result"
|
|
1207 |
(intersect f
|
|
1208 |
(checkInvariants "KeyMap.intersect: input 1" m1)
|
|
1209 |
(checkInvariants "KeyMap.intersect: input 2" m2));
|
|
1210 |
*)
|
|
1211 |
|
|
1212 |
(* ------------------------------------------------------------------------- *)
|
|
1213 |
(* Iterators over maps. *)
|
|
1214 |
(* ------------------------------------------------------------------------- *)
|
|
1215 |
|
|
1216 |
fun mkIterator (Map tree) = treeMkIterator tree;
|
|
1217 |
|
|
1218 |
fun mkRevIterator (Map tree) = treeMkRevIterator tree;
|
|
1219 |
|
|
1220 |
(* ------------------------------------------------------------------------- *)
|
|
1221 |
(* Mapping and folding. *)
|
|
1222 |
(* ------------------------------------------------------------------------- *)
|
|
1223 |
|
|
1224 |
fun mapPartial f (Map tree) =
|
|
1225 |
let
|
|
1226 |
val tree = treeMapPartial f tree
|
|
1227 |
in
|
|
1228 |
Map tree
|
|
1229 |
end;
|
|
1230 |
|
|
1231 |
(*BasicDebug
|
|
1232 |
val mapPartial = fn f => fn m =>
|
|
1233 |
checkInvariants "KeyMap.mapPartial: result"
|
|
1234 |
(mapPartial f (checkInvariants "KeyMap.mapPartial: input" m));
|
|
1235 |
*)
|
|
1236 |
|
|
1237 |
fun map f (Map tree) =
|
|
1238 |
let
|
|
1239 |
val tree = treeMap f tree
|
|
1240 |
in
|
|
1241 |
Map tree
|
|
1242 |
end;
|
|
1243 |
|
|
1244 |
(*BasicDebug
|
|
1245 |
val map = fn f => fn m =>
|
|
1246 |
checkInvariants "KeyMap.map: result"
|
|
1247 |
(map f (checkInvariants "KeyMap.map: input" m));
|
|
1248 |
*)
|
|
1249 |
|
|
1250 |
fun transform f = map (fn (_,value) => f value);
|
|
1251 |
|
|
1252 |
fun filter pred =
|
|
1253 |
let
|
|
1254 |
fun f (key_value as (_,value)) =
|
|
1255 |
if pred key_value then SOME value else NONE
|
|
1256 |
in
|
|
1257 |
mapPartial f
|
|
1258 |
end;
|
|
1259 |
|
|
1260 |
fun partition p =
|
|
1261 |
let
|
|
1262 |
fun np x = not (p x)
|
|
1263 |
in
|
|
1264 |
fn m => (filter p m, filter np m)
|
|
1265 |
end;
|
|
1266 |
|
|
1267 |
fun foldl f b m = foldIterator f b (mkIterator m);
|
|
1268 |
|
|
1269 |
fun foldr f b m = foldIterator f b (mkRevIterator m);
|
|
1270 |
|
|
1271 |
fun app f m = foldl (fn (key,value,()) => f (key,value)) () m;
|
|
1272 |
|
|
1273 |
(* ------------------------------------------------------------------------- *)
|
|
1274 |
(* Searching. *)
|
|
1275 |
(* ------------------------------------------------------------------------- *)
|
|
1276 |
|
|
1277 |
fun findl p m = findIterator p (mkIterator m);
|
|
1278 |
|
|
1279 |
fun findr p m = findIterator p (mkRevIterator m);
|
|
1280 |
|
|
1281 |
fun firstl f m = firstIterator f (mkIterator m);
|
|
1282 |
|
|
1283 |
fun firstr f m = firstIterator f (mkRevIterator m);
|
|
1284 |
|
|
1285 |
fun exists p m = Option.isSome (findl p m);
|
|
1286 |
|
|
1287 |
fun all p =
|
|
1288 |
let
|
|
1289 |
fun np x = not (p x)
|
|
1290 |
in
|
|
1291 |
fn m => not (exists np m)
|
|
1292 |
end;
|
|
1293 |
|
|
1294 |
fun count pred =
|
|
1295 |
let
|
|
1296 |
fun f (k,v,acc) = if pred (k,v) then acc + 1 else acc
|
|
1297 |
in
|
|
1298 |
foldl f 0
|
|
1299 |
end;
|
|
1300 |
|
|
1301 |
(* ------------------------------------------------------------------------- *)
|
|
1302 |
(* Comparing. *)
|
|
1303 |
(* ------------------------------------------------------------------------- *)
|
|
1304 |
|
|
1305 |
fun compare compareValue (m1,m2) =
|
|
1306 |
if pointerEqual (m1,m2) then EQUAL
|
|
1307 |
else
|
|
1308 |
case Int.compare (size m1, size m2) of
|
|
1309 |
LESS => LESS
|
|
1310 |
| EQUAL =>
|
|
1311 |
let
|
|
1312 |
val Map _ = m1
|
|
1313 |
|
|
1314 |
val io1 = mkIterator m1
|
|
1315 |
and io2 = mkIterator m2
|
|
1316 |
in
|
|
1317 |
compareIterator compareValue io1 io2
|
|
1318 |
end
|
|
1319 |
| GREATER => GREATER;
|
|
1320 |
|
|
1321 |
fun equal equalValue m1 m2 =
|
|
1322 |
pointerEqual (m1,m2) orelse
|
|
1323 |
(size m1 = size m2 andalso
|
|
1324 |
let
|
|
1325 |
val Map _ = m1
|
|
1326 |
|
|
1327 |
val io1 = mkIterator m1
|
|
1328 |
and io2 = mkIterator m2
|
|
1329 |
in
|
|
1330 |
equalIterator equalValue io1 io2
|
|
1331 |
end);
|
|
1332 |
|
|
1333 |
(* ------------------------------------------------------------------------- *)
|
|
1334 |
(* Set operations on the domain. *)
|
|
1335 |
(* ------------------------------------------------------------------------- *)
|
|
1336 |
|
|
1337 |
fun unionDomain (Map tree1) (Map tree2) =
|
|
1338 |
let
|
|
1339 |
val tree = treeUnionDomain tree1 tree2
|
|
1340 |
in
|
|
1341 |
Map tree
|
|
1342 |
end;
|
|
1343 |
|
|
1344 |
(*BasicDebug
|
|
1345 |
val unionDomain = fn m1 => fn m2 =>
|
|
1346 |
checkInvariants "KeyMap.unionDomain: result"
|
|
1347 |
(unionDomain
|
|
1348 |
(checkInvariants "KeyMap.unionDomain: input 1" m1)
|
|
1349 |
(checkInvariants "KeyMap.unionDomain: input 2" m2));
|
|
1350 |
*)
|
|
1351 |
|
|
1352 |
local
|
|
1353 |
fun uncurriedUnionDomain (m,acc) = unionDomain acc m;
|
|
1354 |
in
|
|
1355 |
fun unionListDomain ms =
|
|
1356 |
case ms of
|
|
1357 |
[] => raise Bug "KeyMap.unionListDomain: no sets"
|
|
1358 |
| m :: ms => List.foldl uncurriedUnionDomain m ms;
|
|
1359 |
end;
|
|
1360 |
|
|
1361 |
fun intersectDomain (Map tree1) (Map tree2) =
|
|
1362 |
let
|
|
1363 |
val tree = treeIntersectDomain tree1 tree2
|
|
1364 |
in
|
|
1365 |
Map tree
|
|
1366 |
end;
|
|
1367 |
|
|
1368 |
(*BasicDebug
|
|
1369 |
val intersectDomain = fn m1 => fn m2 =>
|
|
1370 |
checkInvariants "KeyMap.intersectDomain: result"
|
|
1371 |
(intersectDomain
|
|
1372 |
(checkInvariants "KeyMap.intersectDomain: input 1" m1)
|
|
1373 |
(checkInvariants "KeyMap.intersectDomain: input 2" m2));
|
|
1374 |
*)
|
|
1375 |
|
|
1376 |
local
|
|
1377 |
fun uncurriedIntersectDomain (m,acc) = intersectDomain acc m;
|
|
1378 |
in
|
|
1379 |
fun intersectListDomain ms =
|
|
1380 |
case ms of
|
|
1381 |
[] => raise Bug "KeyMap.intersectListDomain: no sets"
|
|
1382 |
| m :: ms => List.foldl uncurriedIntersectDomain m ms;
|
|
1383 |
end;
|
|
1384 |
|
|
1385 |
fun differenceDomain (Map tree1) (Map tree2) =
|
|
1386 |
let
|
|
1387 |
val tree = treeDifferenceDomain tree1 tree2
|
|
1388 |
in
|
|
1389 |
Map tree
|
|
1390 |
end;
|
|
1391 |
|
|
1392 |
(*BasicDebug
|
|
1393 |
val differenceDomain = fn m1 => fn m2 =>
|
|
1394 |
checkInvariants "KeyMap.differenceDomain: result"
|
|
1395 |
(differenceDomain
|
|
1396 |
(checkInvariants "KeyMap.differenceDomain: input 1" m1)
|
|
1397 |
(checkInvariants "KeyMap.differenceDomain: input 2" m2));
|
|
1398 |
*)
|
|
1399 |
|
|
1400 |
fun symmetricDifferenceDomain m1 m2 =
|
|
1401 |
unionDomain (differenceDomain m1 m2) (differenceDomain m2 m1);
|
|
1402 |
|
|
1403 |
fun equalDomain m1 m2 = equal (K (K true)) m1 m2;
|
|
1404 |
|
|
1405 |
fun subsetDomain (Map tree1) (Map tree2) =
|
|
1406 |
treeSubsetDomain tree1 tree2;
|
|
1407 |
|
|
1408 |
fun disjointDomain m1 m2 = null (intersectDomain m1 m2);
|
|
1409 |
|
|
1410 |
(* ------------------------------------------------------------------------- *)
|
|
1411 |
(* Converting to and from lists. *)
|
|
1412 |
(* ------------------------------------------------------------------------- *)
|
|
1413 |
|
|
1414 |
fun keys m = foldr (fn (key,_,l) => key :: l) [] m;
|
|
1415 |
|
|
1416 |
fun values m = foldr (fn (_,value,l) => value :: l) [] m;
|
|
1417 |
|
|
1418 |
fun toList m = foldr (fn (key,value,l) => (key,value) :: l) [] m;
|
|
1419 |
|
|
1420 |
fun fromList l =
|
|
1421 |
let
|
|
1422 |
val m = new ()
|
|
1423 |
in
|
|
1424 |
insertList m l
|
|
1425 |
end;
|
|
1426 |
|
|
1427 |
(* ------------------------------------------------------------------------- *)
|
|
1428 |
(* Pretty-printing. *)
|
|
1429 |
(* ------------------------------------------------------------------------- *)
|
|
1430 |
|
|
1431 |
fun toString m = "<" ^ (if null m then "" else Int.toString (size m)) ^ ">";
|
|
1432 |
|
|
1433 |
end
|
|
1434 |
|
|
1435 |
structure IntMap =
|
|
1436 |
KeyMap (IntOrdered);
|
|
1437 |
|
|
1438 |
structure IntPairMap =
|
|
1439 |
KeyMap (IntPairOrdered);
|
|
1440 |
|
|
1441 |
structure StringMap =
|
|
1442 |
KeyMap (StringOrdered);
|