/// <summary>
 /// Short-cut to quickly create a sparse float array representing 
 /// <code>this(new float[capacity]);</code>, but without reading through said array.
 /// The advantage here is that the constructor is lightning-fast in the case that 
 /// all values in the float array are known to 
 /// <c>== 0f</c>.
 /// </summary>
 /// <param name="capacity">The capacity of the array.</param>
 public SparseFloatArray(int capacity)
 {
     _capacity = capacity;
     _floats = null;
     _bits = null;
     _referencePoints = null;
 }
 protected virtual void Condense(float[] floats)
 {
     if (floats.Length != _capacity)
     {
         throw new ArgumentException("bad input float array of length " + floats.Length + " for capacity: " + _capacity);
     }
     var bits = new BitSet(floats.Length);
     int on = 0;
     for (int i = 0; i < floats.Length; i++)
     {
         if (floats[i] != 0f)
         {
             bits.Set(i, true);
             on++;
         }
     }
     if (((float)on) / ((float)floats.Length) < ON_RATIO_CUTOFF)
     {
         // it's worth compressing
         if (0 == on)
         {
             // it's worth super-compressing
             _floats = null;
             _bits = null;
             _referencePoints = null;
             // capacity is good.
         }
         else
         {
             _bits = bits;
             _floats = new float[_bits.Cardinality()];
             _referencePoints = new int[floats.Length / REFERENCE_POINT_EVERY];
             int i = 0;
             int floatsIdx = 0;
             int refIdx = 0;
             while (i < floats.Length && (i = _bits.NextSetBit(i)) >= 0)
             {
                 _floats[floatsIdx] = floats[i];
                 while (refIdx < i / REFERENCE_POINT_EVERY)
                 {
                     _referencePoints[refIdx++] = floatsIdx;
                 }
                 floatsIdx++;
                 i++;
             }
             while (refIdx < _referencePoints.Length)
             {
                 _referencePoints[refIdx++] = floatsIdx;
             }
         }
     }
     else
     {
         // it's not worth compressing
         _floats = floats;
         _bits = null;
     }
 }
示例#3
0
        public void TestFilteredDocSetIterator()
        {
            var set1 = new IntArrayDocIdSet();
            for (int i = 0; i < 100; i++)
            {
                set1.AddDoc(2 * i); // 100 even numbers
            }

            var filteredIter = new MyFilteredDocSetIterator(set1.Iterator());

            var bs = new BitSet(200);
            for (int i = 0; i < 100; ++i)
            {
                int n = 10 * i;
                if (n < 200)
                {
                    bs.Set(n, true);
                }
            }

            try
            {
                while (filteredIter.NextDoc() != DocIdSetIterator.NO_MORE_DOCS)
                {
                    int doc = filteredIter.DocID();
                    if (!bs.Get(doc))
                    {
                        Assert.Fail("failed: " + doc + " not in expected set");
                        return;
                    }
                    else
                    {
                        bs.Set(doc, false);
                    }
                }
                var cardinality = bs.Cardinality();
                if (cardinality > 0)
                {
                    Assert.Fail("failed: leftover cardinality: " + cardinality);
                }
            }
            catch (Exception e)
            {
                Assert.Fail(e.Message);
            }
        }
示例#4
0
        // This is used by EnumSet for efficiency.
        public bool ContainsAll(BitSet other)
        {
            for (int i = other.bits.Length - 1; i >= 0; i--)
            {
                if ((bits[i] & other.bits[i]) != other.bits[i])
                    return false;
            }

            return true;
        }
示例#5
0
 /// <summary>
 /// Performs the logical XOR operation on this bit set and the
 /// given <code>set</code>.  This means it builds the symmetric
 /// remainder of the two sets (the elements that are in one set,
 /// but not in the other).  The result is stored into this bit set,
 /// which grows as necessary.
 /// </summary>
 /// <param name="bs">the second bit set</param>
 public void XOr(BitSet bs)
 {
     Ensure(bs.bits.Length - 1);
     for (int i = bs.bits.Length - 1; i >= 0; i--)
         bits[i] ^= bs.bits[i];
 }
示例#6
0
 /// <summary>
 /// Returns true if the specified BitSet and this one share at least one
 /// common true bit.
 /// </summary>
 /// <param name="set">the set to check for intersection</param>
 /// <returns>true if the sets intersect</returns>
 public bool Intersects(BitSet set)
 {
     int i = Math.Min(bits.Length, set.bits.Length);
     while (--i >= 0)
     {
         if ((bits[i] & set.bits[i]) != 0)
             return true;
     }
     return false;
 }
示例#7
0
        /// <summary>
        /// Returns a new <code>BitSet</code> composed of a range of bits from
        /// this one.
        /// </summary>
        /// <param name="from">the low index (inclusive)</param>
        /// <param name="to">the high index (exclusive)</param>
        /// <returns></returns>
        public BitSet Get(int from, int to)
        {
            if (from < 0 || from > to)
	            throw new ArgumentOutOfRangeException();
            BitSet bs = new BitSet(to - from);
            uint lo_offset = (uint)from >> 6;
            if (lo_offset >= bits.Length || to == from)
	            return bs;

            int lo_bit = from & LONG_MASK;
            uint hi_offset = (uint)to >> 6;
            if (lo_bit == 0)
            {
	            uint len = Math.Min(hi_offset - lo_offset + 1, (uint)bits.Length - lo_offset);
                Array.Copy(bits, lo_offset, bs.bits, 0, len);
	            if (hi_offset < bits.Length)
	            bs.bits[hi_offset - lo_offset] &= (1L << to) - 1;
	            return bs;
            }

            uint len2 = Math.Min(hi_offset, (uint)bits.Length - 1);
            int reverse = 64 - lo_bit;
            int i;
            for (i = 0; lo_offset < len2; lo_offset++, i++)
                bs.bits[i] = ((bits[lo_offset] >> lo_bit) | (bits[lo_offset + 1] << reverse));
            if ((to & LONG_MASK) > lo_bit)
	            bs.bits[i++] = bits[lo_offset] >> lo_bit;
            if (hi_offset < bits.Length)
	            bs.bits[i - 1] &= (1L << (to - from)) - 1;
            return bs;
        }
示例#8
0
 /// <summary>
 /// Performs the logical AND operation on this bit set and the
 /// complement of the given <code>bs</code>.  This means it
 /// selects every element in the first set, that isn't in the
 /// second set.  The result is stored into this bit set and is
 /// effectively the set difference of the two.
 /// </summary>
 /// <param name="bs">the second bit set</param>
 public void AndNot(BitSet bs)
 {
     int i = Math.Min(bits.Length, bs.bits.Length);
     while (--i >= 0)
         bits[i] &= ~bs.bits[i];
 }
示例#9
0
 /// <summary>
 /// Performs the logical AND operation on this bit set and the
 /// given <code>set</code>.  This means it builds the intersection
 /// of the two sets.  The result is stored into this bit set.
 /// </summary>
 /// <param name="bs">the second bit set</param>
 public void And(BitSet bs)
 {
     int max = Math.Min(bits.Length, bs.bits.Length);
     int i;
     for (i = 0; i < max; ++i)
         bits[i] &= bs.bits[i];
     while (i < bits.Length)
         bits[i++] = 0;
 }
示例#10
0
 public BitsDocIdSetIterator(BitSet bs)
 {
     _bs = bs;
     _current = -1;
 }
示例#11
0
 public BitsetDocSet(int nbits)
 {
     _bs = new BitSet(nbits);
 }
示例#12
0
 public BitsetDocSet()
 {
     _bs = new BitSet();
 }