org.apache.mahout.math.list
Class ObjectArrayList<T>

java.lang.Object
  extended by org.apache.mahout.math.PersistentObject
      extended by org.apache.mahout.math.list.AbstractList
          extended by org.apache.mahout.math.list.AbstractObjectList<T>
              extended by org.apache.mahout.math.list.ObjectArrayList<T>
All Implemented Interfaces:
Serializable, Cloneable

public class ObjectArrayList<T>
extends AbstractObjectList<T>

Resizable list holding ${valueType} elements; implemented with arrays.

See Also:
Serialized Form

Constructor Summary
ObjectArrayList()
          Constructs an empty list.
ObjectArrayList(int initialCapacity)
          Constructs an empty list with the specified initial capacity.
ObjectArrayList(T[] elements)
          Constructs a list containing the specified elements.
 
Method Summary
 void add(T element)
          Appends the specified element to the end of this list.
 void beforeInsert(int index, T element)
          Inserts the specified element before the specified position into the receiver.
protected  void beforeInsertDummies(int index, int length)
          Inserts length dummy elements before the specified position into the receiver.
 Object clone()
          Returns a deep copy of the receiver.
 ObjectArrayList<T> copy()
          Returns a deep copy of the receiver; uses clone() and casts the result.
<Q> Q[]
elements()
          Returns the elements currently stored, including invalid elements between size and capacity, if any.
 void elements(T[] elements)
          Sets the receiver's elements to be the specified array (not a copy of it).
 void ensureCapacity(int minCapacity)
          Ensures that the receiver can hold at least the specified number of elements without needing to allocate new internal memory.
 boolean equals(Object otherObj)
          Compares the specified Object with the receiver.
 boolean forEach(ObjectProcedure<T> procedure)
          Applies a procedure to each element of the receiver, if any.
 T get(int index)
          Returns the element at the specified position in the receiver.
 T getQuick(int index)
          Returns the element at the specified position in the receiver; WARNING: Does not check preconditions.
 int indexOfFromTo(T element, int from, int to)
          Returns the index of the first occurrence of the specified element.
 int lastIndexOfFromTo(T element, int from, int to)
          Returns the index of the last occurrence of the specified element.
 void mergeSortFromTo(int from, int to)
          Sorts the receiver into ascending order.
 AbstractObjectList<T> partFromTo(int from, int to)
          Returns a new list of the part of the receiver between from, inclusive, and to, inclusive.
 void quickSortFromTo(int from, int to)
          Sorts the specified range of the receiver into ascending order.
 void removeFromTo(int fromIndex, int toIndex)
          Removes from the receiver all elements whose index is between from, inclusive and to, inclusive.
 void replaceFromWith(int from, Collection<T> other)
          Replaces the part of the receiver starting at from (inclusive) with all the elements of the specified collection.
 void reverse()
          Reverses the elements of the receiver.
 void set(int index, T element)
          Replaces the element at the specified position in the receiver with the specified element.
 void setQuick(int index, T element)
          Replaces the element at the specified position in the receiver with the specified element; WARNING: Does not check preconditions.
 int size()
           
 void trimToSize()
          Trims the capacity of the receiver to be the receiver's current size.
 
Methods inherited from class org.apache.mahout.math.list.AbstractObjectList
addAllOf, beforeInsertAllOf
 
Methods inherited from class org.apache.mahout.math.list.AbstractList
checkRange, checkRangeFromTo, clear, isEmpty, mergeSort, quickSort, remove, setSize, sort, sortFromTo
 
Methods inherited from class java.lang.Object
finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ObjectArrayList

public ObjectArrayList()
Constructs an empty list.


ObjectArrayList

public ObjectArrayList(T[] elements)
Constructs a list containing the specified elements. The initial size and capacity of the list is the length of the array. WARNING: For efficiency reasons and to keep memory usage low, the array is not copied. So if subsequently you modify the specified array directly via the [] operator, be sure you know what you're doing.

Parameters:
elements - the array to be backed by the the constructed list

ObjectArrayList

public ObjectArrayList(int initialCapacity)
Constructs an empty list with the specified initial capacity.

Parameters:
initialCapacity - the number of elements the receiver can hold without auto-expanding itself by allocating new internal memory.
Method Detail

add

public void add(T element)
Appends the specified element to the end of this list.

Parameters:
element - element to be appended to this list.

beforeInsert

public void beforeInsert(int index,
                         T element)
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:
IndexOutOfBoundsException - index is out of range (index < 0 || index > size()).

clone

