summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/impl/TensorAddressAny3.java
blob: 95e14bd375c651fae38cee9ce97a05c99f168cec (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
// 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 static java.lang.Math.abs;

/**
 * 3 dimensional address
 * @author baldersheim
 */
final class TensorAddressAny3 extends TensorAddressAny {
    private final int label0, label1, label2;
    TensorAddressAny3(int label0, int label1, int label2) {
        this.label0 = label0;
        this.label1 = label1;
        this.label2 = label2;
    }

    @Override public int size() { return 3; }

    @Override
    public long numericLabel(int i) {
        return switch (i) {
            case 0 -> label0;
            case 1 -> label1;
            case 2 -> label2;
            default -> throw new IndexOutOfBoundsException("Index is not in [0,2]: " + i);
        };
    }

    @Override
    public TensorAddress withLabel(int labelIndex, long label) {
        return switch (labelIndex) {
            case 0 -> new TensorAddressAny3(Convert.safe2Int(label), label1, label2);
            case 1 -> new TensorAddressAny3(label0, Convert.safe2Int(label), label2);
            case 2 -> new TensorAddressAny3(label0, label1, Convert.safe2Int(label));
            default -> throw new IllegalArgumentException("No label " + labelIndex);
        };
    }

    @Override
    public int hashCode() {
        return abs(label0) |
                (abs(label1) << (1*32 - Integer.numberOfLeadingZeros(abs(label0)))) |
                (abs(label2) << (2*32 - (Integer.numberOfLeadingZeros(abs(label0)) + Integer.numberOfLeadingZeros(abs(label1)))));
    }

    @Override
    public boolean equals(Object o) {
        return (o instanceof TensorAddressAny3 any) &&
                (label0 == any.label0) &&
                (label1 == any.label1) &&
                (label2 == any.label2);
    }
}