public static RoaringBitset and(RoaringBitset x1,
            RoaringBitset x2)
        {
            RoaringBitset answer = new RoaringBitset();
            int length1 = x1.containers.size, length2 = x2.containers.size;
            int pos1 = 0, pos2 = 0;

            while (pos1 < length1 && pos2 < length2) {
                ushort s1 = x1.containers.getKeyAtIndex(pos1);
                ushort s2 = x2.containers.getKeyAtIndex(pos2);

                if (s1 == s2) {
                    Container c1 = x1.containers.getContainerAtIndex(pos1);
                    Container c2 = x2.containers.getContainerAtIndex(pos2);
                    Container c = c1.and(c2);

                    if (c.getCardinality() > 0) {
                        answer.containers.append(s1, c);
                    }

                    ++pos1;
                    ++pos2;
                } else if (s1 < s2 ) { // s1 < s2
                    pos1 = x1.containers.advanceUntil(s2, pos1);
                } else { // s1 > s2
                    pos2 = x2.containers.advanceUntil(s1, pos2);
                }
            }
            return answer;
        }
 public static RoaringBitset Create(int[] input)
 {
     RoaringBitset rb = new RoaringBitset();
     foreach (int i in input) {
         rb.add(i);
     }
     return rb;
 }
Example #3
0
        /// <summary>
        /// Read a binary serialization of a roaring bitset, as written by the Serialize method.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <returns>The bitset deserialized from the stream.</returns>
        public static RoaringBitset Deserialize(Stream stream)
        {
            RoaringBitset bitset = new RoaringBitset();

            //We don't care about the encoding, but we have to specify something to be able to set the stream as leave open.
            using (BinaryReader reader = new BinaryReader(stream, Encoding.Default, true))
            {
                bitset.containers = RoaringArray.Deserialize(reader);
            }

            return bitset;
        }
Example #4
0
        /// <summary>
        /// Creates a new bitset that is the bitwise OR of this bitset with another
        /// </summary>
        /// <param name="otherSet">Other bitset</param>
        /// <returns>A new IBitset</returns>
        public IBitset Or(IBitset otherSet)
        {
            if (!(otherSet is RoaringBitset))
            {
                throw new ArgumentOutOfRangeException("otherSet must be a RoaringBitSet");
            }

            RoaringBitset answer = new RoaringBitset();
            RoaringBitset x2     = (RoaringBitset)otherSet;

            int pos1 = 0, pos2 = 0;
            int thisSize     = this.containers.Size;
            int otherSetSize = x2.containers.Size;

            if (pos1 < thisSize && pos2 < otherSetSize)
            {
                ushort s1 = this.containers.GetKeyAtIndex(pos1);
                ushort s2 = x2.containers.GetKeyAtIndex(pos2);

                while (true)
                {
                    if (s1 == s2)
                    {
                        Container newContainer = this.containers.GetContainerAtIndex(pos1)
                                                 .Or(x2.containers.GetContainerAtIndex(pos2));
                        answer.containers.Append(s1, newContainer);
                        pos1++;
                        pos2++;
                        if ((pos1 == thisSize) || (pos2 == otherSetSize))
                        {
                            break;
                        }
                        s1 = this.containers.GetKeyAtIndex(pos1);
                        s2 = x2.containers.GetKeyAtIndex(pos2);
                    }
                    else if (s1 < s2)
                    {
                        answer.containers.AppendCopy(this.containers, pos1);
                        pos1++;
                        if (pos1 == thisSize)
                        {
                            break;
                        }
                        s1 = this.containers.GetKeyAtIndex(pos1);
                    }
                    else // s1 > s2
                    {
                        answer.containers.AppendCopy(x2.containers, pos2);
                        pos2++;
                        if (pos2 == otherSetSize)
                        {
                            break;
                        }
                        s2 = x2.containers.GetKeyAtIndex(pos2);
                    }
                }
            }

            if (pos1 == thisSize)
            {
                answer.containers.AppendCopy(x2.containers, pos2, otherSetSize);
            }
            else if (pos2 == otherSetSize)
            {
                answer.containers.AppendCopy(this.containers, pos1, thisSize);
            }

            return(answer);
        }
