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

            for (int i = 0; i < iter; i++)
            {
                int          nClauses = r.Next(maxClauses - 1) + 2;        // min 2 clauses
                BooleanQuery bq       = new BooleanQuery();
                System.Collections.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.GetSum();

                if (validate)
                {
                    Assert.AreEqual(BitSetSupport.Cardinality(result), hc.GetCount());
                }
                // System.out.println(hc.getCount());
            }

            return(ret);
        }
Exemplo n.º 2
0
            public override int Advance(int target)
            {
                int d = BitSetSupport.NextSetBit(bitSet, target);

                // -1 returned by BitSet.nextSetBit() when exhausted
                docId = d == -1?NO_MORE_DOCS:d;
                return(docId);
            }
Exemplo n.º 3
0
            public override int NextDoc()
            {
                // (docId + 1) on next line requires -1 initial value for docNr:
                int d = BitSetSupport.NextSetBit(bitSet, docId + 1);

                // -1 returned by BitSet.nextSetBit() when exhausted
                docId = d == -1?NO_MORE_DOCS:d;
                return(docId);
            }
Exemplo n.º 4
0
 public virtual void  Collect(int doc, float score)
 {
     pos = BitSetSupport.NextSetBit(answer, pos + 1);
     if (pos != doc + docBase)
     {
         throw new System.SystemException("Expected doc " + pos + " but got " + doc + docBase);
     }
     base.Collect(doc);
 }
Exemplo n.º 5
0
        internal virtual void  DoNextSetBit(System.Collections.BitArray a, OpenBitSet b)
        {
            int aa = -1, bb = -1;

            do
            {
                aa = BitSetSupport.NextSetBit(a, aa + 1);
                bb = b.NextSetBit(bb + 1);
                Assert.AreEqual(aa, bb);
            }while (aa >= 0);
        }
Exemplo n.º 6
0
        internal virtual void  DoIterate2(System.Collections.BitArray a, OpenBitSet b)
        {
            int aa = -1, bb = -1;
            OpenBitSetIterator iterator = new OpenBitSetIterator(b);

            do
            {
                aa = BitSetSupport.NextSetBit(a, aa + 1);
                bb = rand.NextDouble() > 0.5 ? iterator.NextDoc(null) : iterator.Advance(bb + 1, null);
                Assert.AreEqual(aa == -1?DocIdSetIterator.NO_MORE_DOCS:aa, bb);
            }while (aa >= 0);
        }
Exemplo n.º 7
0
        /// <summary> Create a SortedVIntList from a BitSet.</summary>
        /// <param name="bits"> A bit set representing a set of integers.
        /// </param>
        public SortedVIntList(System.Collections.BitArray bits)
        {
            SortedVIntListBuilder builder = new SortedVIntListBuilder(this);
            int nextInt = BitSetSupport.NextSetBit(bits, 0);

            while (nextInt != -1)
            {
                builder.AddInt(nextInt);
                nextInt = BitSetSupport.NextSetBit(bits, nextInt + 1);
            }
            builder.Done();
        }
