| author | wenzelm | 
| Tue, 13 Mar 2012 17:04:00 +0100 | |
| changeset 46906 | 3c1787d46935 | 
| parent 44336 | 59ff5a93eef4 | 
| child 47980 | c81801f881b3 | 
| 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 | |
| 11 | val ord: key * key -> order | |
| 12 | end; | |
| 13 | ||
| 14 | signature TABLE = | |
| 15 | sig | |
| 16 | type key | |
| 17 | type 'a table | |
| 18 | exception DUP of key | |
| 19031 | 19 | exception SAME | 
| 15014 | 20 | exception UNDEF of key | 
| 5015 | 21 | val empty: 'a table | 
| 22 | val is_empty: 'a table -> bool | |
| 39020 | 23 | val map: (key -> 'a -> 'b) -> 'a table -> 'b table | 
| 16446 | 24 | val fold: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a | 
| 28334 | 25 | val fold_rev: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a | 
| 5015 | 26 | val dest: 'a table -> (key * 'a) list | 
| 5681 | 27 | val keys: 'a table -> key list | 
| 31616 | 28 | val min_key: 'a table -> key option | 
| 29 | val max_key: 'a table -> key option | |
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 30 | val get_first: (key * 'a -> 'b option) -> 'a table -> 'b option | 
| 27508 | 31 | val exists: (key * 'a -> bool) -> 'a table -> bool | 
| 32 | val forall: (key * 'a -> bool) -> 'a table -> bool | |
| 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: 
39020diff
changeset | 33 | val lookup_key: 'a table -> key -> (key * 'a) option | 
| 31616 | 34 | val lookup: 'a table -> key -> 'a option | 
| 16887 | 35 | val defined: 'a table -> key -> bool | 
| 31209 | 36 | val update: key * 'a -> 'a table -> 'a table | 
| 37 | val update_new: key * 'a -> 'a table -> 'a table (*exception DUP*) | |
| 17179 | 38 | val default: key * 'a -> 'a table -> 'a table | 
| 15665 | 39 |   val map_entry: key -> ('a -> 'a) -> 'a table -> 'a table
 | 
| 31209 | 40 |   val map_default: key * 'a -> ('a -> 'a) -> 'a table -> 'a table
 | 
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
21065diff
changeset | 41 | val make: (key * 'a) list -> 'a table (*exception DUP*) | 
| 19031 | 42 | val join: (key -> 'a * 'a -> 'a) (*exception DUP/SAME*) -> | 
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
21065diff
changeset | 43 | 'a table * 'a table -> 'a table (*exception DUP*) | 
| 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
21065diff
changeset | 44 |   val merge: ('a * 'a -> bool) -> 'a table * 'a table -> 'a table      (*exception DUP*)
 | 
| 15665 | 45 | val delete: key -> 'a table -> 'a table (*exception UNDEF*) | 
| 15761 | 46 | val delete_safe: key -> 'a table -> 'a table | 
| 16466 | 47 |   val member: ('b * 'a -> bool) -> 'a table -> key * 'b -> bool
 | 
| 15761 | 48 |   val insert: ('a * 'a -> bool) -> key * 'a -> 'a table -> 'a table    (*exception DUP*)
 | 
| 16139 | 49 |   val remove: ('b * 'a -> bool) -> key * 'b -> 'a table -> 'a table
 | 
| 18946 | 50 | val lookup_list: 'a list table -> key -> 'a list | 
| 31209 | 51 | val cons_list: key * 'a -> 'a list table -> 'a list table | 
| 19506 | 52 |   val insert_list: ('a * 'a -> bool) -> key * 'a -> 'a list table -> 'a list table
 | 
| 18946 | 53 |   val remove_list: ('b * 'a -> bool) -> key * 'b -> 'a list table -> 'a list table
 | 
| 25391 | 54 |   val update_list: ('a * 'a -> bool) -> key * 'a -> 'a list table -> 'a list table
 | 
| 18946 | 55 | val make_list: (key * 'a) list -> 'a list table | 
| 56 | val dest_list: 'a list table -> (key * 'a) list | |
| 33162 | 57 |   val merge_list: ('a * 'a -> bool) -> 'a list table * 'a list table -> 'a list table
 | 
| 5015 | 58 | end; | 
| 59 | ||
| 31971 
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
 wenzelm parents: 
31621diff
changeset | 60 | functor Table(Key: KEY): TABLE = | 
| 5015 | 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 | ||
| 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 | |
| 28334 | 104 | fun fold_rev_table f = | 
| 105 | let | |
| 106 | fun fold Empty x = x | |
| 107 | | fold (Branch2 (left, p, right)) x = | |
| 108 | fold left (f p (fold right x)) | |
| 109 | | fold (Branch3 (left, p1, mid, p2, right)) x = | |
| 110 | fold left (f p1 (fold mid (f p2 (fold right x)))); | |
| 111 | in fold end; | |
| 112 | ||
| 113 | fun dest tab = fold_rev_table cons tab []; | |
| 114 | fun keys tab = fold_rev_table (cons o #1) tab []; | |
| 16192 | 115 | |
| 27508 | 116 | |
| 117 | (* min/max keys *) | |
| 5015 | 118 | |
| 15531 | 119 | fun min_key Empty = NONE | 
| 16864 | 120 | | min_key (Branch2 (Empty, (k, _), _)) = SOME k | 
| 121 | | min_key (Branch3 (Empty, (k, _), _, _, _)) = SOME k | |
| 122 | | min_key (Branch2 (left, _, _)) = min_key left | |
| 123 | | min_key (Branch3 (left, _, _, _, _)) = min_key left; | |
| 8409 | 124 | |
| 15531 | 125 | fun max_key Empty = NONE | 
| 16864 | 126 | | max_key (Branch2 (_, (k, _), Empty)) = SOME k | 
| 127 | | max_key (Branch3 (_, _, _, (k, _), Empty)) = SOME k | |
| 128 | | max_key (Branch2 (_, _, right)) = max_key right | |
| 129 | | max_key (Branch3 (_, _, _, _, right)) = max_key right; | |
| 15665 | 130 | |
| 5015 | 131 | |
| 31616 | 132 | (* get_first *) | 
| 133 | ||
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 134 | fun get_first f = | 
| 31616 | 135 | let | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 136 | fun get Empty = NONE | 
| 31618 
2e4430b84303
improved get_first: check boundary before entering subtrees;
 wenzelm parents: 
31616diff
changeset | 137 | | get (Branch2 (left, (k, x), right)) = | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 138 | (case get left of | 
| 31616 | 139 | NONE => | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 140 | (case f (k, x) of | 
| 31616 | 141 | NONE => get right | 
| 142 | | some => some) | |
| 143 | | some => some) | |
| 31618 
2e4430b84303
improved get_first: check boundary before entering subtrees;
 wenzelm parents: 
31616diff
changeset | 144 | | get (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 145 | (case get left of | 
| 31616 | 146 | NONE => | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 147 | (case f (k1, x1) of | 
| 31616 | 148 | NONE => | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 149 | (case get mid of | 
| 31616 | 150 | NONE => | 
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 151 | (case f (k2, x2) of | 
| 31616 | 152 | NONE => get right | 
| 153 | | some => some) | |
| 154 | | some => some) | |
| 155 | | some => some) | |
| 156 | | some => some); | |
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 157 | in get end; | 
| 31616 | 158 | |
| 35012 
c3e3ac3ca091
removed unused "boundary" of Table/Graph.get_first;
 wenzelm parents: 
33162diff
changeset | 159 | fun exists pred = is_some o get_first (fn entry => if pred entry then SOME () else NONE); | 
| 31616 | 160 | fun forall pred = not o exists (not o pred); | 
| 161 | ||
| 162 | ||
| 5015 | 163 | (* lookup *) | 
| 164 | ||
| 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: 
39020diff
changeset | 165 | fun lookup_key tab key = | 
| 19031 | 166 | let | 
| 167 | fun look Empty = NONE | |
| 168 | | look (Branch2 (left, (k, x), right)) = | |
| 169 | (case Key.ord (key, k) of | |
| 170 | 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: 
39020diff
changeset | 171 | | EQUAL => SOME (k, x) | 
| 19031 | 172 | | GREATER => look right) | 
| 173 | | look (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = | |
| 174 | (case Key.ord (key, k1) of | |
| 175 | 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: 
39020diff
changeset | 176 | | EQUAL => SOME (k1, x1) | 
| 19031 | 177 | | GREATER => | 
| 178 | (case Key.ord (key, k2) of | |
| 179 | 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: 
39020diff
changeset | 180 | | EQUAL => SOME (k2, x2) | 
| 19031 | 181 | | GREATER => look right)); | 
| 182 | in look tab end; | |
| 5015 | 183 | |
| 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: 
39020diff
changeset | 184 | fun lookup tab key = Option.map #2 (lookup_key tab key); | 
| 
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: 
39020diff
changeset | 185 | |
| 19031 | 186 | fun defined tab key = | 
| 187 | let | |
| 188 | fun def Empty = false | |
| 189 | | def (Branch2 (left, (k, x), right)) = | |
| 190 | (case Key.ord (key, k) of | |
| 191 | LESS => def left | |
| 192 | | EQUAL => true | |
| 193 | | GREATER => def right) | |
| 194 | | def (Branch3 (left, (k1, x1), mid, (k2, x2), right)) = | |
| 195 | (case Key.ord (key, k1) of | |
| 196 | LESS => def left | |
| 197 | | EQUAL => true | |
| 198 | | GREATER => | |
| 199 | (case Key.ord (key, k2) of | |
| 200 | LESS => def mid | |
| 201 | | EQUAL => true | |
| 202 | | GREATER => def right)); | |
| 203 | in def tab end; | |
| 16887 | 204 | |
| 5015 | 205 | |
| 19031 | 206 | (* modify *) | 
| 5015 | 207 | |
| 208 | datatype 'a growth = | |
| 209 | Stay of 'a table | | |
| 210 | Sprout of 'a table * (key * 'a) * 'a table; | |
| 211 | ||
| 15761 | 212 | exception SAME; | 
| 213 | ||
| 15665 | 214 | fun modify key f tab = | 
| 215 | let | |
| 216 | fun modfy Empty = Sprout (Empty, (key, f NONE), Empty) | |
| 217 | | modfy (Branch2 (left, p as (k, x), right)) = | |
| 218 | (case Key.ord (key, k) of | |
| 219 | LESS => | |
| 220 | (case modfy left of | |
| 221 | Stay left' => Stay (Branch2 (left', p, right)) | |
| 222 | | Sprout (left1, q, left2) => Stay (Branch3 (left1, q, left2, p, right))) | |
| 223 | | EQUAL => Stay (Branch2 (left, (k, f (SOME x)), right)) | |
| 224 | | GREATER => | |
| 225 | (case modfy right of | |
| 226 | Stay right' => Stay (Branch2 (left, p, right')) | |
| 227 | | Sprout (right1, q, right2) => | |
| 228 | Stay (Branch3 (left, p, right1, q, right2)))) | |
| 229 | | modfy (Branch3 (left, p1 as (k1, x1), mid, p2 as (k2, x2), right)) = | |
| 230 | (case Key.ord (key, k1) of | |
| 5015 | 231 | LESS => | 
| 15665 | 232 | (case modfy left of | 
| 233 | Stay left' => Stay (Branch3 (left', p1, mid, p2, right)) | |
| 234 | | Sprout (left1, q, left2) => | |
| 235 | Sprout (Branch2 (left1, q, left2), p1, Branch2 (mid, p2, right))) | |
| 236 | | EQUAL => Stay (Branch3 (left, (k1, f (SOME x1)), mid, p2, right)) | |
| 5015 | 237 | | GREATER => | 
| 15665 | 238 | (case Key.ord (key, k2) of | 
| 239 | LESS => | |
| 240 | (case modfy mid of | |
| 241 | Stay mid' => Stay (Branch3 (left, p1, mid', p2, right)) | |
| 242 | | Sprout (mid1, q, mid2) => | |
| 243 | Sprout (Branch2 (left, p1, mid1), q, Branch2 (mid2, p2, right))) | |
| 244 | | EQUAL => Stay (Branch3 (left, p1, mid, (k2, f (SOME x2)), right)) | |
| 245 | | GREATER => | |
| 246 | (case modfy right of | |
| 247 | Stay right' => Stay (Branch3 (left, p1, mid, p2, right')) | |
| 248 | | Sprout (right1, q, right2) => | |
| 249 | Sprout (Branch2 (left, p1, mid), p2, Branch2 (right1, q, right2))))); | |
| 5015 | 250 | |
| 15665 | 251 | in | 
| 252 | (case modfy tab of | |
| 253 | Stay tab' => tab' | |
| 254 | | Sprout br => Branch2 br) | |
| 255 | handle SAME => tab | |
| 256 | end; | |
| 5015 | 257 | |
| 17412 | 258 | fun update (key, x) tab = modify key (fn _ => x) tab; | 
| 259 | fun update_new (key, x) tab = modify key (fn NONE => x | SOME _ => raise DUP key) tab; | |
| 17709 | 260 | fun default (key, x) tab = modify key (fn NONE => x | SOME _ => raise SAME) tab; | 
| 15761 | 261 | fun map_entry key f = modify key (fn NONE => raise SAME | SOME x => f x); | 
| 27783 | 262 | fun map_default (key, x) f = modify key (fn NONE => f x | SOME y => f y); | 
| 5015 | 263 | |
| 264 | ||
| 15014 | 265 | (* delete *) | 
| 266 | ||
| 15665 | 267 | exception UNDEF of key; | 
| 268 | ||
| 269 | local | |
| 270 | ||
| 271 | fun compare NONE (k2, _) = LESS | |
| 272 | | compare (SOME k1) (k2, _) = Key.ord (k1, k2); | |
| 15014 | 273 | |
| 274 | fun if_eq EQUAL x y = x | |
| 275 | | if_eq _ x y = y; | |
| 276 | ||
| 15531 | 277 | fun del (SOME k) Empty = raise UNDEF k | 
| 278 | | del NONE (Branch2 (Empty, p, Empty)) = (p, (true, Empty)) | |
| 279 | | del NONE (Branch3 (Empty, p, Empty, q, Empty)) = | |
| 15014 | 280 | (p, (false, Branch2 (Empty, q, Empty))) | 
| 15665 | 281 | | del k (Branch2 (Empty, p, Empty)) = (case compare k p of | 
| 16002 | 282 | EQUAL => (p, (true, Empty)) | _ => raise UNDEF (the k)) | 
| 15665 | 283 | | del k (Branch3 (Empty, p, Empty, q, Empty)) = (case compare k p of | 
| 15014 | 284 | EQUAL => (p, (false, Branch2 (Empty, q, Empty))) | 
| 15665 | 285 | | _ => (case compare k q of | 
| 15014 | 286 | EQUAL => (q, (false, Branch2 (Empty, p, Empty))) | 
| 16002 | 287 | | _ => raise UNDEF (the k))) | 
| 15665 | 288 | | del k (Branch2 (l, p, r)) = (case compare k p of | 
| 15014 | 289 | LESS => (case del k l of | 
| 290 | (p', (false, l')) => (p', (false, Branch2 (l', p, r))) | |
| 291 | | (p', (true, l')) => (p', case r of | |
| 292 | Branch2 (rl, rp, rr) => | |
| 293 | (true, Branch3 (l', p, rl, rp, rr)) | |
| 294 | | Branch3 (rl, rp, rm, rq, rr) => (false, Branch2 | |
| 295 | (Branch2 (l', p, rl), rp, Branch2 (rm, rq, rr))))) | |
| 15531 | 296 | | ord => (case del (if_eq ord NONE k) r of | 
| 15014 | 297 | (p', (false, r')) => (p', (false, Branch2 (l, if_eq ord p' p, r'))) | 
| 298 | | (p', (true, r')) => (p', case l of | |
| 299 | Branch2 (ll, lp, lr) => | |
| 300 | (true, Branch3 (ll, lp, lr, if_eq ord p' p, r')) | |
| 301 | | Branch3 (ll, lp, lm, lq, lr) => (false, Branch2 | |
| 302 | (Branch2 (ll, lp, lm), lq, Branch2 (lr, if_eq ord p' p, r')))))) | |
| 15665 | 303 | | del k (Branch3 (l, p, m, q, r)) = (case compare k q of | 
| 304 | LESS => (case compare k p of | |
| 15014 | 305 | LESS => (case del k l of | 
| 306 | (p', (false, l')) => (p', (false, Branch3 (l', p, m, q, r))) | |
| 307 | | (p', (true, l')) => (p', (false, case (m, r) of | |
| 308 | (Branch2 (ml, mp, mr), Branch2 _) => | |
| 309 | Branch2 (Branch3 (l', p, ml, mp, mr), q, r) | |
| 310 | | (Branch3 (ml, mp, mm, mq, mr), _) => | |
| 311 | Branch3 (Branch2 (l', p, ml), mp, Branch2 (mm, mq, mr), q, r) | |
| 312 | | (Branch2 (ml, mp, mr), Branch3 (rl, rp, rm, rq, rr)) => | |
| 313 | Branch3 (Branch2 (l', p, ml), mp, Branch2 (mr, q, rl), rp, | |
| 314 | Branch2 (rm, rq, rr))))) | |
| 15531 | 315 | | ord => (case del (if_eq ord NONE k) m of | 
| 15014 | 316 | (p', (false, m')) => | 
| 317 | (p', (false, Branch3 (l, if_eq ord p' p, m', q, r))) | |
| 318 | | (p', (true, m')) => (p', (false, case (l, r) of | |
| 319 | (Branch2 (ll, lp, lr), Branch2 _) => | |
| 320 | Branch2 (Branch3 (ll, lp, lr, if_eq ord p' p, m'), q, r) | |
| 321 | | (Branch3 (ll, lp, lm, lq, lr), _) => | |
| 322 | Branch3 (Branch2 (ll, lp, lm), lq, | |
| 323 | Branch2 (lr, if_eq ord p' p, m'), q, r) | |
| 324 | | (_, Branch3 (rl, rp, rm, rq, rr)) => | |
| 325 | Branch3 (l, if_eq ord p' p, Branch2 (m', q, rl), rp, | |
| 326 | Branch2 (rm, rq, rr)))))) | |
| 15531 | 327 | | ord => (case del (if_eq ord NONE k) r of | 
| 15014 | 328 | (q', (false, r')) => | 
| 329 | (q', (false, Branch3 (l, p, m, if_eq ord q' q, r'))) | |
| 330 | | (q', (true, r')) => (q', (false, case (l, m) of | |
| 331 | (Branch2 _, Branch2 (ml, mp, mr)) => | |
| 332 | Branch2 (l, p, Branch3 (ml, mp, mr, if_eq ord q' q, r')) | |
| 333 | | (_, Branch3 (ml, mp, mm, mq, mr)) => | |
| 334 | Branch3 (l, p, Branch2 (ml, mp, mm), mq, | |
| 335 | Branch2 (mr, if_eq ord q' q, r')) | |
| 336 | | (Branch3 (ll, lp, lm, lq, lr), Branch2 (ml, mp, mr)) => | |
| 337 | Branch3 (Branch2 (ll, lp, lm), lq, Branch2 (lr, p, ml), mp, | |
| 338 | Branch2 (mr, if_eq ord q' q, r')))))); | |
| 339 | ||
| 15665 | 340 | in | 
| 341 | ||
| 15761 | 342 | fun delete key tab = snd (snd (del (SOME key) tab)); | 
| 44336 
59ff5a93eef4
tuned Table.delete_safe: avoid potentially expensive attempt of delete;
 wenzelm parents: 
