Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
  
  /*
 Copyright ï¿½ 1999 CERN - European Organization for Nuclear Research.
 Permission to use, copy, modify, distribute and sell this software and its documentation for any purpose 
 is hereby granted without fee, provided that the above copyright notice appear in all copies and 
 that both that copyright notice and this permission notice appear in supporting documentation. 
 CERN makes no representations about the suitability of this software for any purpose. 
 It is provided "as is" without expressed or implied warranty.
 */
 package org.apache.mahout.math.list;
 //CHECKSTYLE:OFF
 //CHECKSTYLE:ON
 
 import java.util.List;

Abstract base class for resizable lists holding int elements; abstract.
 
 public abstract class AbstractIntList extends AbstractList implements IntBufferConsumerCloneable {

  
The size of the list. This is a READ_ONLY variable for all methods but setSizeRaw(int newSize) !!! If you violate this principle in subclasses, you should exactly know what you are doing.
 
   protected int size;

  
Appends the specified element to the end of this list.

Parameters:
element element to be appended to this list.
 
   public void add(int element) {
     beforeInsert(element);
   }

  
Appends all elements of the specified list to the receiver.

Parameters:
other the list of which all elements shall be appended.
 
   public void addAllOf(AbstractIntList other) {
     addAllOfFromTo(other, 0, other.size() - 1);
   }

  
Appends the part of the specified list between from (inclusive) and to (inclusive) to the receiver.

Parameters:
other the list to be added to the receiver.
from the index of the first element to be appended (inclusive).
to the index of the last element to be appended (inclusive).
Throws:
java.lang.IndexOutOfBoundsException index is out of range (other.size()>0 && (from<0 || from>to || to>=other.size())).
 
   public void addAllOfFromTo(AbstractIntList otherint fromint to) {
     beforeInsertAllOfFromTo(otherfromto);
   }
  
  
Appends the specified list to the end of this list.

Parameters:
other the list to be appended.
 
   @Override
   public void addAllOf(IntArrayList other) {
     addAllOfFromTo(other, 0, other.size() - 1);
   }

  
Inserts the specified element before the specified position into the receiver. Shifts the element currently at that position (if any) and any subsequent elements to the right.

Parameters:
index index before which the specified element is to be inserted (must be in [0,size]).
element element to be inserted.
Throws:
java.lang.IndexOutOfBoundsException index is out of range (index < 0 || index > size()).
 
   public void beforeInsert(int indexint element) {
    beforeInsertDummies(index, 1);
    set(indexelement);
  }

  
Inserts the part of the specified list between otherFrom (inclusive) and otherTo (inclusive) before the specified position into the receiver. Shifts the element currently at that position (if any) and any subsequent elements to the right.

Parameters:
index index before which to insert first element from the specified list (must be in [0,size])..
other list of which a part is to be inserted into the receiver.
from the index of the first element to be inserted (inclusive).
to the index of the last element to be inserted (inclusive).
Throws:
java.lang.IndexOutOfBoundsException index is out of range (other.size()>0 && (from<0 || from>to || to>=other.size())).
java.lang.IndexOutOfBoundsException index is out of range (index < 0 || index > size()).
  public void beforeInsertAllOfFromTo(int indexAbstractIntList otherint fromint to) {
    int length = to - from + 1;
    this.beforeInsertDummies(indexlength);
    this.replaceFromToWithFrom(indexindex + length - 1, otherfrom);
  }

  
Inserts length dummy elements before the specified position into the receiver. Shifts the element currently at that position (if any) and any subsequent elements to the right. This method must set the new size to be size()+length.

Parameters:
index index before which to insert dummy elements (must be in [0,size])..
length number of dummy elements to be inserted.
Throws:
java.lang.IndexOutOfBoundsException if index < 0 || index > size().
  protected void beforeInsertDummies(int indexint length) {
    if (index >  || index < 0) {
      throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + );
    }
    if (length > 0) {
      ensureCapacity( + length);
      setSizeRaw( + length);
      replaceFromToWithFrom(index + length - 1, thisindex);
    }
  }

  
