public class SparseDoubleArray extends Object implements SparseNumericArray<Double>, Serializable, Iterable<DoubleEntry>
double
array. This class trades increased space efficiency
at the cost of decreased performance.
This class also provides additional primitive accessor methods. This allows
users to invoke get
and set
without marshalling primitive
types to their Double
equivalents unnecessarily.
The get
operation runs in logarithmic time. The set
operation runs in consant time if setting an existing non-zero value to a
non-zero value. However, if the set
invocation sets a zero value to
non-zero, the operation is linear with the size of the array.
Instance offer a space savings of retaining only the non-zero indices and
values. For large array with only a few values set, this offers a huge
savings. However, as the cardinality of the array grows in relation to its
size, a dense double[]
array will offer better performance in both
space and time. This is especially true if the sparse array instance
approaches a cardinality to size ratio of .5
.
This class supports iterating over the non-zero indices and values in the
array via the iterator()
method. The indices will be returned in
sorted order. In cases where both the values and indices are needed,
iteration will be faster, O(k)
, than using getElementIndices()
and getPrimitive(int)
together, O(k *
log(k))
, where k
is the number of non-zero indices. The iterator
returned by this class is not thread-safe and will not throw an
exception if the array is concurrently modified during iteration.
SparseArray
,
Serialized FormConstructor and Description |
---|
SparseDoubleArray()
Creates a sparse
double array that grows to the maximum size set by
Double.MAX_VALUE . |
SparseDoubleArray(double[] array)
Creates a sparse array copy of the provided array, retaining only the
non-zero values.
|
SparseDoubleArray(int length)
Creates a sparse
double array with a fixed length |
SparseDoubleArray(int[] indices,
double[] values,
int length)
Creates a sparse array using the specified indices and their
corresponding values.
|
Modifier and Type | Method and Description |
---|---|
Double |
add(int index,
Double delta)
Adds the specified value to the value at the index and stores the result
(just as
array[index] += delta ). |
double |
addPrimitive(int index,
double delta)
Adds the specified value to the index.
|
int |
cardinality()
Returns the number of non-zero values in this sparse array
|
Double |
divide(int index,
Double value)
Divides the specified value to the index by the provided value and stores
the result at the index (just as
array[index] /= value ) |
double |
dividePrimitive(int index,
double value)
Divides the specified value to the index by the provided value and stores
the result at the index (just as
array[index] /= value ) |
Double |
get(int index)
Returns the value of this array at the index.
|
int[] |
getElementIndices()
Returns the indices of the array that contain non-
0 values. |
double |
getPrimitive(int index)
Retrieves the value at specified index or 0 if no value had been
specified.
|
Iterator<DoubleEntry> |
iterator()
Returns an iterator over the non-zero values in this array.
|
int |
length()
Returns the maximum length of this array.
|
Double |
multiply(int index,
Double value)
Multiplies the value to the index by the provided value and saves the
result at the index (just as
array[index] *= value ) |
double |
multiplyPrimitive(int index,
double value)
Multiplies the value to the index by the provided value and saves the
result at the index (just as
array[index] *= value ) |
void |
set(int index,
Double value)
Sets the object as the value at the index.
|
void |
setPrimitive(int index,
double value)
Sets the value of the index to the value using the Java primitives
without auto-boxing.
|
<E> E[] |
toArray(E[] array)
Fills the provided array with the values contained in this array that fit
within the length of the provided array.
|
double[] |
toPrimitiveArray(double[] array)
Sets the values of the provided array using the contents of this array.
|
String |
toString() |
public SparseDoubleArray()
double
array that grows to the maximum size set by
Double.MAX_VALUE
.public SparseDoubleArray(int length)
double
array with a fixed lengthpublic SparseDoubleArray(double[] array)
array
- the array whose values will be copied into this sparse arraypublic SparseDoubleArray(int[] indices, double[] values, int length)
indices
- an sorted array of positive values representing the
non-zero indices of the arrayvalues
- an array of values that correspond their respective indiceslength
- the total length of the arraypublic double addPrimitive(int index, double delta)
get
and set
.index
- the position in the arraydelta
- the change in value at the indexpublic Double add(int index, Double delta)
array[index] += delta
). Note that this can be used with
negative delta
values to achieve equivalent -=
functionality.add
in interface SparseNumericArray<Double>
index
- the position in the arraydelta
- the change in value at the indexpublic int cardinality()
cardinality
in interface SparseArray<Double>
public double dividePrimitive(int index, double value)
array[index] /= value
)index
- the position in the arrayvalue
- the value by which the value at the index will be dividedpublic Double divide(int index, Double value)
array[index] /= value
)divide
in interface SparseNumericArray<Double>
index
- the position in the arraypublic Double get(int index)
get
in interface SparseArray<Double>
index
- the position in the arraypublic int[] getElementIndices()
0
values.getElementIndices
in interface SparseArray<Double>
public double getPrimitive(int index)
index
- the position in the arrayArrayIndexOutOfBoundException
- if the index is greater than
the maximum length of the array.public Iterator<DoubleEntry> iterator()
iterator
in interface Iterable<DoubleEntry>
public int length()
length
in interface SparseArray<Double>
public double multiplyPrimitive(int index, double value)
array[index] *= value
)index
- the position in the arrayvalue
- the value that will be multiplied in value at the indexpublic Double multiply(int index, Double value)
array[index] *= value
)multiply
in interface SparseNumericArray<Double>
index
- the position in the arraypublic void set(int index, Double value)
set
in interface SparseArray<Double>
index
- an index in the arrayvalue
- the valuepublic void setPrimitive(int index, double value)
public <E> E[] toArray(E[] array)
toArray
in interface SparseArray<Double>
public double[] toPrimitiveArray(double[] array)
Copyright © 2012. All Rights Reserved.