src/Pure/General/table.ML
author wenzelm
Tue, 28 Mar 2023 22:43:05 +0200
changeset 77736 570f1436fe0a
parent 77735 be3f838b3e17
child 77737 81d553e9428d
permissions -rw-r--r--
more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
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
70586
57df8a85317a clarified signature;
wenzelm
parents: 67557
diff changeset
    11
  val ord: key ord
5015
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
77731
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77727
diff changeset
    16
  structure Key: KEY
5015
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
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
    20
  exception SAME
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
74232
1091880266e5 clarified signature;
wenzelm
parents: 74227
diff changeset
    23
  val build: ('a table -> 'a table) -> 'a table
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    24
  val is_empty: 'a table -> bool
67537
f0b183b433cb more operations;
wenzelm
parents: 67529
diff changeset
    25
  val is_single: 'a table -> bool
39020
ac0f24f850c9 replaced Table.map' by Table.map
haftmann
parents: 38635
diff changeset
    26
  val map: (key -> 'a -> 'b) -> 'a table -> 'b table
16446
0ab34b9d1b1f added map', fold;
wenzelm
parents: 16342
diff changeset
    27
  val fold: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> 'a
28334
7c693bb76366 added fold_rev;
wenzelm
parents: 27783
diff changeset
    28
  val fold_rev: (key * 'b -> 'a -> 'a) -> 'b table -> 'a -> '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
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
    31
  val min: 'a table -> (key * 'a) option
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
    32
  val max: 'a table -> (key * 'a) option
