aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/impl/NumericTensorAddress.java
blob: 983074c9c90e443944b33f505014214b3b722470 (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
package com.yahoo.tensor.impl;

import com.yahoo.tensor.TensorAddress;

import java.util.Arrays;
import java.util.stream.Collectors;

public final class NumericTensorAddress extends TensorAddress {
    private static final String [] SMALL_INDEXES = createSmallIndexesAsStrings(1000);

    private final long[] labels;

    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 NumericTensorAddress(long[] labels) {
        this.labels = labels;
    }

    public static NumericTensorAddress of(long ... labels) {
        return new NumericTensorAddress(Arrays.copyOf(labels, labels.length));
    }

    public static NumericTensorAddress unsafeOf(long ... labels) {
        return new NumericTensorAddress(labels);
    }

    @Override
    public int size() { return labels.length; }

    @Override
    public String label(int i) { return asString(labels[i]); }

    @Override
    public long numericLabel(int i) { return labels[i]; }

    @Override
    public TensorAddress withLabel(int index, long label) {
        long[] labels = Arrays.copyOf(this.labels, this.labels.length);
        labels[index] = label;
        return new NumericTensorAddress(labels);
    }

    @Override
    public String toString() {
        return "cell address (" + Arrays.stream(labels).mapToObj(NumericTensorAddress::asString).collect(Collectors.joining(",")) + ")";
    }

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

}