xcdat/lib/BitVector.cpp

315 lines
11 KiB
C++
Raw Normal View History

2017-07-12 06:48:49 +00:00
#include <popcntintrin.h>
2017-11-12 11:49:13 +00:00
#include "xcdat/BitVector.hpp"
2016-12-03 07:51:00 +00:00
namespace xcdat {
// inspired by marisa-trie
2017-11-12 11:49:13 +00:00
constexpr uint8_t SELECT_TABLE[9][256] = {
2016-12-03 07:51:00 +00:00
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
},
{
8, 1, 2, 1, 3, 1, 2, 1, 4, 1, 2, 1, 3, 1, 2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int32_t pop_count(uint32_t x) {
2017-07-12 06:48:49 +00:00
#ifdef XCDAT_USE_POPCNT
2017-11-12 11:49:13 +00:00
return static_cast<uint32_t>(_mm_popcnt_u32(x));
2017-07-12 06:48:49 +00:00
#else
2017-11-12 11:49:13 +00:00
x = ((x & 0xAAAAAAAA) >> 1) + (x & 0x55555555);
x = ((x & 0xCCCCCCCC) >> 2) + (x & 0x33333333);
x = ((x >> 4) + x) & 0x0F0F0F0F;
x += x >> 8;
x += x >> 16;
return x & 0x3F;
2017-07-12 06:48:49 +00:00
#endif
}
BitVector::BitVector(std::istream& is) {
bits_ = Vector<uint32_t>(is);
rank_tips_ = Vector<RankTip>(is);
select_tips_ = Vector<id_type>(is);
size_ = read_value<size_t>(is);
num_1s_ = read_value<size_t>(is);
2016-12-03 07:51:00 +00:00
}
2017-11-04 14:06:08 +00:00
BitVector::BitVector(BitVectorBuilder& builder, bool rank_flag,
bool select_flag) {
2016-12-03 07:51:00 +00:00
if (!builder.size()) {
return;
}
2017-07-12 06:48:49 +00:00
bits_ = Vector<uint32_t>(builder.bits_);
2016-12-03 07:51:00 +00:00
size_ = builder.size_;
2017-03-29 06:01:06 +00:00
num_1s_ = builder.num_1s_;
2016-12-03 07:51:00 +00:00
// builds rank_tips_
2017-03-29 06:01:06 +00:00
if (rank_flag) {
2017-11-12 11:49:13 +00:00
std::vector<RankTip> rank_tips(size_ / BITS_IN_R1 + 1);
2017-03-29 06:01:06 +00:00
id_type count = 0;
for (id_type i = 0; i < rank_tips.size(); ++i) {
auto& tip = rank_tips[i];
tip.L1 = count;
2017-11-12 11:49:13 +00:00
for (id_type offset = 0; offset < R1_PER_R2; ++offset) {
2017-03-29 06:01:06 +00:00
tip.L2[offset] = static_cast<uint8_t>(count - tip.L1);
2017-11-12 11:49:13 +00:00
auto pos_in_bits = i * R1_PER_R2 + offset;
2017-03-29 06:01:06 +00:00
if (pos_in_bits < bits_.size()) {
count += pop_count(bits_[pos_in_bits]);
}
2016-12-03 07:51:00 +00:00
}
}
2017-07-12 06:48:49 +00:00
rank_tips_ = Vector<RankTip>(rank_tips);
2016-12-03 07:51:00 +00:00
}
// builds select_tips_
2017-03-29 06:01:06 +00:00
if (rank_flag && select_flag) {
std::vector<id_type> select_tips{0};
2017-11-12 11:49:13 +00:00
auto count = ONES_PER_TIP;
2017-03-29 06:01:06 +00:00
for (id_type i = 0; i < rank_tips_.size(); ++i) {
if (count < rank_tips_[i].L1) {
select_tips.push_back(i - 1);
2017-11-12 11:49:13 +00:00
count += ONES_PER_TIP;
2017-03-29 06:01:06 +00:00
}
2016-12-03 07:51:00 +00:00
}
2017-03-29 06:01:06 +00:00
select_tips.push_back(static_cast<id_type>(rank_tips_.size() - 1));
2017-07-12 06:48:49 +00:00
select_tips_ = Vector<id_type>(select_tips);
2016-12-03 07:51:00 +00:00
}
}
2017-11-11 12:01:10 +00:00
id_type BitVector::rank(id_type i) const {
2017-11-12 11:49:13 +00:00
auto& hint = rank_tips_[i / BITS_IN_R1];
return hint.L1 + hint.L2[i / BITS_IN_R2 % R1_PER_R2]
2016-12-03 07:51:00 +00:00
+ pop_count(bits_[i / 32] & ((1U << (i % 32)) - 1));
}
2017-11-11 12:01:10 +00:00
id_type BitVector::select(id_type i) const {
2017-03-29 06:01:06 +00:00
id_type left = 0, right = static_cast<id_type>(rank_tips_.size());
2016-12-03 07:51:00 +00:00
2017-03-29 06:01:06 +00:00
if (!select_tips_.is_empty()) {
2017-11-12 11:49:13 +00:00
auto select_tip_id = static_cast<id_type>(i / ONES_PER_TIP);
2016-12-03 07:51:00 +00:00
left = select_tips_[select_tip_id];
right = select_tips_[select_tip_id + 1] + 1;
}
while (left + 1 < right) {
const auto center = (left + right) / 2;
2017-03-29 06:01:06 +00:00
if (i < rank_tips_[center].L1) {
2016-12-03 07:51:00 +00:00
right = center;
} else {
left = center;
}
}
i += 1; // for i+1 th
2017-03-29 06:01:06 +00:00
i -= rank_tips_[left].L1;
2016-12-03 07:51:00 +00:00
uint32_t offset = 1;
2017-11-12 11:49:13 +00:00
for (; offset < R1_PER_R2; ++offset) {
2017-03-29 06:01:06 +00:00
if (i <= rank_tips_[left].L2[offset]) {
2016-12-03 07:51:00 +00:00
break;
}
}
2017-03-29 06:01:06 +00:00
i -= rank_tips_[left].L2[--offset];
2016-12-03 07:51:00 +00:00
2017-11-12 11:49:13 +00:00
auto ret = (left * BITS_IN_R1) + (offset * BITS_IN_R2);
2016-12-03 07:51:00 +00:00
auto bits = bits_[ret / 32];
{
auto _count = pop_count(bits % 65536);
if (_count < i) {
bits >>= 16;
ret += 16;
i -= _count;
}
}
{
auto _count = pop_count(bits % 256);
if (_count < i) {
bits >>= 8;
ret += 8;
i -= _count;
}
}
2017-11-12 11:49:13 +00:00
ret += SELECT_TABLE[i][bits % 256];
2016-12-03 07:51:00 +00:00
return ret - 1;
}
size_t BitVector::size_in_bytes() const {
size_t ret = 0;
2017-03-29 06:01:06 +00:00
ret += bits_.size_in_bytes();
ret += rank_tips_.size_in_bytes();
ret += select_tips_.size_in_bytes();
2016-12-03 07:51:00 +00:00
ret += sizeof(size_);
ret += sizeof(num_1s_);
return ret;
}
void BitVector::write(std::ostream& os) const {
2017-03-29 06:01:06 +00:00
bits_.write(os);
rank_tips_.write(os);
select_tips_.write(os);
2016-12-04 06:53:02 +00:00
write_value(size_, os);
write_value(num_1s_, os);
2016-12-03 07:51:00 +00:00
}
} //namespace - xcdat