Searches the receiver for the specified value using the binary search algorithm. The receiver must must be sorted (as by the sort method) prior to making this call. If it is not sorted, the results are undefined: in particular, the call may enter an infinite loop. If the receiver contains multiple elements equal to the specified object, there is no guarantee which instance will be found.

Parameters:
key the value to be searched for.
Returns:
index of the search key, if it is contained in the receiver; otherwise, (-(insertion point) - 1). The insertion point is defined as the the point at which the value would be inserted into the receiver: the index of the first element greater than the key, or receiver.size(), if all elements in the receiver are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
See also:
java.util.Arrays
  public int binarySearch(int key) {
    return this.binarySearchFromTo(key, 0,  - 1);
  }

  
Searches the receiver for the specified value using the binary search algorithm. The receiver must must be sorted (as by the sort method) prior to making this call. If it is not sorted, the results are undefined: in particular, the call may enter an infinite loop. If the receiver contains multiple elements equal to the specified object, there is no guarantee which instance will be found.

Parameters:
key the value to be searched for.
from the leftmost search position, inclusive.
to the rightmost search position, inclusive.
Returns:
index of the search key, if it is contained in the receiver; otherwise, (-(insertion point) - 1). The insertion point is defined as the the point at which the value would be inserted into the receiver: the index of the first element greater than the key, or receiver.size(), if all elements in the receiver are less than the specified key. Note that this guarantees that the return value will be >= 0 if and only if the key is found.
See also:
java.util.Arrays
  public int binarySearchFromTo(int keyint fromint to) {
    int low = from;
    int high = to;
    while (low <= high) {
      int mid = (low + high) / 2;
      int midVal = get(mid);
      if (midVal < key) {
        low = mid + 1;
      } else if (midVal > key) {
        high = mid - 1;
      } else {
        return mid;
      } // key found
    }
    return -(low + 1);  // key not found.
  }

  
Returns a deep copy of the receiver.

Returns:
a deep copy of the receiver.
  public Object clone() {
    return partFromTo(0,  - 1);
  }

  
Returns true if the receiver contains the specified element.

Parameters:
elem element whose presence in the receiver is to be tested.
  public boolean contains(int elem) {
    return indexOfFromTo(elem, 0,  - 1) >= 0;
  }

  
Deletes the first element from the receiver that is identical to the specified element. Does nothing, if no such matching element is contained.

Parameters:
element the element to be deleted.
  public void delete(int element) {
    int index = indexOfFromTo(element, 0,  - 1);
    if (index >= 0) {
      remove(index);
    }
  }

  
Returns the elements currently stored, possibly including invalid elements between size and capacity. WARNING: For efficiency reasons and to keep memory usage low, this method may decide not to copy the array. So if subsequently you modify the returned array directly via the [] operator, be sure you know what you're doing.

Returns:
the elements currently stored.
  public int[] elements() {
    int[] myElements = new int[];
    for (int i = ; --i >= 0;) {
      myElements[i] = getQuick(i);
    }
    return myElements;
  }

  
Sets the receiver's elements to be the specified array. The size and capacity of the list is the length of the array. WARNING: For efficiency reasons and to keep memory usage low, this method may decide not to copy the array. So if subsequently you modify the returned array directly via the [] operator, be sure you know what you're doing.

Parameters:
elements the new elements to be stored.
Returns:
the receiver itself.
  public AbstractIntList elements(int[] elements) {
    clear();
    addAllOfFromTo(new IntArrayList(elements), 0, elements.length - 1);
    return this;
  }

  
Ensures that the receiver can hold at least the specified number of elements without needing to allocate new internal memory. If necessary, allocates new internal memory and increases the capacity of the receiver.

Parameters:
minCapacity the desired minimum capacity.
  public abstract void ensureCapacity(int minCapacity);

  
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.

