summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@verizonmedia.com>2019-04-25 16:40:00 +0200
committerJon Bratseth <bratseth@verizonmedia.com>2019-04-25 16:40:00 +0200
commit7ef86b1fb25f2268d00fa3af87bc1e594de0b1b3 (patch)
tree5af4bc2b63e291b7e80d2ffc3ea85b5dfdf2b044 /vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java
parenta8949c869c613d671886b87ab684b2dfef9d9ca5 (diff)
Split values into IndexedDoubleTensor subclass
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java46
1 files changed, 46 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java
new file mode 100644
index 00000000000..27cecdab80c
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java
@@ -0,0 +1,46 @@
+// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.tensor;
+
+import java.util.Arrays;
+
+/**
+ * An indexed tensor implementation holding values as doubles
+ *
+ * @author bratseth
+ */
+class IndexedDoubleTensor extends IndexedTensor {
+
+ private final double[] values;
+
+ IndexedDoubleTensor(TensorType type, DimensionSizes dimensionSizes, double[] values) {
+ super(type, dimensionSizes);
+ this.values = values;
+ }
+
+ @Override
+ public long size() {
+ return values.length;
+ }
+
+ /**
+ * Returns the value at the given index by direct lookup. Only use
+ * if you know the underlying data layout.
+ *
+ * @param valueIndex the direct index into the underlying data.
+ * @throws IndexOutOfBoundsException if index is out of bounds
+ */
+ @Override
+ public double get(long valueIndex) { return values[(int)valueIndex]; }
+
+ @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");
+ return new IndexedDoubleTensor(type, dimensionSizes(), values);
+ }
+
+ @Override
+ public int hashCode() { return Arrays.hashCode(values); }
+
+}