author | wenzelm |
Tue, 19 Jul 2005 20:47:01 +0200 | |
changeset 16894 | 40f80823b451 |
parent 16887 | b24c067b32d6 |
child 17140 | 5be3a21ec949 |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/table.ML |
5015 | 2 |
ID: $Id$ |
15014 | 3 |
Author: Markus Wenzel and Stefan Berghofer, TU Muenchen |
5015 | 4 |
|
16342 | 5 |
Generic tables. Efficient purely functional implementation using |
6 |
balanced 2-3 trees. |
|
5015 | 7 |
*) |
8 |
||
9 |
signature KEY = |
|
10 |
sig |
|
11 |
type key |
|
12 |
val ord: key * key -> order |
|
13 |
end; |
|
14 |
||
15 |
signature TABLE = |
|
16 |
sig |
|
17 |
type key |
|
18 |
type 'a table |
|
19 |
exception DUP of key |
|
20 |
exception DUPS of key list |
|
15014 | 21 |
exception UNDEF of key |
5015 | 22 |
val empty: 'a table |
23 |
val is_empty: 'a table -> bool |
|
5681 | 24 |
val map: ('a -> 'b) -> 'a table -> 'b table |
16446 | 25 |
val map': (key -> 'a -> 'b) -> 'a table -> 'b table |
26 |
val fold: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a |
|
5681 | 27 |
val foldl: ('a * (key * 'b) -> 'a) -> 'a * 'b table -> 'a |
5015 | 28 |
val dest: 'a table -> (key * 'a) list |
5681 | 29 |
val keys: 'a table -> key list |
8409 | 30 |
val min_key: 'a table -> key option |
15160 | 31 |
val max_key: 'a table -> key option |
7061 | 32 |
val exists: (key * 'a -> bool) -> 'a table -> bool |
16192 | 33 |
val forall: (key * 'a -> bool) -> 'a table -> bool |
16887 | 34 |
val defined: 'a table -> key -> bool |
5015 | 35 |
val lookup: 'a table * key -> 'a option |
36 |
val update: (key * 'a) * 'a table -> 'a table |
|
12287 | 37 |
val update_new: (key * 'a) * 'a table -> 'a table (*exception DUP*) |
15665 | 38 |
val map_entry: key -> ('a -> 'a) -> 'a table -> 'a table |
12287 | 39 |
val make: (key * 'a) list -> 'a table (*exception DUPS*) |
40 |
val extend: 'a table * (key * 'a) list -> 'a table (*exception DUPS*) |
|
16446 | 41 |
val join: (key -> 'a * 'a -> 'a option) -> |
42 |
'a table * 'a table -> 'a table (*exception DUPS*) |
|
12287 | 43 |
val merge: ('a * 'a -> bool) -> 'a table * 'a table -> 'a table (*exception DUPS*) |
15665 | 44 |
val delete: key -> 'a table -> 'a table (*exception UNDEF*) |
15761 | 45 |
val delete_safe: key -> 'a table -> 'a table |
16466 | 46 |
val member: ('b * 'a -> bool) -> 'a table -> key * 'b -> bool |
15761 | 47 |
val insert: ('a * 'a -> bool) -> key * 'a -> 'a table -> 'a table (*exception DUP*) |
16139 | 48 |
val remove: ('b * 'a -> bool) -> key * 'b -> 'a table -> 'a table |
5015 | 49 |
val lookup_multi: 'a list table * key -> 'a list |
8606 | 50 |
val update_multi: (key * 'a) * 'a list table -> 'a list table |
16139 | 51 |
val remove_multi: ('b * 'a -> bool) -> key * 'b -> 'a list table -> 'a list table |
5015 | 52 |
val make_multi: (key * 'a) list -> 'a list table |
53 |
val dest_multi: 'a list table -> (key * 'a) list |
|
12287 | 54 |
val merge_multi: ('a * 'a -> bool) -> |
15761 | 55 |
'a list table * 'a list table -> 'a list table (*exception DUPS*) |
12287 | 56 |
val merge_multi': ('a * 'a -> bool) -> |
15761 | 57 |
'a list table * 'a list table -> 'a list table (*exception DUPS*) |
5015 | 58 |
end; |
59 |
||
60 |
functor TableFun(Key: KEY): TABLE = |
|
61 |
struct |
|
62 |
||
63 |
||
64 |
(* datatype table *) |
|
65 |
||
66 |
type key = Key.key; |
|
67 |
||
68 |
datatype 'a table = |
|
69 |
Empty | |
|
70 |
Branch2 of 'a table * (key * 'a) * 'a table | |
|
71 |
Branch3 of 'a table * (key * 'a) * 'a table * (key * 'a) * 'a table; |
|
72 |
||
73 |
exception DUP of key; |
|
74 |
exception DUPS of key list; |
|
75 |
||
76 |
||
5681 | 77 |
(* empty *) |
78 |
||
5015 | 79 |
val empty = Empty; |
80 |
||
81 |
fun is_empty Empty = true |
|
82 |
| is_empty _ = false; |
|
83 |
||
5681 | 84 |
|
85 |
(* map and fold combinators *) |
|
86 |
||
16657 | 87 |
fun map_table f = |
88 |
let |
|
89 |
fun map Empty = Empty |
|
90 |
| map (Branch2 (left, (k, x), right)) = |
|
91 |
Branch2 (map left, (k, f k x), map right) |
|
92 |
| map (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = |
|
93 |
Branch3 (map left, (k1, f k1 x1), map mid, (k2, f k2 x2), map right); |
|
94 |
in map end; |
|
5681 | 95 |
|
16657 | 96 |
fun fold_table f = |
97 |
let |
|
98 |
fun fold Empty x = x |
|
99 |
| fold (Branch2 (left, p, right)) x = |
|
100 |
fold right (f p (fold left x)) |
|
101 |
| fold (Branch3 (left, p1, mid, p2, right)) x = |
|
102 |
fold right (f p2 (fold mid (f p1 (fold left x)))); |
|
103 |
in fold end; |
|
5681 | 104 |
|
16446 | 105 |
fun dest tab = rev (fold_table cons tab []); |
106 |
fun keys tab = rev (fold_table (cons o #1) tab []); |
|
16192 | 107 |
|
108 |
local exception TRUE in |
|
109 |
||
110 |
fun exists pred tab = |
|
16446 | 111 |
(fold_table (fn p => fn () => if pred p then raise TRUE else ()) tab (); false) |
16192 | 112 |
handle TRUE => true; |
113 |
||
114 |
fun forall pred = not o exists (not o pred); |
|
115 |
||
116 |
end; |
|
5015 | 117 |
|
15531 | 118 |
fun min_key Empty = NONE |
16864 | 119 |
| min_key (Branch2 (Empty, (k, _), _)) = SOME k |
120 |
| min_key (Branch3 (Empty, (k, _), _, _, _)) = SOME k |
|
121 |
| min_key (Branch2 (left, _, _)) = min_key left |
|
122 |
| min_key (Branch3 (left, _, _, _, _)) = min_key left; |
|
8409 | 123 |
|
15531 | 124 |
fun max_key Empty = NONE |
16864 | 125 |
| max_key (Branch2 (_, (k, _), Empty)) = SOME k |
126 |
| max_key (Branch3 (_, _, _, (k, _), Empty)) = SOME k |
|
127 |
| max_key (Branch2 (_, _, right)) = max_key right |
|
128 |
| max_key (Branch3 (_, _, _, _, right)) = max_key right; |
|
15665 | 129 |
|
5015 | 130 |
|
131 |
(* lookup *) |
|
132 |
||
15531 | 133 |
fun lookup (Empty, _) = NONE |
5015 | 134 |
| lookup (Branch2 (left, (k, x), right), key) = |
135 |
(case Key.ord (key, k) of |
|
136 |
LESS => lookup (left, key) |
|
15531 | 137 |
| EQUAL => SOME x |
5015 | 138 |
| GREATER => lookup (right, key)) |
139 |
| lookup (Branch3 (left, (k1, x1), mid, (k2, x2), right), key) = |
|
140 |
(case Key.ord (key, k1) of |
|
141 |
LESS => lookup (left, key) |
|
15531 | 142 |
| EQUAL => SOME x1 |
5015 | 143 |
| GREATER => |
144 |
(case Key.ord (key, k2) of |
|
145 |
LESS => lookup (mid, key) |
|
15531 | 146 |
| EQUAL => SOME x2 |
5015 | 147 |
| GREATER => lookup (right, key))); |
148 |
||
16887 | 149 |
fun defined tab key = is_some (lookup (tab, key)); |
150 |
||
5015 | 151 |
|
15665 | 152 |
(* updates *) |
5015 | 153 |
|
154 |
datatype 'a growth = |
|
155 |
Stay of 'a table | |
|
156 |
Sprout of 'a table * (key * 'a) * 'a table; |
|
157 |
||
15761 | 158 |
exception SAME; |
159 |
||
15665 | 160 |
fun modify key f tab = |
161 |
let |
|
162 |
fun modfy Empty = Sprout (Empty, (key, f NONE), Empty) |
|
163 |
| modfy (Branch2 (left, p as (k, x), right)) = |
|
164 |
(case Key.ord (key, k) of |
|
165 |
LESS => |
|
166 |
(case modfy left of |
|
167 |
Stay left' => Stay (Branch2 (left', p, right)) |
|
168 |
| Sprout (left1, q, left2) => Stay (Branch3 (left1, q, left2, p, right))) |
|
169 |
| EQUAL => Stay (Branch2 (left, (k, f (SOME x)), right)) |
|
170 |
| GREATER => |
|
171 |
(case modfy right of |
|
172 |
Stay right' => Stay (Branch2 (left, p, right')) |
|
173 |
| Sprout (right1, q, right2) => |
|
174 |
Stay (Branch3 (left, p, right1, q, right2)))) |
|
175 |
| modfy (Branch3 (left, p1 as (k1, x1), mid, p2 as (k2, x2), right)) = |
|
176 |
(case Key.ord (key, k1) of |
|
5015 | 177 |
LESS => |
15665 | 178 |
(case modfy left of |
179 |
Stay left' => Stay (Branch3 (left', p1, mid, p2, right)) |
|
180 |
| Sprout (left1, q, left2) => |
|
181 |
Sprout (Branch2 (left1, q, left2), p1, Branch2 (mid, p2, right))) |
|
182 |
| EQUAL => Stay (Branch3 (left, (k1, f (SOME x1)), mid, p2, right)) |
|
5015 | 183 |
| GREATER => |
15665 | 184 |
(case Key.ord (key, k2) of |
185 |
LESS => |
|
186 |
(case modfy mid of |
|
187 |
Stay mid' => Stay (Branch3 (left, p1, mid', p2, right)) |
|
188 |
| Sprout (mid1, q, mid2) => |
|
189 |
Sprout (Branch2 (left, p1, mid1), q, Branch2 (mid2, p2, right))) |
|
190 |
| EQUAL => Stay (Branch3 (left, p1, mid, (k2, f (SOME x2)), right)) |
|
191 |
| GREATER => |
|
192 |
(case modfy right of |
|
193 |
Stay right' => Stay (Branch3 (left, p1, mid, p2, right')) |
|
194 |
| Sprout (right1, q, right2) => |
|
195 |
Sprout (Branch2 (left, p1, mid), p2, Branch2 (right1, q, right2))))); |
|
5015 | 196 |
|
15665 | 197 |
in |
198 |
(case modfy tab of |
|
199 |
Stay tab' => tab' |
|
200 |
| Sprout br => Branch2 br) |
|
201 |
handle SAME => tab |
|
202 |
end; |
|
5015 | 203 |
|
15761 | 204 |
fun update ((key, x), tab) = modify key (fn _ => x) tab; |
205 |
fun update_new ((key, x), tab) = modify key (fn NONE => x | SOME _ => raise DUP key) tab; |
|
206 |
fun map_entry key f = modify key (fn NONE => raise SAME | SOME x => f x); |
|
5015 | 207 |
|
208 |
||
12287 | 209 |
(* extend and make *) |
5015 | 210 |
|
15761 | 211 |
fun extend (table, args) = |
12287 | 212 |
let |
15761 | 213 |
fun add (key, x) (tab, dups) = |
16894 | 214 |
if defined tab key then (tab, key :: dups) |
15761 | 215 |
else (update ((key, x), tab), dups); |
12287 | 216 |
in |
15761 | 217 |
(case fold add args (table, []) of |
12287 | 218 |
(table', []) => table' |
219 |
| (_, dups) => raise DUPS (rev dups)) |
|
220 |
end; |
|
221 |
||
222 |
fun make pairs = extend (empty, pairs); |
|
223 |
||
5015 | 224 |
|
15014 | 225 |
(* delete *) |
226 |
||
15665 | 227 |
exception UNDEF of key; |
228 |
||
229 |
local |
|
230 |
||
231 |
fun compare NONE (k2, _) = LESS |
|
232 |
| compare (SOME k1) (k2, _) = Key.ord (k1, k2); |
|
15014 | 233 |
|
234 |
fun if_eq EQUAL x y = x |
|
235 |
| if_eq _ x y = y; |
|
236 |
||
15531 | 237 |
fun del (SOME k) Empty = raise UNDEF k |
238 |
| del NONE (Branch2 (Empty, p, Empty)) = (p, (true, Empty)) |
|
239 |
| del NONE (Branch3 (Empty, p, Empty, q, Empty)) = |
|
15014 | 240 |
(p, (false, Branch2 (Empty, q, Empty))) |
15665 | 241 |
| del k (Branch2 (Empty, p, Empty)) = (case compare k p of |
16002 | 242 |
EQUAL => (p, (true, Empty)) | _ => raise UNDEF (the k)) |
15665 | 243 |
| del k (Branch3 (Empty, p, Empty, q, Empty)) = (case compare k p of |
15014 | 244 |
EQUAL => (p, (false, Branch2 (Empty, q, Empty))) |
15665 | 245 |
| _ => (case compare k q of |
15014 | 246 |
EQUAL => (q, (false, Branch2 (Empty, p, Empty))) |
16002 | 247 |
| _ => raise UNDEF (the k))) |
15665 | 248 |
| del k (Branch2 (l, p, r)) = (case compare k p of |
15014 | 249 |
LESS => (case del k l of |
250 |
(p', (false, l')) => (p', (false, Branch2 (l', p, r))) |
|
251 |
| (p', (true, l')) => (p', case r of |
|
252 |
Branch2 (rl, rp, rr) => |
|
253 |
(true, Branch3 (l', p, rl, rp, rr)) |
|
254 |
| Branch3 (rl, rp, rm, rq, rr) => (false, Branch2 |
|
255 |
(Branch2 (l', p, rl), rp, Branch2 (rm, rq, rr))))) |
|
15531 | 256 |
| ord => (case del (if_eq ord NONE k) r of |
15014 | 257 |
(p', (false, r')) => (p', (false, Branch2 (l, if_eq ord p' p, r'))) |
258 |
| (p', (true, r')) => (p', case l of |
|
259 |
Branch2 (ll, lp, lr) => |
|
260 |
(true, Branch3 (ll, lp, lr, if_eq ord p' p, r')) |
|
261 |
| Branch3 (ll, lp, lm, lq, lr) => (false, Branch2 |
|
262 |
(Branch2 (ll, lp, lm), lq, Branch2 (lr, if_eq ord p' p, r')))))) |
|
15665 | 263 |
| del k (Branch3 (l, p, m, q, r)) = (case compare k q of |
264 |
LESS => (case compare k p of |
|
15014 | 265 |
LESS => (case del k l of |
266 |
(p', (false, l')) => (p', (false, Branch3 (l', p, m, q, r))) |
|
267 |
| (p', (true, l')) => (p', (false, case (m, r) of |
|
268 |
(Branch2 (ml, mp, mr), Branch2 _) => |
|
269 |
Branch2 (Branch3 (l', p, ml, mp, mr), q, r) |
|
270 |
| (Branch3 (ml, mp, mm, mq, mr), _) => |
|
271 |
Branch3 (Branch2 (l', p, ml), mp, Branch2 (mm, mq, mr), q, r) |
|
272 |
| (Branch2 (ml, mp, mr), Branch3 (rl, rp, rm, rq, rr)) => |
|
273 |
Branch3 (Branch2 (l', p, ml), mp, Branch2 (mr, q, rl), rp, |
|
274 |
Branch2 (rm, rq, rr))))) |
|
15531 | 275 |
| ord => (case del (if_eq ord NONE k) m of |
15014 | 276 |
(p', (false, m')) => |
277 |
(p', (false, Branch3 (l, if_eq ord p' p, m', q, r))) |
|
278 |
| (p', (true, m')) => (p', (false, case (l, r) of |
|
279 |
(Branch2 (ll, lp, lr), Branch2 _) => |
|
280 |
Branch2 (Branch3 (ll, lp, lr, if_eq ord p' p, m'), q, r) |
|
281 |
| (Branch3 (ll, lp, lm, lq, lr), _) => |
|
282 |
Branch3 (Branch2 (ll, lp, lm), lq, |
|
283 |
Branch2 (lr, if_eq ord p' p, m'), q, r) |
|
284 |
| (_, Branch3 (rl, rp, rm, rq, rr)) => |
|
285 |
Branch3 (l, if_eq ord p' p, Branch2 (m', q, rl), rp, |
|
286 |
Branch2 (rm, rq, rr)))))) |
|
15531 | 287 |
| ord => (case del (if_eq ord NONE k) r of |
15014 | 288 |
(q', (false, r')) => |
289 |
(q', (false, Branch3 (l, p, m, if_eq ord q' q, r'))) |
|
290 |
| (q', (true, r')) => (q', (false, case (l, m) of |
|
291 |
(Branch2 _, Branch2 (ml, mp, mr)) => |
|
292 |
Branch2 (l, p, Branch3 (ml, mp, mr, if_eq ord q' q, r')) |
|
293 |
| (_, Branch3 (ml, mp, mm, mq, mr)) => |
|
294 |
Branch3 (l, p, Branch2 (ml, mp, mm), mq, |
|
295 |
Branch2 (mr, if_eq ord q' q, r')) |
|
296 |
| (Branch3 (ll, lp, lm, lq, lr), Branch2 (ml, mp, mr)) => |
|
297 |
Branch3 (Branch2 (ll, lp, lm), lq, Branch2 (lr, p, ml), mp, |
|
298 |
Branch2 (mr, if_eq ord q' q, r')))))); |
|
299 |
||
15665 | 300 |
in |
301 |
||
15761 | 302 |
fun delete key tab = snd (snd (del (SOME key) tab)); |
303 |
fun delete_safe key tab = delete key tab handle UNDEF _ => tab; |
|
15014 | 304 |
|
15665 | 305 |
end; |
306 |
||
15014 | 307 |
|
16466 | 308 |
(* member, insert and remove *) |
309 |
||
310 |
fun member eq tab (key, x) = |
|
311 |
(case lookup (tab, key) of |
|
312 |
NONE => false |
|
313 |
| SOME y => eq (x, y)); |
|
15761 | 314 |
|
315 |
fun insert eq (key, x) = |
|
316 |
modify key (fn NONE => x | SOME y => if eq (x, y) then raise SAME else raise DUP key); |
|
317 |
||
318 |
fun remove eq (key, x) tab = |
|
319 |
(case lookup (tab, key) of |
|
320 |
NONE => tab |
|
321 |
| SOME y => if eq (x, y) then delete key tab else tab); |
|
322 |
||
323 |
||
12287 | 324 |
(* join and merge *) |
5015 | 325 |
|
12287 | 326 |
fun join f (table1, table2) = |
327 |
let |
|
16446 | 328 |
fun add (key, x) (tab, dups) = |
12287 | 329 |
(case lookup (tab, key) of |
15531 | 330 |
NONE => (update ((key, x), tab), dups) |
331 |
| SOME y => |
|
16446 | 332 |
(case f key (y, x) of |
15531 | 333 |
SOME z => (update ((key, z), tab), dups) |
334 |
| NONE => (tab, key :: dups))); |
|
12287 | 335 |
in |
16446 | 336 |
(case fold_table add table2 (table1, []) of |
12287 | 337 |
(table', []) => table' |
338 |
| (_, dups) => raise DUPS (rev dups)) |
|
339 |
end; |
|
340 |
||
16446 | 341 |
fun merge eq tabs = join (fn _ => fn (y, x) => if eq (y, x) then SOME y else NONE) tabs; |
5015 | 342 |
|
343 |
||
15761 | 344 |
(* tables with multiple entries per key *) |
345 |
||
16002 | 346 |
fun lookup_multi arg = if_none (lookup arg) []; |
15761 | 347 |
|
348 |
fun update_multi ((key, x), tab) = modify key (fn NONE => [x] | SOME xs => x :: xs) tab; |
|
5015 | 349 |
|
15761 | 350 |
fun remove_multi eq (key, x) tab = |
351 |
map_entry key (fn xs => (case Library.remove eq x xs of [] => raise UNDEF key | ys => ys)) tab |
|
352 |
handle UNDEF _ => delete key tab; |
|
5015 | 353 |
|
15761 | 354 |
fun make_multi args = foldr update_multi empty args; |
355 |
fun dest_multi tab = List.concat (map (fn (key, xs) => map (pair key) xs) (dest tab)); |
|
16446 | 356 |
fun merge_multi eq = join (fn _ => fn (xs, xs') => SOME (gen_merge_lists eq xs xs')); |
357 |
fun merge_multi' eq = join (fn _ => fn (xs, xs') => SOME (gen_merge_lists' eq xs xs')); |
|
5015 | 358 |
|
359 |
||
5681 | 360 |
(*final declarations of this structure!*) |
16446 | 361 |
fun map f = map_table (K f); |
362 |
val map' = map_table; |
|
363 |
val fold = fold_table; |
|
364 |
fun foldl f (x, tab) = fold (fn p => fn x' => f (x', p)) tab x; |
|
5015 | 365 |
|
366 |
end; |
|
367 |
||
16342 | 368 |
structure Inttab = TableFun(type key = int val ord = int_ord); |
16687
51fa05ce0f32
Symtab: use fast_string_ord instead of string_ord -- affects order of Symtab.dest etc.;
wenzelm
parents:
16657
diff
changeset
|
369 |
structure Symtab = TableFun(type key = string val ord = fast_string_ord); |