aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-12-05 09:16:33 -0800
committerJon Bratseth <bratseth@yahoo-inc.com>2016-12-05 09:16:33 -0800
commit8fc656b3491b084fe6459ab3a2decb91715511e6 (patch)
tree859a5ea7e5bd7272e604b7b8bf5b55152bdd29b7 /vespajlib
parent5b454c9ce2674a5e8e39c98b57de446961a924df (diff)
Move Builder below MapTensor
Diffstat (limited to 'vespajlib')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/MapTensor.java53
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/MapTensorBuilder.java60
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/serialization/SparseBinaryFormat.java10
-rw-r--r--vespajlib/src/test/java/com/yahoo/tensor/MapTensorBuilderTestCase.java6
-rw-r--r--vespajlib/src/test/java/com/yahoo/tensor/TensorFunctionBenchmark.java4
5 files changed, 58 insertions, 75 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/MapTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/MapTensor.java
index 436b31e7dae..2d4a593836d 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/MapTensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/MapTensor.java
@@ -3,13 +3,8 @@ package com.yahoo.tensor;
import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.ImmutableSet;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
import java.util.Map;
-import java.util.Optional;
/**
* A sparse implementation of a tensor backed by a Map of cells to values.
@@ -163,4 +158,52 @@ public class MapTensor implements Tensor {
return Tensor.equals(this, (Tensor)o);
}
+ /**
+ * Builder class for a MapTensor.
+ *
+ * The set of dimensions of the resulting tensor is the union of
+ * the dimensions specified explicitly and the ones specified in the
+ * tensor cell addresses.
+ *
+ * @author geirst
+ */
+ @Beta
+ public static class Builder {
+
+ private final TensorType type;
+ private final ImmutableMap.Builder<TensorAddress, Double> cells = new ImmutableMap.Builder<>();
+
+ public Builder(TensorType type) {
+ this.type = type;
+ }
+
+ public CellBuilder cell() {
+ return new CellBuilder(type);
+ }
+
+ public Tensor build() {
+ return new MapTensor(type, cells.build());
+ }
+
+ public class CellBuilder {
+
+ private final TensorAddress.Builder addressBuilder;
+
+ private CellBuilder(TensorType type) {
+ addressBuilder = new TensorAddress.Builder(type);
+ }
+
+ public CellBuilder label(String dimension, String label) {
+ addressBuilder.add(dimension, label);
+ return this;
+ }
+
+ public Builder value(double cellValue) {
+ cells.put(addressBuilder.build(), cellValue);
+ return Builder.this;
+ }
+
+ }
+
+ }
}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/MapTensorBuilder.java b/vespajlib/src/main/java/com/yahoo/tensor/MapTensorBuilder.java
deleted file mode 100644
index 0cca1454ad3..00000000000
--- a/vespajlib/src/main/java/com/yahoo/tensor/MapTensorBuilder.java
+++ /dev/null
@@ -1,60 +0,0 @@
-// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
-package com.yahoo.tensor;
-
-import com.google.common.annotations.Beta;
-import com.google.common.collect.ImmutableMap;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Set;
-
-/**
- * Builder class for a MapTensor.
- *
- * The set of dimensions of the resulting tensor is the union of
- * the dimensions specified explicitly and the ones specified in the
- * tensor cell addresses.
- *
- * @author geirst
- */
-// TODO: Move below MapTensor
-@Beta
-public class MapTensorBuilder {
-
- private final TensorType type;
- private final ImmutableMap.Builder<TensorAddress, Double> cells = new ImmutableMap.Builder<>();
-
- public MapTensorBuilder(TensorType type) {
- this.type = type;
- }
-
- public CellBuilder cell() {
- return new CellBuilder(type);
- }
-
- public Tensor build() {
- return new MapTensor(type, cells.build());
- }
-
- public class CellBuilder {
-
- private final TensorAddress.Builder addressBuilder;
-
- private CellBuilder(TensorType type) {
- addressBuilder = new TensorAddress.Builder(type);
- }
-
- public CellBuilder label(String dimension, String label) {
- addressBuilder.add(dimension, label);
- return this;
- }
-
- public MapTensorBuilder value(double cellValue) {
- cells.put(addressBuilder.build(), cellValue);
- return MapTensorBuilder.this;
- }
-
- }
-
-}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/serialization/SparseBinaryFormat.java b/vespajlib/src/main/java/com/yahoo/tensor/serialization/SparseBinaryFormat.java
index b707802c55f..dcb82d1ced5 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/serialization/SparseBinaryFormat.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/serialization/SparseBinaryFormat.java
@@ -3,7 +3,7 @@ package com.yahoo.tensor.serialization;
import com.google.common.annotations.Beta;
import com.yahoo.io.GrowableByteBuffer;
-import com.yahoo.tensor.MapTensorBuilder;
+import com.yahoo.tensor.MapTensor;
import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorAddress;
import com.yahoo.tensor.TensorType;
@@ -62,7 +62,7 @@ class SparseBinaryFormat implements BinaryFormat {
@Override
public Tensor decode(GrowableByteBuffer buffer) {
TensorType type = decodeDimensions(buffer);
- MapTensorBuilder builder = new MapTensorBuilder(type);
+ MapTensor.Builder builder = new MapTensor.Builder(type);
decodeCells(buffer, builder, type);
return builder.build();
}
@@ -76,16 +76,16 @@ class SparseBinaryFormat implements BinaryFormat {
return builder.build();
}
- private static void decodeCells(GrowableByteBuffer buffer, MapTensorBuilder builder, TensorType type) {
+ private static void decodeCells(GrowableByteBuffer buffer, MapTensor.Builder builder, TensorType type) {
int numCells = buffer.getInt1_4Bytes();
for (int i = 0; i < numCells; ++i) {
- MapTensorBuilder.CellBuilder cellBuilder = builder.cell();
+ MapTensor.Builder.CellBuilder cellBuilder = builder.cell();
decodeAddress(buffer, cellBuilder, type);
cellBuilder.value(buffer.getDouble());
}
}
- private static void decodeAddress(GrowableByteBuffer buffer, MapTensorBuilder.CellBuilder builder,
+ private static void decodeAddress(GrowableByteBuffer buffer, MapTensor.Builder.CellBuilder builder,
TensorType type) {
for (TensorType.Dimension dimension : type.dimensions()) {
String label = decodeString(buffer);
diff --git a/vespajlib/src/test/java/com/yahoo/tensor/MapTensorBuilderTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/MapTensorBuilderTestCase.java
index ee1cf7cc144..3704f54acec 100644
--- a/vespajlib/src/test/java/com/yahoo/tensor/MapTensorBuilderTestCase.java
+++ b/vespajlib/src/test/java/com/yahoo/tensor/MapTensorBuilderTestCase.java
@@ -14,7 +14,7 @@ public class MapTensorBuilderTestCase {
@Test
public void requireThatEmptyTensorCanBeBuilt() {
- Tensor tensor = new MapTensorBuilder(TensorType.empty).build();
+ Tensor tensor = new MapTensor.Builder(TensorType.empty).build();
assertEquals(0, tensor.type().dimensions().size());
assertEquals("{}", tensor.toString());
}
@@ -22,7 +22,7 @@ public class MapTensorBuilderTestCase {
@Test
public void requireThatOneDimensionalTensorCanBeBuilt() {
TensorType type = new TensorType.Builder().mapped("x").build();
- Tensor tensor = new MapTensorBuilder(type).
+ Tensor tensor = new MapTensor.Builder(type).
cell().label("x", "0").value(1).
cell().label("x", "1").value(2).build();
assertEquals(Sets.newHashSet("x"), tensor.type().dimensionNames());
@@ -32,7 +32,7 @@ public class MapTensorBuilderTestCase {
@Test
public void requireThatTwoDimensionalTensorCanBeBuilt() {
TensorType type = new TensorType.Builder().mapped("x").mapped("y").build();
- Tensor tensor = new MapTensorBuilder(type).
+ Tensor tensor = new MapTensor.Builder(type).
cell().label("x", "0").label("y", "0").value(1).
cell().label("x", "1").label("y", "0").value(2).build();
assertEquals(Sets.newHashSet("x", "y"), tensor.type().dimensionNames());
diff --git a/vespajlib/src/test/java/com/yahoo/tensor/TensorFunctionBenchmark.java b/vespajlib/src/test/java/com/yahoo/tensor/TensorFunctionBenchmark.java
index 33ad01b49c5..7b1c07562c6 100644
--- a/vespajlib/src/test/java/com/yahoo/tensor/TensorFunctionBenchmark.java
+++ b/vespajlib/src/test/java/com/yahoo/tensor/TensorFunctionBenchmark.java
@@ -46,7 +46,7 @@ public class TensorFunctionBenchmark {
List<Tensor> tensors = new ArrayList<>();
TensorType type = new TensorType.Builder().mapped("x").build();
for (int i = 0; i < vectorCount; i++) {
- MapTensorBuilder builder = new MapTensorBuilder(type);
+ MapTensor.Builder builder = new MapTensor.Builder(type);
for (int j = 0; j < vectorSize; j++) {
builder.cell().label("x", String.valueOf(j)).value(random.nextDouble());
}
@@ -58,7 +58,7 @@ public class TensorFunctionBenchmark {
private static List<Tensor> generateVectorsInOneTensor(int vectorCount, int vectorSize) {
List<Tensor> tensors = new ArrayList<>();
TensorType type = new TensorType.Builder().mapped("i").mapped("x").build();
- MapTensorBuilder builder = new MapTensorBuilder(type);
+ MapTensor.Builder builder = new MapTensor.Builder(type);
for (int i = 0; i < vectorCount; i++) {
for (int j = 0; j < vectorSize; j++) {
builder.cell()