src/Pure/General/bitset.ML
author wenzelm
Thu, 02 Jan 2025 16:59:42 +0100
changeset 81710 c914db7419a3
parent 80809 4a64fc4d1cde
permissions -rw-r--r--
misc tuning and clarification: more explicit types; proper normal form for repeated text entries;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/General/bitset.ML
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
     3
79080
2c457c4cd486 misc tuning and clarification;
wenzelm
parents: 79079
diff changeset
     4
Compact representation of sets of integers.
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
     5
*)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
     6
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
     7
signature BITSET =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
     8
sig
79080
2c457c4cd486 misc tuning and clarification;
wenzelm
parents: 79079
diff changeset
     9
  type elem = int
79077
6b071d4041d5 tuned signature;
wenzelm
parents: 79075
diff changeset
    10
  val make_elem: int * int -> elem  (*exception*)
6b071d4041d5 tuned signature;
wenzelm
parents: 79075
diff changeset
    11
  val dest_elem: elem -> int * int
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    12
  type T
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    13
  val empty: T
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    14
  val build: (T -> T) -> T
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    15
  val is_empty: T -> bool
79075
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    16
  val fold: (elem -> 'a -> 'a) -> T -> 'a -> 'a
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    17
  val fold_rev: (elem -> 'a -> 'a) -> T -> 'a -> 'a
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    18
  val dest: T -> elem list
79079
01f9128ec655 more compact representation;
wenzelm
parents: 79077
diff changeset
    19
  val is_unique: T -> bool
