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