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