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

public final class SparseArray<N extends 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.

  • Field Details

    • 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 Comparable<N>, ?> myDenseFactory
    • myGrowthStrategy

      private final GrowthStrategy myGrowthStrategy
    • myIndices

      private int[] myIndices
    • myValues

      private PlainArray<N extends Comparable<N>> myValues
  • Constructor Details

  • 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

      public void add(long index, 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: Access1D
      Will calculate y = y + a x, will add "a" times "this" to "y"
      Parameters:
      a - The scale
      y - The "vector" to update
    • count

      public long count()
      Description copied from interface: Structure1D
      The 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: Access1D
      Will 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 exchange
      indexB - 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:
      indexOfLargest in interface Access1D.Aggregatable<N extends Comparable<N>>
      Overrides:
      indexOfLargest in class BasicArray<N extends Comparable<N>>
    • limitOfRange

      public long limitOfRange(long rangeFirst, long rangeLimit)
    • modifyAll

      public void modifyAll(UnaryFunction<N> modifier)
      Specified by:
      modifyAll in interface Mutate1D.Modifiable<N extends Comparable<N>>
      Overrides:
      modifyAll in class BasicArray<N extends Comparable<N>>
    • modifyOne

      public void modifyOne(long index, UnaryFunction<N> modifier)
    • nonzeros

      public SparseArray.NonzeroView<N> nonzeros()
      Description copied from interface: Access1D
      Similar to Access1D.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 the Access1D.nonzeros() and Access1D.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 index is 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: Mutate1D
      Reset 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, 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: Structure1D
      The 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:
      supplyTo in interface Access1D.Collectable<N extends Comparable<N>, Mutate1D>
      Overrides:
      supplyTo in class BasicArray<N extends 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:
      visitRange in interface Access1D.Visitable<N extends Comparable<N>>
      Overrides:
      visitRange in class BasicArray<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:
      exchange in class BasicArray<N extends Comparable<N>>
    • fill

      protected void fill(long first, long limit, long step, N value)
      Overrides:
      fill in class BasicArray<N extends Comparable<N>>
    • fill

      protected void fill(long first, long limit, long step, NullaryFunction<?> supplier)
      Overrides:
      fill in class BasicArray<N extends Comparable<N>>
    • indexOfLargest

      protected long indexOfLargest(long first, long limit, long step)
      Overrides:
      indexOfLargest in class BasicArray<N extends Comparable<N>>
    • modify

      protected void modify(long first, long limit, long step, Access1D<N> left, BinaryFunction<N> function)
      Overrides:
      modify in class BasicArray<N extends Comparable<N>>
    • modify

      protected void modify(long first, long limit, long step, BinaryFunction<N> function, Access1D<N> right)
      Overrides:
      modify in class BasicArray<N extends Comparable<N>>
    • modify

      protected void modify(long first, long limit, long step, UnaryFunction<N> function)
      Overrides:
      modify in class BasicArray<N extends Comparable<N>>
    • visit

      protected void visit(long first, long limit, long step, VoidFunction<N> visitor)
      Overrides:
      visit in class BasicArray<N extends 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()
    • getValues

      Access1D<N> getValues(long fromIncl, long toExcl)
    • 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

      void put(long key, int index, N value)
    • remove

      void remove(long externalIndex, int internalIndex)