Esempio n. 1
0
        /// <summary>
        /// Removes from the receiver all elements that are contained in the specified list.
        /// Tests for identity.
        ///
        /// <summary>
        /// <param name="other">the other list.</param>
        /// <returns><code>true</code> if the receiver changed as a result of the call.</returns>
        public override Boolean RemoveAll(AbstractIntList other)
        {
            // overridden for performance only.
            if (!(other is IntArrayList))
            {
                return(base.RemoveAll(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).
             */
            if (other.Size == 0)
            {
                return(false);
            }                                      //nothing to do
            int limit = other.Size - 1;
            int j     = 0;

            int[] theElements = _elements;
            int   mySize      = Size;

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

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

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

            Boolean modified = (j != mySize);

            SetSize(j);
            return(modified);
        }
Esempio n. 2
0
 /// <summary>
 /// Appends all elements of the specified list to the receiver.
 /// <summary>
 /// <param name="list">the list of which all elements shall be appended.</param>
 public virtual void AddAllOf(IntArrayList other)
 {
     AddAllOfFromTo(other, 0, other.Size - 1);
 }