Skip to content

Commit 398e2a0

Browse files
gselzerctrueden
authored andcommitted
Lift Function to Arrays
1 parent 4af6708 commit 398e2a0

6 files changed

Lines changed: 976 additions & 0 deletions

File tree

Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
1+
/*
2+
* #%L
3+
* SciJava Operations: a framework for reusable algorithms.
4+
* %%
5+
* Copyright (C) 2016 - 2019 SciJava Ops 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+
/*
31+
* This is autogenerated source code -- DO NOT EDIT. Instead, edit the
32+
* corresponding template in templates/ and rerun bin/generate.groovy.
33+
*/
34+
35+
package org.scijava.ops.adapt.lift;
36+
37+
import java.lang.reflect.Array;
38+
import java.util.function.BiFunction;
39+
import java.util.function.Function;
40+
41+
import org.scijava.ops.OpField;
42+
import org.scijava.ops.core.OpCollection;
43+
import org.scijava.ops.function.Functions;
44+
import org.scijava.plugin.Plugin;
45+
46+
/**
47+
* Converts {@link Functions} operating on single types to {@link Functions}
48+
* that operate on arrays of types. N.B. it is the user's responsibility to pass
49+
* arrays of the same length (otherwise the Op will stop when one of the arrays
50+
* runs out of {@link Object}s).
51+
*
52+
* @author Gabriel Selzer
53+
*/
54+
@Plugin(type = OpCollection.class)
55+
public class FunctionToArrays<I, I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15, I16, O> {
56+
57+
// TODO: extract logic to a utility class
58+
private int minLength(Object[]... arrays) {
59+
int minLength = Integer.MAX_VALUE;
60+
for (Object[] array : arrays)
61+
if (array.length < minLength) minLength = array.length;
62+
return minLength;
63+
}
64+
65+
// NOTE: we cannot convert Producers since there is no way to determine the
66+
// length of the output array
67+
68+
@OpField(names = "adapt", params = "fromOp, toOp")
69+
public final Function<Function<I, O>, Function<I[], O[]>> liftFunction1 =
70+
(function) -> {
71+
return (in) -> {
72+
int len = minLength(in);
73+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
74+
O component = function.apply(in[0]);
75+
@SuppressWarnings("unchecked")
76+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
77+
78+
for (int i = 0; i < len; i++) {
79+
out[i] = function.apply(in[i]);
80+
}
81+
return out;
82+
};
83+
};
84+
85+
@OpField(names = "adapt", params = "fromOp, toOp")
86+
public final Function<BiFunction<I1, I2, O>, BiFunction<I1[], I2[], O[]>> liftFunction2 =
87+
(function) -> {
88+
return (in1, in2) -> {
89+
int len = minLength(in1, in2);
90+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
91+
O component = function.apply(in1[0], in2[0]);
92+
@SuppressWarnings("unchecked")
93+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
94+
95+
for (int i = 0; i < len; i++) {
96+
out[i] = function.apply(in1[i], in2[i]);
97+
}
98+
return out;
99+
};
100+
};
101+
102+
@OpField(names = "adapt", params = "fromOp, toOp")
103+
public final Function<Functions.Arity3<I1, I2, I3, O>, Functions.Arity3<I1[], I2[], I3[], O[]>> liftFunction3 =
104+
(function) -> {
105+
return (in1, in2, in3) -> {
106+
int len = minLength(in1, in2, in3);
107+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
108+
O component = function.apply(in1[0], in2[0], in3[0]);
109+
@SuppressWarnings("unchecked")
110+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
111+
112+
for (int i = 0; i < len; i++) {
113+
out[i] = function.apply(in1[i], in2[i], in3[i]);
114+
}
115+
return out;
116+
};
117+
};
118+
119+
@OpField(names = "adapt", params = "fromOp, toOp")
120+
public final Function<Functions.Arity4<I1, I2, I3, I4, O>, Functions.Arity4<I1[], I2[], I3[], I4[], O[]>> liftFunction4 =
121+
(function) -> {
122+
return (in1, in2, in3, in4) -> {
123+
int len = minLength(in1, in2, in3, in4);
124+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
125+
O component = function.apply(in1[0], in2[0], in3[0], in4[0]);
126+
@SuppressWarnings("unchecked")
127+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
128+
129+
for (int i = 0; i < len; i++) {
130+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i]);
131+
}
132+
return out;
133+
};
134+
};
135+
136+
@OpField(names = "adapt", params = "fromOp, toOp")
137+
public final Function<Functions.Arity5<I1, I2, I3, I4, I5, O>, Functions.Arity5<I1[], I2[], I3[], I4[], I5[], O[]>> liftFunction5 =
138+
(function) -> {
139+
return (in1, in2, in3, in4, in5) -> {
140+
int len = minLength(in1, in2, in3, in4, in5);
141+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
142+
O component = function.apply(in1[0], in2[0], in3[0], in4[0], in5[0]);
143+
@SuppressWarnings("unchecked")
144+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
145+
146+
for (int i = 0; i < len; i++) {
147+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i], in5[i]);
148+
}
149+
return out;
150+
};
151+
};
152+
153+
@OpField(names = "adapt", params = "fromOp, toOp")
154+
public final Function<Functions.Arity6<I1, I2, I3, I4, I5, I6, O>, Functions.Arity6<I1[], I2[], I3[], I4[], I5[], I6[], O[]>> liftFunction6 =
155+
(function) -> {
156+
return (in1, in2, in3, in4, in5, in6) -> {
157+
int len = minLength(in1, in2, in3, in4, in5, in6);
158+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
159+
O component = function.apply(in1[0], in2[0], in3[0], in4[0], in5[0], in6[0]);
160+
@SuppressWarnings("unchecked")
161+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
162+
163+
for (int i = 0; i < len; i++) {
164+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i], in5[i], in6[i]);
165+
}
166+
return out;
167+
};
168+
};
169+
170+
@OpField(names = "adapt", params = "fromOp, toOp")
171+
public final Function<Functions.Arity7<I1, I2, I3, I4, I5, I6, I7, O>, Functions.Arity7<I1[], I2[], I3[], I4[], I5[], I6[], I7[], O[]>> liftFunction7 =
172+
(function) -> {
173+
return (in1, in2, in3, in4, in5, in6, in7) -> {
174+
int len = minLength(in1, in2, in3, in4, in5, in6, in7);
175+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
176+
O component = function.apply(in1[0], in2[0], in3[0], in4[0], in5[0], in6[0], in7[0]);
177+
@SuppressWarnings("unchecked")
178+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
179+
180+
for (int i = 0; i < len; i++) {
181+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i], in5[i], in6[i], in7[i]);
182+
}
183+
return out;
184+
};
185+
};
186+
187+
@OpField(names = "adapt", params = "fromOp, toOp")
188+
public final Function<Functions.Arity8<I1, I2, I3, I4, I5, I6, I7, I8, O>, Functions.Arity8<I1[], I2[], I3[], I4[], I5[], I6[], I7[], I8[], O[]>> liftFunction8 =
189+
(function) -> {
190+
return (in1, in2, in3, in4, in5, in6, in7, in8) -> {
191+
int len = minLength(in1, in2, in3, in4, in5, in6, in7, in8);
192+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
193+
O component = function.apply(in1[0], in2[0], in3[0], in4[0], in5[0], in6[0], in7[0], in8[0]);
194+
@SuppressWarnings("unchecked")
195+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
196+
197+
for (int i = 0; i < len; i++) {
198+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i], in5[i], in6[i], in7[i], in8[i]);
199+
}
200+
return out;
201+
};
202+
};
203+
204+
@OpField(names = "adapt", params = "fromOp, toOp")
205+
public final Function<Functions.Arity9<I1, I2, I3, I4, I5, I6, I7, I8, I9, O>, Functions.Arity9<I1[], I2[], I3[], I4[], I5[], I6[], I7[], I8[], I9[], O[]>> liftFunction9 =
206+
(function) -> {
207+
return (in1, in2, in3, in4, in5, in6, in7, in8, in9) -> {
208+
int len = minLength(in1, in2, in3, in4, in5, in6, in7, in8, in9);
209+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
210+
O component = function.apply(in1[0], in2[0], in3[0], in4[0], in5[0], in6[0], in7[0], in8[0], in9[0]);
211+
@SuppressWarnings("unchecked")
212+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
213+
214+
for (int i = 0; i < len; i++) {
215+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i], in5[i], in6[i], in7[i], in8[i], in9[i]);
216+
}
217+
return out;
218+
};
219+
};
220+
221+
@OpField(names = "adapt", params = "fromOp, toOp")
222+
public final Function<Functions.Arity10<I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, O>, Functions.Arity10<I1[], I2[], I3[], I4[], I5[], I6[], I7[], I8[], I9[], I10[], O[]>> liftFunction10 =
223+
(function) -> {
224+
return (in1, in2, in3, in4, in5, in6, in7, in8, in9, in10) -> {
225+
int len = minLength(in1, in2, in3, in4, in5, in6, in7, in8, in9, in10);
226+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
227+
O component = function.apply(in1[0], in2[0], in3[0], in4[0], in5[0], in6[0], in7[0], in8[0], in9[0], in10[0]);
228+
@SuppressWarnings("unchecked")
229+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
230+
231+
for (int i = 0; i < len; i++) {
232+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i], in5[i], in6[i], in7[i], in8[i], in9[i], in10[i]);
233+
}
234+
return out;
235+
};
236+
};
237+
238+
@OpField(names = "adapt", params = "fromOp, toOp")
239+
public final Function<Functions.Arity11<I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, O>, Functions.Arity11<I1[], I2[], I3[], I4[], I5[], I6[], I7[], I8[], I9[], I10[], I11[], O[]>> liftFunction11 =
240+
(function) -> {
241+
return (in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11) -> {
242+
int len = minLength(in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11);
243+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
244+
O component = function.apply(in1[0], in2[0], in3[0], in4[0], in5[0], in6[0], in7[0], in8[0], in9[0], in10[0], in11[0]);
245+
@SuppressWarnings("unchecked")
246+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
247+
248+
for (int i = 0; i < len; i++) {
249+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i], in5[i], in6[i], in7[i], in8[i], in9[i], in10[i], in11[i]);
250+
}
251+
return out;
252+
};
253+
};
254+
255+
@OpField(names = "adapt", params = "fromOp, toOp")
256+
public final Function<Functions.Arity12<I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, O>, Functions.Arity12<I1[], I2[], I3[], I4[], I5[], I6[], I7[], I8[], I9[], I10[], I11[], I12[], O[]>> liftFunction12 =
257+
(function) -> {
258+
return (in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11, in12) -> {
259+
int len = minLength(in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11, in12);
260+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
261+
O component = function.apply(in1[0], in2[0], in3[0], in4[0], in5[0], in6[0], in7[0], in8[0], in9[0], in10[0], in11[0], in12[0]);
262+
@SuppressWarnings("unchecked")
263+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
264+
265+
for (int i = 0; i < len; i++) {
266+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i], in5[i], in6[i], in7[i], in8[i], in9[i], in10[i], in11[i], in12[i]);
267+
}
268+
return out;
269+
};
270+
};
271+
272+
@OpField(names = "adapt", params = "fromOp, toOp")
273+
public final Function<Functions.Arity13<I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, O>, Functions.Arity13<I1[], I2[], I3[], I4[], I5[], I6[], I7[], I8[], I9[], I10[], I11[], I12[], I13[], O[]>> liftFunction13 =
274+
(function) -> {
275+
return (in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11, in12, in13) -> {
276+
int len = minLength(in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11, in12, in13);
277+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
278+
O component = function.apply(in1[0], in2[0], in3[0], in4[0], in5[0], in6[0], in7[0], in8[0], in9[0], in10[0], in11[0], in12[0], in13[0]);
279+
@SuppressWarnings("unchecked")
280+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
281+
282+
for (int i = 0; i < len; i++) {
283+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i], in5[i], in6[i], in7[i], in8[i], in9[i], in10[i], in11[i], in12[i], in13[i]);
284+
}
285+
return out;
286+
};
287+
};
288+
289+
@OpField(names = "adapt", params = "fromOp, toOp")
290+
public final Function<Functions.Arity14<I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, O>, Functions.Arity14<I1[], I2[], I3[], I4[], I5[], I6[], I7[], I8[], I9[], I10[], I11[], I12[], I13[], I14[], O[]>> liftFunction14 =
291+
(function) -> {
292+
return (in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11, in12, in13, in14) -> {
293+
int len = minLength(in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11, in12, in13, in14);
294+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
295+
O component = function.apply(in1[0], in2[0], in3[0], in4[0], in5[0], in6[0], in7[0], in8[0], in9[0], in10[0], in11[0], in12[0], in13[0], in14[0]);
296+
@SuppressWarnings("unchecked")
297+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
298+
299+
for (int i = 0; i < len; i++) {
300+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i], in5[i], in6[i], in7[i], in8[i], in9[i], in10[i], in11[i], in12[i], in13[i], in14[i]);
301+
}
302+
return out;
303+
};
304+
};
305+
306+
@OpField(names = "adapt", params = "fromOp, toOp")
307+
public final Function<Functions.Arity15<I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15, O>, Functions.Arity15<I1[], I2[], I3[], I4[], I5[], I6[], I7[], I8[], I9[], I10[], I11[], I12[], I13[], I14[], I15[], O[]>> liftFunction15 =
308+
(function) -> {
309+
return (in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11, in12, in13, in14, in15) -> {
310+
int len = minLength(in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11, in12, in13, in14, in15);
311+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
312+
O component = function.apply(in1[0], in2[0], in3[0], in4[0], in5[0], in6[0], in7[0], in8[0], in9[0], in10[0], in11[0], in12[0], in13[0], in14[0], in15[0]);
313+
@SuppressWarnings("unchecked")
314+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
315+
316+
for (int i = 0; i < len; i++) {
317+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i], in5[i], in6[i], in7[i], in8[i], in9[i], in10[i], in11[i], in12[i], in13[i], in14[i], in15[i]);
318+
}
319+
return out;
320+
};
321+
};
322+
323+
@OpField(names = "adapt", params = "fromOp, toOp")
324+
public final Function<Functions.Arity16<I1, I2, I3, I4, I5, I6, I7, I8, I9, I10, I11, I12, I13, I14, I15, I16, O>, Functions.Arity16<I1[], I2[], I3[], I4[], I5[], I6[], I7[], I8[], I9[], I10[], I11[], I12[], I13[], I14[], I15[], I16[], O[]>> liftFunction16 =
325+
(function) -> {
326+
return (in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11, in12, in13, in14, in15, in16) -> {
327+
int len = minLength(in1, in2, in3, in4, in5, in6, in7, in8, in9, in10, in11, in12, in13, in14, in15, in16);
328+
if (len == 0) throw new UnsupportedOperationException("Unable to create an empty output array.");
329+
O component = function.apply(in1[0], in2[0], in3[0], in4[0], in5[0], in6[0], in7[0], in8[0], in9[0], in10[0], in11[0], in12[0], in13[0], in14[0], in15[0], in16[0]);
330+
@SuppressWarnings("unchecked")
331+
O[] out = (O[]) Array.newInstance(component.getClass(), len);
332+
333+
for (int i = 0; i < len; i++) {
334+
out[i] = function.apply(in1[i], in2[i], in3[i], in4[i], in5[i], in6[i], in7[i], in8[i], in9[i], in10[i], in11[i], in12[i], in13[i], in14[i], in15[i], in16[i]);
335+
}
336+
return out;
337+
};
338+
};
339+
340+
}

0 commit comments

Comments
 (0)