summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@verizonmedia.com>2020-01-03 11:01:02 +0100
committerJon Bratseth <bratseth@verizonmedia.com>2020-01-03 11:01:02 +0100
commitfcaf3de39b725ece9d57e3c764bf0fae36206d5d (patch)
tree4f424b7a35507da40263211a8e0aa476a6d3dc52 /vespajlib/src/main/java/com/yahoo/tensor/Tensor.java
parent869e9e83274e037cae548ac5eb3c72881e90859a (diff)
More tensor short forms in Tensor.toString()
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor/Tensor.java')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/Tensor.java23
1 files changed, 11 insertions, 12 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java b/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java
index cffd41905a1..b0b1c27962a 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/Tensor.java
@@ -31,6 +31,7 @@ import java.util.Set;
import java.util.function.DoubleBinaryOperator;
import java.util.function.DoubleUnaryOperator;
import java.util.function.Function;
+import java.util.stream.Collectors;
import static com.yahoo.text.Ascii7BitMatcher.charsAndNumbers;
@@ -319,23 +320,21 @@ public interface Tensor {
}
static String contentToString(Tensor tensor) {
- List<java.util.Map.Entry<TensorAddress, Double>> cellEntries = new ArrayList<>(tensor.cells().entrySet());
+ var cellEntries = new ArrayList<>(tensor.cells().entrySet());
if (tensor.type().dimensions().isEmpty()) {
if (cellEntries.isEmpty()) return "{}";
return "{" + cellEntries.get(0).getValue() +"}";
}
+ return "{" + cellEntries.stream().sorted(Map.Entry.comparingByKey())
+ .map(cell -> cellToString(cell, tensor.type()))
+ .collect(Collectors.joining(",")) +
+ "}";
+ }
- Collections.sort(cellEntries, java.util.Map.Entry.<TensorAddress, Double>comparingByKey());
-
- StringBuilder b = new StringBuilder("{");
- for (java.util.Map.Entry<TensorAddress, Double> cell : cellEntries) {
- b.append(cell.getKey().toString(tensor.type())).append(":").append(cell.getValue());
- b.append(",");
- }
- if (b.length() > 1)
- b.setLength(b.length() - 1);
- b.append("}");
- return b.toString();
+ private static String cellToString(Map.Entry<TensorAddress, Double> cell, TensorType type) {
+ return (type.rank() > 1 ? cell.getKey().toString(type) : TensorAddress.labelToString(cell.getKey().label(0))) +
+ ":" +
+ cell.getValue();
}
// ----------------- equality