Skip to content

Commit aca1943

Browse files
Treiblesschorlectrueden
authored andcommitted
Add Inplace, interfaces, tests, ops, utility
1 parent 590e299 commit aca1943

8 files changed

Lines changed: 206 additions & 0 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.scijava.ops;
2+
3+
import java.util.function.BiConsumer;
4+
5+
@FunctionalInterface
6+
public interface BiInplace1<IO, I2> extends BiConsumer<IO, I2> {
7+
void mutate(IO io, I2 in2);
8+
9+
@Override
10+
default void accept(IO io, I2 in2) {
11+
mutate(io, in2);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.scijava.ops;
2+
3+
import java.util.function.BiConsumer;
4+
5+
@FunctionalInterface
6+
public interface BiInplace2<I1, IO> extends BiConsumer<I1, IO> {
7+
void mutate(I1 in1, IO io);
8+
9+
@Override
10+
default void accept(I1 in1, IO io) {
11+
mutate(in1, io);
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.scijava.ops;
2+
3+
import java.util.function.Consumer;
4+
5+
@FunctionalInterface
6+
public interface Inplace<I> extends Consumer<I> {
7+
void mutate(I in1);
8+
9+
@Override
10+
default void accept(I in) {
11+
mutate(in);
12+
}
13+
}

src/main/java/org/scijava/ops/math/Add.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.stream.Stream;
77

88
import org.scijava.ops.BiComputer;
9+
import org.scijava.ops.BiInplace1;
910
import org.scijava.ops.Op;
1011
import org.scijava.param.Parameter;
1112
import org.scijava.plugin.Plugin;
@@ -20,6 +21,8 @@ public interface MathAddOp extends Op {
2021
String ALIASES = "math.sum";
2122
}
2223

24+
// --------- Functions ---------
25+
2326
@Plugin(type = MathAddOp.class)
2427
@Parameter(key = "number1")
2528
@Parameter(key = "number2")
@@ -45,6 +48,8 @@ public double[] apply(double[] arr1, double[] arr2) {
4548
}
4649
}
4750

51+
// --------- Computers ---------
52+
4853
@Plugin(type = MathAddOp.class)
4954
@Parameter(key = "integer1")
5055
@Parameter(key = "integer2")
@@ -69,6 +74,20 @@ public void compute(double[] in1, double[] in2, double[] out) {
6974
}
7075
}
7176

77+
// --------- Inplaces ---------
78+
79+
@Plugin(type = MathAddOp.class)
80+
@Parameter(key = "arrayIO", type = ItemIO.BOTH)
81+
@Parameter(key = "array1")
82+
public static class MathPointwiseAddDoubleArraysInplace1 implements MathAddOp, BiInplace1<double[], double[]> {
83+
@Override
84+
public void mutate(double[] io, double[] in2) {
85+
for (int i = 0; i < io.length; i++ ) {
86+
io[i] += in2[i];
87+
}
88+
}
89+
}
90+
7291
// @Op
7392
// @Parameter(key = "number1")
7493
// @Parameter(key = "number2")

src/main/java/org/scijava/ops/math/Sqrt.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package org.scijava.ops.math;
22

33
import java.util.function.Function;
4+
import java.util.stream.IntStream;
45

6+
import org.scijava.ops.BiInplace1;
57
import org.scijava.ops.Computer;
8+
import org.scijava.ops.Inplace;
69
import org.scijava.ops.Op;
10+
import org.scijava.ops.math.Add.MathAddOp;
711
import org.scijava.param.Parameter;
812
import org.scijava.plugin.Plugin;
913
import org.scijava.struct.ItemIO;
@@ -12,6 +16,8 @@ public class Sqrt {
1216

1317
public interface MathSqrtOp extends Op {
1418
}
19+
20+
// --------- Functions ---------
1521

1622
@Plugin(type = MathSqrtOp.class)
1723
@Parameter(key = "number1")
@@ -22,6 +28,8 @@ public Double apply(Double t) {
2228
return Math.sqrt(t);
2329
}
2430
}
31+
32+
// --------- Computers ---------
2533

2634
@Plugin(type = MathSqrtOp.class)
2735
@Parameter(key = "array1")
@@ -34,4 +42,17 @@ public void compute(double[] in1, double[] out) {
3442
}
3543
}
3644
}
45+
46+
// --------- Inplaces ---------
47+
48+
@Plugin(type = MathSqrtOp.class)
49+
@Parameter(key = "arrayIO", type = ItemIO.BOTH)
50+
public static class MathPointwiseSqrtDoubleArrayInplace implements MathSqrtOp, Inplace<double[]> {
51+
@Override
52+
public void mutate(double[] in1) {
53+
IntStream.range(0, in1.length).forEach(index -> {
54+
in1[index] = Math.sqrt(in1[index]);
55+
});
56+
}
57+
}
3758
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package org.scijava.ops.util;
2+
3+
import java.lang.reflect.Type;
4+
5+
import org.scijava.ops.BiInplace1;
6+
import org.scijava.ops.Inplace;
7+
import org.scijava.ops.Op;
8+
import org.scijava.ops.base.OpService;
9+
import org.scijava.ops.types.Nil;
10+
11+
/**
12+
* Utility providing adaptation between {@link Op} types.
13+
*/
14+
public class Inplaces {
15+
16+
private Inplaces() {
17+
// NB: Prevent instantiation of utility class.
18+
}
19+
20+
public static <IO> Inplace<IO> unary(final OpService ops, final Class<? extends Op> opClass,
21+
final Class<IO> inputOutputType) {
22+
return ops.findOp( //
23+
new Nil<Inplace<IO>>() {
24+
}, //
25+
new Type[] { opClass }, //
26+
new Type[] { inputOutputType }, //
27+
inputOutputType);
28+
}
29+
30+
public static <IO, I2> BiInplace1<IO, I2> binary1(final OpService ops, final Class<? extends Op> opClass,
31+
final Class<IO> inputOutputType, final Class<I2> input2Type) {
32+
return ops.findOp( //
33+
new Nil<BiInplace1<IO, I2>>() {
34+
}, //
35+
new Type[] { opClass }, //
36+
new Type[] { inputOutputType, input2Type}, //
37+
inputOutputType);
38+
}
39+
40+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* #%L
3+
* SciJava Operations: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2018 SciJava developers.
6+
* %%
7+
* Redistribution and use in source and binary forms, with or without
8+
* modification, are permitted provided that the following conditions are met:
9+
*
10+
* 1. Redistributions of source code must retain the above copyright notice,
11+
* this list of conditions and the following disclaimer.
12+
* 2. Redistributions in binary form must reproduce the above copyright notice,
13+
* this list of conditions and the following disclaimer in the documentation
14+
* and/or other materials provided with the distribution.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
* #L%
28+
*/
29+
30+
package org.scijava.ops;
31+
32+
import org.junit.Test;
33+
import org.scijava.ops.math.Add.MathAddOp;
34+
import org.scijava.ops.math.Sqrt.MathSqrtOp;
35+
import org.scijava.ops.util.Inplaces;
36+
37+
public class InplacesTest extends AbstractTestEnvironment {
38+
39+
@Test
40+
public void testUnaryInplaces() {
41+
Inplace<double[]> inplaceSqrt = Inplaces.unary(ops(), MathSqrtOp.class, double[].class);
42+
final double[] a1 = { 4, 100, 36 };
43+
inplaceSqrt.mutate(a1);
44+
assert arrayEquals(a1, 2.0, 10.0, 6.0);
45+
}
46+
47+
@Test
48+
public void testBinaryInplaces() {
49+
final BiInplace1<double[], double[]> inplaceAdd = Inplaces.binary1(ops(), MathAddOp.class, double[].class,
50+
double[].class);
51+
final double[] a1 = { 3, 5, 7 };
52+
final double[] a2 = { 2, 4, 9 };
53+
inplaceAdd.mutate(a1, a2);
54+
assert arrayEquals(a1, 5.0, 9.0, 16.0);
55+
}
56+
}

src/test/java/org/scijava/ops/OpsTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,37 @@ public void binaryComputer() {
122122
assert arrayEquals(result, 5.0, 9.0, 16.0);
123123
}
124124

125+
@Test
126+
public void unaryInplace() {
127+
Class<double[]> cArray = double[].class;
128+
final Inplace<double[]> inplaceSqrt = ops().findOp( //
129+
new Nil<Inplace<double[]>>() {
130+
}, //
131+
new Type[] { MathSqrtOp.class }, //
132+
new Type[] { cArray }, //
133+
cArray//
134+
);
135+
final double[] a1 = { 4, 100, 36 };
136+
inplaceSqrt.mutate(a1);
137+
assert arrayEquals(a1, 2.0, 10.0, 6.0);
138+
}
139+
140+
@Test
141+
public void binaryInplace() {
142+
Class<double[]> cArray = double[].class;
143+
final BiInplace1<double[], double[]> inplaceAdd = ops().findOp( //
144+
new Nil<BiInplace1<double[], double[]>>() {
145+
}, //
146+
new Type[] { MathAddOp.class }, //
147+
new Type[] { cArray, cArray }, //
148+
cArray//
149+
);
150+
final double[] a1 = { 3, 5, 7 };
151+
final double[] a2 = { 2, 4, 9 };
152+
inplaceAdd.mutate(a1, a2);
153+
assert arrayEquals(a1, 5.0, 9.0, 16.0);
154+
}
155+
125156
@Test
126157
public void testSecondaryInputs() {
127158
Class<Double> c = Double.class;

0 commit comments

Comments
 (0)