Parameters:
otherObj the Object to be compared for equality with the receiver.
Returns:
true if the specified Object is equal to the receiver.
  public boolean equals(Object otherObj) { //delta
    if (otherObj == null) {
      return false;
    }
    if (!(otherObj instanceof AbstractIntList)) {
      return false;
    }
    if (this == otherObj) {
      return true;
    }
    AbstractIntList other = (AbstractIntListotherObj;
    if (size() != other.size()) {
      return false;
    }
    for (int i = size(); --i >= 0;) {
      if (getQuick(i) != other.getQuick(i)) {
        return false;
      }
    }
    return true;
  }

  
Sets the specified range of elements in the specified array to the specified value.

Parameters:
from the index of the first element (inclusive) to be filled with the specified value.
to the index of the last element (inclusive) to be filled with the specified value.
val the value to be stored in the specified elements of the receiver.
  public void fillFromToWith(int fromint toint val) {
    checkRangeFromTo(fromtothis.);
    for (int i = fromi <= to;) {
      setQuick(i++, val);
    }
  }

  
Applies a procedure to each element of the receiver, if any. Starts at index 0, moving rightwards.

Parameters:
procedure the procedure to be applied. Stops iteration if the procedure returns false, otherwise continues.
Returns:
false if the procedure stopped before all elements where iterated over, true otherwise.
  public boolean forEach(IntProcedure procedure) {
    for (int i = 0; i < ;) {
      if (!procedure.apply(get(i++))) {
        return false;
      }
    }
    return true;
  }

  
Returns the element at the specified position in the receiver.

Parameters:
index index of element to return.
Throws:
java.lang.IndexOutOfBoundsException index is out of range (index < 0 || index >= size()).
  public int get(int index) {
    if (index >=  || index < 0) {
      throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + );
    }
    return getQuick(index);
  }

  
Returns the element at the specified position in the receiver; WARNING: Does not check preconditions. Provided with invalid parameters this method may return invalid elements without throwing any exception! You should only use this method when you are absolutely sure that the index is within bounds. Precondition (unchecked): index >= 0 && index < size(). This method is normally only used internally in large loops where bounds are explicitly checked before the loop and need no be rechecked within the loop. However, when desperately, you can give this method public visibility in subclasses.

Parameters:
index index of element to return.
  protected abstract int getQuick(int index);

  
Returns the index of the first occurrence of the specified element. Returns -1 if the receiver does not contain this element.

Parameters:
element the element to be searched for.
Returns:
the index of the first occurrence of the element in the receiver; returns -1 if the element is not found.
  public int indexOf(int element) { //delta
    return indexOfFromTo(element, 0,  - 1);
  }

  
Returns the index of the first occurrence of the specified element. Returns -1 if the receiver does not contain this element. Searches between from, inclusive and to, inclusive. Tests for identity.

Parameters:
element element to search for.
from the leftmost search position, inclusive.
to the rightmost search position, inclusive.
Returns:
the index of the first occurrence of the element in the receiver; returns -1 if the element is not found.
Throws:
java.lang.IndexOutOfBoundsException index is out of range (size()>0 && (from<0 || from>to || to>=size())).
  public int indexOfFromTo(int elementint fromint to) {
    checkRangeFromTo(fromto);
    for (int i = fromi <= toi++) {
      if (element == getQuick(i)) {
        return i;
      } //found
    }
    return -1; //not found
  }

  
Returns the index of the last occurrence of the specified element. Returns -1 if the receiver does not contain this element.

Parameters:
element the element to be searched for.
Returns:
the index of the last occurrence of the element in the receiver; returns -1 if the element is not found.
  public int lastIndexOf(int element) {
    return lastIndexOfFromTo(element, 0,  - 1);
  }

  
Returns the index of the last occurrence of the specified element. Returns -1 if the receiver does not contain this element. Searches beginning at to, inclusive until from, inclusive. Tests for identity.

