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