From 7ef86b1fb25f2268d00fa3af87bc1e594de0b1b3 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Thu, 25 Apr 2019 16:40:00 +0200 Subject: Split values into IndexedDoubleTensor subclass --- .../java/com/yahoo/tensor/IndexedDoubleTensor.java | 46 ++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java (limited to 'vespajlib/src/main/java/com/yahoo/tensor/IndexedDoubleTensor.java') 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); } + +} -- cgit v1.2.3