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