Skip to content

Commit 626cd7e

Browse files
committed
BitSet: prefer builtin boost function
1 parent 146a62a commit 626cd7e

File tree

1 file changed

+23
-103
lines changed

1 file changed

+23
-103
lines changed

src/core/util/BitSet.cpp

Lines changed: 23 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,20 @@ void BitSet::fastClear(uint32_t bitIndex) {
4141
}
4242

4343
void BitSet::clear(uint32_t fromIndex, uint32_t toIndex) {
44+
#if BOOST_VERSION >= 106900
45+
fromIndex = std::min(fromIndex, (uint32_t)bitSet.size());
46+
toIndex = std::min(toIndex, (uint32_t)bitSet.size());
47+
bitSet.reset(fromIndex, toIndex - fromIndex);
48+
#else
4449
toIndex = std::min(toIndex, (uint32_t)bitSet.size());
4550
for (bitset_type::size_type i = std::min(fromIndex, (uint32_t)bitSet.size()); i < toIndex; ++i) {
4651
bitSet.set(i, false);
4752
}
53+
#endif
4854
}
4955

5056
void BitSet::fastClear(uint32_t fromIndex, uint32_t toIndex) {
51-
for (bitset_type::size_type i = fromIndex; i < toIndex; ++i) {
52-
bitSet.set(i, false);
53-
}
57+
fastSet(fromIndex, toIndex, false);
5458
}
5559

