Skip to content

Commit

Permalink
Simplify code where possible by reusing a utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
bdezonia committed Jan 23, 2023
1 parent 4396205 commit 8fe57c8
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 38 deletions.
9 changes: 4 additions & 5 deletions src/main/java/nom/bdezonia/zorbage/algorithm/CreateMask.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import nom.bdezonia.zorbage.data.NdData;
import nom.bdezonia.zorbage.datasource.IndexedDataSource;
import nom.bdezonia.zorbage.function.Function1;
import nom.bdezonia.zorbage.misc.DataSourceUtils;
import nom.bdezonia.zorbage.procedure.Procedure2;

/**
Expand Down Expand Up @@ -97,11 +98,9 @@ public void call(U a, UnsignedInt1Member b) {
public static <T extends Algebra<T,U>, U>
DimensionedDataSource<UnsignedInt1Member> compute(T alg, Function1<Boolean,U> condition, DimensionedDataSource<U> a)
{
long[] dims = new long[a.numDimensions()];
for (int i = 0; i < dims.length; i++) {
dims[i] = a.dimension(i);
}
IndexedDataSource<UnsignedInt1Member> data = compute(alg, condition, a.rawData());
long[] dims = DataSourceUtils.dimensions(a);
IndexedDataSource<UnsignedInt1Member> data =
compute(alg, condition, a.rawData());
return new NdData<>(dims, data);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import nom.bdezonia.zorbage.coordinates.LinearNdCoordinateSpace;
import nom.bdezonia.zorbage.data.DimensionedDataSource;
import nom.bdezonia.zorbage.data.DimensionedStorage;
import nom.bdezonia.zorbage.misc.DataSourceUtils;
import nom.bdezonia.zorbage.sampling.IntegerIndex;
import nom.bdezonia.zorbage.sampling.SamplingIterator;

Expand Down Expand Up @@ -87,10 +88,7 @@ DimensionedDataSource<U> compute(T alg, int[] permutation, DimensionedDataSource

int numD = input.numDimensions();

long[] origDims = new long[numD];
for (int i = 0; i < numD; i++) {
origDims[i] = input.dimension(i);
}
long[] origDims = DataSourceUtils.dimensions(input);

// test the correctness of the input data

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
package nom.bdezonia.zorbage.algorithm;

import nom.bdezonia.zorbage.algebra.Dimensioned;
import nom.bdezonia.zorbage.misc.DataSourceUtils;
import nom.bdezonia.zorbage.sampling.IntegerIndex;
import nom.bdezonia.zorbage.sampling.SamplingCartesianIntegerGrid;
import nom.bdezonia.zorbage.sampling.SamplingIterator;
Expand Down Expand Up @@ -106,13 +107,7 @@ SamplingIterator<IntegerIndex> compute(long[] dims)
public static
SamplingIterator<IntegerIndex> compute(Dimensioned entity)
{
int numD = entity.numDimensions();

long[] dims = new long[numD];

for (int i = 0; i < numD; i++) {
dims[i] = entity.dimension(i);
}
long[] dims = DataSourceUtils.dimensions(entity);

return compute(dims);
}
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/nom/bdezonia/zorbage/algorithm/TensorShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
package nom.bdezonia.zorbage.algorithm;

import nom.bdezonia.zorbage.algebra.TensorMember;
import nom.bdezonia.zorbage.misc.DataSourceUtils;

/**
*
Expand All @@ -53,10 +54,7 @@ private TensorShape() { }
void compute(TensorMember<?> input, TensorMember<?> output)
{
if (input == output) return;
long[] dims = new long[input.numDimensions()];
for (int i = 0; i < dims.length; i++) {
dims[i] = input.dimension(i);
}
long[] dims = DataSourceUtils.dimensions(input);
output.alloc(dims);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import nom.bdezonia.zorbage.algorithm.GridIterator;
import nom.bdezonia.zorbage.data.DimensionedDataSource;
import nom.bdezonia.zorbage.data.DimensionedStorage;
import nom.bdezonia.zorbage.misc.DataSourceUtils;
import nom.bdezonia.zorbage.misc.ThreadingUtils;
import nom.bdezonia.zorbage.sampling.IntegerIndex;
import nom.bdezonia.zorbage.sampling.SamplingIterator;
Expand Down Expand Up @@ -176,10 +177,7 @@ public void run() {
U value = alg.construct();
IntegerIndex inputPoint = new IntegerIndex(numD);
IntegerIndex outputPoint = new IntegerIndex(numD);
long[] inputDims = new long[numD];
for (int i = 0; i < numD; i++) {
inputDims[i] = input.dimension(i);
}
long[] inputDims = DataSourceUtils.dimensions(input);
SamplingIterator<IntegerIndex> iter = GridIterator.compute(min, max);
while (iter.hasNext()) {
iter.next(outputPoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import nom.bdezonia.zorbage.algorithm.GridIterator;
import nom.bdezonia.zorbage.data.DimensionedDataSource;
import nom.bdezonia.zorbage.data.DimensionedStorage;
import nom.bdezonia.zorbage.misc.DataSourceUtils;
import nom.bdezonia.zorbage.misc.ThreadingUtils;
import nom.bdezonia.zorbage.sampling.IntegerIndex;
import nom.bdezonia.zorbage.sampling.SamplingIterator;
Expand Down Expand Up @@ -176,10 +177,7 @@ public void run() {
U value = alg.construct();
IntegerIndex inputPoint = new IntegerIndex(numD);
IntegerIndex outputPoint = new IntegerIndex(numD);
long[] inputDims = new long[numD];
for (int i = 0; i < numD; i++) {
inputDims[i] = input.dimension(i);
}
long[] inputDims = DataSourceUtils.dimensions(input);
SamplingIterator<IntegerIndex> iter = GridIterator.compute(min, max);
while (iter.hasNext()) {
iter.next(outputPoint);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import nom.bdezonia.zorbage.data.DimensionedDataSource;
import nom.bdezonia.zorbage.data.DimensionedStorage;
import nom.bdezonia.zorbage.misc.BigDecimalUtils;
import nom.bdezonia.zorbage.misc.DataSourceUtils;
import nom.bdezonia.zorbage.misc.ThreadingUtils;

/**
Expand Down Expand Up @@ -172,10 +173,7 @@ public void run() {
U value = alg.construct();
IntegerIndex inputPoint = new IntegerIndex(numD);
IntegerIndex outputPoint = new IntegerIndex(numD);
long[] inputDims = new long[numD];
for (int i = 0; i < numD; i++) {
inputDims[i] = input.dimension(i);
}
long[] inputDims = DataSourceUtils.dimensions(input);
SamplingIterator<IntegerIndex> iter = GridIterator.compute(min, max);
while (iter.hasNext()) {
iter.next(outputPoint);
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/nom/bdezonia/zorbage/data/PipedDataSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import nom.bdezonia.zorbage.algebra.StorageConstruction;
import nom.bdezonia.zorbage.datasource.IndexedDataSource;
import nom.bdezonia.zorbage.datasource.SequencedDataSource;
import nom.bdezonia.zorbage.misc.DataSourceUtils;

/**
*
Expand Down Expand Up @@ -74,10 +75,7 @@ public PipedDataSource(DimensionedDataSource<U> d, int dim, IntegerIndex coords)
for (int i = 0; i < coords.numDimensions(); i++) {
this.coords.set(i, coords.get(i));
}
this.parentDims = new long[d.numDimensions()];
for (int i = 0; i < d.numDimensions(); i++) {
this.parentDims[i] = d.dimension(i);
}
this.parentDims = DataSourceUtils.dimensions(d);
this.data = findSubset();
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/nom/bdezonia/zorbage/misc/DataSourceUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*/
package nom.bdezonia.zorbage.misc;

import nom.bdezonia.zorbage.data.DimensionedDataSource;
import nom.bdezonia.zorbage.algebra.Dimensioned;

/**
*
Expand All @@ -50,7 +50,7 @@ private DataSourceUtils() { }
*
* @return The dimensions of the data
*/
public static long[] dimensions(DimensionedDataSource<?> ds) {
public static long[] dimensions(Dimensioned ds) {

long[] dims = new long[ds.numDimensions()];

Expand Down

0 comments on commit 8fe57c8

Please sign in to comment.