예제 #1
0
        public virtual void TestEquals()
        {
            // this test can't handle numBits==0:
            int         numBits = Random.Next(2000) + 1;
            Int64BitSet b1      = new Int64BitSet(numBits);
            Int64BitSet b2      = new Int64BitSet(numBits);

            Assert.IsTrue(b1.Equals(b2));
            Assert.IsTrue(b2.Equals(b1));
            for (int iter = 0; iter < 10 * RandomMultiplier; iter++)
            {
                int idx = Random.Next(numBits);
                if (!b1.Get(idx))
                {
                    b1.Set(idx);
                    Assert.IsFalse(b1.Equals(b2));
                    Assert.IsFalse(b2.Equals(b1));
                    b2.Set(idx);
                    Assert.IsTrue(b1.Equals(b2));
                    Assert.IsTrue(b2.Equals(b1));
                }
            }

            // try different type of object
            Assert.IsFalse(b1.Equals(new object()));
        }
예제 #2
0
        public virtual void TestEnsureCapacity()
        {
            Int64BitSet bits = new Int64BitSet(5);

            bits.Set(1);
            bits.Set(4);

            Int64BitSet newBits = Int64BitSet.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 = Int64BitSet.EnsureCapacity(newBits, newBits.Length - 2); // reuse
            Assert.IsTrue(newBits.Get(1));

            bits.Set(1);
            newBits = Int64BitSet.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));
        }
예제 #3
0
        private void CheckNextSetBitArray(int[] a, int numBits)
        {
            Int64BitSet obs = MakeLongFixedBitSet(a, numBits);
            BitSet      bs  = MakeBitSet(a);

            DoNextSetBit(bs, obs);
        }
예제 #4
0
        public void TestClearLarge()
        {
            Random random = Random;
            int    iters  = AtLeast(1000);

            for (int it = 0; it < iters; it++)
            {
                int         sz   = AtLeast(1200);
                Int64BitSet a    = new Int64BitSet(sz);
                Int64BitSet b    = new Int64BitSet(sz);
                int         from = random.Next(sz - 1);
                int         to   = random.Next(from, sz);

                for (int i = 0; i < sz / 2; i++)
                {
                    int index = random.Next(sz - 1);
                    a.Set(index);

                    if (index < from || index >= to)
                    {
                        b.Set(index);
                    }
                }

                a.Clear(from, to);
                Assert.True(a.Equals(b));
            }
        }
예제 #5
0
        public void TestClearSmall()
        {
            Int64BitSet a = new Int64BitSet(30);   // 0110010111001000101101001001110...0

            int[] onesA = { 1, 2, 5, 7, 8, 9, 12, 16, 18, 19, 21, 24, 27, 28, 29 };

            for (int i = 0; i < onesA.size(); i++)
            {
                a.Set(onesA[i]);
            }

            Int64BitSet b = new Int64BitSet(30);   // 0110000001001000101101001001110...0

            int[] onesB = { 1, 2, 9, 12, 16, 18, 19, 21, 24, 27, 28, 29 };

            for (int i = 0; i < onesB.size(); i++)
            {
                b.Set(onesB[i]);
            }

            a.Clear(5, 9);
            Assert.True(a.Equals(b));

            a.Clear(9, 10);
            Assert.False(a.Equals(b));

            a.Set(9);
            Assert.True(a.Equals(b));
        }
예제 #6
0
        internal virtual void DoPrevSetBit(BitArray a, Int64BitSet b)
        {
            int  aa = a.Length + 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);
        }
예제 #7
0
        /// <summary>
        /// this = this AND NOT other </summary>
        public void AndNot(Int64BitSet other)
        {
            int pos = Math.Min(numWords, other.bits.Length);

            while (--pos >= 0)
            {
                bits[pos] &= ~other.bits[pos];
            }
        }
예제 #8
0
 public LongRangeBuilderAnonymousClass(long lower, long upper, bool useBitSet, Int64BitSet bits, IEnumerator <long> neededBounds, IEnumerator <int> neededShifts)
 {
     this.lower        = lower;
     this.upper        = upper;
     this.useBitSet    = useBitSet;
     this.bits         = bits;
     this.neededBounds = neededBounds;
     this.neededShifts = neededShifts;
 }
예제 #9
0
 public LongRangeBuilderAnonymousInnerClassHelper(TestNumericUtils outerInstance, long lower, long upper, bool useBitSet, Int64BitSet bits, IEnumerator <long> neededBounds, IEnumerator <int> neededShifts)
 {
     this.OuterInstance = outerInstance;
     this.Lower         = lower;
     this.Upper         = upper;
     this.UseBitSet     = useBitSet;
     this.Bits          = bits;
     this.NeededBounds  = neededBounds;
     this.NeededShifts  = neededShifts;
 }