27508
abdab08677d8 added get_first;
wenzelm
parents: 25391
diff changeset
    33
  val exists: (key * 'a -> bool) -> 'a table -> bool
abdab08677d8 added get_first;
wenzelm
parents: 25391
diff changeset
    34
  val forall: (key * 'a -> bool) -> 'a table -> bool
74227
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
    35
  val get_first: (key * 'a -> 'b option) -> 'a table -> 'b option
43792
d5803c3d537a Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
wenzelm
parents: 39020
diff changeset
    36
  val lookup_key: 'a table -> key -> (key * 'a) option
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
    37
  val lookup: 'a table -> key -> 'a option
16887
b24c067b32d6 added defined;
wenzelm
parents: 16864
diff changeset
    38
  val defined: 'a table -> key -> bool
31209
77da3aad5917 dropped parentheses
haftmann
parents: 30647
diff changeset
    39
  val update: key * 'a -> 'a table -> 'a table
77da3aad5917 dropped parentheses
haftmann
parents: 30647
diff changeset
    40
  val update_new: key * 'a -> 'a table -> 'a table                     (*exception DUP*)
17179
28802c8a9816 canonical interface for 'default'
haftmann
parents: 17140
diff changeset
    41
  val default: key * 'a -> 'a table -> 'a table
56051
c3681b9e060f tuned comment;
wenzelm
parents: 55727
diff changeset
    42
  val map_entry: key -> ('a -> 'a) (*exception SAME*) -> 'a table -> 'a table
31209
77da3aad5917 dropped parentheses
haftmann
parents: 30647
diff changeset
    43
  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
    44
  val make: (key * 'a) list -> 'a table                                (*exception DUP*)
56051
c3681b9e060f tuned comment;
wenzelm
parents: 55727
diff changeset
    45
  val join: (key -> 'a * 'a -> 'a) (*exception SAME*) ->
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 21065
diff changeset
    46
    'a table * 'a table -> 'a table                                    (*exception DUP*)
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 21065
diff changeset
    47
  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
    48
  val delete: key -> 'a table -> 'a table                              (*exception UNDEF*)
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
    49
  val delete_safe: key -> 'a table -> 'a table
16466
82e2fc13ce54 added member;
wenzelm
parents: 16446
diff changeset
    50
  val member: ('b * 'a -> bool) -> 'a table -> key * 'b -> bool
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
    51
  val insert: ('a * 'a -> bool) -> key * 'a -> 'a table -> 'a table    (*exception DUP*)
16139
21ce46ca61cc remove(_multi): generalized type;
wenzelm
parents: 16002
diff changeset
    52
  val remove: ('b * 'a -> bool) -> key * 'b -> 'a table -> 'a table
18946
ce65d2d2e0c2 renamed xxx_multi to xxx_list;
wenzelm
parents: 18007
diff changeset
    53
  val lookup_list: 'a list table -> key -> 'a list
31209
77da3aad5917 dropped parentheses
haftmann
parents: 30647
diff changeset
    54
  val cons_list: key * 'a -> 'a list table -> 'a list table
19506
980a2edf9e82 added insert_list;
wenzelm
parents: 19482
diff changeset
    55
  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
    56
  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
    57
  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
    58
  val make_list: (key * 'a) list -> 'a list table
ce65d2d2e0c2 renamed xxx_multi to xxx_list;
wenzelm
parents: 18007
diff changeset
    59
  val dest_list: 'a list table -> (key * 'a) list
33162
d11b89e7afd9 merge_list: no exception DUP here;
wenzelm
parents: 31971
diff changeset
    60
  val merge_list: ('a * 'a -> bool) -> 'a list table * 'a list table -> 'a list table
50234
c97c5c34fb1d convenience operations for table as set;
wenzelm
parents: 47980
diff changeset
    61
  type set = unit table
63511
1c2c045decb3 more operations;
wenzelm
parents: 62819
diff changeset
    62
  val insert_set: key -> set -> set
67529
37db2dc5c022 clarified operations;
wenzelm
parents: 66935
diff changeset
    63
  val remove_set: key -> set -> set
59012
f4e9bd04e1d5 clarified Table.make_set: duplicate arguments are allowed, like Table.make_list or Scala Set() formation;
wenzelm
parents: 56051
diff changeset
    64
  val make_set: key list -> set
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    65
end;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    66
31971
8c1b845ed105 renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents: 31621
diff changeset
    67
functor Table(Key: KEY): TABLE =
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    68
struct
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    69
77733
wenzelm
parents: 77732
diff changeset
    70
(* keys *)
wenzelm
parents: 77732
diff changeset
    71
77731
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77727
diff changeset
    72
structure Key = Key;
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77727
diff changeset
    73
type key = Key.key;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    74
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    75
val eq_key = is_equal o Key.ord;
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    76
77733
wenzelm
parents: 77732
diff changeset
    77
exception DUP of key;
wenzelm
parents: 77732
diff changeset
    78
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    79
77731
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77727
diff changeset
    80
(* datatype *)
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    81
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    82
datatype 'a table =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    83
  Empty |
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    84
  Leaf1 of key * 'a |
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    85
  Leaf2 of (key * 'a) * (key * 'a) |
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    86
  Branch2 of 'a table * (key * 'a) * 'a table |
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    87
  Branch3 of 'a table * (key * 'a) * 'a table * (key * 'a) * 'a table;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    88
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    89
fun make2 (Empty, e, Empty) = Leaf1 e
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    90
  | make2 arg = Branch2 arg;
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    91
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    92
fun make3 (Empty, e1, Empty, e2, Empty) = Leaf2 (e1, e2)
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    93
  | make3 arg = Branch3 arg;
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    94
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    95
fun unmake (Leaf1 e) = Branch2 (Empty, e, Empty)
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    96
  | unmake (Leaf2 (e1, e2)) = Branch3 (Empty, e1, Empty, e2, Empty)
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    97
  | unmake arg = arg;
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    98
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    99
67537
f0b183b433cb more operations;
wenzelm
parents: 67529
diff changeset
   100
(* empty and single *)
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   101
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   102
val empty = Empty;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   103
74232
1091880266e5 clarified signature;
wenzelm
parents: 74227
diff changeset
   104
fun build (f: 'a table -> 'a table) = f empty;
1091880266e5 clarified signature;
wenzelm
parents: 74227
diff changeset
   105
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   106
fun is_empty Empty = true
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   107
  | is_empty _ = false;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   108
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   109
fun is_single (Leaf1 _) = true
67537
f0b183b433cb more operations;
wenzelm
parents: 67529
diff changeset
   110
  | is_single _ = false;
f0b183b433cb more operations;
wenzelm
parents: 67529
diff changeset
   111
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   112
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   113
(* map and fold combinators *)
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   114
16657
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   115
fun map_table f =
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   116
  let
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   117
    fun map Empty = Empty
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   118
      | map (Leaf1 (k, x)) = Leaf1 (k, f k x)
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   119
      | map (Leaf2 ((k1, x1), (k2, x2))) = Leaf2 ((k1, f k1 x1), (k2, f k2 x2))