Parameters:
element element to search for.
from the leftmost search position, inclusive.
to the rightmost search position, inclusive.
Returns:
the index of the last occurrence of the element in the receiver; returns -1 if the element is not found.
Throws:
java.lang.IndexOutOfBoundsException index is out of range (size()>0 && (from<0 || from>to || to>=size())).
  public int lastIndexOfFromTo(int elementint fromint to) {
    checkRangeFromTo(fromtosize());
    for (int i = toi >= fromi--) {
      if (element == getQuick(i)) {
        return i;
      } //found
    }
    return -1; //not found
  }

  
Sorts the specified range of the receiver into ascending order. The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance, and can approach linear performance on nearly sorted lists.

You should never call this method unless you are sure that this particular sorting algorithm is the right one for your data set. It is generally better to call sort() or sortFromTo(...) instead, because those methods automatically choose the best sorting algorithm.

Parameters:
from the index of the first element (inclusive) to be sorted.
to the index of the last element (inclusive) to be sorted.
Throws:
java.lang.IndexOutOfBoundsException index is out of range (size()>0 && (from<0 || from>to || to>=size())).
  public void mergeSortFromTo(int fromint to) {
    int mySize = size();
    checkRangeFromTo(fromtomySize);
    int[] myElements = elements();
    Sorting.mergeSort(myElementsfromto + 1);
    elements(myElements);
    setSizeRaw(mySize);
  }

  
Sorts the receiver according to the order induced by the specified comparator. All elements in the range must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the range).

This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort.

The sorting algorithm is a modified mergesort (in which the merge is omitted if the highest element in the low sublist is less than the lowest element in the high sublist). This algorithm offers guaranteed n*log(n) performance, and can approach linear performance on nearly sorted lists.

Parameters:
from the index of the first element (inclusive) to be sorted.
to the index of the last element (inclusive) to be sorted.
c the comparator to determine the order of the receiver.
Throws:
java.lang.ClassCastException if the array contains elements that are not mutually comparable using the specified comparator.
java.lang.IllegalArgumentException if fromIndex > toIndex
java.lang.ArrayIndexOutOfBoundsException if fromIndex < 0 or toIndex > a.length
java.lang.IndexOutOfBoundsException index is out of range (size()>0 && (from<0 || from>to || to>=size())).
  public void mergeSortFromTo(int fromint toIntComparator c) {
    int mySize = size();
    checkRangeFromTo(fromtomySize);
    int[] myElements = elements();
    Sorting.mergeSort(myElementsfromto + 1, c);
    elements(myElements);
    setSizeRaw(mySize);
  }

  
Returns a new list of the part of the receiver between from, inclusive, and to, inclusive.

Parameters:
from the index of the first element (inclusive).
to the index of the last element (inclusive).
Returns:
a new list
Throws:
java.lang.IndexOutOfBoundsException index is out of range (size()>0 && (from<0 || from>to || to>=size())).
  public AbstractIntList partFromTo(int fromint to) {
    checkRangeFromTo(fromto);
    int length = to - from + 1;
    IntArrayList part = new IntArrayList(length);
    part.addAllOfFromTo(thisfromto);
    return part;
  }
  
  
Sorts the specified range of the receiver into ascending numerical order. The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.

You should never call this method unless you are sure that this particular sorting algorithm is the right one for your data set. It is generally better to call sort() or sortFromTo(...) instead, because those methods automatically choose the best sorting algorithm.

Parameters:
from the index of the first element (inclusive) to be sorted.
to the index of the last element (inclusive) to be sorted.
Throws:
java.lang.IndexOutOfBoundsException index is out of range (size()>0 && (from<0 || from>to || to>=size())).
  public void quickSortFromTo(int fromint to) {
    int mySize = size();
    checkRangeFromTo(fromtomySize);
    int[] myElements = elements();
    java.util.Arrays.sort(myElementsfromto + 1);
    elements(myElements);
    setSizeRaw(mySize);
  }

  