Example #5
0
        /// <summary>
        /// Computes the in-place bitwise OR of this bitset with another
        /// </summary>
        /// <param name="otherSet">Other bitset</param>
        public void OrWith(IBitset otherSet)
        {
            if (!(otherSet is RoaringBitset))
            {
                throw new ArgumentOutOfRangeException("otherSet must be a RoaringBitSet");
            }

            RoaringBitset x2 = (RoaringBitset)otherSet;

            int pos1 = 0, pos2 = 0;
            int length1 = this.containers.Size, length2 = x2.containers.Size;

            if (pos1 < length1 && pos2 < length2)
            {
                ushort s1 = this.containers.GetKeyAtIndex(pos1);
                ushort s2 = x2.containers.GetKeyAtIndex(pos2);

                while (true)
                {
                    if (s1 == s2)
                    {
                        Container newContainer = this.containers.GetContainerAtIndex(pos1)
                                                 .IOr(x2.containers.GetContainerAtIndex(pos2));
                        this.containers.SetContainerAtIndex(pos1, newContainer);
                        pos1++;
                        pos2++;
                        if ((pos1 == length1) || (pos2 == length2))
                        {
                            break;
                        }
                        s1 = this.containers.GetKeyAtIndex(pos1);
                        s2 = x2.containers.GetKeyAtIndex(pos2);
                    }
                    else if (s1 < s2)
                    {
                        pos1++;
                        if (pos1 == length1)
                        {
                            break;
                        }
                        s1 = this.containers.GetKeyAtIndex(pos1);
                    }
                    else
                    { // s1 > s2
                        this.containers.InsertNewKeyValueAt(pos1, s2, x2.containers.GetContainerAtIndex(pos2));
                        pos1++;
                        length1++;
                        pos2++;
                        if (pos2 == length2)
                        {
                            break;
                        }
                        s2 = x2.containers.GetKeyAtIndex(pos2);
                    }
                }
            }

            if (pos1 == length1)
            {
                this.containers.AppendCopy(x2.containers, pos2, length2);
            }
        }
