Esempio n. 1
0
 /**
  * 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).
  *
  * @param from the position of the first element to be replaced in the receiver
  * @param to the position of the last element to be replaced in the receiver
  * @param other list holding elements to be copied into the receiver.
  * @param otherFrom position of first element within other list to be copied.
  */
 public void replaceFromToWithFrom(int from, int to, AbstractByteList other, int otherFrom) {
   // overridden for performance only.
   if (!(other instanceof ByteArrayList)) {
     // slower
     super.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());
     System.arraycopy(((ByteArrayList) other).elements, otherFrom, elements, from, length);
   }
 }
Esempio n. 2
0
  /**
   * Removes from the receiver all elements that are contained in the specified list. Tests for
   * identity.
   *
   * @param other the other list.
   * @return <code>true</code> if the receiver changed as a result of the call.
   */
  public boolean removeAll(AbstractByteList other) {
    // overridden for performance only.
    if (!(other instanceof ByteArrayList)) return super.removeAll(other);

    /*
     * There are two possibilities to do the thing a) use other.indexOf(...)
     * b) sort other, then use other.binarySearch(...)
     *
     * Let's try to figure out which one is faster. 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;
    byte[] 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
      ByteArrayList sortedList = (ByteArrayList) 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;
  }