Sorts the receiver according to the order induced by the specified comparator. All elements in the range must be mutually comparable by the specified comparator (that is, c.compare(e1, e2) must not throw a ClassCastException for any elements e1 and e2 in the range).

The sorting algorithm is a tuned quicksort, adapted from Jon L. Bentley and M. Douglas McIlroy's "Engineering a Sort Function", Software-Practice and Experience, Vol. 23(11) P. 1249-1265 (November 1993). This algorithm offers n*log(n) performance on many data sets that cause other quicksorts to degrade to quadratic performance.

Parameters:
from the index of the first element (inclusive) to be sorted.
to the index of the last element (inclusive) to be sorted.
c the comparator to determine the order of the receiver.
Throws:
java.lang.ClassCastException if the array contains elements that are not mutually comparable using the specified comparator.
java.lang.IllegalArgumentException if fromIndex > toIndex
java.lang.ArrayIndexOutOfBoundsException if fromIndex < 0 or toIndex > a.length
java.lang.IndexOutOfBoundsException index is out of range (size()>0 && (from<0 || from>to || to>=size())).
  public void quickSortFromTo(int fromint toIntComparator c) {
    int mySize = size();
    checkRangeFromTo(fromtomySize);
    int[] myElements = elements();
    Sorting.quickSort(myElementsfromto + 1, c);
    elements(myElements);
    setSizeRaw(mySize);
  }

  
Removes from the receiver all elements that are contained in the specified list. Tests for identity.

Parameters:
other the other list.
Returns:
true if the receiver changed as a result of the call.
  public boolean removeAll(AbstractIntList other) {
    if (other.isEmpty()) {
      return false;
    } //nothing to do
    int limit = other.size() - 1;
    int j = 0;
    for (int i = 0; i < i++) {
      if (other.indexOfFromTo(getQuick(i), 0, limit) < 0) {
        setQuick(j++, getQuick(i));
      }
    }
    boolean modified = (j != );
    setSize(j);
    return modified;
  }

  
Removes from the receiver all elements whose index is between from, inclusive and to, inclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by (to - from + 1) elements.

Parameters:
from index of first element to be removed.
to index of last element to be removed.
Throws:
java.lang.IndexOutOfBoundsException index is out of range (size()>0 && (from<0 || from>to || to>=size())).
  public void removeFromTo(int fromint to) {
    checkRangeFromTo(fromto);
    int numMoved =  - to - 1;
    if (numMoved > 0) {
      replaceFromToWithFrom(fromfrom - 1 + numMovedthisto + 1);
      //fillFromToWith(from+numMoved, size-1, 0.0f); //delta
    }
    int width = to - from + 1;
    if (width > 0) {
      setSizeRaw( - width);
    }
  }

  
Replaces a number of elements in the receiver with the same number of elements of another list. Replaces elements in the receiver, between from (inclusive) and to (inclusive), with elements of other, starting from otherFrom (inclusive).

Parameters:
from the position of the first element to be replaced in the receiver
to the position of the last element to be replaced in the receiver
other list holding elements to be copied into the receiver.
otherFrom position of first element within other list to be copied.
  public void replaceFromToWithFrom(int fromint toAbstractIntList otherint otherFrom) {
    int length = to - from + 1;
    if (length > 0) {
      checkRangeFromTo(fromtosize());
      checkRangeFromTo(otherFromotherFrom + length - 1, other.size());
      // unambiguous copy (it may hold other==this)
      if (from <= otherFrom) {
        while (--length >= 0) {
          setQuick(from++, other.getQuick(otherFrom++));
        }
      } else {
        int otherTo = otherFrom + length - 1;
        while (--length >= 0) {
          setQuick(to--, other.getQuick(otherTo--));
        }
      }
    }
  }

  
