#include "xcdat/FitVector.hpp" namespace xcdat { FitVector::FitVector(std::istream& is) { chunks_ = Vector(is); size_= read_value(is); width_ = read_value(is); mask_ = read_value(is); } FitVector::FitVector(const std::vector& values) { if (values.empty()) { return; } width_ = 0; auto max_value = *std::max_element(std::begin(values), std::end(values)); do { ++width_; max_value >>= 1; } while (max_value); size_ = values.size(); mask_ = (1U << width_) - 1; std::vector chunks(size_ * width_ / CHUNK_WIDTH + 1, 0); for (id_type i = 0; i < size_; ++i) { const auto chunk_pos = static_cast(i * width_ / CHUNK_WIDTH); const auto offset = static_cast(i * width_ % CHUNK_WIDTH); chunks[chunk_pos] &= ~(mask_ << offset); chunks[chunk_pos] |= (values[i] & mask_) << offset; if (CHUNK_WIDTH < offset + width_) { chunks[chunk_pos + 1] &= ~(mask_ >> (CHUNK_WIDTH - offset)); chunks[chunk_pos + 1] |= (values[i] & mask_) >> (CHUNK_WIDTH - offset); } } chunks_ = Vector(chunks); } size_t FitVector::size_in_bytes() const { size_t ret = 0; ret += chunks_.size_in_bytes(); ret += sizeof(size_); ret += sizeof(width_); ret += sizeof(mask_); return ret; } void FitVector::write(std::ostream& os) const { chunks_.write(os); write_value(size_, os); write_value(width_, os); write_value(mask_, os); } } //namespace - xcdat