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