Esempio n. 1
0
        public virtual int DoConjunctions(int iter, int maxClauses)
        {
            int ret = 0;

            for (int i = 0; i < iter; i++)
            {
                int          nClauses = Random.Next(maxClauses - 1) + 2; // min 2 clauses
                BooleanQuery bq       = new BooleanQuery();
                BitArray     result   = null;
                for (int j = 0; j < nClauses; j++)
                {
                    result = AddClause(bq, result);
                }

                CountingHitCollector hc = Validate ? new MatchingHitCollector(result) : new CountingHitCollector();
                s.Search(bq, hc);
                ret += hc.Sum;

                if (Validate)
                {
                    Assert.AreEqual(result.Cardinality(), hc.Count);
                }
                // System.out.println(hc.getCount());
            }

            return(ret);
        }
Esempio n. 2
0
        public override EliasFanoDocIdSet CopyOf(BitArray bs, int numBits)
        {
            EliasFanoDocIdSet set = new EliasFanoDocIdSet(bs.Cardinality(), numBits - 1);

            set.EncodeFromDisi(new DocIdSetIteratorAnonymousInnerClassHelper(this, bs, numBits));
            return(set);
        }
Esempio n. 3
0
        public virtual void TestCompact()
        {
            BytesRef @ref = new BytesRef();
            int      num  = AtLeast(2);

            for (int j = 0; j < num; j++)
            {
                int       numEntries = 0;
                const int size       = 797;
                BitArray  bits       = new BitArray(size);
                for (int i = 0; i < size; i++)
                {
                    string str;
                    do
                    {
                        str = TestUtil.RandomRealisticUnicodeString(Random, 1000);
                    } while (str.Length == 0);
                    @ref.CopyChars(str);
                    int key = Hash.Add(@ref);
                    if (key < 0)
                    {
                        Assert.IsTrue(bits.SafeGet((-key) - 1));
                    }
                    else
                    {
                        Assert.IsFalse(bits.SafeGet(key));
                        bits.SafeSet(key, true);
                        numEntries++;
                    }
                }
                Assert.AreEqual(Hash.Count, bits.Cardinality());
                Assert.AreEqual(numEntries, bits.Cardinality());
                Assert.AreEqual(numEntries, Hash.Count);
                int[] compact = Hash.Compact();
                Assert.IsTrue(numEntries < compact.Length);
                for (int i = 0; i < numEntries; i++)
                {
                    bits.SafeSet(compact[i], false);
                }
                Assert.AreEqual(0, bits.Cardinality());
                Hash.Clear();
                Assert.AreEqual(0, Hash.Count);
                Hash.Reinit();
            }
        }
        /**
         * Convenience function to assess equality between a compressed BitArray
         * and an uncompressed BitArray
         *
         * @param x the compressed BitArray/bitmap
         * @param y the uncompressed BitArray/bitmap
         */

        private static void AreEqual(EwahCompressedBitArray x, BitArray y)
        {
            Assert.AreEqual(x.GetCardinality(), y.Cardinality());
            var positions = new List <int>();

            for (int ii = 0; ii < y.Count; ii++)
            {
                if (y[ii])
                {
                    positions.Add(ii);
                }
            }
            AreEqual(x.GetPositions(), positions);
        }
Esempio n. 5
0
        //Compares a BitArray with an OpenBitSet
        public static bool Equal(this BitArray a, OpenBitSet b)
        {
            var bitArrayCardinality = a.Cardinality();

            if (bitArrayCardinality != b.Cardinality())
            {
                return(false);
            }

            for (int i = 0; i < bitArrayCardinality; i++)
            {
                if (a.SafeGet(i) != b.Get(i))
                {
                    return(false);
                }
            }

            return(true);
        }
Esempio n. 6
0
        public virtual int DoNestedConjunctions(int iter, int maxOuterClauses, int maxClauses)
        {
            int  ret      = 0;
            long nMatches = 0;

            for (int i = 0; i < iter; i++)
            {
                int          oClauses = Random.Next(maxOuterClauses - 1) + 2;
                BooleanQuery oq       = new BooleanQuery();
                BitArray     result   = null;

                for (int o = 0; o < oClauses; o++)
                {
                    int          nClauses = Random.Next(maxClauses - 1) + 2; // min 2 clauses
                    BooleanQuery bq       = new BooleanQuery();
                    for (int j = 0; j < nClauses; j++)
                    {
                        result = AddClause(bq, result);
                    }

                    oq.Add(bq, Occur.MUST);
                } // outer

                CountingHitCollector hc = Validate ? new MatchingHitCollector(result) : new CountingHitCollector();
                s.Search(oq, hc);
                nMatches += hc.Count;
                ret      += hc.Sum;
                if (Validate)
                {
                    Assert.AreEqual(result.Cardinality(), hc.Count);
                }
                // System.out.println(hc.getCount());
            }
            if (VERBOSE)
            {
                Console.WriteLine("Average number of matches=" + (nMatches / iter));
            }
            return(ret);
        }
