chemaxon.util
Class IntArray

java.lang.Object
  extended by chemaxon.util.IntArray
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public final class IntArray
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

The IntArray class implements an array of integers. The size of an IntArray can grow or shrink as needed to accommodate adding and removing items after the IntArray has been created.

This class is optimized for the memory efficient storage of very large number of integers. The array is incremented in blocks, so no reallocations take place during the expansion of the array.

Since:
JChem 3.1
Author:
Szilard Dorant
See Also:
Serialized Form

Field Summary
static int DEFAULT_BLOCK_SIZE
          The default block size (in integers)
 
Constructor Summary
IntArray()
          Constructs an empty IntArray.
IntArray(int blockSize)
          Constructs an empty IntArray with the specified block size.
 
Method Summary
 void add(int value)
          Adds the specified component to the end of this IntArray, increasing its size by one.
 boolean addAll(IntArray other)
          Appends all of the elements in the specified collection to the end of this vector, in the order that they are returned by the specified collection's iterator (optional operation).
 int capacity()
          Returns the current capacity of this IntArray.
 void clear()
          Sets the size to zero.
 boolean contains(int elem)
          Tests if the specified object is a component in this IntArray.
 void copyInto(int[] array)
          Copies the components of this IntArray into the specified array.
 void fill(int value)
          Sets all array elemnts to a specified value.
 int get(int index)
          Returns the component at the specified index.
 int indexOf(int elem)
          Searches for the first occurence of the given argument.
 int indexOf(int value, int index)
          Searches for the first occurence of the given argument, beginning the search at index.
 int indexOfWithBinarySearch(int num)
          Searches for the first occurence of the given number with binary search.
 boolean isEmpty()
          Tests if this IntArray has no components.
 int lastIndexOf(int value)
          Returns the index of the last occurrence of the specified value in this IntArray.
 int lastIndexOf(int elem, int index)
          Searches backwards for the specified object, starting from the specified index, and returns an index to it.
static void main(java.lang.String[] args)
          For testing.
 void set(int index, int value)
          Sets the element at the specified index of this IntArray to be the specified value.
 void setSize(int newSize)
          Sets the size of this IntArray.
 void setWithExpansion(int index, int value)
          Sets the element at the specified index of this IntArray to be the specified value.
 int size()
          Returns the number of components in this IntArray.
 void sort()
          Sorts the elements in ascending order.
 int[] toArray()
          Converts this IntArray to an integer array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BLOCK_SIZE

public static final int DEFAULT_BLOCK_SIZE
The default block size (in integers)

See Also:
Constant Field Values
Constructor Detail

IntArray

public IntArray()
Constructs an empty IntArray.


IntArray

public IntArray(int blockSize)
Constructs an empty IntArray with the specified block size.

Parameters:
blockSize - the amount by which the capacity is incremented (number of integers)
Method Detail

copyInto

public void copyInto(int[] array)
Copies the components of this IntArray into the specified array. The array must be big enough to hold all the objects in this IntArray.

Parameters:
array - the array into which the components get copied.

toArray

public int[] toArray()
Converts this IntArray to an integer array.

Returns:
the created integer array

setSize

public void setSize(int newSize)
Sets the size of this IntArray. If the new size is greater than the current size, new 0 items are added to the end of the IntArray. If the new size is less than the current size, all components at index newSize and greater are discarded.

Parameters:
newSize - the new size of this IntArray.

capacity

public int capacity()
Returns the current capacity of this IntArray.

Returns:
the current capacity of this IntArray.

size

public int size()
Returns the number of components in this IntArray.

Returns:
the number of components in this IntArray.

isEmpty

public boolean isEmpty()
Tests if this IntArray has no components.

Returns:
true if this IntArray has no components; false otherwise.

contains

public boolean contains(int elem)
Tests if the specified object is a component in this IntArray.

Parameters:
elem - an object.
Returns:
true if the specified object is a component in this IntArray; false otherwise.

indexOf

public int indexOf(int elem)
Searches for the first occurence of the given argument.

Parameters:
elem - an object.
Returns:
the index of the first occurrence of the argument in this IntArray; returns -1 if the object is not found.

indexOfWithBinarySearch

public int indexOfWithBinarySearch(int num)
Searches for the first occurence of the given number with binary search. NOTE: the IntArray should be ordered with sort() prior to this call, otherwise an IllegalStateException is thrown.

Parameters:
num - the number to look for
Returns:
the index of the first occurrence of the argument in this IntArray; returns -1 if the value is not found.

indexOf

public int indexOf(int value,
                   int index)
Searches for the first occurence of the given argument, beginning the search at index.

Parameters:
value - an object.
index - the index to start searching from.
Returns:
the index of the first occurrence of the object argument in this IntArray at position index or later in the IntArray; returns -1 if the object is not found.

lastIndexOf

public int lastIndexOf(int value)
Returns the index of the last occurrence of the specified value in this IntArray.

Parameters:
value - the desired component.
Returns:
the index of the last occurrence of the specified value in this IntArray; returns -1 if the value is not found.

lastIndexOf

public int lastIndexOf(int elem,
                       int index)
Searches backwards for the specified object, starting from the specified index, and returns an index to it.

Parameters:
elem - the desired component.
index - the index to start searching from.
Returns:
the index of the last occurrence of the specified object in this IntArray at position less than index in the IntArray; -1 if the object is not found.

get

public int get(int index)
Returns the component at the specified index.

Parameters:
index - an index into this IntArray.
Returns:
the component at the specified index.
Throws:
java.lang.IndexOutOfBoundsException - if an invalid index was given.

set

public void set(int index,
                int value)
Sets the element at the specified index of this IntArray to be the specified value.

Parameters:
index - the index of the element.
value - the new value.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size()).
See Also:
size()

setWithExpansion

public void setWithExpansion(int index,
                             int value)
Sets the element at the specified index of this IntArray to be the specified value. Increases the capacity if the index is not less than the current array size.

Parameters:
index - the index of the element.
value - the new value.
Throws:
java.lang.ArrayIndexOutOfBoundsException - if the index is negative
Since:
JChem 3.2

add

public void add(int value)
Adds the specified component to the end of this IntArray, increasing its size by one.

Parameters:
value - the element to be added.

clear

public void clear()
Sets the size to zero.


addAll

public boolean addAll(IntArray other)
Appends all of the elements in the specified collection to the end of this vector, in the order that they are returned by the specified collection's iterator (optional operation). The behavior of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this vector, and it's nonempty.)

Parameters:
other - IntArray whose elements are to be added to this vector.
Returns:
true if this vector changed as a result of the call.
See Also:
add(int)

fill

public void fill(int value)
Sets all array elemnts to a specified value.

Parameters:
value - the value

sort

public void sort()
Sorts the elements in 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.


main

public static void main(java.lang.String[] args)
For testing.