internal virtual void DoPrevSetBit(BitArray a, LongBitSet b) { int aa = a.Count + Random().Next(100); long bb = aa; do { //aa = a.PrevSetBit(aa-1); aa--; while ((aa >= 0) && (!a.SafeGet(aa))) { aa--; } if (b.Length() == 0) { bb = -1; } else if (bb > b.Length() - 1) { bb = b.PrevSetBit(b.Length() - 1); } else if (bb < 1) { bb = -1; } else { bb = bb >= 1 ? b.PrevSetBit(bb - 1) : -1; } Assert.AreEqual(aa, bb); } while (aa >= 0); }
internal virtual void DoPrevSetBit(BitArray a, LongBitSet b) { int aa = a.Count + Random().Next(100); long bb = aa; do { //aa = a.PrevSetBit(aa-1); aa--; while ((aa >= 0) && (!a.SafeGet(aa))) { aa--; } if (b.Length() == 0) { bb = -1; } else if (bb > b.Length() - 1) { bb = b.PrevSetBit(b.Length() - 1); } else if (bb < 1) { bb = -1; } else { bb = bb >= 1 ? b.PrevSetBit(bb - 1) : -1; } Assert.AreEqual(aa, bb); } while (aa >= 0); }
public virtual void TestEnsureCapacity() { LongBitSet bits = new LongBitSet(5); bits.Set(1); bits.Set(4); LongBitSet newBits = LongBitSet.EnsureCapacity(bits, 8); // grow within the word Assert.IsTrue(newBits.Get(1)); Assert.IsTrue(newBits.Get(4)); newBits.Clear(1); // we align to 64-bits, so even though it shouldn't have, it re-allocated a long[1] Assert.IsTrue(bits.Get(1)); Assert.IsFalse(newBits.Get(1)); newBits.Set(1); newBits = LongBitSet.EnsureCapacity(newBits, newBits.Length() - 2); // reuse Assert.IsTrue(newBits.Get(1)); bits.Set(1); newBits = LongBitSet.EnsureCapacity(bits, 72); // grow beyond one word Assert.IsTrue(newBits.Get(1)); Assert.IsTrue(newBits.Get(4)); newBits.Clear(1); // we grew the long[], so it's not shared Assert.IsTrue(bits.Get(1)); Assert.IsFalse(newBits.Get(1)); }
internal virtual void DoNextSetBit(BitArray a, LongBitSet b) { int aa = -1; long bb = -1; do { aa = a.NextSetBit(aa + 1); bb = bb < b.Length() - 1 ? b.NextSetBit(bb + 1) : -1; Assert.AreEqual(aa, bb); } while (aa >= 0); }
internal virtual void DoGet(BitArray a, LongBitSet b) { long max = b.Length(); for (int i = 0; i < max; i++) { if (a.SafeGet(i) != b.Get(i)) { Assert.Fail("mismatch: BitSet=[" + i + "]=" + a.SafeGet(i)); } } }
internal virtual void DoNextSetBit(BitArray a, LongBitSet b) { int aa = -1; long bb = -1; do { aa = a.NextSetBit(aa + 1); bb = bb < b.Length() - 1 ? b.NextSetBit(bb + 1) : -1; Assert.AreEqual(aa, bb); } while (aa >= 0); }
internal virtual void DoGet(BitArray a, LongBitSet b) { long max = b.Length(); for (int i = 0; i < max; i++) { if (a.Get(i) != b.Get(i)) { Assert.Fail("mismatch: BitSet=[" + i + "]=" + a.Get(i)); } } }
/// <summary> /// If the given <seealso cref="LongBitSet"/> is large enough to hold /// {@code numBits}, returns the given bits, otherwise returns a new /// <seealso cref="LongBitSet"/> which can hold the requested number of bits. /// /// <p> /// <b>NOTE:</b> the returned bitset reuses the underlying {@code long[]} of /// the given {@code bits} if possible. Also, calling <seealso cref="#length()"/> on the /// returned bits may return a value greater than {@code numBits}. /// </summary> public static LongBitSet EnsureCapacity(LongBitSet bits, long numBits) { if (numBits < bits.Length()) { return(bits); } else { int numWords = Bits2words(numBits); long[] arr = bits.Bits; if (numWords >= arr.Length) { arr = ArrayUtil.Grow(arr, numWords + 1); } return(new LongBitSet(arr, arr.Length << 6)); } }
/// <summary> /// If the given <seealso cref="LongBitSet"/> is large enough to hold /// {@code numBits}, returns the given bits, otherwise returns a new /// <seealso cref="LongBitSet"/> which can hold the requested number of bits. /// /// <p> /// <b>NOTE:</b> the returned bitset reuses the underlying {@code long[]} of /// the given {@code bits} if possible. Also, calling <seealso cref="#length()"/> on the /// returned bits may return a value greater than {@code numBits}. /// </summary> public static LongBitSet EnsureCapacity(LongBitSet bits, long numBits) { if (numBits < bits.Length()) { return bits; } else { int numWords = Bits2words(numBits); long[] arr = bits.Bits; if (numWords >= arr.Length) { arr = ArrayUtil.Grow(arr, numWords + 1); } return new LongBitSet(arr, arr.Length << 6); } }
/// <summary> /// returns true if both sets have the same bits set </summary> public override bool Equals(object o) { if (this == o) { return(true); } if (!(o is LongBitSet)) { return(false); } LongBitSet other = (LongBitSet)o; if (NumBits != other.Length()) { return(false); } return(Arrays.Equals(bits, other.bits)); }
internal virtual void DoRandomSets(int maxSize, int iter, int mode) { BitArray a0 = null; LongBitSet b0 = null; for (int i = 0; i < iter; i++) { int sz = TestUtil.NextInt(Random(), 2, maxSize); BitArray a = new BitArray(sz); LongBitSet b = new LongBitSet(sz); // test the various ways of setting bits if (sz > 0) { int nOper = Random().Next(sz); for (int j = 0; j < nOper; j++) { int idx; idx = Random().Next(sz); a.SafeSet(idx, true); b.Set(idx); idx = Random().Next(sz); a.SafeSet(idx, false); b.Clear(idx); idx = Random().Next(sz); a.SafeSet(idx, !a.SafeGet(idx)); b.Flip(idx, idx + 1); idx = Random().Next(sz); a.SafeSet(idx, !a.SafeGet(idx)); b.Flip(idx, idx + 1); bool val2 = b.Get(idx); bool val = b.GetAndSet(idx); Assert.IsTrue(val2 == val); Assert.IsTrue(b.Get(idx)); if (!val) { b.Clear(idx); } Assert.IsTrue(b.Get(idx) == val); } } // test that the various ways of accessing the bits are equivalent DoGet(a, b); // test ranges, including possible extension int fromIndex, toIndex; fromIndex = Random().Next(sz / 2); toIndex = fromIndex + Random().Next(sz - fromIndex); BitArray aa = (BitArray)a.Clone(); aa.Flip(fromIndex, toIndex); LongBitSet bb = b.Clone(); bb.Flip(fromIndex, toIndex); fromIndex = Random().Next(sz / 2); toIndex = fromIndex + Random().Next(sz - fromIndex); aa = (BitArray)a.Clone(); aa.Clear(fromIndex, toIndex); bb = b.Clone(); bb.Clear(fromIndex, toIndex); DoNextSetBit(aa, bb); // a problem here is from clear() or nextSetBit DoPrevSetBit(aa, bb); fromIndex = Random().Next(sz / 2); toIndex = fromIndex + Random().Next(sz - fromIndex); aa = (BitArray)a.Clone(); aa.Set(fromIndex, toIndex); bb = b.Clone(); bb.Set(fromIndex, toIndex); DoNextSetBit(aa, bb); // a problem here is from set() or nextSetBit DoPrevSetBit(aa, bb); if (b0 != null && b0.Length() <= b.Length()) { Assert.AreEqual(a.Cardinality(), b.Cardinality()); BitArray a_and = (BitArray)a.Clone(); a_and = a_and.And_UnequalLengths(a0); BitArray a_or = (BitArray)a.Clone(); a_or = a_or.Or_UnequalLengths(a0); BitArray a_xor = (BitArray)a.Clone(); a_xor = a_xor.Xor_UnequalLengths(a0); BitArray a_andn = (BitArray)a.Clone(); a_andn.AndNot(a0); LongBitSet b_and = b.Clone(); Assert.AreEqual(b, b_and); b_and.And(b0); LongBitSet b_or = b.Clone(); b_or.Or(b0); LongBitSet b_xor = b.Clone(); b_xor.Xor(b0); LongBitSet b_andn = b.Clone(); b_andn.AndNot(b0); Assert.AreEqual(a0.Cardinality(), b0.Cardinality()); Assert.AreEqual(a_or.Cardinality(), b_or.Cardinality()); Assert.AreEqual(a_and.Cardinality(), b_and.Cardinality()); Assert.AreEqual(a_or.Cardinality(), b_or.Cardinality()); Assert.AreEqual(a_xor.Cardinality(), b_xor.Cardinality()); Assert.AreEqual(a_andn.Cardinality(), b_andn.Cardinality()); } a0 = a; b0 = b; } }
internal virtual void DoRandomSets(int maxSize, int iter, int mode) { BitArray a0 = null; LongBitSet b0 = null; for (int i = 0; i < iter; i++) { int sz = TestUtil.NextInt(Random(), 2, maxSize); BitArray a = new BitArray(sz); LongBitSet b = new LongBitSet(sz); // test the various ways of setting bits if (sz > 0) { int nOper = Random().Next(sz); for (int j = 0; j < nOper; j++) { int idx; idx = Random().Next(sz); a.SafeSet(idx, true); b.Set(idx); idx = Random().Next(sz); a.SafeSet(idx, false); b.Clear(idx); idx = Random().Next(sz); a.SafeSet(idx, !a.Get(idx)); b.Flip(idx, idx + 1); idx = Random().Next(sz); a.SafeSet(idx, !a.SafeGet(idx)); b.Flip(idx, idx + 1); bool val2 = b.Get(idx); bool val = b.GetAndSet(idx); Assert.IsTrue(val2 == val); Assert.IsTrue(b.Get(idx)); if (!val) { b.Clear(idx); } Assert.IsTrue(b.Get(idx) == val); } } // test that the various ways of accessing the bits are equivalent DoGet(a, b); // test ranges, including possible extension int fromIndex, toIndex; fromIndex = Random().Next(sz / 2); toIndex = fromIndex + Random().Next(sz - fromIndex); BitArray aa = (BitArray)a.Clone(); aa.Flip(fromIndex, toIndex); LongBitSet bb = b.Clone(); bb.Flip(fromIndex, toIndex); fromIndex = Random().Next(sz / 2); toIndex = fromIndex + Random().Next(sz - fromIndex); aa = (BitArray)a.Clone(); aa.Clear(fromIndex, toIndex); bb = b.Clone(); bb.Clear(fromIndex, toIndex); DoNextSetBit(aa, bb); // a problem here is from clear() or nextSetBit DoPrevSetBit(aa, bb); fromIndex = Random().Next(sz / 2); toIndex = fromIndex + Random().Next(sz - fromIndex); aa = (BitArray)a.Clone(); aa.Set(fromIndex, toIndex); bb = b.Clone(); bb.Set(fromIndex, toIndex); DoNextSetBit(aa, bb); // a problem here is from set() or nextSetBit DoPrevSetBit(aa, bb); if (b0 != null && b0.Length() <= b.Length()) { Assert.AreEqual(a.Cardinality(), b.Cardinality()); BitArray a_and = (BitArray)a.Clone(); a_and = a_and.And(a0); BitArray a_or = (BitArray)a.Clone(); a_or = a_or.Or(a0); BitArray a_xor = (BitArray)a.Clone(); a_xor = a_xor.Xor(a0); BitArray a_andn = (BitArray)a.Clone(); a_andn.AndNot(a0); LongBitSet b_and = b.Clone(); Assert.AreEqual(b, b_and); b_and.And(b0); LongBitSet b_or = b.Clone(); b_or.Or(b0); LongBitSet b_xor = b.Clone(); b_xor.Xor(b0); LongBitSet b_andn = b.Clone(); b_andn.AndNot(b0); Assert.AreEqual(a0.Cardinality(), b0.Cardinality()); Assert.AreEqual(a_or.Cardinality(), b_or.Cardinality()); Assert.AreEqual(a_and.Cardinality(), b_and.Cardinality()); Assert.AreEqual(a_or.Cardinality(), b_or.Cardinality()); Assert.AreEqual(a_xor.Cardinality(), b_xor.Cardinality()); Assert.AreEqual(a_andn.Cardinality(), b_andn.Cardinality()); } a0 = a; b0 = b; } }