Module ojalgo

Class SparseArray<N extends java.lang.Comparable<N>>

    • Field Detail

      • 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
      • myIndices

        private int[] myIndices
      • myValues

        private PlainArray<N extends java.lang.Comparable<N>> myValues
    • Method Detail

      • 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: 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)
      • 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)
      • limitOfRange

        public long limitOfRange​(long rangeFirst,
                                 long rangeLimit)
      • 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,
                        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: 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)
      • visitOne

        public void visitOne​(long index,
                             VoidFunction<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:
        exchange in class BasicArray<N extends java.lang.Comparable<N>>
      • fill

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

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

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

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

        protected void visit​(long first,
                             long limit,
                             long step,
                             VoidFunction<N> visitor)
        Overrides:
        visit in class BasicArray<N extends java.lang.Comparable<N>>
      • capacity

        long capacity()
      • doubleValueInternally

        double doubleValueInternally​(int internalIndex)
      • firstIndex

        long firstIndex()
      • getActualLength

        int getActualLength()
      • getInternally

        N getInternally​(int internalIndex)
      • getValues

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