Skip to content

Commit 59947fe

Browse files
authored
Add new formula function arrayIndex (#3333)
1 parent 6fd76fa commit 59947fe

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

core/formula/doc/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ element is defined as array[x] + offset. To subtract, use negative offset.
132132
or VString at the specified index of the input array. If the index is invalid, a NaN or
133133
empty string is returned.
134134

135+
**arrayIndex([VNumberArray | VStringArray] array, [VNumber | VString] value)** - Returns the index of the first
136+
occurrence of the specified value in the input array. If the value is not found, -1 is returned.
137+
135138
**histogramOf(VNumberArray array [, VNumber binCount])** - Computes a histogram for the input array. The binCount
136139
argument is optional and defaults to 100.
137140

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.csstudio.apputil.formula.array;
2+
3+
import org.epics.util.array.ListNumber;
4+
import org.epics.vtype.VNumber;
5+
import org.epics.vtype.VNumberArray;
6+
import org.epics.vtype.VString;
7+
import org.epics.vtype.VStringArray;
8+
import org.epics.vtype.VType;
9+
import org.epics.vtype.VInt;
10+
import org.epics.vtype.Alarm;
11+
import org.epics.vtype.Time;
12+
import org.epics.vtype.Display;
13+
import org.phoebus.core.vtypes.VTypeHelper;
14+
15+
import java.util.List;
16+
17+
/**
18+
* Get index of the specified element in the array.
19+
*/
20+
public class ArrayIndexFunction extends BaseArrayFunction {
21+
22+
@Override
23+
public String getName() {
24+
return "arrayIndex";
25+
}
26+
27+
@Override
28+
public String getDescription() {
29+
return "Returns the index of the specified element in the array, -1 if not found.";
30+
}
31+
32+
@Override
33+
public List<String> getArguments() {
34+
return List.of("<String | Number> array", "element");
35+
}
36+
37+
/**
38+
* Get index of the specified element in the array.
39+
* @param args Arguments, count will match <code>getArgumentCount()</code>
40+
* @return A {@link VInt} with the index of the element in the array, -1 if not found. If the arguments are not of
41+
* the supported types, {@link BaseArrayFunction#DEFAULT_NAN_DOUBLE} or {@link BaseArrayFunction#DEFAULT_EMPTY_STRING}
42+
*/
43+
@Override
44+
public VType compute(VType... args) {
45+
boolean isStringArray = args[0] instanceof VStringArray;
46+
if (isStringArray && args[1] instanceof VString) {
47+
VStringArray stringArray = (VStringArray) args[0];
48+
VString element = (VString) args[1];
49+
List<String> data = stringArray.getData();
50+
51+
int idx = data.indexOf(element.getValue());
52+
return VInt.of(idx, Alarm.none(), Time.now(), Display.none());
53+
}
54+
else if (VTypeHelper.isNumericArray(args[0]) && args[1] instanceof VNumber) {
55+
VNumberArray numberArray = (VNumberArray) args[0];
56+
ListNumber data = numberArray.getData();
57+
VNumber element = (VNumber) args[1];
58+
double elementValue = element.getValue().doubleValue();
59+
60+
for (int i = 0; i < data.size(); i++) {
61+
if (data.getDouble(i) == elementValue) {
62+
return VInt.of(i, Alarm.none(), Time.now(), Display.none());
63+
}
64+
}
65+
// return -1 if not found
66+
return VInt.of(-1, Alarm.none(), Time.now(), Display.none());
67+
}
68+
else {
69+
return isStringArray ? DEFAULT_EMPTY_STRING : DEFAULT_NAN_DOUBLE;
70+
}
71+
}
72+
}

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
@@ -63,6 +63,7 @@ org.csstudio.apputil.formula.array.ArraySampleWithStrideFunction
6363
org.csstudio.apputil.formula.array.ArrayStatsFunction
6464
org.csstudio.apputil.formula.array.ArrayMaxFunction
6565
org.csstudio.apputil.formula.array.ArrayMinFunction
66+
org.csstudio.apputil.formula.array.ArrayIndexFunction
6667
org.csstudio.apputil.formula.array.ArrayCumSumFunction
6768

6869
# Bitwise Operation

0 commit comments

Comments
 (0)