diff options
author | Jon Bratseth <bratseth@verizonmedia.com> | 2019-11-08 14:33:17 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@verizonmedia.com> | 2019-11-08 14:33:17 +0100 |
commit | df287b9364b8088192146df70f5f4814ff6c94c1 (patch) | |
tree | 8ee439c76b6640ce22f121790d10895dd8bdf30c /vespajlib/src/main/java/com/yahoo/tensor | |
parent | 5ccaab5e304bbedc2589610f12ea9fc83d1522ab (diff) |
Always serialize dynamic tensors in verbose form
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor')
3 files changed, 30 insertions, 4 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java b/vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java index c0d817459d0..d81c02fb75f 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/DimensionSizes.java @@ -18,6 +18,21 @@ public final class DimensionSizes { } /** + * Create sizes from a type containing bound indexed dimensions only. + * + * @throws IllegalStateException if the type contains dimensions which are not bound and indexed + */ + public static DimensionSizes of(TensorType type) { + Builder b = new Builder(type.rank()); + for (int i = 0; i < type.rank(); i++) { + if ( type.dimensions().get(i).type() != TensorType.Dimension.Type.indexedBound) + throw new IllegalArgumentException(type + " contains dimensions without a size"); + b.set(i, type.dimensions().get(i).size().get()); + } + return b.build(); + } + + /** * Returns the length of this in the nth dimension * * @throws IllegalArgumentException if the index is larger than the number of dimensions in this tensor minus one diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java index 15476567fb2..176ddfefc13 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java @@ -758,6 +758,15 @@ public abstract class IndexedTensor implements Tensor { protected final long[] indexes; + /** + * Create indexes from a type containing bound indexed dimensions only. + * + * @throws IllegalStateException if the type contains dimensions which are not bound and indexed + */ + public static Indexes of(TensorType type) { + return of(DimensionSizes.of(type)); + } + public static Indexes of(DimensionSizes sizes) { return of(sizes, sizes); } @@ -824,7 +833,7 @@ public abstract class IndexedTensor implements Tensor { } /** Returns the address of the current position of these indexes */ - private TensorAddress toAddress() { + public TensorAddress toAddress() { return TensorAddress.of(indexes); } diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/DynamicTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/DynamicTensor.java index 9ce2496c65b..1154f962a6f 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/DynamicTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/DynamicTensor.java @@ -129,14 +129,16 @@ public abstract class DynamicTensor extends PrimitiveTensorFunction { return "{" + cells.get(0) + "}"; } - StringBuilder b = new StringBuilder("["); + IndexedTensor.Indexes indexes = IndexedTensor.Indexes.of(type()); + StringBuilder b = new StringBuilder("{"); for (var cell : cells) { - b.append(cell); + indexes.next(); + b.append(indexes.toAddress().toString(type())).append(":").append(cell); b.append(","); } if (b.length() > 1) b.setLength(b.length() - 1); - b.append("]"); + b.append("}"); return b.toString(); } |