gnu.trove

Class TIntArrayList

public class TIntArrayList extends Object implements Serializable, Cloneable

A resizable, array-backed list of int primitives. Created: Sat Dec 29 14:21:12 2001

Version: $Id: TIntArrayList.java,v 1.10 2003/03/19 05:07:11 ericdf Exp $

Author: Eric D. Friedman

Field Summary
protected static intDEFAULT_CAPACITY
the default capacity for new lists
protected int[]_data
the data of the list
protected int_pos
the index after the last entry in the list
Constructor Summary
TIntArrayList()
Creates a new TIntArrayList instance with the default capacity.
TIntArrayList(int capacity)
Creates a new TIntArrayList instance with the specified capacity.
TIntArrayList(int[] values)
Creates a new TIntArrayList instance whose capacity is the greater of the length of values and DEFAULT_CAPACITY and whose initial contents are the specified values.
Method Summary
voidadd(int val)
Adds val to the end of the list, growing as needed.
voidadd(int[] vals)
Adds the values in the array vals to the end of the list, in order.
voidadd(int[] vals, int offset, int length)
Adds a subset of the values in the array vals to the end of the list, in order.
intbinarySearch(int value)
Performs a binary search for value in the entire list.
intbinarySearch(int value, int fromIndex, int toIndex)
Performs a binary search for value in the specified range.
voidclear()
Flushes the internal state of the list, resetting the capacity to the default.
voidclear(int capacity)
Flushes the internal state of the list, setting the capacity of the empty list to capacity.
Objectclone()
Returns a clone of this list.
booleancontains(int value)
Searches the list for value
voidensureCapacity(int capacity)
Grow the internal array as needed to accomodate the specified number of elements.
booleanequals(Object other)
Compares this list to another list, value by value.
voidfill(int val)
Fills every slot in the list with the specified value.
voidfill(int fromIndex, int toIndex, int val)
Fills a range in the list with the specified value.
booleanforEach(TIntProcedure procedure)
Applies the procedure to each value in the list in ascending (front to back) order.
booleanforEachDescending(TIntProcedure procedure)
Applies the procedure to each value in the list in descending (back to front) order.
intget(int offset)
Returns the value at the specified offset.
intgetQuick(int offset)
Returns the value at the specified offset without doing any bounds checking.
intgetSet(int offset, int val)
Sets the value at the specified offset and returns the previously stored value.
TIntArrayListgrep(TIntProcedure condition)
Searches the list for values satisfying condition in the manner of the *nix grep utility.
inthashCode()
intindexOf(int value)
Searches the list front to back for the index of value.
intindexOf(int offset, int value)
Searches the list front to back for the index of value, starting at offset.
voidinsert(int offset, int value)
Inserts value into the list at offset.
voidinsert(int offset, int[] values)
Inserts the array of values into the list at offset.
voidinsert(int offset, int[] values, int valOffset, int len)
Inserts a slice of the array of values into the list at offset.
TIntArrayListinverseGrep(TIntProcedure condition)
Searches the list for values which do not satisfy condition.
booleanisEmpty()
Tests whether this list contains any values.
intlastIndexOf(int value)
Searches the list back to front for the last index of value.
intlastIndexOf(int offset, int value)
Searches the list back to front for the last index of value, starting at offset.
intmax()
Finds the maximum value in the list.
intmin()
Finds the minimum value in the list.
voidreadObject(ObjectInputStream stream)
intremove(int offset)
Removes the value at offset from the list.
voidremove(int offset, int length)
Removes length values from the list, starting at offset
voidreset()
Sets the size of the list to 0, but does not change its capacity.
voidresetQuick()
Sets the size of the list to 0, but does not change its capacity.
voidreverse()
Reverse the order of the elements in the list.
voidreverse(int from, int to)
Reverse the order of the elements in the range of the list.
voidset(int offset, int val)
Sets the value at the specified offset.
voidset(int offset, int[] values)
Replace the values in the list starting at offset with the contents of the values array.
voidset(int offset, int[] values, int valOffset, int length)
Replace the values in the list starting at offset with length values from the values array, starting at valOffset.
voidsetQuick(int offset, int val)
Sets the value at the specified offset without doing any bounds checking.
voidshuffle(Random rand)
Shuffle the elements of the list using the specified random number generator.
intsize()
Returns the number of values in the list.
voidsort()
Sort the values in the list (ascending) using the Sun quicksort implementation.
voidsort(int fromIndex, int toIndex)
Sort a slice of the list (ascending) using the Sun quicksort implementation.
voidswap(int i, int j)
Swap the values at offsets i and j.
int[]toNativeArray()
Copies the contents of the list into a native array.
int[]toNativeArray(int offset, int len)
Copies a slice of the list into a native array.
voidtoNativeArray(int[] dest, int offset, int len)
Copies a slice of the list into a native array.
StringtoString()
Returns a String representation of the list, front to back.
voidtransformValues(TIntFunction function)
Transform each value in the list using the specified function.
voidtrimToSize()
Sheds any excess capacity above and beyond the current size of the list.
voidwriteObject(ObjectOutputStream stream)

