aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/impl/TensorAddressAnyN.java
blob: d5bac62bf1878a825ad96077d3a5d64d6bb45d5e (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.tensor.impl;

import com.yahoo.tensor.TensorAddress;

import java.util.Arrays;

import static java.lang.Math.abs;

/**
 * An n-dimensional address.
 *
 * @author baldersheim
 */
final class TensorAddressAnyN extends TensorAddressAny {

    private final int[] labels;

    TensorAddressAnyN(int[] labels) {
        if (labels.length < 1) throw new IllegalArgumentException("Need at least 1 label");
        this.labels = labels;
    }

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

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

    @Override
    public TensorAddress withLabel(int labelIndex, long label) {
        int[] copy = Arrays.copyOf(labels, labels.length);
        copy[labelIndex] = Convert.safe2Int(label);
        return new TensorAddressAnyN(copy);
    }

    @Override public int hashCode() {
        int hash = abs(labels[0]);
        for (int i = 0; i < size(); i++) {
            hash = hash | (abs(labels[i]) << (32 - Integer.numberOfLeadingZeros(hash)));
        }
        return hash;
    }

    @Override
    public boolean equals(Object o) {
        if (! (o instanceof TensorAddressAnyN any) || (size() != any.size())) return false;
        for (int i = 0; i < size(); i++) {
            if (labels[i] != any.labels[i]) return false;
        }
        return true;
    }

}