Exemplo n.º 8
0
        public virtual int DoNestedTermConjunctions(IndexSearcher s, int termsInIndex, int maxOuterClauses, int maxClauses, int iter)
        {
            int  ret      = 0;
            long nMatches = 0;

            for (int i = 0; i < iter; i++)
            {
                int          oClauses = r.Next(maxOuterClauses - 1) + 2;
                BooleanQuery oq       = new BooleanQuery();
                for (int o = 0; o < oClauses; o++)
                {
                    int          nClauses = r.Next(maxClauses - 1) + 2;            // min 2 clauses
                    BooleanQuery bq       = new BooleanQuery();
                    System.Collections.BitArray termflag = new System.Collections.BitArray((termsInIndex % 64 == 0?termsInIndex / 64:termsInIndex / 64 + 1) * 64);
                    for (int j = 0; j < nClauses; j++)
                    {
                        int tnum;
                        // don't pick same clause twice
                        tnum = r.Next(termsInIndex);
                        if (termflag.Get(tnum))
                        {
                            tnum = BitSetSupport.NextClearBit(termflag, tnum);
                        }
                        if (tnum < 0 || tnum >= 25)
                        {
                            tnum = BitSetSupport.NextClearBit(termflag, 0);
                        }
                        termflag.Set(tnum, true);
                        Query tq = new TermQuery(terms[tnum]);
                        bq.Add(tq, Occur.MUST);
                    }                     // inner

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


                CountingHitCollector hc = new CountingHitCollector();
                s.Search(oq, hc);
                nMatches += hc.GetCount();
                ret      += hc.GetSum();
            }
            System.Console.Out.WriteLine("Average number of matches=" + (nMatches / iter));
            return(ret);
        }
 private void  DoTestMultiThreads(bool withTimeout)
 {
     ThreadClass[] threadArray           = new ThreadClass[N_THREADS];
     System.Collections.BitArray success = new System.Collections.BitArray((N_THREADS % 64 == 0?N_THREADS / 64:N_THREADS / 64 + 1) * 64);
     for (int i = 0; i < threadArray.Length; ++i)
     {
         int num = i;
         threadArray[num] = new AnonymousClassThread(withTimeout, success, num, this);
     }
     for (int i = 0; i < threadArray.Length; ++i)
     {
         threadArray[i].Start();
     }
     for (int i = 0; i < threadArray.Length; ++i)
     {
         threadArray[i].Join();
     }
     Assert.AreEqual(N_THREADS, BitSetSupport.Cardinality(success), "some threads failed!");
 }
        public virtual void  Test()
        {
            PositionBasedTermVectorMapper mapper = new PositionBasedTermVectorMapper();

            mapper.SetExpectations("test", tokens.Length, true, true);
            //Test single position
            for (int i = 0; i < tokens.Length; i++)
            {
                System.String token = tokens[i];
                mapper.Map(token, 1, null, thePositions[i]);
            }
            var map = mapper.FieldToTerms;

            Assert.IsTrue(map != null, "map is null and it shouldn't be");
            Assert.IsTrue(map.Count == 1, "map Size: " + map.Count + " is not: " + 1);
            var positions = map["test"];

            Assert.IsNotNull(positions, "thePositions is null and it shouldn't be");

            Assert.AreEqual(numPositions, positions.Count, "thePositions Size: " + positions.Count + " is not: " + numPositions);
            System.Collections.BitArray bits = new System.Collections.BitArray((numPositions % 64 == 0?numPositions / 64:numPositions / 64 + 1) * 64);
            for (var iterator = positions.GetEnumerator(); iterator.MoveNext();)
            {
                var entry = iterator.Current;
                PositionBasedTermVectorMapper.TVPositionInfo info = entry.Value;
                Assert.IsTrue(info != null, "info is null and it shouldn't be");
                int pos = (int)entry.Key;
                bits.Set(pos, true);
                Assert.IsTrue(info.Position == pos, info.Position + " does not equal: " + pos);
                Assert.IsTrue(info.Offsets != null, "info.getOffsets() is null and it shouldn't be");
                if (pos == 0)
                {
                    Assert.IsTrue(info.Terms.Count == 2, "info.getTerms() Size: " + info.Terms.Count + " is not: " + 2); //need a test for multiple terms at one pos
                    Assert.IsTrue(info.Offsets.Count == 2, "info.getOffsets() Size: " + info.Offsets.Count + " is not: " + 2);
                }
                else
                {
                    Assert.IsTrue(info.Terms.Count == 1, "info.getTerms() Size: " + info.Terms.Count + " is not: " + 1); //need a test for multiple terms at one pos
                    Assert.IsTrue(info.Offsets.Count == 1, "info.getOffsets() Size: " + info.Offsets.Count + " is not: " + 1);
                }
            }
            Assert.IsTrue(BitSetSupport.Cardinality(bits) == numPositions, "Bits are not all on");
        }
Exemplo n.º 11
0
        private void  CheckExpecteds(System.Collections.BitArray expecteds)
        {
            IndexReader r = IndexReader.Open(dir, true, null);

            //Perhaps not the most efficient approach but meets our needs here.
            for (int i = 0; i < r.MaxDoc; i++)
            {
                if (!r.IsDeleted(i))
                {
                    System.String sval = r.Document(i, null).Get(FIELD_RECORD_ID, null);
                    if (sval != null)
                    {
                        int val = System.Int32.Parse(sval);
                        Assert.IsTrue(expecteds.Get(val), "Did not expect document #" + val);
                        expecteds.Set(val, false);
                    }
                }
            }
            r.Close();
            Assert.AreEqual(0, BitSetSupport.Cardinality(expecteds), "Should have 0 docs remaining ");
        }
Exemplo n.º 12
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 = r.Next(maxOuterClauses - 1) + 2;
                BooleanQuery oq       = new BooleanQuery();
                System.Collections.BitArray result = null;

                for (int o = 0; o < oClauses; o++)
                {
                    int          nClauses = r.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.GetCount();
                ret      += hc.GetSum();
                if (validate)
                {
                    Assert.AreEqual(BitSetSupport.Cardinality(result), hc.GetCount());
                }
                // System.out.println(hc.getCount());
            }
            System.Console.Out.WriteLine("Average number of matches=" + (nMatches / iter));
            return(ret);
        }
