BitsetsNET.ArrayContainer.NegateRange C# (CSharp) Method

NegateRange() private method

private NegateRange ( ushort buffer, int startIndex, int lastIndex, int startRange, int lastRange ) : void
buffer ushort
startIndex int
lastIndex int
startRange int
lastRange int
return void
        private void NegateRange(ushort[] buffer, int startIndex, int lastIndex, int startRange, int lastRange)
        {
            // compute the negation into buffer

            int outPos = 0;
            int inPos = startIndex; // value here always >= valInRange,
                                    // until it is exhausted
                                    // n.b., we can start initially exhausted.

            int valInRange = startRange;
            for (; valInRange < lastRange && inPos <= lastIndex; ++valInRange)
            {
                if ((short)valInRange != Content[inPos])
                {
                    buffer[outPos++] = (ushort)valInRange;
                }
                else
                {
                    ++inPos;
                }
            }

            // if there are extra items (greater than the biggest
            // pre-existing one in range), buffer them
            for (; valInRange < lastRange; ++valInRange)
            {
                buffer[outPos++] = (ushort)valInRange;
            }

            if (outPos != buffer.Length)
            {
                throw new SystemException("negateRange: outPos " + outPos +
                                          " whereas buffer.length=" + buffer.Length);
            }
            // copy back from buffer...caller must ensure there is room
            int i = startIndex;
            foreach (ushort item in buffer)
            {
                Content[i++] = item;
            }
        }