BitsetsNET.ArrayContainer.IAdd C# (CSharp) Method

IAdd() public method

Adds range of elements (in-place) to this container.
public IAdd ( ushort begin, ushort end ) : Container
begin ushort Start of range (inclusive)
end ushort End of range (exclusive)
return Container
        public override Container IAdd(ushort begin, ushort end)
        {
            // TODO: may need to convert to a RunContainer
            if (end == begin)
            {
                return this;
            }
            if (begin > end)
            {
                throw new ArgumentException("Invalid range [" + begin + "," + end + ")");
            }

            int indexStart = Utility.UnsignedBinarySearch(Content, 0, Cardinality, begin);
            if (indexStart < 0)
            {
                indexStart = -indexStart - 1;
            }

            int indexEnd = Utility.UnsignedBinarySearch(Content, 0, Cardinality, (ushort)(end - 1));
            if (indexEnd < 0)
            {
                indexEnd = -indexEnd - 1;
            }
            else
            {
                indexEnd++;
            }

            int rangeLength = end - begin;
            int newCardinality = indexStart + (Cardinality - indexEnd) + rangeLength;
            if (newCardinality > DEFAULT_MAX_SIZE)
            {
                BitsetContainer a = this.ToBitsetContainer();
                return a.IAdd(begin, end);
            }

            if (newCardinality >= this.Content.Length)
            {
                IncreaseCapacity(newCardinality);
            }

            Array.Copy(this.Content, indexEnd, this.Content, indexStart + rangeLength, Cardinality - indexEnd);
            for (int k = 0; k < rangeLength; ++k)
            {
                Content[k + indexStart] = (ushort)(begin + k);
            }
            Cardinality = newCardinality;
            return this;
        }

Usage Example

Example #1
0
        /// <summary>
        /// Create a container initialized with a range of consecutive values.
        /// </summary>
        /// <param name="start">First index</param>
        /// <param name="last">Last index</param>
        /// <returns>A new container initialized with the specified values</returns>
        /// <remarks>In the original lemire version, there is some optimization here
        /// to choose between an ArrayContainer and a RunContainer based on serialized size.
        /// For now, this has been stripped out and always uses an ArrayContainer.</remarks>
        public static Container RangeOfOnes(ushort start, ushort last)
        {
            //TODO: Add in logic for RunContainers
            Container answer = new ArrayContainer();

            answer = answer.IAdd(start, last);
            return(answer);
        }