aboutsummaryrefslogtreecommitdiffstats
path: root/predicate-search/src/main/java/com/yahoo/search/predicate/index/Interval.java
blob: 18d8f5de288813012d7f54a79fb8dddfb768ca2f (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
83
84
85
86
87
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.predicate.index;

/**
 * Utility class for interval related constants and methods.
 * An interval consists of a begin and end value indicating the start and end of the interval.
 * Both value are inclusive, eg (1,2) is an interval of size 2.
 *
 * There are 3 types of interval; normal, ZStar1 and ZStar2.
 *
 * Normal intervals have begin value in 16 MSB and end in 16 LSB.
 * ZStar1 intervals have end value in 16 MSB and begin in 16 LSB.
 * ZStar2 intervals have only an end value located at 16 LSB.
 *
 * @author Magnar Nedland
 * @author bjorncs
 */
public class Interval {

    public static final int INTERVAL_BEGIN = 0x01;
    public static final int MAX_INTERVAL_END = 0xffff;
    public static final int ZERO_CONSTRAINT_RANGE = 1;

    private Interval() {}

    public static int fromBoundaries(int begin, int end) {
        assert begin >= INTERVAL_BEGIN && begin <= MAX_INTERVAL_END
                && end >= INTERVAL_BEGIN && end <= MAX_INTERVAL_END : toString(begin, end);
        return (begin << 16) | end;
    }

    public static int fromZStar1Boundaries(int begin, int end) {
        assert begin >= 0 && begin <= MAX_INTERVAL_END
                && end >= INTERVAL_BEGIN && end <= MAX_INTERVAL_END : toString(end, begin);
        return (end << 16) | begin;
    }

    public static int fromZStar2Boundaries(int end) {
        assert end >= INTERVAL_BEGIN && end <= MAX_INTERVAL_END : toString(0, end);
        return end;
    }

    public static boolean isZStar1Interval(int interval) {
        return getBegin(interval) > getEnd(interval);
    }

    public static boolean isZStar2Interval(int interval) {
        return (interval & 0xffff0000) == 0;
    }

    public static int getBegin(int interval) {
        return interval >>> 16;
    }

    public static int getEnd(int interval) {
        return interval & 0xffff;
    }

    public static int getZStar1Begin(int interval) {
        return getEnd(interval);
    }

    public static int getZStar1End(int interval) {
        return getBegin(interval);
    }

    public static int getZStar2End(int interval) {
        return interval;
    }

    /**
     * @return A new ZStar1 interval with boundaries [end(zStar1)+1, end(zStar2)]
     */
    public static int combineZStarIntervals(int zStar1, int zStar2) {
        return zStar1 >>> 16 | zStar2 << 16;
    }

    private static String toString(int begin, int end) {
        if (begin == 0) {
            return String.format("[%d]**", end);
        } else if (begin > end) {
            return String.format("[%d, %d]*", begin, end);
        }
        return String.format("[%d, %d]", begin, end);
    }

}