diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2024-01-28 19:28:42 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-01-28 19:28:42 +0100 |
commit | 6992a234c6e474dbe95e446bd1cdc50bdb452414 (patch) | |
tree | 17abc9faf9e43147ee195b0dbe9259eb361b3a6d /vespajlib/src/main/java/com/yahoo/tensor/impl/Label.java | |
parent | 140ed00756d8b45ee622fdd61d2df91dd133133a (diff) | |
parent | c824e1209fdd979612ccead027889eb6294b4652 (diff) |
Merge pull request #30084 from vespa-engine/bratseth/tensor-cleanupv8.294.50
Minor cleanup
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor/impl/Label.java')
-rw-r--r-- | vespajlib/src/main/java/com/yahoo/tensor/impl/Label.java | 22 |
1 files changed, 17 insertions, 5 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/impl/Label.java b/vespajlib/src/main/java/com/yahoo/tensor/impl/Label.java index 0ab1454eb58..a09c0223d28 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/impl/Label.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/impl/Label.java @@ -7,16 +7,26 @@ import java.util.Arrays; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; +/** + * A label is a value of a mapped dimension of a tensor. + * This class provides a mapping of labels to numbers which allow for more efficient computation with + * mapped tensor dimensions. + * + * @author baldersheim + */ public class Label { - private static final String [] SMALL_INDEXES = createSmallIndexesAsStrings(1000); + + private static final String[] SMALL_INDEXES = createSmallIndexesAsStrings(1000); + private final static Map<String, Integer> string2Enum = new ConcurrentHashMap<>(); + // Index 0 is unused, that is a valid positive number // 1(-1) is reserved for the Tensor.INVALID_INDEX - private static volatile String [] uniqueStrings = {"UNIQUE_UNUSED_MAGIC", "Tensor.INVALID_INDEX"}; + private static volatile String[] uniqueStrings = {"UNIQUE_UNUSED_MAGIC", "Tensor.INVALID_INDEX"}; private static int numUniqeStrings = 2; private static String[] createSmallIndexesAsStrings(int count) { - String [] asStrings = new String[count]; + String[] asStrings = new String[count]; for (int i = 0; i < count; i++) { asStrings[i] = String.valueOf(i); } @@ -46,7 +56,7 @@ public class Label { } public static int toNumber(String s) { - if (s == null) { return Tensor.INVALID_INDEX; } + if (s == null) { return Tensor.invalidIndex; } try { if (validNumericIndex(s)) { return Integer.parseInt(s); @@ -55,14 +65,16 @@ public class Label { } return string2Enum.computeIfAbsent(s, Label::addNewUniqueString); } + public static String fromNumber(int v) { if (v >= 0) { return asNumericString(v); } else { - if (v == Tensor.INVALID_INDEX) { return null; } + if (v == Tensor.invalidIndex) { return null; } return uniqueStrings[-v]; } } + public static String fromNumber(long v) { return fromNumber(Convert.safe2Int(v)); } |