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)
671 const auto unused16 = read<uint16_t>(is);
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);
676 const uint64_t pre0 =
static_cast<uint64_t
>(preamble_longs)
677 | (
static_cast<uint64_t
>(UNCOMPRESSED_SERIAL_VERSION) << 8)
678 | (
static_cast<uint64_t
>(SKETCH_TYPE) << 16)
679 | (
static_cast<uint64_t
>(unused16) << 24)
680 | (
static_cast<uint64_t
>(flags_byte) << 40)
681 | (
static_cast<uint64_t
>(seed_hash) << 48);
682 compact_theta_sketch_parser<true>::check_empty_v3(pre0, compute_seed_hash(seed));
683 if (preamble_longs == 3) {
684 const auto num_entries = read<uint32_t>(is);
687 if (num_entries != 0)
throw std::invalid_argument(
"legacy empty compact sketch has entries");
690 checker<true>::check_seed_hash(seed_hash, compute_seed_hash(seed));
693 uint32_t num_entries = 0;
695 if (preamble_longs == 1) {
698 num_entries = read<uint32_t>(is);
700 if (preamble_longs > 2) theta = read<uint64_t>(is);
703 std::vector<uint64_t, A> entries(num_entries, 0, allocator);
704 if (!is_empty) read(is, entries.data(),
sizeof(uint64_t) * entries.size());
705 const bool is_ordered = flags_byte & (1 << flags::IS_ORDERED);
706 if (!is.good())
throw std::runtime_error(
"error reading from std::istream");
712 uint8_t preamble_longs, std::istream& is, uint64_t seed,
const A& allocator)
714 const auto entry_bits = read<uint8_t>(is);
715 compact_theta_sketch_parser<true>::check_v4_entry_bits(entry_bits);
716 const auto num_entries_bytes = read<uint8_t>(is);
717 compact_theta_sketch_parser<true>::check_v4_num_entries_bytes(num_entries_bytes);
718 const auto flags_byte = read<uint8_t>(is);
719 const auto seed_hash = read<uint16_t>(is);
720 const bool is_empty = flags_byte & (1 << flags::IS_EMPTY);
721 if (!is_empty) checker<true>::check_seed_hash(seed_hash, compute_seed_hash(seed));
723 if (preamble_longs > 1) theta = read<uint64_t>(is);
724 uint32_t num_entries = 0;
725 for (
unsigned i = 0; i < num_entries_bytes; ++i) {
726 num_entries |= read<uint8_t>(is) << (i << 3);
728 vector_bytes buffer(entry_bits, 0, allocator);
729 std::vector<uint64_t, A> entries(num_entries, 0, allocator);
733 for (i = 0; i + 7 < num_entries; i += 8) {
734 read(is, buffer.data(), buffer.size());
735 unpack_bits_block8(&entries[i], buffer.data(), entry_bits);
738 if (i < num_entries) read(is, buffer.data(), whole_bytes_to_hold_bits((num_entries - i) * entry_bits));
739 if (!is.good())
throw std::runtime_error(
"error reading from std::istream");
740 const uint8_t* ptr = buffer.data();
742 for (; i < num_entries; ++i) {
743 offset = unpack_bits(entries[i], entry_bits, ptr, offset);
746 uint64_t previous = 0;
747 for (i = 0; i < num_entries; ++i) {
748 entries[i] += previous;
749 previous = entries[i];
751 const bool is_ordered = flags_byte & (1 << flags::IS_ORDERED);
757 auto data = compact_theta_sketch_parser<true>::parse(bytes, size, seed,
false);
758 if (data.entry_bits == 64) {
759 const uint64_t* entries =
reinterpret_cast<const uint64_t*
>(data.entries_start_ptr);
761 std::vector<uint64_t, A>(entries, entries + data.num_entries, allocator));
763 std::vector<uint64_t, A> entries(data.num_entries, 0, allocator);
764 const uint8_t* ptr =
reinterpret_cast<const uint8_t*
>(data.entries_start_ptr);
767 for (i = 0; i + 7 < data.num_entries; i += 8) {
768 unpack_bits_block8(&entries[i], ptr, data.entry_bits);
769 ptr += data.entry_bits;
773 for (; i < data.num_entries; ++i) {
774 offset = unpack_bits(entries[i], data.entry_bits, ptr, offset);
777 uint64_t previous = 0;
778 for (i = 0; i < data.num_entries; ++i) {
779 entries[i] += previous;
780 previous = entries[i];
789wrapped_compact_theta_sketch_alloc<A>::wrapped_compact_theta_sketch_alloc(
const data_type& data):
795 return wrapped_compact_theta_sketch_alloc(compact_theta_sketch_parser<true>::parse(bytes, size, seed, dump_on_error));
805 return data_.is_empty;
810 return data_.is_ordered;
820 return data_.num_entries;
825 return is_empty() ? 0 : data_.seed_hash;
830 return const_iterator(data_.entries_start_ptr, data_.entry_bits, data_.num_entries, 0);
835 return const_iterator(data_.entries_start_ptr, data_.entry_bits, data_.num_entries, data_.num_entries);
839void wrapped_compact_theta_sketch_alloc<A>::print_specifics(std::ostringstream&)
const {}
842void wrapped_compact_theta_sketch_alloc<A>::print_items(std::ostringstream& os)
const {
843 os <<
"### Retained entries" << std::endl;
844 for (
const auto hash: *
this) {
845 os << hash << std::endl;
847 os <<
"### End retained entries" << std::endl;
851template<
typename Allocator>
852wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::const_iterator(
853 const void* ptr, uint8_t entry_bits, uint32_t num_entries, uint32_t index):
855entry_bits_(entry_bits),
856num_entries_(num_entries),
859is_block_mode_(num_entries_ >= 8),
862 if (entry_bits == 64) {
863 ptr_ =
reinterpret_cast<const uint64_t*
>(ptr) + index;
864 }
else if (index < num_entries) {
865 if (is_block_mode_) {
873template<
typename Allocator>
874auto wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator++() -> const_iterator& {
875 if (entry_bits_ == 64) {
876 ptr_ =
reinterpret_cast<const uint64_t*
>(ptr_) + 1;
879 if (++index_ < num_entries_) {
880 if (is_block_mode_) {
881 if ((index_ & 7) == 0) {
882 if (num_entries_ - index_ >= 8) {
885 is_block_mode_ =
false;
896template<
typename Allocator>
897void wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::unpack1() {
898 const uint32_t i = index_ & 7;
899 offset_ = unpack_bits(buffer_[i], entry_bits_,
reinterpret_cast<const uint8_t*&
>(ptr_), offset_);
900 buffer_[i] += previous_;
901 previous_ = buffer_[i];
904template<
typename Allocator>
905void wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::unpack8() {
906 unpack_bits_block8(buffer_,
reinterpret_cast<const uint8_t*
>(ptr_), entry_bits_);
907 ptr_ =
reinterpret_cast<const uint8_t*
>(ptr_) + entry_bits_;
908 for (
int i = 0; i < 8; ++i) {
909 buffer_[i] += previous_;
910 previous_ = buffer_[i];
914template<
typename Allocator>
915auto wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator++(
int) -> const_iterator {
916 const_iterator tmp(*
this);
921template<
typename Allocator>
922bool wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator!=(
const const_iterator& other)
const {
923 if (entry_bits_ == 64)
return ptr_ != other.ptr_;
924 return index_ != other.index_;
927template<
typename Allocator>
928bool wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator==(
const const_iterator& other)
const {
929 if (entry_bits_ == 64)
return ptr_ == other.ptr_;
930 return index_ == other.index_;
933template<
typename Allocator>
934auto wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator*() const -> reference {
935 if (entry_bits_ == 64)
return *
reinterpret_cast<const uint64_t*
>(ptr_);
936 return buffer_[index_ & 7];
939template<
typename Allocator>
940auto wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator::operator->() const -> pointer {
941 if (entry_bits_ == 64)
return reinterpret_cast<const uint64_t*
>(ptr_);
942 return buffer_ + (index_ & 7);