43792diff
changeset | 343 | fun delete_safe key tab = if defined tab key then delete key tab else tab; | 
| 15014 | 344 | |
| 15665 | 345 | end; | 
| 346 | ||
| 15014 | 347 | |
| 19031 | 348 | (* membership operations *) | 
| 16466 | 349 | |
| 350 | fun member eq tab (key, x) = | |
| 17412 | 351 | (case lookup tab key of | 
| 16466 | 352 | NONE => false | 
| 353 | | SOME y => eq (x, y)); | |
| 15761 | 354 | |
| 355 | fun insert eq (key, x) = | |
| 356 | modify key (fn NONE => x | SOME y => if eq (x, y) then raise SAME else raise DUP key); | |
| 357 | ||
| 358 | fun remove eq (key, x) tab = | |
| 17412 | 359 | (case lookup tab key of | 
| 15761 | 360 | NONE => tab | 
| 361 | | SOME y => if eq (x, y) then delete key tab else tab); | |
| 362 | ||
| 363 | ||
| 19031 | 364 | (* simultaneous modifications *) | 
| 365 | ||
| 29004 | 366 | fun make entries = fold update_new entries empty; | 
| 5015 | 367 | |
| 12287 | 368 | fun join f (table1, table2) = | 
| 23655 
d2d1138e0ddc
replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
 wenzelm parents: 