16657
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   120
      | map (Branch2 (left, (k, x), right)) =
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   121
          Branch2 (map left, (k, f k x), map right)
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   122
      | map (Branch3 (left, (k1, x1), mid, (k2, x2), right)) =
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   123
          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
   124
  in map end;
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   125
16657
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   126
fun fold_table f =
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   127
  let
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   128
    fun fold Empty x = x
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   129
      | fold (Leaf1 e) x = f e x
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   130
      | fold (Leaf2 (e1, e2)) x = f e2 (f e1 x)
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   131
      | fold (Branch2 (left, e, right)) x =
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   132
          fold right (f e (fold left x))
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   133
      | fold (Branch3 (left, e1, mid, e2, right)) x =
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   134
          fold right (f e2 (fold mid (f e1 (fold left x))));
16657
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   135
  in fold end;
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   136
28334
7c693bb76366 added fold_rev;
wenzelm
parents: 27783
diff changeset
   137
fun fold_rev_table f =
7c693bb76366 added fold_rev;
wenzelm
parents: 27783
diff changeset
   138
  let
77732
wenzelm
parents: 77731
diff changeset
   139
    fun fold_rev Empty x = x
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   140
      | fold_rev (Leaf1 e) x = f e x
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   141
      | fold_rev (Leaf2 (e1, e2)) x = f e1 (f e2 x)
77732
wenzelm
parents: 77731
diff changeset
   142
      | fold_rev (Branch2 (left, e, right)) x =
wenzelm
parents: 77731
diff changeset
   143
          fold_rev left (f e (fold_rev right x))
wenzelm
parents: 77731
diff changeset
   144
      | fold_rev (Branch3 (left, e1, mid, e2, right)) x =
wenzelm
parents: 77731
diff changeset
   145
          fold_rev left (f e1 (fold_rev mid (f e2 (fold_rev right x))));
wenzelm
parents: 77731
diff changeset
   146
  in fold_rev end;
28334
7c693bb76366 added fold_rev;
wenzelm
parents: 27783
diff changeset
   147
7c693bb76366 added fold_rev;
wenzelm
parents: 27783
diff changeset
   148
fun dest tab = fold_rev_table cons tab [];
7c693bb76366 added fold_rev;
wenzelm
parents: 27783
diff changeset
   149