Replaces the part between from (inclusive) and to (inclusive) with the other list's part between otherFrom and otherTo. Powerful (and tricky) method! Both parts need not be of the same size (part A can both be smaller or larger than part B). Parts may overlap. Receiver and other list may (but most not) be identical. If from > to, then inserts other part before from.

Parameters:
from the first element of the receiver (inclusive)
to the last element of the receiver (inclusive)
other the other list (may be identical with receiver)
otherFrom the first element of the other list (inclusive)
otherTo the last element of the other list (inclusive)

Examples:

                                                                     a=[0, 1, 2, 3, 4, 5, 6, 7]
                                                                     b=[50, 60, 70, 80, 90]
                                                                     a.R(...)=a.replaceFromToWithFromTo(...)

                                                                     a.R(3,5,b,0,4)-->[0, 1, 2, 50, 60, 70, 80, 90,
                  6, 7]
                                                                     a.R(1,6,b,0,4)-->[0, 50, 60, 70, 80, 90, 7]
                                                                     a.R(0,6,b,0,4)-->[50, 60, 70, 80, 90, 7]
                                                                     a.R(3,5,b,1,2)-->[0, 1, 2, 60, 70, 6, 7]
                                                                     a.R(1,6,b,1,2)-->[0, 60, 70, 7]
                                                                     a.R(0,6,b,1,2)-->[60, 70, 7]
                                                                     a.R(5,3,b,0,4)-->[0, 1, 2, 3, 4, 50, 60, 70,
                  80, 90, 5, 6, 7]
                                                                     a.R(5,0,b,0,4)-->[0, 1, 2, 3, 4, 50, 60, 70,
                  80, 90, 5, 6, 7]
                                                                     a.R(5,3,b,1,2)-->[0, 1, 2, 3, 4, 60, 70, 5, 6,
                  7]
                                                                     a.R(5,0,b,1,2)-->[0, 1, 2, 3, 4, 60, 70, 5, 6,
                  7]

                                                                     Extreme cases:
                                                                     a.R(5,3,b,0,0)-->[0, 1, 2, 3, 4, 50, 5, 6, 7]
                                                                     a.R(5,3,b,4,4)-->[0, 1, 2, 3, 4, 90, 5, 6, 7]
                                                                     a.R(3,5,a,0,1)-->[0, 1, 2, 0, 1, 6, 7]
                                                                     a.R(3,5,a,3,5)-->[0, 1, 2, 3, 4, 5, 6, 7]
                                                                     a.R(3,5,a,4,4)-->[0, 1, 2, 4, 6, 7]
                                                                     a.R(5,3,a,0,4)-->[0, 1, 2, 3, 4, 0, 1, 2, 3, 4,
                  5, 6, 7]
                                                                     a.R(0,-1,b,0,4)-->[50, 60, 70, 80, 90, 0, 1, 2,
                  3, 4, 5, 6, 7]
                                                                     a.R(0,-1,a,0,4)-->[0, 1, 2, 3, 4, 0, 1, 2, 3,
                  4, 5, 6, 7]
                                                                     a.R(8,0,a,0,4)-->[0, 1, 2, 3, 4, 5, 6, 7, 0, 1,
                  2, 3, 4]
                                                                     
  public void replaceFromToWithFromTo(int fromint toAbstractIntList otherint otherFromint otherTo) {
    if (otherFrom > otherTo) {
      throw new IndexOutOfBoundsException("otherFrom: " + otherFrom + ", otherTo: " + otherTo);
    }
    if (this == other && to - from != otherTo - otherFrom) { // avoid stumbling over my own feet
      replaceFromToWithFromTo(fromtopartFromTo(otherFromotherTo), 0, otherTo - otherFrom);
      return;
    }
    int length = otherTo - otherFrom + 1;
    int diff = length;
    int theLast = from - 1;
    if (to >= from) {
      diff -= (to - from + 1);
      theLast = to;
    }
    if (diff > 0) {
      beforeInsertDummies(theLast + 1, diff);
    } else {
      if (diff < 0) {
        removeFromTo(theLast + difftheLast - 1);
      }
    }
    if (length > 0) {
      replaceFromToWithFrom(fromfrom + length - 1, otherotherFrom);
    }
  }
  
  
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.

