550 (1 << flags::IS_COMPACT) |
551 (1 << flags::IS_READ_ONLY) |
552 (1 << flags::IS_ORDERED)
557 ptr += copy_to_mem(theta_, ptr);
559 uint32_t num_entries =
static_cast<uint32_t
>(entries_.size());
560 for (
unsigned i = 0; i < num_entries_bytes; ++i) {
561 *ptr++ = num_entries & 0xff;
565 uint64_t previous = 0;
570 for (i = 0; i + 7 < entries_.size(); i += 8) {
571 for (
unsigned j = 0; j < 8; ++j) {
572 deltas[j] = entries_[i + j] - previous;
573 previous = entries_[i + j];
575 pack_bits_block8(deltas, ptr, entry_bits);
581 for (; i < entries_.size(); ++i) {
582 const uint64_t delta = entries_[i] - previous;
583 previous = entries_[i];
584 offset = pack_bits(delta, entry_bits, ptr, offset);
591 const auto preamble_longs = read<uint8_t>(is);
592 const auto serial_version = read<uint8_t>(is);
593 const auto type = read<uint8_t>(is);
594 checker<true>::check_sketch_type(type, SKETCH_TYPE);
595 switch (serial_version) {
597 return deserialize_v4(preamble_longs, is, seed, allocator);
599 return deserialize_v3(preamble_longs, is, seed, allocator);
601 return deserialize_v1(preamble_longs, is, seed, allocator);
603 return deserialize_v2(preamble_longs, is, seed, allocator);
605 throw std::invalid_argument(
"unexpected sketch serialization version " + std::to_string(serial_version));
611 uint8_t, std::istream& is, uint64_t seed,
const A& allocator)
613 const auto seed_hash = compute_seed_hash(seed);
616 const auto num_entries = read<uint32_t>(is);
618 const auto theta = read<uint64_t>(is);
619 std::vector<uint64_t, A> entries(num_entries, 0, allocator);
621 if (!is_empty) read(is, entries.data(),
sizeof(uint64_t) * entries.size());
622 if (!is.good())
throw std::runtime_error(
"error reading from std::istream");
628 uint8_t preamble_longs, std::istream& is, uint64_t seed,
const A& allocator)
632 const uint16_t seed_hash = read<uint16_t>(is);
633 checker<true>::check_seed_hash(seed_hash, compute_seed_hash(seed));
634 if (preamble_longs == 1) {
635 if (!is.good())
throw std::runtime_error(
"error reading from std::istream");
636 std::vector<uint64_t, A> entries(0, 0, allocator);
638 }
else if (preamble_longs == 2) {
639 const uint32_t num_entries = read<uint32_t>(is);
641 std::vector<uint64_t, A> entries(num_entries, 0, allocator);
642 if (num_entries == 0) {
645 read(is, entries.data(), entries.size() *
sizeof(uint64_t));
646 if (!is.good())
throw std::runtime_error(
"error reading from std::istream");
648 }
else if (preamble_longs == 3) {
649 const uint32_t num_entries = read<uint32_t>(is);
651 const auto theta = read<uint64_t>(is);
653 std::vector<uint64_t, A> entries(num_entries, 0, allocator);
655 if (!is.good())
throw std::runtime_error(
"error reading from std::istream");
658 read(is, entries.data(),
sizeof(uint64_t) * entries.size());
659 if (!is.good())
throw std::runtime_error(
"error reading from std::istream");
663 throw std::invalid_argument(std::to_string(preamble_longs) +
" longs of premable, but expected 1, 2, or 3");
669 uint8_t preamble_longs, std::istream& is, uint64_t seed,
const A& allocator)
672 const auto flags_byte = read<uint8_t>(is);
673 const auto seed_hash = read<uint16_t>(is);
674 const bool is_empty = flags_byte & (1 << flags::IS_EMPTY);
675 if (!is_empty) checker<true>::check_seed_hash(seed_hash, compute_seed_hash(seed));
677 uint32_t num_entries = 0;
679 if (preamble_longs == 1) {
682 num_entries = read<uint32_t>(is);
684 if (preamble_longs > 2) theta = read<uint64_t>(is);
687 std::vector<uint64_t, A> entries(num_entries, 0, allocator);
688 if (!is_empty) read(is, entries.data(),
sizeof(uint64_t) * entries.size());
689 const bool is_ordered = flags_byte & (1 << flags::IS_ORDERED);
690 if (!is.good())
throw std::runtime_error(
"error reading from std::istream");
696 uint8_t preamble_longs, std::istream& is, uint64_t seed,
const A& allocator)
698 const auto entry_bits = read<uint8_t>(is);
699 compact_theta_sketch_parser<true>::check_v4_entry_bits(entry_bits);
700 const auto num_entries_bytes = read<uint8_t>(is);
701 compact_theta_sketch_parser<true>::check_v4_num_entries_bytes(num_entries_bytes);
702 const auto flags_byte = read<uint8_t>(is);
703 const auto seed_hash = read<uint16_t>(is);
704 const bool is_empty = flags_byte & (1 << flags::IS_EMPTY);
705 if (!is_empty) checker<true>::check_seed_hash(seed_hash, compute_seed_hash(seed));
707 if (preamble_longs > 1) theta = read<uint64_t>(is);
708 uint32_t num_entries = 0;
709 for (
unsigned i = 0; i < num_entries_bytes; ++i) {
710 num_entries |= read<uint8_t>(is) << (i << 3);
712 vector_bytes buffer(entry_bits, 0, allocator);
713 std::vector<uint64_t, A> entries(num_entries, 0, allocator);
717 for (i = 0; i + 7 < num_entries; i += 8) {
718 read(is, buffer.data(), buffer.size());
719 unpack_bits_block8(&entries[i], buffer.data(), entry_bits);
722 if (i < num_entries) read(is, buffer.data(), whole_bytes_to_hold_bits((num_entries - i) * entry_bits));
723 if (!is.good())
throw std::runtime_error(
"error reading from std::istream");
724 const uint8_t* ptr = buffer.data();
726 for (; i < num_entries; ++i) {
727 offset = unpack_bits(entries[i], entry_bits, ptr, offset);
730 uint64_t previous = 0;
731 for (i = 0; i < num_entries; ++i) {
732 entries[i] += previous;
733 previous = entries[i];
735 const bool is_ordered = flags_byte & (1 << flags::IS_ORDERED);
741 auto data = compact_theta_sketch_parser<true>::parse(bytes, size, seed,
false);
742 if (data.entry_bits == 64) {
743 const uint64_t* entries =
reinterpret_cast<const uint64_t*
>(data.entries_start_ptr);
745 std::vector<uint64_t, A>(entries, entries + data.num_entries, allocator));
747 std::vector<uint64_t, A> entries(data.num_entries, 0, allocator);
748 const uint8_t* ptr =
reinterpret_cast<const uint8_t*
>(data.entries_start_ptr);
751 for (i = 0; i + 7 < data.num_entries; i += 8) {
752 unpack_bits_block8(&entries[i], ptr, data.entry_bits);
753 ptr += data.entry_bits;
757 for (; i < data.num_entries; ++i) {
758 offset = unpack_bits(entries[i], data.entry_bits, ptr, offset);
761 uint64_t previous = 0;
762 for (i = 0; i < data.num_entries; ++i) {
763 entries[i] += previous;
764 previous = entries[i];
773wrapped_compact_theta_sketch_alloc<A>::wrapped_compact_theta_sketch_alloc(
const data_type& data):
779 return wrapped_compact_theta_sketch_alloc(compact_theta_sketch_parser<true>::parse(bytes, size, seed, dump_on_error));
789 return data_.is_empty;
794 return data_.is_ordered;
804 return data_.num_entries;
809 return is_empty() ? 0 : data_.seed_hash;
814 return const_iterator(data_.entries_start_ptr, data_.entry_bits, data_.num_entries, 0);
819 return const_iterator(data_.entries_start_ptr, data_.entry_bits, data_.num_entries, data_.num_entries);
823void wrapped_compact_theta_sketch_alloc<A>::print_specifics(std::ostringstream&)
const {}
826void wrapped_compact_theta_sketch_alloc<A>::print_items(std::ostringstream& os)
const {
827 os <<
"### Retained entries" << std::endl;
828 for (
const auto hash: *
this) {
829 os << hash << std::endl;
831 os <<
"### End retained entries" << std::endl;
835template<
typename Allocator>
836wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::const_iterator(
837 const void* ptr, uint8_t entry_bits, uint32_t num_entries, uint32_t index):
839entry_bits_(entry_bits),
840num_entries_(num_entries),
843is_block_mode_(num_entries_ >= 8),
846 if (entry_bits == 64) {
847 ptr_ =
reinterpret_cast<const uint64_t*
>(ptr) + index;
848 }
else if (index < num_entries) {
849 if (is_block_mode_) {
857template<
typename Allocator>
858auto wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator++() -> const_iterator& {
859 if (entry_bits_ == 64) {
860 ptr_ =
reinterpret_cast<const uint64_t*
>(ptr_) + 1;
863 if (++index_ < num_entries_) {
864 if (is_block_mode_) {
865 if ((index_ & 7) == 0) {
866 if (num_entries_ - index_ >= 8) {
869 is_block_mode_ =
false;
880template<
typename Allocator>
881void wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::unpack1() {
882 const uint32_t i = index_ & 7;
883 offset_ = unpack_bits(buffer_[i], entry_bits_,
reinterpret_cast<const uint8_t*&
>(ptr_), offset_);
884 buffer_[i] += previous_;
885 previous_ = buffer_[i];
888template<
typename Allocator>
889void wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::unpack8() {
890 unpack_bits_block8(buffer_,
reinterpret_cast<const uint8_t*
>(ptr_), entry_bits_);
891 ptr_ =
reinterpret_cast<const uint8_t*
>(ptr_) + entry_bits_;
892 for (
int i = 0; i < 8; ++i) {
893 buffer_[i] += previous_;
894 previous_ = buffer_[i];
898template<
typename Allocator>
899auto wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator++(
int) -> const_iterator {
900 const_iterator tmp(*
this);
905template<
typename Allocator>
906bool wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator!=(
const const_iterator& other)
const {
907 if (entry_bits_ == 64)
return ptr_ != other.ptr_;
908 return index_ != other.index_;
911template<
typename Allocator>
912bool wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator==(
const const_iterator& other)
const {
913 if (entry_bits_ == 64)
return ptr_ == other.ptr_;
914 return index_ == other.index_;
917template<
typename Allocator>
918auto wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator*() const -> reference {
919 if (entry_bits_ == 64)
return *
reinterpret_cast<const uint64_t*
>(ptr_);
920 return buffer_[index_ & 7];
923template<
typename Allocator>
924auto wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator->() const -> pointer {
925 if (entry_bits_ == 64)
return reinterpret_cast<const uint64_t*
>(ptr_);
926 return buffer_ + (index_ & 7);