Example #1
0
  /**
   * Compares the specified Object with the receiver. 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.
   *
   * @param otherObj the Object to be compared for equality with the receiver.
   * @return true if the specified Object is equal to the receiver.
   */
  public boolean equals(Object otherObj) { // delta
    if (otherObj == null) {
      return false;
    }
    // overridden for performance only.
    if (!(otherObj instanceof FloatArrayList)) {
      return super.equals(otherObj);
    }
    if (this == otherObj) {
      return true;
    }
    FloatArrayList other = (FloatArrayList) otherObj;
    if (size() != other.size()) {
      return false;
    }

    float[] theElements = elements();
    float[] otherElements = other.elements();
    for (int i = size(); --i >= 0; ) {
      if (theElements[i] != otherElements[i]) {
        return false;
      }
    }
    return true;
  }
Example #2
0
 /**
  * Returns a deep copy of the receiver.
  *
  * @return a deep copy of the receiver.
  */
 @Override
 public Object clone() {
   // overridden for performance only.
   FloatArrayList clone = new FloatArrayList(elements.clone());
   clone.setSizeRaw(size);
   return clone;
 }
  /**
   * Fills all values contained in the receiver into the specified list. Fills the list, starting at
   * index 0. After this call returns the specified list has a new size that equals
   * <tt>this.size()</tt>. Iteration order is guaranteed to be <i>identical</i> to the order used by
   * method {@link #forEachKey(LongProcedure)}.
   *
   * <p>This method can be used to iterate over the values of the receiver.
   *
   * @param list the list to be filled, can have any size.
   */
  @Override
  public void values(FloatArrayList list) {
    list.setSize(distinct);
    float[] elements = list.elements();

    int j = 0;
    for (int i = state.length; i-- > 0; ) {
      if (state[i] == FULL) {
        elements[j++] = values[i];
      }
    }
  }
  /**
   * Fills all pairs satisfying a given condition into the specified lists. Fills into the lists,
   * starting at index 0. After this call returns the specified lists both have a new size, the
   * number of pairs satisfying the condition. Iteration order is guaranteed to be <i>identical</i>
   * to the order used by method {@link #forEachKey(LongProcedure)}.
   *
   * <p><b>Example:</b> <br>
   *
   * <pre>
   * LongFloatProcedure condition = new LongFloatProcedure() { // match even values only
   * public boolean apply(long key, float value) { return value%2==0; }
   * }
   * keys = (8,7,6), values = (1,2,2) --> keyList = (6,8), valueList = (2,1)</tt>
   * </pre>
   *
   * @param condition the condition to be matched. Takes the current key as first and the current
   *     value as second argument.
   * @param keyList the list to be filled with keys, can have any size.
   * @param valueList the list to be filled with values, can have any size.
   */
  @Override
  public void pairsMatching(
      LongFloatProcedure condition, LongArrayList keyList, FloatArrayList valueList) {
    keyList.clear();
    valueList.clear();

    for (int i = table.length; i-- > 0; ) {
      if (state[i] == FULL && condition.apply(table[i], values[i])) {
        keyList.add(table[i]);
        valueList.add(values[i]);
      }
    }
  }
Example #5
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.
   */
  @Override
  public boolean removeAll(AbstractFloatList other) {
    // overridden for performance only.
    if (!(other instanceof FloatArrayList)) {
      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.isEmpty()) {
      return false;
    } // nothing to do
    int limit = other.size() - 1;
    int j = 0;
    float[] theElements = elements;
    int mySize = size();

    double N = (double) other.size();
    double M = (double) mySize;
    if ((N + M) * org.apache.mahout.collections.Arithmetic.log2(N) < M * N) {
      // it is faster to sort other before searching in it
      FloatArrayList sortedList = (FloatArrayList) 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;
  }