25package umontreal.ssj.util;
27import java.io.Serializable;
45public class BitVector implements Serializable, Cloneable {
47 static final long serialVersionUID = -3448233092524725148L;
52 private final static int all_1 = -1;
53 private final static int one_1 = 1;
69 v =
new int[(length + 31) / 32];
70 for (
int i = 0; i < v.length; i++)
87 if (((length + 31) / 32) != vect.length)
88 throw new IllegalArgumentException(
"The int[] length must be equal to the (length + 31) / 32");
91 v =
new int[vect.length];
92 for (
int i = 0; i < vect.length; i++)
96 v[v.length - 1] &= (all_1 >>> (31 - (length - 1) % 32));
106 this(vect, vect.length * 32);
115 this.length = that.length;
116 this.v =
new int[that.v.length];
117 for (
int i = 0; i < that.v.length; i++)
118 this.v[i] = that.v[i];
129 c.v = (
int[]) v.clone();
131 }
catch (CloneNotSupportedException e) {
132 IllegalStateException ne =
new IllegalStateException();
146 if (this.length != that.length)
148 for (
int i = 0; i < this.v.length; i++)
149 if (this.v[i] != that.v[i])
173 throw new NegativeArraySizeException(
"The BitVector must have a non-negative size");
174 if (filling && (length % 32) != 0)
175 v[v.length - 1] ^= all_1 << (length % 32);
176 if ((
size + 31) / 32 != v.length) {
177 int[] new_v =
new int[(
size + 31) / 32];
179 for (i = 0; i < new_v.length && i < v.length; i++)
181 for (; i < new_v.length; i++)
182 new_v[i] = filling ? all_1 : 0;
188 v[v.length - 1] &= (all_1 >>> (31 - (length - 1) % 32));
212 if (pos < 0 || pos >= length)
213 throw new ArrayIndexOutOfBoundsException(pos);
214 return (v[pos >>> 5] & (one_1 << (pos & 31))) != 0;
227 if (pos > length || pos < 0)
228 throw new ArrayIndexOutOfBoundsException(pos);
230 v[pos / 32] |= (one_1 << (pos % 32));
232 v[pos / 32] &= (one_1 << (pos % 32)) ^ all_1;
246 if (pos >= v.length || pos < 0)
247 throw new ArrayIndexOutOfBoundsException(pos);
259 StringBuffer sb =
new StringBuffer();
261 for (
int i = length - 1; i > 0; i--)
262 sb.append(
getBool(i) ?
"1" :
"0").append(i % 8 == 0 ?
" " :
"");
263 sb.append(
getBool(0) ?
"1" :
"0");
265 return sb.toString();
278 for (
int i = 0; i < v.length; i++)
279 bv.v[i] = v[i] ^ all_1;
282 bv.v[v.length - 1] &= (all_1 >>> (31 - (length - 1) % 32));
293 for (
int i = 0; i < v.length; i++)
297 v[v.length - 1] &= (all_1 >>> (31 - (length - 1) % 32));
314 if (that.length >
this.length)
315 return that.
xor(
this);
319 int max = this.v.length;
320 int min = that.v.length;
322 for (
int i = 0; i < min; i++)
323 bv.v[i] =
this.v[i] ^ that.v[i];
324 for (
int i = min; i < max; i++)
339 if (this.length < that.length)
342 int min = that.v.length;
344 for (
int i = 0; i < min; i++)
345 this.v[i] ^= that.v[i];
361 if (that.length >
this.length)
362 return that.
and(
this);
366 int max = this.v.length;
367 int min = that.v.length;
369 for (
int i = 0; i < min; i++)
370 bv.v[i] =
this.v[i] & that.v[i];
371 if (that.length % 32 != 0)
372 bv.v[min - 1] |= this.v[min - 1] & (all_1 << (that.length % 32));
373 for (
int i = min; i < max; i++)
388 if (this.length < that.length)
389 this.
enlarge(that.length,
true);
391 int min = that.v.length;
393 for (
int i = 0; i < min - 1; i++)
394 this.v[i] &= that.v[i];
395 this.v[min - 1] &= (that.v[min - 1] | (all_1 << (that.length % 32)));
411 if (that.length >
this.length)
412 return that.
or(
this);
416 int max = this.v.length;
417 int min = that.v.length;
419 for (
int i = 0; i < min; i++)
420 bv.v[i] =
this.v[i] | that.v[i];
421 for (
int i = min; i < max; i++)
436 if (this.length < that.length)
439 int min = that.v.length;
441 for (
int i = 0; i < min; i++)
442 this.v[i] |= that.v[i];
468 for (i = 0; i + a < v.length; i++)
469 bv.v[i] = v[i + a] >>> b;
471 for (i = 0; i + a + 1 < v.length; i++)
472 bv.v[i] ^= v[i + a + 1] << c;
481 for (i = a; i < v.length; i++)
482 bv.v[i] ^= v[i - a] << b;
484 for (i = a + 1; i < v.length; i++)
485 bv.v[i] ^= v[i - a - 1] >>> c;
502 else if (j > length || j < -length) {
503 for (
int i = 0; i < v.length; i++)
511 for (i = 0; i + a + 1 < v.length; i++) {
512 v[i] = v[i + a] >>> b;
514 v[i] ^= v[i + a + 1] << c;
516 v[i] = v[i + a] >>> b;
517 for (i += 1; i < v.length; i++)
527 for (i = v.length - 1; i > a; i--) {
528 v[i] = v[i - a] << b;
530 v[i] ^= v[i - a - 1] >>> c;
532 v[i] = v[i - a] << b;
533 for (i -= 1; i >= 0; i--)
551 if (that.v.length >
this.v.length)
554 boolean result =
false;
557 for (
int i = 0; i < that.v.length; i++) {
558 prod = this.v[i] & that.v[i];
int getInt(int pos)
Returns an int containing all the bits in the interval.
Object clone()
Creates a copy of the BitVector.
BitVector selfNot()
Applies the not operator on the current BitVector and returns it.
boolean equals(BitVector that)
Verifies if two BitVector’s have the same length and the same data.
BitVector(int length)
Creates a new BitVector of length length with all its bits set to 0.
BitVector selfXor(BitVector that)
Applies the xor operator on this with that.
void enlarge(int size)
Resizes the BitVector so that its length is equal to size.
BitVector(int[] vect)
Creates a new BitVector using the data in vect.
int size()
Returns the length of the BitVector.
BitVector xor(BitVector that)
Returns a BitVector which is the result of the xor operator applied on this and that.
BitVector shift(int j)
Returns a BitVector equal to the original with all the bits shifted j positions to the right if j is ...
boolean scalarProduct(BitVector that)
Returns the scalar product of two BitVector’s modulo 2.
BitVector selfShift(int j)
Shift all the bits of the current BitVector j positions to the right if j is positive,...
BitVector and(BitVector that)
Returns a BitVector which is the result of the and operator with both the this and that BitVector’s.
void enlarge(int size, boolean filling)
Resizes the BitVector so that its length is equal to size.
String toString()
Returns a string containing all the bits of the BitVector, starting with the highest order bit and fi...
boolean getBool(int pos)
Gives the value of the bit in position pos.
void setBool(int pos, boolean value)
Sets the value of the bit in position pos.
BitVector selfOr(BitVector that)
Applies the or operator on this with that.
BitVector or(BitVector that)
Returns a BitVector which is the result of the or operator with both the this and that BitVector’s.
BitVector not()
Returns a BitVector which is the result of the not operator on the current BitVector.
BitVector selfAnd(BitVector that)
Applies the and operator on this with that.
BitVector(BitVector that)
Creates a copy of the BitVector that.
BitVector(int[] vect, int length)
Creates a new BitVector of length length using the data in vect.