aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/impl/TensorAddressAny2.java
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2024-01-24 01:33:20 +0100
committerHenning Baldersheim <balder@yahoo-inc.com>2024-01-25 20:17:49 +0100
commita1e14c645d88fecfab1abb0072e0abc26677e752 (patch)
tree9d22ff810f3694912005ddc455ca3425699b2fe7 /vespajlib/src/main/java/com/yahoo/tensor/impl/TensorAddressAny2.java
parentd9fb5104948ad6b8758e5a902af3fad0f9e506ce (diff)
Make tensor addresses integer based instead of as strings.
Positive numbers are direct indexes, while strings that does not represent numbers are enumerated and represented with negative integers.
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor/impl/TensorAddressAny2.java')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/impl/TensorAddressAny2.java49
1 files changed, 49 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/impl/TensorAddressAny2.java b/vespajlib/src/main/java/com/yahoo/tensor/impl/TensorAddressAny2.java
new file mode 100644
index 00000000000..d77a689852f
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/tensor/impl/TensorAddressAny2.java
@@ -0,0 +1,49 @@
+// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+
+package com.yahoo.tensor.impl;
+
+import com.yahoo.tensor.TensorAddress;
+
+import static java.lang.Math.abs;
+
+/**
+ * 2 dimensional address
+ * @author baldersheim
+ */
+final class TensorAddressAny2 extends TensorAddressAny {
+ private final int label0, label1;
+ TensorAddressAny2(int label0, int label1) {
+ this.label0 = label0;
+ this.label1 = label1;
+ }
+
+ @Override public int size() { return 2; }
+
+ @Override
+ public long numericLabel(int i) {
+ return switch (i) {
+ case 0 -> label0;
+ case 1 -> label1;
+ default -> throw new IndexOutOfBoundsException("Index is not in [0,1]: " + i);
+ };
+ }
+
+ @Override
+ public TensorAddress withLabel(int labelIndex, long label) {
+ return switch (labelIndex) {
+ case 0 -> new TensorAddressAny2(Convert.safe2Int(label), label1);
+ case 1 -> new TensorAddressAny2(label0, Convert.safe2Int(label));
+ default -> throw new IllegalArgumentException("No label " + labelIndex);
+ };
+ }
+
+ @Override
+ public int hashCode() {
+ return abs(label0) | (abs(label1) << 32 - Integer.numberOfLeadingZeros(abs(label0)));
+ }
+
+ @Override
+ public boolean equals(Object o) {
+ return (o instanceof TensorAddressAny2 any) && (label0 == any.label0) && (label1 == any.label1);
+ }
+}