2017-03-29 06:01:06 +00:00
|
|
|
#include <sstream>
|
|
|
|
|
2017-11-12 11:49:13 +00:00
|
|
|
#include "xcdat/DacBc.hpp"
|
2016-12-03 07:51:00 +00:00
|
|
|
|
|
|
|
namespace xcdat {
|
|
|
|
|
2017-07-12 06:48:49 +00:00
|
|
|
DacBc::DacBc(std::istream& is) {
|
|
|
|
for (size_t i = 0; i < sizeof(id_type); ++i) {
|
|
|
|
values_[i] = Vector<uint8_t>(is);
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < sizeof(id_type) - 1; ++i) {
|
|
|
|
flags_[i] = BitVector(is);
|
|
|
|
}
|
|
|
|
leaf_flags_ = BitVector(is);
|
|
|
|
links_ = FitVector(is);
|
|
|
|
max_level_ = read_value<uint8_t>(is);
|
|
|
|
num_free_nodes_ = read_value<size_t>(is);
|
|
|
|
}
|
|
|
|
|
2017-03-29 06:01:06 +00:00
|
|
|
DacBc::DacBc(const std::vector<BcPair>& bc, BitVectorBuilder& leaf_flags) {
|
2016-12-03 07:51:00 +00:00
|
|
|
if (bc.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-12 06:48:49 +00:00
|
|
|
std::vector<uint8_t> values[sizeof(id_type)];
|
|
|
|
BitVectorBuilder flags[sizeof(id_type)];
|
2017-03-29 06:01:06 +00:00
|
|
|
std::vector<id_type> links;
|
|
|
|
|
2017-07-12 06:48:49 +00:00
|
|
|
leaf_flags_ = BitVector(leaf_flags, true, false);
|
2016-12-03 07:51:00 +00:00
|
|
|
|
2017-03-29 06:01:06 +00:00
|
|
|
values[0].reserve(bc.size() * 2);
|
2016-12-03 07:51:00 +00:00
|
|
|
flags[0].reserve(bc.size() * 2);
|
|
|
|
links.reserve(bc.size());
|
|
|
|
|
|
|
|
max_level_ = 0;
|
|
|
|
|
2017-03-29 06:01:06 +00:00
|
|
|
auto append = [&](id_type value) {
|
2016-12-03 07:51:00 +00:00
|
|
|
uint8_t level = 0;
|
2017-03-29 06:01:06 +00:00
|
|
|
values[level].push_back(static_cast<uint8_t>(value & 0xFF));
|
2016-12-03 07:51:00 +00:00
|
|
|
flags[level].push_back(true);
|
|
|
|
value >>= 8;
|
|
|
|
while (value) {
|
|
|
|
++level;
|
2017-03-29 06:01:06 +00:00
|
|
|
values[level].push_back(static_cast<uint8_t>(value & 0xFF));
|
2016-12-03 07:51:00 +00:00
|
|
|
flags[level].push_back(true);
|
|
|
|
value >>= 8;
|
|
|
|
}
|
|
|
|
flags[level].set_bit(flags[level].size() - 1, false);
|
|
|
|
max_level_ = std::max(max_level_, level);
|
|
|
|
};
|
|
|
|
|
2017-03-29 06:01:06 +00:00
|
|
|
auto append_leaf = [&](id_type value) {
|
|
|
|
links.push_back(value >> 8);
|
|
|
|
values[0].push_back(static_cast<uint8_t>(value & 0xFF));
|
|
|
|
flags[0].push_back(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
for (id_type i = 0; i < bc.size(); ++i) {
|
|
|
|
if (leaf_flags_[i]) {
|
|
|
|
append_leaf(bc[i].base);
|
2016-12-03 07:51:00 +00:00
|
|
|
} else {
|
2017-03-29 06:01:06 +00:00
|
|
|
append(bc[i].base ^ i);
|
|
|
|
}
|
|
|
|
append(bc[i].check ^ i);
|
|
|
|
if (bc[i].check == i) {
|
|
|
|
++num_free_nodes_;
|
2016-12-03 07:51:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// release
|
|
|
|
for (uint8_t i = 0; i < max_level_; ++i) {
|
2017-07-12 06:48:49 +00:00
|
|
|
values_[i] = Vector<uint8_t>(values[i]);
|
|
|
|
flags_[i] = BitVector(flags[i], true, false);
|
2016-12-03 07:51:00 +00:00
|
|
|
}
|
2017-07-12 06:48:49 +00:00
|
|
|
values_[max_level_] = Vector<uint8_t>(values[max_level_]);
|
|
|
|
links_ = FitVector(links);
|
2016-12-03 07:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t DacBc::size_in_bytes() const {
|
|
|
|
size_t ret = 0;
|
|
|
|
for (auto& values : values_) {
|
2017-03-29 06:01:06 +00:00
|
|
|
ret += values.size_in_bytes();
|
2016-12-03 07:51:00 +00:00
|
|
|
}
|
|
|
|
for (auto& flags : flags_) {
|
|
|
|
ret += flags.size_in_bytes();
|
|
|
|
}
|
2017-03-29 06:01:06 +00:00
|
|
|
ret += leaf_flags_.size_in_bytes();
|
2016-12-03 07:51:00 +00:00
|
|
|
ret += links_.size_in_bytes();
|
|
|
|
ret += sizeof(max_level_);
|
|
|
|
ret += sizeof(num_free_nodes_);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void DacBc::show_stat(std::ostream& os) const {
|
|
|
|
const auto total_size = size_in_bytes();
|
|
|
|
os << "basic statistics of xcdat::DacBc" << std::endl;
|
2016-12-04 06:53:02 +00:00
|
|
|
show_size("\tnum links: ", links_.size(), os);
|
|
|
|
show_size("\tbytes per node:", double(total_size) / num_nodes(), os);
|
2016-12-03 07:51:00 +00:00
|
|
|
os << "member size statistics of xcdat::DacBc" << std::endl;
|
2017-03-29 06:01:06 +00:00
|
|
|
for (int i = 0; i <= max_level_; ++i) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "\tvalues_L" << i << ":";
|
|
|
|
show_size_ratio(oss.str().c_str(), values_[i].size_in_bytes(), total_size, os);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < max_level_; ++i) {
|
|
|
|
std::ostringstream oss;
|
|
|
|
oss << "\tflags_L" << i << ": ";
|
|
|
|
show_size_ratio(oss.str().c_str(), flags_[i].size_in_bytes(), total_size, os);
|
|
|
|
}
|
|
|
|
show_size_ratio("\tleaves: ", leaf_flags_.size_in_bytes(), total_size, os);
|
|
|
|
show_size_ratio("\tlinks: ", links_.size_in_bytes(), total_size, os);
|
2016-12-03 07:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DacBc::write(std::ostream& os) const {
|
|
|
|
for (auto& values : values_) {
|
2017-03-29 06:01:06 +00:00
|
|
|
values.write(os);
|
2016-12-03 07:51:00 +00:00
|
|
|
}
|
|
|
|
for (auto& flags : flags_) {
|
|
|
|
flags.write(os);
|
|
|
|
}
|
2017-03-29 06:01:06 +00:00
|
|
|
leaf_flags_.write(os);
|
2016-12-03 07:51:00 +00:00
|
|
|
links_.write(os);
|
2016-12-04 06:53:02 +00:00
|
|
|
write_value(max_level_, os);
|
|
|
|
write_value(num_free_nodes_, os);
|
2016-12-03 07:51:00 +00:00
|
|
|
}
|
|
|
|
|
2017-03-29 06:01:06 +00:00
|
|
|
id_type DacBc::access_(id_type i) const {
|
2016-12-03 07:51:00 +00:00
|
|
|
uint8_t level = 0;
|
2017-03-29 06:01:06 +00:00
|
|
|
id_type value = values_[level][i];
|
2016-12-03 07:51:00 +00:00
|
|
|
while (level < max_level_) {
|
|
|
|
if (!flags_[level][i]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i = flags_[level].rank(i);
|
|
|
|
++level;
|
2017-03-29 06:01:06 +00:00
|
|
|
value |= static_cast<id_type>(values_[level][i]) << (level * 8);
|
2016-12-03 07:51:00 +00:00
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
} //namespace - xcdat
|