20#ifndef THETA_SKETCH_HPP_
21#define THETA_SKETCH_HPP_
23#include "theta_update_sketch_base.hpp"
24#include "compact_theta_sketch_parser.hpp"
44template<
typename Allocator = std::allocator<u
int64_t>>
118 virtual string<Allocator>
to_string(
bool print_items =
false)
const;
121 virtual void print_specifics(std::ostringstream& os)
const = 0;
122 virtual void print_items(std::ostringstream& os)
const = 0;
126template<
typename Allocator = std::allocator<u
int64_t>>
129 using Entry = uint64_t;
130 using ExtractKey = trivial_extract_key;
131 using iterator = theta_iterator<Entry, ExtractKey>;
132 using const_iterator = theta_const_iterator<Entry, ExtractKey>;
147 virtual iterator
end() = 0;
153 virtual const_iterator
begin()
const = 0;
160 virtual const_iterator
end()
const = 0;
163 virtual void print_items(std::ostringstream& os)
const;
174template<
typename Allocator = std::allocator<u
int64_t>>
178 using Entry =
typename Base::Entry;
179 using ExtractKey =
typename Base::ExtractKey;
180 using iterator =
typename Base::iterator;
181 using const_iterator =
typename Base::const_iterator;
182 using theta_table = theta_update_sketch_base<Entry, ExtractKey, Allocator>;
183 using resize_factor =
typename theta_table::resize_factor;
320 void update(
const void* data,
size_t length);
355 virtual const_iterator
begin()
const;
356 virtual const_iterator
end()
const;
363 uint64_t theta, uint64_t seed,
const Allocator& allocator);
365 virtual void print_specifics(std::ostringstream& os)
const;
372template<
typename Allocator = std::allocator<u
int64_t>>
376 using iterator =
typename Base::iterator;
377 using const_iterator =
typename Base::const_iterator;
378 using AllocBytes =
typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>;
379 using vector_bytes = std::vector<uint8_t, AllocBytes>;
381 static const uint8_t UNCOMPRESSED_SERIAL_VERSION = 3;
382 static const uint8_t COMPRESSED_SERIAL_VERSION = 4;
383 static const uint8_t SKETCH_TYPE = 3;
396 template<
typename Other>
461 vector_bytes
serialize(
unsigned header_size_bytes = 0)
const;
484 virtual const_iterator
begin()
const;
485 virtual const_iterator
end()
const;
495 uint64_t seed = DEFAULT_SEED,
const Allocator& allocator = Allocator());
506 uint64_t seed = DEFAULT_SEED,
const Allocator& allocator = Allocator());
509 enum flags { IS_BIG_ENDIAN, IS_READ_ONLY, IS_EMPTY, IS_COMPACT, IS_ORDERED, IS_SINGLE_ITEM };
515 std::vector<uint64_t, Allocator> entries_;
517 uint8_t get_preamble_longs(
bool compressed)
const;
518 bool is_single_item()
const;
519 bool is_suitable_for_compression()
const;
520 uint8_t compute_entry_bits()
const;
521 uint8_t get_num_entries_bytes()
const;
522 size_t get_compressed_serialized_size_bytes(uint8_t entry_bits, uint8_t num_entries_bytes)
const;
523 void serialize_version_4(std::ostream& os)
const;
524 vector_bytes serialize_version_4(
unsigned header_size_bytes = 0)
const;
526 static compact_theta_sketch_alloc deserialize_v1(uint8_t preamble_longs, std::istream& is, uint64_t seed,
const Allocator& allocator);
527 static compact_theta_sketch_alloc deserialize_v2(uint8_t preamble_longs, std::istream& is, uint64_t seed,
const Allocator& allocator);
528 static compact_theta_sketch_alloc deserialize_v3(uint8_t preamble_longs, std::istream& is, uint64_t seed,
const Allocator& allocator);
529 static compact_theta_sketch_alloc deserialize_v4(uint8_t preamble_longs, std::istream& is, uint64_t seed,
const Allocator& allocator);
531 virtual void print_specifics(std::ostringstream& os)
const;
533 template<
typename E,
typename EK,
typename P,
typename S,
typename CS,
typename A>
friend class theta_union_base;
534 template<
typename E,
typename EK,
typename P,
typename S,
typename CS,
typename A>
friend class theta_intersection_base;
535 template<
typename E,
typename EK,
typename CS,
typename A>
friend class theta_set_difference_base;
541template<
typename Allocator>
548 builder(
const Allocator& allocator = Allocator());
558template<
typename Allocator = std::allocator<u
int64_t>>
561 class const_iterator;
581 const_iterator
end()
const;
591 static const wrapped_compact_theta_sketch_alloc
wrap(
const void* bytes,
size_t size, uint64_t seed = DEFAULT_SEED,
bool dump_on_error =
false);
594 virtual void print_specifics(std::ostringstream& os)
const;
595 virtual void print_items(std::ostringstream& os)
const;
598 using data_type = compact_theta_sketch_parser<true>::compact_theta_sketch_data;
601 wrapped_compact_theta_sketch_alloc(
const data_type& data);
604template<
typename Allocator>
607 using iterator_category = std::input_iterator_tag;
608 using value_type =
const uint64_t;
609 using difference_type = void;
610 using pointer = value_type*;
611 using reference = uint64_t;
613 const_iterator(
const void* ptr, uint8_t entry_bits, uint32_t num_entries, uint32_t index);
614 const_iterator& operator++();
615 const_iterator operator++(
int);
616 bool operator==(
const const_iterator& other)
const;
617 bool operator!=(
const const_iterator& other)
const;
618 reference operator*()
const;
619 pointer operator->()
const;
624 uint32_t num_entries_;
631 inline void unpack1();
632 inline void unpack8();
637#include "theta_sketch_impl.hpp"
Abstract base class for Theta sketch.
Definition theta_sketch.hpp:45
double get_estimate() const
Definition theta_sketch_impl.hpp:48
double get_lower_bound(uint8_t num_std_devs) const
Returns the approximate lower error bound given a number of standard deviations.
Definition theta_sketch_impl.hpp:53
virtual bool is_ordered() const =0
virtual bool is_empty() const =0
virtual string< Allocator > to_string(bool print_items=false) const
Provides a human-readable summary of this sketch as a string.
Definition theta_sketch_impl.hpp:65
virtual uint32_t get_num_retained() const =0
double get_upper_bound(uint8_t num_std_devs) const
Returns the approximate upper error bound given a number of standard deviations.
Definition theta_sketch_impl.hpp:59
double get_theta() const
Definition theta_sketch_impl.hpp:42
virtual uint16_t get_seed_hash() const =0
bool is_estimation_mode() const
Definition theta_sketch_impl.hpp:37
virtual Allocator get_allocator() const =0
virtual uint64_t get_theta64() const =0
Compact Theta sketch.
Definition theta_sketch.hpp:373
compact_theta_sketch_alloc & operator=(compact_theta_sketch_alloc &&other)=default
Move assignment.
compact_theta_sketch_alloc(const Other &other, bool ordered)
Copy constructor.
Definition theta_sketch_impl.hpp:281
compact_theta_sketch_alloc(const compact_theta_sketch_alloc &other)=default
Copy constructor.
compact_theta_sketch_alloc & operator=(const compact_theta_sketch_alloc &other)=default
Copy assignment.
void serialize(std::ostream &os) const
This method serializes the sketch into a given stream in a binary form.
Definition theta_sketch_impl.hpp:392
vector_bytes serialize(unsigned header_size_bytes=0) const
This method serializes the sketch as a vector of bytes.
Definition theta_sketch_impl.hpp:416
virtual uint64_t get_theta64() const
Definition theta_sketch_impl.hpp:321
static compact_theta_sketch_alloc deserialize(const void *bytes, size_t size, uint64_t seed=DEFAULT_SEED, const Allocator &allocator=Allocator())
This method deserializes a sketch from a given array of bytes.
virtual uint32_t get_num_retained() const
Definition theta_sketch_impl.hpp:326
virtual bool is_empty() const
Definition theta_sketch_impl.hpp:311
virtual bool is_ordered() const
Definition theta_sketch_impl.hpp:316
virtual uint16_t get_seed_hash() const
Definition theta_sketch_impl.hpp:331
virtual iterator end()
Iterator pointing past the valid range.
Definition theta_sketch_impl.hpp:341
static size_t get_max_serialized_size_bytes(uint8_t lg_k)
Computes maximum serialized size in bytes.
Definition theta_sketch_impl.hpp:367
static compact_theta_sketch_alloc deserialize(std::istream &is, uint64_t seed=DEFAULT_SEED, const Allocator &allocator=Allocator())
This method deserializes a sketch from a given stream.
virtual Allocator get_allocator() const
Definition theta_sketch_impl.hpp:306
size_t get_serialized_size_bytes(bool compressed=false) const
Computes size in bytes required to serialize the current state of the sketch.
Definition theta_sketch_impl.hpp:372
compact_theta_sketch_alloc(compact_theta_sketch_alloc &&other) noexcept=default
Move constructor.
void serialize_compressed(std::ostream &os) const
This method serializes the sketch into a given stream in a compressed binary form.
Definition theta_sketch_impl.hpp:457
virtual const_iterator begin() const
Const iterator over hash values in this sketch.
Definition theta_sketch_impl.hpp:346
virtual iterator begin()
Iterator over hash values in this sketch.
Definition theta_sketch_impl.hpp:336
virtual const_iterator end() const
Const iterator pointing past the valid range.
Definition theta_sketch_impl.hpp:351
vector_bytes serialize_compressed(unsigned header_size_bytes=0) const
This method serializes the sketch as a vector of bytes.
Definition theta_sketch_impl.hpp:463
theta_base_builder(const Allocator &allocator)
Definition theta_update_sketch_base_impl.hpp:304
Base class for the Theta Sketch, a generalization of the Kth Minimum Value (KMV) sketch.
Definition theta_sketch.hpp:127
virtual const_iterator begin() const =0
Const iterator over hash values in this sketch.
virtual const_iterator end() const =0
Const iterator pointing past the valid range.
virtual iterator end()=0
Iterator pointing past the valid range.
virtual iterator begin()=0
Iterator over hash values in this sketch.
Update Theta sketch builder.
Definition theta_sketch.hpp:542
builder(const Allocator &allocator=Allocator())
Constructor.
Definition theta_sketch_impl.hpp:270
update_theta_sketch_alloc build() const
Definition theta_sketch_impl.hpp:273
Update Theta sketch.
Definition theta_sketch.hpp:175
void update(int16_t value)
Update this sketch with a given signed 16-bit integer.
Definition theta_sketch_impl.hpp:172
void trim()
Remove retained entries in excess of the nominal size k (if any).
Definition theta_sketch_impl.hpp:213
virtual uint64_t get_theta64() const
Definition theta_sketch_impl.hpp:122
virtual uint32_t get_num_retained() const
Definition theta_sketch_impl.hpp:127
resize_factor get_rf() const
Definition theta_sketch_impl.hpp:142
void update(int64_t value)
Update this sketch with a given signed 64-bit integer.
Definition theta_sketch_impl.hpp:152
void update(const std::string &value)
Update this sketch with a given string.
Definition theta_sketch_impl.hpp:197
virtual bool is_empty() const
Definition theta_sketch_impl.hpp:112
void update(uint64_t value)
Update this sketch with a given unsigned 64-bit integer.
Definition theta_sketch_impl.hpp:147
virtual bool is_ordered() const
Definition theta_sketch_impl.hpp:117
void update(double value)
Update this sketch with a given double-precision floating point value.
Definition theta_sketch_impl.hpp:187
virtual uint16_t get_seed_hash() const
Definition theta_sketch_impl.hpp:132
update_theta_sketch_alloc(update_theta_sketch_alloc &&other) noexcept=default
Move constructor.
compact_theta_sketch_alloc< Allocator > compact(bool ordered=true, bool trim=false) const
Converts this sketch to a compact sketch (ordered or unordered, trimmed or not trimmed).
Definition theta_sketch_impl.hpp:243
virtual iterator end()
Iterator pointing past the valid range.
Definition theta_sketch_impl.hpp:228
void update(float value)
Update this sketch with a given floating point value.
Definition theta_sketch_impl.hpp:192
uint8_t get_lg_k() const
Definition theta_sketch_impl.hpp:137
virtual Allocator get_allocator() const
Definition theta_sketch_impl.hpp:107
update_theta_sketch_alloc & operator=(const update_theta_sketch_alloc &other)=default
Copy assignment.
void update(uint32_t value)
Update this sketch with a given unsigned 32-bit integer.
Definition theta_sketch_impl.hpp:157
update_theta_sketch_alloc & operator=(update_theta_sketch_alloc &&other)=default
Move assignment.
void update(int32_t value)
Update this sketch with a given signed 32-bit integer.
Definition theta_sketch_impl.hpp:162
void reset()
Reset the sketch to the initial empty state.
Definition theta_sketch_impl.hpp:218
virtual const_iterator begin() const
Const iterator over hash values in this sketch.
Definition theta_sketch_impl.hpp:233
void update(const void *data, size_t length)
Update this sketch with given data of any type.
Definition theta_sketch_impl.hpp:203
virtual iterator begin()
Iterator over hash values in this sketch.
Definition theta_sketch_impl.hpp:223
virtual const_iterator end() const
Const iterator pointing past the valid range.
Definition theta_sketch_impl.hpp:238
void update(int8_t value)
Update this sketch with a given signed 8-bit integer.
Definition theta_sketch_impl.hpp:182
void update(uint8_t value)
Update this sketch with a given unsigned 8-bit integer.
Definition theta_sketch_impl.hpp:177
void update(uint16_t value)
Update this sketch with a given unsigned 16-bit integer.
Definition theta_sketch_impl.hpp:167
update_theta_sketch_alloc(const update_theta_sketch_alloc &other)=default
Copy constructor.
Wrapped Compact Theta sketch.
Definition theta_sketch.hpp:559
uint64_t get_theta64() const
Definition theta_sketch_impl.hpp:798
uint32_t get_num_retained() const
Definition theta_sketch_impl.hpp:803
bool is_empty() const
Definition theta_sketch_impl.hpp:788
bool is_ordered() const
Definition theta_sketch_impl.hpp:793
uint16_t get_seed_hash() const
Definition theta_sketch_impl.hpp:808
static const wrapped_compact_theta_sketch_alloc wrap(const void *bytes, size_t size, uint64_t seed=DEFAULT_SEED, bool dump_on_error=false)
This method wraps a serialized compact sketch as an array of bytes.
Definition theta_sketch_impl.hpp:778
Allocator get_allocator() const
Definition theta_sketch_impl.hpp:783
const_iterator begin() const
Const iterator over hash values in this sketch.
Definition theta_sketch_impl.hpp:813
const_iterator end() const
Const iterator pointing past the valid range.
Definition theta_sketch_impl.hpp:818
DataSketches namespace.
Definition binomial_bounds.hpp:38
wrapped_compact_theta_sketch_alloc< std::allocator< uint64_t > > wrapped_compact_theta_sketch
Wrapped Compact Theta sketch alias with default allocator.
Definition theta_sketch.hpp:41
update_theta_sketch_alloc< std::allocator< uint64_t > > update_theta_sketch
Update Theta sketch alias with default allocator.
Definition theta_sketch.hpp:37
compact_theta_sketch_alloc< std::allocator< uint64_t > > compact_theta_sketch
Compact Theta sketch alias with default allocator.
Definition theta_sketch.hpp:39
theta_sketch_alloc< std::allocator< uint64_t > > theta_sketch
Theta sketch alias with default allocator.
Definition theta_sketch.hpp:35