src/Pure/General/set.ML
author wenzelm
Wed, 12 Apr 2023 10:52:50 +0200
changeset 77836 9d124714a9e8
parent 77822 353c4d3e6dda
child 77875 9374e13655e8
permissions -rw-r--r--
more operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/set.ML
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
     3
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
     4
Efficient representation of sets (see also Pure/General/table.ML).
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
     5
*)
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
     6
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
     7
signature SET =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
     8
sig
77731
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77728
diff changeset
     9
  structure Key: KEY
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    10
  type elem
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    11
  type T
77725
96a594e5e054 added Set.size;
wenzelm
parents: 77722
diff changeset
    12
  val size: T -> int
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    13
  val empty: T
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    14
  val build: (T -> T) -> T
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    15
  val is_empty: T -> bool
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    16
  val fold: (elem -> 'a -> 'a) -> T -> 'a -> 'a
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    17
  val fold_rev: (elem -> 'a -> 'a) -> T -> 'a -> 'a
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    18
  val dest: T -> elem list
77816
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
    19
  val min: T -> elem option
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
    20
  val max: T -> elem option
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    21
  val exists: (elem -> bool) -> T -> bool
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    22
  val forall: (elem -> bool) -> T -> bool
77728
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
    23
  val get_first: (elem -> 'a option) -> T -> 'a option
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    24
  val member: T -> elem -> bool
77728
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
    25
  val subset: T * T -> bool
77836
9d124714a9e8 more operations;
wenzelm
parents: 77822
diff changeset
    26
  val eq_set: T * T -> bool
77728
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
    27
  val ord: T ord
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    28
  val insert: elem -> T -> T
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    29
  val make: elem list -> T
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    30
  val merge: T * T -> T
77822
353c4d3e6dda more operations;
wenzelm
parents: 77816
diff changeset
    31
  val merges: T list -> T
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    32
  val remove: elem -> T -> T
77728
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
    33
  val subtract: T -> T -> T
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    34
end;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    35
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    36
functor Set(Key: KEY): SET =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    37
struct
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    38
77733
wenzelm
parents: 77732
diff changeset
    39
(* keys *)
wenzelm
parents: 77732
diff changeset
    40
77731
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77728
diff changeset
    41
structure Key = Key;
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77728
diff changeset
    42
type elem = Key.key;
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    43
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    44
77731
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77728
diff changeset
    45
(* datatype *)
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    46
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    47
datatype T =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    48
  Empty |
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    49
  Leaf1 of elem |
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    50
  Leaf2 of elem * elem |
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    51
  Leaf3 of elem * elem * elem |
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    52
  Branch2 of T * elem * T |
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
    53
  Branch3 of T * elem * T * elem * T |
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
    54
  Size of int * T;
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    55
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    56
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
    57
  | 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
    58
  | make2 (left, e1, Branch2 (Empty, e2, Empty)) = make2 (left, e1, Leaf1 e2)
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    59
  | make2 (Branch3 (Empty, e1, Empty, e2, Empty), e3, right) = make2 (Leaf2 (e1, e2), e3, right)
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    60
  | make2 (left, e1, Branch3 (Empty, e2, Empty, e3, Empty)) = make2 (left, e1, Leaf2 (e2, e3))
77739
2225d3267f58 slightly more compact data;
wenzelm
parents: 77737
diff changeset
    61
  | make2 (Leaf1 e1, e2, Empty) = Leaf2 (e1, e2)
2225d3267f58 slightly more compact data;
wenzelm
parents: 77737
diff changeset
    62
  | make2 (Empty, e1, Leaf1 e2) = Leaf2 (e1, e2)
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    63
  | make2 (Leaf1 e1, e2, Leaf1 e3) = Leaf3 (e1, e2, e3)
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    64
  | make2 (Leaf2 (e1, e2), e3, Empty) = Leaf3 (e1, e2, e3)
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    65
  | 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
    66
  | 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
    67
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    68
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
    69
  | 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
    70
  | 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
    71
  | make3 (left, e1, mid, e2, Branch2 (Empty, e3, Empty)) = make3 (left, e1, mid, e2, Leaf1 e3)
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    72
  | make3 (Leaf1 e1, e2, Empty, e3, Empty) = Leaf3 (e1, e2, e3)
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    73
  | make3 (Empty, e1, Leaf1 e2, e3, Empty) = Leaf3 (e1, e2, e3)
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    74
  | 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
    75
  | 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
    76
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    77
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
    78
  | unmake (Leaf2 (e1, e2)) = Branch3 (Empty, e1, Empty, e2, Empty)
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    79
  | unmake (Leaf3 (e1, e2, e3)) =
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
    80
      Branch2 (Branch2 (Empty, e1, Empty), e2, Branch2 (Empty, e3, Empty))
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
    81
  | 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
    82
  | unmake arg = arg;
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
    83
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
    84
77725
96a594e5e054 added Set.size;
wenzelm
parents: 77722
diff changeset
    85
(* size *)
96a594e5e054 added Set.size;
wenzelm
parents: 77722
diff changeset
    86
77780
97febdb6ee58 clarified signature: more uniform Table() vs. Set();
wenzelm
parents: 77768
diff changeset
    87
(*literal copy from table.ML*)
77725
96a594e5e054 added Set.size;
wenzelm
parents: 77722
diff changeset
    88
local
77768
65008644d394 tuned: prefer "build" combinator;
wenzelm
parents: 77743
diff changeset
    89
  fun count Empty n = n
65008644d394 tuned: prefer "build" combinator;
wenzelm
parents: 77743
diff changeset
    90
    | count (Leaf1 _) n = n + 1
65008644d394 tuned: prefer "build" combinator;
wenzelm
parents: 77743
diff changeset
    91
    | count (Leaf2 _) n = n + 2
65008644d394 tuned: prefer "build" combinator;
wenzelm
parents: 77743
diff changeset
    92
    | count (Leaf3 _) n = n + 3
65008644d394 tuned: prefer "build" combinator;
wenzelm
parents: 77743
diff changeset
    93
    | count (Branch2 (left, _, right)) n = count right (count left (n + 1))
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
    94
    | count (Branch3 (left, _, mid, _, right)) n = count right (count mid (count left (n + 2)))
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
    95
    | 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
    96
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
    97
  fun box (Branch2 _) = 1
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
    98
    | box (Branch3 _) = 1
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
    99
    | box _ = 0;
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   100
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   101
  fun bound arg b =
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   102
    if b > 0 then
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   103
      (case arg of
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   104
        Branch2 (left, _, right) =>
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   105
          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
   106
      | Branch3 (left, _, mid, _, right) =>
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   107
          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
   108
      | _ => b)
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   109
    else b;
77725
96a594e5e054 added Set.size;
wenzelm
parents: 77722
diff changeset
   110
in
77802
25c114e2528e performance tuning: make_size accounts for boxes, i.e. pointer derefs required in "count";
wenzelm
parents: 77800
diff changeset
   111
  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
   112
  fun make_size m arg = if bound arg 3 <= 0 then Size (m, arg) else arg;
77725
96a594e5e054 added Set.size;
wenzelm
parents: 77722
diff changeset
   113
end;
96a594e5e054 added Set.size;
wenzelm
parents: 77722
diff changeset
   114
96a594e5e054 added Set.size;
wenzelm
parents: 77722
diff changeset
   115
77743
33bee7a96f72 tuned comments (amending 1951f6470792);
wenzelm
parents: 77742
diff changeset
   116
(* empty *)
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   117
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   118
val empty = Empty;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   119
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   120
fun build (f: T -> T) = f empty;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   121
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   122
(*literal copy from table.ML*)
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   123
fun is_empty Empty = true
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   124
  | is_empty (Size (_, arg)) = is_empty arg
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   125
  | is_empty _ = false;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   126
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   127
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   128
(* fold combinators *)
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   129
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   130
fun fold_set f =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   131
  let
77813
wenzelm
parents: 77805
diff changeset
   132
    fun fold Empty a = a
wenzelm
parents: 77805
diff changeset
   133
      | fold (Leaf1 e) a = f e a
wenzelm
parents: 77805
diff changeset
   134
      | fold (Leaf2 (e1, e2)) a = f e2 (f e1 a)
wenzelm
parents: 77805
diff changeset
   135
      | fold (Leaf3 (e1, e2, e3)) a = f e3 (f e2 (f e1 a))
wenzelm
parents: 77805
diff changeset
   136
      | fold (Branch2 (left, e, right)) a =
wenzelm
parents: 77805
diff changeset
   137
          fold right (f e (fold left a))
wenzelm
parents: 77805
diff changeset
   138
      | fold (Branch3 (left, e1, mid, e2, right)) a =
wenzelm
parents: 77805
diff changeset
   139
          fold right (f e2 (fold mid (f e1 (fold left a))))
wenzelm
parents: 77805
diff changeset
   140
      | fold (Size (_, arg)) a = fold arg a;
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   141
  in fold end;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   142
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   143
fun fold_rev_set f =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   144
  let
77813
wenzelm
parents: 77805
diff changeset
   145
    fun fold_rev Empty a = a
wenzelm
parents: 77805
diff changeset
   146
      | fold_rev (Leaf1 e) a = f e a
wenzelm
parents: 77805
diff changeset
   147
      | fold_rev (Leaf2 (e1, e2)) a = f e1 (f e2 a)
wenzelm
parents: 77805
diff changeset
   148
      | fold_rev (Leaf3 (e1, e2, e3)) a = f e1 (f e2 (f e3 a))
wenzelm
parents: 77805
diff changeset
   149
      | fold_rev (Branch2 (left, e, right)) a =
wenzelm
parents: 77805
diff changeset
   150
          fold_rev left (f e (fold_rev right a))
wenzelm
parents: 77805
diff changeset
   151
      | fold_rev (Branch3 (left, e1, mid, e2, right)) a =
wenzelm
parents: 77805
diff changeset
   152
          fold_rev left (f e1 (fold_rev mid (f e2 (fold_rev right a))))
wenzelm
parents: 77805
diff changeset
   153
      | fold_rev (Size (_, arg)) a = fold_rev arg a;
77732
wenzelm
parents: 77731
diff changeset
   154
  in fold_rev end;
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   155
77768
65008644d394 tuned: prefer "build" combinator;
wenzelm
parents: 77743
diff changeset
   156
val dest = Library.build o fold_rev_set cons;
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   157
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   158
77816
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   159
(* min/max entries *)
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   160
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   161
fun min Empty = NONE
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   162
  | min (Leaf1 e) = SOME e
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   163
  | min (Leaf2 (e, _)) = SOME e
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   164
  | min (Leaf3 (e, _, _)) = SOME e
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   165
  | min (Branch2 (Empty, e, _)) = SOME e
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   166
  | min (Branch3 (Empty, e, _, _, _)) = SOME e
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   167
  | min (Branch2 (left, _, _)) = min left
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   168
  | min (Branch3 (left, _, _, _, _)) = min left
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   169
  | min (Size (_, arg)) = min arg;
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   170
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   171
fun max Empty = NONE
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   172
  | max (Leaf1 e) = SOME e
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   173
  | max (Leaf2 (_, e)) = SOME e
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   174
  | max (Leaf3 (_, _, e)) = SOME e
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   175
  | max (Branch2 (_, e, Empty)) = SOME e
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   176
  | max (Branch3 (_, _, _, e, Empty)) = SOME e
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   177
  | max (Branch2 (_, _, right)) = max right
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   178
  | max (Branch3 (_, _, _, _, right)) = max right
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   179
  | max (Size (_, arg)) = max arg;
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   180
aa814dc5a685 more operations;
wenzelm
parents: 77814
diff changeset
   181
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   182
(* exists and forall *)
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   183
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   184
fun exists pred =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   185
  let
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   186
    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
   187
      | 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
   188
      | ex (Leaf2 (e1, e2)) = pred e1 orelse pred e2
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   189
      | ex (Leaf3 (e1, e2, e3)) = pred e1 orelse pred e2 orelse pred e3
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   190
      | ex (Branch2 (left, e, right)) =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   191
          ex left orelse pred e orelse ex right
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   192
      | ex (Branch3 (left, e1, mid, e2, right)) =
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   193
          ex left orelse pred e1 orelse ex mid orelse pred e2 orelse ex right
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   194
      | ex (Size (_, arg)) = ex arg;
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   195
  in ex end;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   196
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   197
fun forall pred = not o exists (not o pred);
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   198
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   199
77728
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   200
(* get_first *)
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   201
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   202
fun get_first f =
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   203
  let
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   204
    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
   205
      | 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
   206
      | 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
   207
          (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
   208
            NONE => f e2
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   209
          | some => some)
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   210
      | get (Leaf3 (e1, e2, e3)) =
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   211
          (case f e1 of
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   212
            NONE =>
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   213
              (case f e2 of
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   214
                NONE => f e3
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   215
              | some => some)
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   216
          | some => some)
77728
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   217
      | get (Branch2 (left, e, right)) =
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   218
          (case get left of
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   219
            NONE =>
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   220
              (case f e of
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   221
                NONE => get right
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   222
              | some => some)
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   223
          | some => some)
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   224
      | get (Branch3 (left, e1, mid, e2, right)) =
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   225
          (case get left of
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   226
            NONE =>
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   227
              (case f e1 of
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   228
                NONE =>
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   229
                  (case get mid of
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   230
                    NONE =>
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   231
                      (case f e2 of
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   232
                        NONE => get right
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   233
                      | some => some)
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   234
                  | some => some)
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   235
              | some => some)
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   236
          | some => some)
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   237
      | get (Size (_, arg)) = get arg;
77728
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   238
  in get end;
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   239
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   240
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   241
(* member *)
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   242
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   243
fun member set elem =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   244
  let
77742
wenzelm
parents: 77741
diff changeset
   245
    fun elem_ord e = Key.ord (elem, e)
wenzelm
parents: 77741
diff changeset
   246
    val elem_eq = is_equal o elem_ord;
wenzelm
parents: 77741
diff changeset
   247
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   248
    fun mem Empty = false
77742
wenzelm
parents: 77741
diff changeset
   249
      | mem (Leaf1 e) = elem_eq e
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   250
      | mem (Leaf2 (e1, e2)) =
77742
wenzelm
parents: 77741
diff changeset
   251
          (case elem_ord e1 of
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   252
            LESS => false
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   253
          | EQUAL => true
77742
wenzelm
parents: 77741
diff changeset
   254
          | GREATER => elem_eq e2)
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   255
      | mem (Leaf3 (e1, e2, e3)) =
77742
wenzelm
parents: 77741
diff changeset
   256
          (case elem_ord e2 of
wenzelm
parents: 77741
diff changeset
   257
            LESS => elem_eq e1
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   258
          | EQUAL => true
77742
wenzelm
parents: 77741
diff changeset
   259
          | GREATER => elem_eq e3)
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   260
      | mem (Branch2 (left, e, right)) =
77742
wenzelm
parents: 77741
diff changeset
   261
          (case elem_ord e of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   262
            LESS => mem left
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   263
          | EQUAL => true
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   264
          | GREATER => mem right)
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   265
      | mem (Branch3 (left, e1, mid, e2, right)) =
77742
wenzelm
parents: 77741
diff changeset
   266
          (case elem_ord e1 of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   267
            LESS => mem left
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   268
          | EQUAL => true
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   269
          | GREATER =>
77742
wenzelm
parents: 77741
diff changeset
   270
              (case elem_ord e2 of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   271
                LESS => mem mid
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   272
              | EQUAL => true
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   273
              | GREATER => mem right))
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   274
      | mem (Size (_, arg)) = mem arg;
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   275
  in mem set end;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   276
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   277
77728
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   278
(* subset and order *)
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   279
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   280
fun subset (set1, set2) = forall (member set2) set1;
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   281
77836
9d124714a9e8 more operations;
wenzelm
parents: 77822
diff changeset
   282
fun eq_set (set1, set2) =
9d124714a9e8 more operations;
wenzelm
parents: 77822
diff changeset
   283
  pointer_eq (set1, set2) orelse size set1 = size set2 andalso subset (set1, set2);
9d124714a9e8 more operations;
wenzelm
parents: 77822
diff changeset
   284
77728
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   285
val ord =
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   286
  pointer_eq_ord (fn sets =>
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   287
    (case int_ord (apply2 size sets) of
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   288
      EQUAL =>
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   289
        if subset sets then EQUAL
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   290
        else dict_ord Key.ord (apply2 dest sets)
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   291
    | ord => ord));
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   292
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   293
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   294
(* insert *)
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   295
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   296
datatype growth = Stay of T | Sprout of T * elem * T;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   297
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   298
fun insert elem set =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   299
  if member set elem then set
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   300
  else
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   301
    let
77742
wenzelm
parents: 77741
diff changeset
   302
      fun elem_ord e = Key.ord (elem, e)
wenzelm
parents: 77741
diff changeset
   303
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   304
      fun ins Empty = Sprout (Empty, elem, Empty)
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   305
        | ins (t as Leaf1 _) = ins (unmake t)
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   306
        | ins (t as Leaf2 _) = ins (unmake t)
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   307
        | ins (t as Leaf3 _) = ins (unmake t)
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   308
        | ins (Branch2 (left, e, right)) =
77742
wenzelm
parents: 77741
diff changeset
   309
            (case elem_ord e of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   310
              LESS =>
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   311
                (case ins left of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   312
                  Stay left' => Stay (make2 (left', e, right))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   313
                | Sprout (left1, e', left2) => Stay (make3 (left1, e', left2, e, right)))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   314
            | EQUAL => Stay (make2 (left, e, right))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   315
            | GREATER =>
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   316
                (case ins right of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   317
                  Stay right' => Stay (make2 (left, e, right'))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   318
                | Sprout (right1, e', right2) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   319
                    Stay (make3 (left, e, right1, e', right2))))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   320
        | ins (Branch3 (left, e1, mid, e2, right)) =
77742
wenzelm
parents: 77741
diff changeset
   321
            (case elem_ord e1 of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   322
              LESS =>
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   323
                (case ins left of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   324
                  Stay left' => Stay (make3 (left', e1, mid, e2, right))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   325
                | Sprout (left1, e', left2) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   326
                    Sprout (make2 (left1, e', left2), e1, make2 (mid, e2, right)))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   327
            | EQUAL => Stay (make3 (left, e1, mid, e2, right))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   328
            | GREATER =>
77742
wenzelm
parents: 77741
diff changeset
   329
                (case elem_ord e2 of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   330
                  LESS =>
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   331
                    (case ins mid of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   332
                      Stay mid' => Stay (make3 (left, e1, mid', e2, right))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   333
                    | Sprout (mid1, e', mid2) =>
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   334
                        Sprout (make2 (left, e1, mid1), e', make2 (mid2, e2, right)))
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   335
                | EQUAL => Stay (make3 (left, e1, mid, e2, right))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   336
                | GREATER =>
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   337
                    (case ins right of
77736
570f1436fe0a more compact representation of leaf nodes: only 1.10 .. 1.33 larger than plain list;
wenzelm
parents: 77735
diff changeset
   338
                      Stay right' => Stay (make3 (left, e1, mid, e2, right'))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   339
                    | Sprout (right1, e', right2) =>
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   340
                        Sprout (make2 (left, e1, mid), e2, make2 (right1, e', right2)))))
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   341
        | ins (Size (_, arg)) = ins arg;
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   342
    in
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   343
      make_size (size set + 1)
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   344
        (case ins set of
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   345
          Stay set' => set'
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   346
        | Sprout br => make2 br)
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   347
    end;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   348
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   349
fun make elems = build (fold insert elems);
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   350
77822
353c4d3e6dda more operations;
wenzelm
parents: 77816
diff changeset
   351
353c4d3e6dda more operations;
wenzelm
parents: 77816
diff changeset
   352
(* merge *)
353c4d3e6dda more operations;
wenzelm
parents: 77816
diff changeset
   353
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   354
fun merge (set1, set2) =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   355
  if pointer_eq (set1, set2) then set1
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   356
  else if is_empty set1 then set2
77725
96a594e5e054 added Set.size;
wenzelm
parents: 77722
diff changeset
   357
  else if is_empty set2 then set1
96a594e5e054 added Set.size;
wenzelm
parents: 77722
diff changeset
   358
  else if size set1 >= size set2
96a594e5e054 added Set.size;
wenzelm
parents: 77722
diff changeset
   359
  then fold_set insert set2 set1
96a594e5e054 added Set.size;
wenzelm
parents: 77722
diff changeset
   360
  else fold_set insert set1 set2;
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   361
77822
353c4d3e6dda more operations;
wenzelm
parents: 77816
diff changeset
   362
fun merges sets = Library.foldl merge (empty, sets);
353c4d3e6dda more operations;
wenzelm
parents: 77816
diff changeset
   363
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   364
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   365
(* remove *)
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   366
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   367
local
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   368
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   369
fun compare NONE _ = LESS
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   370
  | compare (SOME e1) e2 = Key.ord (e1, e2);
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   371
77737
wenzelm
parents: 77736
diff changeset
   372
fun if_equal ord x y = if is_equal ord then x else y;
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   373
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   374
exception UNDEF of elem;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   375
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   376
fun del (SOME k) Empty = raise UNDEF k
77735
be3f838b3e17 tuned --- fewer compiler warnings;
wenzelm
parents: 77733
diff changeset
   377
  | 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
   378
  | 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
   379
  | 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
   380
  | del k (Leaf1 p) =
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   381
      (case compare k p of
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   382
        EQUAL => (p, (true, Empty))
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   383
      | _ => 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
   384
  | del k (Leaf2 (p, q)) =
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   385
      (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
   386
        EQUAL => (p, (false, Leaf1 q))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   387
      | _ =>
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   388
        (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
   389
          EQUAL => (q, (false, Leaf1 p))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   390
        | _ => raise UNDEF (the k)))
77740
19c539f5d4d3 more compact data: approx. 0.85 .. 1.10 of plain list size;
wenzelm
parents: 77739
diff changeset
   391
  | del k (Leaf3 (p, q, r)) = del k (Branch2 (Leaf1 p, q, Leaf1 r))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   392
  | del k (Branch2 (l, p, r)) =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   393
      (case compare k p of
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   394
        LESS =>
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   395
          (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
   396
            (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
   397
          | (p', (true, l')) => (p', case unmake r of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   398
              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
   399
                (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
   400
            | 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
   401
                (make2 (l', p, rl), rp, make2 (rm, rq, rr)))))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   402
      | ord =>
77737
wenzelm
parents: 77736
diff changeset
   403
          (case del (if_equal ord NONE k) r of
wenzelm
parents: 77736
diff changeset
   404
            (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
   405
          | (p', (true, r')) => (p', case unmake l of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   406
              Branch2 (ll, lp, lr) =>
77737
wenzelm
parents: 77736
diff changeset
   407
                (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
   408
            | Branch3 (ll, lp, lm, lq, lr) => (false, make2
77737
wenzelm
parents: 77736
diff changeset
   409
                (make2 (ll, lp, lm), lq, make2 (lr, if_equal ord p' p, r'))))))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   410
  | del k (Branch3 (l, p, m, q, r)) =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   411
      (case compare k q of
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   412
        LESS =>
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   413
          (case compare k p of
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   414
            LESS =>
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   415
              (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
   416
                (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
   417
              | (p', (true, l')) => (p', (false, case (unmake m, unmake r) of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   418
                  (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
   419
                    make2 (make3 (l', p, ml, mp, mr), q, r)
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   420
                | (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
   421
                    make3 (make2 (l', p, ml), mp, make2 (mm, mq, mr), q, r)
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   422
                | (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
   423
                    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
   424
                      make2 (rm, rq, rr)))))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   425
          | ord =>
77737
wenzelm
parents: 77736
diff changeset
   426
              (case del (if_equal ord NONE k) m of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   427
                (p', (false, m')) =>
77737
wenzelm
parents: 77736
diff changeset
   428
                  (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
   429
              | (p', (true, m')) => (p', (false, case (unmake l, unmake r) of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   430
                  (Branch2 (ll, lp, lr), Branch2 _) =>
77737
wenzelm
parents: 77736
diff changeset
   431
                    make2 (make3 (ll, lp, lr, if_equal ord p' p, m'), q, r)
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   432
                | (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
   433
                    make3 (make2 (ll, lp, lm), lq,
77737
wenzelm
parents: 77736
diff changeset
   434
                      make2 (lr, if_equal ord p' p, m'), q, r)
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   435
                | (_, Branch3 (rl, rp, rm, rq, rr)) =>
77737
wenzelm
parents: 77736
diff changeset
   436
                    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
   437
                      make2 (rm, rq, rr))))))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   438
      | ord =>
77737
wenzelm
parents: 77736
diff changeset
   439
          (case del (if_equal ord NONE k) r of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   440
            (q', (false, r')) =>
77737
wenzelm
parents: 77736
diff changeset
   441
              (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
   442
          | (q', (true, r')) => (q', (false, case (unmake l, unmake m) of
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   443
              (Branch2 _, Branch2 (ml, mp, mr)) =>
77737
wenzelm
parents: 77736
diff changeset
   444
                make2 (l, p, make3 (ml, mp, mr, if_equal ord q' q, r'))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   445
            | (_, 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
   446
                make3 (l, p, make2 (ml, mp, mm), mq,
77737
wenzelm
parents: 77736
diff changeset
   447
                  make2 (mr, if_equal ord q' q, r'))
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   448
            | (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
   449
                make3 (make2 (ll, lp, lm), lq, make2 (lr, p, ml), mp,
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   450
                  make2 (mr, if_equal ord q' q, r'))))))
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   451
  | del k (Size (_, arg)) = del k arg;
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   452
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   453
in
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   454
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   455
fun remove elem set =
77800
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   456
  if member set elem
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   457
  then make_size (size set - 1) (snd (snd (del (SOME elem) set)))
9a30b76a6f60 performance tuning;
wenzelm
parents: 77780
diff changeset
   458
  else set;
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   459
77728
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   460
val subtract = fold_set remove;
b0d3951232ad more operations;
wenzelm
parents: 77725
diff changeset
   461
77722
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   462
end;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   463
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   464
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   465
(* ML pretty-printing *)
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   466
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   467
val _ =
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   468
  ML_system_pp (fn depth => fn _ => fn set =>
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   469
    ML_Pretty.to_polyml
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   470
      (ML_Pretty.enum "," "{" "}" (ML_Pretty.from_polyml o ML_system_pretty) (dest set, depth)));
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   471
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   472
(*final declarations of this structure!*)
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   473
val fold = fold_set;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   474
val fold_rev = fold_rev_set;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   475
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   476
end;
8faf28a80a7f efficient representation of sets: more compact than Table.set;
wenzelm
parents:
diff changeset
   477
77731
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77728
diff changeset
   478
structure Intset = Set(Inttab.Key);
77805
66779a752f10 more Set() and Table() instances;
wenzelm
parents: 77802
diff changeset
   479
structure Intset' = Set(Inttab'.Key);
77731
48fbecc8fab1 tuned signature: more uniform structure Key;
wenzelm
parents: 77728
diff changeset
   480
structure Symset = Set(Symtab.Key);