21065diff
changeset | 369 | let fun add (key, y) tab = modify key (fn NONE => y | SOME x => f key (x, y)) tab; | 
| 30283 
520872460b7b
TableFun.join/merge: optimize the important special case where the tables coincide -- NOTE: this changes both the operational behaviour and the result for non-standard join/eq notion;
 wenzelm parents: 
29606diff
changeset | 370 | in if pointer_eq (table1, table2) then table1 else fold_table add table2 table1 end; | 
| 12287 | 371 | |
| 19031 | 372 | fun merge eq = join (fn key => fn xy => if eq xy then raise SAME else raise DUP key); | 
| 5015 | 373 | |
| 374 | ||
| 19031 | 375 | (* list tables *) | 
| 15761 | 376 | |
| 18946 | 377 | fun lookup_list tab key = these (lookup tab key); | 
| 25391 | 378 | |
| 379 | fun cons_list (key, x) tab = modify key (fn NONE => [x] | SOME xs => x :: xs) tab; | |
| 5015 | 380 | |
| 20124 | 381 | fun insert_list eq (key, x) = | 
| 382 | modify key (fn NONE => [x] | SOME xs => if Library.member eq xs x then raise SAME else x :: xs); | |
| 25391 | 383 | |
| 18946 | 384 | fun remove_list eq (key, x) tab = | 
| 15761 | 385 | map_entry key (fn xs => (case Library.remove eq x xs of [] => raise UNDEF key | ys => ys)) tab | 
| 386 | handle UNDEF _ => delete key tab; | |
| 5015 | 387 | |
| 25391 | 388 | fun update_list eq (key, x) = | 
| 389 | modify key (fn NONE => [x] | SOME [] => [x] | SOME (xs as y :: _) => | |
| 390 | if eq (x, y) then raise SAME else Library.update eq x xs); | |
| 391 | ||
| 392 | fun make_list args = fold_rev cons_list args empty; | |
| 19482 
9f11af8f7ef9
tuned basic list operators (flat, maps, map_filter);
 wenzelm parents: 