예제 #10
0
        /// <summary>
        /// this = this XOR other </summary>
        public void Xor(Int64BitSet other)
        {
            Debug.Assert(other.numWords <= numWords, "numWords=" + numWords + ", other.numWords=" + other.numWords);
            int pos = Math.Min(numWords, other.numWords);

            while (--pos >= 0)
            {
                bits[pos] ^= other.bits[pos];
            }
        }
예제 #11
0
        internal virtual void DoNextSetBit(BitArray a, Int64BitSet 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);
        }
예제 #12
0
        internal virtual void DoGet(BitArray a, Int64BitSet 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));
                }
            }
        }
예제 #13
0
        /// <summary>
        /// this = this AND other </summary>
        public void And(Int64BitSet other)
        {
            int pos = Math.Min(numWords, other.numWords);

            while (--pos >= 0)
            {
                bits[pos] &= other.bits[pos];
            }
            if (numWords > other.numWords)
            {
                Arrays.Fill(bits, other.numWords, numWords, 0L);
            }
        }
예제 #14
0
        /// <summary>
        /// this = this OR other </summary>
        public void Or(Int64BitSet other)
        {
            if (Debugging.AssertsEnabled)
            {
                Debugging.Assert(other.numWords <= numWords, () => "numWords=" + numWords + ", other.numWords=" + other.numWords);
            }
            int pos = Math.Min(numWords, other.numWords);

            while (--pos >= 0)
            {
                bits[pos] |= other.bits[pos];
            }
        }
예제 #15
0
        /// <summary>
        /// this = this XOR other </summary>
        public void Xor(Int64BitSet other)
        {
            if (Debugging.AssertsEnabled)
            {
                Debugging.Assert(other.numWords <= numWords, "numWords={0}, other.numWords={1}", numWords, other.numWords);
            }
            int pos = Math.Min(numWords, other.numWords);

            while (--pos >= 0)
            {
                bits[pos] ^= other.bits[pos];
            }
        }
예제 #16
0
        /// <summary>
        /// Returns <c>true</c> if the sets have any elements in common </summary>
        public bool Intersects(Int64BitSet other)
        {
            int pos = Math.Min(numWords, other.numWords);

            while (--pos >= 0)
            {
                if ((bits[pos] & other.bits[pos]) != 0)
                {
                    return(true);
                }
            }
            return(false);
        }
예제 #17
0
        // INFO: Tests for trieCodeLong()/trieCodeInt() not needed because implicitely tested by range filter tests

        /// <summary>
        /// Note: The neededBounds Iterable must be unsigned (easier understanding what's happening) </summary>
        private void AssertLongRangeSplit(long lower, long upper, int precisionStep, bool useBitSet, IEnumerable <long> expectedBounds, IEnumerable <int> expectedShifts)
        {
            // Cannot use FixedBitSet since the range could be long:
            Int64BitSet        bits         = useBitSet ? new Int64BitSet(upper - lower + 1) : null;
            IEnumerator <long> neededBounds = (expectedBounds == null) ? null : expectedBounds.GetEnumerator();
            IEnumerator <int>  neededShifts = (expectedShifts == null) ? null : expectedShifts.GetEnumerator();

            NumericUtils.SplitInt64Range(new LongRangeBuilderAnonymousInnerClassHelper(this, lower, upper, useBitSet, bits, neededBounds, neededShifts), precisionStep, lower, upper);

            if (useBitSet)
            {
                // after flipping all bits in the range, the cardinality should be zero
                bits.Flip(0, upper - lower + 1);
                Assert.AreEqual(0, bits.Cardinality(), "The sub-range concenated should match the whole range");
            }
        }
예제 #18
0
 /// <summary>
 /// If the given <see cref="Int64BitSet"/> is large enough to hold
 /// <paramref name="numBits"/>, returns the given <paramref name="bits"/>, otherwise returns a new
 /// <see cref="Int64BitSet"/> which can hold the requested number of bits.
 ///
 /// <para/>
 /// <b>NOTE:</b> the returned bitset reuses the underlying <see cref="T:long[]"/> of
 /// the given <paramref name="bits"/> if possible. Also, reading <see cref="Length"/> on the
 /// returned bits may return a value greater than <paramref name="numBits"/>.
 /// </summary>
 public static Int64BitSet EnsureCapacity(Int64BitSet bits, long numBits)
 {
     if (numBits < bits.Length)
     {
         return(bits);
     }
     else
     {
         int    numWords = Bits2words(numBits);
         long[] arr      = bits.GetBits();
         if (numWords >= arr.Length)
         {
             arr = ArrayUtil.Grow(arr, numWords + 1);
         }
         return(new Int64BitSet(arr, arr.Length << 6));
     }
 }
