public class SparseLongArray extends Object implements SparseNumericArray<Long>, Serializable
long 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 Long 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 long[] 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.
SparseArray,
Serialized Form| Constructor and Description |
|---|
SparseLongArray()
Creates a sparse
long array that grows to the maximum size set by
Long.MAX_VALUE. |
SparseLongArray(int length)
Creates a sparse
long array with a fixed length |
SparseLongArray(long[] array)
Creates a sparse array copy of the provided array, retaining only the
non-zero values.
|
| Modifier and Type | Method and Description |
|---|---|
Long |
add(int index,
Long delta)
Adds the specified value to the value at the index and stores the result
(just as
array[index] += delta). |
long |
addPrimitive(int index,
long delta)
Adds the specified value to the index.
|
int |
cardinality()
Returns the number of non-zero values in this sparse array
|
Long |
divide(int index,
Long value)
Divides the specified value to the index by the provided value and stores
the result at the index (just as
array[index] /= value) |
long |
dividePrimitive(int index,
long value)
Divides the specified value to the index by the provided value and stores
the result at the index (just as
array[index] /= value) |
Long |
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. |
long |
getPrimitive(int index)
Retrieve the value at specified index or 0 if no value had been
specified.
|
int |
length()
Returns the maximum length of this array.
|
Long |
multiply(int index,
Long value)
Multiplies the value to the index by the provided value and saves the
result at the index (just as
array[index] *= value) |
long |
multiplyPrimitive(int index,
long 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,
Long value)
Sets the object as the value at the index.
|
void |
setPrimitive(int index,
long 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.
|
long[] |
toPrimitiveArray(long[] array)
Sets the values of the provided array using the contents of this array.
|
public SparseLongArray()
long array that grows to the maximum size set by
Long.MAX_VALUE.public SparseLongArray(int length)
long array with a fixed lengthpublic SparseLongArray(long[] array)
public long addPrimitive(int index,
long delta)
get and set.index - the position in the arraydelta - the change in value at the indexpublic Long add(int index, Long delta)
array[index] += delta). Note that this can be used with
negative delta values to achieve equivalent -=
functionality.add in interface SparseNumericArray<Long>index - the position in the arraydelta - the change in value at the indexpublic int cardinality()
cardinality in interface SparseArray<Long>public long dividePrimitive(int index,
long value)
array[index] /= value)index - the position in the arrayvalue - the value by which the value at the index will be dividedpublic Long divide(int index, Long value)
array[index] /= value)divide in interface SparseNumericArray<Long>index - the position in the arraypublic Long get(int index)
get in interface SparseArray<Long>index - the position in the arraypublic int[] getElementIndices()
0 values.getElementIndices in interface SparseArray<Long>public long getPrimitive(int index)
index - the position in the arrayArrayIndexOutOfBoundException - if the index is greater than
the maximum length of the array.public int length()
length in interface SparseArray<Long>public long multiplyPrimitive(int index,
long value)
array[index] *= value)index - the position in the arrayvalue - the value that will be multiplied in value at the indexpublic Long multiply(int index, Long value)
array[index] *= value)multiply in interface SparseNumericArray<Long>index - the position in the arraypublic void set(int index,
Long value)
set in interface SparseArray<Long>index - an index in the arrayvalue - the valuepublic void setPrimitive(int index,
long value)
public <E> E[] toArray(E[] array)
toArray in interface SparseArray<Long>public long[] toPrimitiveArray(long[] array)
Copyright © 2012. All Rights Reserved.