Field Detail

DEFAULT_CAPACITY

protected static final int DEFAULT_CAPACITY
the default capacity for new lists

_data

protected transient int[] _data
the data of the list

_pos

protected transient int _pos
the index after the last entry in the list

Constructor Detail

TIntArrayList

public TIntArrayList()
Creates a new TIntArrayList instance with the default capacity.

TIntArrayList

public TIntArrayList(int capacity)
Creates a new TIntArrayList instance with the specified capacity.

Parameters: capacity an int value

TIntArrayList

public TIntArrayList(int[] values)
Creates a new TIntArrayList instance whose capacity is the greater of the length of values and DEFAULT_CAPACITY and whose initial contents are the specified values.

Parameters: values an int[] value

Method Detail

add

public void add(int val)
Adds val to the end of the list, growing as needed.

Parameters: val an int value

add

public void add(int[] vals)
Adds the values in the array vals to the end of the list, in order.

Parameters: vals an int[] value

add

public void add(int[] vals, int offset, int length)
Adds a subset of the values in the array vals to the end of the list, in order.

Parameters: vals an int[] value offset the offset at which to start copying length the number of values to copy.

binarySearch

public int binarySearch(int value)
Performs a binary search for value in the entire list. Note that you must sort the list before doing a search.

Parameters: value the value to search for

Returns: the absolute offset in the list of the value, or its negative insertion point into the sorted list.

binarySearch

public int binarySearch(int value, int fromIndex, int toIndex)
Performs a binary search for value in the specified range. Note that you must sort the list or the range before doing a search.

Parameters: value the value to search for fromIndex the lower boundary of the range (inclusive) toIndex the upper boundary of the range (exclusive)

Returns: the absolute offset in the list of the value, or its negative insertion point into the sorted list.

clear

public void clear()
Flushes the internal state of the list, resetting the capacity to the default.

clear

public void clear(int capacity)
Flushes the internal state of the list, setting the capacity of the empty list to capacity.

Parameters: capacity an int value

clone

public Object clone()
Returns a clone of this list. Since this is a primitive collection, this will be a deep clone.

Returns: a deep clone of the list.

contains

public boolean contains(int value)
Searches the list for value

Parameters: value an int value

Returns: true if value is in the list.

ensureCapacity

public void ensureCapacity(int capacity)
Grow the internal array as needed to accomodate the specified number of elements. The size of the array doubles on each resize unless capacity requires more than twice the current capacity.

Parameters: capacity an int value

equals

public boolean equals(Object other)
Compares this list to another list, value by value.

Parameters: other the object to compare against

Returns: true if other is a TIntArrayList and has exactly the same values.

fill

public void fill(int val)
Fills every slot in the list with the specified value.

Parameters: val the value to use when filling

fill

public void fill(int fromIndex, int toIndex, int val)
Fills a range in the list with the specified value.

Parameters: fromIndex the offset at which to start filling (inclusive) toIndex the offset at which to stop filling (exclusive) val the value to use when filling

forEach

public boolean forEach(TIntProcedure procedure)
Applies the procedure to each value in the list in ascending (front to back) order.

Parameters: procedure a TIntProcedure value

Returns: true if the procedure did not terminate prematurely.

forEachDescending

public boolean forEachDescending(TIntProcedure procedure)
Applies the procedure to each value in the list in descending (back to front) order.

Parameters: procedure a TIntProcedure value

Returns: true if the procedure did not terminate prematurely.

get

public int get(int offset)
Returns the value at the specified offset.

Parameters: offset an int value

Returns: an int value

getQuick

public int getQuick(int offset)
Returns the value at the specified offset without doing any bounds checking.

Parameters: offset an int value

Returns: an int value

getSet

public int getSet(int offset, int val)
Sets the value at the specified offset and returns the previously stored value.

Parameters: offset an int value val an int value

Returns: the value previously stored at offset.

grep

public TIntArrayList grep(TIntProcedure condition)
Searches the list for values satisfying condition in the manner of the *nix grep utility.

Parameters: condition a condition to apply to each element in the list

Returns: a list of values which match the condition.

hashCode

public int hashCode()

indexOf

public int indexOf(int value)
Searches the list front to back for the index of value.

Parameters: value an int value

Returns: the first offset of the value, or -1 if it is not in the list.

See Also: for faster searches on sorted lists

indexOf

public int indexOf(int offset, int value)
Searches the list front to back for the index of value, starting at offset.

Parameters: offset the offset at which to start the linear search (inclusive) value an int value

Returns: the first offset of the value, or -1 if it is not in the list.

See Also: for faster searches on sorted lists

insert

public void insert(int offset, int value)
Inserts value into the list at offset. All values including and to the right of offset are shifted to the right.

Parameters: offset an int value value an int value

insert

