author | wenzelm |
Thu, 30 Nov 2023 12:23:47 +0100 | |
changeset 79092 | 06176f4e2e70 |
parent 77981 | f83702560730 |
child 79093 | 6c5ca8f04d60 |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/table.ML |
15014 | 2 |
Author: Markus Wenzel and Stefan Berghofer, TU Muenchen |
5015 | 3 |
|
16342 | 4 |
Generic tables. Efficient purely functional implementation using |
5 |
balanced 2-3 trees. |
|
5015 | 6 |
*) |
7 |
||
8 |
signature KEY = |
|
9 |
sig |
|
10 |
type key |
|
70586 | 11 |
val ord: key ord |
5015 | 12 |
end; |
13 |
||
14 |
signature TABLE = |
|
15 |
sig |
|
77731 | 16 |
structure Key: KEY |
5015 | 17 |
type key |
18 |
type 'a table |
|
19 |
exception DUP of key |
|
19031 | 20 |
exception SAME |
15014 | 21 |
exception UNDEF of key |
77780
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
22 |
val size: 'a table -> int |
5015 | 23 |
val empty: 'a table |
74232 | 24 |
val build: ('a table -> 'a table) -> 'a table |
5015 | 25 |
val is_empty: 'a table -> bool |
39020 | 26 |
val map: (key -> 'a -> 'b) -> 'a table -> 'b table |
16446 | 27 |
val fold: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a |
28334 | 28 |
val fold_rev: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a |
5015 | 29 |
val dest: 'a table -> (key * 'a) list |
5681 | 30 |
val keys: 'a table -> key list |
52049 | 31 |
val min: 'a table -> (key * 'a) option |
32 |
val max: 'a table -> (key * 'a) option |
|
27508 | 33 |
val exists: (key * 'a -> bool) -> 'a table -> bool |
34 |
val forall: (key * 'a -> bool) -> 'a table -> bool |
|
74227 | 35 |
val get_first: (key * 'a -> 'b option) -> 'a table -> 'b option |
43792
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
wenzelm
parents:
39020
diff
changeset
|
36 |
val lookup_key: 'a table -> key -> (key * 'a) option |
31616 | 37 |
val lookup: 'a table -> key -> 'a option |
16887 | 38 |
val defined: 'a table -> key -> bool |
31209 | 39 |
val update: key * 'a -> 'a table -> 'a table |
40 |
val update_new: key * 'a -> 'a table -> 'a table (*exception DUP*) |
|
17179 | 41 |
val default: key * 'a -> 'a table -> 'a table |
56051 | 42 |
val map_entry: key -> ('a -> 'a) (*exception SAME*) -> 'a table -> 'a table |
31209 | 43 |
val map_default: key * 'a -> ('a -> 'a) -> 'a table -> 'a table |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
21065
diff
changeset
|
44 |
val make: (key * 'a) list -> 'a table (*exception DUP*) |
56051 | 45 |
val join: (key -> 'a * 'a -> 'a) (*exception SAME*) -> |
23655
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
21065
diff
changeset
|
46 |
'a table * 'a table -> 'a table (*exception DUP*) |
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents:
21065
diff
changeset
|
47 |
val merge: ('a * 'a -> bool) -> 'a table * 'a table -> 'a table (*exception DUP*) |
77822 | 48 |
val joins: (key -> 'a * 'a -> 'a) (*exception SAME*) -> |
49 |
'a table list -> 'a table (*exception DUP*) |
|
50 |
val merges: ('a * 'a -> bool) -> 'a table list -> 'a table (*exception DUP*) |
|
15665 | 51 |
val delete: key -> 'a table -> 'a table (*exception UNDEF*) |
15761 | 52 |
val delete_safe: key -> 'a table -> 'a table |
16466 | 53 |
val member: ('b * 'a -> bool) -> 'a table -> key * 'b -> bool |
15761 | 54 |
val insert: ('a * 'a -> bool) -> key * 'a -> 'a table -> 'a table (*exception DUP*) |
16139 | 55 |
val remove: ('b * 'a -> bool) -> key * 'b -> 'a table -> 'a table |
18946 | 56 |
val lookup_list: 'a list table -> key -> 'a list |
31209 | 57 |
val cons_list: key * 'a -> 'a list table -> 'a list table |
19506 | 58 |
val insert_list: ('a * 'a -> bool) -> key * 'a -> 'a list table -> 'a list table |
77981 | 59 |
val update_list: ('a * 'a -> bool) -> key * 'a -> 'a list table -> 'a list table |
18946 | 60 |
val remove_list: ('b * 'a -> bool) -> key * 'b -> 'a list table -> 'a list table |
61 |
val make_list: (key * 'a) list -> 'a list table |
|
62 |
val dest_list: 'a list table -> (key * 'a) list |
|
33162 | 63 |
val merge_list: ('a * 'a -> bool) -> 'a list table * 'a list table -> 'a list table |
50234 | 64 |
type set = unit table |
63511 | 65 |
val insert_set: key -> set -> set |
67529 | 66 |
val remove_set: key -> set -> set |
59012
f4e9bd04e1d5
clarified Table.make_set: duplicate arguments are allowed, like Table.make_list or Scala Set() formation;
wenzelm
parents:
56051
diff
changeset
|
67 |
val make_set: key list -> set |
5015 | 68 |
end; |
69 |
||
31971
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31621
diff
changeset
|
70 |
functor Table(Key: KEY): TABLE = |
5015 | 71 |
struct |
72 |
||
77733 | 73 |
(* keys *) |
74 |
||
77731 | 75 |
structure Key = Key; |
76 |
type key = Key.key; |
|
5015 | 77 |
|
77733 | 78 |
exception DUP of key; |
79 |
||
5015 | 80 |
|
77731 | 81 |
(* datatype *) |
5015 | 82 |
|
83 |
datatype 'a table = |
|
84 |
Empty | |
|
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
85 |
Leaf1 of key * 'a | |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
86 |
Leaf2 of (key * 'a) * (key * 'a) | |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
87 |
Leaf3 of (key * 'a) * (key * 'a) * (key * 'a) | |
5015 | 88 |
Branch2 of 'a table * (key * 'a) * 'a table | |
77800 | 89 |
Branch3 of 'a table * (key * 'a) * 'a table * (key * 'a) * 'a table | |
90 |
Size of int * 'a table; |
|
5015 | 91 |
|
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
92 |
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
|
93 |
| 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
|
94 |
| make2 (left, e1, Branch2 (Empty, e2, Empty)) = make2 (left, e1, Leaf1 e2) |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
95 |
| make2 (Branch3 (Empty, e1, Empty, e2, Empty), e3, right) = make2 (Leaf2 (e1, e2), e3, right) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
96 |
| make2 (left, e1, Branch3 (Empty, e2, Empty, e3, Empty)) = make2 (left, e1, Leaf2 (e2, e3)) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
97 |
| make2 (Leaf1 e1, e2, Empty) = Leaf2 (e1, e2) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
98 |
| make2 (Empty, e1, Leaf1 e2) = Leaf2 (e1, e2) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
99 |
| make2 (Leaf1 e1, e2, Leaf1 e3) = Leaf3 (e1, e2, e3) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
100 |
| make2 (Leaf2 (e1, e2), e3, Empty) = Leaf3 (e1, e2, e3) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
101 |
| 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
|
102 |
| 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
|
103 |
|
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
104 |
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
|
105 |
| 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
|
106 |
| 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
|
107 |
| make3 (left, e1, mid, e2, Branch2 (Empty, e3, Empty)) = make3 (left, e1, mid, e2, Leaf1 e3) |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
108 |
| make3 (Leaf1 e1, e2, Empty, e3, Empty) = Leaf3 (e1, e2, e3) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
109 |
| make3 (Empty, e1, Leaf1 e2, e3, Empty) = Leaf3 (e1, e2, e3) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
110 |
| 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
|
111 |
| 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
|
112 |
|
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
113 |
fun unmake (Leaf1 e) = Branch2 (Empty, e, Empty) |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
114 |
| unmake (Leaf2 (e1, e2)) = Branch3 (Empty, e1, Empty, e2, Empty) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
115 |
| unmake (Leaf3 (e1, e2, e3)) = Branch2 (Branch2 (Empty, e1, Empty), e2, Branch2 (Empty, e3, Empty)) |
77800 | 116 |
| 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
|
117 |
| unmake arg = arg; |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
118 |
|
5015 | 119 |
|
77780
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
120 |
(* size *) |
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
121 |
|
77800 | 122 |
(*literal copy from set.ML*) |
77780
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
123 |
local |
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
124 |
fun count Empty n = n |
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
125 |
| count (Leaf1 _) n = n + 1 |
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
126 |
| count (Leaf2 _) n = n + 2 |
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
127 |
| count (Leaf3 _) n = n + 3 |
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
128 |
| count (Branch2 (left, _, right)) n = count right (count left (n + 1)) |
77800 | 129 |
| count (Branch3 (left, _, mid, _, right)) n = count right (count mid (count left (n + 2))) |
130 |
| 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
|
131 |
|
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
132 |
fun box (Branch2 _) = 1 |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
133 |
| box (Branch3 _) = 1 |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
134 |
| box _ = 0; |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
135 |
|
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
136 |
fun bound arg b = |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
137 |
if b > 0 then |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
138 |
(case arg of |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
139 |
Branch2 (left, _, right) => |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
140 |
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
|
141 |
| Branch3 (left, _, mid, _, right) => |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
142 |
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
|
143 |
| _ => b) |
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
144 |
else b; |
77780
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
145 |
in |
77802
25c114e2528e
performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents:
77800
diff
changeset
|
146 |
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
|
147 |
fun make_size m arg = if bound arg 3 <= 0 then Size (m, arg) else arg; |
77780
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
148 |
end; |
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
149 |
|
97febdb6ee58
clarified signature: more uniform Table() vs. Set();
wenzelm
parents:
77768
diff
changeset
|
150 |
|
77743 | 151 |
(* empty *) |
5681 | 152 |
|
5015 | 153 |
val empty = Empty; |
154 |
||
74232 | 155 |
fun build (f: 'a table -> 'a table) = f empty; |
156 |
||
77800 | 157 |
(*literal copy from set.ML*) |
5015 | 158 |
fun is_empty Empty = true |
77800 | 159 |
| is_empty (Size (_, arg)) = is_empty arg |
5015 | 160 |
| is_empty _ = false; |
161 |
||
5681 | 162 |
|
163 |
(* map and fold combinators *) |
|
164 |
||
16657 | 165 |
fun map_table f = |
166 |
let |
|
167 |
fun map Empty = Empty |
|
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
168 |
| map (Leaf1 (k, x)) = Leaf1 (k, f k x) |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
169 |
| map (Leaf2 ((k1, x1), (k2, x2))) = |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
170 |
Leaf2 ((k1, f k1 x1), (k2, f k2 x2)) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
171 |
| map (Leaf3 ((k1, x1), (k2, x2), (k3, x3))) = |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
172 |
Leaf3 ((k1, f k1 x1), (k2, f k2 x2), (k3, f k3 x3)) |
16657 | 173 |
| map (Branch2 (left, (k, x), right)) = |
174 |
Branch2 (map left, (k, f k x), map right) |
|
175 |
| map (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = |
|
77800 | 176 |
Branch3 (map left, (k1, f k1 x1), map mid, (k2, f k2 x2), map right) |
177 |
| map (Size (m, arg)) = Size (m, map arg); |
|
16657 | 178 |
in map end; |
5681 | 179 |
|
16657 | 180 |
fun fold_table f = |
181 |
let |
|
77813 | 182 |
fun fold Empty a = a |
183 |
| fold (Leaf1 e) a = f e a |
|
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
184 |
| fold (Leaf2 (e1, e2)) a = f e2 (f e1 a) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
185 |
| fold (Leaf3 (e1, e2, e3)) a = f e3 (f e2 (f e1 a)) |
77813 | 186 |
| fold (Branch2 (left, e, right)) a = |
187 |
fold right (f e (fold left a)) |
|
188 |
| fold (Branch3 (left, e1, mid, e2, right)) a = |
|
189 |
fold right (f e2 (fold mid (f e1 (fold left a)))) |
|
190 |
| fold (Size (_, arg)) a = fold arg a; |
|
16657 | 191 |
in fold end; |
5681 | 192 |
|
28334 | 193 |
fun fold_rev_table f = |
194 |
let |
|
77813 | 195 |
fun fold_rev Empty a = a |
196 |
| fold_rev (Leaf1 e) a = f e a |
|
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
197 |
| fold_rev (Leaf2 (e1, e2)) a = f e1 (f e2 a) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
198 |
| fold_rev (Leaf3 (e1, e2, e3)) a = f e1 (f e2 (f e3 a)) |
77813 | 199 |
| fold_rev (Branch2 (left, e, right)) a = |
200 |
fold_rev left (f e (fold_rev right a)) |
|
201 |
| fold_rev (Branch3 (left, e1, mid, e2, right)) a = |
|
202 |
fold_rev left (f e1 (fold_rev mid (f e2 (fold_rev right a)))) |
|
203 |
| fold_rev (Size (_, arg)) a = fold_rev arg a; |
|
77732 | 204 |
in fold_rev end; |
28334 | 205 |
|
77768 | 206 |
fun dest tab = Library.build (fold_rev_table cons tab); |
207 |
fun keys tab = Library.build (fold_rev_table (cons o #1) tab); |
|
16192 | 208 |
|
27508 | 209 |
|
52049 | 210 |
(* min/max entries *) |
5015 | 211 |
|
52049 | 212 |
fun min Empty = NONE |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
213 |
| min (Leaf1 e) = SOME e |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
214 |
| min (Leaf2 (e, _)) = SOME e |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
215 |
| min (Leaf3 (e, _, _)) = SOME e |
77727
b98edf66ca96
tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents:
77721
diff
changeset
|
216 |
| min (Branch2 (Empty, e, _)) = SOME e |
b98edf66ca96
tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents:
77721
diff
changeset
|
217 |
| min (Branch3 (Empty, e, _, _, _)) = SOME e |
52049 | 218 |
| min (Branch2 (left, _, _)) = min left |
77800 | 219 |
| min (Branch3 (left, _, _, _, _)) = min left |
220 |
| min (Size (_, arg)) = min arg; |
|
8409 | 221 |
|
52049 | 222 |
fun max Empty = NONE |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
223 |
| max (Leaf1 e) = SOME e |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
224 |
| max (Leaf2 (_, e)) = SOME e |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
225 |
| max (Leaf3 (_, _, e)) = SOME e |
77727
b98edf66ca96
tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents:
77721
diff
changeset
|
226 |
| max (Branch2 (_, e, Empty)) = SOME e |
b98edf66ca96
tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents:
77721
diff
changeset
|
227 |
| max (Branch3 (_, _, _, e, Empty)) = SOME e |
52049 | 228 |
| max (Branch2 (_, _, right)) = max right |
77800 | 229 |
| max (Branch3 (_, _, _, _, right)) = max right |
230 |
| max (Size (_, arg)) = max arg; |
|
15665 | 231 |
|
5015 | 232 |
|
74227 | 233 |
(* exists and forall *) |
234 |
||
235 |
fun exists pred = |
|
236 |
let |
|
237 |
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
|
238 |
| ex (Leaf1 e) = pred e |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
239 |
| ex (Leaf2 (e1, e2)) = pred e1 orelse pred e2 |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
240 |
| ex (Leaf3 (e1, e2, e3)) = pred e1 orelse pred e2 orelse pred e3 |
77727
b98edf66ca96
tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents:
77721
diff
changeset
|
241 |
| ex (Branch2 (left, e, right)) = |
b98edf66ca96
tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents:
77721
diff
changeset
|
242 |
ex left orelse pred e orelse ex right |
b98edf66ca96
tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents:
77721
diff
changeset
|
243 |
| ex (Branch3 (left, e1, mid, e2, right)) = |
77800 | 244 |
ex left orelse pred e1 orelse ex mid orelse pred e2 orelse ex right |
245 |
| ex (Size (_, arg)) = ex arg; |
|
74227 | 246 |
in ex end; |
247 |
||
248 |
fun forall pred = not o exists (not o pred); |
|
249 |
||
250 |
||
31616 | 251 |
(* get_first *) |
252 |
||
35012
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents:
33162
diff
changeset
|
253 |
fun get_first f = |
31616 | 254 |
let |
35012
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents:
33162
diff
changeset
|
255 |
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
|
256 |
| get (Leaf1 e) = f e |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
257 |
| get (Leaf2 (e1, e2)) = |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
258 |
(case f e1 of |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
259 |
NONE => f e2 |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
260 |
| some => some) |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
261 |
| get (Leaf3 (e1, e2, e3)) = |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
262 |
(case f e1 of |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
263 |
NONE => |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
264 |
(case f e2 of |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
265 |
NONE => f e3 |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
266 |
| some => some) |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
267 |
| some => some) |
77727
b98edf66ca96
tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents:
77721
diff
changeset
|
268 |
| get (Branch2 (left, e, right)) = |
35012
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents:
33162
diff
changeset
|
269 |
(case get left of |
31616 | 270 |
NONE => |
77727
b98edf66ca96
tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents:
77721
diff
changeset
|
271 |
(case f e of |
31616 | 272 |
NONE => get right |
273 |
| some => some) |
|
274 |
| some => some) |
|
77727
b98edf66ca96
tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents:
77721
diff
changeset
|
275 |
| get (Branch3 (left, e1, mid, e2, right)) = |
35012
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents:
33162
diff
changeset
|
276 |
(case get left of |
31616 | 277 |
NONE => |
77727
b98edf66ca96
tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents:
77721
diff
changeset
|
278 |
(case f e1 of |
31616 | 279 |
NONE => |
35012
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents:
33162
diff
changeset
|
280 |
(case get mid of |
31616 | 281 |
NONE => |
77727
b98edf66ca96
tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents:
77721
diff
changeset
|
282 |
(case f e2 of |
31616 | 283 |
NONE => get right |
284 |
| some => some) |
|
285 |
| some => some) |
|
286 |
| some => some) |
|
77800 | 287 |
| some => some) |
288 |
| get (Size (_, arg)) = get arg; |
|
35012
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents:
33162
diff
changeset
|
289 |
in get end; |
31616 | 290 |
|
291 |
||
5015 | 292 |
(* lookup *) |
293 |
||
67557 | 294 |
fun lookup tab key = |
295 |
let |
|
77742 | 296 |
fun key_ord k = Key.ord (key, k); |
297 |
val key_eq = is_equal o key_ord; |
|
298 |
||
67557 | 299 |
fun look Empty = NONE |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
300 |
| look (Leaf1 (k, x)) = |
77742 | 301 |
if key_eq k then SOME x else NONE |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
302 |
| look (Leaf2 ((k1, x1), (k2, x2))) = |
77742 | 303 |
(case key_ord k1 of |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
304 |
LESS => NONE |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
305 |
| EQUAL => SOME x1 |
77742 | 306 |
| GREATER => if key_eq k2 then SOME x2 else NONE) |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
307 |
| look (Leaf3 ((k1, x1), (k2, x2), (k3, x3))) = |
77742 | 308 |
(case key_ord k2 of |
309 |
LESS => if key_eq k1 then SOME x1 else NONE |
|
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
310 |
| EQUAL => SOME x2 |
77742 | 311 |
| GREATER => if key_eq k3 then SOME x3 else NONE) |
67557 | 312 |
| look (Branch2 (left, (k, x), right)) = |
77742 | 313 |
(case key_ord k of |
67557 | 314 |
LESS => look left |
315 |
| EQUAL => SOME x |
|
316 |
| GREATER => look right) |
|
317 |
| look (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = |
|
77742 | 318 |
(case key_ord k1 of |
67557 | 319 |
LESS => look left |
320 |
| EQUAL => SOME x1 |
|
321 |
| GREATER => |
|
77742 | 322 |
(case key_ord k2 of |
67557 | 323 |
LESS => look mid |
324 |
| EQUAL => SOME x2 |
|
77800 | 325 |
| GREATER => look right)) |
326 |
| look (Size (_, arg)) = look arg; |
|
67557 | 327 |
in look tab end; |
328 |
||
43792
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
wenzelm
parents:
39020
diff
changeset
|
329 |
fun lookup_key tab key = |
19031 | 330 |
let |
77742 | 331 |
fun key_ord k = Key.ord (key, k); |
332 |
val key_eq = is_equal o key_ord; |
|
333 |
||
19031 | 334 |
fun look Empty = NONE |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
335 |
| look (Leaf1 (k, x)) = |
77742 | 336 |
if key_eq k then SOME (k, x) else NONE |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
337 |
| look (Leaf2 ((k1, x1), (k2, x2))) = |
77742 | 338 |
(case key_ord k1 of |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
339 |
LESS => NONE |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
340 |
| EQUAL => SOME (k1, x1) |
77742 | 341 |
| GREATER => if key_eq k2 then SOME (k2, x2) else NONE) |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
342 |
| look (Leaf3 ((k1, x1), (k2, x2), (k3, x3))) = |
77742 | 343 |
(case key_ord k2 of |
344 |
LESS => if key_eq k1 then SOME (k1, x1) else NONE |
|
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
345 |
| EQUAL => SOME (k2, x2) |
77742 | 346 |
| GREATER => if key_eq k3 then SOME (k3, x3) else NONE) |
19031 | 347 |
| look (Branch2 (left, (k, x), right)) = |
77742 | 348 |
(case key_ord k of |
19031 | 349 |
LESS => look left |
43792
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
wenzelm
parents:
39020
diff
changeset
|
350 |
| EQUAL => SOME (k, x) |
19031 | 351 |
| GREATER => look right) |
352 |
| look (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = |
|
77742 | 353 |
(case key_ord k1 of |
19031 | 354 |
LESS => look left |
43792
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
wenzelm
parents:
39020
diff
changeset
|
355 |
| EQUAL => SOME (k1, x1) |
19031 | 356 |
| GREATER => |
77742 | 357 |
(case key_ord k2 of |
19031 | 358 |
LESS => look mid |
43792
d5803c3d537a
Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
wenzelm
parents:
39020
diff
changeset
|
359 |
| EQUAL => SOME (k2, x2) |
77800 | 360 |
| GREATER => look right)) |
361 |
| look (Size (_, arg)) = look arg; |
|
19031 | 362 |
in look tab end; |
5015 | 363 |
|
19031 | 364 |
fun defined tab key = |
365 |
let |
|
77742 | 366 |
fun key_ord k = Key.ord (key, k); |
367 |
val key_eq = is_equal o key_ord; |
|
368 |
||
19031 | 369 |
fun def Empty = false |
77742 | 370 |
| def (Leaf1 (k, _)) = key_eq k |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
371 |
| def (Leaf2 ((k1, _), (k2, _))) = |
77742 | 372 |
(case key_ord k1 of |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
373 |
LESS => false |
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
374 |
| EQUAL => true |
77742 | 375 |
| GREATER => key_eq k2) |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
376 |
| def (Leaf3 ((k1, _), (k2, _), (k3, _))) = |
77742 | 377 |
(case key_ord k2 of |
378 |
LESS => key_eq k1 |
|
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
379 |
| EQUAL => true |
77742 | 380 |
| GREATER => key_eq k3) |
77735 | 381 |
| def (Branch2 (left, (k, _), right)) = |
77742 | 382 |
(case key_ord k of |
19031 | 383 |
LESS => def left |
384 |
| EQUAL => true |
|
385 |
| GREATER => def right) |
|
77735 | 386 |
| def (Branch3 (left, (k1, _), mid, (k2, _), right)) = |
77742 | 387 |
(case key_ord k1 of |
19031 | 388 |
LESS => def left |
389 |
| EQUAL => true |
|
390 |
| GREATER => |
|
77742 | 391 |
(case key_ord k2 of |
19031 | 392 |
LESS => def mid |
393 |
| EQUAL => true |
|
77800 | 394 |
| GREATER => def right)) |
395 |
| def (Size (_, arg)) = def arg; |
|
19031 | 396 |
in def tab end; |
16887 | 397 |
|
5015 | 398 |
|
19031 | 399 |
(* modify *) |
5015 | 400 |
|
401 |
datatype 'a growth = |
|
402 |
Stay of 'a table | |
|
403 |
Sprout of 'a table * (key * 'a) * 'a table; |
|
404 |
||
15761 | 405 |
exception SAME; |
406 |
||
15665 | 407 |
fun modify key f tab = |
408 |
let |
|
77742 | 409 |
fun key_ord k = Key.ord (key, k); |
410 |
||
77800 | 411 |
val inc = Unsynchronized.ref 0; |
412 |
fun insert () = f NONE before ignore (Unsynchronized.inc inc); |
|
413 |
fun update x = f (SOME x); |
|
414 |
||
415 |
fun modfy Empty = Sprout (Empty, (key, insert ()), Empty) |
|
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
416 |
| modfy (t as Leaf1 _) = modfy (unmake t) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
417 |
| modfy (t as Leaf2 _) = modfy (unmake t) |
77740
19c539f5d4d3
more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents:
77739
diff
changeset
|
418 |
| modfy (t as Leaf3 _) = modfy (unmake t) |
15665 | 419 |
| modfy (Branch2 (left, p as (k, x), right)) = |
77742 | 420 |
(case key_ord k of |
15665 | 421 |
LESS => |
422 |
(case modfy left of |
|
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
423 |
Stay left' => Stay (make2 (left', p, right)) |
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
424 |
| Sprout (left1, q, left2) => Stay (make3 (left1, q, left2, p, right))) |
77800 | 425 |
| EQUAL => Stay (make2 (left, (k, update x), right)) |
15665 | 426 |
| GREATER => |
427 |
(case modfy right of |
|
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
428 |
Stay right' => Stay (make2 (left, p, right')) |
15665 | 429 |
| Sprout (right1, q, right2) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
430 |
Stay (make3 (left, p, right1, q, right2)))) |
15665 | 431 |
| modfy (Branch3 (left, p1 as (k1, x1), mid, p2 as (k2, x2), right)) = |
77742 | 432 |
(case key_ord k1 of |
5015 | 433 |
LESS => |
15665 | 434 |
(case modfy left of |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
435 |
Stay left' => Stay (make3 (left', p1, mid, p2, right)) |
15665 | 436 |
| Sprout (left1, q, left2) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
437 |
Sprout (make2 (left1, q, left2), p1, make2 (mid, p2, right))) |
77800 | 438 |
| EQUAL => Stay (make3 (left, (k1, update x1), mid, p2, right)) |
5015 | 439 |
| GREATER => |
77742 | 440 |
(case key_ord k2 of |
15665 | 441 |
LESS => |
442 |
(case modfy mid of |
|
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
443 |
Stay mid' => Stay (make3 (left, p1, mid', p2, right)) |
15665 | 444 |
| Sprout (mid1, q, mid2) => |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
445 |
Sprout (make2 (left, p1, mid1), q, make2 (mid2, p2, right))) |
77800 | 446 |
| EQUAL => Stay (make3 (left, p1, mid, (k2, update x2), right)) |
15665 | 447 |
| GREATER => |
448 |
(case modfy right of |
|
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
449 |
Stay right' => Stay (make3 (left, p1, mid, p2, right')) |
15665 | 450 |
| Sprout (right1, q, right2) => |
77800 | 451 |
Sprout (make2 (left, p1, mid), p2, make2 (right1, q, right2))))) |
452 |
| modfy (Size (_, arg)) = modfy arg; |
|
5015 | 453 |
|
77800 | 454 |
val tab' = |
77882
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
455 |
(case tab of |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
456 |
Empty => Leaf1 (key, insert ()) |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
457 |
| Leaf1 (k, x) => |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
458 |
(case key_ord k of |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
459 |
LESS => Leaf2 ((key, insert ()), (k, x)) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
460 |
| EQUAL => Leaf1 ((k, update x)) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
461 |
| GREATER => Leaf2 ((k, x), (key, insert ()))) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
462 |
| Leaf2 ((k1, x1), (k2, x2)) => |
77882
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
463 |
(case key_ord k1 of |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
464 |
LESS => Leaf3 ((key, insert ()), (k1, x1), (k2, x2)) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
465 |
| EQUAL => Leaf2 ((k1, update x1), (k2, x2)) |
77882
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
466 |
| GREATER => |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
467 |
(case key_ord k2 of |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
468 |
LESS => Leaf3 ((k1, x1), (key, insert ()), (k2, x2)) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
469 |
| EQUAL => Leaf2 ((k1, x1), (k2, update x2)) |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
470 |
| GREATER => Leaf3 ((k1, x1), (k2, x2), (key, insert ())))) |
77882
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
471 |
| _ => |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
472 |
(case modfy tab of |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
473 |
Stay tab' => tab' |
bb7238e7d2d9
minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents:
77881
diff
changeset
|
474 |
| Sprout br => make2 br)); |
15665 | 475 |
in |
77800 | 476 |
make_size (size tab + !inc) tab' |
477 |
end handle SAME => tab; |
|
5015 | 478 |
|
17412 | 479 |
fun update (key, x) tab = modify key (fn _ => x) tab; |
480 |
fun update_new (key, x) tab = modify key (fn NONE => x | SOME _ => raise DUP key) tab; |
|
17709 | 481 |
fun default (key, x) tab = modify key (fn NONE => x | SOME _ => raise SAME) tab; |
15761 | 482 |
fun map_entry key f = modify key (fn NONE => raise SAME | SOME x => f x); |
27783 | 483 |
fun map_default (key, x) f = modify key (fn NONE => f x | SOME y => f y); |
5015 | 484 |
|
485 |
||
15014 | 486 |
(* delete *) |
487 |
||
15665 | 488 |
exception UNDEF of key; |
489 |
||
490 |
local |
|
491 |
||
77735 | 492 |
fun compare NONE _ = LESS |
15665 | 493 |
| compare (SOME k1) (k2, _) = Key.ord (k1, k2); |
15014 | 494 |
|
77737 | 495 |
fun if_equal ord x y = if is_equal ord then x else y; |
15014 | 496 |
|
15531 | 497 |
fun del (SOME k) Empty = raise UNDEF k |
77735 | 498 |
| 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
|
499 |
| del NONE (Leaf1 p) = (p, (true, Empty)) |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
500 |
| del NONE (Leaf2 (e1, e2)) = (e1, (false, Leaf1 e2)) |
77736
570f1436fe0a
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents:
77735
diff
changeset
|
501 |
| del k (Leaf1 p) = |
77721 | 502 |
(case compare k p of |
503 |
EQUAL => (p, (true, Empty)) |
|
504 |
| _ => raise UNDEF (the k)) |
|
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
505 |
| del k (Leaf2 (e1, e2)) = |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
506 |
(case compare k e1 of |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
507 |
EQUAL => (e1, (false, Leaf1 e2)) |
77721 | 508 |
| _ => |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
509 |
(case compare k e2 of |
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
510 |
EQUAL => (e2, (false, Leaf1 e1)) |
77721 | 511 |
| _ => raise UNDEF (the k))) |
79092
06176f4e2e70
slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents:
77981
diff
changeset
|
512 |
| del k (Leaf3 (e1, e2, e3)) = del k (Branch2 (Leaf1 e1, e2, Leaf1 e3)) |
77721 | 513 |
| del k (Branch2 (l, p, r)) = |
514 |
(case compare k p of |
|
515 |
LESS => |
|
516 |
(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
|
517 |
(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
|
518 |
| (p', (true, l')) => (p', case unmake r of |
77721 | 519 |
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
|
520 |
(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
|
521 |
| 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
|
522 |
(make2 (l', p, rl), rp, make2 (rm, rq, rr))))) |
77721 | 523 |
| ord => |
77737 | 524 |
(case del (if_equal ord NONE k) r of |
525 |
(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
|
526 |
| (p', (true, r')) => (p', case unmake l of |
77721 | 527 |
Branch2 (ll, lp, lr) => |
77737 | 528 |
(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
|
529 |
| Branch3 (ll, lp, lm, lq, lr) => (false, make2 |
77737 | 530 |
(make2 (ll, lp, lm), lq, make2 (lr, if_equal ord p' p, r')))))) |
77721 | 531 |
| del k (Branch3 (l, p, m, q, r)) = |
532 |
(case compare k q of |
|
533 |
LESS => |
|
534 |
(case compare k p of |
|
535 |
LESS => |
|
536 |
(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
|
537 |
(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
|
538 |
| (p', (true, l')) => (p', (false, case (unmake m, unmake r) of |
77721 | 539 |
(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
|
540 |
make2 (make3 (l', p, ml, mp, mr), q, r) |
77721 | 541 |
| (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
|
542 |
make3 (make2 (l', p, ml), mp, make2 (mm, mq, mr), q, r) |
77721 | 543 |
| (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
|
544 |
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
|
545 |
make2 (rm, rq, rr))))) |
77721 | 546 |
| ord => |
77737 | 547 |
(case del (if_equal ord NONE k) m of |
77721 | 548 |
(p', (false, m')) => |
77737 | 549 |
(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
|
550 |
| (p', (true, m')) => (p', (false, case (unmake l, unmake r) of |
77721 | 551 |
(Branch2 (ll, lp, lr), Branch2 _) => |
77737 | 552 |
make2 (make3 (ll, lp, lr, if_equal ord p' p, m'), q, r) |
77721 | 553 |
| (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
|
554 |
make3 (make2 (ll, lp, lm), lq, |
77737 | 555 |
make2 (lr, if_equal ord p' p, m'), q, r) |
77721 | 556 |
| (_, Branch3 (rl, rp, rm, rq, rr)) => |
77737 | 557 |
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
|
558 |
make2 (rm, rq, rr)))))) |
77721 | 559 |
| ord => |
77737 | 560 |
(case del (if_equal ord NONE k) r of |
77721 | 561 |
(q', (false, r')) => |
77737 | 562 |
(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
|
563 |
| (q', (true, r')) => (q', (false, case (unmake l, unmake m) of |
77721 | 564 |
(Branch2 _, Branch2 (ml, mp, mr)) => |
77737 | 565 |
make2 (l, p, make3 (ml, mp, mr, if_equal ord q' q, r')) |
77721 | 566 |
| (_, 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
|
567 |
make3 (l, p, make2 (ml, mp, mm), mq, |
77737 | 568 |
make2 (mr, if_equal ord q' q, r')) |
77721 | 569 |
| (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
|
570 |
make3 (make2 (ll, lp, lm), lq, make2 (lr, p, ml), mp, |
77800 | 571 |
make2 (mr, if_equal ord q' q, r')))))) |
572 |
| del k (Size (_, arg)) = del k arg; |
|
15665 | 573 |
in |
574 |
||
77800 | 575 |
fun delete key tab = make_size (size tab - 1) (snd (snd (del (SOME key) tab))); |
44336
59ff5a93eef4
tuned Table.delete_safe: avoid potentially expensive attempt of delete;
wenzelm
parents:
43792
diff
changeset
|
576 |
fun delete_safe key tab = if defined tab key then delete key tab else tab; |
15014 | 577 |
|
15665 | 578 |
end; |
579 |
||
15014 | 580 |
|
19031 | 581 |
(* membership operations *) |
16466 | 582 |
|
583 |
fun member eq tab (key, x) = |
|
17412 | 584 |
(case lookup tab key of |
16466 | 585 |
NONE => false |
586 |
| SOME y => eq (x, y)); |
|
15761 | 587 |
|
588 |
fun insert eq (key, x) = |
|
589 |
modify key (fn NONE => x | SOME y => if eq (x, y) then raise SAME else raise DUP key); |
|
590 |
||
591 |
fun remove eq (key, x) tab = |
|
17412 | 592 |
(case lookup tab key of |
15761 | 593 |
NONE => tab |
594 |
| SOME y => if eq (x, y) then delete key tab else tab); |
|
595 |
||
596 |
||
19031 | 597 |
(* simultaneous modifications *) |
598 |
||
74266 | 599 |
fun make entries = build (fold update_new entries); |
5015 | 600 |
|
77781 | 601 |
fun join f (tab1, tab2) = |
55727
7e330ae052bb
optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents:
52049
diff
changeset
|
602 |
let |
7e330ae052bb
optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents:
52049
diff
changeset
|
603 |
fun add (key, y) tab = modify key (fn NONE => y | SOME x => f key (x, y)) tab; |
7e330ae052bb
optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents:
52049
diff
changeset
|
604 |
in |
77781 | 605 |
if pointer_eq (tab1, tab2) then tab1 |
606 |
else if is_empty tab1 then tab2 |
|
607 |
else fold_table add tab2 tab1 |
|
55727
7e330ae052bb
optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents:
52049
diff
changeset
|
608 |
end; |
12287 | 609 |
|
19031 | 610 |
fun merge eq = join (fn key => fn xy => if eq xy then raise SAME else raise DUP key); |
5015 | 611 |
|
77822 | 612 |
fun joins f tabs = Library.foldl (join f) (empty, tabs); |
613 |
fun merges eq tabs = Library.foldl (merge eq) (empty, tabs); |
|
614 |
||
5015 | 615 |
|
19031 | 616 |
(* list tables *) |
15761 | 617 |
|
18946 | 618 |
fun lookup_list tab key = these (lookup tab key); |
25391 | 619 |
|
620 |
fun cons_list (key, x) tab = modify key (fn NONE => [x] | SOME xs => x :: xs) tab; |
|
5015 | 621 |
|
77981 | 622 |
fun modify_list key f = |
623 |
modify key (fn arg => |
|
624 |
let |
|
625 |
val xs = the_default [] arg; |
|
626 |
val ys = f xs; |
|
627 |
in if pointer_eq (xs, ys) then raise SAME else ys end); |
|
628 |
||
629 |
fun insert_list eq (key, x) = modify_list key (Library.insert eq x); |
|
630 |
fun update_list eq (key, x) = modify_list key (Library.update eq x); |
|
25391 | 631 |
|
18946 | 632 |
fun remove_list eq (key, x) tab = |
77981 | 633 |
map_entry key (fn xs => |
634 |
(case Library.remove eq x xs of |
|
635 |
[] => raise UNDEF key |
|
636 |
| ys => if pointer_eq (xs, ys) then raise SAME else ys)) tab |
|
15761 | 637 |
handle UNDEF _ => delete key tab; |
5015 | 638 |
|
74266 | 639 |
fun make_list args = build (fold_rev cons_list args); |
19482
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
wenzelm
parents:
19073
diff
changeset
|
640 |
fun dest_list tab = maps (fn (key, xs) => map (pair key) xs) (dest tab); |
77973 | 641 |
fun merge_list eq = |
77975 | 642 |
join (fn _ => fn xy => if eq_list eq xy then raise SAME else Library.merge eq xy); |
5015 | 643 |
|
644 |
||
74227 | 645 |
(* set operations *) |
50234 | 646 |
|
647 |
type set = unit table; |
|
648 |
||
67529 | 649 |
fun insert_set x = default (x, ()); |
650 |
fun remove_set x : set -> set = delete_safe x; |
|
74266 | 651 |
fun make_set xs = build (fold insert_set xs); |
50234 | 652 |
|
653 |
||
47980
c81801f881b3
simplified Poly/ML setup -- 5.3.0 is now the common base-line;
wenzelm
parents:
44336
diff
changeset
|
654 |
(* ML pretty-printing *) |
38635
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents:
35012
diff
changeset
|
655 |
|
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents:
35012
diff
changeset
|
656 |
val _ = |
62819
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
wenzelm
parents:
62503
diff
changeset
|
657 |
ML_system_pp (fn depth => fn pretty => fn tab => |
62503 | 658 |
ML_Pretty.to_polyml |
38635
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents:
35012
diff
changeset
|
659 |
(ML_Pretty.enum "," "{" "}" |
62503 | 660 |
(ML_Pretty.pair |
62819
d3ff367a16a0
careful export of type-dependent functions, without losing their special status;
wenzelm
parents:
62503
diff
changeset
|
661 |
(ML_Pretty.from_polyml o ML_system_pretty) |
62503 | 662 |
(ML_Pretty.from_polyml o pretty)) |
38635
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents:
35012
diff
changeset
|
663 |
(dest tab, depth))); |
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents:
35012
diff
changeset
|
664 |
|
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents:
35012
diff
changeset
|
665 |
|
5681 | 666 |
(*final declarations of this structure!*) |
39020 | 667 |
val map = map_table; |
16446 | 668 |
val fold = fold_table; |
28334 | 669 |
val fold_rev = fold_rev_table; |
5015 | 670 |
|
671 |
end; |
|
672 |
||
31971
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31621
diff
changeset
|
673 |
structure Inttab = Table(type key = int val ord = int_ord); |
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31621
diff
changeset
|
674 |
structure Symtab = Table(type key = string val ord = fast_string_ord); |
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents:
31621
diff
changeset
|
675 |
structure Symreltab = Table(type key = string * string |
30647
ef8f46c3158a
added Symreltab (binary relations of symbols) instance of TableFun
haftmann
parents:
30290
diff
changeset
|
676 |
val ord = prod_ord fast_string_ord fast_string_ord); |