Skip to content

Commit 56570d9

Browse files
committed
Add a formula function to reverse and array
1 parent 1213397 commit 56570d9

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package org.csstudio.apputil.formula.array;
2+
3+
import org.csstudio.apputil.formula.Formula;
4+
import org.epics.util.array.ListDouble;
5+
import org.epics.util.array.ListNumber;
6+
import org.epics.vtype.Alarm;
7+
import org.epics.vtype.Display;
8+
import org.epics.vtype.VNumberArray;
9+
import org.epics.vtype.VType;
10+
import org.phoebus.core.vtypes.VTypeHelper;
11+
12+
import java.util.Arrays;
13+
import java.util.List;
14+
import java.util.logging.Level;
15+
16+
/**
17+
* A formula function for reversing the given array.
18+
* <p>
19+
* The function allows for reversing the order of elements in an array, such that the first element
20+
* becomes the last, the second element becomes the second to last, and so on.
21+
* </p>
22+
*
23+
* <b>Arguments:</b>
24+
* <ul>
25+
* <li><b>array</b>: The input numeric array to reverse.</li>
26+
* </ul>
27+
*
28+
* <b>Example:</b>
29+
* <p>
30+
* If given an array [1, 2, 3, 4, 5], the result will be [5, 4, 3, 2, 1].
31+
* </p>
32+
*
33+
* @author Kunal Shroff
34+
*/
35+
public class ArrayReversalFunction extends BaseArrayFunction {
36+
37+
@Override
38+
public String getName() {
39+
return "arrayReverse";
40+
}
41+
42+
@Override
43+
public String getDescription() {
44+
return "Reverses the given numeric array so that the first element becomes the last and vice versa.";
45+
}
46+
47+
@Override
48+
public List<String> getArguments() {
49+
return List.of("array");
50+
}
51+
52+
/**
53+
* Reverses the elements of a numeric array.
54+
*
55+
* @param array The input numeric array (VNumberArray) to reverse.
56+
* @return A {@link VNumberArray} containing the reversed elements.
57+
*/
58+
protected VType getArrayData(final VNumberArray array) {
59+
return VNumberArray.of(reverse(array.getData()), Alarm.none(), array.getTime(), Display.none());
60+
}
61+
62+
private static ListDouble reverse(final ListNumber data) {
63+
return new ListDouble() {
64+
@Override
65+
public double getDouble(int index) {
66+
return data.getDouble(data.size() - 1 - index);
67+
}
68+
69+
@Override
70+
public int size() {
71+
return data.size();
72+
}
73+
};
74+
}
75+
76+
/**
77+
* Computes the function's result by reversing the provided array.
78+
*
79+
* @param args The arguments to the function:
80+
* - args[0]: The input array to reverse.
81+
* @return A {@link VType} containing the reversed array.
82+
* @throws Exception If an invalid number of arguments or incorrect argument types are provided.
83+
*/
84+
@Override
85+
public VType compute(final VType... args) throws Exception {
86+
if (args.length != 1) {
87+
throw new Exception("Function " + getName() +
88+
" requires 1 argument but received " + Arrays.toString(args));
89+
}
90+
if (!VTypeHelper.isNumericArray(args[0])) {
91+
Formula.logger.log(Level.WARNING, "Function " + getName() +
92+
" takes array but received " + Arrays.toString(args));
93+
return DEFAULT_NAN_DOUBLE_ARRAY;
94+
} else {
95+
return getArrayData((VNumberArray) args[0]);
96+
}
97+
}
98+
}

core/formula/src/main/resources/META-INF/services/org.csstudio.apputil.formula.spi.FormulaFunction

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ org.csstudio.apputil.formula.array.ArrayScalarDivisionFunction
5959
org.csstudio.apputil.formula.array.ArrayInverseScalarDivisionFunction
6060
org.csstudio.apputil.formula.array.ArrayOfFunction
6161
org.csstudio.apputil.formula.array.ArrayRangeOfFunction
62+
org.csstudio.apputil.formula.array.ArrayReversalFunction
6263
org.csstudio.apputil.formula.array.ArraySampleWithLTTBFunction
6364
org.csstudio.apputil.formula.array.ArraySampleWithStrideFunction
6465
org.csstudio.apputil.formula.array.ArrayStatsFunction

0 commit comments

Comments
 (0)