Пример #1
0
        public override Container add(ushort x)
        {
            int loc = Utility.unsignedBinarySearch(content, 0, cardinality, x);

            // if the location is positive, it means the number being added already existed in the
            // array, so no need to do anything.

            // if the location is negative, we did not find the value in the array. The location represents
            // the negative value of the position in the array (not the index) where we want to add the value
            if (loc < 0)
            {
                // Transform the ArrayContainer to a BitmapContainer
                // when cardinality = DEFAULT_MAX_SIZE
                if (cardinality >= DEFAULT_MAX_SIZE)
                {
                    BitsetContainer a = this.toBitsetContainer();
                    a.add(x);
                    return(a);
                }
                if (cardinality >= this.content.Length)
                {
                    increaseCapacity();
                }

                // insertion : shift the elements > x by one position to the right
                // and put x in its appropriate place
                Array.Copy(content, -loc - 1, content, -loc, cardinality + loc + 1);
                content[-loc - 1] = x;
                ++cardinality;
            }
            return(this);
        }
Пример #2
0
        public override Container add(ushort begin, ushort 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.add(begin, end));
            }

            if (newcardinality >= this.content.Length)
            {
                increaseCapacity(newcardinality);
            }

            Array.Copy(content, indexend, this.content, indexstart
                       + rangelength, cardinality - indexend);

            for (int k = 0; k < rangelength; ++k)
            {
                this.content[k + indexstart] = (ushort)(begin + k);
            }

            this.cardinality = newcardinality;

            return(this);
        }