Parameters:
other the other list to test against.
Returns:
true if the receiver changed as a result of the call.
  public boolean retainAll(AbstractIntList other) {
    if (other.isEmpty()) {
      if ( == 0) {
        return false;
      }
      setSize(0);
      return true;
    }
    int limit = other.size() - 1;
    int j = 0;
    for (int i = 0; i < i++) {
      if (other.indexOfFromTo(getQuick(i), 0, limit) >= 0) {
        setQuick(j++, getQuick(i));
      }
    }
    boolean modified = (j != );
    setSize(j);
    return modified;
  }
  
  
Reverses the elements of the receiver. Last becomes first, second last becomes second first, and so on.
  public void reverse() {
    int limit = size() / 2;
    int j = size() - 1;
    for (int i = 0; i < limit;) { //swap
      int tmp = getQuick(i);
      setQuick(i++, getQuick(j));
      setQuick(j--, tmp);
    }
  }

  
Replaces the element at the specified position in the receiver with the specified element.

Parameters:
index index of element to replace.
element element to be stored at the specified position.
Throws:
java.lang.IndexOutOfBoundsException if index < 0 || index >= size().
  public void set(int indexint element) {
    if (index >=  || index < 0) {
      throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + );
    }
    setQuick(indexelement);
  }

  
Replaces the element at the specified position in the receiver with the specified element; WARNING: Does not check preconditions. Provided with invalid parameters this method may access invalid indexes without throwing any exception! You should only use this method when you are absolutely sure that the index is within bounds. Precondition (unchecked): index >= 0 && index < size(). This method is normally only used internally in large loops where bounds are explicitly checked before the loop and need no be rechecked within the loop. However, when desperately, you can give this method public visibility in subclasses.

Parameters:
index index of element to replace.
element element to be stored at the specified position.
  protected abstract void setQuick(int indexint element);

  
Sets the size of the receiver without modifying it otherwise. This method should not release or allocate new memory but simply set some instance variable like size. If your subclass overrides and delegates size changing methods to some other object, you must make sure that those overriding methods not only update the size of the delegate but also of this class. For example: public DatabaseList extends AbstractIntList { ... public void removeFromTo(int from,int to) { myDatabase.removeFromTo(from,to); this.setSizeRaw(size-(to-from+1)); } }
  protected void setSizeRaw(int newSize) {
     = newSize;
  }

  
Returns the number of elements contained in the receiver.
  public int size() {
    return ;
  }

  
Returns a list which is a concatenation of times times the receiver.

Parameters:
times the number of times the receiver shall be copied.
  public AbstractIntList times(int times) {
    AbstractIntList newList = new IntArrayList(times * size());
    for (int i = times; --i >= 0;) {
      newList.addAllOfFromTo(this, 0, size() - 1);
    }
    return newList;
  }

  
Returns a ArrayList containing all the elements in the receiver.
  public List<IntegertoList() {
    int mySize = size();
    List<Integerlist = new ArrayList<Integer>(mySize);
    for (int i = 0; i < mySizei++) {
      list.add(get(i));
    }
    return list;
  }
  
  public int[] toArray(int[] values) {
   int mySize = size();
   int[] myElements;
   if (values.length >= mySize) {
     myElements = values;
   } else {
     myElements = new int[mySize];
   }
   for (int i = ; --i >= 0;) {
      myElements[i] = getQuick(i);
    }
    return myElements;
  }

  
Returns a string representation of the receiver, containing the String representation of each element.
  public String toString() {
    return org.apache.mahout.math.Arrays.toString(partFromTo(0, size() - 1).elements());
  }
New to GrepCode? Check out our FAQ X