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