From 448231f18ba53edf5c0e7ab4b6732ef69328281c Mon Sep 17 00:00:00 2001 From: Henning Baldersheim Date: Wed, 30 Nov 2022 10:04:50 +0100 Subject: Reduce the simple usage of guava where java has caught up --- .../main/java/com/yahoo/tensor/IndexedTensor.java | 16 ++++------ .../main/java/com/yahoo/tensor/MixedTensor.java | 1 - .../java/com/yahoo/tensor/functions/Argmax.java | 10 +++--- .../java/com/yahoo/tensor/functions/Argmin.java | 10 +++--- .../java/com/yahoo/tensor/functions/Concat.java | 37 ++++++---------------- .../com/yahoo/tensor/functions/DynamicTensor.java | 3 +- .../main/java/com/yahoo/tensor/functions/Join.java | 6 ++-- .../java/com/yahoo/tensor/functions/Matmul.java | 3 +- .../java/com/yahoo/tensor/functions/Merge.java | 11 +------ .../java/com/yahoo/tensor/functions/Reduce.java | 29 ++++++++--------- .../com/yahoo/tensor/functions/ReduceJoin.java | 5 ++- .../java/com/yahoo/tensor/functions/Rename.java | 10 +++--- .../yahoo/tensor/functions/ScalarFunctions.java | 10 +++--- .../java/com/yahoo/tensor/functions/Softmax.java | 3 +- .../java/com/yahoo/tensor/functions/XwPlusB.java | 3 +- 15 files changed, 55 insertions(+), 102 deletions(-) (limited to 'vespajlib/src/main/java/com/yahoo/tensor') diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java index 50809ab3ff6..996d85b3199 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java @@ -1,19 +1,16 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Optional; import java.util.Set; -import java.util.function.DoubleBinaryOperator; /** * An indexed (dense) tensor. @@ -202,7 +199,7 @@ public abstract class IndexedTensor implements Tensor { @Override public Map cells() { if (dimensionSizes.dimensions() == 0) - return Collections.singletonMap(TensorAddress.of(), get(0)); + return Map.of(TensorAddress.of(), get(0)); ImmutableMap.Builder builder = new ImmutableMap.Builder<>(); Indexes indexes = Indexes.of(dimensionSizes, dimensionSizes, size()); @@ -574,7 +571,7 @@ public abstract class IndexedTensor implements Tensor { throw new IllegalArgumentException("Wrong number of indexes (" + indexes.length + ") for " + type); if (indexes.length == 0) { - firstDimension = Collections.singletonList(value); + firstDimension = List.of(value); return this; } @@ -767,7 +764,7 @@ public abstract class IndexedTensor implements Tensor { private final static class LazyCell extends Tensor.Cell { private double value; - private Indexes indexes; + private final Indexes indexes; private LazyCell(Indexes indexes, Double value) { super(null, value); @@ -919,10 +916,9 @@ public abstract class IndexedTensor implements Tensor { /** Returns an immutable list containing a copy of the indexes in this */ public List toList() { - ImmutableList.Builder builder = new ImmutableList.Builder<>(); - for (long index : indexes) - builder.add(index); - return builder.build(); + ArrayList list = new ArrayList<>(indexes.length); + for(long index : indexes) { list.add(index); } + return List.copyOf(list); } @Override diff --git a/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java index e7690876434..fed9f7017ed 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java @@ -11,7 +11,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.function.DoubleBinaryOperator; import java.util.stream.Collectors; /** diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmax.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmax.java index d2762ad762d..2ee3d7dab60 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmax.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmax.java @@ -1,10 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.evaluation.Name; -import java.util.Collections; import java.util.List; import java.util.Objects; @@ -17,21 +15,21 @@ public class Argmax extends CompositeTensorFunction dimensions; public Argmax(TensorFunction argument) { - this(argument, Collections.emptyList()); + this(argument, List.of()); } public Argmax(TensorFunction argument, String dimension) { - this(argument, Collections.singletonList(dimension)); + this(argument, List.of(dimension)); } public Argmax(TensorFunction argument, List dimensions) { Objects.requireNonNull(dimensions, "The dimensions cannot be null"); this.argument = argument; - this.dimensions = ImmutableList.copyOf(dimensions); + this.dimensions = List.copyOf(dimensions); } @Override - public List> arguments() { return Collections.singletonList(argument); } + public List> arguments() { return List.of(argument); } @Override public TensorFunction withArguments(List> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmin.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmin.java index baedf41bcb8..a1e30c419e3 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmin.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmin.java @@ -1,10 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.evaluation.Name; -import java.util.Collections; import java.util.List; import java.util.Objects; @@ -17,21 +15,21 @@ public class Argmin extends CompositeTensorFunction dimensions; public Argmin(TensorFunction argument) { - this(argument, Collections.emptyList()); + this(argument, List.of()); } public Argmin(TensorFunction argument, String dimension) { - this(argument, Collections.singletonList(dimension)); + this(argument, List.of(dimension)); } public Argmin(TensorFunction argument, List dimensions) { Objects.requireNonNull(dimensions, "The dimensions cannot be null"); this.argument = argument; - this.dimensions = ImmutableList.copyOf(dimensions); + this.dimensions = List.copyOf(dimensions); } @Override - public List> arguments() { return Collections.singletonList(argument); } + public List> arguments() { return List.of(argument); } @Override public TensorFunction withArguments(List> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java index abf0d89c2b7..2a93acc19e6 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.DimensionSizes; import com.yahoo.tensor.IndexedTensor; import com.yahoo.tensor.Tensor; @@ -46,7 +45,7 @@ public class Concat extends PrimitiveTensorFunction> arguments() { return ImmutableList.of(argumentA, argumentB); } + public List> arguments() { return List.of(argumentA, argumentB); } @Override public TensorFunction withArguments(List> arguments) { @@ -362,20 +361,11 @@ public class Concat extends PrimitiveTensorFunction labels[out++] = leftOnly.label(a++); + case right -> labels[out++] = rightOnly.label(b++); + case both -> labels[out++] = match.label(m++); + case concat -> labels[out++] = String.valueOf(concatDimIdx); + default -> throw new IllegalArgumentException("cannot handle: " + how); } } return TensorAddress.of(labels); @@ -419,17 +409,10 @@ public class Concat extends PrimitiveTensorFunction commonLabels[commonIdx++] = addr.label(i); + case separate -> separateLabels[separateIdx++] = addr.label(i); + case concat -> ccDimIndex = addr.numericLabel(i); + default -> throw new IllegalArgumentException("cannot handle: " + how.handleDims.get(i)); } } TensorAddress commonAddr = TensorAddress.of(commonLabels); diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/DynamicTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/DynamicTensor.java index c402a1bde5b..558b01baa02 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/DynamicTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/DynamicTensor.java @@ -10,7 +10,6 @@ import com.yahoo.tensor.evaluation.EvaluationContext; import com.yahoo.tensor.evaluation.Name; import com.yahoo.tensor.evaluation.TypeContext; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; @@ -32,7 +31,7 @@ public abstract class DynamicTensor extends PrimitiveTens public TensorType type(TypeContext context) { return type; } @Override - public List> arguments() { return Collections.emptyList(); } + public List> arguments() { return List.of(); } @Override public TensorFunction withArguments(List> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java index 1009177761b..61060b700a3 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Sets; import com.yahoo.tensor.DimensionSizes; import com.yahoo.tensor.IndexedTensor; @@ -15,7 +14,6 @@ import com.yahoo.tensor.evaluation.Name; import com.yahoo.tensor.evaluation.TypeContext; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -60,7 +58,7 @@ public class Join extends PrimitiveTensorFunction> arguments() { return ImmutableList.of(argumentA, argumentB); } + public List> arguments() { return List.of(argumentA, argumentB); } @Override public TensorFunction withArguments(List> arguments) { @@ -345,7 +343,7 @@ public class Join extends PrimitiveTensorFunction cellIterator = b.cellIterator(); cellIterator.hasNext(); ) { Tensor.Cell bCell = cellIterator.next(); TensorAddress partialCommonAddress = partialCommonAddress(bCell, bIndexesInCommon); - for (Tensor.Cell aCell : aCellsByCommonAddress.getOrDefault(partialCommonAddress, Collections.emptyList())) { + for (Tensor.Cell aCell : aCellsByCommonAddress.getOrDefault(partialCommonAddress, List.of())) { TensorAddress combinedAddress = joinAddresses(aCell.getKey(), aIndexesInJoined, bCell.getKey(), bIndexesInJoined, joinedType); if (combinedAddress == null) continue; // not combinable diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java index 3239ab1a70c..a06053c88a4 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.TensorType; import com.yahoo.tensor.evaluation.Name; @@ -27,7 +26,7 @@ public class Matmul extends CompositeTensorFunction> arguments() { return ImmutableList.of(argument1, argument2); } + public List> arguments() { return List.of(argument1, argument2); } @Override public TensorFunction withArguments(List> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java index 2b9dc709e0e..86d3ca50bd7 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java @@ -1,11 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Sets; -import com.yahoo.tensor.DimensionSizes; import com.yahoo.tensor.IndexedTensor; -import com.yahoo.tensor.PartialAddress; import com.yahoo.tensor.Tensor; import com.yahoo.tensor.TensorAddress; import com.yahoo.tensor.TensorType; @@ -14,15 +10,10 @@ import com.yahoo.tensor.evaluation.EvaluationContext; import com.yahoo.tensor.evaluation.Name; import com.yahoo.tensor.evaluation.TypeContext; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Optional; -import java.util.Set; import java.util.function.DoubleBinaryOperator; /** @@ -55,7 +46,7 @@ public class Merge extends PrimitiveTensorFunction> arguments() { return ImmutableList.of(argumentA, argumentB); } + public List> arguments() { return List.of(argumentA, argumentB); } @Override public TensorFunction withArguments(List> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java index 96465de6c0f..83d5187f116 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.IndexedTensor; import com.yahoo.tensor.Tensor; import com.yahoo.tensor.TensorAddress; @@ -37,12 +36,12 @@ public class Reduce extends PrimitiveTensorFunction argument, Aggregator aggregator) { - this(argument, aggregator, Collections.emptyList()); + this(argument, aggregator, List.of()); } /** Creates a reduce function reducing a single dimension */ public Reduce(TensorFunction argument, Aggregator aggregator, String dimension) { - this(argument, aggregator, Collections.singletonList(dimension)); + this(argument, aggregator, List.of(dimension)); } /** @@ -57,7 +56,7 @@ public class Reduce extends PrimitiveTensorFunction argument, Aggregator aggregator, List dimensions) { this.argument = Objects.requireNonNull(argument, "The argument tensor cannot be null"); this.aggregator = Objects.requireNonNull(aggregator, "The aggregator cannot be null"); - this.dimensions = ImmutableList.copyOf(dimensions); + this.dimensions = List.copyOf(dimensions); } public static TensorType outputType(TensorType inputType, List reduceDimensions) { @@ -71,7 +70,7 @@ public class Reduce extends PrimitiveTensorFunction dimensions() { return dimensions; } @Override - public List> arguments() { return Collections.singletonList(argument); } + public List> arguments() { return List.of(argument); } @Override public TensorFunction withArguments(List> arguments) { @@ -174,16 +173,16 @@ public class Reduce extends PrimitiveTensorFunction new AvgAggregator(); + case count -> new CountAggregator(); + case max -> new MaxAggregator(); + case median -> new MedianAggregator(); + case min -> new MinAggregator(); + case prod -> new ProdAggregator(); + case sum -> new SumAggregator(); + default -> throw new UnsupportedOperationException("Aggregator " + aggregator + " is not implemented"); + }; } 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 ccb437ef5a7..1048e5ab10e 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/ReduceJoin.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/ReduceJoin.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.DimensionSizes; import com.yahoo.tensor.IndexedTensor; import com.yahoo.tensor.Tensor; @@ -47,12 +46,12 @@ public class ReduceJoin extends CompositeTensorFunction> arguments() { - return ImmutableList.of(argumentA, argumentB); + return List.of(argumentA, argumentB); } @Override diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java index 023e91e424f..8d58cee5e1e 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.Tensor; import com.yahoo.tensor.TensorAddress; import com.yahoo.tensor.TensorType; @@ -10,7 +9,6 @@ import com.yahoo.tensor.evaluation.EvaluationContext; import com.yahoo.tensor.evaluation.Name; import com.yahoo.tensor.evaluation.TypeContext; -import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -30,7 +28,7 @@ public class Rename extends PrimitiveTensorFunction fromToMap; public Rename(TensorFunction argument, String fromDimension, String toDimension) { - this(argument, ImmutableList.of(fromDimension), ImmutableList.of(toDimension)); + this(argument, List.of(fromDimension), List.of(toDimension)); } public Rename(TensorFunction argument, List fromDimensions, List toDimensions) { @@ -43,8 +41,8 @@ public class Rename extends PrimitiveTensorFunction extends PrimitiveTensorFunction> arguments() { return Collections.singletonList(argument); } + public List> arguments() { return List.of(argument); } @Override public TensorFunction withArguments(List> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java index 2639e153923..1057ffa9552 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java @@ -1,8 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; - import java.util.Comparator; import java.util.List; import java.util.Objects; @@ -510,9 +508,9 @@ public class ScalarFunctions { // Variable-length operators ----------------------------------------------------------------------------- public static class EqualElements implements Function, Double> { - private final ImmutableList argumentNames; + private final List argumentNames; private EqualElements(List argumentNames) { - this.argumentNames = ImmutableList.copyOf(argumentNames); + this.argumentNames = List.copyOf(argumentNames); } @Override @@ -553,9 +551,9 @@ public class ScalarFunctions { } public static class SumElements implements Function, Double> { - private final ImmutableList argumentNames; + private final List argumentNames; private SumElements(List argumentNames) { - this.argumentNames = ImmutableList.copyOf(argumentNames); + this.argumentNames = List.copyOf(argumentNames); } @Override diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java index df8cd6d39cd..0853e1becf6 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.TensorType; import com.yahoo.tensor.evaluation.Name; @@ -23,7 +22,7 @@ public class Softmax extends CompositeTensorFunction extends CompositeTensorFunction> arguments() { return ImmutableList.of(x, w, b); } + public List> arguments() { return List.of(x, w, b); } @Override public TensorFunction withArguments(List> arguments) { -- cgit v1.2.3