author | wenzelm |
Mon, 02 Dec 2024 22:16:29 +0100 | |
changeset 81541 | 5335b1ca6233 |
parent 80809 | 4a64fc4d1cde |
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 fold: (elem -> 'a -> 'a) -> T -> 'a -> 'a |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
17 |
val fold_rev: (elem -> 'a -> 'a) -> T -> 'a -> 'a |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
18 |
val dest: T -> elem list |
77816 | 19 |
val min: T -> elem option |
20 |
val max: T -> elem option |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
21 |
val exists: (elem -> bool) -> T -> bool |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
22 |
val forall: (elem -> bool) -> T -> bool |
77728 | 23 |
val get_first: (elem -> 'a option) -> T -> 'a option |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
24 |
val member: T -> elem -> bool |
77728 | 25 |
val subset: T * T -> bool |
77836 | 26 |
val eq_set: T * T -> bool |
77912 | 27 |
val ord: T ord |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
28 |
val insert: elem -> T -> T |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
29 |
val make: elem list -> T |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
30 |
val merge: T * T -> T |
77822 | 31 |
val merges: T list -> T |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
32 |
val remove: elem -> T -> T |
77728 | 33 |
val subtract: T -> T -> T |
77911
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
34 |
val restrict: (elem -> bool) -> T -> T |
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
35 |
val inter: T -> T -> T |
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
36 |
val union: T -> T -> T |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
37 |
end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
38 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
39 |
functor Set(Key: KEY): SET = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
40 |
struct |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
41 |
|
77733 | 42 |
(* keys *) |
43 |
||
77731 | 44 |
structure Key = Key; |
45 |
type elem = Key.key; |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
46 |
|
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
47 |
|
77731 | 48 |
(* datatype *) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
49 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
50 |
datatype T = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
51 |
Empty | |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
52 |
Leaf1 of elem | |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
53 |
Leaf2 of elem * elem | |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
54 |
Leaf3 of elem * elem * elem | |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
55 |
Branch2 of T * elem * T | |
77800 | 56 |
Branch3 of T * elem * T * elem * T | |
57 |
Size of int * T; |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
58 |
|
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
59 |
fun make2 (Empty, e, Empty) = Leaf1 e |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
60 |
| make2 (Branch2 (Empty, e1, Empty), e2, right) = make2 (Leaf1 e1, e2, right) |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
61 |
| make2 (left, e1, Branch2 (Empty, e2, Empty)) = make2 (left, e1, Leaf1 e2) |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
62 |
| make2 (Branch3 (Empty, e1, Empty, e2, Empty), e3, right) = make2 (Leaf2 (e1, e2), e3, right) |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
63 |
| make2 (left, e1, Branch3 (Empty, e2, Empty, e3, Empty)) = make2 (left, e1, Leaf2 (e2, e3)) |
77739 | 64 |
| make2 (Leaf1 e1, e2, Empty) = Leaf2 (e1, e2) |
65 |
| make2 (Empty, e1, Leaf1 e2) = Leaf2 (e1, e2) |
|
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
66 |
| make2 (Leaf1 e1, e2, Leaf1 e3) = Leaf3 (e1, e2, e3) |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
67 |
| make2 (Leaf2 (e1, e2), e3, Empty) = Leaf3 (e1, e2, e3) |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
68 |
| make2 (Empty, e1, Leaf2 (e2, e3)) = Leaf3 (e1, e2, e3) |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
69 |
| make2 arg = Branch2 arg; |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
70 |
|
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
71 |
fun make3 (Empty, e1, Empty, e2, Empty) = Leaf2 (e1, e2) |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
72 |
| make3 (Branch2 (Empty, e1, Empty), e2, mid, e3, right) = make3 (Leaf1 e1, e2, mid, e3, right) |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
73 |
| make3 (left, e1, Branch2 (Empty, e2, Empty), e3, right) = make3 (left, e1, Leaf1 e2, e3, right) |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
74 |
| make3 (left, e1, mid, e2, Branch2 (Empty, e3, Empty)) = make3 (left, e1, mid, e2, Leaf1 e3) |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
75 |
| make3 (Leaf1 e1, e2, Empty, e3, Empty) = Leaf3 (e1, e2, e3) |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
76 |
| make3 (Empty, e1, Leaf1 e2, e3, Empty) = Leaf3 (e1, e2, e3) |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
77 |
| make3 (Empty, e1, Empty, e2, Leaf1 e3) = Leaf3 (e1, e2, e3) |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
78 |
| make3 arg = Branch3 arg; |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
79 |
|
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
80 |
fun unmake (Leaf1 e) = Branch2 (Empty, e, Empty) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
81 |
| unmake (Leaf2 (e1, e2)) = Branch3 (Empty, e1, Empty, e2, Empty) |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
82 |
| unmake (Leaf3 (e1, e2, e3)) = |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
83 |
Branch2 (Branch2 (Empty, e1, Empty), e2, Branch2 (Empty, e3, Empty)) |
77800 | 84 |
| unmake (Size (_, arg)) = arg |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
85 |
| unmake arg = arg; |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
86 |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
87 |
|
77725 | 88 |
(* size *) |
89 |
||
77780
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
90 |
(*literal copy from table.ML*) |
77725 | 91 |
local |
77768 | 92 |
fun count Empty n = n |
93 |
| count (Leaf1 _) n = n + 1 |
|
94 |
| count (Leaf2 _) n = n + 2 |
|
95 |
| count (Leaf3 _) n = n + 3 |
|
96 |
| count (Branch2 (left, _, right)) n = count right (count left (n + 1)) |
|
77800 | 97 |
| count (Branch3 (left, _, mid, _, right)) n = count right (count mid (count left (n + 2))) |
98 |
| count (Size (m, _)) n = m + n; |
|
77802
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
99 |
|
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
100 |
fun box (Branch2 _) = 1 |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
101 |
| box (Branch3 _) = 1 |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
102 |
| box _ = 0; |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
103 |
|
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
104 |
fun bound arg b = |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
105 |
if b > 0 then |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
106 |
(case arg of |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
107 |
Branch2 (left, _, right) => |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
108 |
bound right (bound left (b - box left - box right)) |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
109 |
| Branch3 (left, _, mid, _, right) => |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
110 |
bound right (bound mid (bound left (b - box left - box mid - box right))) |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
111 |
| _ => b) |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
112 |
else b; |
77725 | 113 |
in |
77802
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
114 |
fun size arg = count arg 0; |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
115 |
fun make_size m arg = if bound arg 3 <= 0 then Size (m, arg) else arg; |
77725 | 116 |
end; |
117 |
||
118 |
||
77743 | 119 |
(* empty *) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
120 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
121 |
val empty = Empty; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
122 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
123 |
fun build (f: T -> T) = f empty; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
124 |
|
77800 | 125 |
(*literal copy from table.ML*) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
126 |
fun is_empty Empty = true |
77800 | 127 |
| is_empty (Size (_, arg)) = is_empty arg |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
128 |
| is_empty _ = false; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
129 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
130 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
131 |
(* fold combinators *) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
132 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
133 |
fun fold_set f = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
134 |
let |
77813 | 135 |
fun fold Empty a = a |
136 |
| fold (Leaf1 e) a = f e a |
|
137 |
| fold (Leaf2 (e1, e2)) a = f e2 (f e1 a) |
|
138 |
| fold (Leaf3 (e1, e2, e3)) a = f e3 (f e2 (f e1 a)) |
|
139 |
| fold (Branch2 (left, e, right)) a = |
|
140 |
fold right (f e (fold left a)) |
|
141 |
| fold (Branch3 (left, e1, mid, e2, right)) a = |
|
142 |
fold right (f e2 (fold mid (f e1 (fold left a)))) |
|
143 |
| fold (Size (_, arg)) a = fold arg a; |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
144 |
in fold end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
145 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
146 |
fun fold_rev_set f = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
147 |
let |
77813 | 148 |
fun fold_rev Empty a = a |
149 |
| fold_rev (Leaf1 e) a = f e a |
|
150 |
| fold_rev (Leaf2 (e1, e2)) a = f e1 (f e2 a) |
|
151 |
| fold_rev (Leaf3 (e1, e2, e3)) a = f e1 (f e2 (f e3 a)) |
|
152 |
| fold_rev (Branch2 (left, e, right)) a = |
|
153 |
fold_rev left (f e (fold_rev right a)) |
|
154 |
| fold_rev (Branch3 (left, e1, mid, e2, right)) a = |
|
155 |
fold_rev left (f e1 (fold_rev mid (f e2 (fold_rev right a)))) |
|
156 |
| fold_rev (Size (_, arg)) a = fold_rev arg a; |
|
77732 | 157 |
in fold_rev end; |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
158 |
|
77768 | 159 |
val dest = Library.build o fold_rev_set cons; |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
160 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
161 |
|
77816 | 162 |
(* min/max entries *) |
163 |
||
164 |
fun min Empty = NONE |
|
165 |
| min (Leaf1 e) = SOME e |
|
166 |
| min (Leaf2 (e, _)) = SOME e |
|
167 |
| min (Leaf3 (e, _, _)) = SOME e |
|
168 |
| min (Branch2 (Empty, e, _)) = SOME e |
|
169 |
| min (Branch3 (Empty, e, _, _, _)) = SOME e |
|
170 |
| min (Branch2 (left, _, _)) = min left |
|
171 |
| min (Branch3 (left, _, _, _, _)) = min left |
|
172 |
| min (Size (_, arg)) = min arg; |
|
173 |
||
174 |
fun max Empty = NONE |
|
175 |
| max (Leaf1 e) = SOME e |
|
176 |
| max (Leaf2 (_, e)) = SOME e |
|
177 |
| max (Leaf3 (_, _, e)) = SOME e |
|
178 |
| max (Branch2 (_, e, Empty)) = SOME e |
|
179 |
| max (Branch3 (_, _, _, e, Empty)) = SOME e |
|
180 |
| max (Branch2 (_, _, right)) = max right |
|
181 |
| max (Branch3 (_, _, _, _, right)) = max right |
|
182 |
| max (Size (_, arg)) = max arg; |
|
183 |
||
184 |
||
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
185 |
(* exists and forall *) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
186 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
187 |
fun exists pred = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
188 |
let |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
189 |
fun ex Empty = false |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
190 |
| ex (Leaf1 e) = pred e |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
191 |
| ex (Leaf2 (e1, e2)) = pred e1 orelse pred e2 |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
192 |
| ex (Leaf3 (e1, e2, e3)) = pred e1 orelse pred e2 orelse pred e3 |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
193 |
| ex (Branch2 (left, e, right)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
194 |
ex left orelse pred e orelse ex right |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
195 |
| ex (Branch3 (left, e1, mid, e2, right)) = |
77800 | 196 |
ex left orelse pred e1 orelse ex mid orelse pred e2 orelse ex right |
197 |
| ex (Size (_, arg)) = ex arg; |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
198 |
in ex end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
199 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
200 |
fun forall pred = not o exists (not o pred); |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
201 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
202 |
|
77728 | 203 |
(* get_first *) |
204 |
||
205 |
fun get_first f = |
|
206 |
let |
|
207 |
fun get Empty = NONE |
|
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
208 |
| get (Leaf1 e) = f e |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
209 |
| get (Leaf2 (e1, e2)) = |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
210 |
(case f e1 of |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
211 |
NONE => f e2 |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
212 |
| some => some) |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
213 |
| get (Leaf3 (e1, e2, e3)) = |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
214 |
(case f e1 of |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
215 |
NONE => |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
216 |
(case f e2 of |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
217 |
NONE => f e3 |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
218 |
| some => some) |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
219 |
| some => some) |
77728 | 220 |
| get (Branch2 (left, e, right)) = |
221 |
(case get left of |
|
222 |
NONE => |
|
223 |
(case f e of |
|
224 |
NONE => get right |
|
225 |
| some => some) |
|
226 |
| some => some) |
|
227 |
| get (Branch3 (left, e1, mid, e2, right)) = |
|
228 |
(case get left of |
|
229 |
NONE => |
|
230 |
(case f e1 of |
|
231 |
NONE => |
|
232 |
(case get mid of |
|
233 |
NONE => |
|
234 |
(case f e2 of |
|
235 |
NONE => get right |
|
236 |
| some => some) |
|
237 |
| some => some) |
|
238 |
| some => some) |
|
77800 | 239 |
| some => some) |
240 |
| get (Size (_, arg)) = get arg; |
|
77728 | 241 |
in get end; |
242 |
||
243 |
||
77875
9374e13655e8
drop unused Set().ord, which is potentially inefficient due to dict_ord/dest;
wenzelm
parents:
77836
diff
changeset
|
244 |
(* member and subset *) |
77722
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 |
fun member set elem = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
247 |
let |
77742 | 248 |
fun elem_ord e = Key.ord (elem, e) |
249 |
val elem_eq = is_equal o elem_ord; |
|
250 |
||
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
251 |
fun mem Empty = false |
77742 | 252 |
| mem (Leaf1 e) = elem_eq e |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
253 |
| mem (Leaf2 (e1, e2)) = |
77742 | 254 |
(case elem_ord e1 of |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
255 |
LESS => false |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
256 |
| EQUAL => true |
77742 | 257 |
| GREATER => elem_eq e2) |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
258 |
| mem (Leaf3 (e1, e2, e3)) = |
77742 | 259 |
(case elem_ord e2 of |
260 |
LESS => elem_eq e1 |
|
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
261 |
| EQUAL => true |
77742 | 262 |
| GREATER => elem_eq e3) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
263 |
| mem (Branch2 (left, e, right)) = |
77742 | 264 |
(case elem_ord e of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
265 |
LESS => mem left |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
266 |
| EQUAL => true |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
267 |
| GREATER => mem right) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
268 |
| mem (Branch3 (left, e1, mid, e2, right)) = |
77742 | 269 |
(case elem_ord e1 of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
270 |
LESS => mem left |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
271 |
| EQUAL => true |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
272 |
| GREATER => |
77742 | 273 |
(case elem_ord e2 of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
274 |
LESS => mem mid |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
275 |
| EQUAL => true |
77800 | 276 |
| GREATER => mem right)) |
277 |
| mem (Size (_, arg)) = mem arg; |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
278 |
in mem set end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
279 |
|
77728 | 280 |
fun subset (set1, set2) = forall (member set2) set1; |
281 |
||
77912 | 282 |
|
283 |
(* equality and order *) |
|
284 |
||
77836 | 285 |
fun eq_set (set1, set2) = |
286 |
pointer_eq (set1, set2) orelse size set1 = size set2 andalso subset (set1, set2); |
|
287 |
||
77912 | 288 |
val ord = |
289 |
pointer_eq_ord (fn (set1, set2) => |
|
290 |
(case int_ord (size set1, size set2) of |
|
291 |
EQUAL => |
|
292 |
(case get_first (fn a => if member set2 a then NONE else SOME a) set1 of |
|
293 |
NONE => EQUAL |
|
294 |
| SOME a => |
|
295 |
(case get_first (fn b => if member set1 b then NONE else SOME b) set2 of |
|
296 |
NONE => EQUAL |
|
297 |
| SOME b => Key.ord (a, b))) |
|
298 |
| order => order)); |
|
299 |
||
77728 | 300 |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
301 |
(* insert *) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
302 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
303 |
datatype growth = Stay of T | Sprout of T * elem * T; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
304 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
305 |
fun insert elem set = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
306 |
if member set elem then set |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
307 |
else |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
308 |
let |
77882
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
309 |
fun elem_ord e = Key.ord (elem, e); |
77909 | 310 |
val elem_less = is_less o elem_ord; |
77742 | 311 |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
312 |
fun ins Empty = Sprout (Empty, elem, Empty) |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
313 |
| ins (t as Leaf1 _) = ins (unmake t) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
314 |
| ins (t as Leaf2 _) = ins (unmake t) |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
315 |
| ins (t as Leaf3 _) = ins (unmake t) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
316 |
| ins (Branch2 (left, e, right)) = |
77742 | 317 |
(case elem_ord e of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
318 |
LESS => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
319 |
(case ins left of |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
320 |
Stay left' => Stay (make2 (left', e, right)) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
321 |
| Sprout (left1, e', left2) => Stay (make3 (left1, e', left2, e, right))) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
322 |
| EQUAL => Stay (make2 (left, e, right)) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
323 |
| GREATER => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
324 |
(case ins right of |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
325 |
Stay right' => Stay (make2 (left, e, right')) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
326 |
| Sprout (right1, e', right2) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
327 |
Stay (make3 (left, e, right1, e', right2)))) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
328 |
| ins (Branch3 (left, e1, mid, e2, right)) = |
77742 | 329 |
(case elem_ord e1 of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
330 |
LESS => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
331 |
(case ins left of |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
332 |
Stay left' => Stay (make3 (left', e1, mid, e2, right)) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
333 |
| Sprout (left1, e', left2) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
334 |
Sprout (make2 (left1, e', left2), e1, make2 (mid, e2, right))) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
335 |
| EQUAL => Stay (make3 (left, e1, mid, e2, right)) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
336 |
| GREATER => |
77742 | 337 |
(case elem_ord e2 of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
338 |
LESS => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
339 |
(case ins mid of |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
340 |
Stay mid' => Stay (make3 (left, e1, mid', e2, right)) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
341 |
| Sprout (mid1, e', mid2) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
342 |
Sprout (make2 (left, e1, mid1), e', make2 (mid2, e2, right))) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
343 |
| EQUAL => Stay (make3 (left, e1, mid, e2, right)) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
344 |
| GREATER => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
345 |
(case ins right of |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
346 |
Stay right' => Stay (make3 (left, e1, mid, e2, right')) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
347 |
| Sprout (right1, e', right2) => |
77800 | 348 |
Sprout (make2 (left, e1, mid), e2, make2 (right1, e', right2))))) |
349 |
| ins (Size (_, arg)) = ins arg; |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
350 |
in |
77882
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
351 |
(case set of |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
352 |
Empty => Leaf1 elem |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
353 |
| Leaf1 e => |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
354 |
if elem_less e |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
355 |
then Leaf2 (elem, e) |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
356 |
else Leaf2 (e, elem) |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
357 |
| Leaf2 (e1, e2) => |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
358 |
if elem_less e1 |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
359 |
then Leaf3 (elem, e1, e2) |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
360 |
else if elem_less e2 |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
361 |
then Leaf3 (e1, elem, e2) |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
362 |
else Leaf3 (e1, e2, elem) |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
363 |
| _ => |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
364 |
make_size (size set + 1) |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
365 |
(case ins set of |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
366 |
Stay set' => set' |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
367 |
| Sprout br => make2 br)) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
368 |
end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
369 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
370 |
fun make elems = build (fold insert elems); |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
371 |
|
77822 | 372 |
|
373 |
(* merge *) |
|
374 |
||
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
375 |
fun merge (set1, set2) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
376 |
if pointer_eq (set1, set2) then set1 |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
377 |
else if is_empty set1 then set2 |
77725 | 378 |
else if is_empty set2 then set1 |
379 |
else if size set1 >= size set2 |
|
380 |
then fold_set insert set2 set1 |
|
381 |
else fold_set insert set1 set2; |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
382 |
|
77822 | 383 |
fun merges sets = Library.foldl merge (empty, sets); |
384 |
||
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
385 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
386 |
(* remove *) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
387 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
388 |
local |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
389 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
390 |
fun compare NONE _ = LESS |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
391 |
| compare (SOME e1) e2 = Key.ord (e1, e2); |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
392 |
|
77737 | 393 |
fun if_equal ord x y = if is_equal ord then x else y; |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
394 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
395 |
exception UNDEF of elem; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
396 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
397 |
fun del (SOME k) Empty = raise UNDEF k |
77735 | 398 |
| del NONE Empty = raise Match |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
399 |
| del NONE (Leaf1 p) = (p, (true, Empty)) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
400 |
| del NONE (Leaf2 (p, q)) = (p, (false, Leaf1 q)) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
401 |
| del k (Leaf1 p) = |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
402 |
(case compare k p of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
403 |
EQUAL => (p, (true, Empty)) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
404 |
| _ => raise UNDEF (the k)) |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
405 |
| del k (Leaf2 (p, q)) = |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
406 |
(case compare k p of |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
407 |
EQUAL => (p, (false, Leaf1 q)) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
408 |
| _ => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
409 |
(case compare k q of |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
410 |
EQUAL => (q, (false, Leaf1 p)) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
411 |
| _ => raise UNDEF (the k))) |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
412 |
| del k (Leaf3 (p, q, r)) = del k (Branch2 (Leaf1 p, q, Leaf1 r)) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
413 |
| del k (Branch2 (l, p, r)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
414 |
(case compare k p of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
415 |
LESS => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
416 |
(case del k l of |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
417 |
(p', (false, l')) => (p', (false, make2 (l', p, r))) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
418 |
| (p', (true, l')) => (p', case unmake r of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
419 |
Branch2 (rl, rp, rr) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
420 |
(true, make3 (l', p, rl, rp, rr)) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
421 |
| Branch3 (rl, rp, rm, rq, rr) => (false, make2 |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
422 |
(make2 (l', p, rl), rp, make2 (rm, rq, rr))))) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
423 |
| ord => |
77737 | 424 |
(case del (if_equal ord NONE k) r of |
425 |
(p', (false, r')) => (p', (false, make2 (l, if_equal ord p' p, r'))) |
|
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
426 |
| (p', (true, r')) => (p', case unmake l of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
427 |
Branch2 (ll, lp, lr) => |
77737 | 428 |
(true, make3 (ll, lp, lr, if_equal ord p' p, r')) |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
429 |
| Branch3 (ll, lp, lm, lq, lr) => (false, make2 |
77737 | 430 |
(make2 (ll, lp, lm), lq, make2 (lr, if_equal ord p' p, r')))))) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
431 |
| del k (Branch3 (l, p, m, q, r)) = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
432 |
(case compare k q of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
433 |
LESS => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
434 |
(case compare k p of |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
435 |
LESS => |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
436 |
(case del k l of |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
437 |
(p', (false, l')) => (p', (false, make3 (l', p, m, q, r))) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
438 |
| (p', (true, l')) => (p', (false, case (unmake m, unmake r) of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
439 |
(Branch2 (ml, mp, mr), Branch2 _) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
440 |
make2 (make3 (l', p, ml, mp, mr), q, r) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
441 |
| (Branch3 (ml, mp, mm, mq, mr), _) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
442 |
make3 (make2 (l', p, ml), mp, make2 (mm, mq, mr), q, r) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
443 |
| (Branch2 (ml, mp, mr), Branch3 (rl, rp, rm, rq, rr)) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
444 |
make3 (make2 (l', p, ml), mp, make2 (mr, q, rl), rp, |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
445 |
make2 (rm, rq, rr))))) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
446 |
| ord => |
77737 | 447 |
(case del (if_equal ord NONE k) m of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
448 |
(p', (false, m')) => |
77737 | 449 |
(p', (false, make3 (l, if_equal ord p' p, m', q, r))) |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
450 |
| (p', (true, m')) => (p', (false, case (unmake l, unmake r) of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
451 |
(Branch2 (ll, lp, lr), Branch2 _) => |
77737 | 452 |
make2 (make3 (ll, lp, lr, if_equal ord p' p, m'), q, r) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
453 |
| (Branch3 (ll, lp, lm, lq, lr), _) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
454 |
make3 (make2 (ll, lp, lm), lq, |
77737 | 455 |
make2 (lr, if_equal ord p' p, m'), q, r) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
456 |
| (_, Branch3 (rl, rp, rm, rq, rr)) => |
77737 | 457 |
make3 (l, if_equal ord p' p, make2 (m', q, rl), rp, |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
458 |
make2 (rm, rq, rr)))))) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
459 |
| ord => |
77737 | 460 |
(case del (if_equal ord NONE k) r of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
461 |
(q', (false, r')) => |
77737 | 462 |
(q', (false, make3 (l, p, m, if_equal ord q' q, r'))) |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
463 |
| (q', (true, r')) => (q', (false, case (unmake l, unmake m) of |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
464 |
(Branch2 _, Branch2 (ml, mp, mr)) => |
77737 | 465 |
make2 (l, p, make3 (ml, mp, mr, if_equal ord q' q, r')) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
466 |
| (_, Branch3 (ml, mp, mm, mq, mr)) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
467 |
make3 (l, p, make2 (ml, mp, mm), mq, |
77737 | 468 |
make2 (mr, if_equal ord q' q, r')) |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
469 |
| (Branch3 (ll, lp, lm, lq, lr), Branch2 (ml, mp, mr)) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
470 |
make3 (make2 (ll, lp, lm), lq, make2 (lr, p, ml), mp, |
77800 | 471 |
make2 (mr, if_equal ord q' q, r')))))) |
472 |
| del k (Size (_, arg)) = del k arg; |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
473 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
474 |
in |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
475 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
476 |
fun remove elem set = |
77800 | 477 |
if member set elem |
478 |
then make_size (size set - 1) (snd (snd (del (SOME elem) set))) |
|
479 |
else set; |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
480 |
|
77728 | 481 |
val subtract = fold_set remove; |
482 |
||
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
483 |
end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
484 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
485 |
|
77911
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
486 |
(* conventional set operations *) |
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
487 |
|
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
488 |
fun restrict pred set = |
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
489 |
fold_set (fn x => not (pred x) ? remove x) set set; |
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
490 |
|
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
491 |
fun inter set1 set2 = |
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
492 |
if pointer_eq (set1, set2) then set1 |
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
493 |
else if is_empty set1 orelse is_empty set2 then empty |
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
494 |
else if size set1 < size set2 then restrict (member set2) set1 |
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
495 |
else restrict (member set1) set2; |
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
496 |
|
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
497 |
fun union set1 set2 = merge (set2, set1); |
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
498 |
|
b83a561086d3
more operations: following Library list operations and Ord_List.T operations;
wenzelm
parents:
77909
diff
changeset
|
499 |
|
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
500 |
(* ML pretty-printing *) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
501 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
502 |
val _ = |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
503 |
ML_system_pp (fn depth => fn _ => fn set => |
80809
4a64fc4d1cde
clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents:
77912
diff
changeset
|
504 |
ML_Pretty.enum "," "{" "}" ML_system_pretty (dest set, depth)); |
77722
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
505 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
506 |
(*final declarations of this structure!*) |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
507 |
val fold = fold_set; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
508 |
val fold_rev = fold_rev_set; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
509 |
|
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
510 |
end; |
8faf28a80a7f
efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff
changeset
|
511 |
|
77731 | 512 |
structure Intset = Set(Inttab.Key); |
513 |
structure Symset = Set(Symtab.Key); |