-
Notifications
You must be signed in to change notification settings - Fork 155
/
PgmIndexUtil.java
122 lines (107 loc) · 4.29 KB
/
PgmIndexUtil.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* HPPC
*
* Copyright (C) 2010-2022 Carrot Search s.c.
* All rights reserved.
*
* Refer to the full license file "LICENSE.txt":
* https://github.com/carrotsearch/hppc/blob/master/LICENSE.txt
*/
package com.carrotsearch.hppc;
/** Utility methods for {@code KTypePgmIndex}. */
class PgmIndexUtil {
/** Adds the first key of the current segment to the segment data bytes. */
static <KType> void addKey(KType key, IntGrowableArray segmentData) {
throw new UnsupportedOperationException("Invalid for generic type: " + key);
}
/** Adds the first key of the current segment to the segment data bytes. */
static void addKey(int key, IntGrowableArray segmentData) {
segmentData.add(key);
}
/** Adds the first key of the current segment to the segment data bytes. */
static void addKey(float key, IntGrowableArray segmentData) {
addKey(Float.floatToIntBits(key), segmentData);
}
/** Adds the first key of the current segment to the segment data bytes. */
static void addKey(long key, IntGrowableArray segmentData) {
segmentData.add((int) key);
segmentData.add((int) (key >> 32));
}
/** Adds the first key of the current segment to the segment data bytes. */
static void addKey(double key, IntGrowableArray segmentData) {
addKey(Double.doubleToRawLongBits(key), segmentData);
}
/** Gets the first key of the segment at the given data index. */
static <KType> KType getKey(int segmentDataIndex, int[] segmentData, KType keyType) {
throw new UnsupportedOperationException("Invalid for generic type: " + keyType);
}
/** Gets the first key of the segment at the given data index. */
static int getKey(int segmentDataIndex, int[] segmentData, int keyType) {
return segmentData[segmentDataIndex];
}
/** Gets the first key of the segment at the given data index. */
static float getKey(int segmentDataIndex, int[] segmentData, float keyType) {
return Float.intBitsToFloat(getKey(segmentDataIndex, segmentData, 0));
}
/** Gets the first key of the segment at the given data index. */
static long getKey(int segmentDataIndex, int[] segmentData, long keyType) {
return (segmentData[segmentDataIndex] & 0xFFFFFFFFL)
| (((long) segmentData[segmentDataIndex + 1]) << 32);
}
/** Gets the first key of the segment at the given data index. */
static double getKey(int segmentDataIndex, int[] segmentData, double keyType) {
return Double.longBitsToDouble(getKey(segmentDataIndex, segmentData, 0L));
}
/**
* Adds the intercept of the current segment to the segment data bytes. The intercept is stored as
* an int for a key size equal to 1, otherwise it is stored as a long.
*
* @param keySize The size of the key, measure in {@link Integer#BYTES}.
*/
static void addIntercept(long intercept, IntGrowableArray segmentData, int keySize) {
assert keySize >= 1 && keySize <= 2;
if (keySize == 1) {
addKey((int) intercept, segmentData);
} else {
addKey(intercept, segmentData);
}
}
/**
* Gets the intercept of the segment at the given data index.
*
* @param keySize The size of the key, measure in {@link Integer#BYTES}.
*/
static long getIntercept(int segmentDataIndex, int[] segmentData, int keySize) {
assert keySize >= 1 && keySize <= 2;
if (keySize == 1) {
return getKey(segmentDataIndex, segmentData, 0);
}
return getKey(segmentDataIndex, segmentData, 0L);
}
/**
* Adds the slope of the current segment to the segment data bytes. The intercept is stored as a
* float for a key size equal to 1, otherwise it is stored as a double.
*
* @param keySize The size of the key, measure in {@link Integer#BYTES}.
*/
static void addSlope(double slope, IntGrowableArray segmentData, int keySize) {
assert keySize >= 1 && keySize <= 2;
if (keySize == 1) {
addKey((float) slope, segmentData);
} else {
addKey(slope, segmentData);
}
}
/**
* Gets the slope of the segment at the given data index.
*
* @param keySize The size of the key, measure in {@link Integer#BYTES}.
*/
static double getSlope(int segmentDataIndex, int[] segmentData, int keySize) {
assert keySize >= 1 && keySize <= 2;
if (keySize == 1) {
return getKey(segmentDataIndex, segmentData, 0f);
}
return getKey(segmentDataIndex, segmentData, 0d);
}
}