Esempio n. 7
0
        private void CheckExpecteds(BitArray expecteds)
        {
            IndexReader r = DirectoryReader.Open(Dir);

            //Perhaps not the most efficient approach but meets our
            //needs here.
            IBits liveDocs = MultiFields.GetLiveDocs(r);

            for (int i = 0; i < r.MaxDoc; i++)
            {
                if (liveDocs == null || liveDocs.Get(i))
                {
                    string sval = r.Document(i).Get(FIELD_RECORD_ID);
                    if (sval != null)
                    {
                        int val = Convert.ToInt32(sval);
                        Assert.IsTrue(expecteds.SafeGet(val), "Did not expect document #" + val);
                        expecteds.SafeSet(val, false);
                    }
                }
            }
            r.Dispose();
            Assert.AreEqual(0, expecteds.Cardinality(), "Should have 0 docs remaining ");
        }
Esempio n. 8
0
        public void TestGetScriptExtensions()
        {
            BitArray scripts = new BitArray(UScript.CodeLimit);

            /* invalid code points */
            if (UScript.GetScriptExtensions(-1, scripts) != UScript.Unknown || scripts.Cardinality() != 1 ||
                !scripts.Get(UScript.Unknown))
            {
                Errln("UScript.getScriptExtensions(-1) is not {UNKNOWN}");
            }
            if (UScript.GetScriptExtensions(0x110000, scripts) != UScript.Unknown || scripts.Cardinality() != 1 ||
                !scripts.Get(UScript.Unknown))
            {
                Errln("UScript.getScriptExtensions(0x110000) is not {UNKNOWN}");
            }

            /* normal usage */
            if (UScript.GetScriptExtensions(0x063f, scripts) != UScript.Arabic || scripts.Cardinality() != 1 ||
                !scripts.Get(UScript.Arabic))
            {
                Errln("UScript.getScriptExtensions(U+063F) is not {ARABIC}");
            }
            if (UScript.GetScriptExtensions(0x0640, scripts) > -3 || scripts.Cardinality() < 3 ||
                !scripts.Get(UScript.Arabic) || !scripts.Get(UScript.Syriac) || !scripts.Get(UScript.Mandaic)
                )
            {
                Errln("UScript.getScriptExtensions(U+0640) failed");
            }
            if (UScript.GetScriptExtensions(0xfdf2, scripts) != -2 || scripts.Cardinality() != 2 ||
                !scripts.Get(UScript.Arabic) || !scripts.Get(UScript.Thaana))
            {
                Errln("UScript.getScriptExtensions(U+FDF2) failed");
            }
            if (UScript.GetScriptExtensions(0xff65, scripts) != -6 || scripts.Cardinality() != 6 ||
                !scripts.Get(UScript.Bopomofo) || !scripts.Get(UScript.Yi))
            {
                Errln("UScript.getScriptExtensions(U+FF65) failed");
            }
        }
Esempio n. 9
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;
            }
        }
Esempio n. 10
0
 public override void AssertEquals(int numBits, BitArray ds1, WAH8DocIdSet ds2)
 {
     base.AssertEquals(numBits, ds1, ds2);
     Assert.AreEqual(ds1.Cardinality(), ds2.Cardinality());
 }
Esempio n. 11
0
 public override long Cost()
 {
     return(Bs.Cardinality());
 }
