-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathIndexExtractor.java
More file actions
160 lines (147 loc) · 5.2 KB
/
IndexExtractor.java
File metadata and controls
160 lines (147 loc) · 5.2 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import java.util.Arrays;
import java.util.BitSet;
import java.util.Objects;
import java.util.function.IntPredicate;
import java.util.function.LongPredicate;
/**
* An object that produces indices of a Bloom filter.
* <p><em>
* The default implementation of {@code asIndexArray} is slow. Implementers should reimplement the
* method where possible.</em></p>
*
* @since 4.5.0-M2
*/
@FunctionalInterface
public interface IndexExtractor {
/**
* Creates an IndexExtractor from a {@code BitMapExtractor}.
*
* @param bitMapExtractor the {@code BitMapExtractor}
* @return a new {@code IndexExtractor}.
*/
static IndexExtractor fromBitMapExtractor(final BitMapExtractor bitMapExtractor) {
Objects.requireNonNull(bitMapExtractor, "bitMapExtractor");
return consumer -> {
final LongPredicate longPredicate = new LongPredicate() {
int wordIdx;
@Override
public boolean test(long word) {
int i = wordIdx;
while (word != 0) {
if ((word & 1) == 1 && !consumer.test(i)) {
return false;
}
word >>>= 1;
i++;
}
wordIdx += 64;
return true;
}
};
return bitMapExtractor.processBitMaps(longPredicate::test);
};
}
/**
* Creates an IndexExtractor from an array of integers.
*
* @param values the index values
* @return an IndexExtractor that uses the values.
*/
static IndexExtractor fromIndexArray(final int... values) {
return new IndexExtractor() {
@Override
public int[] asIndexArray() {
return values.clone();
}
@Override
public boolean processIndices(final IntPredicate predicate) {
for (final int value : values) {
if (!predicate.test(value)) {
return false;
}
}
return true;
}
};
}
/**
* Return a copy of the IndexExtractor data as an int array.
*
* <p>Indices ordering and uniqueness is not guaranteed.</p>
*
* <p><em>
* The default implementation of this method creates an array and populates
* it. Implementations that have access to an index array should consider
* returning a copy of that array if possible.
* </em></p>
*
* @return An int array of the data.
*/
default int[] asIndexArray() {
final class Indices {
private int[] data = new int[32];
private int size;
boolean add(final int index) {
data = IndexUtils.ensureCapacityForAdd(data, size);
data[size++] = index;
return true;
}
int[] toArray() {
// Edge case to avoid a large array copy
return size == data.length ? data : Arrays.copyOf(data, size);
}
}
final Indices indices = new Indices();
processIndices(indices::add);
return indices.toArray();
}
/**
* Each index is passed to the predicate. The predicate is applied to each
* index value, if the predicate returns {@code false} the execution is stopped, {@code false}
* is returned, and no further indices are processed.
*
* <p>Any exceptions thrown by the action are relayed to the caller.</p>
*
* <p>Indices ordering and uniqueness is not guaranteed.</p>
*
* @param predicate the action to be performed for each non-zero bit index.
* @return {@code true} if all indexes return true from consumer, {@code false} otherwise.
* @throws NullPointerException if the specified action is null
*/
boolean processIndices(IntPredicate predicate);
/**
* Creates an IndexExtractor comprising the unique indices for this extractor.
*
* <p>By default creates a new extractor with some overhead to remove
* duplicates. IndexExtractors that return unique indices by default
* should override this to return {@code this}.</p>
*
* <p>The default implementation will filter the indices from this instance
* and return them in ascending order.</p>
*
* @return the IndexExtractor of unique values.
* @throws IndexOutOfBoundsException if any index is less than zero.
*/
default IndexExtractor uniqueIndices() {
final BitSet bitSet = new BitSet();
processIndices(i -> {
bitSet.set(i);
return true;
});
return new IndexExtractor() {
@Override
public boolean processIndices(final IntPredicate predicate) {
for (int idx = bitSet.nextSetBit(0); idx >= 0; idx = bitSet.nextSetBit(idx + 1)) {
if (!predicate.test(idx)) {
return false;
}
}
return true;
}
@Override
public IndexExtractor uniqueIndices() {
return this;
}
};
}
}