summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/impl/Label.java
blob: a09c0223d28e2ef58b14ed728b59058c3b7f4915 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.yahoo.tensor.impl;


import com.yahoo.tensor.Tensor;

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 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 int numUniqeStrings = 2;

    private static String[] createSmallIndexesAsStrings(int count) {
        String[] asStrings = new String[count];
        for (int i = 0; i < count; i++) {
            asStrings[i] = String.valueOf(i);
        }
        return asStrings;
    }

    private static int addNewUniqueString(String s) {
        synchronized (string2Enum) {
            if (numUniqeStrings >= uniqueStrings.length) {
                uniqueStrings = Arrays.copyOf(uniqueStrings, uniqueStrings.length*2);
            }
            uniqueStrings[numUniqeStrings] = s;
            return -numUniqeStrings++;
        }
    }

    private static String asNumericString(long index) {
        return ((index >= 0) && (index < SMALL_INDEXES.length)) ? SMALL_INDEXES[(int)index] : String.valueOf(index);
    }

    private static boolean validNumericIndex(String s) {
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if ((c < '0') || (c > '9')) return false;
        }
        return true;
    }

    public static int toNumber(String s) {
        if (s == null) { return Tensor.invalidIndex; }
        try {
            if (validNumericIndex(s)) {
                return Integer.parseInt(s);
            }
        } catch (NumberFormatException e) {
        }
        return string2Enum.computeIfAbsent(s, Label::addNewUniqueString);
    }

    public static String fromNumber(int v) {
        if (v >= 0) {
            return asNumericString(v);
        } else {
            if (v == Tensor.invalidIndex) { return null; }
            return uniqueStrings[-v];
        }
    }

    public static String fromNumber(long v) {
        return fromNumber(Convert.safe2Int(v));
    }

}