All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ojalgo.matrix.store.RowsSupplier Maven / Gradle / Ivy

Go to download

oj! Algorithms - ojAlgo - is Open Source Java code that has to do with mathematics, linear algebra and optimisation.

There is a newer version: 55.0.1
Show newest version
package org.ojalgo.matrix.store;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

import org.ojalgo.array.SparseArray;
import org.ojalgo.array.SparseArray.SparseFactory;
import org.ojalgo.matrix.store.PhysicalStore.Factory;
import org.ojalgo.structure.Access2D;
import org.ojalgo.structure.ElementView1D;
import org.ojalgo.structure.RowView;

public final class RowsSupplier> implements Access2D, ElementsSupplier, Supplier> {

    static final class ItemView> extends RowView {

        private final RowsSupplier mySupplier;

        ItemView(final RowsSupplier access) {
            super(access);
            mySupplier = access;
        }

        public ElementView1D elements() {
            return this.getCurrent().elements();
        }

        public ElementView1D nonzeros() {
            return this.getCurrent().nonzeros();
        }

        private SparseArray getCurrent() {
            return mySupplier.getRow(Math.toIntExact(this.row()));
        }

    }

    private final int myColumnsCount;
    private final PhysicalStore.Factory myFactory;
    private final List> myRows = new ArrayList<>();

    RowsSupplier(final Factory factory, final int numberOfColumns) {
        super();
        myColumnsCount = numberOfColumns;
        myFactory = factory;
    }

    public SparseArray addRow() {
        return this.addRow(SparseArray.factory(myFactory.array()).limit(myColumnsCount).make());
    }

    public void addRows(final int numberToAdd) {
        SparseFactory factory = SparseArray.factory(myFactory.array()).limit(myColumnsCount);
        for (int i = 0; i < numberToAdd; i++) {
            myRows.add(factory.make());
        }
    }

    public long countColumns() {
        return myColumnsCount;
    }

    public long countRows() {
        return myRows.size();
    }

    public double doubleValue(final long row, final long col) {
        return myRows.get((int) row).doubleValue(col);
    }

    public PhysicalStore get() {
        return this.collect(myFactory);
    }

    public N get(final long row, final long col) {
        return myRows.get((int) row).get(col);
    }

    public SparseArray getRow(final int index) {
        return myRows.get(index);
    }

    public Factory physical() {
        return myFactory;
    }

    public SparseArray removeRow(final int index) {
        return myRows.remove(index);
    }

    public RowView rows() {
        return new ItemView<>(this);
    }

    public RowsSupplier selectRows(final int[] indices) {
        RowsSupplier retVal = new RowsSupplier<>(myFactory, myColumnsCount);
        for (int i = 0; i < indices.length; i++) {
            retVal.addRow(this.getRow(indices[i]));
        }
        return retVal;
    }

    public void supplyTo(final TransformableRegion receiver) {

        receiver.reset();

        for (int i = 0, limit = myRows.size(); i < limit; i++) {
            myRows.get(i).supplyNonZerosTo(receiver.regionByRows(i));
        }
    }

    @Override
    public String toString() {
        return Access2D.toString(this);
    }

    SparseArray addRow(final SparseArray rowToAdd) {
        if (myRows.add(rowToAdd)) {
            return rowToAdd;
        } else {
            return null;
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy