-
Notifications
You must be signed in to change notification settings - Fork 611
/
LongSet.java
144 lines (125 loc) · 3.85 KB
/
LongSet.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.util;
import java.util.NoSuchElementException;
/**
* Collects long values in a hash set (closed hashing on power-of-two sized long[])
*
* @lucene.internal
*/
public class LongSet {
private static final float LOAD_FACTOR = 0.7f;
private long[] vals;
private int cardinality;
private int mask;
private int threshold;
private int zeroCount; // 1 if a 0 was collected
public LongSet(int sz) {
sz = Math.max(org.apache.lucene.util.BitUtil.nextHighestPowerOfTwo(sz), 2);
vals = new long[sz];
mask = sz - 1;
threshold = (int) (sz * LOAD_FACTOR);
}
/**
* Returns the long[] array that has entries filled in with values or "0" for empty. To see if "0"
* itself is in the set, call containsZero()
*/
public long[] getBackingArray() {
return vals;
}
public boolean containsZero() {
return zeroCount != 0;
}
/**
* Adds an additional value to the set, returns true if the set did not already contain the value.
*/
public boolean add(long val) {
if (val == 0) {
if (zeroCount != 0) {
return false;
} else {
zeroCount = 1;
return true;
}
}
if (cardinality >= threshold) {
rehash();
}
// For floats: exponent bits start at bit 23 for single precision,
// and bit 52 for double precision.
// Many values will only have significant bits just to the right of that.
// For now, lets just settle to get first 8 significant mantissa bits of double or float in the
// lowest bits of our hash. The upper bits of our hash will be irrelevant.
int h = (int) (val + (val >>> 44) + (val >>> 15));
for (int slot = h & mask; ; slot = (slot + 1) & mask) {
long v = vals[slot];
if (v == 0) {
vals[slot] = val;
cardinality++;
return true;
} else if (v == val) {
// val is already in the set
break;
}
}
return false;
}
private void rehash() {
long[] oldVals = vals;
int newCapacity = vals.length << 1;
vals = new long[newCapacity];
mask = newCapacity - 1;
threshold = (int) (newCapacity * LOAD_FACTOR);
cardinality = 0;
for (long val : oldVals) {
if (val != 0) {
add(val);
}
}
}
/** The number of values in the set */
public int cardinality() {
return cardinality + zeroCount;
}
/** Returns an iterator over the values in the set. */
public LongIterator iterator() {
return new LongIterator() {
private int remainingValues = cardinality();
private int valsIdx = 0;
@Override
public boolean hasNext() {
return remainingValues > 0;
}
@Override
public long next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
remainingValues--;
if (remainingValues == 0 && zeroCount > 0) {
return 0;
}
while (true) { // guaranteed to find another value if we get here
long value = vals[valsIdx++];
if (value != 0) {
return value;
}
}
}
};
}
}