fun keys tab = fold_rev_table (cons o #1) tab [];
16192
733267a60e32 exists: made non-strict;
wenzelm
parents: 16139
diff changeset
   150
27508
abdab08677d8 added get_first;
wenzelm
parents: 25391
diff changeset
   151
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
   152
(* min/max entries *)
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   153
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
   154
fun min Empty = NONE
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   155
  | min (Leaf1 e) = SOME e
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   156
  | min (Leaf2 (e, _)) = SOME e
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   157
  | min (Branch2 (Empty, e, _)) = SOME e
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   158
  | min (Branch3 (Empty, e, _, _, _)) = SOME e
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
   159
  | min (Branch2 (left, _, _)) = min left
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
   160
  | min (Branch3 (left, _, _, _, _)) = min left;
8409
ef01ee11b14e Added function min_key.
berghofe
parents: 7061
diff changeset
   161
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
   162
fun max Empty = NONE
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   163
  | max (Leaf1 e) = SOME e
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   164
  | max (Leaf2 (_, e)) = SOME e
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   165
  | max (Branch2 (_, e, Empty)) = SOME e
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   166
  | max (Branch3 (_, _, _, e, Empty)) = SOME e
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
   167
  | max (Branch2 (_, _, right)) = max right
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
   168
  | max (Branch3 (_, _, _, _, right)) = max right;
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   169
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   170
74227
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   171
(* exists and forall *)
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   172
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   173
fun exists pred =
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   174
  let
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   175
    fun ex Empty = false
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   176
      | ex (Leaf1 e) = pred e
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   177
      | ex (Leaf2 (e1, e2)) = pred e1 orelse pred e2
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   178
      | ex (Branch2 (left, e, right)) =
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   179
          ex left orelse pred e orelse ex right
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   180
      | ex (Branch3 (left, e1, mid, e2, right)) =
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   181
          ex left orelse pred e1 orelse ex mid orelse pred e2 orelse ex right;
74227
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   182
  in ex end;
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   183
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   184
fun forall pred = not o exists (not o pred);
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   185
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   186
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   187
(* get_first *)
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   188
35012
c3e3ac3ca091 removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents: 33162
diff changeset
   189
fun get_first f =
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   190
  let
35012
c3e3ac3ca091 removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents: 33162
diff changeset
   191
    fun get Empty = NONE
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   192
      | get (Leaf1 e) = f e
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   193
      | get (Leaf2 (e1, e2)) =
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   194
          (case f e1 of
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   195
            NONE => f e2
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   196
          | some => some)
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   197
      | get (Branch2 (left, e, right)) =
35012
c3e3ac3ca091 removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents: 33162
diff changeset
   198
          (case get left of
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   199
            NONE =>
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   200
              (case f e of
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   201
                NONE => get right
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   202
              | some => some)
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   203
          | some => some)
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   204
      | get (Branch3 (left, e1, mid, e2, right)) =
35012
c3e3ac3ca091 removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents: 33162
diff changeset
   205
          (case get left of
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   206
            NONE =>
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   207
              (case f e1 of
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   208
                NONE =>
35012
c3e3ac3ca091 removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents: 33162
diff changeset
   209
                  (case get mid of
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   210
                    NONE =>
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   211
                      (case f e2 of
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   212
                        NONE => get right
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   213
                      | some => some)
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   214
                  | some => some)
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   215
              | some => some)
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   216
          | some => some);
35012
c3e3ac3ca091 removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents: 33162
diff changeset
   217
  in get end;
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   218
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   219
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   220
(* lookup *)
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   221
67557
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   222
fun lookup tab key =
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   223
  let
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   224
    fun look Empty = NONE
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   225
      | look (Leaf1 (k, x)) =
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   226
          if eq_key (key, k) then SOME x else NONE
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   227
      | look (Leaf2 ((k1, x1), (k2, x2))) =
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   228
          if eq_key (key, k1) then SOME x1 else
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   229
          if eq_key (key, k2) then SOME x2 else NONE
67557
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   230
      | look (Branch2 (left, (k, x), right)) =
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   231
          (case Key.ord (key, k) of
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   232
            LESS => look left
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   233
          | EQUAL => SOME x
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   234
          | GREATER => look right)
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   235
      | look (Branch3 (left, (k1, x1), mid, (k2, x2), right)) =
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   236
          (case Key.ord (key, k1) of
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   237
            LESS => look left
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   238
          | EQUAL => SOME x1
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   239
          | GREATER =>
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   240
              (case Key.ord (key, k2) of
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   241
                LESS => look mid
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   242
              | EQUAL => SOME x2
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   243
              | GREATER => look right));
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   244
  in look tab end;
a965ccf7414e performance fine-tuning of hot spot;
wenzelm
parents: 67537
diff changeset
   245
43792
d5803c3d537a Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
wenzelm
parents: 39020
diff changeset
   246
fun lookup_key tab key =
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   247
  let
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   248
    fun look Empty = NONE
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   249
      | look (Leaf1 (k, x)) =
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   250
          if eq_key (key, k) then SOME (k, x) else NONE
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   251
      | look (Leaf2 ((k1, x1), (k2, x2))) =
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   252
          if eq_key (key, k1) then SOME (k1, x1) else
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   253
          if eq_key (key, k2) then SOME (k2, x2) else NONE
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   254
      | look (Branch2 (left, (k, x), right)) =
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   255
          (case Key.ord (key, k) of
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   256
            LESS => look left
43792
d5803c3d537a Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
wenzelm
parents: 39020
diff changeset
   257
          | EQUAL => SOME (k, x)
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   258
          | GREATER => look right)
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   259
      | look (Branch3 (left, (k1, x1), mid, (k2, x2), right)) =
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   260
          (case Key.ord (key, k1) of
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   261
            LESS => look left
43792
d5803c3d537a Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
wenzelm
parents: 39020
diff changeset
   262
          | EQUAL => SOME (k1, x1)
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   263
          | GREATER =>
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   264
              (case Key.ord (key, k2) of
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   265
                LESS => look mid
43792
d5803c3d537a Table.lookup_key and Graph.get_entry allow to retrieve the original key, which is not necessarily identical to the given one;
wenzelm
parents: 39020
diff changeset
   266
              | EQUAL => SOME (k2, x2)
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   267
              | GREATER => look right));
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   268
  in look tab end;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   269
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   270
fun defined tab key =
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   271
  let
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   272
    fun def Empty = false
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   273
      | def (Leaf1 (k, _)) = eq_key (key, k)
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   274
      | def (Leaf2 ((k1, _), (k2, _))) = eq_key (key, k1) orelse eq_key (key, k2)
77735
be3f838b3e17 tuned --- fewer compiler warnings;
wenzelm
parents: 77733
diff changeset
   275
      | def (Branch2 (left, (k, _), right)) =
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   276
          (case Key.ord (key, k) of
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   277
            LESS => def left
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   278
          | EQUAL => true
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   279
          | GREATER => def right)
77735
be3f838b3e17 tuned --- fewer compiler warnings;
wenzelm
parents: 77733
diff changeset
   280
      | def (Branch3 (left, (k1, _), mid, (k2, _), right)) =
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   281
          (case Key.ord (key, k1) of
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   282
            LESS => def left
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   283
          | EQUAL => true
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   284
          | GREATER =>
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   285
              (case Key.ord (key, k2) of
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   286
                LESS => def mid
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   287
              | EQUAL => true
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   288
              | GREATER => def right));
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   289
  in def tab end;
16887
b24c067b32d6 added defined;
wenzelm
parents: 16864
diff changeset
   290
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   291
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   292
(* modify *)
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   293
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   294
datatype 'a growth =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   295
  Stay of 'a table |
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   296
  Sprout of 'a table * (key * 'a) * 'a table;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   297
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   298
exception SAME;
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   299
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   300
fun modify key f tab =
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   301
  let
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   302
    fun modfy Empty = Sprout (Empty, (key, f NONE), Empty)
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   303
      | modfy (t as Leaf1 _) = modfy (unmake t)
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   304
      | modfy (t as Leaf2 _) = modfy (unmake t)
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   305
      | modfy (Branch2 (left, p as (k, x), right)) =
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   306
          (case Key.ord (key, k) of
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   307
            LESS =>
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   308
              (case modfy left of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   309
                Stay left' => Stay (make2 (left', p, right))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   310
              | Sprout (left1, q, left2) => Stay (make3 (left1, q, left2, p, right)))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   311
          | EQUAL => Stay (make2 (left, (k, f (SOME x)), right))
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   312
          | GREATER =>
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   313
              (case modfy right of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   314
                Stay right' => Stay (make2 (left, p, right'))
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   315
              | Sprout (right1, q, right2) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   316
                  Stay (make3 (left, p, right1, q, right2))))
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   317
      | modfy (Branch3 (left, p1 as (k1, x1), mid, p2 as (k2, x2), right)) =
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   318
          (case Key.ord (key, k1) of
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   319
            LESS =>
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   320
              (case modfy left of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   321
                Stay left' => Stay (make3 (left', p1, mid, p2, right))
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   322
              | Sprout (left1, q, left2) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   323
                  Sprout (make2 (left1, q, left2), p1, make2 (mid, p2, right)))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   324
          | EQUAL => Stay (make3 (left, (k1, f (SOME x1)), mid, p2, right))
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   325
          | GREATER =>
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   326
              (case Key.ord (key, k2) of
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   327
                LESS =>
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   328
                  (case modfy mid of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   329
                    Stay mid' => Stay (make3 (left, p1, mid', p2, right))
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   330
                  | Sprout (mid1, q, mid2) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   331
                      Sprout (make2 (left, p1, mid1), q, make2 (mid2, p2, right)))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   332
              | EQUAL => Stay (make3 (left, p1, mid, (k2, f (SOME x2)), right))
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   333
              | GREATER =>
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   334
                  (case modfy right of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   335
                    Stay right' => Stay (make3 (left, p1, mid, p2, right'))
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   336
                  | Sprout (right1, q, right2) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   337
                      Sprout (make2 (left, p1, mid), p2, make2 (right1, q, right2)))));
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   338
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   339
  in
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   340
    (case modfy tab of
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   341
      Stay tab' => tab'
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   342
    | Sprout br => make2 br)
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   343
    handle SAME => tab
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   344
  end;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   345
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17222
diff changeset
   346
