author | wenzelm |
Tue, 28 Mar 2023 19:43:49 +0200 | |
changeset 77735 | be3f838b3e17 |
parent 77733 | 59c94a376a3c |
child 77736 | 570f1436fe0a |
permissions | -rw-r--r-- |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
1 |
(* Title: Pure/General/set.ML |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
3 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
4 |
Efficient representation of sets (see also Pure/General/table.ML). |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
5 |
*) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
6 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
7 |
signature SET = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
8 |
sig |
77731 | 9 |
structure Key: KEY |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
10 |
type elem |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
11 |
type T |
77725 | 12 |
val size: T -> int |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
13 |
val empty: T |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
14 |
val build: (T -> T) -> T |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
15 |
val is_empty: T -> bool |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
16 |
val is_single: T -> bool |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
17 |
val fold: (elem -> 'a -> 'a) -> T -> 'a -> 'a |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
18 |
val fold_rev: (elem -> 'a -> 'a) -> T -> 'a -> 'a |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
19 |
val dest: T -> elem list |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
20 |
val exists: (elem -> bool) -> T -> bool |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
21 |
val forall: (elem -> bool) -> T -> bool |
77728 | 22 |
val get_first: (elem -> 'a option) -> T -> 'a option |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
23 |
val member: T -> elem -> bool |
77728 | 24 |
val subset: T * T -> bool |
25 |
val ord: T ord |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
26 |
val insert: elem -> T -> T |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
27 |
val make: elem list -> T |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
28 |
val merge: T * T -> T |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
29 |
val remove: elem -> T -> T |
77728 | 30 |
val subtract: T -> T -> T |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
31 |
end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
32 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
33 |
functor Set(Key: KEY): SET = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
34 |
struct |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
35 |
|
77733 | 36 |
(* keys *) |
37 |
||
77731 | 38 |
structure Key = Key; |
39 |
type elem = Key.key; |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
40 |
|
77731 | 41 |
|
42 |
(* datatype *) |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
43 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
44 |
datatype T = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
45 |
Empty | |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
46 |
Branch2 of T * elem * T | |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
47 |
Branch3 of T * elem * T * elem * T; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
48 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
49 |
|
77725 | 50 |
(* size *) |
51 |
||
52 |
local |
|
53 |
fun add_size Empty n = n |
|
54 |
| add_size (Branch2 (left, _, right)) n = |
|
55 |
n + 1 |> add_size left |> add_size right |
|
56 |
| add_size (Branch3 (left, _, mid, _, right)) n = |
|
57 |
n + 2 |> add_size left |> add_size mid |> add_size right; |
|
58 |
in |
|
59 |
fun size set = add_size set 0; |
|
60 |
end; |
|
61 |
||
62 |
||
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
63 |
(* empty and single *) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
64 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
65 |
val empty = Empty; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
66 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
67 |
fun build (f: T -> T) = f empty; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
68 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
69 |
fun is_empty Empty = true |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
70 |
| is_empty _ = false; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
71 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
72 |
fun is_single (Branch2 (Empty, _, Empty)) = true |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
73 |
| is_single _ = false; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
74 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
75 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
76 |
(* fold combinators *) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
77 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
78 |
fun fold_set f = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
79 |
let |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
80 |
fun fold Empty x = x |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
81 |
| fold (Branch2 (left, e, right)) x = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
82 |
fold right (f e (fold left x)) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
83 |
| fold (Branch3 (left, e1, mid, e2, right)) x = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
84 |
fold right (f e2 (fold mid (f e1 (fold left x)))); |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
85 |
in fold end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
86 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
87 |
fun fold_rev_set f = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
88 |
let |
77732 | 89 |
fun fold_rev Empty x = x |
90 |
| fold_rev (Branch2 (left, e, right)) x = |
|
91 |
fold_rev left (f e (fold_rev right x)) |
|
92 |
| fold_rev (Branch3 (left, e1, mid, e2, right)) x = |
|
93 |
fold_rev left (f e1 (fold_rev mid (f e2 (fold_rev right x)))); |
|
94 |
in fold_rev end; |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
95 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
96 |
fun dest tab = fold_rev_set cons tab []; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
97 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
98 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
99 |
(* exists and forall *) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
100 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
101 |
fun exists pred = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
102 |
let |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
103 |
fun ex Empty = false |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
104 |
| ex (Branch2 (left, e, right)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
105 |
ex left orelse pred e orelse ex right |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
106 |
| ex (Branch3 (left, e1, mid, e2, right)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
107 |
ex left orelse pred e1 orelse ex mid orelse pred e2 orelse ex right; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
108 |
in ex end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
109 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
110 |
fun forall pred = not o exists (not o pred); |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
111 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
112 |
|
77728 | 113 |
(* get_first *) |
114 |
||
115 |
fun get_first f = |
|
116 |
let |
|
117 |
fun get Empty = NONE |
|
118 |
| get (Branch2 (left, e, right)) = |
|
119 |
(case get left of |
|
120 |
NONE => |
|
121 |
(case f e of |
|
122 |
NONE => get right |
|
123 |
| some => some) |
|
124 |
| some => some) |
|
125 |
| get (Branch3 (left, e1, mid, e2, right)) = |
|
126 |
(case get left of |
|
127 |
NONE => |
|
128 |
(case f e1 of |
|
129 |
NONE => |
|
130 |
(case get mid of |
|
131 |
NONE => |
|
132 |
(case f e2 of |
|
133 |
NONE => get right |
|
134 |
| some => some) |
|
135 |
| some => some) |
|
136 |
| some => some) |
|
137 |
| some => some); |
|
138 |
in get end; |
|
139 |
||
140 |
||
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
141 |
(* member *) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
142 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
143 |
fun member set elem = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
144 |
let |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
145 |
fun mem Empty = false |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
146 |
| mem (Branch2 (left, e, right)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
147 |
(case Key.ord (elem, e) of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
148 |
LESS => mem left |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
149 |
| EQUAL => true |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
150 |
| GREATER => mem right) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
151 |
| mem (Branch3 (left, e1, mid, e2, right)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
152 |
(case Key.ord (elem, e1) of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
153 |
LESS => mem left |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
154 |
| EQUAL => true |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
155 |
| GREATER => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
156 |
(case Key.ord (elem, e2) of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
157 |
LESS => mem mid |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
158 |
| EQUAL => true |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
159 |
| GREATER => mem right)); |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
160 |
in mem set end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
161 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
162 |
|
77728 | 163 |
(* subset and order *) |
164 |
||
165 |
fun subset (set1, set2) = forall (member set2) set1; |
|
166 |
||
167 |
val ord = |
|
168 |
pointer_eq_ord (fn sets => |
|
169 |
(case int_ord (apply2 size sets) of |
|
170 |
EQUAL => |
|
171 |
if subset sets then EQUAL |
|
172 |
else dict_ord Key.ord (apply2 dest sets) |
|
173 |
| ord => ord)); |
|
174 |
||
175 |
||
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
176 |
(* insert *) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
177 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
178 |
datatype growth = Stay of T | Sprout of T * elem * T; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
179 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
180 |
fun insert elem set = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
181 |
if member set elem then set |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
182 |
else |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
183 |
let |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
184 |
fun ins Empty = Sprout (Empty, elem, Empty) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
185 |
| ins (Branch2 (left, e, right)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
186 |
(case Key.ord (elem, e) of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
187 |
LESS => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
188 |
(case ins left of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
189 |
Stay left' => Stay (Branch2 (left', e, right)) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
190 |
| Sprout (left1, e', left2) => Stay (Branch3 (left1, e', left2, e, right))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
191 |
| EQUAL => Stay (Branch2 (left, e, right)) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
192 |
| GREATER => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
193 |
(case ins right of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
194 |
Stay right' => Stay (Branch2 (left, e, right')) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
195 |
| Sprout (right1, e', right2) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
196 |
Stay (Branch3 (left, e, right1, e', right2)))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
197 |
| ins (Branch3 (left, e1, mid, e2, right)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
198 |
(case Key.ord (elem, e1) of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
199 |
LESS => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
200 |
(case ins left of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
201 |
Stay left' => Stay (Branch3 (left', e1, mid, e2, right)) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
202 |
| Sprout (left1, e', left2) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
203 |
Sprout (Branch2 (left1, e', left2), e1, Branch2 (mid, e2, right))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
204 |
| EQUAL => Stay (Branch3 (left, e1, mid, e2, right)) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
205 |
| GREATER => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
206 |
(case Key.ord (elem, e2) of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
207 |
LESS => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
208 |
(case ins mid of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
209 |
Stay mid' => Stay (Branch3 (left, e1, mid', e2, right)) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
210 |
| Sprout (mid1, e', mid2) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
211 |
Sprout (Branch2 (left, e1, mid1), e', Branch2 (mid2, e2, right))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
212 |
| EQUAL => Stay (Branch3 (left, e1, mid, e2, right)) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
213 |
| GREATER => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
214 |
(case ins right of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
215 |
Stay right' => Stay (Branch3 (left, e1, mid, e2, right')) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
216 |
| Sprout (right1, e', right2) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
217 |
Sprout (Branch2 (left, e1, mid), e2, Branch2 (right1, e', right2))))); |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
218 |
in |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
219 |
(case ins set of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
220 |
Stay set' => set' |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
221 |
| Sprout br => Branch2 br) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
222 |
end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
223 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
224 |
fun make elems = build (fold insert elems); |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
225 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
226 |
fun merge (set1, set2) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
227 |
if pointer_eq (set1, set2) then set1 |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
228 |
else if is_empty set1 then set2 |
77725 | 229 |
else if is_empty set2 then set1 |
230 |
else if size set1 >= size set2 |
|
231 |
then fold_set insert set2 set1 |
|
232 |
else fold_set insert set1 set2; |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
233 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
234 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
235 |
(* remove *) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
236 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
237 |
local |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
238 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
239 |
fun compare NONE _ = LESS |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
240 |
| compare (SOME e1) e2 = Key.ord (e1, e2); |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
241 |
|
77735 | 242 |
fun if_eq ord x y = if ord = EQUAL then x else y; |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
243 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
244 |
exception UNDEF of elem; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
245 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
246 |
(*literal copy from table.ML -- by Stefan Berghofer*) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
247 |
fun del (SOME k) Empty = raise UNDEF k |
77735 | 248 |
| del NONE Empty = raise Match |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
249 |
| del NONE (Branch2 (Empty, p, Empty)) = (p, (true, Empty)) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
250 |
| del NONE (Branch3 (Empty, p, Empty, q, Empty)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
251 |
(p, (false, Branch2 (Empty, q, Empty))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
252 |
| del k (Branch2 (Empty, p, Empty)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
253 |
(case compare k p of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
254 |
EQUAL => (p, (true, Empty)) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
255 |
| _ => raise UNDEF (the k)) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
256 |
| del k (Branch3 (Empty, p, Empty, q, Empty)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
257 |
(case compare k p of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
258 |
EQUAL => (p, (false, Branch2 (Empty, q, Empty))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
259 |
| _ => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
260 |
(case compare k q of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
261 |
EQUAL => (q, (false, Branch2 (Empty, p, Empty))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
262 |
| _ => raise UNDEF (the k))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
263 |
| del k (Branch2 (l, p, r)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
264 |
(case compare k p of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
265 |
LESS => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
266 |
(case del k l of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
267 |
(p', (false, l')) => (p', (false, Branch2 (l', p, r))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
268 |
| (p', (true, l')) => (p', case r of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
269 |
Branch2 (rl, rp, rr) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
270 |
(true, Branch3 (l', p, rl, rp, rr)) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
271 |
| Branch3 (rl, rp, rm, rq, rr) => (false, Branch2 |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
272 |
(Branch2 (l', p, rl), rp, Branch2 (rm, rq, rr))))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
273 |
| ord => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
274 |
(case del (if_eq ord NONE k) r of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
275 |
(p', (false, r')) => (p', (false, Branch2 (l, if_eq ord p' p, r'))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
276 |
| (p', (true, r')) => (p', case l of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
277 |
Branch2 (ll, lp, lr) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
278 |
(true, Branch3 (ll, lp, lr, if_eq ord p' p, r')) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
279 |
| Branch3 (ll, lp, lm, lq, lr) => (false, Branch2 |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
280 |
(Branch2 (ll, lp, lm), lq, Branch2 (lr, if_eq ord p' p, r')))))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
281 |
| del k (Branch3 (l, p, m, q, r)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
282 |
(case compare k q of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
283 |
LESS => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
284 |
(case compare k p of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
285 |
LESS => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
286 |
(case del k l of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
287 |
(p', (false, l')) => (p', (false, Branch3 (l', p, m, q, r))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
288 |
| (p', (true, l')) => (p', (false, case (m, r) of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
289 |
(Branch2 (ml, mp, mr), Branch2 _) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
290 |
Branch2 (Branch3 (l', p, ml, mp, mr), q, r) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
291 |
| (Branch3 (ml, mp, mm, mq, mr), _) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
292 |
Branch3 (Branch2 (l', p, ml), mp, Branch2 (mm, mq, mr), q, r) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
293 |
| (Branch2 (ml, mp, mr), Branch3 (rl, rp, rm, rq, rr)) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
294 |
Branch3 (Branch2 (l', p, ml), mp, Branch2 (mr, q, rl), rp, |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
295 |
Branch2 (rm, rq, rr))))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
296 |
| ord => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
297 |
(case del (if_eq ord NONE k) m of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
298 |
(p', (false, m')) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
299 |
(p', (false, Branch3 (l, if_eq ord p' p, m', q, r))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
300 |
| (p', (true, m')) => (p', (false, case (l, r) of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
301 |
(Branch2 (ll, lp, lr), Branch2 _) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
302 |
Branch2 (Branch3 (ll, lp, lr, if_eq ord p' p, m'), q, r) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
303 |
| (Branch3 (ll, lp, lm, lq, lr), _) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
304 |
Branch3 (Branch2 (ll, lp, lm), lq, |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
305 |
Branch2 (lr, if_eq ord p' p, m'), q, r) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
306 |
| (_, Branch3 (rl, rp, rm, rq, rr)) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
307 |
Branch3 (l, if_eq ord p' p, Branch2 (m', q, rl), rp, |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
308 |
Branch2 (rm, rq, rr)))))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
309 |
| ord => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
310 |
(case del (if_eq ord NONE k) r of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
311 |
(q', (false, r')) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
312 |
(q', (false, Branch3 (l, p, m, if_eq ord q' q, r'))) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
313 |
| (q', (true, r')) => (q', (false, case (l, m) of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
314 |
(Branch2 _, Branch2 (ml, mp, mr)) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
315 |
Branch2 (l, p, Branch3 (ml, mp, mr, if_eq ord q' q, r')) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
316 |
| (_, Branch3 (ml, mp, mm, mq, mr)) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
317 |
Branch3 (l, p, Branch2 (ml, mp, mm), mq, |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
318 |
Branch2 (mr, if_eq ord q' q, r')) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
319 |
| (Branch3 (ll, lp, lm, lq, lr), Branch2 (ml, mp, mr)) => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
320 |
Branch3 (Branch2 (ll, lp, lm), lq, Branch2 (lr, p, ml), mp, |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
321 |
Branch2 (mr, if_eq ord q' q, r')))))); |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
322 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
323 |
in |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
324 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
325 |
fun remove elem set = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
326 |
if member set elem then snd (snd (del (SOME elem) set)) else set; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
327 |
|
77728 | 328 |
val subtract = fold_set remove; |
329 |
||
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
330 |
end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
331 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
332 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
333 |
(* ML pretty-printing *) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
334 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
335 |
val _ = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
336 |
ML_system_pp (fn depth => fn _ => fn set => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
337 |
ML_Pretty.to_polyml |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
338 |
(ML_Pretty.enum "," "{" "}" (ML_Pretty.from_polyml o ML_system_pretty) (dest set, depth))); |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
339 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
340 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
341 |
(*final declarations of this structure!*) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
342 |
val fold = fold_set; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
343 |
val fold_rev = fold_rev_set; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
344 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
345 |
end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
346 |
|
77731 | 347 |
structure Intset = Set(Inttab.Key); |
348 |
structure Symset = Set(Symtab.Key); |