summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@verizonmedia.com>2019-04-26 11:26:04 +0200
committerJon Bratseth <bratseth@verizonmedia.com>2019-04-26 11:26:04 +0200
commit3873424bb18acd179441cdd914070c32e41699ee (patch)
treebd843dc77a84a7a30f65405f17334dd3907defee /vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java
parent7ef86b1fb25f2268d00fa3af87bc1e594de0b1b3 (diff)
Move bound builder double array into double subclass
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java65
1 files changed, 62 insertions, 3 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java
index 27cecdab80c..80350d9e5f5 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java
@@ -34,13 +34,72 @@ class IndexedDoubleTensor extends IndexedTensor {
@Override
public IndexedTensor withType(TensorType type) {
- if ( ! this.type().isRenamableTo(type))
- throw new IllegalArgumentException("Can not change type from " + this.type() + " to " + type +
- ": Types are not compatible");
+ throwOnIncompatibleType(type);
return new IndexedDoubleTensor(type, dimensionSizes(), values);
}
@Override
public int hashCode() { return Arrays.hashCode(values); }
+ /** A bound builder can create the double array directly */
+ public static class BoundDoubleBuilder extends BoundBuilder {
+
+ private double[] values;
+
+ BoundDoubleBuilder(TensorType type) {
+ this(type, dimensionSizesOf(type));
+ }
+
+ BoundDoubleBuilder(TensorType type, DimensionSizes sizes) {
+ super(type, sizes);
+ values = new double[(int)sizes.totalSize()];
+ }
+
+ @Override
+ public IndexedTensor.BoundBuilder cell(double value, long ... indexes) {
+ values[(int)toValueIndex(indexes, sizes())] = value;
+ return this;
+ }
+
+ @Override
+ public CellBuilder cell() {
+ return new CellBuilder(type, this);
+ }
+
+ @Override
+ public Builder cell(TensorAddress address, double value) {
+ values[(int)toValueIndex(address, sizes())] = value;
+ return this;
+ }
+
+ @Override
+ public IndexedTensor build() {
+ IndexedTensor tensor = new IndexedDoubleTensor(type, sizes(), values);
+ // prevent further modification
+ values = null;
+ return tensor;
+ }
+
+ @Override
+ public Builder cell(Cell cell, double value) {
+ long directIndex = cell.getDirectIndex();
+ if (directIndex >= 0) // optimization
+ values[(int)directIndex] = value;
+ else
+ super.cell(cell, value);
+ return this;
+ }
+
+ /**
+ * Set a cell value by the index in the internal layout of this cell.
+ * This requires knowledge of the internal layout of cells in this implementation, and should therefore
+ * probably not be used (but when it can be used it is fast).
+ */
+ @Override
+ public void cellByDirectIndex(long index, double value) {
+ values[(int)index] = value;
+ }
+
+ }
+
}