fun update (key, x) tab = modify key (fn _ => x) tab;
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17222
diff changeset
   347
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
   348
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
   349
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
   350
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
   351
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   352
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   353
(* delete *)
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   354
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   355
exception UNDEF of key;
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   356
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   357
local
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   358
77735
be3f838b3e17 tuned --- fewer compiler warnings;
wenzelm
parents: 77733
diff changeset
   359
fun compare NONE _ = LESS
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   360
  | compare (SOME k1) (k2, _) = Key.ord (k1, k2);
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   361
77735
be3f838b3e17 tuned --- fewer compiler warnings;
wenzelm
parents: 77733
diff changeset
   362
fun if_eq ord x y = if ord = EQUAL then x else y;
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   363
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   364
fun del (SOME k) Empty = raise UNDEF k
77735
be3f838b3e17 tuned --- fewer compiler warnings;
wenzelm
parents: 77733
diff changeset
   365
  | del NONE Empty = raise Match
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   366
  | del NONE (Leaf1 p) = (p, (true, Empty))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   367
  | del NONE (Leaf2 (p, q)) = (p, (false, Leaf1 q))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   368
  | del k (Leaf1 p) =
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   369
      (case compare k p of
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   370
        EQUAL => (p, (true, Empty))
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   371
      | _ => raise UNDEF (the k))
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   372
  | del k (Leaf2 (p, q)) =
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   373
      (case compare k p of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   374
        EQUAL => (p, (false, Leaf1 q))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   375
      | _ =>
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   376
        (case compare k q of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   377
          EQUAL => (q, (false, Leaf1 p))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   378
        | _ => raise UNDEF (the k)))
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   379
  | del k (Branch2 (l, p, r)) =
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   380
      (case compare k p of
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   381
        LESS =>
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   382
          (case del k l of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   383
            (p', (false, l')) => (p', (false, make2 (l', p, r)))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   384
          | (p', (true, l')) => (p', case unmake r of
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   385
              Branch2 (rl, rp, rr) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   386
                (true, make3 (l', p, rl, rp, rr))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   387
            | Branch3 (rl, rp, rm, rq, rr) => (false, make2
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   388
                (make2 (l', p, rl), rp, make2 (rm, rq, rr)))))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   389
      | ord =>
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   390
          (case del (if_eq ord NONE k) r of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   391
            (p', (false, r')) => (p', (false, make2 (l, if_eq ord p' p, r')))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   392
          | (p', (true, r')) => (p', case unmake l of
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   393
              Branch2 (ll, lp, lr) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   394
                (true, make3 (ll, lp, lr, if_eq ord p' p, r'))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   395
            | Branch3 (ll, lp, lm, lq, lr) => (false, make2
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   396
                (make2 (ll, lp, lm), lq, make2 (lr, if_eq ord p' p, r'))))))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   397
  | del k (Branch3 (l, p, m, q, r)) =
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   398
      (case compare k q of
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   399
        LESS =>
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   400
          (case compare k p of
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   401
            LESS =>
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   402
              (case del k l of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   403
                (p', (false, l')) => (p', (false, make3 (l', p, m, q, r)))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   404
              | (p', (true, l')) => (p', (false, case (unmake m, unmake r) of
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   405
                  (Branch2 (ml, mp, mr), Branch2 _) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   406
                    make2 (make3 (l', p, ml, mp, mr), q, r)
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   407
                | (Branch3 (ml, mp, mm, mq, mr), _) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   408
                    make3 (make2 (l', p, ml), mp, make2 (mm, mq, mr), q, r)
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   409
                | (Branch2 (ml, mp, mr), Branch3 (rl, rp, rm, rq, rr)) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   410
                    make3 (make2 (l', p, ml), mp, make2 (mr, q, rl), rp,
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   411
                      make2 (rm, rq, rr)))))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   412
          | ord =>
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   413
              (case del (if_eq ord NONE k) m of
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   414
                (p', (false, m')) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   415
                  (p', (false, make3 (l, if_eq ord p' p, m', q, r)))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   416
              | (p', (true, m')) => (p', (false, case (unmake l, unmake r) of
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   417
                  (Branch2 (ll, lp, lr), Branch2 _) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   418
                    make2 (make3 (ll, lp, lr, if_eq ord p' p, m'), q, r)
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   419
                | (Branch3 (ll, lp, lm, lq, lr), _) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   420
                    make3 (make2 (ll, lp, lm), lq,
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   421
                      make2 (lr, if_eq ord p' p, m'), q, r)
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   422
                | (_, Branch3 (rl, rp, rm, rq, rr)) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   423
                    make3 (l, if_eq ord p' p, make2 (m', q, rl), rp,
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   424
                      make2 (rm, rq, rr))))))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   425
      | ord =>
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   426
          (case del (if_eq ord NONE k) r of
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   427
            (q', (false, r')) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   428
              (q', (false, make3 (l, p, m, if_eq ord q' q, r')))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   429
          | (q', (true, r')) => (q', (false, case (unmake l, unmake m) of
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   430
              (Branch2 _, Branch2 (ml, mp, mr)) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   431
                make2 (l, p, make3 (ml, mp, mr, if_eq ord q' q, r'))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   432
            | (_, Branch3 (ml, mp, mm, mq, mr)) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   433
                make3 (l, p, make2 (ml, mp, mm), mq,
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   434
                  make2 (mr, if_eq ord q' q, r'))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   435
            | (Branch3 (ll, lp, lm, lq, lr), Branch2 (ml, mp, mr)) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   436
                make3 (make2 (ll, lp, lm), lq, make2 (lr, p, ml), mp,
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   437
                  make2 (mr, if_eq ord q' q, r'))))));
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   438
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   439
in
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   440
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   441
fun delete key tab = snd (snd (del (SOME key) tab));
44336
59ff5a93eef4 tuned Table.delete_safe: avoid potentially expensive attempt of delete;
wenzelm
parents: 43792
diff changeset
   442
