Class SparseArray<N extends Comparable<N>>
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
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 ClassesModifier and TypeClassDescriptionstatic interfacestatic interfaceSparseArray.NonzeroReferenceTypeCallback<N extends Comparable<N>>static final classSparseArray.NonzeroView<N extends Comparable<N>>static final classSparseArray.SparseFactory<N extends Comparable<N>>Nested classes/interfaces inherited from class BasicArray
BasicArray.BaseFactory<N,A>, BasicArray.Factory<N> Nested classes/interfaces inherited from interface Access1D
Access1D.Aggregatable<N>, Access1D.Collectable<N,R>, Access1D.ElementView<N>, Access1D.SelectionView<N>, Access1D.Sliceable<N>, Access1D.Visitable<N> Nested classes/interfaces inherited from interface Mutate1D
Mutate1D.Fillable<N>, Mutate1D.Mixable<N>, Mutate1D.Modifiable<N>, Mutate1D.ModifiableReceiver<N>, Mutate1D.Receiver<N>, Mutate1D.SortableNested classes/interfaces inherited from interface Structure1D
Structure1D.BasicMapper<T>, Structure1D.IndexMapper<T>, Structure1D.IntIndex, Structure1D.Logical<S,B>, Structure1D.LongIndex, Structure1D.LoopCallback -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final NumberContextprivate intThe number of nonzero elementsprivate final PlainArray.Factory<N, ?> private final GrowthStrategyprivate int[]private final intThe capacityprivate PlainArray<N> -
Constructor Summary
ConstructorsConstructorDescriptionSparseArray(PlainArray.Factory<N, ?> denseFactory, GrowthStrategy growthStrategy, int size) -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(int index, double addend) voidadd(long index, double addend) voidadd(long index, float addend) voidadd(long index, 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.intlong(package private) PlainArray<N> densify()doubleWill 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 Comparable<N>>
SparseArray.SparseFactory<N> factory(PlainArray.Factory<N, ?> denseFactory) protected voidprotected voidfill(long first, long limit, long step, NullaryFunction<?> supplier) voidvoidfillAll(NullaryFunction<?> supplier) voidvoidfillRange(long first, long limit, NullaryFunction<?> supplier) (package private) longlongfirstInRange(long rangeFirst, long rangeLimit) get(long index) (package private) int(package private) NgetInternally(int internalIndex) (package private) DenseArray<N> getValues(long fromIncl, long toExcl) (package private) intindex(int index) (package private) intindex(long index) longprotected longindexOfLargest(long first, long limit, long step) (package private) IntStreamindices()(package private) longlonglimitOfRange(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) 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) voidvoidputLast(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, 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) voidprivate voidupdate(int externalIndex, int internalIndex, double value, boolean shouldStoreZero) Will never remove anything - just insert or updateprivate voidupdate(int externalIndex, int internalIndex, 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, 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 BasicArray
aggregateRange, equals, factory, getMathType, hashCode, isPrimitive, modifyMatching, modifyMatching, modifyRange, toString, visitAll, wrapInArray1D, wrapInArray2D, wrapInArrayAnyDMethods inherited from interface Access1D
asCollectable1D, asKeyed1D, asList, byteValue, byteValue, elements, floatValue, floatValue, intValue, intValue, longValue, longValue, select, shortValue, shortValue, toList, toRawCopy1DMethods inherited from interface Access1D.Aggregatable
aggregateAllMethods inherited from interface Access1D.Collectable
collectMethods inherited from interface Mutate1D.Fillable
fillCompatible, fillMatching, fillMatching, fillMatching, fillMatching
-
Field Details
-
MATH_CONTEXT
-
myActualLength
private int myActualLengthThe number of nonzero elements -
mySize
private final int mySizeThe capacity -
myDenseFactory
-
myGrowthStrategy
-
myIndices
private int[] myIndices -
myValues
-
-
Constructor Details
-
SparseArray
SparseArray(PlainArray.Factory<N, ?> denseFactory, GrowthStrategy growthStrategy, int size)
-
-
Method Details
-
factory
public static <N extends Comparable<N>> SparseArray.SparseFactory<N> factory(PlainArray.Factory<N, ?> denseFactory) -
add
public void add(int index, double addend) -
add
-
add
public void add(long index, double addend) -
add
public void add(long index, float addend) -
axpy
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
-
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
-
fillAll
-
fillRange
-
fillRange
-
firstInRange
public long firstInRange(long rangeFirst, long rangeLimit) -
get
-
indexOfLargest
public long indexOfLargest()- Specified by:
indexOfLargestin interfaceAccess1D.Aggregatable<N extends Comparable<N>>- Overrides:
indexOfLargestin classBasicArray<N extends Comparable<N>>
-
limitOfRange
public long limitOfRange(long rangeFirst, long rangeLimit) -
modifyAll
- Specified by:
modifyAllin interfaceMutate1D.Modifiable<N extends Comparable<N>>- Overrides:
modifyAllin classBasicArray<N extends Comparable<N>>
-
modifyOne
-
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
-
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
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
- Specified by:
supplyToin interfaceAccess1D.Collectable<N extends Comparable<N>, Mutate1D>- Overrides:
supplyToin classBasicArray<N extends Comparable<N>>
-
visitOne
-
visitPrimitiveNonzerosInRange
public void visitPrimitiveNonzerosInRange(long first, long limit, SparseArray.NonzeroPrimitiveCallback visitor) -
visitRange
- Specified by:
visitRangein interfaceAccess1D.Visitable<N extends Comparable<N>>- Overrides:
visitRangein classBasicArray<N extends Comparable<N>>
-
visitReferenceTypeNonzerosInRange
public void visitReferenceTypeNonzerosInRange(long first, long limit, SparseArray.NonzeroReferenceTypeCallback<N> visitor) -
update
private void update(int externalIndex, int internalIndex, 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, 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 Comparable<N>>
-
fill
- Overrides:
fillin classBasicArray<N extends Comparable<N>>
-
fill
- Overrides:
fillin classBasicArray<N extends Comparable<N>>
-
indexOfLargest
protected long indexOfLargest(long first, long limit, long step) - Overrides:
indexOfLargestin classBasicArray<N extends Comparable<N>>
-
modify
protected void modify(long first, long limit, long step, Access1D<N> left, BinaryFunction<N> function) - Overrides:
modifyin classBasicArray<N extends Comparable<N>>
-
modify
protected void modify(long first, long limit, long step, BinaryFunction<N> function, Access1D<N> right) - Overrides:
modifyin classBasicArray<N extends Comparable<N>>
-
modify
- Overrides:
modifyin classBasicArray<N extends Comparable<N>>
-
visit
- Overrides:
visitin classBasicArray<N extends Comparable<N>>
-
capacity
long capacity() -
densify
PlainArray<N> densify() -
doubleValueInternally
double doubleValueInternally(int internalIndex) -
firstIndex
long firstIndex() -
getActualLength
int getActualLength() -
getInternally
-
getValues
DenseArray<N> getValues() -
getValues
-
index
int index(int index) -
index
int index(long index) -
indices
IntStream indices() -
lastIndex
long lastIndex() -
put
void put(long key, int index, double value) -
put
-
remove
void remove(long externalIndex, int internalIndex)
-