src/Pure/General/table.ML
changeset 77800 9a30b76a6f60
parent 77781 c55c4c0c9ef9
child 77802 25c114e2528e
equal deleted inserted replaced
77799:3fb2c47a7605 77800:9a30b76a6f60
    81   Empty |
    81   Empty |
    82   Leaf1 of key * 'a |
    82   Leaf1 of key * 'a |
    83   Leaf2 of (key * 'a) * (key * 'a) |
    83   Leaf2 of (key * 'a) * (key * 'a) |
    84   Leaf3 of (key * 'a) * (key * 'a) * (key * 'a) |
    84   Leaf3 of (key * 'a) * (key * 'a) * (key * 'a) |
    85   Branch2 of 'a table * (key * 'a) * 'a table |
    85   Branch2 of 'a table * (key * 'a) * 'a table |
    86   Branch3 of 'a table * (key * 'a) * 'a table * (key * 'a) * 'a table;
    86   Branch3 of 'a table * (key * 'a) * 'a table * (key * 'a) * 'a table |
       
    87   Size of int * 'a table;
    87 
    88 
    88 (*literal copy from set.ML*)
    89 (*literal copy from set.ML*)
    89 fun make2 (Empty, e, Empty) = Leaf1 e
    90 fun make2 (Empty, e, Empty) = Leaf1 e
    90   | make2 (Branch2 (Empty, e1, Empty), e2, right) = make2 (Leaf1 e1, e2, right)
    91   | make2 (Branch2 (Empty, e1, Empty), e2, right) = make2 (Leaf1 e1, e2, right)
    91   | make2 (left, e1, Branch2 (Empty, e2, Empty)) = make2 (left, e1, Leaf1 e2)
    92   | make2 (left, e1, Branch2 (Empty, e2, Empty)) = make2 (left, e1, Leaf1 e2)
   111 (*literal copy from set.ML*)
   112 (*literal copy from set.ML*)
   112 fun unmake (Leaf1 e) = Branch2 (Empty, e, Empty)
   113 fun unmake (Leaf1 e) = Branch2 (Empty, e, Empty)
   113   | unmake (Leaf2 (e1, e2)) = Branch3 (Empty, e1, Empty, e2, Empty)
   114   | unmake (Leaf2 (e1, e2)) = Branch3 (Empty, e1, Empty, e2, Empty)
   114   | unmake (Leaf3 (e1, e2, e3)) =
   115   | unmake (Leaf3 (e1, e2, e3)) =
   115       Branch2 (Branch2 (Empty, e1, Empty), e2, Branch2 (Empty, e3, Empty))
   116       Branch2 (Branch2 (Empty, e1, Empty), e2, Branch2 (Empty, e3, Empty))
       
   117   | unmake (Size (_, arg)) = arg
   116   | unmake arg = arg;
   118   | unmake arg = arg;
   117 
   119 
   118 
   120 
   119 (* size *)
   121 (* size *)
       
   122 
       
   123 (*literal copy from set.ML*)
       
   124 fun make_size m arg = if m > 12 then Size (m, arg) else arg;
   120 
   125 
   121 local
   126 local
   122   (*literal copy from set.ML*)
   127   (*literal copy from set.ML*)
   123   fun count Empty n = n
   128   fun count Empty n = n
   124     | count (Leaf1 _) n = n + 1
   129     | count (Leaf1 _) n = n + 1
   125     | count (Leaf2 _) n = n + 2
   130     | count (Leaf2 _) n = n + 2
   126     | count (Leaf3 _) n = n + 3
   131     | count (Leaf3 _) n = n + 3
   127     | count (Branch2 (left, _, right)) n = count right (count left (n + 1))
   132     | count (Branch2 (left, _, right)) n = count right (count left (n + 1))
   128     | count (Branch3 (left, _, mid, _, right)) n = count right (count mid (count left (n + 2)));
   133     | count (Branch3 (left, _, mid, _, right)) n = count right (count mid (count left (n + 2)))
       
   134     | count (Size (m, _)) n = m + n;
   129 in
   135 in
   130   fun size tab = Integer.build (count tab);
   136   fun size tab = Integer.build (count tab);
   131 end;
   137 end;
   132 
   138 
   133 
   139 
   135 
   141 
   136 val empty = Empty;
   142 val empty = Empty;
   137 
   143 
   138 fun build (f: 'a table -> 'a table) = f empty;
   144 fun build (f: 'a table -> 'a table) = f empty;
   139 
   145 
       
   146 (*literal copy from set.ML*)
   140 fun is_empty Empty = true
   147 fun is_empty Empty = true
       
   148   | is_empty (Size (_, arg)) = is_empty arg
   141   | is_empty _ = false;
   149   | is_empty _ = false;
   142 
   150 
   143 
   151 
   144 (* map and fold combinators *)
   152 (* map and fold combinators *)
   145 
   153 
   151       | map (Leaf3 ((k1, x1), (k2, x2), (k3, x3))) =
   159       | map (Leaf3 ((k1, x1), (k2, x2), (k3, x3))) =
   152           Leaf3 ((k1, f k1 x1), (k2, f k2 x2), (k3, f k3 x3))
   160           Leaf3 ((k1, f k1 x1), (k2, f k2 x2), (k3, f k3 x3))
   153       | map (Branch2 (left, (k, x), right)) =
   161       | map (Branch2 (left, (k, x), right)) =
   154           Branch2 (map left, (k, f k x), map right)
   162           Branch2 (map left, (k, f k x), map right)
   155       | map (Branch3 (left, (k1, x1), mid, (k2, x2), right)) =
   163       | map (Branch3 (left, (k1, x1), mid, (k2, x2), right)) =
   156           Branch3 (map left, (k1, f k1 x1), map mid, (k2, f k2 x2), map right);
   164           Branch3 (map left, (k1, f k1 x1), map mid, (k2, f k2 x2), map right)
       
   165       | map (Size (m, arg)) = Size (m, map arg);
   157   in map end;
   166   in map end;
   158 
   167 
   159 fun fold_table f =
   168 fun fold_table f =
   160   let
   169   let
   161     fun fold Empty x = x
   170     fun fold Empty x = x
   163       | fold (Leaf2 (e1, e2)) x = f e2 (f e1 x)
   172       | fold (Leaf2 (e1, e2)) x = f e2 (f e1 x)
   164       | fold (Leaf3 (e1, e2, e3)) x = f e3 (f e2 (f e1 x))
   173       | fold (Leaf3 (e1, e2, e3)) x = f e3 (f e2 (f e1 x))
   165       | fold (Branch2 (left, e, right)) x =
   174       | fold (Branch2 (left, e, right)) x =
   166           fold right (f e (fold left x))
   175           fold right (f e (fold left x))
   167       | fold (Branch3 (left, e1, mid, e2, right)) x =
   176       | fold (Branch3 (left, e1, mid, e2, right)) x =
   168           fold right (f e2 (fold mid (f e1 (fold left x))));
   177           fold right (f e2 (fold mid (f e1 (fold left x))))
       
   178       | fold (Size (_, arg)) x = fold arg x;
   169   in fold end;
   179   in fold end;
   170 
   180 
   171 fun fold_rev_table f =
   181 fun fold_rev_table f =
   172   let
   182   let
   173     fun fold_rev Empty x = x
   183     fun fold_rev Empty x = x
   175       | fold_rev (Leaf2 (e1, e2)) x = f e1 (f e2 x)
   185       | fold_rev (Leaf2 (e1, e2)) x = f e1 (f e2 x)
   176       | fold_rev (Leaf3 (e1, e2, e3)) x = f e1 (f e2 (f e3 x))
   186       | fold_rev (Leaf3 (e1, e2, e3)) x = f e1 (f e2 (f e3 x))
   177       | fold_rev (Branch2 (left, e, right)) x =
   187       | fold_rev (Branch2 (left, e, right)) x =
   178           fold_rev left (f e (fold_rev right x))
   188           fold_rev left (f e (fold_rev right x))
   179       | fold_rev (Branch3 (left, e1, mid, e2, right)) x =
   189       | fold_rev (Branch3 (left, e1, mid, e2, right)) x =
   180           fold_rev left (f e1 (fold_rev mid (f e2 (fold_rev right x))));
   190           fold_rev left (f e1 (fold_rev mid (f e2 (fold_rev right x))))
       
   191       | fold_rev (Size (_, arg)) x = fold_rev arg x;
   181   in fold_rev end;
   192   in fold_rev end;
   182 
   193 
   183 fun dest tab = Library.build (fold_rev_table cons tab);
   194 fun dest tab = Library.build (fold_rev_table cons tab);
   184 fun keys tab = Library.build (fold_rev_table (cons o #1) tab);
   195 fun keys tab = Library.build (fold_rev_table (cons o #1) tab);
   185 
   196 
   191   | min (Leaf2 (e, _)) = SOME e
   202   | min (Leaf2 (e, _)) = SOME e
   192   | min (Leaf3 (e, _, _)) = SOME e
   203   | min (Leaf3 (e, _, _)) = SOME e
   193   | min (Branch2 (Empty, e, _)) = SOME e
   204   | min (Branch2 (Empty, e, _)) = SOME e
   194   | min (Branch3 (Empty, e, _, _, _)) = SOME e
   205   | min (Branch3 (Empty, e, _, _, _)) = SOME e
   195   | min (Branch2 (left, _, _)) = min left
   206   | min (Branch2 (left, _, _)) = min left
   196   | min (Branch3 (left, _, _, _, _)) = min left;
   207   | min (Branch3 (left, _, _, _, _)) = min left
       
   208   | min (Size (_, arg)) = min arg;
   197 
   209 
   198 fun max Empty = NONE
   210 fun max Empty = NONE
   199   | max (Leaf1 e) = SOME e
   211   | max (Leaf1 e) = SOME e
   200   | max (Leaf2 (_, e)) = SOME e
   212   | max (Leaf2 (_, e)) = SOME e
   201   | max (Leaf3 (_, _, e)) = SOME e
   213   | max (Leaf3 (_, _, e)) = SOME e
   202   | max (Branch2 (_, e, Empty)) = SOME e
   214   | max (Branch2 (_, e, Empty)) = SOME e
   203   | max (Branch3 (_, _, _, e, Empty)) = SOME e
   215   | max (Branch3 (_, _, _, e, Empty)) = SOME e
   204   | max (Branch2 (_, _, right)) = max right
   216   | max (Branch2 (_, _, right)) = max right
   205   | max (Branch3 (_, _, _, _, right)) = max right;
   217   | max (Branch3 (_, _, _, _, right)) = max right
       
   218   | max (Size (_, arg)) = max arg;
   206 
   219 
   207 
   220 
   208 (* exists and forall *)
   221 (* exists and forall *)
   209 
   222 
   210 fun exists pred =
   223 fun exists pred =
   214       | ex (Leaf2 (e1, e2)) = pred e1 orelse pred e2
   227       | ex (Leaf2 (e1, e2)) = pred e1 orelse pred e2
   215       | ex (Leaf3 (e1, e2, e3)) = pred e1 orelse pred e2 orelse pred e3
   228       | ex (Leaf3 (e1, e2, e3)) = pred e1 orelse pred e2 orelse pred e3
   216       | ex (Branch2 (left, e, right)) =
   229       | ex (Branch2 (left, e, right)) =
   217           ex left orelse pred e orelse ex right
   230           ex left orelse pred e orelse ex right
   218       | ex (Branch3 (left, e1, mid, e2, right)) =
   231       | ex (Branch3 (left, e1, mid, e2, right)) =
   219           ex left orelse pred e1 orelse ex mid orelse pred e2 orelse ex right;
   232           ex left orelse pred e1 orelse ex mid orelse pred e2 orelse ex right
       
   233       | ex (Size (_, arg)) = ex arg;
   220   in ex end;
   234   in ex end;
   221 
   235 
   222 fun forall pred = not o exists (not o pred);
   236 fun forall pred = not o exists (not o pred);
   223 
   237 
   224 
   238 
   256                       (case f e2 of
   270                       (case f e2 of
   257                         NONE => get right
   271                         NONE => get right
   258                       | some => some)
   272                       | some => some)
   259                   | some => some)
   273                   | some => some)
   260               | some => some)
   274               | some => some)
   261           | some => some);
   275           | some => some)
       
   276       | get (Size (_, arg)) = get arg;
   262   in get end;
   277   in get end;
   263 
   278 
   264 
   279 
   265 (* lookup *)
   280 (* lookup *)
   266 
   281 
   293           | EQUAL => SOME x1
   308           | EQUAL => SOME x1
   294           | GREATER =>
   309           | GREATER =>
   295               (case key_ord k2 of
   310               (case key_ord k2 of
   296                 LESS => look mid
   311                 LESS => look mid
   297               | EQUAL => SOME x2
   312               | EQUAL => SOME x2
   298               | GREATER => look right));
   313               | GREATER => look right))
       
   314       | look (Size (_, arg)) = look arg;
   299   in look tab end;
   315   in look tab end;
   300 
   316 
   301 fun lookup_key tab key =
   317 fun lookup_key tab key =
   302   let
   318   let
   303     fun key_ord k = Key.ord (key, k);
   319     fun key_ord k = Key.ord (key, k);
   327           | EQUAL => SOME (k1, x1)
   343           | EQUAL => SOME (k1, x1)
   328           | GREATER =>
   344           | GREATER =>
   329               (case key_ord k2 of
   345               (case key_ord k2 of
   330                 LESS => look mid
   346                 LESS => look mid
   331               | EQUAL => SOME (k2, x2)
   347               | EQUAL => SOME (k2, x2)
   332               | GREATER => look right));
   348               | GREATER => look right))
       
   349       | look (Size (_, arg)) = look arg;
   333   in look tab end;
   350   in look tab end;
   334 
   351 
   335 fun defined tab key =
   352 fun defined tab key =
   336   let
   353   let
   337     fun key_ord k = Key.ord (key, k);
   354     fun key_ord k = Key.ord (key, k);
   360           | EQUAL => true
   377           | EQUAL => true
   361           | GREATER =>
   378           | GREATER =>
   362               (case key_ord k2 of
   379               (case key_ord k2 of
   363                 LESS => def mid
   380                 LESS => def mid
   364               | EQUAL => true
   381               | EQUAL => true
   365               | GREATER => def right));
   382               | GREATER => def right))
       
   383       | def (Size (_, arg)) = def arg;
   366   in def tab end;
   384   in def tab end;
   367 
   385 
   368 
   386 
   369 (* modify *)
   387 (* modify *)
   370 
   388 
   376 
   394 
   377 fun modify key f tab =
   395 fun modify key f tab =
   378   let
   396   let
   379     fun key_ord k = Key.ord (key, k);
   397     fun key_ord k = Key.ord (key, k);
   380 
   398 
   381     fun modfy Empty = Sprout (Empty, (key, f NONE), Empty)
   399     val inc = Unsynchronized.ref 0;
       
   400     fun insert () = f NONE before ignore (Unsynchronized.inc inc);
       
   401     fun update x = f (SOME x);
       
   402 
       
   403     fun modfy Empty = Sprout (Empty, (key, insert ()), Empty)
   382       | modfy (t as Leaf1 _) = modfy (unmake t)
   404       | modfy (t as Leaf1 _) = modfy (unmake t)
   383       | modfy (t as Leaf2 _) = modfy (unmake t)
   405       | modfy (t as Leaf2 _) = modfy (unmake t)
   384       | modfy (t as Leaf3 _) = modfy (unmake t)
   406       | modfy (t as Leaf3 _) = modfy (unmake t)
   385       | modfy (Branch2 (left, p as (k, x), right)) =
   407       | modfy (Branch2 (left, p as (k, x), right)) =
   386           (case key_ord k of
   408           (case key_ord k of
   387             LESS =>
   409             LESS =>
   388               (case modfy left of
   410               (case modfy left of
   389                 Stay left' => Stay (make2 (left', p, right))
   411                 Stay left' => Stay (make2 (left', p, right))
   390               | Sprout (left1, q, left2) => Stay (make3 (left1, q, left2, p, right)))
   412               | Sprout (left1, q, left2) => Stay (make3 (left1, q, left2, p, right)))
   391           | EQUAL => Stay (make2 (left, (k, f (SOME x)), right))
   413           | EQUAL => Stay (make2 (left, (k, update x), right))
   392           | GREATER =>
   414           | GREATER =>
   393               (case modfy right of
   415               (case modfy right of
   394                 Stay right' => Stay (make2 (left, p, right'))
   416                 Stay right' => Stay (make2 (left, p, right'))
   395               | Sprout (right1, q, right2) =>
   417               | Sprout (right1, q, right2) =>
   396                   Stay (make3 (left, p, right1, q, right2))))
   418                   Stay (make3 (left, p, right1, q, right2))))
   399             LESS =>
   421             LESS =>
   400               (case modfy left of
   422               (case modfy left of
   401                 Stay left' => Stay (make3 (left', p1, mid, p2, right))
   423                 Stay left' => Stay (make3 (left', p1, mid, p2, right))
   402               | Sprout (left1, q, left2) =>
   424               | Sprout (left1, q, left2) =>
   403                   Sprout (make2 (left1, q, left2), p1, make2 (mid, p2, right)))
   425                   Sprout (make2 (left1, q, left2), p1, make2 (mid, p2, right)))
   404           | EQUAL => Stay (make3 (left, (k1, f (SOME x1)), mid, p2, right))
   426           | EQUAL => Stay (make3 (left, (k1, update x1), mid, p2, right))
   405           | GREATER =>
   427           | GREATER =>
   406               (case key_ord k2 of
   428               (case key_ord k2 of
   407                 LESS =>
   429                 LESS =>
   408                   (case modfy mid of
   430                   (case modfy mid of
   409                     Stay mid' => Stay (make3 (left, p1, mid', p2, right))
   431                     Stay mid' => Stay (make3 (left, p1, mid', p2, right))
   410                   | Sprout (mid1, q, mid2) =>
   432                   | Sprout (mid1, q, mid2) =>
   411                       Sprout (make2 (left, p1, mid1), q, make2 (mid2, p2, right)))
   433                       Sprout (make2 (left, p1, mid1), q, make2 (mid2, p2, right)))
   412               | EQUAL => Stay (make3 (left, p1, mid, (k2, f (SOME x2)), right))
   434               | EQUAL => Stay (make3 (left, p1, mid, (k2, update x2), right))
   413               | GREATER =>
   435               | GREATER =>
   414                   (case modfy right of
   436                   (case modfy right of
   415                     Stay right' => Stay (make3 (left, p1, mid, p2, right'))
   437                     Stay right' => Stay (make3 (left, p1, mid, p2, right'))
   416                   | Sprout (right1, q, right2) =>
   438                   | Sprout (right1, q, right2) =>
   417                       Sprout (make2 (left, p1, mid), p2, make2 (right1, q, right2)))));
   439                       Sprout (make2 (left, p1, mid), p2, make2 (right1, q, right2)))))
   418 
   440       | modfy (Size (_, arg)) = modfy arg;
       
   441 
       
   442     val tab' =
       
   443       (case modfy tab of
       
   444         Stay tab' => tab'
       
   445       | Sprout br => make2 br);
   419   in
   446   in
   420     (case modfy tab of
   447     make_size (size tab + !inc) tab'
   421       Stay tab' => tab'
   448   end handle SAME => tab;
   422     | Sprout br => make2 br)
       
   423     handle SAME => tab
       
   424   end;
       
   425 
   449 
   426 fun update (key, x) tab = modify key (fn _ => x) tab;
   450 fun update (key, x) tab = modify key (fn _ => x) tab;
   427 fun update_new (key, x) tab = modify key (fn NONE => x | SOME _ => raise DUP key) tab;
   451 fun update_new (key, x) tab = modify key (fn NONE => x | SOME _ => raise DUP key) tab;
   428 fun default (key, x) tab = modify key (fn NONE => x | SOME _ => raise SAME) tab;
   452 fun default (key, x) tab = modify key (fn NONE => x | SOME _ => raise SAME) tab;
   429 fun map_entry key f = modify key (fn NONE => raise SAME | SOME x => f x);
   453 fun map_entry key f = modify key (fn NONE => raise SAME | SOME x => f x);
   514             | (_, Branch3 (ml, mp, mm, mq, mr)) =>
   538             | (_, Branch3 (ml, mp, mm, mq, mr)) =>
   515                 make3 (l, p, make2 (ml, mp, mm), mq,
   539                 make3 (l, p, make2 (ml, mp, mm), mq,
   516                   make2 (mr, if_equal ord q' q, r'))
   540                   make2 (mr, if_equal ord q' q, r'))
   517             | (Branch3 (ll, lp, lm, lq, lr), Branch2 (ml, mp, mr)) =>
   541             | (Branch3 (ll, lp, lm, lq, lr), Branch2 (ml, mp, mr)) =>
   518                 make3 (make2 (ll, lp, lm), lq, make2 (lr, p, ml), mp,
   542                 make3 (make2 (ll, lp, lm), lq, make2 (lr, p, ml), mp,
   519                   make2 (mr, if_equal ord q' q, r'))))));
   543                   make2 (mr, if_equal ord q' q, r'))))))
   520 
   544   | del k (Size (_, arg)) = del k arg;
   521 in
   545 in
   522 
   546 
   523 fun delete key tab = snd (snd (del (SOME key) tab));
   547 fun delete key tab = make_size (size tab - 1) (snd (snd (del (SOME key) tab)));
   524 fun delete_safe key tab = if defined tab key then delete key tab else tab;
   548 fun delete_safe key tab = if defined tab key then delete key tab else tab;
   525 
   549 
   526 end;
   550 end;
   527 
   551 
   528 
   552