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