예제 #19
0
        /// <summary>
        /// Returns <c>true</c> if both sets have the same bits set </summary>
        public override bool Equals(object o)
        {
            if (this == o)
            {
                return(true);
            }
            if (!(o is Int64BitSet))
            {
                return(false);
            }
            Int64BitSet other = (Int64BitSet)o;

            if (numBits != other.Length)
            {
                return(false);
            }
            return(Arrays.Equals(bits, other.bits));
        }
예제 #20
0
 public virtual void TestSmallBitSets()
 {
     // Make sure size 0-10 bit sets are OK:
     for (int numBits = 0; numBits < 10; numBits++)
     {
         Int64BitSet b1 = new Int64BitSet(numBits);
         Int64BitSet b2 = new Int64BitSet(numBits);
         Assert.IsTrue(b1.Equals(b2));
         Assert.AreEqual(b1.GetHashCode(), b2.GetHashCode());
         Assert.AreEqual(0, b1.Cardinality());
         if (numBits > 0)
         {
             b1.Set(0, numBits);
             Assert.AreEqual(numBits, b1.Cardinality());
             b1.Flip(0, numBits);
             Assert.AreEqual(0, b1.Cardinality());
         }
     }
 }
예제 #21
0
        public virtual void TestHashCodeEquals()
        {
            // this test can't handle numBits==0:
            int         numBits = Random().Next(2000) + 1;
            Int64BitSet b1      = new Int64BitSet(numBits);
            Int64BitSet b2      = new Int64BitSet(numBits);

            Assert.IsTrue(b1.Equals(b2));
            Assert.IsTrue(b2.Equals(b1));
            for (int iter = 0; iter < 10 * RANDOM_MULTIPLIER; iter++)
            {
                int idx = Random().Next(numBits);
                if (!b1.Get(idx))
                {
                    b1.Set(idx);
                    Assert.IsFalse(b1.Equals(b2));
                    Assert.IsFalse(b1.GetHashCode() == b2.GetHashCode());
                    b2.Set(idx);
                    Assert.AreEqual(b1, b2);
                    Assert.AreEqual(b1.GetHashCode(), b2.GetHashCode());
                }
            }
        }
예제 #22
0
        private Int64BitSet MakeLongFixedBitSet(int[] a, int numBits)
        {
            Int64BitSet bs;

            if (Random().NextBoolean())
            {
                int    bits2words = Int64BitSet.Bits2words(numBits);
                long[] words      = new long[bits2words + Random().Next(100)];
                for (int i = bits2words; i < words.Length; i++)
                {
                    words[i] = Random().NextLong();
                }
                bs = new Int64BitSet(words, numBits);
            }
            else
            {
                bs = new Int64BitSet(numBits);
            }
            foreach (int e in a)
            {
                bs.Set(e);
            }
            return(bs);
        }
예제 #23
0
        internal virtual void DoRandomSets(int maxSize, int iter, int mode)
        {
            BitArray    a0 = null;
            Int64BitSet b0 = null;

            for (int i = 0; i < iter; i++)
            {
                int         sz = TestUtil.NextInt(Random(), 2, maxSize);
                BitArray    a  = new BitArray(sz);
                Int64BitSet b  = new Int64BitSet(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 = new BitArray(a);
                aa.Flip(fromIndex, toIndex);
                Int64BitSet bb = b.Clone();
                bb.Flip(fromIndex, toIndex);

                fromIndex = Random().Next(sz / 2);
                toIndex   = fromIndex + Random().Next(sz - fromIndex);
                aa        = new BitArray(a);
                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        = new BitArray(a);
                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 = new BitArray(a);
                    a_and = a_and.And_UnequalLengths(a0);
                    BitArray a_or = new BitArray(a);
                    a_or = a_or.Or_UnequalLengths(a0);
                    BitArray a_xor = new BitArray(a);
                    a_xor = a_xor.Xor_UnequalLengths(a0);
                    BitArray a_andn = new BitArray(a);
                    a_andn.AndNot(a0);

                    Int64BitSet b_and = b.Clone();
                    Assert.AreEqual(b, b_and);
                    b_and.And(b0);
                    Int64BitSet b_or = b.Clone();
                    b_or.Or(b0);
                    Int64BitSet b_xor = b.Clone();
                    b_xor.Xor(b0);
                    Int64BitSet 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;
            }
        }
예제 #24
0
 public static long Cardinality(this Int64BitSet set)
 {
     return(set.Cardinality);
 }