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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
#include "coding/compressed_bit_vector.hpp"
#include "coding/write_to_sink.hpp"
#include "base/assert.hpp"
#include "base/bits.hpp"
#include "std/algorithm.hpp"
namespace coding
{
namespace
{
struct IntersectOp
{
IntersectOp() {}
unique_ptr<coding::CompressedBitVector> operator()(coding::DenseCBV const & a,
coding::DenseCBV const & b) const
{
size_t sizeA = a.NumBitGroups();
size_t sizeB = b.NumBitGroups();
vector<uint64_t> resGroups(min(sizeA, sizeB));
for (size_t i = 0; i < resGroups.size(); ++i)
resGroups[i] = a.GetBitGroup(i) & b.GetBitGroup(i);
return coding::CompressedBitVectorBuilder::FromBitGroups(move(resGroups));
}
// The intersection of dense and sparse is always sparse.
unique_ptr<coding::CompressedBitVector> operator()(coding::DenseCBV const & a,
coding::SparseCBV const & b) const
{
vector<uint64_t> resPos;
for (size_t i = 0; i < b.PopCount(); ++i)
{
auto pos = b.Select(i);
if (a.GetBit(pos))
resPos.push_back(pos);
}
return make_unique<coding::SparseCBV>(move(resPos));
}
unique_ptr<coding::CompressedBitVector> operator()(coding::SparseCBV const & a,
coding::DenseCBV const & b) const
{
return operator()(b, a);
}
unique_ptr<coding::CompressedBitVector> operator()(coding::SparseCBV const & a,
coding::SparseCBV const & b) const
{
vector<uint64_t> resPos;
set_intersection(a.Begin(), a.End(), b.Begin(), b.End(), back_inserter(resPos));
return make_unique<coding::SparseCBV>(move(resPos));
}
};
struct SubtractOp
{
SubtractOp() {}
unique_ptr<coding::CompressedBitVector> operator()(coding::DenseCBV const & a,
coding::DenseCBV const & b) const
{
size_t sizeA = a.NumBitGroups();
size_t sizeB = b.NumBitGroups();
vector<uint64_t> resGroups(min(sizeA, sizeB));
for (size_t i = 0; i < resGroups.size(); ++i)
resGroups[i] = a.GetBitGroup(i) & ~b.GetBitGroup(i);
return CompressedBitVectorBuilder::FromBitGroups(move(resGroups));
}
unique_ptr<coding::CompressedBitVector> operator()(coding::DenseCBV const & a,
coding::SparseCBV const & b) const
{
vector<uint64_t> resGroups(a.NumBitGroups());
size_t i = 0;
auto j = b.Begin();
for (; i < resGroups.size() && j < b.End(); ++i)
{
uint64_t const kBitsBegin = i * DenseCBV::kBlockSize;
uint64_t const kBitsEnd = (i + 1) * DenseCBV::kBlockSize;
uint64_t mask = 0;
for (; j < b.End() && *j < kBitsEnd; ++j)
{
ASSERT_GREATER_OR_EQUAL(*j, kBitsBegin, ());
mask |= static_cast<uint64_t>(1) << (*j - kBitsBegin);
}
resGroups[i] = a.GetBitGroup(i) & ~mask;
}
for (; i < resGroups.size(); ++i)
resGroups[i] = a.GetBitGroup(i);
return CompressedBitVectorBuilder::FromBitGroups(move(resGroups));
}
unique_ptr<coding::CompressedBitVector> operator()(coding::SparseCBV const & a,
coding::DenseCBV const & b) const
{
vector<uint64_t> resPos;
copy_if(a.Begin(), a.End(), back_inserter(resPos), [&](uint64_t bit)
{
return !b.GetBit(bit);
});
return CompressedBitVectorBuilder::FromBitPositions(move(resPos));
}
unique_ptr<coding::CompressedBitVector> operator()(coding::SparseCBV const & a,
coding::SparseCBV const & b) const
{
vector<uint64_t> resPos;
set_difference(a.Begin(), a.End(), b.Begin(), b.End(), back_inserter(resPos));
return CompressedBitVectorBuilder::FromBitPositions(move(resPos));
}
};
struct UnionOp
{
UnionOp() {}
unique_ptr<coding::CompressedBitVector> operator()(coding::DenseCBV const & a,
coding::DenseCBV const & b) const
{
size_t sizeA = a.NumBitGroups();
size_t sizeB = b.NumBitGroups();
size_t commonSize = min(sizeA, sizeB);
size_t resultSize = max(sizeA, sizeB);
vector<uint64_t> resGroups(resultSize);
for (size_t i = 0; i < commonSize; ++i)
resGroups[i] = a.GetBitGroup(i) | b.GetBitGroup(i);
if (a.NumBitGroups() == resultSize)
{
for (size_t i = commonSize; i < resultSize; ++i)
resGroups[i] = a.GetBitGroup(i);
}
else
{
for (size_t i = commonSize; i < resultSize; ++i)
resGroups[i] = b.GetBitGroup(i);
}
return CompressedBitVectorBuilder::FromBitGroups(move(resGroups));
}
unique_ptr<coding::CompressedBitVector> operator()(coding::DenseCBV const & a,
coding::SparseCBV const & b) const
{
size_t sizeA = a.NumBitGroups();
size_t sizeB = b.PopCount() == 0 ? 0 : (b.Select(b.PopCount() - 1) + DenseCBV::kBlockSize - 1) /
DenseCBV::kBlockSize;
if (sizeB > sizeA)
{
vector<uint64_t> resPos;
auto j = b.Begin();
auto merge = [&](uint64_t va)
{
while (j < b.End() && *j < va)
{
resPos.push_back(*j);
++j;
}
resPos.push_back(va);
};
a.ForEach(merge);
for (; j < b.End(); ++j)
resPos.push_back(*j);
return CompressedBitVectorBuilder::FromBitPositions(move(resPos));
}
vector<uint64_t> resGroups(sizeA);
size_t i = 0;
auto j = b.Begin();
for (; i < sizeA || j < b.End(); ++i)
{
uint64_t const kBitsBegin = i * DenseCBV::kBlockSize;
uint64_t const kBitsEnd = (i + 1) * DenseCBV::kBlockSize;
uint64_t mask = i < sizeA ? a.GetBitGroup(i) : 0;
for (; j < b.End() && *j < kBitsEnd; ++j)
{
ASSERT_GREATER_OR_EQUAL(*j, kBitsBegin, ());
mask |= static_cast<uint64_t>(1) << (*j - kBitsBegin);
}
resGroups[i] = mask;
}
return CompressedBitVectorBuilder::FromBitGroups(move(resGroups));
}
unique_ptr<coding::CompressedBitVector> operator()(coding::SparseCBV const & a,
coding::DenseCBV const & b) const
{
return operator()(b, a);
}
unique_ptr<coding::CompressedBitVector> operator()(coding::SparseCBV const & a,
coding::SparseCBV const & b) const
{
vector<uint64_t> resPos;
set_union(a.Begin(), a.End(), b.Begin(), b.End(), back_inserter(resPos));
return CompressedBitVectorBuilder::FromBitPositions(move(resPos));
}
};
template <typename TBinaryOp>
unique_ptr<coding::CompressedBitVector> Apply(TBinaryOp const & op, CompressedBitVector const & lhs,
CompressedBitVector const & rhs)
{
using strat = CompressedBitVector::StorageStrategy;
auto const stratA = lhs.GetStorageStrategy();
auto const stratB = rhs.GetStorageStrategy();
if (stratA == strat::Dense && stratB == strat::Dense)
{
DenseCBV const & a = static_cast<DenseCBV const &>(lhs);
DenseCBV const & b = static_cast<DenseCBV const &>(rhs);
return op(a, b);
}
if (stratA == strat::Dense && stratB == strat::Sparse)
{
DenseCBV const & a = static_cast<DenseCBV const &>(lhs);
SparseCBV const & b = static_cast<SparseCBV const &>(rhs);
return op(a, b);
}
if (stratA == strat::Sparse && stratB == strat::Dense)
{
SparseCBV const & a = static_cast<SparseCBV const &>(lhs);
DenseCBV const & b = static_cast<DenseCBV const &>(rhs);
return op(a, b);
}
if (stratA == strat::Sparse && stratB == strat::Sparse)
{
SparseCBV const & a = static_cast<SparseCBV const &>(lhs);
SparseCBV const & b = static_cast<SparseCBV const &>(rhs);
return op(a, b);
}
return nullptr;
}
// Returns true if a bit vector with popCount bits set out of totalBits
// is fit to be represented as a DenseCBV. Note that we do not
// account for possible irregularities in the distribution of bits.
// In particular, we do not break the bit vector into blocks that are
// stored separately although this might turn out to be a good idea.
bool DenseEnough(uint64_t popCount, uint64_t totalBits)
{
// Settle at 30% for now.
return popCount * 10 >= totalBits * 3;
}
template <typename TBitPositions>
unique_ptr<CompressedBitVector> BuildFromBitPositions(TBitPositions && setBits)
{
if (setBits.empty())
return make_unique<SparseCBV>(forward<TBitPositions>(setBits));
uint64_t const maxBit = *max_element(setBits.begin(), setBits.end());
if (DenseEnough(setBits.size(), maxBit))
return make_unique<DenseCBV>(forward<TBitPositions>(setBits));
return make_unique<SparseCBV>(forward<TBitPositions>(setBits));
}
} // namespace
// static
uint64_t const DenseCBV::kBlockSize;
DenseCBV::DenseCBV(vector<uint64_t> const & setBits)
{
if (setBits.empty())
{
return;
}
uint64_t const maxBit = *max_element(setBits.begin(), setBits.end());
size_t const sz = 1 + maxBit / kBlockSize;
m_bitGroups.resize(sz);
m_popCount = static_cast<uint64_t>(setBits.size());
for (uint64_t pos : setBits)
m_bitGroups[pos / kBlockSize] |= static_cast<uint64_t>(1) << (pos % kBlockSize);
}
// static
unique_ptr<DenseCBV> DenseCBV::BuildFromBitGroups(vector<uint64_t> && bitGroups)
{
unique_ptr<DenseCBV> cbv(new DenseCBV());
cbv->m_popCount = 0;
for (size_t i = 0; i < bitGroups.size(); ++i)
cbv->m_popCount += bits::PopCount(bitGroups[i]);
cbv->m_bitGroups = move(bitGroups);
return cbv;
}
uint64_t DenseCBV::GetBitGroup(size_t i) const
{
return i < m_bitGroups.size() ? m_bitGroups[i] : 0;
}
uint64_t DenseCBV::PopCount() const { return m_popCount; }
bool DenseCBV::GetBit(uint64_t pos) const
{
uint64_t bitGroup = GetBitGroup(pos / kBlockSize);
return ((bitGroup >> (pos % kBlockSize)) & 1) > 0;
}
unique_ptr<CompressedBitVector> DenseCBV::LeaveFirstSetNBits(uint64_t n) const
{
if (PopCount() <= n)
return Clone();
vector<uint64_t> groups;
for (size_t i = 0; i < m_bitGroups.size() && n != 0; ++i)
{
uint64_t group = m_bitGroups[i];
uint32_t const bits = bits::PopCount(group);
if (bits <= n)
{
n -= bits;
groups.push_back(group);
}
else
{
uint64_t part = 0;
while (n != 0)
{
part = part | (group & -group);
group = group & (group - 1);
--n;
}
groups.push_back(part);
}
}
return CompressedBitVectorBuilder::FromBitGroups(move(groups));
}
CompressedBitVector::StorageStrategy DenseCBV::GetStorageStrategy() const
{
return CompressedBitVector::StorageStrategy::Dense;
}
void DenseCBV::Serialize(Writer & writer) const
{
uint8_t header = static_cast<uint8_t>(GetStorageStrategy());
WriteToSink(writer, header);
rw::WriteVectorOfPOD(writer, m_bitGroups);
}
unique_ptr<CompressedBitVector> DenseCBV::Clone() const
{
DenseCBV * cbv = new DenseCBV();
cbv->m_popCount = m_popCount;
cbv->m_bitGroups = m_bitGroups;
return unique_ptr<CompressedBitVector>(cbv);
}
SparseCBV::SparseCBV(vector<uint64_t> const & setBits) : m_positions(setBits)
{
ASSERT(is_sorted(m_positions.begin(), m_positions.end()), ());
}
SparseCBV::SparseCBV(vector<uint64_t> && setBits) : m_positions(move(setBits))
{
ASSERT(is_sorted(m_positions.begin(), m_positions.end()), ());
}
uint64_t SparseCBV::Select(size_t i) const
{
ASSERT_LESS(i, m_positions.size(), ());
return m_positions[i];
}
uint64_t SparseCBV::PopCount() const { return m_positions.size(); }
bool SparseCBV::GetBit(uint64_t pos) const
{
return binary_search(m_positions.begin(), m_positions.end(), pos);
}
unique_ptr<CompressedBitVector> SparseCBV::LeaveFirstSetNBits(uint64_t n) const
{
if (PopCount() <= n)
return Clone();
vector<uint64_t> positions(m_positions.begin(), m_positions.begin() + n);
return CompressedBitVectorBuilder::FromBitPositions(move(positions));
}
CompressedBitVector::StorageStrategy SparseCBV::GetStorageStrategy() const
{
return CompressedBitVector::StorageStrategy::Sparse;
}
void SparseCBV::Serialize(Writer & writer) const
{
uint8_t header = static_cast<uint8_t>(GetStorageStrategy());
WriteToSink(writer, header);
rw::WriteVectorOfPOD(writer, m_positions);
}
unique_ptr<CompressedBitVector> SparseCBV::Clone() const
{
SparseCBV * cbv = new SparseCBV();
cbv->m_positions = m_positions;
return unique_ptr<CompressedBitVector>(cbv);
}
// static
unique_ptr<CompressedBitVector> CompressedBitVectorBuilder::FromBitPositions(
vector<uint64_t> const & setBits)
{
return BuildFromBitPositions(setBits);
}
// static
unique_ptr<CompressedBitVector> CompressedBitVectorBuilder::FromBitPositions(
vector<uint64_t> && setBits)
{
return BuildFromBitPositions(move(setBits));
}
// static
unique_ptr<CompressedBitVector> CompressedBitVectorBuilder::FromBitGroups(
vector<uint64_t> && bitGroups)
{
static uint64_t const kBlockSize = DenseCBV::kBlockSize;
while (!bitGroups.empty() && bitGroups.back() == 0)
bitGroups.pop_back();
if (bitGroups.empty())
return make_unique<SparseCBV>(move(bitGroups));
uint64_t const maxBit = kBlockSize * (bitGroups.size() - 1) + bits::FloorLog(bitGroups.back());
uint64_t popCount = 0;
for (size_t i = 0; i < bitGroups.size(); ++i)
popCount += bits::PopCount(bitGroups[i]);
if (DenseEnough(popCount, maxBit))
return DenseCBV::BuildFromBitGroups(move(bitGroups));
vector<uint64_t> setBits;
for (size_t i = 0; i < bitGroups.size(); ++i)
{
for (size_t j = 0; j < kBlockSize; ++j)
{
if (((bitGroups[i] >> j) & 1) > 0)
setBits.push_back(kBlockSize * i + j);
}
}
return make_unique<SparseCBV>(setBits);
}
string DebugPrint(CompressedBitVector::StorageStrategy strat)
{
switch (strat)
{
case CompressedBitVector::StorageStrategy::Dense: return "Dense";
case CompressedBitVector::StorageStrategy::Sparse: return "Sparse";
}
}
// static
unique_ptr<CompressedBitVector> CompressedBitVector::Intersect(CompressedBitVector const & lhs,
CompressedBitVector const & rhs)
{
static IntersectOp const op;
return Apply(op, lhs, rhs);
}
// static
unique_ptr<CompressedBitVector> CompressedBitVector::Subtract(CompressedBitVector const & lhs,
CompressedBitVector const & rhs)
{
static SubtractOp const op;
return Apply(op, lhs, rhs);
}
// static
unique_ptr<CompressedBitVector> CompressedBitVector::Union(CompressedBitVector const & lhs,
CompressedBitVector const & rhs)
{
static UnionOp const op;
return Apply(op, lhs, rhs);
}
// static
bool CompressedBitVector::IsEmpty(unique_ptr<CompressedBitVector> const & cbv)
{
return !cbv || cbv->PopCount() == 0;
}
// static
bool CompressedBitVector::IsEmpty(CompressedBitVector const * cbv)
{
return !cbv || cbv->PopCount() == 0;
}
} // namespace coding
|