BitsetsNET.RoaringArray.AdvanceUntil C# (CSharp) Method

AdvanceUntil() private method

private AdvanceUntil ( ushort x, int pos ) : int
x ushort
pos int
return int
        internal int AdvanceUntil(ushort x, int pos)
        {
            int lower = pos + 1;

            // special handling for a possibly common sequential case
            if (lower >= Size || keys[lower] >= x)
            {
                return lower;
            }

            int spansize = 1; // could set larger
            // bootstrap an upper limit

            while (lower + spansize < Size && keys[lower + spansize] < x)
            {
                spansize *= 2; // hoping for compiler will reduce to shift
            }
            int upper = (lower + spansize < Size) ? lower + spansize : Size - 1;

            // maybe we are lucky (could be common case when the seek ahead
            // expected to be small and sequential will otherwise make us look bad)
            if (keys[upper] == x)
            {
                return upper;
            }

            if (keys[upper] < x)
            {// means array has no item key >= x
                return Size;
            }

            // we know that the next-smallest span was too small
            lower += (spansize / 2);

            // else begin binary search
            // invariant: array[lower]<x && array[upper]>x
            while (lower + 1 != upper)
            {
                int mid = (lower + upper) / 2;
                if (keys[mid] == x)
                {
                    return mid;
                }
                else if (keys[mid] < x)
                {
                    lower = mid;
                }
                else
                {
                    upper = mid;
                }
            }
            return upper;
        }

Usage Example

Example #1
0
        /// <summary>
        /// Performs an in-place intersection of two Roaring Bitsets.
        /// </summary>
        /// <param name="other">the second Roaring Bitset to intersect</param>
        private void AndWith(RoaringBitset other)
        {
            int thisLength = containers.Size;
            int otherLength = other.containers.Size;
            int pos1 = 0, pos2 = 0, intersectionSize = 0;

            while (pos1 < thisLength && pos2 < otherLength)
            {
                ushort s1 = containers.GetKeyAtIndex(pos1);
                ushort s2 = other.containers.GetKeyAtIndex(pos2);

                if (s1 == s2)
                {
                    Container c1 = containers.GetContainerAtIndex(pos1);
                    Container c2 = other.containers.GetContainerAtIndex(pos2);
                    Container c  = c1.IAnd(c2);

                    if (c.GetCardinality() > 0)
                    {
                        containers.ReplaceKeyAndContainerAtIndex(intersectionSize++, s1, c);
                    }

                    ++pos1;
                    ++pos2;
                }
                else if (s1 < s2)
                {
                    // s1 < s2
                    pos1 = containers.AdvanceUntil(s2, pos1);
                }
                else
                {
                    // s1 > s2
                    pos2 = other.containers.AdvanceUntil(s1, pos2);
                }
            }

            containers.Resize(intersectionSize);
        }
All Usage Examples Of BitsetsNET.RoaringArray::AdvanceUntil