summaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@gmail.com>2022-05-18 14:37:17 +0200
committerJon Bratseth <bratseth@gmail.com>2022-05-18 14:37:17 +0200
commit4f8fa003117d3ad1f30b6dc0adcabf4e923b0f90 (patch)
treece780f3bac6be9f7a70b63f1853a96b2b9c322b8 /vespajlib
parent2c9cb8159adaced573fd1e34689e533946feea85 (diff)
Revert "Merge pull request #22642 from vespa-engine/revert-22637-bratseth/model-syntax"
This reverts commit 2c9cb8159adaced573fd1e34689e533946feea85, reversing changes made to a4dbfc43c7df534ee5b032204ef19a7b038d7e3e.
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/abi-spec.json12
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/TensorAddress.java49
2 files changed, 51 insertions, 10 deletions
diff --git a/vespajlib/abi-spec.json b/vespajlib/abi-spec.json
index f7be61946ba..654042372cf 100644
--- a/vespajlib/abi-spec.json
+++ b/vespajlib/abi-spec.json
@@ -1273,6 +1273,18 @@
],
"fields": []
},
+ "com.yahoo.tensor.TensorAddress$PartialBuilder": {
+ "superClass": "com.yahoo.tensor.TensorAddress$Builder",
+ "interfaces": [],
+ "attributes": [
+ "public"
+ ],
+ "methods": [
+ "public void <init>(com.yahoo.tensor.TensorType)",
+ "public com.yahoo.tensor.TensorAddress$Builder copy()"
+ ],
+ "fields": []
+ },
"com.yahoo.tensor.TensorAddress": {
"superClass": "java.lang.Object",
"interfaces": [
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/TensorAddress.java b/vespajlib/src/main/java/com/yahoo/tensor/TensorAddress.java
index d9ab67d6c5f..92bdfb2b3a4 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/TensorAddress.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/TensorAddress.java
@@ -61,8 +61,10 @@ public abstract class TensorAddress implements Comparable<TensorAddress> {
@Override
public int hashCode() {
int result = 1;
- for (int i = 0; i < size(); i++)
- result = 31 * result + label(i).hashCode();
+ for (int i = 0; i < size(); i++) {
+ if (label(i) != null)
+ result = 31 * result + label(i).hashCode();
+ }
return result;
}
@@ -73,7 +75,7 @@ public abstract class TensorAddress implements Comparable<TensorAddress> {
TensorAddress other = (TensorAddress)o;
if (other.size() != this.size()) return false;
for (int i = 0; i < this.size(); i++)
- if ( ! this.label(i).equals(other.label(i)))
+ if ( ! Objects.equals(this.label(i), other.label(i)))
return false;
return true;
}
@@ -171,8 +173,8 @@ public abstract class TensorAddress implements Comparable<TensorAddress> {
/** Builder of a tensor address */
public static class Builder {
- private final TensorType type;
- private final String[] labels;
+ final TensorType type;
+ final String[] labels;
public Builder(TensorType type) {
this(type, new String[type.dimensions().size()]);
@@ -184,14 +186,16 @@ public abstract class TensorAddress implements Comparable<TensorAddress> {
}
/**
- * Adds the label to the only dimension of this.
+ * Adds the label to the only mapped dimension of this.
*
* @throws IllegalArgumentException if this does not have exactly one dimension
*/
public Builder add(String label) {
- if (type.rank() != 1)
- throw new IllegalArgumentException("Cannot add a label without explicit dimension to a tensor of type " + type);
- add(type.dimensions().get(0).name(), label);
+ var mappedSubtype = type.mappedSubtype();
+ if (mappedSubtype.rank() != 1)
+ throw new IllegalArgumentException("Cannot add a label without explicit dimension to a tensor of type " +
+ type + ": Must have exactly one sparse dimension");
+ add(mappedSubtype.dimensions().get(0).name(), label);
return this;
}
@@ -218,14 +222,39 @@ public abstract class TensorAddress implements Comparable<TensorAddress> {
/** Returns the type of the tensor this address is being built for. */
public TensorType type() { return type; }
- public TensorAddress build() {
+ void validate() {
for (int i = 0; i < labels.length; i++)
if (labels[i] == null)
throw new IllegalArgumentException("Missing a label for dimension " +
type.dimensions().get(i).name() + " for " + type);
+ }
+
+ public TensorAddress build() {
+ validate();
return TensorAddress.of(labels);
}
}
+ /** Builder of an address to a subset of the dimensions of a tensor type */
+ public static class PartialBuilder extends Builder {
+
+ public PartialBuilder(TensorType type) {
+ super(type);
+ }
+
+ private PartialBuilder(TensorType type, String[] labels) {
+ super(type, labels);
+ }
+
+ /** Creates a copy of this which can be modified separately */
+ public Builder copy() {
+ return new PartialBuilder(type, Arrays.copyOf(labels, labels.length));
+ }
+
+ @Override
+ void validate() { }
+
+ }
+
}