Exemplo n.º 13
0
 public virtual int HitCount()
 {
     return(BitSetSupport.Cardinality(bits));
 }
Exemplo n.º 14
0
        internal virtual void  DoRandomSets(int maxSize, int iter, int mode)
        {
            System.Collections.BitArray a0 = null;
            OpenBitSet b0 = null;

            for (int i = 0; i < iter; i++)
            {
                int sz = rand.Next(maxSize);
                System.Collections.BitArray a = new System.Collections.BitArray(sz);
                OpenBitSet b = new OpenBitSet(sz);

                // test the various ways of setting bits
                if (sz > 0)
                {
                    int nOper = rand.Next(sz);
                    for (int j = 0; j < nOper; j++)
                    {
                        int idx;

                        idx = rand.Next(sz);
                        a.Set(idx, true);
                        b.FastSet(idx);
                        idx = rand.Next(sz);
                        a.Set(idx, false);
                        b.FastClear(idx);
                        idx = rand.Next(sz);
                        a.Set(idx, !a.Get(idx));
                        b.FastFlip(idx);

                        bool val  = b.FlipAndGet(idx);
                        bool val2 = b.FlipAndGet(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);

                // {{dougsale-2.4.0}}
                //
                // Java's java.util.BitSet automatically grows as needed - i.e., when a bit is referenced beyond
                // the size of the BitSet, an exception isn't thrown - rather, the set grows to the size of the
                // referenced bit.
                //
                // System.Collections.BitArray does not have this feature, and thus I've faked it here by
                // "growing" the array explicitly when necessary (creating a new instance of the appropriate size
                // and setting the appropriate bits).
                //

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

                // {{dougsale-2.4.0}}:
                // The following commented-out, compound statement's 'for loop' implicitly grows the Java BitSets 'a'
                // and 'aa' to the same cardinality as 'j+1' when 'a.Count < j+1' and 'fromIndex < toIndex':
                //BitArray aa = (BitArray)a.Clone(); for (int j = fromIndex; j < toIndex; j++) aa.Set(j, !a.Get(j));
                // So, if necessary, lets explicitly grow 'a' now; then 'a' and its clone, 'aa', will be of the required size.
                if (a.Length < toIndex && fromIndex < toIndex)
                {
                    System.Collections.BitArray tmp = new System.Collections.BitArray(toIndex, false);
                    for (int k = 0; k < a.Length; k++)
                    {
                        tmp.Set(k, a.Get(k));
                    }
                    a = tmp;
                }
                // {{dougsale-2.4.0}}: now we can invoke this statement without going 'out-of-bounds'
                System.Collections.BitArray aa = (System.Collections.BitArray)a.Clone(); for (int j = fromIndex; j < toIndex; j++)
                {
                    aa.Set(j, !a.Get(j));
                }
                OpenBitSet bb = (OpenBitSet)b.Clone(); bb.Flip(fromIndex, toIndex);

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

                fromIndex = rand.Next(sz + 80);
                toIndex   = fromIndex + rand.Next((sz >> 1) + 1);
                // {{dougsale-2.4.0}}:
                // The following commented-out, compound statement's 'for loop' implicitly grows the Java BitSet 'aa'
                // when 'a.Count < j+1' and 'fromIndex < toIndex'
                //aa = (BitArray)a.Clone(); for (int j = fromIndex; j < toIndex; j++) aa.Set(j, false);
                // So, if necessary, lets explicitly grow 'aa' now
                if (a.Length < toIndex && fromIndex < toIndex)
                {
                    aa = new System.Collections.BitArray(toIndex);
                    for (int k = 0; k < a.Length; k++)
                    {
                        aa.Set(k, a.Get(k));
                    }
                }
                else
                {
                    aa = (System.Collections.BitArray)a.Clone();
                }
                for (int j = fromIndex; j < toIndex; j++)
                {
                    aa.Set(j, false);
                }
                bb = (OpenBitSet)b.Clone(); bb.Clear(fromIndex, toIndex);

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

                fromIndex = rand.Next(sz + 80);
                toIndex   = fromIndex + rand.Next((sz >> 1) + 1);
                // {{dougsale-2.4.0}}:
                // The following commented-out, compound statement's 'for loop' implicitly grows the Java BitSet 'aa'
                // when 'a.Count < j+1' and 'fromIndex < toIndex'
                //aa = (BitArray)a.Clone(); for (int j = fromIndex; j < toIndex; j++) aa.Set(j, false);
                // So, if necessary, lets explicitly grow 'aa' now
                if (a.Length < toIndex && fromIndex < toIndex)
                {
                    aa = new System.Collections.BitArray(toIndex);
                    for (int k = 0; k < a.Length; k++)
                    {
                        aa.Set(k, a.Get(k));
                    }
                }
                else
                {
                    aa = (System.Collections.BitArray)a.Clone();
                }
                for (int j = fromIndex; j < toIndex; j++)
                {
                    aa.Set(j, true);
                }
                bb = (OpenBitSet)b.Clone(); bb.Set(fromIndex, toIndex);

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


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

                    Assert.AreEqual(BitSetSupport.Cardinality(a), b.Cardinality());

                    // {{dougsale-2.4.0}}
                    //
                    // The Java code used java.util.BitSet, which grows as needed.
                    // When a bit, outside the dimension of the set is referenced,
                    // the set automatically grows to the necessary size.  The
                    // new entries default to false.
                    //
                    // BitArray does not grow automatically and is not growable.
                    // Thus when BitArray instances of mismatched cardinality
                    // interact, we must first explicitly "grow" the smaller one.
                    //
                    // This growth is acheived by creating a new instance of the
                    // required size and copying the appropriate values.
                    //

                    //BitArray a_and = (BitArray)a.Clone(); a_and.And(a0);
                    //BitArray a_or = (BitArray)a.Clone(); a_or.Or(a0);
                    //BitArray a_xor = (BitArray)a.Clone(); a_xor.Xor(a0);
                    //BitArray a_andn = (BitArray)a.Clone(); for (int j = 0; j < a_andn.Count; j++) if (a0.Get(j)) a_andn.Set(j, false);

                    System.Collections.BitArray a_and;
                    System.Collections.BitArray a_or;
                    System.Collections.BitArray a_xor;
                    System.Collections.BitArray a_andn;

                    if (a.Length < a0.Length)
                    {
                        // the Java code would have implicitly resized 'a_and', 'a_or', 'a_xor', and 'a_andn'
                        // in this case, so we explicitly create a resized stand-in for 'a' here, allowing for
                        // a to keep its original size while 'a_and', 'a_or', 'a_xor', and 'a_andn' are resized
                        System.Collections.BitArray tmp = new System.Collections.BitArray(a0.Length, false);
                        for (int z = 0; z < a.Length; z++)
                        {
                            tmp.Set(z, a.Get(z));
                        }

                        a_and  = (System.Collections.BitArray)tmp.Clone(); a_and.And(a0);
                        a_or   = (System.Collections.BitArray)tmp.Clone(); a_or.Or(a0);
                        a_xor  = (System.Collections.BitArray)tmp.Clone(); a_xor.Xor(a0);
                        a_andn = (System.Collections.BitArray)tmp.Clone(); for (int j = 0; j < a_andn.Length; j++)
                        {
                            if (a0.Get(j))
                            {
                                a_andn.Set(j, false);
                            }
                        }
                    }
                    else if (a.Length > a0.Length)
                    {
                        // the Java code would have implicitly resized 'a0' in this case, so
                        // we explicitly do so here:
                        System.Collections.BitArray tmp = new System.Collections.BitArray(a.Length, false);
                        for (int z = 0; z < a0.Length; z++)
                        {
                            tmp.Set(z, a0.Get(z));
                        }
                        a0 = tmp;

                        a_and  = (System.Collections.BitArray)a.Clone(); a_and.And(a0);
                        a_or   = (System.Collections.BitArray)a.Clone(); a_or.Or(a0);
                        a_xor  = (System.Collections.BitArray)a.Clone(); a_xor.Xor(a0);
                        a_andn = (System.Collections.BitArray)a.Clone(); for (int j = 0; j < a_andn.Length; j++)
                        {
                            if (a0.Get(j))
                            {
                                a_andn.Set(j, false);
                            }
                        }
                    }
                    else
                    {
                        // 'a' and 'a0' are the same size, no explicit growing necessary
                        a_and  = (System.Collections.BitArray)a.Clone(); a_and.And(a0);
                        a_or   = (System.Collections.BitArray)a.Clone(); a_or.Or(a0);
                        a_xor  = (System.Collections.BitArray)a.Clone(); a_xor.Xor(a0);
                        a_andn = (System.Collections.BitArray)a.Clone(); for (int j = 0; j < a_andn.Length; j++)
                        {
                            if (a0.Get(j))
                            {
                                a_andn.Set(j, false);
                            }
                        }
                    }

                    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(BitSetSupport.Cardinality(a_and), b_and.Cardinality());
                    Assert.AreEqual(BitSetSupport.Cardinality(a_or), b_or.Cardinality());
                    Assert.AreEqual(BitSetSupport.Cardinality(a_xor), b_xor.Cardinality());
                    Assert.AreEqual(BitSetSupport.Cardinality(a_andn), 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;
            }
        }