39348
|
1 |
(* ========================================================================= *)
|
|
2 |
(* FINITE SETS WITH A FIXED ELEMENT TYPE *)
|
72004
|
3 |
(* Copyright (c) 2004 Joe Leslie-Hurd, distributed under the BSD License *)
|
39348
|
4 |
(* ========================================================================= *)
|
|
5 |
|
43269
|
6 |
functor ElementSet (
|
|
7 |
KM : KeyMap
|
|
8 |
) :> ElementSet
|
|
9 |
where type element = KM.key
|
|
10 |
and type 'a map = 'a KM.map =
|
39348
|
11 |
struct
|
|
12 |
|
|
13 |
(* ------------------------------------------------------------------------- *)
|
|
14 |
(* A type of set elements. *)
|
|
15 |
(* ------------------------------------------------------------------------- *)
|
|
16 |
|
|
17 |
type element = KM.key;
|
|
18 |
|
45778
|
19 |
val compareElement = KM.compareKey;
|
|
20 |
|
|
21 |
val equalElement = KM.equalKey;
|
|
22 |
|
39348
|
23 |
(* ------------------------------------------------------------------------- *)
|
|
24 |
(* A type of finite sets. *)
|
|
25 |
(* ------------------------------------------------------------------------- *)
|
|
26 |
|
|
27 |
type 'a map = 'a KM.map;
|
|
28 |
|
|
29 |
datatype set = Set of unit map;
|
|
30 |
|
|
31 |
(* ------------------------------------------------------------------------- *)
|
|
32 |
(* Converting to and from maps. *)
|
|
33 |
(* ------------------------------------------------------------------------- *)
|
|
34 |
|
|
35 |
fun dest (Set m) = m;
|
|
36 |
|
|
37 |
fun mapPartial f =
|
|
38 |
let
|
|
39 |
fun mf (elt,()) = f elt
|
|
40 |
in
|
|
41 |
fn Set m => KM.mapPartial mf m
|
|
42 |
end;
|
|
43 |
|
|
44 |
fun map f =
|
|
45 |
let
|
|
46 |
fun mf (elt,()) = f elt
|
|
47 |
in
|
|
48 |
fn Set m => KM.map mf m
|
|
49 |
end;
|
|
50 |
|
|
51 |
fun domain m = Set (KM.transform (fn _ => ()) m);
|
|
52 |
|
|
53 |
(* ------------------------------------------------------------------------- *)
|
|
54 |
(* Constructors. *)
|
|
55 |
(* ------------------------------------------------------------------------- *)
|
|
56 |
|
|
57 |
val empty = Set (KM.new ());
|
|
58 |
|
|
59 |
fun singleton elt = Set (KM.singleton (elt,()));
|
|
60 |
|
|
61 |
(* ------------------------------------------------------------------------- *)
|
|
62 |
(* Set size. *)
|
|
63 |
(* ------------------------------------------------------------------------- *)
|
|
64 |
|
|
65 |
fun null (Set m) = KM.null m;
|
|
66 |
|
|
67 |
fun size (Set m) = KM.size m;
|
|
68 |
|
|
69 |
(* ------------------------------------------------------------------------- *)
|
|
70 |
(* Querying. *)
|
|
71 |
(* ------------------------------------------------------------------------- *)
|
|
72 |
|
|
73 |
fun peek (Set m) elt =
|
|
74 |
case KM.peekKey m elt of
|
|
75 |
SOME (elt,()) => SOME elt
|
|
76 |
| NONE => NONE;
|
|
77 |
|
|
78 |
fun member elt (Set m) = KM.inDomain elt m;
|
|
79 |
|
|
80 |
fun pick (Set m) =
|
|
81 |
let
|
|
82 |
val (elt,_) = KM.pick m
|
|
83 |
in
|
|
84 |
elt
|
|
85 |
end;
|
|
86 |
|
|
87 |
fun nth (Set m) n =
|
|
88 |
let
|
|
89 |
val (elt,_) = KM.nth m n
|
|
90 |
in
|
|
91 |
elt
|
|
92 |
end;
|
|
93 |
|
|
94 |
fun random (Set m) =
|
|
95 |
let
|
|
96 |
val (elt,_) = KM.random m
|
|
97 |
in
|
|
98 |
elt
|
|
99 |
end;
|
|
100 |
|
|
101 |
(* ------------------------------------------------------------------------- *)
|
|
102 |
(* Adding. *)
|
|
103 |
(* ------------------------------------------------------------------------- *)
|
|
104 |
|
|
105 |
fun add (Set m) elt =
|
|
106 |
let
|
|
107 |
val m = KM.insert m (elt,())
|
|
108 |
in
|
|
109 |
Set m
|
|
110 |
end;
|
|
111 |
|
|
112 |
local
|
|
113 |
fun uncurriedAdd (elt,set) = add set elt;
|
|
114 |
in
|
|
115 |
fun addList set = List.foldl uncurriedAdd set;
|
|
116 |
end;
|
|
117 |
|
|
118 |
(* ------------------------------------------------------------------------- *)
|
|
119 |
(* Removing. *)
|
|
120 |
(* ------------------------------------------------------------------------- *)
|
|
121 |
|
|
122 |
fun delete (Set m) elt =
|
|
123 |
let
|
|
124 |
val m = KM.delete m elt
|
|
125 |
in
|
|
126 |
Set m
|
|
127 |
end;
|
|
128 |
|
|
129 |
fun remove (Set m) elt =
|
|
130 |
let
|
|
131 |
val m = KM.remove m elt
|
|
132 |
in
|
|
133 |
Set m
|
|
134 |
end;
|
|
135 |
|
|
136 |
fun deletePick (Set m) =
|
|
137 |
let
|
|
138 |
val ((elt,()),m) = KM.deletePick m
|
|
139 |
in
|
|
140 |
(elt, Set m)
|
|
141 |
end;
|
|
142 |
|
|
143 |
fun deleteNth (Set m) n =
|
|
144 |
let
|
|
145 |
val ((elt,()),m) = KM.deleteNth m n
|
|
146 |
in
|
|
147 |
(elt, Set m)
|
|
148 |
end;
|
|
149 |
|
|
150 |
fun deleteRandom (Set m) =
|
|
151 |
let
|
|
152 |
val ((elt,()),m) = KM.deleteRandom m
|
|
153 |
in
|
|
154 |
(elt, Set m)
|
|
155 |
end;
|
|
156 |
|
|
157 |
(* ------------------------------------------------------------------------- *)
|
|
158 |
(* Joining. *)
|
|
159 |
(* ------------------------------------------------------------------------- *)
|
|
160 |
|
|
161 |
fun union (Set m1) (Set m2) = Set (KM.unionDomain m1 m2);
|
|
162 |
|
|
163 |
fun unionList sets =
|
|
164 |
let
|
|
165 |
val ms = List.map dest sets
|
|
166 |
in
|
|
167 |
Set (KM.unionListDomain ms)
|
|
168 |
end;
|
|
169 |
|
|
170 |
fun intersect (Set m1) (Set m2) = Set (KM.intersectDomain m1 m2);
|
|
171 |
|
|
172 |
fun intersectList sets =
|
|
173 |
let
|
|
174 |
val ms = List.map dest sets
|
|
175 |
in
|
|
176 |
Set (KM.intersectListDomain ms)
|
|
177 |
end;
|
|
178 |
|
|
179 |
fun difference (Set m1) (Set m2) =
|
|
180 |
Set (KM.differenceDomain m1 m2);
|
|
181 |
|
|
182 |
fun symmetricDifference (Set m1) (Set m2) =
|
|
183 |
Set (KM.symmetricDifferenceDomain m1 m2);
|
|
184 |
|
|
185 |
(* ------------------------------------------------------------------------- *)
|
|
186 |
(* Mapping and folding. *)
|
|
187 |
(* ------------------------------------------------------------------------- *)
|
|
188 |
|
|
189 |
fun filter pred =
|
|
190 |
let
|
|
191 |
fun mpred (elt,()) = pred elt
|
|
192 |
in
|
|
193 |
fn Set m => Set (KM.filter mpred m)
|
|
194 |
end;
|
|
195 |
|
|
196 |
fun partition pred =
|
|
197 |
let
|
|
198 |
fun mpred (elt,()) = pred elt
|
|
199 |
in
|
|
200 |
fn Set m =>
|
|
201 |
let
|
|
202 |
val (m1,m2) = KM.partition mpred m
|
|
203 |
in
|
|
204 |
(Set m1, Set m2)
|
|
205 |
end
|
|
206 |
end;
|
|
207 |
|
|
208 |
fun app f =
|
|
209 |
let
|
|
210 |
fun mf (elt,()) = f elt
|
|
211 |
in
|
|
212 |
fn Set m => KM.app mf m
|
|
213 |
end;
|
|
214 |
|
|
215 |
fun foldl f =
|
|
216 |
let
|
|
217 |
fun mf (elt,(),acc) = f (elt,acc)
|
|
218 |
in
|
|
219 |
fn acc => fn Set m => KM.foldl mf acc m
|
|
220 |
end;
|
|
221 |
|
|
222 |
fun foldr f =
|
|
223 |
let
|
|
224 |
fun mf (elt,(),acc) = f (elt,acc)
|
|
225 |
in
|
|
226 |
fn acc => fn Set m => KM.foldr mf acc m
|
|
227 |
end;
|
|
228 |
|
|
229 |
(* ------------------------------------------------------------------------- *)
|
|
230 |
(* Searching. *)
|
|
231 |
(* ------------------------------------------------------------------------- *)
|
|
232 |
|
|
233 |
fun findl p =
|
|
234 |
let
|
|
235 |
fun mp (elt,()) = p elt
|
|
236 |
in
|
|
237 |
fn Set m =>
|
|
238 |
case KM.findl mp m of
|
|
239 |
SOME (elt,()) => SOME elt
|
|
240 |
| NONE => NONE
|
|
241 |
end;
|
|
242 |
|
|
243 |
fun findr p =
|
|
244 |
let
|
|
245 |
fun mp (elt,()) = p elt
|
|
246 |
in
|
|
247 |
fn Set m =>
|
|
248 |
case KM.findr mp m of
|
|
249 |
SOME (elt,()) => SOME elt
|
|
250 |
| NONE => NONE
|
|
251 |
end;
|
|
252 |
|
|
253 |
fun firstl f =
|
|
254 |
let
|
|
255 |
fun mf (elt,()) = f elt
|
|
256 |
in
|
|
257 |
fn Set m => KM.firstl mf m
|
|
258 |
end;
|
|
259 |
|
|
260 |
fun firstr f =
|
|
261 |
let
|
|
262 |
fun mf (elt,()) = f elt
|
|
263 |
in
|
|
264 |
fn Set m => KM.firstr mf m
|
|
265 |
end;
|
|
266 |
|
|
267 |
fun exists p =
|
|
268 |
let
|
|
269 |
fun mp (elt,()) = p elt
|
|
270 |
in
|
|
271 |
fn Set m => KM.exists mp m
|
|
272 |
end;
|
|
273 |
|
|
274 |
fun all p =
|
|
275 |
let
|
|
276 |
fun mp (elt,()) = p elt
|
|
277 |
in
|
|
278 |
fn Set m => KM.all mp m
|
|
279 |
end;
|
|
280 |
|
|
281 |
fun count p =
|
|
282 |
let
|
|
283 |
fun mp (elt,()) = p elt
|
|
284 |
in
|
|
285 |
fn Set m => KM.count mp m
|
|
286 |
end;
|
|
287 |
|
|
288 |
(* ------------------------------------------------------------------------- *)
|
|
289 |
(* Comparing. *)
|
|
290 |
(* ------------------------------------------------------------------------- *)
|
|
291 |
|
|
292 |
fun compareValue ((),()) = EQUAL;
|
|
293 |
|
|
294 |
fun equalValue () () = true;
|
|
295 |
|
|
296 |
fun compare (Set m1, Set m2) = KM.compare compareValue (m1,m2);
|
|
297 |
|
|
298 |
fun equal (Set m1) (Set m2) = KM.equal equalValue m1 m2;
|
|
299 |
|
|
300 |
fun subset (Set m1) (Set m2) = KM.subsetDomain m1 m2;
|
|
301 |
|
|
302 |
fun disjoint (Set m1) (Set m2) = KM.disjointDomain m1 m2;
|
|
303 |
|
|
304 |
(* ------------------------------------------------------------------------- *)
|
72004
|
305 |
(* Pointwise operations. *)
|
43269
|
306 |
(* ------------------------------------------------------------------------- *)
|
|
307 |
|
72004
|
308 |
fun lift f =
|
|
309 |
let
|
|
310 |
fun inc (elt,set) = union set (f elt)
|
|
311 |
in
|
|
312 |
foldl inc empty
|
|
313 |
end;
|
|
314 |
|
43269
|
315 |
fun closedAdd f =
|
|
316 |
let
|
|
317 |
fun adds acc set = foldl check acc set
|
|
318 |
|
|
319 |
and check (elt,acc) =
|
|
320 |
if member elt acc then acc
|
|
321 |
else expand (add acc elt) elt
|
|
322 |
|
|
323 |
and expand acc elt = adds acc (f elt)
|
|
324 |
in
|
|
325 |
adds
|
|
326 |
end;
|
|
327 |
|
|
328 |
fun close f = closedAdd f empty;
|
|
329 |
|
|
330 |
(* ------------------------------------------------------------------------- *)
|
39348
|
331 |
(* Converting to and from lists. *)
|
|
332 |
(* ------------------------------------------------------------------------- *)
|
|
333 |
|
|
334 |
fun transform f =
|
|
335 |
let
|
|
336 |
fun inc (x,l) = f x :: l
|
|
337 |
in
|
|
338 |
foldr inc []
|
|
339 |
end;
|
|
340 |
|
|
341 |
fun toList (Set m) = KM.keys m;
|
|
342 |
|
|
343 |
fun fromList elts = addList empty elts;
|
|
344 |
|
|
345 |
(* ------------------------------------------------------------------------- *)
|
72004
|
346 |
(* Depth-first search. *)
|
|
347 |
(* ------------------------------------------------------------------------- *)
|
|
348 |
|
|
349 |
datatype ordering =
|
|
350 |
Linear of element list
|
|
351 |
| Cycle of element list;
|
|
352 |
|
|
353 |
fun postOrdered children =
|
|
354 |
let
|
|
355 |
fun check acc elts =
|
|
356 |
case elts of
|
|
357 |
[] => true
|
|
358 |
| elt :: elts =>
|
|
359 |
not (member elt acc) andalso
|
|
360 |
let
|
|
361 |
val acc = closedAdd children acc (singleton elt)
|
|
362 |
in
|
|
363 |
check acc elts
|
|
364 |
end
|
|
365 |
in
|
|
366 |
check empty
|
|
367 |
end;
|
|
368 |
|
|
369 |
fun preOrdered children elts = postOrdered children (List.rev elts);
|
|
370 |
|
|
371 |
local
|
|
372 |
fun takeStackset elt =
|
|
373 |
let
|
|
374 |
fun notElement (e,_,_) = not (equalElement e elt)
|
|
375 |
in
|
|
376 |
Useful.takeWhile notElement
|
|
377 |
end;
|
|
378 |
|
|
379 |
fun consElement ((e,_,_),el) = e :: el;
|
|
380 |
|
|
381 |
fun depthFirstSearch children =
|
|
382 |
let
|
|
383 |
fun traverse (dealt,dealtset) (stack,stackset) work =
|
|
384 |
case work of
|
|
385 |
[] =>
|
|
386 |
(case stack of
|
|
387 |
[] => Linear dealt
|
|
388 |
| (elt,work,stackset) :: stack =>
|
|
389 |
let
|
|
390 |
val dealt = elt :: dealt
|
|
391 |
|
|
392 |
val dealtset = add dealtset elt
|
|
393 |
in
|
|
394 |
traverse (dealt,dealtset) (stack,stackset) work
|
|
395 |
end)
|
|
396 |
| elt :: work =>
|
|
397 |
if member elt dealtset then
|
|
398 |
traverse (dealt,dealtset) (stack,stackset) work
|
|
399 |
else if member elt stackset then
|
|
400 |
let
|
|
401 |
val cycle = takeStackset elt stack
|
|
402 |
|
|
403 |
val cycle = elt :: List.foldl consElement [elt] cycle
|
|
404 |
in
|
|
405 |
Cycle cycle
|
|
406 |
end
|
|
407 |
else
|
|
408 |
let
|
|
409 |
val stack = (elt,work,stackset) :: stack
|
|
410 |
|
|
411 |
val stackset = add stackset elt
|
|
412 |
|
|
413 |
val work = toList (children elt)
|
|
414 |
in
|
|
415 |
traverse (dealt,dealtset) (stack,stackset) work
|
|
416 |
end
|
|
417 |
|
|
418 |
val dealt = []
|
|
419 |
and dealtset = empty
|
|
420 |
and stack = []
|
|
421 |
and stackset = empty
|
|
422 |
in
|
|
423 |
traverse (dealt,dealtset) (stack,stackset)
|
|
424 |
end;
|
|
425 |
in
|
|
426 |
fun preOrder children roots =
|
|
427 |
let
|
|
428 |
val result = depthFirstSearch children (toList roots)
|
|
429 |
|
|
430 |
(*BasicDebug
|
|
431 |
val () =
|
|
432 |
case result of
|
|
433 |
Cycle _ => ()
|
|
434 |
| Linear l =>
|
|
435 |
let
|
|
436 |
val () =
|
|
437 |
if subset roots (fromList l) then ()
|
|
438 |
else raise Useful.Bug "ElementSet.preOrder: missing roots"
|
|
439 |
|
|
440 |
val () =
|
|
441 |
if preOrdered children l then ()
|
|
442 |
else raise Useful.Bug "ElementSet.preOrder: bad ordering"
|
|
443 |
in
|
|
444 |
()
|
|
445 |
end
|
|
446 |
*)
|
|
447 |
in
|
|
448 |
result
|
|
449 |
end;
|
|
450 |
|
|
451 |
fun postOrder children roots =
|
|
452 |
case depthFirstSearch children (toList roots) of
|
|
453 |
Linear l =>
|
|
454 |
let
|
|
455 |
val l = List.rev l
|
|
456 |
|
|
457 |
(*BasicDebug
|
|
458 |
val () =
|
|
459 |
if subset roots (fromList l) then ()
|
|
460 |
else raise Useful.Bug "ElementSet.postOrder: missing roots"
|
|
461 |
|
|
462 |
val () =
|
|
463 |
if postOrdered children l then ()
|
|
464 |
else raise Useful.Bug "ElementSet.postOrder: bad ordering"
|
|
465 |
*)
|
|
466 |
in
|
|
467 |
Linear l
|
|
468 |
end
|
|
469 |
| cycle => cycle;
|
|
470 |
end;
|
|
471 |
|
|
472 |
(* ------------------------------------------------------------------------- *)
|
|
473 |
(* Strongly connected components. *)
|
|
474 |
(* ------------------------------------------------------------------------- *)
|
|
475 |
|
|
476 |
fun postOrderedSCC children =
|
|
477 |
let
|
|
478 |
fun check acc eltsl =
|
|
479 |
case eltsl of
|
|
480 |
[] => true
|
|
481 |
| elts :: eltsl =>
|
|
482 |
not (null elts) andalso
|
|
483 |
disjoint elts acc andalso
|
|
484 |
let
|
|
485 |
fun addElt elt = closedAdd children acc (singleton elt)
|
|
486 |
|
|
487 |
val (root,elts) = deletePick elts
|
|
488 |
|
|
489 |
fun checkElt elt = member root (addElt elt)
|
|
490 |
in
|
|
491 |
all checkElt elts andalso
|
|
492 |
let
|
|
493 |
val acc = addElt root
|
|
494 |
in
|
|
495 |
subset elts acc andalso
|
|
496 |
check acc eltsl
|
|
497 |
end
|
|
498 |
end
|
|
499 |
in
|
|
500 |
check empty
|
|
501 |
end;
|
|
502 |
|
|
503 |
fun preOrderedSCC children eltsl = postOrderedSCC children (List.rev eltsl);
|
|
504 |
|
|
505 |
(* An implementation of Tarjan's algorithm: *)
|
|
506 |
|
|
507 |
(* http://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm *)
|
|
508 |
|
|
509 |
local
|
|
510 |
datatype stackSCC = StackSCC of set * (element * set) list;
|
|
511 |
|
|
512 |
val emptyStack = StackSCC (empty,[]);
|
|
513 |
|
|
514 |
fun pushStack (StackSCC (elts,eltl)) elt =
|
|
515 |
StackSCC (add elts elt, (elt,elts) :: eltl);
|
|
516 |
|
|
517 |
fun inStack elt (StackSCC (elts,_)) = member elt elts;
|
|
518 |
|
|
519 |
fun popStack root =
|
|
520 |
let
|
|
521 |
fun pop scc eltl =
|
|
522 |
case eltl of
|
|
523 |
[] => raise Useful.Bug "ElementSet.popStack"
|
|
524 |
| (elt,elts) :: eltl =>
|
|
525 |
let
|
|
526 |
val scc = add scc elt
|
|
527 |
in
|
|
528 |
if equalElement elt root then (scc, StackSCC (elts,eltl))
|
|
529 |
else pop scc eltl
|
|
530 |
end
|
|
531 |
in
|
|
532 |
fn sccs => fn StackSCC (_,eltl) =>
|
|
533 |
let
|
|
534 |
val (scc,stack) = pop empty eltl
|
|
535 |
in
|
|
536 |
(scc :: sccs, stack)
|
|
537 |
end
|
|
538 |
end;
|
|
539 |
|
|
540 |
fun getIndex indices e : int =
|
|
541 |
case KM.peek indices e of
|
|
542 |
SOME i => i
|
|
543 |
| NONE => raise Useful.Bug "ElementSet.getIndex";
|
|
544 |
|
|
545 |
fun isRoot indices lows e = getIndex indices e = getIndex lows e;
|
|
546 |
|
|
547 |
fun reduceIndex indices (e,i) =
|
|
548 |
let
|
|
549 |
val j = getIndex indices e
|
|
550 |
in
|
|
551 |
if j <= i then indices else KM.insert indices (e,i)
|
|
552 |
end;
|
|
553 |
|
|
554 |
fun tarjan children =
|
|
555 |
let
|
|
556 |
fun dfsVertex sccs callstack index indices lows stack elt =
|
|
557 |
let
|
|
558 |
val indices = KM.insert indices (elt,index)
|
|
559 |
and lows = KM.insert lows (elt,index)
|
|
560 |
|
|
561 |
val index = index + 1
|
|
562 |
|
|
563 |
val stack = pushStack stack elt
|
|
564 |
|
|
565 |
val chil = toList (children elt)
|
|
566 |
in
|
|
567 |
dfsSuccessors sccs callstack index indices lows stack elt chil
|
|
568 |
end
|
|
569 |
|
|
570 |
and dfsSuccessors sccs callstack index indices lows stack elt chil =
|
|
571 |
case chil of
|
|
572 |
[] =>
|
|
573 |
let
|
|
574 |
val (sccs,stack) =
|
|
575 |
if isRoot indices lows elt then popStack elt sccs stack
|
|
576 |
else (sccs,stack)
|
|
577 |
in
|
|
578 |
case callstack of
|
|
579 |
[] => (sccs,index,indices,lows)
|
|
580 |
| (p,elts) :: callstack =>
|
|
581 |
let
|
|
582 |
val lows = reduceIndex lows (p, getIndex lows elt)
|
|
583 |
in
|
|
584 |
dfsSuccessors sccs callstack index indices lows stack p elts
|
|
585 |
end
|
|
586 |
end
|
|
587 |
| c :: chil =>
|
|
588 |
case KM.peek indices c of
|
|
589 |
NONE =>
|
|
590 |
let
|
|
591 |
val callstack = (elt,chil) :: callstack
|
|
592 |
in
|
|
593 |
dfsVertex sccs callstack index indices lows stack c
|
|
594 |
end
|
|
595 |
| SOME cind =>
|
|
596 |
let
|
|
597 |
val lows =
|
|
598 |
if inStack c stack then reduceIndex lows (elt,cind)
|
|
599 |
else lows
|
|
600 |
in
|
|
601 |
dfsSuccessors sccs callstack index indices lows stack elt chil
|
|
602 |
end
|
|
603 |
|
|
604 |
fun dfsRoots sccs index indices lows elts =
|
|
605 |
case elts of
|
|
606 |
[] => sccs
|
|
607 |
| elt :: elts =>
|
|
608 |
if KM.inDomain elt indices then
|
|
609 |
dfsRoots sccs index indices lows elts
|
|
610 |
else
|
|
611 |
let
|
|
612 |
val callstack = []
|
|
613 |
|
|
614 |
val (sccs,index,indices,lows) =
|
|
615 |
dfsVertex sccs callstack index indices lows emptyStack elt
|
|
616 |
in
|
|
617 |
dfsRoots sccs index indices lows elts
|
|
618 |
end
|
|
619 |
|
|
620 |
val sccs = []
|
|
621 |
and index = 0
|
|
622 |
and indices = KM.new ()
|
|
623 |
and lows = KM.new ()
|
|
624 |
in
|
|
625 |
dfsRoots sccs index indices lows
|
|
626 |
end;
|
|
627 |
in
|
|
628 |
fun preOrderSCC children roots =
|
|
629 |
let
|
|
630 |
val result = tarjan children (toList roots)
|
|
631 |
|
|
632 |
(*BasicDebug
|
|
633 |
val () =
|
|
634 |
if subset roots (unionList result) then ()
|
|
635 |
else raise Useful.Bug "ElementSet.preOrderSCC: missing roots"
|
|
636 |
|
|
637 |
val () =
|
|
638 |
if preOrderedSCC children result then ()
|
|
639 |
else raise Useful.Bug "ElementSet.preOrderSCC: bad ordering"
|
|
640 |
*)
|
|
641 |
in
|
|
642 |
result
|
|
643 |
end;
|
|
644 |
|
|
645 |
fun postOrderSCC children roots =
|
|
646 |
let
|
|
647 |
val result = List.rev (tarjan children (toList roots))
|
|
648 |
|
|
649 |
(*BasicDebug
|
|
650 |
val () =
|
|
651 |
if subset roots (unionList result) then ()
|
|
652 |
else raise Useful.Bug "ElementSet.postOrderSCC: missing roots"
|
|
653 |
|
|
654 |
val () =
|
|
655 |
if postOrderedSCC children result then ()
|
|
656 |
else raise Useful.Bug "ElementSet.postOrderSCC: bad ordering"
|
|
657 |
*)
|
|
658 |
in
|
|
659 |
result
|
|
660 |
end;
|
|
661 |
end;
|
|
662 |
|
|
663 |
(* ------------------------------------------------------------------------- *)
|
39348
|
664 |
(* Pretty-printing. *)
|
|
665 |
(* ------------------------------------------------------------------------- *)
|
|
666 |
|
|
667 |
fun toString set =
|
|
668 |
"{" ^ (if null set then "" else Int.toString (size set)) ^ "}";
|
|
669 |
|
|
670 |
(* ------------------------------------------------------------------------- *)
|
|
671 |
(* Iterators over sets *)
|
|
672 |
(* ------------------------------------------------------------------------- *)
|
|
673 |
|
|
674 |
type iterator = unit KM.iterator;
|
|
675 |
|
|
676 |
fun mkIterator (Set m) = KM.mkIterator m;
|
|
677 |
|
|
678 |
fun mkRevIterator (Set m) = KM.mkRevIterator m;
|
|
679 |
|
|
680 |
fun readIterator iter =
|
|
681 |
let
|
|
682 |
val (elt,()) = KM.readIterator iter
|
|
683 |
in
|
|
684 |
elt
|
|
685 |
end;
|
|
686 |
|
|
687 |
fun advanceIterator iter = KM.advanceIterator iter;
|
|
688 |
|
|
689 |
end
|
|
690 |
|
|
691 |
structure IntSet =
|
|
692 |
ElementSet (IntMap);
|
|
693 |
|
|
694 |
structure IntPairSet =
|
|
695 |
ElementSet (IntPairMap);
|
|
696 |
|
|
697 |
structure StringSet =
|
|
698 |
ElementSet (StringMap);
|