diff options
author | Jon Bratseth <bratseth@yahoo-inc.com> | 2017-01-10 16:30:02 +0100 |
---|---|---|
committer | Jon Bratseth <bratseth@yahoo-inc.com> | 2017-01-10 16:30:02 +0100 |
commit | 9574c0894ddbf88ad3ff311688e597a939df1a41 (patch) | |
tree | 67b3e2907c4451c50569ad573c96f00351376c79 /vespajlib/src/main/java/com/yahoo/tensor | |
parent | 451e7cf03729b7a09c8e4f9457edf9ae1007ba8a (diff) |
Disallow empty tensors without dimensions
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java | 24 |
1 files changed, 7 insertions, 17 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java index deee4aa02b6..bee93ddb4e0 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java @@ -220,7 +220,7 @@ public class IndexedTensor implements Tensor { public TensorType type() { return type; } @Override - public abstract Tensor build(); + public abstract IndexedTensor build(); } @@ -247,7 +247,6 @@ public class IndexedTensor implements Tensor { throw new IllegalArgumentException("Must have a dimension size entry for each dimension in " + type); this.sizes = sizes; values = new double[sizes.totalSize()]; - Arrays.fill(values, Double.NaN); } @Override @@ -268,14 +267,7 @@ public class IndexedTensor implements Tensor { } @Override - public Tensor build() { - // Note that we do not check for no NaN's here for performance reasons. - // NaN's don't get lost so leaving them in place should be quite benign - - // An empty tensor with no dimensions is mapped - if (values.length == 1 && Double.isNaN(values[0]) && type.dimensions().isEmpty()) - return MappedTensor.Builder.of(type).build(); - + public IndexedTensor build() { IndexedTensor tensor = new IndexedTensor(type, sizes, values); // prevent further modification sizes = null; @@ -318,23 +310,21 @@ public class IndexedTensor implements Tensor { } @Override - public Tensor build() { - if (firstDimension == null && type.dimensions().isEmpty()) // empty - return MappedTensor.Builder.of(type).build(); + public IndexedTensor build() { + if (firstDimension == null) throw new IllegalArgumentException("Tensor of type " + type() + " has no values"); + if (type.dimensions().isEmpty()) // single number return new IndexedTensor(type, new DimensionSizes.Builder(type.dimensions().size()).build(), new double[] {(Double) firstDimension.get(0) }); DimensionSizes dimensionSizes = findDimensionSizes(firstDimension); double[] values = new double[dimensionSizes.totalSize()]; - if (firstDimension != null) - fillValues(0, 0, firstDimension, dimensionSizes, values); + fillValues(0, 0, firstDimension, dimensionSizes, values); return new IndexedTensor(type, dimensionSizes, values); } private DimensionSizes findDimensionSizes(List<Object> firstDimension) { List<Integer> dimensionSizeList = new ArrayList<>(type.dimensions().size()); - if (firstDimension != null) - findDimensionSizes(0, dimensionSizeList, firstDimension); + findDimensionSizes(0, dimensionSizeList, firstDimension); DimensionSizes.Builder b = new DimensionSizes.Builder(type.dimensions().size()); // may be longer than the list but that's correct for (int i = 0; i < b.dimensions(); i++) { if (i < dimensionSizeList.size()) |