88 fun dest tab = rev (foldl_table (fn (rev_ps, p) => p :: rev_ps) ([], tab)); |
88 fun dest tab = rev (foldl_table (fn (rev_ps, p) => p :: rev_ps) ([], tab)); |
89 fun keys tab = rev (foldl_table (fn (rev_ks, (k, _)) => k :: rev_ks) ([], tab)); |
89 fun keys tab = rev (foldl_table (fn (rev_ks, (k, _)) => k :: rev_ks) ([], tab)); |
90 fun exists P tab = foldl_table (fn (false, e) => P e | (b, _) => b) (false, tab); |
90 fun exists P tab = foldl_table (fn (false, e) => P e | (b, _) => b) (false, tab); |
91 |
91 |
92 fun min_key Empty = NONE |
92 fun min_key Empty = NONE |
93 | min_key (Branch2 (left, (k, _), _)) = SOME (if_none (min_key left) k) |
93 | min_key (Branch2 (left, (k, _), _)) = SOME (getOpt (min_key left,k)) |
94 | min_key (Branch3 (left, (k, _), _, _, _)) = SOME (if_none (min_key left) k); |
94 | min_key (Branch3 (left, (k, _), _, _, _)) = SOME (getOpt (min_key left,k)); |
95 |
95 |
96 fun max_key Empty = NONE |
96 fun max_key Empty = NONE |
97 | max_key (Branch2 (_, (k, _), right)) = SOME (if_none (max_key right) k) |
97 | max_key (Branch2 (_, (k, _), right)) = SOME (getOpt (max_key right,k)) |
98 | max_key (Branch3 (_, _, _, (k,_), right)) = SOME (if_none (max_key right) k); |
98 | max_key (Branch3 (_, _, _, (k,_), right)) = SOME (getOpt (max_key right,k)); |
99 |
99 |
100 (* lookup *) |
100 (* lookup *) |
101 |
101 |
102 fun lookup (Empty, _) = NONE |
102 fun lookup (Empty, _) = NONE |
103 | lookup (Branch2 (left, (k, x), right), key) = |
103 | lookup (Branch2 (left, (k, x), right), key) = |
176 fun add ((tab, dups), (key, x)) = |
176 fun add ((tab, dups), (key, x)) = |
177 (case lookup (tab, key) of |
177 (case lookup (tab, key) of |
178 NONE => (update ((key, x), tab), dups) |
178 NONE => (update ((key, x), tab), dups) |
179 | _ => (tab, key :: dups)); |
179 | _ => (tab, key :: dups)); |
180 in |
180 in |
181 (case foldl add ((table, []), pairs) of |
181 (case Library.foldl add ((table, []), pairs) of |
182 (table', []) => table' |
182 (table', []) => table' |
183 | (_, dups) => raise DUPS (rev dups)) |
183 | (_, dups) => raise DUPS (rev dups)) |
184 end; |
184 end; |
185 |
185 |
186 fun make pairs = extend (empty, pairs); |
186 fun make pairs = extend (empty, pairs); |
199 fun del (SOME k) Empty = raise UNDEF k |
199 fun del (SOME k) Empty = raise UNDEF k |
200 | del NONE (Branch2 (Empty, p, Empty)) = (p, (true, Empty)) |
200 | del NONE (Branch2 (Empty, p, Empty)) = (p, (true, Empty)) |
201 | del NONE (Branch3 (Empty, p, Empty, q, Empty)) = |
201 | del NONE (Branch3 (Empty, p, Empty, q, Empty)) = |
202 (p, (false, Branch2 (Empty, q, Empty))) |
202 (p, (false, Branch2 (Empty, q, Empty))) |
203 | del k (Branch2 (Empty, p, Empty)) = (case compare' k p of |
203 | del k (Branch2 (Empty, p, Empty)) = (case compare' k p of |
204 EQUAL => (p, (true, Empty)) | _ => raise UNDEF (the k)) |
204 EQUAL => (p, (true, Empty)) | _ => raise UNDEF (valOf k)) |
205 | del k (Branch3 (Empty, p, Empty, q, Empty)) = (case compare' k p of |
205 | del k (Branch3 (Empty, p, Empty, q, Empty)) = (case compare' k p of |
206 EQUAL => (p, (false, Branch2 (Empty, q, Empty))) |
206 EQUAL => (p, (false, Branch2 (Empty, q, Empty))) |
207 | _ => (case compare' k q of |
207 | _ => (case compare' k q of |
208 EQUAL => (q, (false, Branch2 (Empty, p, Empty))) |
208 EQUAL => (q, (false, Branch2 (Empty, p, Empty))) |
209 | _ => raise UNDEF (the k))) |
209 | _ => raise UNDEF (valOf k))) |
210 | del k (Branch2 (l, p, r)) = (case compare' k p of |
210 | del k (Branch2 (l, p, r)) = (case compare' k p of |
211 LESS => (case del k l of |
211 LESS => (case del k l of |
212 (p', (false, l')) => (p', (false, Branch2 (l', p, r))) |
212 (p', (false, l')) => (p', (false, Branch2 (l', p, r))) |
213 | (p', (true, l')) => (p', case r of |
213 | (p', (true, l')) => (p', case r of |
214 Branch2 (rl, rp, rr) => |
214 Branch2 (rl, rp, rr) => |
282 fun merge eq tabs = join (fn (y, x) => if eq (y, x) then SOME y else NONE) tabs; |
282 fun merge eq tabs = join (fn (y, x) => if eq (y, x) then SOME y else NONE) tabs; |
283 |
283 |
284 |
284 |
285 (* tables with multiple entries per key (preserving order) *) |
285 (* tables with multiple entries per key (preserving order) *) |
286 |
286 |
287 fun lookup_multi tab_key = if_none (lookup tab_key) []; |
287 fun lookup_multi tab_key = getOpt (lookup tab_key,[]); |
288 fun update_multi ((key, x), tab) = update ((key, x :: lookup_multi (tab, key)), tab); |
288 fun update_multi ((key, x), tab) = update ((key, x :: lookup_multi (tab, key)), tab); |
289 |
289 |
290 fun make_multi pairs = foldr update_multi (pairs, empty); |
290 fun make_multi pairs = Library.foldr update_multi (pairs, empty); |
291 fun dest_multi tab = flat (map (fn (key, xs) => map (Library.pair key) xs) (dest tab)); |
291 fun dest_multi tab = List.concat (map (fn (key, xs) => map (Library.pair key) xs) (dest tab)); |
292 fun merge_multi eq tabs = join (fn (xs, xs') => SOME (gen_merge_lists eq xs xs')) tabs; |
292 fun merge_multi eq tabs = join (fn (xs, xs') => SOME (gen_merge_lists eq xs xs')) tabs; |
293 fun merge_multi' eq tabs = join (fn (xs, xs') => SOME (gen_merge_lists' eq xs xs')) tabs; |
293 fun merge_multi' eq tabs = join (fn (xs, xs') => SOME (gen_merge_lists' eq xs xs')) tabs; |
294 |
294 |
295 |
295 |
296 (*final declarations of this structure!*) |
296 (*final declarations of this structure!*) |