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
|
#include "coding/compressed_bit_vector.hpp"
#include "coding/writer.hpp"
#include "coding/write_to_sink.hpp"
#include "base/bits.hpp"
#include "std/algorithm.hpp"
namespace coding
{
// static
uint32_t const DenseCBV::kBlockSize;
} // namespace coding
namespace
{
uint64_t const kBlockSize = coding::DenseCBV::kBlockSize;
unique_ptr<coding::CompressedBitVector> IntersectImpl(coding::DenseCBV const & a,
coding::DenseCBV const & b)
{
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> IntersectImpl(coding::DenseCBV const & a,
coding::SparseCBV const & b)
{
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> IntersectImpl(coding::SparseCBV const & a,
coding::DenseCBV const & b)
{
return IntersectImpl(b, a);
}
unique_ptr<coding::CompressedBitVector> IntersectImpl(coding::SparseCBV const & a,
coding::SparseCBV const & b)
{
size_t sizeA = a.PopCount();
size_t sizeB = b.PopCount();
vector<uint64_t> resPos;
size_t i = 0;
size_t j = 0;
while (i < sizeA && j < sizeB)
{
auto posA = a.Select(i);
auto posB = b.Select(j);
if (posA == posB)
{
resPos.push_back(posA);
++i;
++j;
}
else if (posA < posB)
{
++i;
}
else
{
++j;
}
}
return make_unique<coding::SparseCBV>(move(resPos));
}
// 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;
}
} // namespace
namespace coding
{
DenseCBV::DenseCBV(vector<uint64_t> const & setBits)
{
if (setBits.empty())
{
return;
}
uint64_t maxBit = setBits[0];
for (size_t i = 1; i < setBits.size(); ++i)
maxBit = max(maxBit, setBits[i]);
size_t const sz = 1 + maxBit / kBlockSize;
m_bitGroups.resize(sz);
m_popCount = static_cast<uint32_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;
}
uint32_t DenseCBV::PopCount() const { return m_popCount; }
bool DenseCBV::GetBit(uint32_t pos) const
{
uint64_t bitGroup = GetBitGroup(pos / kBlockSize);
return ((bitGroup >> (pos % kBlockSize)) & 1) > 0;
}
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);
WriteToSink(writer, static_cast<uint32_t>(NumBitGroups()));
for (size_t i = 0; i < NumBitGroups(); ++i)
WriteToSink(writer, GetBitGroup(i));
}
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];
}
uint32_t SparseCBV::PopCount() const { return m_positions.size(); }
bool SparseCBV::GetBit(uint32_t pos) const
{
auto const it = lower_bound(m_positions.begin(), m_positions.end(), pos);
return it != m_positions.end() && *it == pos;
}
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);
WriteToSink(writer, PopCount());
ForEach([&](uint64_t bitPos)
{
WriteToSink(writer, bitPos);
});
}
// static
unique_ptr<CompressedBitVector> CompressedBitVectorBuilder::FromBitPositions(
vector<uint64_t> const & setBits)
{
if (setBits.empty())
return make_unique<SparseCBV>(setBits);
uint64_t maxBit = setBits[0];
for (size_t i = 1; i < setBits.size(); ++i)
maxBit = max(maxBit, setBits[i]);
if (DenseEnough(setBits.size(), maxBit))
return make_unique<DenseCBV>(setBits);
return make_unique<SparseCBV>(setBits);
}
// static
unique_ptr<CompressedBitVector> CompressedBitVectorBuilder::FromBitGroups(
vector<uint64_t> && bitGroups)
{
while (!bitGroups.empty() && bitGroups.back() == 0)
bitGroups.pop_back();
if (bitGroups.empty())
return make_unique<SparseCBV>(bitGroups);
uint64_t const maxBit = kBlockSize * bitGroups.size() - 1;
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)
{
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 IntersectImpl(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 IntersectImpl(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 IntersectImpl(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 IntersectImpl(a, b);
}
return nullptr;
}
} // namespace coding
|