Ejemplo n.º 1
0
  /**
   * Reverses the elements of the receiver. Last becomes first, second last becomes second first,
   * and so on.
   */
  public void reverse() {
    long tmp;
    int limit = size() / 2;
    int j = size() - 1;

    for (int i = 0; i < limit; ) { // swap
      tmp = getQuick(i);
      setQuick(i++, getQuick(j));
      setQuick(j--, tmp);
    }
  }
Ejemplo n.º 2
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, AbstractLongList 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--));
      }
    }
  }
Ejemplo n.º 3
0
  /**
   * Randomly permutes the part of the receiver between <code>from</code> (inclusive) and <code>to
   * </code> (inclusive).
   *
   * @param from the index of the first element (inclusive) to be permuted.
   * @param to the index of the last element (inclusive) to be permuted.
   * @exception IndexOutOfBoundsException index is out of range (<tt>size()&gt;0 && (from&lt;0 ||
   *     from&gt;to || to&gt;=size())</tt>).
   */
  public void shuffleFromTo(int from, int to) {
    checkRangeFromTo(from, to, size());

    // cern.jet.random.Uniform gen = new cern.jet.random.Uniform(new
    // cern.jet.random.engine.DRand(new java.util.Date()));
    java.util.Random gen = new java.util.Random();
    for (int i = from; i < to; i++) {
      int rp = gen.nextInt(to - i);
      int random = rp + i;

      //		int random = gen.nextIntFromTo(i, to);

      // swap(i, random)
      long tmpElement = getQuick(random);
      setQuick(random, getQuick(i));
      setQuick(i, tmpElement);
    }
  }
Ejemplo n.º 4
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(AbstractLongList other) {
    if (other.size() == 0) return false; // nothing to do
    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);
    setSize(j);
    return modified;
  }
Ejemplo n.º 5
0
  /**
   * 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 list.
   *
   * @param other the other list to test against.
   * @return <code>true</code> if the receiver changed as a result of the call.
   */
  public boolean retainAll(AbstractLongList other) {
    if (other.size() == 0) {
      if (size == 0) return false;
      setSize(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);
    setSize(j);
    return modified;
  }
Ejemplo n.º 6
0
 /**
  * Replaces the element at the specified position in the receiver with the specified element.
  *
  * @param index index of element to replace.
  * @param element element to be stored at the specified position.
  * @throws IndexOutOfBoundsException if <tt>index &lt; 0 || index &gt;= size()</tt>.
  */
 public void set(int index, long element) {
   if (index >= size || index < 0)
     throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + size);
   setQuick(index, element);
 }
Ejemplo n.º 7
0
 /**
  * Sets the specified range of elements in the specified array to the specified value.
  *
  * @param from the index of the first element (inclusive) to be filled with the specified value.
  * @param to the index of the last element (inclusive) to be filled with the specified value.
  * @param val the value to be stored in the specified elements of the receiver.
  */
 public void fillFromToWith(int from, int to, long val) {
   checkRangeFromTo(from, to, this.size);
   for (int i = from; i <= to; ) setQuick(i++, val);
 }