src/Pure/General/table.ML
author skalberg
Sun, 13 Feb 2005 17:15:14 +0100
changeset 15531 08c8dad8e399
parent 15160 394fb9b8908b
child 15570 8d8c70b41bab
permissions -rw-r--r--
Deleted Library.option type.
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
8806
wenzelm
parents: 8606
diff changeset
     5
Generic tables and tables indexed by strings.  Efficient purely
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
     6
functional implementation using 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
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    25
  val foldl: ('a * (key * 'b) -> 'a) -> 'a * 'b table -> 'a
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    26
  val dest: 'a table -> (key * 'a) list
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    27
  val keys: 'a table -> key list
8409
ef01ee11b14e Added function min_key.
berghofe
parents: 7061
diff changeset
    28
  val min_key: 'a table -> key option
15160
394fb9b8908b changes
obua
parents: 15014
diff changeset
    29
  val max_key: 'a table -> key option
7061
69d42b56151f added exists;
wenzelm
parents: 6118
diff changeset
    30
  val exists: (key * 'a -> bool) -> 'a table -> bool
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    31
  val lookup: 'a table * key -> 'a option
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    32
  val update: (key * 'a) * 'a table -> 'a table
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
    33
  val update_new: (key * 'a) * 'a table -> 'a table                    (*exception DUP*)
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
    34
  val make: (key * 'a) list -> 'a table                                (*exception DUPS*)
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
    35
  val extend: 'a table * (key * 'a) list -> 'a table                   (*exception DUPS*)
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
    36
  val join: ('a * 'a -> 'a option) -> 'a table * 'a table -> 'a table  (*exception DUPS*)
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
    37
  val merge: ('a * 'a -> bool) -> 'a table * 'a table -> 'a table      (*exception DUPS*)
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
    38
  val delete: key -> 'a table -> 'a table    (* exception UNDEF *)
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    39
  val lookup_multi: 'a list table * key -> 'a list
8606
80992b8566e5 export update_multi;
wenzelm
parents: 8409
diff changeset
    40
  val update_multi: (key * 'a) * 'a list table -> 'a list table
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    41
  val make_multi: (key * 'a) list -> 'a list table
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    42
  val dest_multi: 'a list table -> (key * 'a) list
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
    43
  val merge_multi: ('a * 'a -> bool) ->
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
    44
    'a list table * 'a list table -> 'a list table    (*exception DUPS*)
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
    45
  val merge_multi': ('a * 'a -> bool) ->
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
    46
    'a list table * 'a list table -> 'a list table    (*exception DUPS*)
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    47
end;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    48
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    49
functor TableFun(Key: KEY): TABLE =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    50
struct
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    51
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    52
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    53
(* datatype table *)
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    54
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    55
type key = Key.key;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    56
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    57
datatype 'a table =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    58
  Empty |
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    59
  Branch2 of 'a table * (key * 'a) * 'a table |
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    60
  Branch3 of 'a table * (key * 'a) * 'a table * (key * 'a) * 'a table;
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
exception DUP of key;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    63
exception DUPS of key list;
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
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    66
(* empty *)
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    67
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    68
val empty = Empty;
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
fun is_empty Empty = true
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    71
  | is_empty _ = false;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    72
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    73
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    74
(* map and fold combinators *)
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    75
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    76
fun map_table _ Empty = Empty
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    77
  | map_table f (Branch2 (left, (k, x), right)) =
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    78
      Branch2 (map_table f left, (k, f x), map_table f right)
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    79
  | map_table f (Branch3 (left, (k1, x1), mid, (k2, x2), right)) =
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    80
      Branch3 (map_table f left, (k1, f x1), map_table f mid, (k2, f x2), map_table f right);
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    81
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    82
fun foldl_table _ (x, Empty) = x
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    83
  | foldl_table f (x, Branch2 (left, p, right)) =
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    84
      foldl_table f (f (foldl_table f (x, left), p), right)
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    85
  | foldl_table f (x, Branch3 (left, p1, mid, p2, right)) =
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    86
      foldl_table f (f (foldl_table f (f (foldl_table f (x, left), p1), mid), p2), right);
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    87
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    88
fun dest tab = rev (foldl_table (fn (rev_ps, p) => p :: rev_ps) ([], tab));
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
    89
fun keys tab = rev (foldl_table (fn (rev_ks, (k, _)) => k :: rev_ks) ([], tab));
7061
69d42b56151f added exists;
wenzelm
parents: 6118
diff changeset
    90
fun exists P tab = foldl_table (fn (false, e) => P e | (b, _) => b) (false, tab);
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    91
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
    92
fun min_key Empty = NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
    93
  | min_key (Branch2 (left, (k, _), _)) = SOME (if_none (min_key left) k)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
    94
  | min_key (Branch3 (left, (k, _), _, _, _)) = SOME (if_none (min_key left) k);
8409
ef01ee11b14e Added function min_key.
berghofe
parents: 7061
diff changeset
    95
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
    96
fun max_key Empty = NONE
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
    97
  | max_key (Branch2 (_, (k, _), right)) = SOME (if_none (max_key right) k)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
    98
  | max_key (Branch3 (_, _, _, (k,_), right)) = SOME (if_none (max_key right) k);
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    99
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   100
(* lookup *)
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   101
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   102
fun lookup (Empty, _) = NONE
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   103
  | lookup (Branch2 (left, (k, x), right), key) =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   104
      (case Key.ord (key, k) of
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   105
        LESS => lookup (left, key)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   106
      | EQUAL => SOME x
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   107
      | GREATER => lookup (right, key))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   108
  | lookup (Branch3 (left, (k1, x1), mid, (k2, x2), right), key) =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   109
      (case Key.ord (key, k1) of
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   110
        LESS => lookup (left, key)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   111
      | EQUAL => SOME x1
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   112
      | GREATER =>
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   113
          (case Key.ord (key, k2) of
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   114
            LESS => lookup (mid, key)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   115
          | EQUAL => SOME x2
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   116
          | GREATER => lookup (right, key)));
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   117
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   118
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   119
(* update *)
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   120
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   121
fun compare (k1, _) (k2, _) = Key.ord (k1, k2);
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   122
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   123
datatype 'a growth =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   124
  Stay of 'a table |
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   125
  Sprout of 'a table * (key * 'a) * 'a table;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   126
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   127
fun insert pair Empty = Sprout (Empty, pair, Empty)
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   128
  | insert pair (Branch2 (left, p, right)) =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   129
      (case compare pair p of
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   130
        LESS =>
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   131
          (case insert pair left of
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   132
            Stay left' => Stay (Branch2 (left', p, right))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   133
          | Sprout (left1, q, left2) => Stay (Branch3 (left1, q, left2, p, right)))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   134
      | EQUAL => Stay (Branch2 (left, pair, right))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   135
      | GREATER =>
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   136
          (case insert pair right of
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   137
            Stay right' => Stay (Branch2 (left, p, right'))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   138
          | Sprout (right1, q, right2) =>
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   139
              Stay (Branch3 (left, p, right1, q, right2))))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   140
  | insert pair (Branch3 (left, p1, mid, p2, right)) =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   141
      (case compare pair p1 of
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   142
        LESS =>
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   143
          (case insert pair left of
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   144
            Stay left' => Stay (Branch3 (left', p1, mid, p2, right))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   145
          | Sprout (left1, q, left2) =>
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   146
              Sprout (Branch2 (left1, q, left2), p1, Branch2 (mid, p2, right)))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   147
      | EQUAL => Stay (Branch3 (left, pair, mid, p2, right))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   148
      | GREATER =>
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   149
          (case compare pair p2 of
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   150
            LESS =>
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   151
              (case insert pair mid of
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   152
                Stay mid' => Stay (Branch3 (left, p1, mid', p2, right))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   153
              | Sprout (mid1, q, mid2) =>
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   154
                  Sprout (Branch2 (left, p1, mid1), q, Branch2 (mid2, p2, right)))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   155
          | EQUAL => Stay (Branch3 (left, p1, mid, pair, right))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   156
          | GREATER =>
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   157
              (case insert pair right of
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   158
                Stay right' => Stay (Branch3 (left, p1, mid, p2, right'))
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   159
              | Sprout (right1, q, right2) =>
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   160
                  Sprout (Branch2 (left, p1, mid), p2, Branch2 (right1, q, right2)))));
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   161
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   162
fun update (pair, tab) =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   163
  (case insert pair tab of
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   164
    Stay tab => tab
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   165
  | Sprout br => Branch2 br);
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   166
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   167
fun update_new (pair as (key, _), tab) =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   168
  if is_none (lookup (tab, key)) then update (pair, tab)
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   169
  else raise DUP key;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   170
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   171
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   172
(* extend and make *)
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   173
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   174
fun extend (table, pairs) =
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   175
  let
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   176
    fun add ((tab, dups), (key, x)) =
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   177
      (case lookup (tab, key) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   178
        NONE => (update ((key, x), tab), dups)
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   179
      | _ => (tab, key :: dups));
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   180
  in
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   181
    (case foldl add ((table, []), pairs) of
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   182
      (table', []) => table'
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   183
    | (_, dups) => raise DUPS (rev dups))
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   184
  end;
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   185
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   186
fun make pairs = extend (empty, pairs);
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   187
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   188
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   189
(* delete *)
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   190
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   191
fun compare' NONE (k2, _) = LESS
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   192
  | compare' (SOME k1) (k2, _) = Key.ord (k1, k2);
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   193
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   194
fun if_eq EQUAL x y = x
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   195
  | if_eq _ x y = y;
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   196
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   197
exception UNDEF of key;
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   198
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   199
fun del (SOME k) Empty = raise UNDEF k
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   200
  | del NONE (Branch2 (Empty, p, Empty)) = (p, (true, Empty))
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   201
  | del NONE (Branch3 (Empty, p, Empty, q, Empty)) =
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   202
      (p, (false, Branch2 (Empty, q, Empty)))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   203
  | del k (Branch2 (Empty, p, Empty)) = (case compare' k p of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   204
      EQUAL => (p, (true, Empty)) | _ => raise UNDEF (the k))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   205
  | del k (Branch3 (Empty, p, Empty, q, Empty)) = (case compare' k p of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   206
      EQUAL => (p, (false, Branch2 (Empty, q, Empty)))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   207
    | _ => (case compare' k q of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   208
        EQUAL => (q, (false, Branch2 (Empty, p, Empty)))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   209
      | _ => raise UNDEF (the k)))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   210
  | del k (Branch2 (l, p, r)) = (case compare' k p of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   211
      LESS => (case del k l of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   212
        (p', (false, l')) => (p', (false, Branch2 (l', p, r)))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   213
      | (p', (true, l')) => (p', case r of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   214
          Branch2 (rl, rp, rr) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   215
            (true, Branch3 (l', p, rl, rp, rr))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   216
        | Branch3 (rl, rp, rm, rq, rr) => (false, Branch2
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   217
            (Branch2 (l', p, rl), rp, Branch2 (rm, rq, rr)))))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   218
    | ord => (case del (if_eq ord NONE k) r of
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   219
        (p', (false, r')) => (p', (false, Branch2 (l, if_eq ord p' p, r')))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   220
      | (p', (true, r')) => (p', case l of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   221
          Branch2 (ll, lp, lr) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   222
            (true, Branch3 (ll, lp, lr, if_eq ord p' p, r'))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   223
        | Branch3 (ll, lp, lm, lq, lr) => (false, Branch2
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   224
            (Branch2 (ll, lp, lm), lq, Branch2 (lr, if_eq ord p' p, r'))))))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   225
  | del k (Branch3 (l, p, m, q, r)) = (case compare' k q of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   226
      LESS => (case compare' k p of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   227
        LESS => (case del k l of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   228
          (p', (false, l')) => (p', (false, Branch3 (l', p, m, q, r)))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   229
        | (p', (true, l')) => (p', (false, case (m, r) of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   230
            (Branch2 (ml, mp, mr), Branch2 _) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   231
              Branch2 (Branch3 (l', p, ml, mp, mr), q, r)
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   232
          | (Branch3 (ml, mp, mm, mq, mr), _) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   233
              Branch3 (Branch2 (l', p, ml), mp, Branch2 (mm, mq, mr), q, r)
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   234
          | (Branch2 (ml, mp, mr), Branch3 (rl, rp, rm, rq, rr)) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   235
              Branch3 (Branch2 (l', p, ml), mp, Branch2 (mr, q, rl), rp,
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   236
                Branch2 (rm, rq, rr)))))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   237
      | ord => (case del (if_eq ord NONE k) m of
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   238
          (p', (false, m')) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   239
            (p', (false, Branch3 (l, if_eq ord p' p, m', q, r)))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   240
        | (p', (true, m')) => (p', (false, case (l, r) of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   241
            (Branch2 (ll, lp, lr), Branch2 _) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   242
              Branch2 (Branch3 (ll, lp, lr, if_eq ord p' p, m'), q, r)
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   243
          | (Branch3 (ll, lp, lm, lq, lr), _) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   244
              Branch3 (Branch2 (ll, lp, lm), lq,
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   245
                Branch2 (lr, if_eq ord p' p, m'), q, r)
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   246
          | (_, Branch3 (rl, rp, rm, rq, rr)) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   247
              Branch3 (l, if_eq ord p' p, Branch2 (m', q, rl), rp,
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   248
                Branch2 (rm, rq, rr))))))
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   249
    | ord => (case del (if_eq ord NONE k) r of
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   250
        (q', (false, r')) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   251
          (q', (false, Branch3 (l, p, m, if_eq ord q' q, r')))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   252
      | (q', (true, r')) => (q', (false, case (l, m) of
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   253
          (Branch2 _, Branch2 (ml, mp, mr)) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   254
            Branch2 (l, p, Branch3 (ml, mp, mr, if_eq ord q' q, r'))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   255
        | (_, Branch3 (ml, mp, mm, mq, mr)) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   256
            Branch3 (l, p, Branch2 (ml, mp, mm), mq,
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   257
              Branch2 (mr, if_eq ord q' q, r'))
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   258
        | (Branch3 (ll, lp, lm, lq, lr), Branch2 (ml, mp, mr)) =>
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   259
            Branch3 (Branch2 (ll, lp, lm), lq, Branch2 (lr, p, ml), mp,
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   260
              Branch2 (mr, if_eq ord q' q, r'))))));
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   261
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   262
fun delete k t = snd (snd (del (SOME k) t));
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   263
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   264
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   265
(* join and merge *)
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   266
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   267
fun join f (table1, table2) =
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   268
  let
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   269
    fun add ((tab, dups), (key, x)) =
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   270
      (case lookup (tab, key) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   271
        NONE => (update ((key, x), tab), dups)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   272
      | SOME y =>
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   273
          (case f (y, x) of
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   274
            SOME z => (update ((key, z), tab), dups)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   275
          | NONE => (tab, key :: dups)));
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   276
  in
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   277
    (case foldl_table add ((table1, []), table2) of
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   278
      (table', []) => table'
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   279
    | (_, dups) => raise DUPS (rev dups))
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   280
  end;
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   281
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   282
fun merge eq tabs = join (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
   283
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   284
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   285
(* tables with multiple entries per key (preserving order) *)
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   286
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   287
fun lookup_multi tab_key = if_none (lookup tab_key) [];
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   288
fun update_multi ((key, x), tab) = update ((key, x :: lookup_multi (tab, key)), tab);
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   289
8606
80992b8566e5 export update_multi;
wenzelm
parents: 8409
diff changeset
   290
fun make_multi pairs = foldr update_multi (pairs, empty);
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   291
fun dest_multi tab = flat (map (fn (key, xs) => map (Library.pair key) xs) (dest tab));
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   292
fun merge_multi eq tabs = join (fn (xs, xs') => SOME (gen_merge_lists eq xs xs')) tabs;
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   293
fun merge_multi' eq tabs = join (fn (xs, xs') => SOME (gen_merge_lists' eq xs xs')) tabs;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   294
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   295
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   296
(*final declarations of this structure!*)
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   297
val map = map_table;
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   298
val foldl = foldl_table;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   299
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   300
end;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   301
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   302
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   303
(*tables indexed by strings*)
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   304
structure Symtab = TableFun(type key = string val ord = string_ord);