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