-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBitSet.cs
428 lines (390 loc) · 11.5 KB
/
BitSet.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
namespace BitSet
{
public sealed class BitSet : System.IComparable
{
/*
* Sorted array of bit-block offsets.
*/
int[] offs;
/*
* Array of bit-blocks; each holding BITS bits.
*/
ulong[] bits;
/*
* Number of blocks currently in use.
*/
int inuse;
/*
* log base 2 of BITS, for the identity: x/BITS == x >> LG_BITS
*/
const int LG_BITS = 6;
/*
* Number of bits in a block.
*/
const int BITS = 1 << LG_BITS;
/*
* BITS-1, using the identity: x % BITS == x & (BITS-1)
*/
const int BITS_M1 = BITS - 1;
/*
* Creates an empty set.
*/
public BitSet()
{
bits = new ulong[4];
offs = new int[4];
inuse = 0;
}
/*
* Creates an empty set with the specified size.
*/
public BitSet(int nbits) : this() { }
/*
* Create a bitset and initialize with a specified value
*/
public BitSet(int nbits, bool val)
: this()
{
for (int i = 0; i < nbits; i++)
Set(i, val);
}
/*
* Creates a new set from existing set
*/
public BitSet(BitSet set)
{
bits = new ulong[set.bits.Length];
offs = new int[set.offs.Length];
System.Array.Copy(set.bits, 0, bits, 0, set.bits.Length);
System.Array.Copy(set.offs, 0, offs, 0, set.offs.Length);
inuse = set.inuse;
}
void new_block(int i, int b)
{
if (inuse == bits.Length)
{ // resize
ulong[] nbits = new ulong[inuse + 4];
int[] noffs = new int[inuse + 4];
System.Array.Copy(bits, 0, nbits, 0, inuse);
System.Array.Copy(offs, 0, noffs, 0, inuse);
bits = nbits;
offs = noffs;
}
insert_block(i, b);
}
void insert_block(int i, int b)
{
System.Array.Copy(bits, i, bits, i + 1, inuse - i);
System.Array.Copy(offs, i, offs, i + 1, inuse - i);
offs[i] = b;
bits[i] = 0;
inuse++;
}
int BinarySearch(int[] x, int i, int m, int val)
{
int l = i;
int r = m;
while (l < r)
{
int p = (l + r) / 2;
if (val < x[p])
r = p;
else if (val > x[p])
l = p + 1;
else
return p;
}
return -l; // this is the next elem
}
/*
* Sets a bit.
*/
public void Set(int bit, bool val)
{
int b = bit >> LG_BITS;
int i = BinarySearch(offs, 0, inuse, b);
if (i < 0)
{
i = -i; // bitwise complement
new_block(i, b);
}
else if (i >= inuse || offs[i] != b)
new_block(i, b);
if (val)
bits[i] |= (1UL << (bit & BITS_M1));
else
bits[i] &= ~(1UL << (bit & BITS_M1));
}
/*
* Clears all bits.
*/
public void ClearAll()
{
inuse = 0;
}
/*
* Gets a bit.
*/
public bool Get(int bit)
{
int b = bit >> LG_BITS;
int i = BinarySearch(offs, 0, inuse, b);
if (i < 0)
return false;
if (i >= inuse || offs[i] != b)
return false;
return 0 != (bits[i] & (1UL << (bit & BITS_M1)));
}
delegate ulong BinOp(ulong x, ulong y);
/*
* Logically ANDs this bit set with the specified set of bits.
*/
public void And(BitSet set)
{
binop(this, set, new BinOp(BitSet.AND));
}
/*
* Logically ORs this bit set with the specified set of bits.
*/
public void Or(BitSet set)
{
binop(this, set, new BinOp(BitSet.OR));
}
/*
* Logically XORs this bit set with the specified set of bits.
*/
public void Xor(BitSet set)
{
binop(this, set, new BinOp(BitSet.XOR));
}
static public ulong AND(ulong x, ulong y) { return x & y; }
static public ulong OR(ulong x, ulong y) { return x | y; }
static public ulong XOR(ulong x, ulong y) { return x ^ y; }
void binop(BitSet a, BitSet b, BinOp op)
{
int n_sum = a.inuse + b.inuse;
ulong[] n_bits = new ulong[n_sum];
int[] n_offs = new int[n_sum];
int n_len = 0;
int a_len = a.bits.Length;
int b_len = b.bits.Length;
for (int i = 0, j = 0; i < a_len || j < b_len; )
{
ulong nb; int no;
if (i < a_len && ((j >= b_len) || (a.offs[i] < b.offs[j])))
{
nb = op(a.bits[i], 0); // invoke delegate
no = a.offs[i];
i++;
}
else if (j < b_len && ((i >= a_len) || (a.offs[i] > b.offs[j])))
{
nb = op(0, b.bits[j]); // invoke delegate
no = b.offs[j];
j++;
}
else
{ // equal keys; merge.
nb = op(a.bits[i], b.bits[j]); // invoke delegate
no = a.offs[i];
i++;
j++;
}
if (nb != 0)
{
n_bits[n_len] = nb;
n_offs[n_len] = no;
n_len++;
}
}
if (n_len > 0)
{
a.bits = new ulong[n_len];
a.offs = new int[n_len];
a.inuse = n_len;
System.Array.Copy(n_bits, 0, a.bits, 0, n_len);
System.Array.Copy(n_offs, 0, a.offs, 0, n_len);
}
else
{
bits = new ulong[4];
offs = new int[4];
a.inuse = 0;
}
}
/*
* Gets the hashcode.
*/
const uint prime = 1299827;
public override int GetHashCode()
{
ulong h = prime;
for (int i = 0; i < inuse; i++)
h ^= bits[i] * (ulong)offs[i];
return (int)((h >> 32) ^ h);
}
public int Count
{
get { return GetLength(); }
}
/*
* Calculates and returns the set's size
*/
public int GetLength()
{
if (inuse == 0)
return 0;
return ((1 + offs[inuse - 1]) << LG_BITS);
}
/*
* Compares this object against the specified object.
* obj - the object to commpare with
* returns true if the objects are the same; false otherwise.
*/
public override bool Equals(object obj)
{
if (obj == null)
return false;
if (!(obj is BitSet))
return false;
return Equals(this, (BitSet)obj);
}
/*
* Compares two BitSets for equality.
* return true if the objects are the same; false otherwise.
*/
public static bool Equals(BitSet a, BitSet b)
{
for (int i = 0, j = 0; i < a.inuse || j < b.inuse; )
{
if (i < a.inuse && (j >= b.inuse || a.offs[i] < b.offs[j]))
{
if (a.bits[i++] != 0)
return false;
}
else if (j < b.inuse && (i >= a.inuse || a.offs[i] > b.offs[j]))
{
if (b.bits[j++] != 0)
return false;
}
else
{ // equal keys
if (a.bits[i++] != b.bits[j++])
return false;
}
}
return true;
}
/*
* Provides a compare function for a BitSort, for use with Sort or
* binarysearch.
*/
public int CompareTo(object o)
{
if (!(o is BitSet))
throw new System.ApplicationException("Argument must be a BitSet");
BitSet a = (BitSet)o;
if (inuse < a.inuse)
return -1;
if (inuse > a.inuse)
return 1;
for (int i = 0, j = 0; i < inuse || j < a.inuse; )
{
if (i < inuse && (j >= a.inuse || offs[i] < a.offs[j]))
{
if (bits[i++] != 0)
return -1;
}
else if (j < a.inuse && (i >= inuse || offs[i] > a.offs[j]))
{
if (a.bits[j++] != 0)
return 1;
}
else
{ // equal keys
long val = ((long)a.bits[j++]) - ((long)bits[i++]);
if (val < 0)
return -1;
else if (val > 0)
return 1;
}
}
return 0;
}
class BitSetEnum : System.Collections.IEnumerator
{
int idx = -1;
int bit = BITS;
BitSet p;
public BitSetEnum(BitSet x)
{
p = x;
}
public void Reset()
{
idx = -1;
bit = BITS;
}
public bool MoveNext()
{
advance();
return (idx < p.inuse);
}
public object Current
{
get
{
if (idx < 0)
return null;
int r = bit + (p.offs[idx] << LG_BITS);
return r;
}
}
private void advance()
{
int max = BITS;
if (idx < 0)
{
idx++;
bit = -1;
}
while (idx < p.inuse)
{
ulong val = p.bits[idx];
while (++bit < max)
{
ulong mask = (1UL << bit);
ulong result = val & mask;
if (result != 0)
return;
}
idx++;
bit = -1;
}
}
}
/*
* Return an IEnumerator which represents set bit indices in this BitSet.
*/
public System.Collections.IEnumerator GetEnumerator()
{
return new BitSetEnum(this);
}
/*
* Converts the BitSet to a String.
*/
public override string ToString()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append('{');
foreach (int bit in this)
{
if (sb.Length > 1)
sb.Append(", ");
sb.Append(bit);
}
sb.Append('}');
return sb.ToString();
}
}
}