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 0x0F0F0F0F;
bits += bits >> 8;
bits += bits >> 16;
return bits & 0x3F;
}
2017-03-29 06:01:06 +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-03-29 06:01:06 +00:00
bits_.steal(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) {
std::vector<RankTip> rank_tips(size_ / kBitsInR1 + 1);
id_type count = 0;
for (id_type i = 0; i < rank_tips.size(); ++i) {
auto& tip = rank_tips[i];
tip.L1 = count;
for (id_type offset = 0; offset < kR1PerR2; ++offset) {
tip.L2[offset] = static_cast<uint8_t>(count - tip.L1);
auto pos_in_bits = i * kR1PerR2 + offset;
if (pos_in_bits < bits_.size()) {
count += pop_count(bits_[pos_in_bits]);
}
2016-12-03 07:51:00 +00:00
}
}
2017-03-29 06:01:06 +00:00
rank_tips_.steal(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};
auto count = kNum1sPerTip;
for (id_type i = 0; i < rank_tips_.size(); ++i) {
if (count < rank_tips_[i].L1) {
select_tips.push_back(i - 1);
count += kNum1sPerTip;
}
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));
select_tips_.steal(select_tips);
2016-12-03 07:51:00 +00:00
}
}
2017-03-29 06:01:06 +00:00
id_type BitVector::rank(size_t i) const {
2016-12-03 07:51:00 +00:00
auto& hint = rank_tips_[i / kBitsInR1];
2017-03-29 06:01:06 +00:00
return hint.L1 + hint.L2[i / kBitsInR2 % kR1PerR2]
2016-12-03 07:51:00 +00:00
+ pop_count(bits_[i / 32] & ((1U << (i % 32)) - 1));
}
2017-03-29 06:01:06 +00:00
id_type BitVector::select(size_t i) const {
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()) {
id_type select_tip_id = i / kNum1sPerTip;
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;
for (; offset < kR1PerR2; ++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
auto ret = (left * kBitsInR1) + (offset * kBitsInR2);
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;
}
}
ret += kSelectTable[i][bits % 256];
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
}
void BitVector::read(std::istream& is) {
2017-03-29 06:01:06 +00:00
bits_.read(is);
rank_tips_.read(is);
select_tips_.read(is);
2016-12-04 06:53:02 +00:00
read_value(size_, is);
read_value(num_1s_, is);
2016-12-03 07:51:00 +00:00
}
void BitVector::swap(BitVector& rhs) {
bits_.swap(rhs.bits_);
rank_tips_.swap(rhs.rank_tips_);
select_tips_.swap(rhs.select_tips_);
std::swap(size_, rhs.size_);
std::swap(num_1s_, rhs.num_1s_);
}
} //namespace - xcdat