summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2024-01-21 19:18:55 +0100
committerHenning Baldersheim <balder@yahoo-inc.com>2024-01-21 19:25:32 +0100
commit718b455587a6e093abfdbe5a45dc40221abc90eb (patch)
tree60d09423de6db9a07e0180b1abf01fdfa7500ba3 /vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
parent8c80c15025e67cc8c31f390d4f6f800f7d3dc63a (diff)
- Add and use getAsDouble method returning a Double object. It behaves similar to Map.get(key).
null indicates no value present. Then you avoid 2 lookups to first check if a value is present, and then to fetch that value. This does wonders when it is backed by a map and hashCode/equals are relatively expensive.
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java11
1 files changed, 11 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
index 5d384e0329b..f26174d9576 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java
@@ -120,6 +120,17 @@ public abstract class IndexedTensor implements Tensor {
}
@Override
+ public Double getAsDouble(TensorAddress address) {
+ try {
+ long index = toValueIndex(address, dimensionSizes, type);
+ if (index < 0 || size() <= index) return null;
+ return get(index);
+ } catch (IllegalArgumentException e) {
+ return null;
+ }
+ }
+
+ @Override
public boolean has(TensorAddress address) {
try {
long index = toValueIndex(address, dimensionSizes, type);