Esempio n. 12
0
        internal virtual void DoRandomSets(int maxSize, int iter, int mode)
        {
            BitArray   a0 = null;
            OpenBitSet b0 = null;

            for (int i = 0; i < iter; i++)
            {
                int        sz = Random().Next(maxSize);
                BitArray   a  = new BitArray(sz);
                OpenBitSet b  = new OpenBitSet(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.FastSet(idx);

                        idx = Random().Next(sz);
                        a.SafeSet(idx, true);
                        b.FastSet((long)idx);

                        idx = Random().Next(sz);
                        a.SafeSet(idx, false);
                        b.FastClear(idx);

                        idx = Random().Next(sz);
                        a.SafeSet(idx, false);
                        b.FastClear((long)idx);

                        idx = Random().Next(sz);
                        a.SafeSet(idx, !a.SafeGet(idx));
                        b.FastFlip(idx);

                        bool val  = b.FlipAndGet(idx);
                        bool val2 = b.FlipAndGet(idx);
                        Assert.IsTrue(val != val2);

                        idx = Random().Next(sz);
                        a.SafeSet(idx, !a.SafeGet(idx));
                        b.FastFlip((long)idx);

                        val  = b.FlipAndGet((long)idx);
                        val2 = b.FlipAndGet((long)idx);
                        Assert.IsTrue(val != val2);

                        val = b.GetAndSet(idx);
                        Assert.IsTrue(val2 == val);
                        Assert.IsTrue(b.Get(idx));

                        if (!val)
                        {
                            b.FastClear(idx);
                        }
                        Assert.IsTrue(b.Get(idx) == val);
                    }
                }

                // test that the various ways of accessing the bits are equivalent
                DoGet(a, b);
                DoGetFast(a, b, sz);

                // test ranges, including possible extension
                int fromIndex, toIndex;
                fromIndex = Random().Next(sz + 80);
                toIndex   = fromIndex + Random().Next((sz >> 1) + 1);

                BitArray aa = (BitArray)a.Clone();
                // C# BitArray class does not support dynamic sizing.
                // We have to explicitly change its size using the Length attribute.
                if (toIndex > aa.Length)
                {
                    aa.Length = toIndex;
                }
                aa.Flip(fromIndex, toIndex);
                OpenBitSet bb = (OpenBitSet)b.Clone();
                bb.Flip(fromIndex, toIndex);

                DoIterate(aa, bb, mode); // a problem here is from flip or doIterate

                fromIndex = Random().Next(sz + 80);
                toIndex   = fromIndex + Random().Next((sz >> 1) + 1);
                aa        = (BitArray)a.Clone();
                if (toIndex > aa.Length)
                {
                    aa.Length = toIndex;
                }
                aa.Clear(fromIndex, toIndex);
                bb = (OpenBitSet)b.Clone();
                bb.Clear(fromIndex, toIndex);

                DoNextSetBit(aa, bb); // a problem here is from clear() or nextSetBit
                DoNextSetBitLong(aa, bb);

                DoPrevSetBit(aa, bb);
                DoPrevSetBitLong(aa, bb);

                fromIndex = Random().Next(sz + 80);
                toIndex   = fromIndex + Random().Next((sz >> 1) + 1);
                aa        = (BitArray)a.Clone();
                if (toIndex > aa.Length)
                {
                    aa.Length = toIndex;
                }
                aa.Set(fromIndex, toIndex);
                bb = (OpenBitSet)b.Clone();
                bb.Set(fromIndex, toIndex);

                DoNextSetBit(aa, bb); // a problem here is from set() or nextSetBit
                DoNextSetBitLong(aa, bb);

                DoPrevSetBit(aa, bb);
                DoPrevSetBitLong(aa, bb);

                if (a0 != null)
                {
                    aa = (BitArray)a.Clone();
                    BitArray aa0     = (BitArray)a0.Clone();
                    int      largest = Math.Max(a.Length, a0.Length);
                    aa.Length = aa0.Length = largest;
                    // BitWiseEquals needs both arrays to be the same size for succeeding.
                    // We could enlarge the smallest of a and a0, but then the tests below
                    // won't test "UnequalLengths" operations.
                    Assert.AreEqual(aa.BitWiseEquals(aa0), b.Equals(b0));

                    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);

                    OpenBitSet b_and = (OpenBitSet)b.Clone();
                    Assert.AreEqual(b, b_and);
                    b_and.And(b0);
                    OpenBitSet b_or = (OpenBitSet)b.Clone();
                    b_or.Or(b0);
                    OpenBitSet b_xor = (OpenBitSet)b.Clone();
                    b_xor.Xor(b0);
                    OpenBitSet b_andn = (OpenBitSet)b.Clone();
                    b_andn.AndNot(b0);

                    DoIterate(a_and, b_and, mode);
                    DoIterate(a_or, b_or, mode);
                    DoIterate(a_xor, b_xor, mode);
                    DoIterate(a_andn, b_andn, mode);

                    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());

                    // test non-mutating popcounts
                    Assert.AreEqual(b_and.Cardinality(), OpenBitSet.IntersectionCount(b, b0));
                    Assert.AreEqual(b_or.Cardinality(), OpenBitSet.UnionCount(b, b0));
                    Assert.AreEqual(b_xor.Cardinality(), OpenBitSet.XorCount(b, b0));
                    Assert.AreEqual(b_andn.Cardinality(), OpenBitSet.AndNotCount(b, b0));
                }

                a0 = a;
                b0 = b;
            }
        }
        /**
         * Assess equality between an uncompressed bitmap and a compressed one,
         * part of a test contributed by Marc Polizzi
         *
         * @param clrBitArray the uncompressed bitmap
         * @param ewahBitmap the compressed bitmap
         */

        private static void assertCardinality(BitArray clrBitArray,
                                              EwahCompressedBitArray ewahBitmap)
        {
            Assert.AreEqual(ewahBitmap.GetCardinality(), clrBitArray.Cardinality());
        }