public Object clone()
Returns a deep copy of the receiver.

Overrides:
clone in class PersistentObject
Returns:
a deep copy of the receiver.

copy

public ObjectArrayList<T> copy()
Returns a deep copy of the receiver; uses clone() and casts the result.

Returns:
a deep copy of the receiver.

elements

public <Q> Q[] elements()
Returns the elements currently stored, including invalid elements between size and capacity, if any. WARNING: For efficiency reasons and to keep memory usage low, the array is not copied. 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.

elements

public void elements(T[] elements)
Sets the receiver's elements to be the specified array (not a copy of it). The size and capacity of the list is the length of the array. WARNING: For efficiency reasons and to keep memory usage low, the array is not copied. So if subsequently you modify the specified array directly via the [] operator, be sure you know what you're doing.

Parameters:
elements - the new elements to be stored.

ensureCapacity

public void ensureCapacity(int minCapacity)
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.

equals

public boolean equals(Object otherObj)
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.

Overrides:
equals in class Object
Parameters:
otherObj - the Object to be compared for equality with the receiver.
Returns:
true if the specified Object is equal to the receiver.

forEach

public boolean forEach(ObjectProcedure<T> procedure)
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.

get

public T get(int index)
Returns the element at the specified position in the receiver.

Parameters:
index - index of element to return.
Throws:
IndexOutOfBoundsException - index is out of range (index < 0 || index >= size()).

getQuick

public T getQuick(int 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().

Parameters:
index - index of element to return.

indexOfFromTo

public int indexOfFromTo(T element,
                         int from,
                         int to)
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:
IndexOutOfBoundsException - index is out of range (size()>0 && (from<0 || from>to || to>=size())).

lastIndexOfFromTo

public int lastIndexOfFromTo(T element,
                             int from,
                             int to)
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:
IndexOutOfBoundsException - index is out of range (size()>0 && (from<0 || from>to || to>=size())).

partFromTo

public AbstractObjectList<T> partFromTo(int from,
                                        int to)
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:
IndexOutOfBoundsException - index is out of range (size()>0 && (from<0 || from>to || to>=size())).

reverse

public void reverse()
Reverses the elements of the receiver. Last becomes first, second last becomes second first, and so on.

Specified by:
reverse in class AbstractList

set

public void set(int index,
                T element)
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:
IndexOutOfBoundsException - index is out of range (index < 0 || index >= size()).

setQuick

public void setQuick(int index,
                     T element)
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().

Parameters:
index - index of element to replace.
element - element to be stored at the specified position.

trimToSize

public void trimToSize()
Trims the capacity of the receiver to be the receiver's current size. Releases any superfluous internal memory. An application can use this operation to minimize the storage of the receiver.

Overrides:
trimToSize in class AbstractList

removeFromTo

public void removeFromTo(int fromIndex,
                         int toIndex)
Description copied from class: AbstractList
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.

Specified by:
removeFromTo in class AbstractList
Parameters:
fromIndex - index of first element to be removed.
toIndex - index of last element to be removed.

replaceFromWith

public void replaceFromWith(int from,
                            Collection<T> other)
Description copied from class: AbstractObjectList
Replaces the part of the receiver starting at from (inclusive) with all the elements of the specified collection. Does not alter the size of the receiver. Replaces exactly Math.max(0,Math.min(size()-from, other.size())) elements.

Specified by:
replaceFromWith in class AbstractObjectList<T>
Parameters:
from - the index at which to copy the first element from the specified collection.
other - Collection to replace part of the receiver

beforeInsertDummies

protected void beforeInsertDummies(int index,
                                   int length)
Description copied from class: AbstractList
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.

Specified by:
beforeInsertDummies in class AbstractList
Parameters:
index - index before which to insert dummy elements (must be in [0,size])..
length - number of dummy elements to be inserted.

mergeSortFromTo

public void mergeSortFromTo(int from,
                            int to)
Description copied from class: AbstractList
Sorts the receiver into ascending order. 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.

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.

Specified by:
mergeSortFromTo in class AbstractList
Parameters:
from - the index of the first element (inclusive) to be sorted.
to - the index of the last element (inclusive) to be sorted.

quickSortFromTo

public void quickSortFromTo(int from,
                            int to)
Description copied from class: AbstractList
Sorts the specified range of the receiver into ascending 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.

Specified by:
quickSortFromTo in class AbstractList
Parameters:
from - the index of the first element (inclusive) to be sorted.
to - the index of the last element (inclusive) to be sorted.

size

public int size()
Specified by:
size in class AbstractList


Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.