19073diff
changeset | 393 | fun dest_list tab = maps (fn (key, xs) => map (pair key) xs) (dest tab); | 
| 19031 | 394 | fun merge_list eq = join (fn _ => Library.merge eq); | 
| 5015 | 395 | |
| 396 | ||
| 38635 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 397 | (* ML pretty-printing -- requires Poly/ML 5.3.0 or later *) | 
| 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 398 | |
| 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 399 | val _ = | 
| 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 400 | PolyML.addPrettyPrinter (fn depth => fn pretty => fn tab => | 
| 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 401 | ml_pretty | 
| 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 402 |       (ML_Pretty.enum "," "{" "}"
 | 
| 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 403 | (ML_Pretty.pair (pretty_ml o PolyML.prettyRepresentation) (pretty_ml o pretty)) | 
| 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 404 | (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: 
35012diff
changeset | 405 | |
| 
f76ad0771f67
added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
 wenzelm parents: 
35012diff
changeset | 406 | |
| 5681 | 407 | (*final declarations of this structure!*) | 
| 39020 | 408 | val map = map_table; | 
| 16446 | 409 | val fold = fold_table; | 
| 28334 | 410 | val fold_rev = fold_rev_table; | 
| 5015 | 411 | |
| 412 | end; | |
| 413 | ||
| 31971 
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
 wenzelm parents: 
31621diff
changeset | 414 | structure Inttab = Table(type key = int val ord = int_ord); | 
| 
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
 wenzelm parents: 
31621diff
changeset | 415 | structure Symtab = Table(type key = string val ord = fast_string_ord); | 
| 
8c1b845ed105
renamed functor TableFun to Table, and GraphFun to Graph;
 wenzelm parents: 
31621diff
changeset | 416 | structure Symreltab = Table(type key = string * string | 
| 30647 
ef8f46c3158a
added Symreltab (binary relations of symbols) instance of TableFun
 haftmann parents: 
30290diff
changeset | 417 | val ord = prod_ord fast_string_ord fast_string_ord); | 
| 
ef8f46c3158a
added Symreltab (binary relations of symbols) instance of TableFun
 haftmann parents: 
30290diff
changeset | 418 |