fun delete_safe key tab = if defined tab key then delete key tab else tab;
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   443
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   444
end;
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   445
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   446
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   447
(* membership operations *)
16466
82e2fc13ce54 added member;
wenzelm
parents: 16446
diff changeset
   448
82e2fc13ce54 added member;
wenzelm
parents: 16446
diff changeset
   449
fun member eq tab (key, x) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17222
diff changeset
   450
  (case lookup tab key of
16466
82e2fc13ce54 added member;
wenzelm
parents: 16446
diff changeset
   451
    NONE => false
82e2fc13ce54 added member;
wenzelm
parents: 16446
diff changeset
   452
  | SOME y => eq (x, y));
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   453
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   454
fun insert eq (key, x) =
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   455
  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
   456
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   457
fun remove eq (key, x) tab =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17222
diff changeset
   458
  (case lookup tab key of
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   459
    NONE => tab
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   460
  | 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
   461
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   462
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   463
(* simultaneous modifications *)
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   464
74266
612b7e0d6721 clarified signature;
wenzelm
parents: 74232
diff changeset
   465
fun make entries = build (fold update_new entries);
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   466
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   467
fun join f (table1, table2) =
55727
7e330ae052bb optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents: 52049
diff changeset
   468
  let
7e330ae052bb optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents: 52049
diff changeset
   469
    fun add (key, y) tab = modify key (fn NONE => y | SOME x => f key (x, y)) tab;
7e330ae052bb optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents: 52049
diff changeset
   470
  in
7e330ae052bb optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents: 52049
diff changeset
   471
    if pointer_eq (table1, table2) then table1
7e330ae052bb optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents: 52049
diff changeset
   472
    else if is_empty table1 then table2
7e330ae052bb optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents: 52049
diff changeset
   473
    else fold_table add table2 table1
7e330ae052bb optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents: 52049
diff changeset
   474
  end;
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   475
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   476
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
   477
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   478
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   479
(* list tables *)
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   480
18946
ce65d2d2e0c2 renamed xxx_multi to xxx_list;
wenzelm
parents: 18007
diff changeset
   481
fun lookup_list tab key = these (lookup tab key);
25391
783e5de2a6c7 renamed update_list to cons_list;
wenzelm
parents: 23655
diff changeset
   482
783e5de2a6c7 renamed update_list to cons_list;
wenzelm
parents: 23655
diff changeset
   483
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
   484
20124
caf3a129b90d tuned insert_list;
wenzelm
parents: 19506
diff changeset
   485
fun insert_list eq (key, x) =
caf3a129b90d tuned insert_list;
wenzelm
parents: 19506
diff changeset
   486
  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
   487
18946
ce65d2d2e0c2 renamed xxx_multi to xxx_list;
wenzelm
parents: 18007
diff changeset
   488
fun remove_list eq (key, x) tab =
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   489
  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
   490
  handle UNDEF _ => delete key tab;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   491
25391
783e5de2a6c7 renamed update_list to cons_list;
wenzelm
parents: 23655
diff changeset
   492
fun update_list eq (key, x) =
783e5de2a6c7 renamed update_list to cons_list;
wenzelm
parents: 23655
diff changeset
   493
  modify key (fn NONE => [x] | SOME [] => [x] | SOME (xs as y :: _) =>
783e5de2a6c7 renamed update_list to cons_list;
wenzelm
parents: 23655
diff changeset
   494
    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
   495
74266
612b7e0d6721 clarified signature;
wenzelm
parents: 74232
diff changeset
   496
fun make_list args = build (fold_rev cons_list args);
19482
9f11af8f7ef9 tuned basic list operators (flat, maps, map_filter);
wenzelm
parents: 19073
diff changeset
   497
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
   498
fun merge_list eq = join (fn _ => Library.merge eq);
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   499
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   500
74227
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   501
(* set operations *)
50234
c97c5c34fb1d convenience operations for table as set;
wenzelm
parents: 47980
diff changeset
   502
c97c5c34fb1d convenience operations for table as set;
wenzelm
parents: 47980
diff changeset
   503
type set = unit table;
c97c5c34fb1d convenience operations for table as set;
wenzelm
parents: 47980
diff changeset
   504
67529
37db2dc5c022 clarified operations;
wenzelm
parents: 66935
diff changeset
   505
fun insert_set x = default (x, ());
37db2dc5c022 clarified operations;
wenzelm
parents: 66935
diff changeset
   506
fun remove_set x : set -> set = delete_safe x;
74266
612b7e0d6721 clarified signature;
wenzelm
parents: 74232
diff changeset
   507
fun make_set xs = build (fold insert_set xs);
50234
c97c5c34fb1d convenience operations for table as set;
wenzelm
parents: 47980
diff changeset
   508
c97c5c34fb1d convenience operations for table as set;
wenzelm
parents: 47980
diff changeset
   509
47980
c81801f881b3 simplified Poly/ML setup -- 5.3.0 is now the common base-line;
wenzelm
parents: 44336
diff changeset
   510
(* ML pretty-printing *)
38635
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 35012
diff changeset
   511
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 35012
diff changeset
   512
val _ =
62819
d3ff367a16a0 careful export of type-dependent functions, without losing their special status;
wenzelm
parents: 62503
diff changeset
   513
  ML_system_pp (fn depth => fn pretty => fn tab =>
62503
19afb533028e clarified modules;
wenzelm
parents: 59012
diff changeset
   514
    ML_Pretty.to_polyml
38635
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 35012
diff changeset
   515
      (ML_Pretty.enum "," "{" "}"
62503
19afb533028e clarified modules;
wenzelm
parents: 59012
diff changeset
   516
        (ML_Pretty.pair
62819
d3ff367a16a0 careful export of type-dependent functions, without losing their special status;
wenzelm
parents: 62503
diff changeset
   517
          (ML_Pretty.from_polyml o ML_system_pretty)
62503
19afb533028e clarified modules;
wenzelm
parents: 59012
diff changeset
   518
          (ML_Pretty.from_polyml o pretty))
38635
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 35012
diff changeset
   519
        (dest tab, depth)));
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 35012
diff changeset
   520
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 35012
diff changeset
   521
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   522
(*final declarations of this structure!*)
39020
ac0f24f850c9 replaced Table.map' by Table.map
haftmann
parents: 38635
diff changeset
   523
val map = map_table;
16446
0ab34b9d1b1f added map', fold;
wenzelm
parents: 16342
diff changeset
   524
val fold = fold_table;
28334
7c693bb76366 added fold_rev;
wenzelm
parents: 27783
diff changeset
   525
val fold_rev = fold_rev_table;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   526
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   527
end;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   528
31971
8c1b845ed105 renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents: 31621
diff changeset
   529
structure Inttab = Table(type key = int val ord = int_ord);
8c1b845ed105 renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents: 31621
diff changeset
   530
structure Symtab = Table(type key = string val ord = fast_string_ord);
8c1b845ed105 renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents: 31621
diff changeset
   531
structure Symreltab = Table(type key = string * string
30647
ef8f46c3158a added Symreltab (binary relations of symbols) instance of TableFun
haftmann
parents: 30290
diff changeset
   532
  val ord = prod_ord fast_string_ord fast_string_ord);