public void insert(int offset, int[] values)
Inserts the array of values into the list at offset. All values including and to the right of offset are shifted to the right.

Parameters: offset an int value values an int[] value

insert

public void insert(int offset, int[] values, int valOffset, int len)
Inserts a slice of the array of values into the list at offset. All values including and to the right of offset are shifted to the right.

Parameters: offset an int value values an int[] value valOffset the offset in the values array at which to start copying. len the number of values to copy from the values array

inverseGrep

public TIntArrayList inverseGrep(TIntProcedure condition)
Searches the list for values which do not satisfy condition. This is akin to *nix grep -v.

Parameters: condition a condition to apply to each element in the list

Returns: a list of values which do not match the condition.

isEmpty

public boolean isEmpty()
Tests whether this list contains any values.

Returns: true if the list is empty.

lastIndexOf

public int lastIndexOf(int value)
Searches the list back to front for the last index of value.

Parameters: value an int value

Returns: the last offset of the value, or -1 if it is not in the list.

See Also: for faster searches on sorted lists

lastIndexOf

public int lastIndexOf(int offset, int value)
Searches the list back to front for the last index of value, starting at offset.

Parameters: offset the offset at which to start the linear search (exclusive) value an int value

Returns: the last offset of the value, or -1 if it is not in the list.

See Also: for faster searches on sorted lists

max

public int max()
Finds the maximum value in the list.

Returns: the largest value in the list.

Throws: IllegalStateException if the list is empty

min

public int min()
Finds the minimum value in the list.

Returns: the smallest value in the list.

Throws: IllegalStateException if the list is empty

readObject

private void readObject(ObjectInputStream stream)

remove

public int remove(int offset)
Removes the value at offset from the list.

Parameters: offset an int value

Returns: the value previously stored at offset.

remove

public void remove(int offset, int length)
Removes length values from the list, starting at offset

Parameters: offset an int value length an int value

reset

public void reset()
Sets the size of the list to 0, but does not change its capacity. This method can be used as an alternative to the clear method if you want to recyle a list without allocating new backing arrays.

See Also: TIntArrayList

resetQuick

public void resetQuick()
Sets the size of the list to 0, but does not change its capacity. This method can be used as an alternative to the clear method if you want to recyle a list without allocating new backing arrays. This method differs from reset in that it does not clear the old values in the backing array. Thus, it is possible for getQuick to return stale data if this method is used and the caller is careless about bounds checking.

See Also: TIntArrayList TIntArrayList TIntArrayList

reverse

public void reverse()
Reverse the order of the elements in the list.

reverse

public void reverse(int from, int to)
Reverse the order of the elements in the range of the list.

Parameters: from the inclusive index at which to start reversing to the exclusive index at which to stop reversing

set

public void set(int offset, int val)
Sets the value at the specified offset.

Parameters: offset an int value val an int value

set

public void set(int offset, int[] values)
Replace the values in the list starting at offset with the contents of the values array.

Parameters: offset the first offset to replace values the source of the new values

set

public void set(int offset, int[] values, int valOffset, int length)
Replace the values in the list starting at offset with length values from the values array, starting at valOffset.

Parameters: offset the first offset to replace values the source of the new values valOffset the first value to copy from the values array length the number of values to copy

setQuick

public void setQuick(int offset, int val)
Sets the value at the specified offset without doing any bounds checking.

Parameters: offset an int value val an int value

shuffle

public void shuffle(Random rand)
Shuffle the elements of the list using the specified random number generator.

Parameters: rand a Random value

size

public int size()
Returns the number of values in the list.

Returns: the number of values in the list.

sort

public void sort()
Sort the values in the list (ascending) using the Sun quicksort implementation.

See Also: java.util.Arrays#sort

sort

public void sort(int fromIndex, int toIndex)
Sort a slice of the list (ascending) using the Sun quicksort implementation.

Parameters: fromIndex the index at which to start sorting (inclusive) toIndex the index at which to stop sorting (exclusive)

See Also: java.util.Arrays#sort

swap

private final void swap(int i, int j)
Swap the values at offsets i and j.

Parameters: i an offset into the data array j an offset into the data array

toNativeArray

public int[] toNativeArray()
Copies the contents of the list into a native array.

Returns: an int[] value

toNativeArray

public int[] toNativeArray(int offset, int len)
Copies a slice of the list into a native array.

Parameters: offset the offset at which to start copying len the number of values to copy.

Returns: an int[] value

toNativeArray

public void toNativeArray(int[] dest, int offset, int len)
Copies a slice of the list into a native array.

Parameters: dest the array to copy into. offset the offset of the first value to copy len the number of values to copy.

toString

public String toString()
Returns a String representation of the list, front to back.

Returns: a String value

transformValues

public void transformValues(TIntFunction function)
Transform each value in the list using the specified function.

Parameters: function a TIntFunction value

trimToSize

public void trimToSize()
Sheds any excess capacity above and beyond the current size of the list.

writeObject

private void writeObject(ObjectOutputStream stream)