Skip to content

Commit 9067c88

Browse files
committed
BitSet: Partial fix for Boost 1.90
1 parent 47ce8e7 commit 9067c88

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

include/lucene++/BitSet.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class LPPAPI BitSet : public LuceneObject {
2424
bitset_type bitSet;
2525

2626
public:
27-
const uint64_t* getBits();
27+
const bitset_type& getBits();
2828
void clear();
2929
void clear(uint32_t bitIndex);
3030
void fastClear(uint32_t bitIndex);

src/core/util/BitSet.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include "LuceneInc.h"
88
#include "BitSet.h"
99
#include "BitUtil.h"
10+
#include <boost/version.hpp>
11+
#include <boost/iterator/function_output_iterator.hpp>
1012

1113
namespace Lucene {
1214

@@ -16,9 +18,15 @@ BitSet::BitSet(uint32_t size) : bitSet(size) {
1618
BitSet::~BitSet() {
1719
}
1820

21+
#if BOOST_VERSION < 108900
1922
const uint64_t* BitSet::getBits() {
2023
return bitSet.empty() ? NULL : static_cast<const uint64_t*>(&bitSet.m_bits[0]);
2124
}
25+
#else
26+
const BitSet::bitset_type& BitSet::getBits() {
27+
return bitSet;
28+
}
29+
#endif
2230

2331
void BitSet::clear() {
2432
bitSet.clear();
@@ -151,16 +159,32 @@ int32_t BitSet::nextSetBit(uint32_t fromIndex) const {
151159
}
152160

153161
void BitSet::_and(const BitSetPtr& set) {
162+
#if BOOST_VERSION >= 108900
163+
bitset_type other = set->bitSet;
164+
other.resize(bitSet.size());
165+
bitSet &= other;
166+
#else
154167
bitset_type::size_type minBlocks = std::min(bitSet.num_blocks(), set->bitSet.num_blocks());
155168
for (bitset_type::size_type i = 0; i < minBlocks; ++i) {
156169
bitSet.m_bits[i] &= set->bitSet.m_bits[i];
157170
}
158171
if (bitSet.num_blocks() > minBlocks) {
159172
std::fill(bitSet.m_bits.begin() + minBlocks, bitSet.m_bits.end(), bitset_type::block_type(0));
160173
}
174+
#endif
161175
}
162176

163177
void BitSet::_or(const BitSetPtr& set) {
178+
#if BOOST_VERSION >= 108900
179+
if (set->bitSet.size() > bitSet.size()) {
180+
resize(set->bitSet.size());
181+
bitSet |= set->bitSet;
182+
} else {
183+
bitset_type other = set->bitSet;
184+
other.resize(bitSet.size());
185+
bitSet |= other;
186+
}
187+
#else
164188
bitset_type::size_type minBlocks = std::min(bitSet.num_blocks(), set->bitSet.num_blocks());
165189
if (set->bitSet.size() > bitSet.size()) {
166190
resize(set->bitSet.size());
@@ -171,9 +195,20 @@ void BitSet::_or(const BitSetPtr& set) {
171195
if (bitSet.num_blocks() > minBlocks) {
172196
std::copy(set->bitSet.m_bits.begin() + minBlocks, set->bitSet.m_bits.end(), bitSet.m_bits.begin() + minBlocks);
173197
}
198+
#endif
174199
}
175200

176201
void BitSet::_xor(const BitSetPtr& set) {
202+
#if BOOST_VERSION >= 108900
203+
if (set->bitSet.size() > bitSet.size()) {
204+
resize(set->bitSet.size());
205+
bitSet ^= set->bitSet;
206+
} else {
207+
bitset_type other = set->bitSet;
208+
other.resize(bitSet.size());
209+
bitSet ^= other;
210+
}
211+
#else
177212
bitset_type::size_type minBlocks = std::min(bitSet.num_blocks(), set->bitSet.num_blocks());
178213
if (set->bitSet.size() > bitSet.size()) {
179214
resize(set->bitSet.size());
@@ -184,24 +219,38 @@ void BitSet::_xor(const BitSetPtr& set) {
184219
if (bitSet.num_blocks() > minBlocks) {
185220
std::copy(set->bitSet.m_bits.begin() + minBlocks, set->bitSet.m_bits.end(), bitSet.m_bits.begin() + minBlocks);
186221
}
222+
#endif
187223
}
188224

189225
void BitSet::andNot(const BitSetPtr& set) {
226+
#if BOOST_VERSION >= 108900
227+
bitset_type other = set->bitSet;
228+
other.resize(bitSet.size());
229+
bitSet &= other.flip();
230+
#else
190231
bitset_type::size_type minBlocks = std::min(bitSet.num_blocks(), set->bitSet.num_blocks());
191232
for (bitset_type::size_type i = 0; i < minBlocks; ++i) {
192233
bitSet.m_bits[i] &= ~set->bitSet.m_bits[i];
193234
}
235+
#endif
194236
}
195237

196238
bool BitSet::intersectsBitSet(const BitSetPtr& set) const {
197239
return bitSet.intersects(set->bitSet);
198240
}
199241

200242
uint32_t BitSet::cardinality() {
243+
#if BOOST_VERSION >= 108900
244+
return bitSet.count();
245+
#else
201246
return bitSet.num_blocks() == 0 ? 0 : (uint32_t)BitUtil::pop_array((int64_t*)getBits(), 0, bitSet.num_blocks());
247+
#endif
202248
}
203249

204250
void BitSet::resize(uint32_t size) {
251+
#if BOOST_VERSION >= 108900
252+
bitSet.resize(size);
253+
#else
205254
bitset_type::size_type old_num_blocks = bitSet.num_blocks();
206255
bitset_type::size_type required_blocks = bitSet.calc_num_blocks(size);
207256
if (required_blocks != old_num_blocks) {
@@ -212,6 +261,7 @@ void BitSet::resize(uint32_t size) {
212261
if (extra_bits != 0) {
213262
bitSet.m_bits.back() &= ~(~static_cast<bitset_type::block_type>(0) << extra_bits);
214263
}
264+
#endif
215265
}
216266

217267
bool BitSet::equals(const LuceneObjectPtr& other) {
@@ -224,6 +274,18 @@ bool BitSet::equals(const LuceneObjectPtr& other) {
224274
}
225275
BitSetPtr first = bitSet.num_blocks() < otherBitSet->bitSet.num_blocks() ? otherBitSet : shared_from_this();
226276
BitSetPtr second = bitSet.num_blocks() < otherBitSet->bitSet.num_blocks() ? shared_from_this() : otherBitSet;
277+
#if BOOST_VERSION >= 108900
278+
bitset_type::size_type f = first->bitSet.find_first();
279+
bitset_type::size_type s = second->bitSet.find_first();
280+
while (f == s) {
281+
if (f == bitset_type::npos) {
282+
return true;
283+
}
284+
f = first->bitSet.find_next(f);
285+
s = second->bitSet.find_next(f);
286+
}
287+
return false;
288+
#else
227289
bitset_type::size_type firstLength = first->bitSet.num_blocks();
228290
bitset_type::size_type secondLength = second->bitSet.num_blocks();
229291
for (bitset_type::size_type i = secondLength; i < firstLength; ++i) {
@@ -237,18 +299,28 @@ bool BitSet::equals(const LuceneObjectPtr& other) {
237299
}
238300
}
239301
return true;
302+
#endif
240303
}
241304

242305
int32_t BitSet::hashCode() {
243306
// Start with a zero hash and use a mix that results in zero if the input is zero.
244307
// This effectively truncates trailing zeros without an explicit check.
245308
int64_t hash = 0;
309+
#if BOOST_VERSION >= 108900
310+
to_block_range(bitSet, boost::make_function_output_iterator(
311+
[&hash](bitset_type::block_type block) {
312+
hash ^= block;
313+
hash = (hash << 1) | (hash >> 63); // rotate left
314+
}
315+
));
316+
#else
246317
uint32_t maxSize = bitSet.num_blocks();
247318
const uint64_t* bits = getBits();
248319
for (uint32_t bit = 0; bit < maxSize; ++bit) {
249320
hash ^= bits[bit];
250321
hash = (hash << 1) | (hash >> 63); // rotate left
251322
}
323+
#endif
252324
// Fold leftmost bits into right and add a constant to prevent empty sets from
253325
// returning 0, which is too common.
254326
return (int32_t)((hash >> 32) ^ hash) + 0x98761234;

0 commit comments

Comments
 (0)