src/Pure/General/table.ML
author wenzelm
Wed, 11 Sep 2024 22:28:42 +0200
changeset 80863 af34fcf7215d
parent 80809 4a64fc4d1cde
child 81172 7c01a86def85
permissions -rw-r--r--
tuned signature: more operations;
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
77780
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
    22
  val size: 'a table -> int
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    23
  val empty: 'a table
74232
1091880266e5 clarified signature;
wenzelm
parents: 74227
diff changeset
    24
  val build: ('a table -> 'a table) -> 'a table
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    25
  val is_empty: '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
79249
718798653cf1 more operations;
wenzelm
parents: 79163
diff changeset
    44
  val make_distinct: (key * 'a) list -> 'a table
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 21065
diff changeset
    45
  val make: (key * 'a) list -> 'a table                                (*exception DUP*)
56051
c3681b9e060f tuned comment;
wenzelm
parents: 55727
diff changeset
    46
  val join: (key -> 'a * 'a -> 'a) (*exception SAME*) ->
23655
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 21065
diff changeset
    47
    'a table * 'a table -> 'a table                                    (*exception DUP*)
d2d1138e0ddc replaced exception TableFun/GraphFun.DUPS by TableFun/GraphFun.DUP;
wenzelm
parents: 21065
diff changeset
    48
  val merge: ('a * 'a -> bool) -> 'a table * 'a table -> 'a table      (*exception DUP*)
77822
353c4d3e6dda more operations;
wenzelm
parents: 77814
diff changeset
    49
  val joins: (key -> 'a * 'a -> 'a) (*exception SAME*) ->
353c4d3e6dda more operations;
wenzelm
parents: 77814
diff changeset
    50
    'a table list -> 'a table                                          (*exception DUP*)
353c4d3e6dda more operations;
wenzelm
parents: 77814
diff changeset
    51
  val merges: ('a * 'a -> bool) -> 'a table list -> 'a table           (*exception DUP*)
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
    52
  val delete: key -> 'a table -> 'a table                              (*exception UNDEF*)
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
    53
  val delete_safe: key -> 'a table -> 'a table
16466
82e2fc13ce54 added member;
wenzelm
parents: 16446
diff changeset
    54
  val member: ('b * 'a -> bool) -> 'a table -> key * 'b -> bool
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
    55
  val insert: ('a * 'a -> bool) -> key * 'a -> 'a table -> 'a table    (*exception DUP*)
16139
21ce46ca61cc remove(_multi): generalized type;
wenzelm
parents: 16002
diff changeset
    56
  val remove: ('b * 'a -> bool) -> key * 'b -> 'a table -> 'a table
18946
ce65d2d2e0c2 renamed xxx_multi to xxx_list;
wenzelm
parents: 18007
diff changeset
    57
  val lookup_list: 'a list table -> key -> 'a list
31209
77da3aad5917 dropped parentheses
haftmann
parents: 30647
diff changeset
    58
  val cons_list: key * 'a -> 'a list table -> 'a list table
19506
980a2edf9e82 added insert_list;
wenzelm
parents: 19482
diff changeset
    59
  val insert_list: ('a * 'a -> bool) -> key * 'a -> 'a list table -> 'a list table
77981
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
    60
  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
    61
  val remove_list: ('b * 'a -> bool) -> key * 'b -> 'a list table -> 'a list table
ce65d2d2e0c2 renamed xxx_multi to xxx_list;
wenzelm
parents: 18007
diff changeset
    62
  val make_list: (key * 'a) list -> 'a list table
ce65d2d2e0c2 renamed xxx_multi to xxx_list;
wenzelm
parents: 18007
diff changeset
    63
  val dest_list: 'a list table -> (key * 'a) list
33162
d11b89e7afd9 merge_list: no exception DUP here;
wenzelm
parents: 31971
diff changeset
    64
  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
    65
  type set = unit table
63511
1c2c045decb3 more operations;
wenzelm
parents: 62819
diff changeset
    66
  val insert_set: key -> set -> set
67529
37db2dc5c022 clarified operations;
wenzelm
parents: 66935
diff changeset
    67
  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
    68
  val make_set: key list -> set
80583
9a40ec7a2027 more operations;
wenzelm
parents: 80579
diff changeset
    69
  type 'a cache_ops = {apply: key -> 'a, reset: unit -> unit, size: unit -> int}
80579
69cf3c308d6c clarified signature: more operations;
wenzelm
parents: 80240
diff changeset
    70
  val unsynchronized_cache: (key -> 'a) -> 'a cache_ops
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    71
end;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    72
31971
8c1b845ed105 renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents: 31621
diff changeset
    73
functor Table(Key: KEY): TABLE =
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    74
struct
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    75
77733
wenzelm
parents: 77732
diff changeset
    76
(* keys *)
wenzelm
parents: 77732
diff changeset
    77
77731
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77727
diff changeset
    78
structure Key = Key;
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77727
diff changeset
    79
type key = Key.key;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    80
77733
wenzelm
parents: 77732
diff changeset
    81
exception DUP of key;
wenzelm
parents: 77732
diff changeset
    82
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    83
77731
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77727
diff changeset
    84
(* datatype *)
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    85
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    86
datatype 'a table =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    87
  Empty |
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    88
  Leaf1 of key * 'a |
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
    89
  Leaf2 of (key * 'a) * (key * 'a) |
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
    90
  Leaf3 of (key * 'a) * (key * 'a) * (key * 'a) |
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    91
  Branch2 of 'a table * (key * 'a) * 'a table |
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
    92
  Branch3 of 'a table * (key * 'a) * 'a table * (key * 'a) * 'a table |
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
    93
  Size of int * 'a table;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
    94
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    95
fun make2 (Empty, e, Empty) = Leaf1 e
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    96
  | make2 (Branch2 (Empty, e1, Empty), e2, right) = make2 (Leaf1 e1, e2, right)
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    97
  | make2 (left, e1, Branch2 (Empty, e2, Empty)) = make2 (left, e1, Leaf1 e2)
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
    98
  | make2 (Branch3 (Empty, e1, Empty, e2, Empty), e3, right) = make2 (Leaf2 (e1, e2), e3, right)
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
    99
  | make2 (left, e1, Branch3 (Empty, e2, Empty, e3, Empty)) = make2 (left, e1, Leaf2 (e2, e3))
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   100
  | make2 (Leaf1 e1, e2, Empty) = Leaf2 (e1, e2)
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   101
  | make2 (Empty, e1, Leaf1 e2) = Leaf2 (e1, e2)
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   102
  | make2 (Leaf1 e1, e2, Leaf1 e3) = Leaf3 (e1, e2, e3)
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   103
  | make2 (Leaf2 (e1, e2), e3, Empty) = Leaf3 (e1, e2, e3)
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   104
  | make2 (Empty, e1, Leaf2 (e2, e3)) = Leaf3 (e1, e2, e3)
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   105
  | 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
   106
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   107
fun make3 (Empty, e1, Empty, e2, Empty) = Leaf2 (e1, e2)
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   108
  | make3 (Branch2 (Empty, e1, Empty), e2, mid, e3, right) = make3 (Leaf1 e1, e2, mid, e3, right)
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   109
  | make3 (left, e1, Branch2 (Empty, e2, Empty), e3, right) = make3 (left, e1, Leaf1 e2, e3, right)
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   110
  | make3 (left, e1, mid, e2, Branch2 (Empty, e3, Empty)) = make3 (left, e1, mid, e2, Leaf1 e3)
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   111
  | make3 (Leaf1 e1, e2, Empty, e3, Empty) = Leaf3 (e1, e2, e3)
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   112
  | make3 (Empty, e1, Leaf1 e2, e3, Empty) = Leaf3 (e1, e2, e3)
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   113
  | make3 (Empty, e1, Empty, e2, Leaf1 e3) = Leaf3 (e1, e2, e3)
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   114
  | 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
   115
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   116
fun unmake (Leaf1 e) = Branch2 (Empty, e, Empty)
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   117
  | unmake (Leaf2 (e1, e2)) = Branch3 (Empty, e1, Empty, e2, Empty)
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   118
  | unmake (Leaf3 (e1, e2, e3)) = Branch2 (Branch2 (Empty, e1, Empty), e2, Branch2 (Empty, e3, Empty))
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   119
  | unmake (Size (_, arg)) = arg
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   120
  | unmake arg = arg;
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   121
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   122
77780
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
   123
(* size *)
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
   124
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   125
(*literal copy from set.ML*)
77780
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
   126
local
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
   127
  fun count Empty n = n
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
   128
    | count (Leaf1 _) n = n + 1
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
   129
    | count (Leaf2 _) n = n + 2
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
   130
    | count (Leaf3 _) n = n + 3
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
   131
    | count (Branch2 (left, _, right)) n = count right (count left (n + 1))
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   132
    | count (Branch3 (left, _, mid, _, right)) n = count right (count mid (count left (n + 2)))
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   133
    | count (Size (m, _)) n = m + n;
77802
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   134
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   135
  fun box (Branch2 _) = 1
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   136
    | box (Branch3 _) = 1
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   137
    | box _ = 0;
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   138
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   139
  fun bound arg b =
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   140
    if b > 0 then
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   141
      (case arg of
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   142
        Branch2 (left, _, right) =>
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   143
          bound right (bound left (b - box left - box right))
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   144
      | Branch3 (left, _, mid, _, right) =>
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   145
          bound right (bound mid (bound left (b - box left - box mid - box right)))
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   146
      | _ => b)
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   147
    else b;
77780
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
   148
in
77802
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   149
  fun size arg = count arg 0;
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   150
  fun make_size m arg = if bound arg 3 <= 0 then Size (m, arg) else arg;
77780
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
   151
end;
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
   152
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
   153
77743
33bee7a96f72 tuned comments (amending 1951f6470792);
wenzelm
parents: 77742
diff changeset
   154
(* empty *)
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   155
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   156
val empty = Empty;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   157
74232
1091880266e5 clarified signature;
wenzelm
parents: 74227
diff changeset
   158
fun build (f: 'a table -> 'a table) = f empty;
1091880266e5 clarified signature;
wenzelm
parents: 74227
diff changeset
   159
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   160
(*literal copy from set.ML*)
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   161
fun is_empty Empty = true
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   162
  | is_empty (Size (_, arg)) = is_empty arg
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   163
  | is_empty _ = false;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   164
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   165
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   166
(* map and fold combinators *)
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   167
16657
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   168
fun map_table f =
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   169
  let
79094
wenzelm
parents: 79093
diff changeset
   170
    fun apply (k, x) = (k, f k x);
wenzelm
parents: 79093
diff changeset
   171
16657
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   172
    fun map Empty = Empty
79094
wenzelm
parents: 79093
diff changeset
   173
      | map (Leaf1 e) = Leaf1 (apply e)
wenzelm
parents: 79093
diff changeset
   174
      | map (Leaf2 (e1, e2)) = Leaf2 (apply e1, apply e2)
wenzelm
parents: 79093
diff changeset
   175
      | map (Leaf3 (e1, e2, e3)) = Leaf3 (apply e1, apply e2, apply e3)
wenzelm
parents: 79093
diff changeset
   176
      | map (Branch2 (left, e, right)) =
wenzelm
parents: 79093
diff changeset
   177
          Branch2 (map left, apply e, map right)
wenzelm
parents: 79093
diff changeset
   178
      | map (Branch3 (left, e1, mid, e2, right)) =
wenzelm
parents: 79093
diff changeset
   179
          Branch3 (map left, apply e1, map mid, apply e2, map right)
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   180
      | map (Size (m, arg)) = Size (m, map arg);
16657
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   181
  in map end;
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   182
16657
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   183
fun fold_table f =
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   184
  let
77813
wenzelm
parents: 77805
diff changeset
   185
    fun fold Empty a = a
wenzelm
parents: 77805
diff changeset
   186
      | fold (Leaf1 e) a = f e a
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   187
      | fold (Leaf2 (e1, e2)) a = f e2 (f e1 a)
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   188
      | fold (Leaf3 (e1, e2, e3)) a = f e3 (f e2 (f e1 a))
77813
wenzelm
parents: 77805
diff changeset
   189
      | fold (Branch2 (left, e, right)) a =
wenzelm
parents: 77805
diff changeset
   190
          fold right (f e (fold left a))
wenzelm
parents: 77805
diff changeset
   191
      | fold (Branch3 (left, e1, mid, e2, right)) a =
wenzelm
parents: 77805
diff changeset
   192
          fold right (f e2 (fold mid (f e1 (fold left a))))
wenzelm
parents: 77805
diff changeset
   193
      | fold (Size (_, arg)) a = fold arg a;
16657
a6f65f47eda1 low-level tuning of map, fold;
wenzelm
parents: 16466
diff changeset
   194
  in fold end;
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   195
28334
7c693bb76366 added fold_rev;
wenzelm
parents: 27783
diff changeset
   196
fun fold_rev_table f =
7c693bb76366 added fold_rev;
wenzelm
parents: 27783
diff changeset
   197
  let
77813
wenzelm
parents: 77805
diff changeset
   198
    fun fold_rev Empty a = a
wenzelm
parents: 77805
diff changeset
   199
      | fold_rev (Leaf1 e) a = f e a
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   200
      | fold_rev (Leaf2 (e1, e2)) a = f e1 (f e2 a)
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   201
      | fold_rev (Leaf3 (e1, e2, e3)) a = f e1 (f e2 (f e3 a))
77813
wenzelm
parents: 77805
diff changeset
   202
      | fold_rev (Branch2 (left, e, right)) a =
wenzelm
parents: 77805
diff changeset
   203
          fold_rev left (f e (fold_rev right a))
wenzelm
parents: 77805
diff changeset
   204
      | fold_rev (Branch3 (left, e1, mid, e2, right)) a =
wenzelm
parents: 77805
diff changeset
   205
          fold_rev left (f e1 (fold_rev mid (f e2 (fold_rev right a))))
wenzelm
parents: 77805
diff changeset
   206
      | fold_rev (Size (_, arg)) a = fold_rev arg a;
77732
wenzelm
parents: 77731
diff changeset
   207
  in fold_rev end;
28334
7c693bb76366 added fold_rev;
wenzelm
parents: 27783
diff changeset
   208
77768
65008644d394 tuned: prefer "build" combinator;
wenzelm
parents: 77743
diff changeset
   209
fun dest tab = Library.build (fold_rev_table cons tab);
65008644d394 tuned: prefer "build" combinator;
wenzelm
parents: 77743
diff changeset
   210
fun keys tab = Library.build (fold_rev_table (cons o #1) tab);
16192
733267a60e32 exists: made non-strict;
wenzelm
parents: 16139
diff changeset
   211
27508
abdab08677d8 added get_first;
wenzelm
parents: 25391
diff changeset
   212
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
   213
(* min/max entries *)
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   214
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
   215
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
   216
  | min (Leaf1 e) = SOME e
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   217
  | min (Leaf2 (e, _)) = SOME e
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   218
  | min (Leaf3 (e, _, _)) = SOME e
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   219
  | 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
   220
  | min (Branch3 (Empty, e, _, _, _)) = SOME e
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
   221
  | min (Branch2 (left, _, _)) = min left
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   222
  | min (Branch3 (left, _, _, _, _)) = min left
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   223
  | min (Size (_, arg)) = min arg;
8409
ef01ee11b14e Added function min_key.
berghofe
parents: 7061
diff changeset
   224
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
   225
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
   226
  | max (Leaf1 e) = SOME e
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   227
  | max (Leaf2 (_, e)) = SOME e
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   228
  | max (Leaf3 (_, _, e)) = SOME e
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   229
  | 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
   230
  | max (Branch3 (_, _, _, e, Empty)) = SOME e
52049
156e12d5cb92 tuned signature;
wenzelm
parents: 50234
diff changeset
   231
  | max (Branch2 (_, _, right)) = max right
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   232
  | max (Branch3 (_, _, _, _, right)) = max right
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   233
  | max (Size (_, arg)) = max arg;
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   234
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   235
74227
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   236
(* exists and forall *)
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   237
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   238
fun exists pred =
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   239
  let
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   240
    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
   241
      | ex (Leaf1 e) = pred e
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   242
      | ex (Leaf2 (e1, e2)) = pred e1 orelse pred e2
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   243
      | ex (Leaf3 (e1, e2, e3)) = pred e1 orelse pred e2 orelse pred e3
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   244
      | ex (Branch2 (left, e, right)) =
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   245
          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
   246
      | ex (Branch3 (left, e1, mid, e2, right)) =
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   247
          ex left orelse pred e1 orelse ex mid orelse pred e2 orelse ex right
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   248
      | ex (Size (_, arg)) = ex arg;
74227
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   249
  in ex end;
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   250
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   251
fun forall pred = not o exists (not o pred);
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   252
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   253
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   254
(* get_first *)
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   255
35012
c3e3ac3ca091 removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents: 33162
diff changeset
   256
fun get_first f =
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   257
  let
35012
c3e3ac3ca091 removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents: 33162
diff changeset
   258
    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
   259
      | get (Leaf1 e) = f e
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   260
      | get (Leaf2 (e1, e2)) =
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   261
          (case f e1 of
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   262
            NONE => f e2
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   263
          | some => some)
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   264
      | get (Leaf3 (e1, e2, e3)) =
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   265
          (case f e1 of
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   266
            NONE =>
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   267
              (case f e2 of
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   268
                NONE => f e3
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   269
              | some => some)
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   270
          | some => some)
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   271
      | get (Branch2 (left, e, right)) =
35012
c3e3ac3ca091 removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents: 33162
diff changeset
   272
          (case get left of
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   273
            NONE =>
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   274
              (case f e of
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   275
                NONE => get right
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   276
              | some => some)
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   277
          | some => some)
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   278
      | get (Branch3 (left, e1, mid, e2, right)) =
35012
c3e3ac3ca091 removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents: 33162
diff changeset
   279
          (case get left of
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   280
            NONE =>
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   281
              (case f e1 of
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   282
                NONE =>
35012
c3e3ac3ca091 removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents: 33162
diff changeset
   283
                  (case get mid of
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   284
                    NONE =>
77727
b98edf66ca96 tuned names: "e" means "entry" in table.ML and "elem" in set.ML;
wenzelm
parents: 77721
diff changeset
   285
                      (case f e2 of
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   286
                        NONE => get right
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   287
                      | some => some)
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   288
                  | some => some)
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   289
              | some => some)
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   290
          | some => some)
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   291
      | get (Size (_, arg)) = get arg;
35012
c3e3ac3ca091 removed unused "boundary" of Table/Graph.get_first;
wenzelm
parents: 33162
diff changeset
   292
  in get end;
31616
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   293
63893e3a50a6 native get_first, with optional boundary;
wenzelm
parents: 31209
diff changeset
   294
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   295
(* retrieve *)
77742
wenzelm
parents: 77741
diff changeset
   296
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   297
fun retrieve {result, no_result} tab (key: key) =
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   298
  let
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   299
    fun compare (k, _) = Key.ord (key, k)
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   300
    fun result_equal e = if is_equal (compare e) then result e else no_result;
77742
wenzelm
parents: 77741
diff changeset
   301
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   302
    fun find Empty = no_result
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   303
      | find (Leaf1 e) = result_equal e
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   304
      | find (Leaf2 (e1, e2)) =
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   305
          (case compare e1 of
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   306
            LESS => no_result
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   307
          | EQUAL => result e1
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   308
          | GREATER => result_equal e2)
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   309
      | find (Leaf3 (e1, e2, e3)) =
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   310
          (case compare e2 of
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   311
            LESS => result_equal e1
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   312
          | EQUAL => result e2
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   313
          | GREATER => result_equal e3)
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   314
      | find (Branch2 (left, e, right)) =
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   315
          (case compare e of
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   316
            LESS => find left
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   317
          | EQUAL => result e
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   318
          | GREATER => find right)
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   319
      | find (Branch3 (left, e1, mid, e2, right)) =
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   320
          (case compare e1 of
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   321
            LESS => find left
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   322
          | EQUAL => result e1
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   323
          | GREATER =>
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   324
              (case compare e2 of
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   325
                LESS => find mid
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   326
              | EQUAL => result e2
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   327
              | GREATER => find right))
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   328
      | find (Size (_, arg)) = find arg;
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   329
  in find tab end;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   330
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   331
fun lookup tab key = retrieve {result = SOME o #2, no_result = NONE} tab key;
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   332
fun lookup_key tab key = retrieve {result = SOME, no_result = NONE} tab key;
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   333
fun defined tab key = retrieve {result = K true, no_result = false} tab key;
16887
b24c067b32d6 added defined;
wenzelm
parents: 16864
diff changeset
   334
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   335
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   336
(* modify *)
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   337
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   338
datatype 'a growth =
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   339
  Stay of 'a table |
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   340
  Sprout of 'a table * (key * 'a) * 'a table;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   341
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   342
exception SAME;
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   343
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   344
fun modify key f tab =
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   345
  let
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   346
    fun compare (k, _) = Key.ord (key, k)
77742
wenzelm
parents: 77741
diff changeset
   347
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   348
    val inc = Unsynchronized.ref 0;
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   349
    fun insert (k: key) = (k, f NONE) before ignore (Unsynchronized.inc inc);
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   350
    fun update (k: key, x) = (k, f (SOME x));
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   351
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   352
    fun modfy Empty = Sprout (Empty, (insert key), Empty)
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   353
      | 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
   354
      | modfy (t as Leaf2 _) = modfy (unmake t)
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   355
      | modfy (t as Leaf3 _) = modfy (unmake t)
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   356
      | modfy (Branch2 (left, e, right)) =
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   357
          (case compare e of
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   358
            LESS =>
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   359
              (case modfy left of
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   360
                Stay left' => Stay (make2 (left', e, right))
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   361
              | Sprout (left1, e', left2) => Stay (make3 (left1, e', left2, e, right)))
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   362
          | EQUAL => Stay (make2 (left, update e, right))
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   363
          | GREATER =>
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   364
              (case modfy right of
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   365
                Stay right' => Stay (make2 (left, e, right'))
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   366
              | Sprout (right1, e', right2) =>
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   367
                  Stay (make3 (left, e, right1, e', right2))))
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   368
      | modfy (Branch3 (left, e1, mid, e2, right)) =
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   369
          (case compare e1 of
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   370
            LESS =>
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   371
              (case modfy left of
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   372
                Stay left' => Stay (make3 (left', e1, mid, e2, right))
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   373
              | Sprout (left1, e', left2) =>
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   374
                  Sprout (make2 (left1, e', left2), e1, make2 (mid, e2, right)))
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   375
          | EQUAL => Stay (make3 (left, update e1, mid, e2, right))
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   376
          | GREATER =>
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   377
              (case compare e2 of
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   378
                LESS =>
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   379
                  (case modfy mid of
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   380
                    Stay mid' => Stay (make3 (left, e1, mid', e2, right))
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   381
                  | Sprout (mid1, e', mid2) =>
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   382
                      Sprout (make2 (left, e1, mid1), e', make2 (mid2, e2, right)))
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   383
              | EQUAL => Stay (make3 (left, e1, mid, update e2, right))
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   384
              | GREATER =>
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   385
                  (case modfy right of
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   386
                    Stay right' => Stay (make3 (left, e1, mid, e2, right'))
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   387
                  | Sprout (right1, q, right2) =>
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   388
                      Sprout (make2 (left, e1, mid), e2, make2 (right1, q, right2)))))
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   389
      | modfy (Size (_, arg)) = modfy arg;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   390
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   391
    val tab' =
77882
bb7238e7d2d9 minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents: 77881
diff changeset
   392
      (case tab of
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   393
        Empty => Leaf1 (insert key)
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   394
      | Leaf1 e =>
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   395
          (case compare e of
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   396
            LESS => Leaf2 (insert key, e)
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   397
          | EQUAL => Leaf1 (update e)
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   398
          | GREATER => Leaf2 (e, insert key))
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   399
      | Leaf2 (e1, e2) =>
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   400
          (case compare e1 of
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   401
            LESS => Leaf3 (insert key, e1, e2)
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   402
          | EQUAL => Leaf2 (update e1, e2)
77882
bb7238e7d2d9 minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents: 77881
diff changeset
   403
          | GREATER =>
79093
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   404
              (case compare e2 of
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   405
                LESS => Leaf3 (e1, insert key, e2)
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   406
              | EQUAL => Leaf2 (e1, update e2)
6c5ca8f04d60 misc tuning and clarification;
wenzelm
parents: 79092
diff changeset
   407
              | GREATER => Leaf3 (e1, e2, insert key)))
77882
bb7238e7d2d9 minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents: 77881
diff changeset
   408
      | _ =>
bb7238e7d2d9 minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents: 77881
diff changeset
   409
          (case modfy tab of
bb7238e7d2d9 minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents: 77881
diff changeset
   410
            Stay tab' => tab'
bb7238e7d2d9 minor performance tuning: avoid excessive (de)constructions for base cases;
wenzelm
parents: 77881
diff changeset
   411
          | Sprout br => make2 br));
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   412
  in
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   413
    make_size (size tab + !inc) tab'
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   414
  end handle SAME => tab;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   415
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17222
diff changeset
   416
fun update (key, x) tab = modify key (fn _ => x) tab;
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17222
diff changeset
   417
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
   418
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
   419
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
   420
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
   421
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   422
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   423
(* delete *)
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   424
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   425
exception UNDEF of key;
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   426
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   427
local
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   428
77735
be3f838b3e17 tuned --- fewer compiler warnings;
wenzelm
parents: 77733
diff changeset
   429
fun compare NONE _ = LESS
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   430
  | compare (SOME k1) (k2, _) = Key.ord (k1, k2);
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   431
77737
wenzelm
parents: 77736
diff changeset
   432
fun if_equal ord x y = if is_equal ord then x else y;
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   433
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 15160
diff changeset
   434
fun del (SOME k) Empty = raise UNDEF k
77735
be3f838b3e17 tuned --- fewer compiler warnings;
wenzelm
parents: 77733
diff changeset
   435
  | del NONE Empty = raise Match
79094
wenzelm
parents: 79093
diff changeset
   436
  | del NONE (Leaf1 e) = (e, (true, Empty))
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   437
  | del NONE (Leaf2 (e1, e2)) = (e1, (false, Leaf1 e2))
79094
wenzelm
parents: 79093
diff changeset
   438
  | del k (Leaf1 e) =
wenzelm
parents: 79093
diff changeset
   439
      (case compare k e of
wenzelm
parents: 79093
diff changeset
   440
        EQUAL => (e, (true, Empty))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   441
      | _ => raise UNDEF (the k))
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   442
  | del k (Leaf2 (e1, e2)) =
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   443
      (case compare k e1 of
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   444
        EQUAL => (e1, (false, Leaf1 e2))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   445
      | _ =>
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   446
        (case compare k e2 of
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   447
          EQUAL => (e2, (false, Leaf1 e1))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   448
        | _ => raise UNDEF (the k)))
79092
06176f4e2e70 slightly more compact heap: better sharing of persistent tuples;
wenzelm
parents: 77981
diff changeset
   449
  | del k (Leaf3 (e1, e2, e3)) = del k (Branch2 (Leaf1 e1, e2, Leaf1 e3))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   450
  | del k (Branch2 (l, p, r)) =
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   451
      (case compare k p of
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   452
        LESS =>
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   453
          (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
   454
            (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
   455
          | (p', (true, l')) => (p', case unmake r of
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   456
              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
   457
                (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
   458
            | 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
   459
                (make2 (l', p, rl), rp, make2 (rm, rq, rr)))))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   460
      | ord =>
77737
wenzelm
parents: 77736
diff changeset
   461
          (case del (if_equal ord NONE k) r of
wenzelm
parents: 77736
diff changeset
   462
            (p', (false, r')) => (p', (false, make2 (l, if_equal ord p' p, r')))
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   463
          | (p', (true, r')) => (p', case unmake l of
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   464
              Branch2 (ll, lp, lr) =>
77737
wenzelm
parents: 77736
diff changeset
   465
                (true, make3 (ll, lp, lr, if_equal ord p' p, r'))
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   466
            | Branch3 (ll, lp, lm, lq, lr) => (false, make2
77737
wenzelm
parents: 77736
diff changeset
   467
                (make2 (ll, lp, lm), lq, make2 (lr, if_equal ord p' p, r'))))))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   468
  | del k (Branch3 (l, p, m, q, r)) =
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   469
      (case compare k q of
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   470
        LESS =>
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   471
          (case compare k p of
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   472
            LESS =>
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   473
              (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
   474
                (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
   475
              | (p', (true, l')) => (p', (false, case (unmake m, unmake r) of
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   476
                  (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
   477
                    make2 (make3 (l', p, ml, mp, mr), q, r)
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   478
                | (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
   479
                    make3 (make2 (l', p, ml), mp, make2 (mm, mq, mr), q, r)
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   480
                | (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
   481
                    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
   482
                      make2 (rm, rq, rr)))))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   483
          | ord =>
77737
wenzelm
parents: 77736
diff changeset
   484
              (case del (if_equal ord NONE k) m of
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   485
                (p', (false, m')) =>
77737
wenzelm
parents: 77736
diff changeset
   486
                  (p', (false, make3 (l, if_equal ord p' p, m', q, r)))
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   487
              | (p', (true, m')) => (p', (false, case (unmake l, unmake r) of
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   488
                  (Branch2 (ll, lp, lr), Branch2 _) =>
77737
wenzelm
parents: 77736
diff changeset
   489
                    make2 (make3 (ll, lp, lr, if_equal ord p' p, m'), q, r)
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   490
                | (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
   491
                    make3 (make2 (ll, lp, lm), lq,
77737
wenzelm
parents: 77736
diff changeset
   492
                      make2 (lr, if_equal ord p' p, m'), q, r)
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   493
                | (_, Branch3 (rl, rp, rm, rq, rr)) =>
77737
wenzelm
parents: 77736
diff changeset
   494
                    make3 (l, if_equal ord p' p, make2 (m', q, rl), rp,
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   495
                      make2 (rm, rq, rr))))))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   496
      | ord =>
77737
wenzelm
parents: 77736
diff changeset
   497
          (case del (if_equal ord NONE k) r of
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   498
            (q', (false, r')) =>
77737
wenzelm
parents: 77736
diff changeset
   499
              (q', (false, make3 (l, p, m, if_equal ord q' q, r')))
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   500
          | (q', (true, r')) => (q', (false, case (unmake l, unmake m) of
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   501
              (Branch2 _, Branch2 (ml, mp, mr)) =>
77737
wenzelm
parents: 77736
diff changeset
   502
                make2 (l, p, make3 (ml, mp, mr, if_equal ord q' q, r'))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   503
            | (_, 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
   504
                make3 (l, p, make2 (ml, mp, mm), mq,
77737
wenzelm
parents: 77736
diff changeset
   505
                  make2 (mr, if_equal ord q' q, r'))
77721
7c1cc9ce9340 tuned whitespace;
wenzelm
parents: 74269
diff changeset
   506
            | (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
   507
                make3 (make2 (ll, lp, lm), lq, make2 (lr, p, ml), mp,
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   508
                  make2 (mr, if_equal ord q' q, r'))))))
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   509
  | del k (Size (_, arg)) = del k arg;
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   510
in
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   511
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77781
diff changeset
   512
fun delete key tab = make_size (size tab - 1) (snd (snd (del (SOME key) tab)));
44336
59ff5a93eef4 tuned Table.delete_safe: avoid potentially expensive attempt of delete;
wenzelm
parents: 43792
diff changeset
   513
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
   514
15665
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   515
end;
7e7412fffc0c tuned updates, added map_entry;
wenzelm
parents: 15574
diff changeset
   516
15014
97ab70c3d955 Added delete operation.
berghofe
parents: 14981
diff changeset
   517
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   518
(* membership operations *)
16466
82e2fc13ce54 added member;
wenzelm
parents: 16446
diff changeset
   519
82e2fc13ce54 added member;
wenzelm
parents: 16446
diff changeset
   520
fun member eq tab (key, x) =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17222
diff changeset
   521
  (case lookup tab key of
16466
82e2fc13ce54 added member;
wenzelm
parents: 16446
diff changeset
   522
    NONE => false
82e2fc13ce54 added member;
wenzelm
parents: 16446
diff changeset
   523
  | SOME y => eq (x, y));
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   524
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   525
fun insert eq (key, x) =
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   526
  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
   527
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   528
fun remove eq (key, x) tab =
17412
e26cb20ef0cc TableFun/Symtab: curried lookup and update;
wenzelm
parents: 17222
diff changeset
   529
  (case lookup tab key of
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   530
    NONE => tab
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   531
  | 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
   532
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   533
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   534
(* simultaneous modifications *)
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   535
79249
718798653cf1 more operations;
wenzelm
parents: 79163
diff changeset
   536
fun make_distinct entries = build (fold default entries);
718798653cf1 more operations;
wenzelm
parents: 79163
diff changeset
   537
74266
612b7e0d6721 clarified signature;
wenzelm
parents: 74232
diff changeset
   538
fun make entries = build (fold update_new entries);
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   539
77781
wenzelm
parents: 77780
diff changeset
   540
fun join f (tab1, tab2) =
55727
7e330ae052bb optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents: 52049
diff changeset
   541
  let
7e330ae052bb optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents: 52049
diff changeset
   542
    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
   543
  in
77781
wenzelm
parents: 77780
diff changeset
   544
    if pointer_eq (tab1, tab2) then tab1
wenzelm
parents: 77780
diff changeset
   545
    else if is_empty tab1 then tab2
wenzelm
parents: 77780
diff changeset
   546
    else fold_table add tab2 tab1
55727
7e330ae052bb optimize special case according to Library.merge (see also 8fbc355100f2, 520872460b7b);
wenzelm
parents: 52049
diff changeset
   547
  end;
12287
7017cee2f3ac added join, merge_multi(');
wenzelm
parents: 8806
diff changeset
   548
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   549
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
   550
77822
353c4d3e6dda more operations;
wenzelm
parents: 77814
diff changeset
   551
fun joins f tabs = Library.foldl (join f) (empty, tabs);
353c4d3e6dda more operations;
wenzelm
parents: 77814
diff changeset
   552
fun merges eq tabs = Library.foldl (merge eq) (empty, tabs);
353c4d3e6dda more operations;
wenzelm
parents: 77814
diff changeset
   553
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   554
19031
0059b5b195a2 export exception SAME (for join);
wenzelm
parents: 18946
diff changeset
   555
(* list tables *)
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   556
18946
ce65d2d2e0c2 renamed xxx_multi to xxx_list;
wenzelm
parents: 18007
diff changeset
   557
fun lookup_list tab key = these (lookup tab key);
25391
783e5de2a6c7 renamed update_list to cons_list;
wenzelm
parents: 23655
diff changeset
   558
783e5de2a6c7 renamed update_list to cons_list;
wenzelm
parents: 23655
diff changeset
   559
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
   560
77981
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   561
fun modify_list key f =
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   562
  modify key (fn arg =>
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   563
    let
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   564
      val xs = the_default [] arg;
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   565
      val ys = f xs;
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   566
    in if pointer_eq (xs, ys) then raise SAME else ys end);
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   567
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   568
fun insert_list eq (key, x) = modify_list key (Library.insert eq x);
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   569
fun update_list eq (key, x) = modify_list key (Library.update eq x);
25391
783e5de2a6c7 renamed update_list to cons_list;
wenzelm
parents: 23655
diff changeset
   570
18946
ce65d2d2e0c2 renamed xxx_multi to xxx_list;
wenzelm
parents: 18007
diff changeset
   571
fun remove_list eq (key, x) tab =
77981
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   572
  map_entry key (fn xs =>
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   573
    (case Library.remove eq x xs of
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   574
      [] => raise UNDEF key
f83702560730 minor performance tuning;
wenzelm
parents: 77975
diff changeset
   575
    | ys => if pointer_eq (xs, ys) then raise SAME else ys)) tab
15761
c9561302c74a added delete_safe, insert, remove, remove_multi;
wenzelm
parents: 15665
diff changeset
   576
  handle UNDEF _ => delete key tab;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   577
74266
612b7e0d6721 clarified signature;
wenzelm
parents: 74232
diff changeset
   578
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
   579
fun dest_list tab = maps (fn (key, xs) => map (pair key) xs) (dest tab);
77973
ab6e4085a19d minor performance tuning;
wenzelm
parents: 77882
diff changeset
   580
fun merge_list eq =
77975
a7ca67369755 more operations;
wenzelm
parents: 77973
diff changeset
   581
  join (fn _ => fn xy => if eq_list eq xy then raise SAME else Library.merge eq xy);
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   582
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   583
74227
fdcc7e8f95ea more scalable operations;
wenzelm
parents: 70586
diff changeset
   584
(* set operations *)
50234
c97c5c34fb1d convenience operations for table as set;
wenzelm
parents: 47980
diff changeset
   585
c97c5c34fb1d convenience operations for table as set;
wenzelm
parents: 47980
diff changeset
   586
type set = unit table;
c97c5c34fb1d convenience operations for table as set;
wenzelm
parents: 47980
diff changeset
   587
67529
37db2dc5c022 clarified operations;
wenzelm
parents: 66935
diff changeset
   588
fun insert_set x = default (x, ());
37db2dc5c022 clarified operations;
wenzelm
parents: 66935
diff changeset
   589
fun remove_set x : set -> set = delete_safe x;
74266
612b7e0d6721 clarified signature;
wenzelm
parents: 74232
diff changeset
   590
fun make_set xs = build (fold insert_set xs);
50234
c97c5c34fb1d convenience operations for table as set;
wenzelm
parents: 47980
diff changeset
   591
c97c5c34fb1d convenience operations for table as set;
wenzelm
parents: 47980
diff changeset
   592
79163
9ddcaca41ed2 more operations;
wenzelm
parents: 79094
diff changeset
   593
(* cache *)
9ddcaca41ed2 more operations;
wenzelm
parents: 79094
diff changeset
   594
80583
9a40ec7a2027 more operations;
wenzelm
parents: 80579
diff changeset
   595
type 'a cache_ops = {apply: key -> 'a, reset: unit -> unit, size: unit -> int};
80579
69cf3c308d6c clarified signature: more operations;
wenzelm
parents: 80240
diff changeset
   596
79163
9ddcaca41ed2 more operations;
wenzelm
parents: 79094
diff changeset
   597
fun unsynchronized_cache f =
80240
534c5e4f07c0 minor performance tuning: more compact data;
wenzelm
parents: 79249
diff changeset
   598
  let
534c5e4f07c0 minor performance tuning: more compact data;
wenzelm
parents: 79249
diff changeset
   599
    val cache1 = Unsynchronized.ref empty;
534c5e4f07c0 minor performance tuning: more compact data;
wenzelm
parents: 79249
diff changeset
   600
    val cache2 = Unsynchronized.ref empty;
80579
69cf3c308d6c clarified signature: more operations;
wenzelm
parents: 80240
diff changeset
   601
    fun apply x =
80240
534c5e4f07c0 minor performance tuning: more compact data;
wenzelm
parents: 79249
diff changeset
   602
      (case lookup (! cache1) x of
534c5e4f07c0 minor performance tuning: more compact data;
wenzelm
parents: 79249
diff changeset
   603
        SOME y => y
79163
9ddcaca41ed2 more operations;
wenzelm
parents: 79094
diff changeset
   604
      | NONE =>
80240
534c5e4f07c0 minor performance tuning: more compact data;
wenzelm
parents: 79249
diff changeset
   605
          (case lookup (! cache2) x of
534c5e4f07c0 minor performance tuning: more compact data;
wenzelm
parents: 79249
diff changeset
   606
            SOME exn => Exn.reraise exn
534c5e4f07c0 minor performance tuning: more compact data;
wenzelm
parents: 79249
diff changeset
   607
          | NONE =>
534c5e4f07c0 minor performance tuning: more compact data;
wenzelm
parents: 79249
diff changeset
   608
              (case Exn.result f x of
80591
1d6e5b7a6906 more conservative cache: retain concurrent value;
wenzelm
parents: 80583
diff changeset
   609
                Exn.Res y => (Unsynchronized.change cache1 (default (x, y)); y)
1d6e5b7a6906 more conservative cache: retain concurrent value;
wenzelm
parents: 80583
diff changeset
   610
              | Exn.Exn exn => (Unsynchronized.change cache2 (default (x, exn)); Exn.reraise exn))));
80579
69cf3c308d6c clarified signature: more operations;
wenzelm
parents: 80240
diff changeset
   611
    fun reset () = (cache2 := empty; cache1 := empty);
80583
9a40ec7a2027 more operations;
wenzelm
parents: 80579
diff changeset
   612
    fun total_size () = size (! cache1) + size (! cache2);
9a40ec7a2027 more operations;
wenzelm
parents: 80579
diff changeset
   613
  in {apply = apply, reset = reset, size = total_size} end;
79163
9ddcaca41ed2 more operations;
wenzelm
parents: 79094
diff changeset
   614
9ddcaca41ed2 more operations;
wenzelm
parents: 79094
diff changeset
   615
47980
c81801f881b3 simplified Poly/ML setup -- 5.3.0 is now the common base-line;
wenzelm
parents: 44336
diff changeset
   616
(* 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
   617
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
   618
val _ =
62819
d3ff367a16a0 careful export of type-dependent functions, without losing their special status;
wenzelm
parents: 62503
diff changeset
   619
  ML_system_pp (fn depth => fn pretty => fn tab =>
80809
4a64fc4d1cde clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents: 80591
diff changeset
   620
    ML_Pretty.enum "," "{" "}" (ML_Pretty.pair ML_system_pretty pretty) (dest tab, depth));
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
   621
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
   622
5681
464913c6086a added foldl, keys;
wenzelm
parents: 5015
diff changeset
   623
(*final declarations of this structure!*)
39020
ac0f24f850c9 replaced Table.map' by Table.map
haftmann
parents: 38635
diff changeset
   624
val map = map_table;
16446
0ab34b9d1b1f added map', fold;
wenzelm
parents: 16342
diff changeset
   625
val fold = fold_table;
28334
7c693bb76366 added fold_rev;
wenzelm
parents: 27783
diff changeset
   626
val fold_rev = fold_rev_table;
5015
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   627
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   628
end;
44fd9e09c637 moved table.ML to General/table.ML;
wenzelm
parents:
diff changeset
   629
31971
8c1b845ed105 renamed functor TableFun to Table, and GraphFun to Graph;
wenzelm
parents: 31621
diff changeset
   630
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
   631
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
   632
structure Symreltab = Table(type key = string * string
30647
ef8f46c3158a added Symreltab (binary relations of symbols) instance of TableFun
haftmann
parents: 30290
diff changeset
   633
  val ord = prod_ord fast_string_ord fast_string_ord);