aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2023-09-13 14:25:17 +0200
committerHenning Baldersheim <balder@yahoo-inc.com>2023-09-13 15:06:24 +0200
commit7050087f2ba90981ed80118361d229ea5181918f (patch)
treee14a1090924aade36312523d05ec8e466a89be13 /vespajlib/src
parentcc015e6d4601b9966ec2d092697a146a7fd2c2a3 (diff)
- Use equals when comparing Optional<Long>
- Minor cleanup
Diffstat (limited to 'vespajlib/src')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java56
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/TensorType.java29
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/CosineSimilarity.java1
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/EuclideanDistance.java1
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/functions/ReduceJoin.java1
5 files changed, 44 insertions, 44 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java b/vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java
index 0c78c2891d6..4e7aa4bd482 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/TensorParser.java
@@ -137,7 +137,7 @@ class TensorParser {
sz *= d.size().orElse(0L);
}
if (sz == 0
- || type.dimensions().size() == 0
+ || type.dimensions().isEmpty()
|| valueString.length() < sz * 2
|| valueString.chars().anyMatch(ch -> (Character.digit(ch, 16) == -1)))
{
@@ -253,14 +253,13 @@ class TensorParser {
try {
String cellValueString = string.substring(position, nextNumberEnd);
try {
- switch (cellValueType) {
- case DOUBLE: return Double.parseDouble(cellValueString);
- case FLOAT: return Float.parseFloat(cellValueString);
- case BFLOAT16: return Float.parseFloat(cellValueString);
- case INT8: return Float.parseFloat(cellValueString);
- default:
- throw new IllegalArgumentException(cellValueType + " is not supported");
- }
+ return switch (cellValueType) {
+ case DOUBLE -> Double.parseDouble(cellValueString);
+ case FLOAT -> Float.parseFloat(cellValueString);
+ case BFLOAT16 -> Float.parseFloat(cellValueString);
+ case INT8 -> Float.parseFloat(cellValueString);
+ default -> throw new IllegalArgumentException(cellValueType + " is not supported");
+ };
} catch (NumberFormatException e) {
throw new IllegalArgumentException("At value position " + position + ": '" +
cellValueString + "' is not a valid " + cellValueType);
@@ -346,10 +345,10 @@ class TensorParser {
protected void consumeNumber() {
Number number = consumeNumber(builder.type().valueType());
switch (builder.type().valueType()) {
- case DOUBLE: builder.cellByDirectIndex(indexes.toSourceValueIndex(), (Double)number); break;
- case FLOAT: builder.cellByDirectIndex(indexes.toSourceValueIndex(), (Float)number); break;
- case BFLOAT16: builder.cellByDirectIndex(indexes.toSourceValueIndex(), (Float)number); break;
- case INT8: builder.cellByDirectIndex(indexes.toSourceValueIndex(), (Float)number); break;
+ case DOUBLE -> builder.cellByDirectIndex(indexes.toSourceValueIndex(), (Double) number);
+ case FLOAT -> builder.cellByDirectIndex(indexes.toSourceValueIndex(), (Float) number);
+ case BFLOAT16 -> builder.cellByDirectIndex(indexes.toSourceValueIndex(), (Float) number);
+ case INT8 -> builder.cellByDirectIndex(indexes.toSourceValueIndex(), (Float) number);
}
}
}
@@ -390,10 +389,10 @@ class TensorParser {
private void consumeNumber() {
Number number = consumeNumber(builder.type().valueType());
switch (builder.type().valueType()) {
- case DOUBLE: builder.cell((Double)number, indexes); break;
- case FLOAT: builder.cell((Float)number, indexes); break;
- case BFLOAT16: builder.cell((Float)number, indexes); break;
- case INT8: builder.cell((Float)number, indexes); break;
+ case DOUBLE -> builder.cell((Double) number, indexes);
+ case FLOAT -> builder.cell((Float) number, indexes);
+ case BFLOAT16 -> builder.cell((Float) number, indexes);
+ case INT8 -> builder.cell((Float) number, indexes);
}
}
@@ -418,7 +417,7 @@ class TensorParser {
private static class MixedValueParser extends ValueParser {
private final Tensor.Builder builder;
- private List<String> dimensionOrder;
+ private final List<String> dimensionOrder;
public MixedValueParser(String string, List<String> dimensionOrder, Tensor.Builder builder) {
super(string);
@@ -450,7 +449,7 @@ class TensorParser {
}
private TensorType.Dimension findMappedDimension() {
- Optional<TensorType.Dimension> mappedDimension = builder.type().dimensions().stream().filter(d -> d.isMapped()).findAny();
+ Optional<TensorType.Dimension> mappedDimension = builder.type().dimensions().stream().filter(TensorType.Dimension::isMapped).findAny();
if (mappedDimension.isPresent()) return mappedDimension.get();
if (builder.type().rank() == 1 && builder.type().dimensions().get(0).size().isEmpty())
return builder.type().dimensions().get(0);
@@ -469,10 +468,10 @@ class TensorParser {
private void consumeNumber(TensorAddress address) {
Number number = consumeNumber(builder.type().valueType());
switch (builder.type().valueType()) {
- case DOUBLE: builder.cell(address, (Double)number); break;
- case FLOAT: builder.cell(address, (Float)number); break;
- case BFLOAT16: builder.cell(address, (Float)number); break;
- case INT8: builder.cell(address, (Float)number); break;
+ case DOUBLE -> builder.cell(address, (Double) number);
+ case FLOAT -> builder.cell(address, (Float) number);
+ case BFLOAT16 -> builder.cell(address, (Float) number);
+ case INT8 -> builder.cell(address, (Float) number);
}
}
}
@@ -507,12 +506,11 @@ class TensorParser {
String cellValueString = string.substring(position, valueEnd).trim();
try {
switch (cellValueType) {
- case DOUBLE: builder.cell(address, Double.parseDouble(cellValueString)); break;
- case FLOAT: builder.cell(address, Float.parseFloat(cellValueString)); break;
- case BFLOAT16: builder.cell(address, Float.parseFloat(cellValueString)); break;
- case INT8: builder.cell(address, Float.parseFloat(cellValueString)); break;
- default:
- throw new IllegalArgumentException(cellValueType + " is not supported");
+ case DOUBLE -> builder.cell(address, Double.parseDouble(cellValueString));
+ case FLOAT -> builder.cell(address, Float.parseFloat(cellValueString));
+ case BFLOAT16 -> builder.cell(address, Float.parseFloat(cellValueString));
+ case INT8 -> builder.cell(address, Float.parseFloat(cellValueString));
+ default -> throw new IllegalArgumentException(cellValueType + " is not supported");
}
}
catch (NumberFormatException e) {
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java b/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java
index 6b010529046..f702dba6739 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/TensorType.java
@@ -96,11 +96,11 @@ public class TensorType {
Collections.sort(dimensionList);
this.dimensions = List.copyOf(dimensionList);
- if (dimensionList.stream().allMatch(d -> d.isIndexed())) {
+ if (dimensionList.stream().allMatch(Dimension::isIndexed)) {
mappedSubtype = empty;
indexedSubtype = this;
}
- else if (dimensionList.stream().noneMatch(d -> d.isIndexed())) {
+ else if (dimensionList.stream().noneMatch(Dimension::isIndexed)) {
mappedSubtype = this;
indexedSubtype = empty;
}
@@ -158,7 +158,7 @@ public class TensorType {
/** Returns the dimension with this name, or empty if not present */
public Optional<Dimension> dimension(String name) {
- return indexOfDimension(name).map(i -> dimensions.get(i));
+ return indexOfDimension(name).map(dimensions::get);
}
/** Returns the 0-base index of this dimension, or empty if it is not present */
@@ -172,7 +172,7 @@ public class TensorType {
/* Returns the bound of this dimension if it is present and bound in this, empty otherwise */
public Optional<Long> sizeOfDimension(String dimension) {
Optional<Dimension> d = dimension(dimension);
- if ( ! d.isPresent()) return Optional.empty();
+ if (d.isEmpty()) return Optional.empty();
return d.get().size();
}
@@ -213,12 +213,12 @@ public class TensorType {
if (thisDimension.isIndexed() != generalizationDimension.isIndexed()) return false;
if (considerName && ! thisDimension.name().equals(generalizationDimension.name())) return false;
if (generalizationDimension.size().isPresent()) {
- if ( ! thisDimension.size().isPresent()) return false;
+ if (thisDimension.size().isEmpty()) return false;
if (convertible) {
if (thisDimension.size().get() > generalizationDimension.size().get()) return false;
}
else { // assignable
- if (!thisDimension.size().get().equals(generalizationDimension.size().get())) return false;
+ if (!thisDimension.size().equals(generalizationDimension.size())) return false;
}
}
}
@@ -269,7 +269,7 @@ public class TensorType {
if ( ! thisDim.name().equals(otherDim.name())) return Optional.empty();
if (thisDim.isIndexed() && otherDim.isIndexed()) {
if (thisDim.size().isPresent() && otherDim.size().isPresent()) {
- if ( ! thisDim.size().get().equals(otherDim.size().get()))
+ if ( ! thisDim.size().equals(otherDim.size()))
return Optional.empty();
b.dimension(thisDim); // both are equal and bound
}
@@ -314,7 +314,12 @@ public class TensorType {
public final String name() { return name; }
- /** Returns the size of this dimension if it is bound, empty otherwise */
+ /**
+ * Returns the size of this dimension if it is bound, empty otherwise
+ * Beware not use == != when comparing size. Use equals
+ */
+ // TODO Optional<Long> => OptionalLong to avoid mistakes when comparing values
+ // Deprecate if we find an alternative good name for size()
public abstract Optional<Long> size();
public abstract Type type();
@@ -337,7 +342,7 @@ public class TensorType {
* [] + {} = {}
*/
Dimension combineWith(Optional<Dimension> other, boolean allowDifferentSizes) {
- if ( ! other.isPresent()) return this;
+ if (other.isEmpty()) return this;
if (this instanceof MappedDimension) return this;
if (other.get() instanceof MappedDimension) return other.get();
// both are indexed
@@ -600,9 +605,9 @@ public class TensorType {
public Builder dimension(String name, Dimension.Type type) {
switch (type) {
- case mapped : mapped(name); break;
- case indexedUnbound : indexed(name); break;
- default : throw new IllegalArgumentException("This can not create a dimension of type " + type);
+ case mapped -> mapped(name);
+ case indexedUnbound -> indexed(name);
+ default -> throw new IllegalArgumentException("This can not create a dimension of type " + type);
}
return this;
}
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/CosineSimilarity.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/CosineSimilarity.java
index 0e5b031c2cc..9bc3c80a230 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/CosineSimilarity.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/CosineSimilarity.java
@@ -8,7 +8,6 @@ import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.TensorType.Dimension;
-import java.util.Collections;
import java.util.List;
import java.util.Objects;
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/EuclideanDistance.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/EuclideanDistance.java
index 4c771fe8843..07b572b3f93 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/EuclideanDistance.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/EuclideanDistance.java
@@ -8,7 +8,6 @@ import com.yahoo.tensor.Tensor;
import com.yahoo.tensor.TensorType;
import com.yahoo.tensor.TensorType.Dimension;
-import java.util.Collections;
import java.util.List;
import java.util.Objects;
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/ReduceJoin.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/ReduceJoin.java
index de1c30e6414..323056c7204 100644
--- a/vespajlib/src/main/java/com/yahoo/tensor/functions/ReduceJoin.java
+++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/ReduceJoin.java
@@ -12,7 +12,6 @@ import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.DoubleBinaryOperator;
-import java.util.stream.Collectors;
/**
* An optimization for tensor expressions where a join immediately follows a