- java.lang.Object
-
- org.ojalgo.array.BasicArray<N>
-
- org.ojalgo.array.SparseArray<N>
-
- All Implemented Interfaces:
Access1D<N>,Access1D.Aggregatable<N>,Access1D.Collectable<N,Mutate1D>,Access1D.Visitable<N>,Mutate1D,Mutate1D.Fillable<N>,Mutate1D.Modifiable<N>,Structure1D
public final class SparseArray<N extends java.lang.Comparable<N>> extends BasicArray<N>
Only stores nonzero elements and/or elements specifically set by the user. The nonzero elements are stored internally in a
DenseArray.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceSparseArray.NonzeroPrimitiveCallbackstatic interfaceSparseArray.NonzeroReferenceTypeCallback<N extends java.lang.Comparable<N>>static classSparseArray.NonzeroView<N extends java.lang.Comparable<N>>static classSparseArray.SparseFactory<N extends java.lang.Comparable<N>>-
Nested classes/interfaces inherited from class org.ojalgo.array.BasicArray
BasicArray.BaseFactory<N extends java.lang.Comparable<N>,A extends BasicArray<N>>, BasicArray.Factory<N extends java.lang.Comparable<N>>
-
Nested classes/interfaces inherited from interface org.ojalgo.structure.Access1D
Access1D.Aggregatable<N extends java.lang.Comparable<N>>, Access1D.Collectable<N extends java.lang.Comparable<N>,R extends Mutate1D>, Access1D.ElementView<N extends java.lang.Comparable<N>>, Access1D.SelectionView<N extends java.lang.Comparable<N>>, Access1D.Sliceable<N extends java.lang.Comparable<N>>, Access1D.Visitable<N extends java.lang.Comparable<N>>
-
Nested classes/interfaces inherited from interface org.ojalgo.structure.Mutate1D
Mutate1D.Fillable<N extends java.lang.Comparable<N>>, Mutate1D.Mixable<N extends java.lang.Comparable<N>>, Mutate1D.Modifiable<N extends java.lang.Comparable<N>>, Mutate1D.ModifiableReceiver<N extends java.lang.Comparable<N>>, Mutate1D.Receiver<N extends java.lang.Comparable<N>>, Mutate1D.Sortable
-
Nested classes/interfaces inherited from interface org.ojalgo.structure.Structure1D
Structure1D.BasicMapper<T>, Structure1D.IndexMapper<T>, Structure1D.IntIndex, Structure1D.Logical<S extends Structure1D,B extends Structure1D.Logical<S,B>>, Structure1D.LongIndex, Structure1D.LoopCallback
-
-
Field Summary
Fields Modifier and Type Field Description private static NumberContextMATH_CONTEXTprivate intmyActualLengthThe number of nonzero elementsprivate PlainArray.Factory<N,?>myDenseFactoryprivate GrowthStrategymyGrowthStrategyprivate int[]myIndicesprivate intmySizeThe capacityprivate PlainArray<N>myValues
-
Constructor Summary
Constructors Constructor Description SparseArray(PlainArray.Factory<N,?> denseFactory, GrowthStrategy growthStrategy, int size)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, double addend)voidadd(long index, double addend)voidadd(long index, float addend)voidadd(long index, java.lang.Comparable<?> addend)voidaxpy(double a, Mutate1D.Modifiable<?> y)Will calculate y = y + a x, will add "a" times "this" to "y"(package private) longcapacity()longcount()The total number of elements in this structure.intcountNonzeros()longcountZeros()(package private) PlainArray<N>densify()doubledot(Access1D<?> vector)Will calculate and return the dot product of this 1D-structure and another input 1D-vector.doubledoubleValue(int index)doubledoubleValue(long index)(package private) doubledoubleValueInternally(int internalIndex)voidexchange(int indexA, int indexB)Efficiently exchanges two elements in the sparse array.protected voidexchange(long firstA, long firstB, long step, long count)static <N extends java.lang.Comparable<N>>
SparseArray.SparseFactory<N>factory(PlainArray.Factory<N,?> denseFactory)protected voidfill(long first, long limit, long step, N value)protected voidfill(long first, long limit, long step, NullaryFunction<?> supplier)voidfillAll(N value)voidfillAll(NullaryFunction<?> supplier)voidfillRange(long first, long limit, N value)voidfillRange(long first, long limit, NullaryFunction<?> supplier)(package private) longfirstIndex()longfirstInRange(long rangeFirst, long rangeLimit)Nget(long index)(package private) intgetActualLength()(package private) NgetInternally(int internalIndex)(package private) DenseArray<N>getValues()(package private) Access1D<N>getValues(long fromIncl, long toExcl)(package private) intindex(int index)(package private) intindex(long index)longindexOfLargest()protected longindexOfLargest(long first, long limit, long step)(package private) java.util.stream.IntStreamindices()(package private) longlastIndex()longlimitOfRange(long rangeFirst, long rangeLimit)protected voidmodify(long first, long limit, long step, BinaryFunction<N> function, Access1D<N> right)protected voidmodify(long first, long limit, long step, UnaryFunction<N> function)protected voidmodify(long first, long limit, long step, Access1D<N> left, BinaryFunction<N> function)voidmodifyAll(UnaryFunction<N> modifier)voidmodifyOne(long index, UnaryFunction<N> modifier)SparseArray.NonzeroView<N>nonzeros()Similar toAccess1D.elements()but avoids elements that are structurally known to be zero.(package private) voidput(long key, int index, double value)(package private) voidput(long key, int index, N value)voidputLast(int index, double value)Efficiently appends a new nonzero element at the end of this sparse array.(package private) voidremove(long externalIndex, int internalIndex)voidremoveShiftAndInsert(int first, int last, double newValue)All elements with indices in the range [first,last] should be shifted one step to the left (their indices decreased by 1).voidreset()Reset this mutable structure to some standard (all zeros) initial state.voidset(int index, double value)voidset(long index, double value)voidset(long index, float value)voidset(long index, java.lang.Comparable<?> value)intsize()The total number of elements in this structure.voidsupplyNonZerosTo(double[] receiver)Does NOT first reset the receiver! That means the elements in the receiver corresponding to zeros in this sparse array are not zero:ed or modified in any way.voidsupplyNonZerosTo(Mutate1D receiver)Does NOT first reset the receiver! That means the elements in the receiver corresponding to zeros in this sparse array are not zero:ed or modified in any way.voidsupplyTo(double[] receiver)voidsupplyTo(Mutate1D receiver)private voidupdate(int externalIndex, int internalIndex, double value, boolean shouldStoreZero)Will never remove anything - just insert or updateprivate voidupdate(int externalIndex, int internalIndex, java.lang.Comparable<?> value, boolean shouldStoreZero)Will never remove anything - just insert or updateprivate voidupdate(long externalIndex, int internalIndex, double value, boolean shouldStoreZero)private voidupdate(long externalIndex, int internalIndex, java.lang.Comparable<?> value, boolean shouldStoreZero)protected voidvisit(long first, long limit, long step, VoidFunction<N> visitor)voidvisitOne(long index, VoidFunction<N> visitor)voidvisitPrimitiveNonzerosInRange(long first, long limit, SparseArray.NonzeroPrimitiveCallback visitor)voidvisitRange(long first, long limit, VoidFunction<N> visitor)voidvisitReferenceTypeNonzerosInRange(long first, long limit, SparseArray.NonzeroReferenceTypeCallback<N> visitor)-
Methods inherited from class org.ojalgo.array.BasicArray
aggregateRange, equals, factory, getMathType, hashCode, isPrimitive, modifyMatching, modifyMatching, modifyRange, toString, visitAll, wrapInArray1D, wrapInArray2D, wrapInArrayAnyD
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.ojalgo.structure.Access1D
asCollectable1D, asKeyed1D, asList, byteValue, byteValue, elements, floatValue, floatValue, intValue, intValue, longValue, longValue, select, shortValue, shortValue, toList, toRawCopy1D
-
Methods inherited from interface org.ojalgo.structure.Access1D.Aggregatable
aggregateAll
-
Methods inherited from interface org.ojalgo.structure.Access1D.Collectable
collect
-
Methods inherited from interface org.ojalgo.structure.Mutate1D
set, set, set, set, set, set, set, set, set
-
Methods inherited from interface org.ojalgo.structure.Mutate1D.Fillable
fillCompatible, fillMatching, fillMatching, fillMatching
-
-
-
-
Field Detail
-
MATH_CONTEXT
private static final NumberContext MATH_CONTEXT
-
myActualLength
private int myActualLength
The number of nonzero elements
-
mySize
private final int mySize
The capacity
-
myDenseFactory
private final PlainArray.Factory<N extends java.lang.Comparable<N>,?> myDenseFactory
-
myGrowthStrategy
private final GrowthStrategy myGrowthStrategy
-
myIndices
private int[] myIndices
-
myValues
private PlainArray<N extends java.lang.Comparable<N>> myValues
-
-
Constructor Detail
-
SparseArray
SparseArray(PlainArray.Factory<N,?> denseFactory, GrowthStrategy growthStrategy, int size)
-
-
Method Detail
-
factory
public static <N extends java.lang.Comparable<N>> SparseArray.SparseFactory<N> factory(PlainArray.Factory<N,?> denseFactory)
-
add
public void add(int index, double addend)
-
add
public void add(long index, java.lang.Comparable<?> addend)
-
add
public void add(long index, double addend)
-
add
public void add(long index, float addend)
-
axpy
public void axpy(double a, Mutate1D.Modifiable<?> y)Description copied from interface:Access1DWill calculate y = y + a x, will add "a" times "this" to "y"- Parameters:
a- The scaley- The "vector" to update
-
count
public long count()
Description copied from interface:Structure1DThe total number of elements in this structure.You only need to implement this method if the structure can contain more than Integer.MAX_VALUE elements.
-
countNonzeros
public int countNonzeros()
-
countZeros
public long countZeros()
-
dot
public double dot(Access1D<?> vector)
Description copied from interface:Access1DWill calculate and return the dot product of this 1D-structure and another input 1D-vector.- Parameters:
vector- Another 1D-structure- Returns:
- The dot product
-
doubleValue
public double doubleValue(int index)
-
doubleValue
public double doubleValue(long index)
-
exchange
public void exchange(int indexA, int indexB)Efficiently exchanges two elements in the sparse array.This method is optimized for sparse arrays and handles all cases efficiently:
- Both elements are nonzero (direct swap)
- One element is zero, other is nonzero (remove one, add other)
- Both elements are zero (no operation needed)
- Parameters:
indexA- the first index to exchangeindexB- the second index to exchange
-
fillAll
public void fillAll(N value)
-
fillAll
public void fillAll(NullaryFunction<?> supplier)
-
fillRange
public void fillRange(long first, long limit, N value)
-
fillRange
public void fillRange(long first, long limit, NullaryFunction<?> supplier)
-
firstInRange
public long firstInRange(long rangeFirst, long rangeLimit)
-
get
public N get(long index)
-
indexOfLargest
public long indexOfLargest()
- Specified by:
indexOfLargestin interfaceAccess1D.Aggregatable<N extends java.lang.Comparable<N>>- Overrides:
indexOfLargestin classBasicArray<N extends java.lang.Comparable<N>>
-
limitOfRange
public long limitOfRange(long rangeFirst, long rangeLimit)
-
modifyAll
public void modifyAll(UnaryFunction<N> modifier)
- Specified by:
modifyAllin interfaceMutate1D.Modifiable<N extends java.lang.Comparable<N>>- Overrides:
modifyAllin classBasicArray<N extends java.lang.Comparable<N>>
-
modifyOne
public void modifyOne(long index, UnaryFunction<N> modifier)
-
nonzeros
public SparseArray.NonzeroView<N> nonzeros()
Description copied from interface:Access1DSimilar toAccess1D.elements()but avoids elements that are structurally known to be zero. (That does not eliminate all zero-values from this view.) With an arbitrary (dense) unstructured implementation theAccess1D.nonzeros()andAccess1D.elements()methods do the same thing! Only some specific implementations are able to actually exploit structure/sparsity to view fewer elements.
-
putLast
public void putLast(int index, double value)Efficiently appends a new nonzero element at the end of this sparse array.This method assumes that the supplied
indexis strictly greater than all existing indices in the array. No search is performed; the value is simply appended. If the ascending order of indices is broken, future behavior is unspecified. If the value is zero, nothing is stored.- Parameters:
index- the index at which to insert the new value (must be after all existing indices)value- the value to insert (only nonzero values are actually stored)
-
removeShiftAndInsert
public void removeShiftAndInsert(int first, int last, double newValue)All elements with indices in the range [first,last] should be shifted one step to the left (their indices decreased by 1). It is safe to assume that there is currently nothing stored at 'from'. The new value 'newValue' should be placed at position 'last'. That position is typically freed by the shift. If 'newValue' is zero it doesn't have to stored. The case where the range [first,last] was originally empty, but the new value is non-zero requires special logic.
-
reset
public void reset()
Description copied from interface:Mutate1DReset this mutable structure to some standard (all zeros) initial state. It must still be usuable after this call, and the structure/size/shape must not change.
-
set
public void set(int index, double value)
-
set
public void set(long index, java.lang.Comparable<?> value)
-
set
public void set(long index, double value)
-
set
public void set(long index, float value)
-
size
public int size()
Description copied from interface:Structure1DThe total number of elements in this structure.
-
supplyNonZerosTo
public void supplyNonZerosTo(double[] receiver)
Does NOT first reset the receiver! That means the elements in the receiver corresponding to zeros in this sparse array are not zero:ed or modified in any way.
-
supplyNonZerosTo
public void supplyNonZerosTo(Mutate1D receiver)
Does NOT first reset the receiver! That means the elements in the receiver corresponding to zeros in this sparse array are not zero:ed or modified in any way.
-
supplyTo
public void supplyTo(double[] receiver)
-
supplyTo
public void supplyTo(Mutate1D receiver)
- Specified by:
supplyToin interfaceAccess1D.Collectable<N extends java.lang.Comparable<N>,Mutate1D>- Overrides:
supplyToin classBasicArray<N extends java.lang.Comparable<N>>
-
visitOne
public void visitOne(long index, VoidFunction<N> visitor)
-
visitPrimitiveNonzerosInRange
public void visitPrimitiveNonzerosInRange(long first, long limit, SparseArray.NonzeroPrimitiveCallback visitor)
-
visitRange
public void visitRange(long first, long limit, VoidFunction<N> visitor)- Specified by:
visitRangein interfaceAccess1D.Visitable<N extends java.lang.Comparable<N>>- Overrides:
visitRangein classBasicArray<N extends java.lang.Comparable<N>>
-
visitReferenceTypeNonzerosInRange
public void visitReferenceTypeNonzerosInRange(long first, long limit, SparseArray.NonzeroReferenceTypeCallback<N> visitor)
-
update
private void update(int externalIndex, int internalIndex, java.lang.Comparable<?> value, boolean shouldStoreZero)Will never remove anything - just insert or update
-
update
private void update(int externalIndex, int internalIndex, double value, boolean shouldStoreZero)Will never remove anything - just insert or update
-
update
private void update(long externalIndex, int internalIndex, java.lang.Comparable<?> value, boolean shouldStoreZero)
-
update
private void update(long externalIndex, int internalIndex, double value, boolean shouldStoreZero)
-
exchange
protected void exchange(long firstA, long firstB, long step, long count)- Overrides:
exchangein classBasicArray<N extends java.lang.Comparable<N>>
-
fill
protected void fill(long first, long limit, long step, N value)- Overrides:
fillin classBasicArray<N extends java.lang.Comparable<N>>
-
fill
protected void fill(long first, long limit, long step, NullaryFunction<?> supplier)- Overrides:
fillin classBasicArray<N extends java.lang.Comparable<N>>
-
indexOfLargest
protected long indexOfLargest(long first, long limit, long step)- Overrides:
indexOfLargestin classBasicArray<N extends java.lang.Comparable<N>>
-
modify
protected void modify(long first, long limit, long step, Access1D<N> left, BinaryFunction<N> function)- Overrides:
modifyin classBasicArray<N extends java.lang.Comparable<N>>
-
modify
protected void modify(long first, long limit, long step, BinaryFunction<N> function, Access1D<N> right)- Overrides:
modifyin classBasicArray<N extends java.lang.Comparable<N>>
-
modify
protected void modify(long first, long limit, long step, UnaryFunction<N> function)- Overrides:
modifyin classBasicArray<N extends java.lang.Comparable<N>>
-
visit
protected void visit(long first, long limit, long step, VoidFunction<N> visitor)- Overrides:
visitin classBasicArray<N extends java.lang.Comparable<N>>
-
capacity
long capacity()
-
densify
PlainArray<N> densify()
-
doubleValueInternally
double doubleValueInternally(int internalIndex)
-
firstIndex
long firstIndex()
-
getActualLength
int getActualLength()
-
getInternally
N getInternally(int internalIndex)
-
getValues
DenseArray<N> getValues()
-
index
int index(int index)
-
index
int index(long index)
-
indices
java.util.stream.IntStream indices()
-
lastIndex
long lastIndex()
-
put
void put(long key, int index, double value)
-
put
void put(long key, int index, N value)
-
remove
void remove(long externalIndex, int internalIndex)
-
-