org.ojalgo.data.DataBatch Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ojalgo Show documentation
Show all versions of ojalgo Show documentation
oj! Algorithms - ojAlgo - is Open Source Java code that has to do with mathematics, linear algebra and optimisation.
/*
* Copyright 1997-2021 Optimatika
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package org.ojalgo.data;
import static org.ojalgo.function.constant.PrimitiveMath.*;
import java.util.Collection;
import org.ojalgo.structure.Access1D;
import org.ojalgo.structure.Access2D;
import org.ojalgo.structure.Factory2D;
import org.ojalgo.structure.Mutate2D;
import org.ojalgo.structure.Mutate2D.Receiver;
/**
* A reusable data batch. Build 2D data structures by adding a set of 1D structures treating then as rows. Use
* it, {@link #reset()} and do it again.
*
* @author apete
*/
public final class DataBatch implements Access2D, Access2D.Collectable> {
/**
* With the batch size/capacity specified. The returned instance will throw an exception if too many rows
* are added to it.
*/
public static DataBatch from(final Factory2D extends Mutate2D.ModifiableReceiver> factory, final int batchSize, final int dataNodes) {
return new DataBatch(factory.make(batchSize, dataNodes));
}
private int myCursor = 0;
private final Mutate2D.ModifiableReceiver myData;
DataBatch(final Mutate2D.ModifiableReceiver data) {
super();
myData = data;
}
public void addRow(final Access1D row) {
myData.fillRow(myCursor, row);
myCursor++;
}
public void addRows(final Collection extends Access1D> rows) {
rows.forEach(this::addRow);
}
public void addRowWithSingleUnit(final int unitIndex) {
myData.fillRow(myCursor, ZERO);
myData.set(myCursor, unitIndex, ONE);
myCursor++;
}
/**
* Assumes all rows are of equal length and will only check the first. Returns 0 if there are no rows.
*
* @see org.ojalgo.structure.Structure2D#countColumns()
*/
public long countColumns() {
return myData.countColumns();
}
public long countRows() {
return myData.countRows();
}
public double doubleValue(final long row, final long col) {
return myData.doubleValue(row, col);
}
public Double get(final long row, final long col) {
return Double.valueOf(this.doubleValue(row, col));
}
public boolean isFull() {
return this.remaining() == 0;
}
/**
* Remaining capacity / batch entries / number of rows.
*/
public int remaining() {
return this.getRowDim() - myCursor;
}
public void reset() {
myCursor = 0;
}
public void supplyTo(final Receiver receiver) {
receiver.fillMatching(myData);
}
}