Example #6
0
 public static RoaringBitset Or(RoaringBitset x1, RoaringBitset x2)
 {
     return((RoaringBitset)x1.Or(x2));
 }
        /// <summary>
        /// Finds members of this bitset that are not in the other set (ANDNOT).
        /// Modifies current bitset in place.
        /// </summary>
        /// <param name="otherSet">The set to compare against</param>
        public void iandNot(RoaringBitset otherSet)
        {
            int pos1 = 0, pos2 = 0, intersectionSize = 0;
            int length1 = containers.size, length2 = otherSet.containers.size;

            while (pos1 < length1 && pos2 < length2)
            {
                ushort s1 = containers.getKeyAtIndex(pos1);
                ushort s2 = otherSet.containers.getKeyAtIndex(pos2);
                if (s1 == s2)
                {
                    Container c1 = containers.getContainerAtIndex(pos1);
                    Container c2 = otherSet.containers.getContainerAtIndex(pos2);
                    Container c = c1.iandNot(c2);
                    if (c.getCardinality() > 0)
                    {
                        containers.replaceKeyAndContainerAtIndex(intersectionSize++, s1, c);
                    }
                    ++pos1;
                    ++pos2;
                }
                else if (Utility.compareUnsigned(s1, s2) < 0)
                { // s1 < s2
                    if (pos1 != intersectionSize)
                    {
                        Container c1 = containers.getContainerAtIndex(pos1);
                        containers.replaceKeyAndContainerAtIndex(intersectionSize, s1, c1);
                    }
                    ++intersectionSize;
                    ++pos1;
                }
                else
                { // s1 > s2
                    pos2 = otherSet.containers.advanceUntil(s1, pos2);
                }
            }
            if (pos1 < length1)
            {
                containers.copyRange(pos1, length1, intersectionSize);
                intersectionSize += length1 - pos1;
            }
            containers.resize(intersectionSize);
        }
        /// <summary>
        /// Finds members of a bitset that are not in the other set (ANDNOT).
        /// This does not modify either bitset.
        /// </summary>
        /// <param name="otherSet">The set to compare against</param>
        /// <returns>A new IBitset containing the members that are in
        /// the first bitset but not in the second.</returns>
        public IBitset andNot(RoaringBitset otherSet)
        {

            RoaringBitset answer = new RoaringBitset();
            int pos1 = 0, pos2 = 0;
            int length1 = containers.size, length2 = otherSet.containers.size;

            while (pos1 < length1 && pos2 < length2)
            {
                ushort s1 = containers.getKeyAtIndex(pos1);
                ushort s2 = otherSet.containers.getKeyAtIndex(pos2);
                if (s1 == s2)
                {
                    Container c1 = containers.getContainerAtIndex(pos1);
                    Container c2 = otherSet.containers.getContainerAtIndex(pos2);
                    Container c = c1.andNot(c2);
                    if (c.getCardinality() > 0)
                    {
                        answer.containers.append(s1, c);
                    }
                    ++pos1;
                    ++pos2;
                }
                else if (Utility.compareUnsigned(s1, s2) < 0)
                { // s1 < s2
                    int nextPos1 = containers.advanceUntil(s2, pos1);
                    answer.containers.appendCopy(containers, pos1, nextPos1);
                    pos1 = nextPos1;
                }
                else
                { // s1 > s2
                    pos2 = otherSet.containers.advanceUntil(s1, pos2);
                }
            }
            if (pos2 == length2)
            {
                answer.containers.appendCopy(containers, pos1, length1);
            }
            return answer;
        }
        public IBitset Or(IBitset otherSet)
        {
            if (!(otherSet is RoaringBitset))
                throw new ArgumentOutOfRangeException("otherSet must be a RoaringBitSet");
            
            RoaringBitset answer = new RoaringBitset();
            RoaringBitset x2 = (RoaringBitset) otherSet;

            int pos1 = 0, pos2 = 0;
            int length1 = this.containers.size, length2 = x2.containers.size;

            if (pos1 < length1 && pos2 < length2) {
                ushort s1 = this.containers.getKeyAtIndex(pos1);
                ushort s2 = x2.containers.getKeyAtIndex(pos2);

                while (true) {
                    if (s1 == s2) {
                        answer.containers.append(
                            s1, 
                            this.containers.getContainerAtIndex(pos1).or(
                                x2.containers.getContainerAtIndex(pos2)
                            )
                        );
                        pos1++;
                        pos2++;
                        if ((pos1 == length1) || (pos2 == length2)) {
                            break;
                        }
                        s1 = this.containers.getKeyAtIndex(pos1);
                        s2 = x2.containers.getKeyAtIndex(pos2);
                    } else if (s1 < s2) {
                        answer.containers.appendCopy(this.containers, pos1);
                        pos1++;
                        if (pos1 == length1) {
                            break;
                        }
                        s1 = this.containers.getKeyAtIndex(pos1);
                    } else { // s1 > s2
                        answer.containers.appendCopy(x2.containers, pos2);
                        pos2++;
                        if (pos2 == length2) {
                            break;
                        }
                        s2 = x2.containers.getKeyAtIndex(pos2);
                    }
                }
            }

            if (pos1 == length1) {
                answer.containers.appendCopy(x2.containers, pos2, length2);
            } else if (pos2 == length2) {
                answer.containers.appendCopy(this.containers, pos1, length1);
            }

            return answer;
        }
 public IBitset Clone()
 {
     RoaringBitset x = new RoaringBitset();
     x.containers = containers.clone();
     return x;
 }
        /// <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 length1 = this.containers.size, length2 = other.containers.size;
            int pos1 = 0, pos2 = 0, intersectionSize = 0;

            while (pos1 < length1 && pos2 < length2)
            {
                ushort s1 = this.containers.getKeyAtIndex(pos1);
                ushort s2 = other.containers.getKeyAtIndex(pos2);

                if (s1 == s2)
                {
                    Container c1 = this.containers.getContainerAtIndex(pos1);
                    Container c2 = other.containers.getContainerAtIndex(pos2);
                    Container c = c1.iand(c2);

                    if (c.getCardinality() > 0)
                    {
                        this.containers.replaceKeyAndContainerAtIndex(intersectionSize++, s1, c);
                    }
                        
                    ++pos1;
                    ++pos2;
                }
                else if (s1 < s2)
                { // s1 < s2
                    pos1 = this.containers.advanceUntil(s2, pos1);
                }
                else
                { // s1 > s2
                    pos2 = other.containers.advanceUntil(s1, pos2);
                }
            }
            this.containers.resize(intersectionSize);
        }
Example #12
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 = this.containers.Size;
            int otherLength = other.containers.Size;
            int pos1 = 0, pos2 = 0, intersectionSize = 0;

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

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

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

                    ++pos1;
                    ++pos2;
                }
                else if (s1 < s2)
                { // s1 < s2
                    pos1 = this.containers.AdvanceUntil(s2, pos1);
                }
                else
                { // s1 > s2
                    pos2 = other.containers.AdvanceUntil(s1, pos2);
                }
            }
            this.containers.Resize(intersectionSize);
        }
Example #13
0
        /// <summary>
        /// Creates a new bitset that is the bitwise OR of this bitset with another
        /// </summary>
        /// <param name="otherSet">Other bitset</param>
        /// <returns>A new IBitset</returns>
        public IBitset Or(IBitset otherSet)
        {
            if (!(otherSet is RoaringBitset))
            {
                throw new ArgumentOutOfRangeException("otherSet must be a RoaringBitSet");
            }

            RoaringBitset answer = new RoaringBitset();
            RoaringBitset x2 = (RoaringBitset) otherSet;

            int pos1 = 0, pos2 = 0;
            int thisSize = this.containers.Size;
            int otherSetSize = x2.containers.Size;

            if (pos1 < thisSize && pos2 < otherSetSize)
            {
                ushort s1 = this.containers.GetKeyAtIndex(pos1);
                ushort s2 = x2.containers.GetKeyAtIndex(pos2);

                while (true)
                {
                    if (s1 == s2)
                    {
                        Container newContainer = this.containers.GetContainerAtIndex(pos1)
                                                     .Or(x2.containers.GetContainerAtIndex(pos2));
                        answer.containers.Append(s1, newContainer);
                        pos1++;
                        pos2++;
                        if ((pos1 == thisSize) || (pos2 == otherSetSize))
                        {
                            break;
                        }
                        s1 = this.containers.GetKeyAtIndex(pos1);
                        s2 = x2.containers.GetKeyAtIndex(pos2);
                    }
                    else if (s1 < s2)
                    {
                        answer.containers.AppendCopy(this.containers, pos1);
                        pos1++;
                        if (pos1 == thisSize)
                        {
                            break;
                        }
                        s1 = this.containers.GetKeyAtIndex(pos1);
                    }
                    else // s1 > s2
                    {
                        answer.containers.AppendCopy(x2.containers, pos2);
                        pos2++;
                        if (pos2 == otherSetSize)
                        {
                            break;
                        }
                        s2 = x2.containers.GetKeyAtIndex(pos2);
                    }
                }
            }

            if (pos1 == thisSize)
            {
                answer.containers.AppendCopy(x2.containers, pos2, otherSetSize);
            }
            else if (pos2 == otherSetSize)
            {
                answer.containers.AppendCopy(this.containers, pos1, thisSize);
            }

            return answer;
        }
Example #14
0
        /// <summary>
        /// Finds members of this bitset that are not in the other set (ANDNOT).
        /// Modifies current bitset in place.
        /// </summary>
        /// <param name="otherSet">The set to compare against</param>
        public void IAndNot(RoaringBitset otherSet)
        {
            int pos1 = 0, pos2 = 0, intersectionSize = 0;
            int thisSize = containers.Size;
            int otherSetSize = otherSet.containers.Size;

            while (pos1 < thisSize && pos2 < otherSetSize)
            {
                ushort s1 = containers.GetKeyAtIndex(pos1);
                ushort s2 = otherSet.containers.GetKeyAtIndex(pos2);
                if (s1 == s2)
                {
                    Container c1 = containers.GetContainerAtIndex(pos1);
                    Container c2 = otherSet.containers.GetContainerAtIndex(pos2);
                    Container c = c1.IAndNot(c2);
                    if (c.GetCardinality() > 0)
                    {
                        containers.ReplaceKeyAndContainerAtIndex(intersectionSize++, s1, c);
                    }
                    ++pos1;
                    ++pos2;
                }
                else if (Utility.CompareUnsigned(s1, s2) < 0)
                { // s1 < s2
                    if (pos1 != intersectionSize)
                    {
                        Container c1 = containers.GetContainerAtIndex(pos1);
                        containers.ReplaceKeyAndContainerAtIndex(intersectionSize, s1, c1);
                    }
                    ++intersectionSize;
                    ++pos1;
                }
                else
                { // s1 > s2
                    pos2 = otherSet.containers.AdvanceUntil(s1, pos2);
                }
            }
            if (pos1 < thisSize)
            {
                containers.CopyRange(pos1, thisSize, intersectionSize);
                intersectionSize += thisSize - pos1;
            }
            containers.Resize(intersectionSize);
        }