Esempio n. 14
0
        internal virtual void DoRandomSets(int maxSize, int iter, int mode)
        {
            BitArray   a0 = null;
            OpenBitSet b0 = null;

            for (int i = 0; i < iter; i++)
            {
                int        sz = Random().Next(maxSize);
                BitArray   a  = new BitArray(sz);
                OpenBitSet b  = new OpenBitSet(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.FastSet(idx);

                        idx = Random().Next(sz);
                        a.SafeSet(idx, true);
                        b.FastSet((long)idx);

                        idx = Random().Next(sz);
                        a.SafeSet(idx, false);
                        b.FastClear(idx);

                        idx = Random().Next(sz);
                        a.SafeSet(idx, false);
                        b.FastClear((long)idx);

                        idx = Random().Next(sz);
                        a.SafeSet(idx, !a.SafeGet(idx));
                        b.FastFlip(idx);

                        bool val  = b.FlipAndGet(idx);
                        bool val2 = b.FlipAndGet(idx);
                        Assert.IsTrue(val != val2);

                        idx = Random().Next(sz);
                        a.SafeSet(idx, !a.SafeGet(idx));
                        b.FastFlip((long)idx);

                        val  = b.FlipAndGet((long)idx);
                        val2 = b.FlipAndGet((long)idx);
                        Assert.IsTrue(val != val2);

                        val = b.GetAndSet(idx);
                        Assert.IsTrue(val2 == val);
                        Assert.IsTrue(b.Get(idx));

                        if (!val)
                        {
                            b.FastClear(idx);
                        }
                        Assert.IsTrue(b.Get(idx) == val);
                    }
                }

                // test that the various ways of accessing the bits are equivalent
                DoGet(a, b);
                DoGetFast(a, b, sz);

                // test ranges, including possible extension
                int fromIndex, toIndex;
                fromIndex = Random().Next(sz + 80);
                toIndex   = fromIndex + Random().Next((sz >> 1) + 1);
                BitArray aa = (BitArray)a.Clone();
                aa.Flip(fromIndex, toIndex);
                OpenBitSet bb = (OpenBitSet)b.Clone();
                bb.Flip(fromIndex, toIndex);

                DoIterate(aa, bb, mode); // a problem here is from flip or doIterate

                fromIndex = Random().Next(sz + 80);
                toIndex   = fromIndex + Random().Next((sz >> 1) + 1);
                aa        = (BitArray)a.Clone();
                aa.Clear(fromIndex, toIndex);
                bb = (OpenBitSet)b.Clone();
                bb.Clear(fromIndex, toIndex);

                DoNextSetBit(aa, bb); // a problem here is from clear() or nextSetBit
                DoNextSetBitLong(aa, bb);

                DoPrevSetBit(aa, bb);
                DoPrevSetBitLong(aa, bb);

                fromIndex = Random().Next(sz + 80);
                toIndex   = fromIndex + Random().Next((sz >> 1) + 1);
                aa        = (BitArray)a.Clone();
                aa.Set(fromIndex, toIndex);
                bb = (OpenBitSet)b.Clone();
                bb.Set(fromIndex, toIndex);

                DoNextSetBit(aa, bb); // a problem here is from set() or nextSetBit
                DoNextSetBitLong(aa, bb);

                DoPrevSetBit(aa, bb);
                DoPrevSetBitLong(aa, bb);

                if (a0 != null)
                {
                    Assert.AreEqual(a.BitWiseEquals(a0), b.Equals(b0));

                    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);

                    OpenBitSet b_and = (OpenBitSet)b.Clone();
                    Assert.AreEqual(b, b_and);
                    b_and.And(b0);
                    OpenBitSet b_or = (OpenBitSet)b.Clone();
                    b_or.Or(b0);
                    OpenBitSet b_xor = (OpenBitSet)b.Clone();
                    b_xor.Xor(b0);
                    OpenBitSet b_andn = (OpenBitSet)b.Clone();
                    b_andn.AndNot(b0);

                    DoIterate(a_and, b_and, mode);
                    DoIterate(a_or, b_or, mode);
                    DoIterate(a_xor, b_xor, mode);
                    DoIterate(a_andn, b_andn, mode);

                    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());

                    // test non-mutating popcounts
                    Assert.AreEqual(b_and.Cardinality(), OpenBitSet.IntersectionCount(b, b0));
                    Assert.AreEqual(b_or.Cardinality(), OpenBitSet.UnionCount(b, b0));
                    Assert.AreEqual(b_xor.Cardinality(), OpenBitSet.XorCount(b, b0));
                    Assert.AreEqual(b_andn.Cardinality(), OpenBitSet.AndNotCount(b, b0));
                }

                a0 = a;
                b0 = b;
            }
        }
Esempio n. 15
0
 private static bool FloorIsSafe(BitArray chips, BitArray generators)
 {
     return(generators.Cardinality() <= 0 || chips.MakeNot().Or(generators).Cardinality() == chips.Length);
 }