/// <summary>
        /// Replaces a number of elements in the receiver with the same number of elements of another list.
        /// Replaces elements in the receiver, between <code>from</code> (inclusive) and <code>to</code> (inclusive),
        /// with elements of <code>other</code>, starting from <code>otherFrom</code> (inclusive).
        ///
        /// <summary>
        /// <param name="from">the position of the first element to be replaced in the receiver</param>
        /// <param name="to">the position of the last element to be replaced in the receiver</param>
        /// <param name="other">list holding elements to be copied into the receiver.</param>
        /// <param name="otherFrom">position of first element within other list to be copied.</param>
        public virtual void ReplaceFromToWithFrom(int from, int to, AbstractBooleanList other, int otherFrom)
        {
            int Length = to - from + 1;

            if (Length > 0)
            {
                CheckRangeFromTo(from, to, _size);
                CheckRangeFromTo(otherFrom, otherFrom + Length - 1, other.Size);

                // unambiguous copy (it may hold other==this)
                if (from <= otherFrom)
                {
                    for (; --Length >= 0;)
                    {
                        SetQuick(from++, other.GetQuick(otherFrom++));
                    }
                }
                else
                {
                    int otherTo = otherFrom + Length - 1;
                    for (; --Length >= 0;)
                    {
                        SetQuick(to--, other.GetQuick(otherTo--));
                    }
                }
            }
        }
        /// <summary>
        /// Retains (keeps) only the elements in the receiver that are contained in the specified other list.
        /// In other words, removes from the receiver all of its elements that are not contained in the
        /// specified other listd
        /// <summary>
        /// <param name="other">the other list to test against.</param>
        /// <returns><code>true</code> if the receiver changed as a result of the call.</returns>
        public virtual Boolean RetainAll(AbstractBooleanList other)
        {
            if (other.Size == 0)
            {
                if (_size == 0)
                {
                    return(false);
                }
                SetSizeRaw(0);
                return(true);
            }

            int limit = other.Size - 1;
            int j     = 0;

            for (int i = 0; i < _size; i++)
            {
                if (other.IndexOfFromTo(GetQuick(i), 0, limit) >= 0)
                {
                    SetQuick(j++, GetQuick(i));
                }
            }

            Boolean modified = (j != _size);

            SetSizeRaw(j);
            return(modified);
        }
        /// <summary>
        /// Compares the specified Object with the receiverd
        /// Returns true if and only if the specified Object is also an ArrayList of the same type, both Lists have the
        /// same _size, and all corresponding pairs of elements in the two Lists are identical.
        /// In other words, two Lists are defined to be equal if they contain the
        /// same elements in the same order.
        ///
        /// <summary>
        /// <param name="otherObj">the Object to be compared for equality with the receiver.</param>
        /// <returns>true if the specified Object is equal to the receiver.</returns>
        public override Boolean Equals(Object otherObj)
        { //delta
            if (!(otherObj is AbstractBooleanList))
            {
                return(false);
            }
            if (this == otherObj)
            {
                return(true);
            }
            if (otherObj == null)
            {
                return(false);
            }
            AbstractBooleanList other = (AbstractBooleanList)otherObj;

            if (_size != other.Size)
            {
                return(false);
            }

            for (int i = _size; --i >= 0;)
            {
                if (GetQuick(i) != other.GetQuick(i))
                {
                    return(false);
                }
            }
            return(true);
        }
        /// <summary>
        /// Inserts the part of the specified list between <code>otherFrom</code> (inclusive) and <code>otherTo</code> (inclusive) before the specified position into the receiverd
        /// Shifts the element currently at that position (if any) and
        /// any subsequent elements to the right.
        ///
        /// <summary>
        /// <param name="index">index before which to insert first element from the specified list (must be in [0,_size])..</param>
        /// <param name="other">list of which a part is to be inserted into the receiver.</param>
        /// <param name="from">the index of the first element to be inserted (inclusive).</param>
        /// <param name="to">the index of the last element to be inserted (inclusive).</param>
        /// <exception cref="IndexOutOfRangeException">index is out of range (<i>other.Size&gt;0 && (from&lt;0 || from&gt;to || to&gt;=other.Size)</i>). </exception>
        /// <exception cref="IndexOutOfRangeException">index is out of range (<i>index &lt; 0 || index &gt; _size</i>). </exception>
        public virtual void BeforeInsertAllOfFromTo(int index, AbstractBooleanList other, int from, int to)
        {
            int Length = to - from + 1;

            this.BeforeInsertDummies(index, Length);
            this.ReplaceFromToWithFrom(index, index + Length - 1, other, from);
        }
        /// <summary>
        /// Retains (keeps) only the _elements in the receiver that are contained in the specified other list.
        /// In other words, removes from the receiver all of its _elements that are not contained in the
        /// specified other listd
        /// <summary>
        /// <param name="other">the other list to test against.</param>
        /// <returns><code>true</code> if the receiver changed as a result of the call.</returns>
        public override Boolean RetainAll(AbstractBooleanList other)
        {
            // overridden for performance only.
            if (!(other is BooleanArrayList))
            {
                return(base.RetainAll(other));
            }

            /* There are two possibilities to do the thing
             * a) use other.IndexOf(..d)
             * b) sort other, then use other.BinarySearch(..d)
             *
             * Let's try to figure out which one is fasterd Let M=Size, N=other.Size, then
             * a) takes O(M*N) steps
             * b) takes O(N*logN + M*logN) steps (sorting is O(N*logN) and binarySearch is O(logN))
             *
             * Hence, if N*logN + M*logN < M*N, we use b) otherwise we use a).
             */
            int limit = other.Size - 1;
            int j     = 0;

            Boolean[] the_elements = _elements;
            int       mySize       = Size;

            double N = (double)other.Size;
            double M = (double)mySize;

            if ((N + M) * Cern.Jet.Math.Arithmetic.Log2(N) < M * N)
            {
                // it is faster to sort other before searching in it
                BooleanArrayList sortedList = (BooleanArrayList)other.Clone();
                sortedList.QuickSort();

                for (int i = 0; i < mySize; i++)
                {
                    if (sortedList.BinarySearchFromTo(the_elements[i], 0, limit) >= 0)
                    {
                        the_elements[j++] = the_elements[i];
                    }
                }
            }
            else
            {
                // it is faster to search in other without sorting
                for (int i = 0; i < mySize; i++)
                {
                    if (other.IndexOfFromTo(the_elements[i], 0, limit) >= 0)
                    {
                        the_elements[j++] = the_elements[i];
                    }
                }
            }

            Boolean modified = (j != mySize);

            SetSize(j);
            return(modified);
        }
        /// <summary>
        /// Replaces a number of _elements in the receiver with the same number of _elements of another list.
        /// Replaces _elements in the receiver, between <code>from</code> (inclusive) and <code>to</code> (inclusive),
        /// with _elements of <code>other</code>, starting from <code>otherFrom</code> (inclusive).
        ///
        /// <summary>
        /// <param name="from">the position of the first element to be replaced in the receiver</param>
        /// <param name="to">the position of the last element to be replaced in the receiver</param>
        /// <param name="other">list holding _elements to be copied into the receiver.</param>
        /// <param name="otherFrom">position of first element within other list to be copied.</param>
        public override void ReplaceFromToWithFrom(int from, int to, AbstractBooleanList other, int otherFrom)
        {
            // overridden for performance only.
            if (!(other is BooleanArrayList))
            {
                // slower
                base.ReplaceFromToWithFrom(from, to, other, otherFrom);
                return;
            }
            int Length = to - from + 1;

            if (Length > 0)
            {
                CheckRangeFromTo(from, to, Size);
                CheckRangeFromTo(otherFrom, otherFrom + Length - 1, other.Size);
                Array.Copy(((BooleanArrayList)other)._elements, otherFrom, _elements, from, Length);
            }
        }
        /// <summary>
        /// Replaces the part between <code>from</code> (inclusive) and <code>to</code> (inclusive) with the other list's
        /// part between <code>otherFrom</code> and <code>otherTo</code>d
        /// Powerful (and tricky) method!
        /// Both parts need not be of the same _size (part A can both be smaller or larger than part B).
        /// Parts may overlap.
        /// Receiver and other list may (but most not) be identical.
        /// If <code>from &gt; to</code>, then inserts other part before <code>from</code>.
        ///
        /// <summary>
        /// <param name="from">the first element of the receiver (inclusive)</param>
        /// <param name="to">the last element of the receiver (inclusive)</param>
        /// <param name="other">the other list (may be identical with receiver)</param>
        /// <param name="otherFrom">the first element of the other list (inclusive)</param>
        /// <param name="otherTo">the last element of the other list (inclusive)</param>
        ///
        /// <p><b>Examples:</b><pre>
        /// a=[0, 1, 2, 3, 4, 5, 6, 7]
        /// b=[50, 60, 70, 80, 90]
        /// a.R(..d)=a.replaceFromToWithFromTo(..d)
        ///
        /// a.R(3,5,b,0,4)-->[0, 1, 2, 50, 60, 70, 80, 90, 6, 7]
        /// a.R(1,6,b,0,4)-->[0, 50, 60, 70, 80, 90, 7]
        /// a.R(0,6,b,0,4)-->[50, 60, 70, 80, 90, 7]
        /// a.R(3,5,b,1,2)-->[0, 1, 2, 60, 70, 6, 7]
        /// a.R(1,6,b,1,2)-->[0, 60, 70, 7]
        /// a.R(0,6,b,1,2)-->[60, 70, 7]
        /// a.R(5,3,b,0,4)-->[0, 1, 2, 3, 4, 50, 60, 70, 80, 90, 5, 6, 7]
        /// a.R(5,0,b,0,4)-->[0, 1, 2, 3, 4, 50, 60, 70, 80, 90, 5, 6, 7]
        /// a.R(5,3,b,1,2)-->[0, 1, 2, 3, 4, 60, 70, 5, 6, 7]
        /// a.R(5,0,b,1,2)-->[0, 1, 2, 3, 4, 60, 70, 5, 6, 7]
        ///
        /// Extreme cases:
        /// a.R(5,3,b,0,0)-->[0, 1, 2, 3, 4, 50, 5, 6, 7]
        /// a.R(5,3,b,4,4)-->[0, 1, 2, 3, 4, 90, 5, 6, 7]
        /// a.R(3,5,a,0,1)-->[0, 1, 2, 0, 1, 6, 7]
        /// a.R(3,5,a,3,5)-->[0, 1, 2, 3, 4, 5, 6, 7]
        /// a.R(3,5,a,4,4)-->[0, 1, 2, 4, 6, 7]
        /// a.R(5,3,a,0,4)-->[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7]
        /// a.R(0,-1,b,0,4)-->[50, 60, 70, 80, 90, 0, 1, 2, 3, 4, 5, 6, 7]
        /// a.R(0,-1,a,0,4)-->[0, 1, 2, 3, 4, 0, 1, 2, 3, 4, 5, 6, 7]
        /// a.R(8,0,a,0,4)-->[0, 1, 2, 3, 4, 5, 6, 7, 0, 1, 2, 3, 4]
        /// </pre>
        public virtual void ReplaceFromToWithFromTo(int from, int to, AbstractBooleanList other, int otherFrom, int otherTo)
        {
            if (otherFrom > otherTo)
            {
                throw new IndexOutOfRangeException("otherFrom: " + otherFrom + ", otherTo: " + otherTo);
            }

            if (this == other && to - from != otherTo - otherFrom)
            { // avoid stumbling over my own feet
                ReplaceFromToWithFromTo(from, to, PartFromTo(otherFrom, otherTo), 0, otherTo - otherFrom);
                return;
            }

            int Length  = otherTo - otherFrom + 1;
            int diff    = Length;
            int theLast = from - 1;

            if (to >= from)
            {
                diff   -= (to - from + 1);
                theLast = to;
            }

            if (diff > 0)
            {
                BeforeInsertDummies(theLast + 1, diff);
            }
            else
            {
                if (diff < 0)
                {
                    RemoveFromTo(theLast + diff, theLast - 1);
                }
            }

            if (Length > 0)
            {
                ReplaceFromToWithFrom(from, from + Length - 1, other, otherFrom);
            }
        }
 /// <summary>
 /// Appends the part of the specified list between <code>from</code> (inclusive) and <code>to</code> (inclusive) to the receiver.
 ///
 /// <summary>
 /// <param name="other">the list to be added to the receiver.</param>
 /// <param name="from">the index of the first element to be appended (inclusive).</param>
 /// <param name="to">the index of the last element to be appended (inclusive).</param>
 /// <exception cref="IndexOutOfRangeException">index is out of range (<i>other.Size&gt;0 && (from&lt;0 || from&gt;to || to&gt;=other.Size)</i>). </exception>
 public virtual void AddAllOfFromTo(AbstractBooleanList other, int from, int to)
 {
     BeforeInsertAllOfFromTo(_size, other, from, to);
 }