/// <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 // overridden for performance only. if (!(otherObj is DoubleArrayList)) { return(base.Equals(otherObj)); } if (this == otherObj) { return(true); } if (otherObj == null) { return(false); } DoubleArrayList other = (DoubleArrayList)otherObj; if (Size != other.Size) { return(false); } double[] theElements = GetElements(); double[] otherElements = other.ToArray(); for (int i = Size; --i >= 0;) { if (theElements[i] != otherElements[i]) { return(false); } } return(true); }
/// <summary> /// Returns a deep copy of the receiverd /// /// <summary> /// <returns> a deep copy of the receiver.</returns> public override Object Clone() { // overridden for performance only. DoubleArrayList clone = new DoubleArrayList((double[])_elements.Clone()); clone.SetSizeRaw(Size); return(clone); }
/// <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(AbstractDoubleList other) { // overridden for performance only. if (!(other is DoubleArrayList)) { 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; double[] theElements = _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 DoubleArrayList sortedList = (DoubleArrayList)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); }
/// <summary> /// Returns a list which is a concatenation of <code>times</code> times the receiver. /// <summary> /// <param name="times">the number of times the receiver shall be copied.</param> public virtual AbstractDoubleList Times(int times) { AbstractDoubleList newList = new DoubleArrayList(times * Size); for (int i = times; --i >= 0;) { newList.AddAllOfFromTo(this, 0, Size - 1); } return(newList); }
/// <summary> /// Returns a new list of the part of the receiver between <code>from</code>, inclusive, and <code>to</code>, inclusive. /// <summary> /// <param name="from">the index of the first element (inclusive).</param> /// <param name="to">the index of the last element (inclusive).</param> /// <returns>a new list</returns> /// <exception cref="IndexOutOfRangeException">index is out of range (<i>_size()>0 && (from<0 || from>to || to>=_size())</i>). </exception> public virtual AbstractDoubleList PartFromTo(int from, int to) { CheckRangeFromTo(from, to, _size); int Length = to - from + 1; var part = new DoubleArrayList(Length); part.AddAllOfFromTo(this, from, to); return(part); }
/// <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 // overridden for performance only. if ((otherObj is AbstractDoubleList)) { return(base.Equals(otherObj)); } if (this == otherObj) { return(true); } if (otherObj == null) { return(false); } double[] otherElements = null; if (otherObj is DoubleArrayList) { DoubleArrayList other = (DoubleArrayList)otherObj; if (Size != other.Size) { return(false); } otherElements = other.ToArray(); } else if (otherObj.IsList <Double>()) { otherElements = ((List <Double>)otherObj).ToArray <Double>(); } else if (otherObj.GetType().IsArray) { otherElements = (double[])otherObj; } if (otherElements != null) { double[] theElements = GetElements(); for (int i = Size; --i >= 0;) { if (theElements[i] != otherElements[i]) { return(false); } } } return(true); }
/// <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(DoubleArrayList other) { AddAllOfFromTo(other, 0, other.Size - 1); }