de.torstennahm.integrate.sparse.index
Class FastIndex

java.lang.Object
  extended by de.torstennahm.integrate.sparse.index.FastIndex
All Implemented Interfaces:
Index, java.lang.Iterable<de.torstennahm.math.IntEntry>

public class FastIndex
extends java.lang.Object
implements Index

Implements Index. Only the non-zero entries for the index are stored. For performance, an array is used for storage, which is grown when needed.

According to general index contract, this class is thread-safe.

Author:
Torsten Nahm

Field Summary
protected  int[] entryNums
          Array storing the entry number of each entry.
protected  int[] entryVals
          Array storing the corresponding value of each entry.
protected  int hashCode
          Hash code.
protected  int nonZeroEntries
          Number of entries that are not zero.
 
Constructor Summary
FastIndex()
          Creates a new fast index.
FastIndex(Index index)
          Creates an new FastIndex from any Index.
FastIndex(int[] array)
          Creates a fast index from an array.
FastIndex(de.torstennahm.math.SparseIntVector vector)
          Creates a new FastIndex from a PackedIntVector.
 
Method Summary
 Index add(int entryNum, int add)
          Returns a new index with the specified increment at the specified entry.
 boolean equals(java.lang.Object o)
          Compares the specified object with this index for equality.
 int get(int entryNum)
          Returns the value for the specified entry.
 int hashCode()
          Returns the hash code value for this index.
 java.util.Iterator<de.torstennahm.math.IntEntry> iterator()
          Returns an IntPairIterator for iterating over the index.
 int lastEntry()
          Returns the index of highest entry whose value is not 0.
protected static int newHash(int hashCode, int entryNum, int oldVal, int newVal)
           
 int nonZeroEntries()
          Returns the number of entries with values not equal to 0.
 Index set(int entryNum, int value)
          Returns a new index with the specified new value at the specified entry.
 int sum()
          Returns the the sum of all entry values.
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

nonZeroEntries

protected int nonZeroEntries
Number of entries that are not zero.


entryNums

protected int[] entryNums
Array storing the entry number of each entry.


entryVals

protected int[] entryVals
Array storing the corresponding value of each entry.


hashCode

protected int hashCode
Hash code.

Constructor Detail

FastIndex

public FastIndex()
Creates a new fast index. All entries are initially zero.


FastIndex

public FastIndex(Index index)
Creates an new FastIndex from any Index.

Parameters:
index - index to initialize from

FastIndex

public FastIndex(de.torstennahm.math.SparseIntVector vector)
Creates a new FastIndex from a PackedIntVector.

Parameters:
vector - vector to initialize from

FastIndex

public FastIndex(int[] array)
Creates a fast index from an array.

Parameters:
array - array to initialize from
Method Detail

nonZeroEntries

public int nonZeroEntries()
Description copied from interface: Index
Returns the number of entries with values not equal to 0.

Specified by:
nonZeroEntries in interface Index
Returns:
number of entries with value != 0

lastEntry

public int lastEntry()
Description copied from interface: Index
Returns the index of highest entry whose value is not 0.

Specified by:
lastEntry in interface Index
Returns:
number of last non-0 entry, -1 if all entries are 0

get

public int get(int entryNum)
Description copied from interface: Index
Returns the value for the specified entry.

Specified by:
get in interface Index
Parameters:
entryNum - number of the entry
Returns:
value value of the entry

set

public Index set(int entryNum,
                 int value)
Description copied from interface: Index
Returns a new index with the specified new value at the specified entry.

Specified by:
set in interface Index
Parameters:
entryNum - number of the entry
value - new value for the entry
Returns:
new index with the requested change

add

public Index add(int entryNum,
                 int add)
Description copied from interface: Index
Returns a new index with the specified increment at the specified entry.

Specified by:
add in interface Index
Parameters:
entryNum - number of the entry
add - increment for the entry, may be negative
Returns:
new index with the requested change

sum

public int sum()
Description copied from interface: Index
Returns the the sum of all entry values.

Specified by:
sum in interface Index
Returns:
length of the index

iterator

public java.util.Iterator<de.torstennahm.math.IntEntry> iterator()
Description copied from interface: Index
Returns an IntPairIterator for iterating over the index. The iterator skips all entries whose value is zero.

Specified by:
iterator in interface Index
Specified by:
iterator in interface java.lang.Iterable<de.torstennahm.math.IntEntry>
Returns:
iterator iterator over the non-zero entries

newHash

protected static int newHash(int hashCode,
                             int entryNum,
                             int oldVal,
                             int newVal)

hashCode

public int hashCode()
Description copied from interface: Index
Returns the hash code value for this index. The hash code of an index is defined by the following expression:
hashCode = e[0] * a0 + ... + e[dimension - 1] * a_(dimension-1)
, where e[i] is the i-th entry, and a_i = (i + 13) << i in integer arithmetic.

Specified by:
hashCode in interface Index
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object o)
Description copied from interface: Index
Compares the specified object with this index for equality. Returns true if the specified object is also an index, the two indices have the same dimension, and all the entries of the two indices are equal. This definition ensures that the equals method works properly across different implementations of the Index interface.

Specified by:
equals in interface Index
Overrides:
equals in class java.lang.Object

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object