22 val is_empty: 'a table -> bool |
22 val is_empty: 'a table -> bool |
23 val is_single: 'a table -> bool |
23 val is_single: 'a table -> bool |
24 val map: (key -> 'a -> 'b) -> 'a table -> 'b table |
24 val map: (key -> 'a -> 'b) -> 'a table -> 'b table |
25 val fold: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a |
25 val fold: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a |
26 val fold_rev: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a |
26 val fold_rev: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a |
|
27 val size: 'a table -> int |
27 val dest: 'a table -> (key * 'a) list |
28 val dest: 'a table -> (key * 'a) list |
28 val keys: 'a table -> key list |
29 val keys: 'a table -> key list |
29 val min: 'a table -> (key * 'a) option |
30 val min: 'a table -> (key * 'a) option |
30 val max: 'a table -> (key * 'a) option |
31 val max: 'a table -> (key * 'a) option |
31 val get_first: (key * 'a -> 'b option) -> 'a table -> 'b option |
|
32 val exists: (key * 'a -> bool) -> 'a table -> bool |
32 val exists: (key * 'a -> bool) -> 'a table -> bool |
33 val forall: (key * 'a -> bool) -> 'a table -> bool |
33 val forall: (key * 'a -> bool) -> 'a table -> bool |
|
34 val get_first: (key * 'a -> 'b option) -> 'a table -> 'b option |
34 val lookup_key: 'a table -> key -> (key * 'a) option |
35 val lookup_key: 'a table -> key -> (key * 'a) option |
35 val lookup: 'a table -> key -> 'a option |
36 val lookup: 'a table -> key -> 'a option |
36 val defined: 'a table -> key -> bool |
37 val defined: 'a table -> key -> bool |
37 val update: key * 'a -> 'a table -> 'a table |
38 val update: key * 'a -> 'a table -> 'a table |
38 val update_new: key * 'a -> 'a table -> 'a table (*exception DUP*) |
39 val update_new: key * 'a -> 'a table -> 'a table (*exception DUP*) |
58 val merge_list: ('a * 'a -> bool) -> 'a list table * 'a list table -> 'a list table |
59 val merge_list: ('a * 'a -> bool) -> 'a list table * 'a list table -> 'a list table |
59 type set = unit table |
60 type set = unit table |
60 val insert_set: key -> set -> set |
61 val insert_set: key -> set -> set |
61 val remove_set: key -> set -> set |
62 val remove_set: key -> set -> set |
62 val make_set: key list -> set |
63 val make_set: key list -> set |
|
64 val subset: set * set -> bool |
|
65 val eq_set: set * set -> bool |
63 end; |
66 end; |
64 |
67 |
65 functor Table(Key: KEY): TABLE = |
68 functor Table(Key: KEY): TABLE = |
66 struct |
69 struct |
67 |
70 |
116 fold left (f p (fold right x)) |
119 fold left (f p (fold right x)) |
117 | fold (Branch3 (left, p1, mid, p2, right)) x = |
120 | fold (Branch3 (left, p1, mid, p2, right)) x = |
118 fold left (f p1 (fold mid (f p2 (fold right x)))); |
121 fold left (f p1 (fold mid (f p2 (fold right x)))); |
119 in fold end; |
122 in fold end; |
120 |
123 |
|
124 fun size tab = fold_table (fn _ => fn n => n + 1) tab 0; |
121 fun dest tab = fold_rev_table cons tab []; |
125 fun dest tab = fold_rev_table cons tab []; |
122 fun keys tab = fold_rev_table (cons o #1) tab []; |
126 fun keys tab = fold_rev_table (cons o #1) tab []; |
123 |
127 |
124 |
128 |
125 (* min/max entries *) |
129 (* min/max entries *) |
133 fun max Empty = NONE |
137 fun max Empty = NONE |
134 | max (Branch2 (_, p, Empty)) = SOME p |
138 | max (Branch2 (_, p, Empty)) = SOME p |
135 | max (Branch3 (_, _, _, p, Empty)) = SOME p |
139 | max (Branch3 (_, _, _, p, Empty)) = SOME p |
136 | max (Branch2 (_, _, right)) = max right |
140 | max (Branch2 (_, _, right)) = max right |
137 | max (Branch3 (_, _, _, _, right)) = max right; |
141 | max (Branch3 (_, _, _, _, right)) = max right; |
|
142 |
|
143 |
|
144 (* exists and forall *) |
|
145 |
|
146 fun exists pred = |
|
147 let |
|
148 fun ex Empty = false |
|
149 | ex (Branch2 (left, (k, x), right)) = |
|
150 ex left orelse pred (k, x) orelse ex right |
|
151 | ex (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = |
|
152 ex left orelse pred (k1, x1) orelse ex mid orelse pred (k2, x2) orelse ex right; |
|
153 in ex end; |
|
154 |
|
155 fun forall pred = not o exists (not o pred); |
138 |
156 |
139 |
157 |
140 (* get_first *) |
158 (* get_first *) |
141 |
159 |
142 fun get_first f = |
160 fun get_first f = |
422 fun make_list args = fold_rev cons_list args empty; |
437 fun make_list args = fold_rev cons_list args empty; |
423 fun dest_list tab = maps (fn (key, xs) => map (pair key) xs) (dest tab); |
438 fun dest_list tab = maps (fn (key, xs) => map (pair key) xs) (dest tab); |
424 fun merge_list eq = join (fn _ => Library.merge eq); |
439 fun merge_list eq = join (fn _ => Library.merge eq); |
425 |
440 |
426 |
441 |
427 (* unit tables *) |
442 (* set operations *) |
428 |
443 |
429 type set = unit table; |
444 type set = unit table; |
430 |
445 |
431 fun insert_set x = default (x, ()); |
446 fun insert_set x = default (x, ()); |
432 fun remove_set x : set -> set = delete_safe x; |
447 fun remove_set x : set -> set = delete_safe x; |
433 fun make_set entries = fold insert_set entries empty; |
448 fun make_set entries = fold insert_set entries empty; |
|
449 |
|
450 fun subset (A: set, B: set) = forall (defined B o #1) A; |
|
451 fun eq_set (A: set, B: set) = size A = size B andalso subset (A, B); |
434 |
452 |
435 |
453 |
436 (* ML pretty-printing *) |
454 (* ML pretty-printing *) |
437 |
455 |
438 val _ = |
456 val _ = |