5660
void BitSet::set(uint32_t bitIndex) {
@@ -76,33 +80,28 @@ void BitSet::fastSet(uint32_t bitIndex, bool value) {
7680
}
7781

7882
void BitSet::set(uint32_t fromIndex, uint32_t toIndex) {
79-
if (toIndex >= bitSet.size()) {
80-
resize(toIndex + 1);
81-
}
82-
for (bitset_type::size_type i = fromIndex; i < toIndex; ++i) {
83-
bitSet.set(i, true);
84-
}
83+
set(fromIndex, toIndex, true);
8584
}
8685

8786
void BitSet::fastSet(uint32_t fromIndex, uint32_t toIndex) {
88-
for (bitset_type::size_type i = fromIndex; i < toIndex; ++i) {
89-
bitSet.set(i, true);
90-
}
87+
fastSet(fromIndex, toIndex, true);
9188
}
9289

9390
void BitSet::set(uint32_t fromIndex, uint32_t toIndex, bool value) {
9491
if (toIndex >= bitSet.size()) {
9592
resize(toIndex + 1);
9693
}
97-
for (bitset_type::size_type i = fromIndex; i < toIndex; ++i) {
98-
bitSet.set(i, value);
99-
}
94+
fastSet(fromIndex, toIndex, value);
10095
}
10196

10297
void BitSet::fastSet(uint32_t fromIndex, uint32_t toIndex, bool value) {
98+
#if BOOST_VERSION >= 106900
99+
bitSet.set(fromIndex, toIndex - fromIndex, value);
100+
#else
103101
for (bitset_type::size_type i = fromIndex; i < toIndex; ++i) {
104102
bitSet.set(i, value);
105103
}
104+
#endif
106105
}
107106

108107
void BitSet::flip(uint32_t bitIndex) {
@@ -120,15 +119,17 @@ void BitSet::flip(uint32_t fromIndex, uint32_t toIndex) {
120119
if (toIndex >= bitSet.size()) {
121120
resize(toIndex + 1);
122121
}
123-
for (bitset_type::size_type i = fromIndex; i < toIndex; ++i) {
124-
bitSet.flip(i);
125-
}
122+
fastFlip(fromIndex, toIndex);
126123
}
127124

128125
void BitSet::fastFlip(uint32_t fromIndex, uint32_t toIndex) {
126+
#if BOOST_VERSION >= 106900
127+
bitSet.flip(fromIndex, toIndex - fromIndex);
128+
#else
129129
for (bitset_type::size_type i = fromIndex; i < toIndex; ++i) {
130130
bitSet.flip(i);
131131
}
132+
#endif
132133
}
133134

134135
uint32_t BitSet::size() const {
@@ -152,28 +153,21 @@ bool BitSet::fastGet(uint32_t bitIndex) const {
152153
}
153154

154155
int32_t BitSet::nextSetBit(uint32_t fromIndex) const {
156+
#if BOOST_VERSION >= 108800
157+
return bitSet.find_first(fromIndex);
158+
#else
155159
bitset_type::size_type next = fromIndex == 0 ? bitSet.find_first() : bitSet.find_next(fromIndex - 1);
156160
return next == bitset_type::npos ? -1 : next;
161+
#endif
157162
}
158163

159164
void BitSet::_and(const BitSetPtr& set) {
160-
#if BOOST_VERSION >= 108900
161165
bitset_type other = set->bitSet;
162166
other.resize(bitSet.size());
163167
bitSet &= other;
164-
#else
165-
bitset_type::size_type minBlocks = std::min(bitSet.num_blocks(), set->bitSet.num_blocks());
166-
for (bitset_type::size_type i = 0; i < minBlocks; ++i) {
167-
bitSet.m_bits[i] &= set->bitSet.m_bits[i];
168-
}
169-
if (bitSet.num_blocks() > minBlocks) {
170-
std::fill(bitSet.m_bits.begin() + minBlocks, bitSet.m_bits.end(), bitset_type::block_type(0));
171-
}
172-
#endif
173168
}
174169

175170
void BitSet::_or(const BitSetPtr& set) {
176-
#if BOOST_VERSION >= 108900
177171
if (set->bitSet.size() > bitSet.size()) {
178172
resize(set->bitSet.size());
179173
bitSet |= set->bitSet;
@@ -182,22 +176,9 @@ void BitSet::_or(const BitSetPtr& set) {
182176
other.resize(bitSet.size());
183177
bitSet |= other;
184178
}
185-
#else
186-
bitset_type::size_type minBlocks = std::min(bitSet.num_blocks(), set->bitSet.num_blocks());
187-
if (set->bitSet.size() > bitSet.size()) {
188-
resize(set->bitSet.size());
189-
}
190-
for (bitset_type::size_type i = 0; i < minBlocks; ++i) {
191-
bitSet.m_bits[i] |= set->bitSet.m_bits[i];
192-
}
193-
if (bitSet.num_blocks() > minBlocks) {
194-
std::copy(set->bitSet.m_bits.begin() + minBlocks, set->bitSet.m_bits.end(), bitSet.m_bits.begin() + minBlocks);
195-
}
196-
#endif
197179
}
198180

199181
void BitSet::_xor(const BitSetPtr& set) {
200-
#if BOOST_VERSION >= 108900
201182
if (set->bitSet.size() > bitSet.size()) {
202183
resize(set->bitSet.size());
203184
bitSet ^= set->bitSet;
@@ -206,60 +187,24 @@ void BitSet::_xor(const BitSetPtr& set) {
206187
other.resize(bitSet.size());
207188
bitSet ^= other;
208189
}
209-
#else
210-
bitset_type::size_type minBlocks = std::min(bitSet.num_blocks(), set->bitSet.num_blocks());
211-
if (set->bitSet.size() > bitSet.size()) {
212-
resize(set->bitSet.size());
213-
}
214-
for (bitset_type::size_type i = 0; i < minBlocks; ++i) {
215-
bitSet.m_bits[i] ^= set->bitSet.m_bits[i];
216-
}
217-
if (bitSet.num_blocks() > minBlocks) {
218-
std::copy(set->bitSet.m_bits.begin() + minBlocks, set->bitSet.m_bits.end(), bitSet.m_bits.begin() + minBlocks);
219-
}
220-
#endif
221190
}
222191

223192
void BitSet::andNot(const BitSetPtr& set) {
224-
#if BOOST_VERSION >= 108900
225193
bitset_type other = set->bitSet;
226194
other.resize(bitSet.size());
227195
bitSet &= other.flip();
228-
#else
229-
bitset_type::size_type minBlocks = std::min(bitSet.num_blocks(), set->bitSet.num_blocks());
230-
for (bitset_type::size_type i = 0; i < minBlocks; ++i) {
231-
bitSet.m_bits[i] &= ~set->bitSet.m_bits[i];
232-
}
233-
#endif
234196
}
235197

236198
bool BitSet::intersectsBitSet(const BitSetPtr& set) const {
237199
return bitSet.intersects(set->bitSet);
238200
}
239201

240202
uint32_t BitSet::cardinality() {
241-
#if BOOST_VERSION >= 108900
242203
return bitSet.count();
243-
#else
244-
return bitSet.num_blocks() == 0 ? 0 : (uint32_t)BitUtil::pop_array((int64_t*)getBits(), 0, bitSet.num_blocks());
245-
#endif
246204
}
247205

248206
void BitSet::resize(uint32_t size) {
249-
#if BOOST_VERSION >= 108900
250207
bitSet.resize(size);
251-
#else
252-
bitset_type::size_type old_num_blocks = bitSet.num_blocks();
253-
bitset_type::size_type required_blocks = bitSet.calc_num_blocks(size);
254-
if (required_blocks != old_num_blocks) {
255-
bitSet.m_bits.resize(required_blocks, bitset_type::block_type(0));
256-
}
257-
bitSet.m_num_bits = size;
258-
uint64_t extra_bits = static_cast<uint64_t>(bitSet.size() % bitSet.bits_per_block);
259-
if (extra_bits != 0) {
260-
bitSet.m_bits.back() &= ~(~static_cast<bitset_type::block_type>(0) << extra_bits);
261-
}
262-
#endif
263208
}
264209

265210
bool BitSet::equals(const LuceneObjectPtr& other) {
@@ -272,7 +217,6 @@ bool BitSet::equals(const LuceneObjectPtr& other) {
272217
}
273218
BitSetPtr first = bitSet.num_blocks() < otherBitSet->bitSet.num_blocks() ? otherBitSet : shared_from_this();
274219
BitSetPtr second = bitSet.num_blocks() < otherBitSet->bitSet.num_blocks() ? shared_from_this() : otherBitSet;
275-
#if BOOST_VERSION >= 108900
276220
bitset_type::size_type f = first->bitSet.find_first();
277221
bitset_type::size_type s = second->bitSet.find_first();
278222
while (f == s) {
@@ -283,42 +227,18 @@ bool BitSet::equals(const LuceneObjectPtr& other) {
283227
s = second->bitSet.find_next(s);
284228
}
285229
return false;
286-
#else
287-
bitset_type::size_type firstLength = first->bitSet.num_blocks();
288-
bitset_type::size_type secondLength = second->bitSet.num_blocks();
289-
for (bitset_type::size_type i = secondLength; i < firstLength; ++i) {
290-
if (first->bitSet.m_bits[i] != 0) {
291-
return false;
292-
}
293-
}
294-
for (bitset_type::size_type i = 0; i < secondLength; ++i) {
295-
if (first->bitSet.m_bits[i] != second->bitSet.m_bits[i]) {
296-
return false;
297-
}
298-
}
299-
return true;
300-
#endif
301230
}
302231

303232
int32_t BitSet::hashCode() {
304233
// Start with a zero hash and use a mix that results in zero if the input is zero.
305234
// This effectively truncates trailing zeros without an explicit check.
306235
int64_t hash = 0;
307-
#if BOOST_VERSION >= 108900
308236
to_block_range(bitSet, boost::make_function_output_iterator(
309237
[&hash](bitset_type::block_type block) {
310238
hash ^= block;
311239
hash = (hash << 1) | (hash >> 63); // rotate left
312240
}
313241
));
314-
#else
315-
uint32_t maxSize = bitSet.num_blocks();
316-
const uint64_t* bits = getBits();
317-
for (uint32_t bit = 0; bit < maxSize; ++bit) {
318-
hash ^= bits[bit];
319-
hash = (hash << 1) | (hash >> 63); // rotate left
320-
}
321-
#endif
322242
// Fold leftmost bits into right and add a constant to prevent empty sets from
323243
// returning 0, which is too common.
324244
return (int32_t)((hash >> 32) ^ hash) + 0x98761234;

0 commit comments

Comments
 (0)