79075
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    20
  val min: T -> elem option
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    21
  val max: T -> elem option
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    22
  val get_first: (elem -> 'a option) -> T -> 'a option
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    23
  val exists: (elem -> bool) -> T -> bool
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    24
  val forall: (elem -> bool) -> T -> bool
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    25
  val member: T -> elem -> bool
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    26
  val subset: T * T -> bool
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    27
  val eq_set: T * T -> bool
79075
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    28
  val insert: elem -> T -> T
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    29
  val make: elem list -> T
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    30
  val merge: T * T -> T
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    31
  val merges: T list -> T
79075
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    32
  val remove: elem -> T -> T
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    33
  val subtract: T -> T -> T
79075
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    34
  val restrict: (elem -> bool) -> T -> T
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    35
  val inter: T -> T -> T
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    36
  val union: T -> T -> T
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    37
end;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    38
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    39
structure Bitset: BITSET =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    40
struct
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    41
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    42
(* bits and words *)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    43
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    44
exception BAD of int;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    45
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    46
val word_size = Word.wordSize;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    47
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    48
val min_bit = 0;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    49
val max_bit = word_size - 1;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    50
fun check_bit n = min_bit <= n andalso n <= max_bit;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    51
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    52
fun make_bit n = if check_bit n then Word.<< (0w1, Word.fromInt n) else raise BAD n;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    53
val mimimum_bit = make_bit min_bit;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    54
val maximum_bit = make_bit max_bit;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    55
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    56
fun add_bits v w = Word.orb (v, w);
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    57
fun del_bits v w = Word.andb (Word.notb v, w);
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    58
fun incl_bits v w = add_bits v w = w;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    59
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    60
fun fold_bits f w =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    61
  let
79080
2c457c4cd486 misc tuning and clarification;
wenzelm
parents: 79079
diff changeset
    62
    fun app n b a = if incl_bits b w then f n a else a;
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    63
    fun bits n b a =
79080
2c457c4cd486 misc tuning and clarification;
wenzelm
parents: 79079
diff changeset
    64
      if n = max_bit then app n b a
2c457c4cd486 misc tuning and clarification;
wenzelm
parents: 79079
diff changeset
    65
      else bits (n + 1) (Word.<< (b, 0w1)) (app n b a);
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    66
  in bits min_bit mimimum_bit end;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    67
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    68
fun fold_rev_bits f w =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    69
  let
79080
2c457c4cd486 misc tuning and clarification;
wenzelm
parents: 79079
diff changeset
    70
    fun app n b a = if incl_bits b w then f n a else a;
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    71
    fun bits n b a =
79080
2c457c4cd486 misc tuning and clarification;
wenzelm
parents: 79079
diff changeset
    72
      if n = min_bit then app n b a
2c457c4cd486 misc tuning and clarification;
wenzelm
parents: 79079
diff changeset
    73
      else bits (n - 1) (Word.>> (b, 0w1)) (app n b a);
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    74
  in bits max_bit maximum_bit end;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    75
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    76
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    77
(* datatype *)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    78
79080
2c457c4cd486 misc tuning and clarification;
wenzelm
parents: 79079
diff changeset
    79
type elem = int;
79075
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    80
79077
6b071d4041d5 tuned signature;
wenzelm
parents: 79075
diff changeset
    81
fun make_elem (m, n) : elem = if check_bit n then m * word_size + n else raise BAD n;
79075
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    82
fun dest_elem (x: elem) = Integer.div_mod x word_size;
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    83
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    84
datatype T = Bitset of word Inttab.table;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    85
79075
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    86
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    87
(* empty *)
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
    88
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    89
val empty = Bitset Inttab.empty;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    90
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    91
fun build (f: T -> T) = f empty;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    92
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    93
fun is_empty (Bitset t) = Inttab.is_empty t;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    94
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    95
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    96
(* fold combinators *)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    97
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    98
fun fold_set f (Bitset t) =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
    99
  Inttab.fold (fn (m, w) =>
79077
6b071d4041d5 tuned signature;
wenzelm
parents: 79075
diff changeset
   100
    (if m < 0 then fold_rev_bits else fold_bits) (fn n => f (make_elem (m, n))) w) t;
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   101
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   102
fun fold_rev_set f (Bitset t) =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   103
  Inttab.fold_rev (fn (m, w) =>
79077
6b071d4041d5 tuned signature;
wenzelm
parents: 79075
diff changeset
   104
    (if m < 0 then fold_bits else fold_rev_bits) (fn n => f (make_elem (m, n))) w) t;
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   105
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   106
val dest = Library.build o fold_rev_set cons;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   107
79079
01f9128ec655 more compact representation;
wenzelm
parents: 79077
diff changeset
   108
fun is_unique (set as Bitset t) =
01f9128ec655 more compact representation;
wenzelm
parents: 79077
diff changeset
   109
  is_empty set orelse
01f9128ec655 more compact representation;
wenzelm
parents: 79077
diff changeset
   110
  Inttab.size t = 1 andalso fold_set (fn _ => Integer.add 1) set 0 = 1;
01f9128ec655 more compact representation;
wenzelm
parents: 79077
diff changeset
   111
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   112
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   113
(* min/max entries *)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   114
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   115
fun min (Bitset t) =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   116
  Inttab.min t |> Option.map (fn (m, w) =>
79077
6b071d4041d5 tuned signature;
wenzelm
parents: 79075
diff changeset
   117
    make_elem (m, fold_bits Integer.min w max_bit));
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   118
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   119
fun max (Bitset t) =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   120
  Inttab.max t |> Option.map (fn (m, w) =>
79077
6b071d4041d5 tuned signature;
wenzelm
parents: 79075
diff changeset
   121
    make_elem (m, fold_bits Integer.max w min_bit));
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   122
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   123
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   124
(* linear search *)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   125
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   126
fun get_first f set =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   127
  let exception FOUND of 'a in
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   128
    fold_set (fn x => fn a => (case f x of SOME b => raise FOUND b | NONE => a)) set NONE
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   129
      handle FOUND b => SOME b
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   130
  end;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   131
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   132
fun exists pred = is_some o get_first (fn x => if pred x then SOME x else NONE);
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   133
fun forall pred = not o exists (not o pred);
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   134
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   135
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   136
(* member *)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   137
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   138
fun member (Bitset t) x =
79075
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
   139
  let val (m, n) = dest_elem x in
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   140
    (case Inttab.lookup t m of
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   141
      NONE => false
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   142
    | SOME w => incl_bits (make_bit n) w)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   143
  end;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   144
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   145
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   146
(* subset *)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   147
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   148
fun subset (Bitset t1, Bitset t2) =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   149
  pointer_eq (t1, t2) orelse
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   150
    Inttab.size t1 <= Inttab.size t2 andalso
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   151
    t1 |> Inttab.forall (fn (m, w1) =>
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   152
      (case Inttab.lookup t2 m of
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   153
        NONE => false
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   154
      | SOME w2 => incl_bits w1 w2));
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   155
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   156
fun eq_set (set1, set2) =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   157
  pointer_eq (set1, set2) orelse subset (set1, set2) andalso subset (set2, set1);
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   158
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   159
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   160
(* insert *)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   161
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   162
fun insert x (Bitset t) =
79075
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
   163
  let val (m, n) = dest_elem x
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   164
  in Bitset (Inttab.map_default (m, 0w0) (add_bits (make_bit n)) t) end;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   165
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   166
fun make xs = build (fold insert xs);
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   167
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   168
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   169
(* merge *)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   170
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   171
fun join_bits (w1, w2) =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   172
  let val w = add_bits w2 w1
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   173
  in if w = w1 then raise Inttab.SAME else w end;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   174
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   175
fun merge (set1 as Bitset t1, set2 as Bitset t2) =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   176
  if pointer_eq (set1, set2) then set1
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   177
  else if is_empty set1 then set2
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   178
  else if is_empty set2 then set1
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   179
  else Bitset (Inttab.join (K join_bits) (t1, t2));
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   180
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   181
fun merges sets = Library.foldl merge (empty, sets);
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   182
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   183
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   184
(* remove *)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   185
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   186
fun remove x (set as Bitset t) =
79075
e671abce8f8e tuned signature;
wenzelm
parents: 79074
diff changeset
   187
  let val (m, n) = dest_elem x in
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   188
    (case Inttab.lookup t m of
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   189
      NONE => set
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   190
    | SOME w =>
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   191
        let val w' = del_bits (make_bit n) w in
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   192
          if w = w' then set
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   193
          else if w' = 0w0 then Bitset (Inttab.delete m t)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   194
          else Bitset (Inttab.update (m, w') t)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   195
        end)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   196
  end;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   197
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   198
val subtract = fold_set remove;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   199
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   200
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   201
(* conventional set operations *)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   202
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   203
fun restrict pred set =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   204
  fold_set (fn x => not (pred x) ? remove x) set set;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   205
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   206
fun inter set1 set2 =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   207
  if pointer_eq (set1, set2) then set1
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   208
  else if is_empty set1 orelse is_empty set2 then empty
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   209
  else restrict (member set1) set2;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   210
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   211
fun union set1 set2 = merge (set2, set1);
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   212
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   213
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   214
(* ML pretty-printing *)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   215
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   216
val _ =
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   217
  ML_system_pp (fn depth => fn _ => fn set =>
80809
4a64fc4d1cde clarified signature: type ML_Pretty.pretty coincides with PolyML.pretty;
wenzelm
parents: 79080
diff changeset
   218
    ML_Pretty.enum "," "{" "}" ML_system_pretty (dest set, depth));
79074
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   219
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   220
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   221
(*final declarations of this structure!*)
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   222
val fold = fold_set;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   223
val fold_rev = fold_rev_set;
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   224
7f24c5be57bd compact representation of sets of integers;
wenzelm
parents:
diff changeset
   225
end;