Debian Codegraph @

lib/mlrisc-lib/MLRISC/library/dynamic-bitset.sml

raw
1 (*
2 * Growable bitset.
3 *
4 * -- Allen
5 *)
6
7 structure DynamicBitSet :> BITSET =
8 struct
9
10 structure A = Word8Array
11 structure W = Word8
12 open A
13
14 infix << >> & ||
15 infix sub
16
17 type bitset = array ref
18
19 val word = Word.fromInt
20 val int = Word.toInt
21 val op & = Word.andb
22 val op >> = Word.>>
23 val op << = W.<<
24
25 fun create n = ref(array((n+7)div 8, 0wx0))
26
27 fun size a = length(! a) * 8
28
29 fun grow (r as ref a, i) =
30 let val new_size = Int.max(length a * 2, i)
31 val new_array = array(new_size, 0wx0)
32 val _ = copy { src = a, si = 0, dst = new_array, di = 0,
33 len = NONE }
34 in r := new_array
35 end
36
37 fun set (r as ref a, i) =
38 let val byte = int((word i) >> 0w3)
39 val mask = W.<< (0w1, (word i) & 0w7)
40 in update(a, byte, W.orb(a sub byte, mask)) end
41 handle Subscript => (grow (r, i+1); set(r,i))
42
43 fun reset (r as ref a, i) =
44 let val byte = int((word i) >> 0w3)
45 val mask = W.notb(W.<< (0w1, (word i) & 0w7))
46 in update(a, byte, W.andb(a sub byte, mask)) end
47 handle Subscript => ()
48
49 fun clear (ref a) = modify (fn _ => 0wx0) a
50
51 fun negate (ref a) = ref(tabulate (length a, fn i => W.notb(a sub i)))
52
53 fun union (ref a, ref b) =
54 let val m = Int.max(length a, length b)
55 val n = Int.min(length a, length b)
56 val c = array(m, 0wx0)
57 fun f ~1 = ()
58 | f i = update(c, i, W.orb(a sub i, b sub i))
59 in f n;
60 copy { src = if length a > length b then a else b,
61 si = n, dst = c, di = n, len = NONE };
62 ref c
63 end
64
65 fun intersect (ref a, ref b) =
66 let val n = Int.min(length a, length b)
67 val c = array(n, 0wx0)
68 fun f ~1 = ()
69 | f i = update(c, i, W.andb(a sub i, b sub i))
70 in f n;
71 ref c
72 end
73
74 fun diff (ref a, ref b) =
75 let val m = length a
76 val c = array(m, 0wx0)
77 fun f ~1 = ()
78 | f i = update(c, i, W.andb(a sub i, W.notb(b sub i)))
79 in f m; ref c
80 end
81
82 fun unionWith (r as ref a, ref b) =
83 (if length b > length a then grow(r, length b) else ();
84 modifyi (fn (i,x) => W.orb(x,b sub i)) (a, 0, NONE))
85
86 fun intersectWith (ref a, ref b) =
87 modifyi (fn (i,x) => W.andb(x,b sub i)) (a, 0, NONE)
88
89 fun diffWith (ref a, ref b) =
90 modifyi (fn (i,x) => W.andb(x,W.notb(b sub i))) (a, 0, NONE)
91
92 fun complement (ref a) = modify W.notb a
93
94 fun copy (ref a) = ref(tabulate (length a, fn i => a sub i))
95
96 fun toString (ref a) =
97 let fun f i = if i < length a then W.toString(a sub i)::f(i+1) else []
98 val s = String.concat(f 0)
99 in "[" ^ s ^ "]" end
100
101 fun contains (r as ref a, i) =
102 let val byte = int((word i) >> 0w3)
103 val mask = W.<<(0w1, (word i) & 0w7)
104 in W.andb(A.sub(a, byte), mask) <> 0wx0 end
105 handle Subscript => false
106
107 fun markAndTest (r as ref a, i) =
108 let val byte = int((word i) >> 0w3)
109 val mask = W.<<(0w1, (word i) & 0w7)
110 val word = A.sub(a,byte)
111 in if W.andb(word, mask) <> 0wx0 then
112 true
113 else
114 (A.update(a, byte, W.orb(word, mask)); false)
115 end handle Subscript => (grow (r, i+1); markAndTest(r,i))
116
117 fun unmarkAndTest (r as ref a, i) =
118 let val byte = int(word i >> 0w3)
119 val mask = W.<<(0w1, (word i) & 0w7)
120 val word = A.sub(a,byte)
121 in if W.andb(word, mask) <> 0wx0 then
122 (A.update(a, byte, W.andb(word,W.notb mask)); true)
123 else
124 false
125 end handle Subscript => false
126
127 end
128