20#ifndef TUPLE_SKETCH_HPP_
21#define TUPLE_SKETCH_HPP_
26#include "theta_update_sketch_base.hpp"
36template<
typename K,
typename V>
37struct pair_extract_key {
38 K& operator()(std::pair<K, V>& entry)
const {
41 const K& operator()(
const std::pair<K, V>& entry)
const {
57 typename Allocator = std::allocator<Summary>
61 using Entry = std::pair<uint64_t, Summary>;
62 using ExtractKey = pair_extract_key<uint64_t, Summary>;
63 using iterator = theta_iterator<Entry, ExtractKey>;
64 using const_iterator = theta_const_iterator<Entry, ExtractKey>;
160 string<Allocator>
to_string(
bool print_items =
false)
const;
173 virtual iterator
end() = 0;
179 virtual const_iterator
begin()
const = 0;
186 virtual const_iterator
end()
const = 0;
189 virtual void print_specifics(std::ostringstream& os)
const = 0;
193 static void check_sketch_type(uint8_t actual, uint8_t expected);
194 static void check_serial_version(uint8_t actual, uint8_t expected);
195 static void check_seed_hash(uint16_t actual, uint16_t expected);
201template<
typename Summary,
typename Update>
202struct default_tuple_update_policy {
203 Summary create()
const {
206 void update(Summary& summary,
const Update& update)
const {
218 typename Update = Summary,
219 typename Policy = default_tuple_update_policy<Summary, Update>,
220 typename Allocator = std::allocator<Summary>
225 using Entry =
typename Base::Entry;
226 using ExtractKey =
typename Base::ExtractKey;
227 using iterator =
typename Base::iterator;
228 using const_iterator =
typename Base::const_iterator;
229 using AllocEntry =
typename std::allocator_traits<Allocator>::template rebind_alloc<Entry>;
230 using tuple_map = theta_update_sketch_base<Entry, ExtractKey, AllocEntry>;
231 using resize_factor =
typename tuple_map::resize_factor;
236 update_tuple_sketch(
const update_tuple_sketch&) =
default;
237 update_tuple_sketch(update_tuple_sketch&&)
noexcept =
default;
238 virtual ~update_tuple_sketch() =
default;
239 update_tuple_sketch& operator=(
const update_tuple_sketch&) =
default;
240 update_tuple_sketch& operator=(update_tuple_sketch&&) =
default;
267 template<
typename FwdUpdate>
268 inline void update(
const std::string& key, FwdUpdate&& value);
278 template<
typename FwdUpdate>
279 inline void update(uint64_t key, FwdUpdate&& value);
289 template<
typename FwdUpdate>
290 inline void update(int64_t key, FwdUpdate&& value);
301 template<
typename FwdUpdate>
302 inline void update(uint32_t key, FwdUpdate&& value);
313 template<
typename FwdUpdate>
314 inline void update(int32_t key, FwdUpdate&& value);
325 template<
typename FwdUpdate>
326 inline void update(uint16_t key, FwdUpdate&& value);
337 template<
typename FwdUpdate>
338 inline void update(int16_t key, FwdUpdate&& value);
349 template<
typename FwdUpdate>
350 inline void update(uint8_t key, FwdUpdate&& value);
361 template<
typename FwdUpdate>
362 inline void update(int8_t key, FwdUpdate&& value);
373 template<
typename FwdUpdate>
374 inline void update(
double key, FwdUpdate&& value);
385 template<
typename FwdUpdate>
386 inline void update(
float key, FwdUpdate&& value);
405 template<
typename FwdUpdate>
406 void update(
const void* key,
size_t length, FwdUpdate&& value);
431 template<
typename Predicate>
436 virtual const_iterator
begin()
const;
437 virtual const_iterator
end()
const;
444 update_tuple_sketch(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf,
float p, uint64_t theta, uint64_t seed,
const Policy& policy,
const Allocator& allocator);
446 virtual void print_specifics(std::ostringstream& os)
const;
455 typename Allocator = std::allocator<Summary>
460 using Entry =
typename Base::Entry;
461 using ExtractKey =
typename Base::ExtractKey;
462 using iterator =
typename Base::iterator;
463 using const_iterator =
typename Base::const_iterator;
464 using AllocEntry =
typename std::allocator_traits<Allocator>::template rebind_alloc<Entry>;
465 using AllocU64 =
typename std::allocator_traits<Allocator>::template rebind_alloc<uint64_t>;
466 using AllocBytes =
typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>;
467 using vector_bytes = std::vector<uint8_t, AllocBytes>;
468 using comparator = compare_by_key<ExtractKey>;
470 static const uint8_t SERIAL_VERSION_LEGACY = 1;
471 static const uint8_t SERIAL_VERSION = 3;
472 static const uint8_t SKETCH_FAMILY = 9;
473 static const uint8_t SKETCH_TYPE = 1;
474 static const uint8_t SKETCH_TYPE_LEGACY = 5;
475 enum flags { IS_BIG_ENDIAN, IS_READ_ONLY, IS_EMPTY, IS_COMPACT, IS_ORDERED };
539 template<typename Predicate>
549 template<typename Sketch, typename Predicate>
557 template<typename SerDe =
serde<Summary>>
558 void serialize(std::ostream& os, const SerDe& sd = SerDe()) const;
569 template<typename SerDe =
serde<Summary>>
570 vector_bytes
serialize(
unsigned header_size_bytes = 0, const SerDe& sd = SerDe()) const;
574 virtual const_iterator
begin() const;
575 virtual const_iterator
end() const;
585 template<typename SerDe =
serde<Summary>>
587 const SerDe& sd = SerDe(), const Allocator& allocator = Allocator());
598 template<typename SerDe =
serde<Summary>>
600 const SerDe& sd = SerDe(), const Allocator& allocator = Allocator());
607 std::vector<Entry, AllocEntry> entries_;
614 template<typename SerDe, typename SS = Summary, typename std::enable_if<std::is_arithmetic<SS>::value,
int>::type = 0>
622 template<typename SerDe, typename SS = Summary, typename std::enable_if<!std::is_arithmetic<SS>::value,
int>::type = 0>
626 class deleter_of_summaries {
628 deleter_of_summaries(uint32_t num,
bool destroy,
const Allocator& allocator):
629 allocator_(allocator), num_(num), destroy_(destroy) {}
630 void set_destroy(
bool destroy) { destroy_ = destroy; }
631 void operator() (Summary* ptr) {
632 if (ptr !=
nullptr) {
634 for (uint32_t i = 0; i < num_; ++i) ptr[i].~Summary();
636 allocator_.deallocate(ptr, num_);
640 Allocator allocator_;
645 virtual void print_specifics(std::ostringstream& os)
const;
647 template<
typename E,
typename EK,
typename P,
typename S,
typename CS,
typename A>
friend class theta_union_base;
648 template<
typename E,
typename EK,
typename P,
typename S,
typename CS,
typename A>
friend class theta_intersection_base;
649 template<
typename E,
typename EK,
typename CS,
typename A>
friend class theta_set_difference_base;
654template<
typename Derived,
typename Policy,
typename Allocator>
657 tuple_base_builder(
const Policy& policy,
const Allocator& allocator);
664template<
typename S,
typename U,
typename P,
typename A>
665class update_tuple_sketch<S, U, P, A>::
builder:
public tuple_base_builder<builder, P, A> {
673 builder(
const P& policy = P(),
const A& allocator = A());
679 update_tuple_sketch<S, U, P, A>
build()
const;
684#include "tuple_sketch_impl.hpp"
Compact Tuple sketch.
Definition tuple_sketch.hpp:457
virtual uint64_t get_theta64() const
virtual uint32_t get_num_retained() const
compact_tuple_sketch(compact_tuple_sketch &&) noexcept
Move constructor.
Definition tuple_sketch_impl.hpp:300
compact_tuple_sketch filter(const Predicate &predicate) const
void serialize(std::ostream &os, const SerDe &sd=SerDe()) const
virtual bool is_empty() const
virtual bool is_ordered() const
virtual uint16_t get_seed_hash() const
compact_tuple_sketch(const Base &other, bool ordered)
Copy constructor.
Definition tuple_sketch_impl.hpp:287
virtual Allocator get_allocator() const
static compact_tuple_sketch deserialize(std::istream &is, uint64_t seed=DEFAULT_SEED, const SerDe &sd=SerDe(), const Allocator &allocator=Allocator())
compact_tuple_sketch(const compact_tuple_sketch &other)=default
Copy constructor.
size_t get_serialized_size_summaries_bytes(const SerDe &sd) const
theta_base_builder(const Allocator &allocator)
Creates and instance of the builder with default parameters.
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
Base class for Tuple sketch.
Definition tuple_sketch.hpp:59
double get_upper_bound(uint8_t num_std_devs, uint32_t num_subset_entries) const
Returns the approximate upper error bound given a number of standard deviations over an arbitrary num...
Definition tuple_sketch_impl.hpp:57
virtual const_iterator begin() const =0
Const iterator over entries in this sketch.
double get_estimate() const
Definition tuple_sketch_impl.hpp:40
double get_lower_bound(uint8_t num_std_devs) const
Returns the approximate lower error bound given a number of standard deviations.
Definition tuple_sketch_impl.hpp:52
virtual bool is_ordered() const =0
virtual const_iterator end() const =0
Const iterator pointing past the valid range.
virtual bool is_empty() const =0
double get_lower_bound(uint8_t num_std_devs, uint32_t num_subset_entries) const
Returns the approximate lower error bound given a number of standard deviations over an arbitrary num...
Definition tuple_sketch_impl.hpp:45
string< Allocator > to_string(bool print_items=false) const
Provides a human-readable summary of this sketch as a string.
Definition tuple_sketch_impl.hpp:69
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 tuple_sketch_impl.hpp:64
double get_theta() const
Definition tuple_sketch_impl.hpp:34
virtual iterator end()=0
Iterator pointing past the valid range.
virtual uint16_t get_seed_hash() const =0
virtual iterator begin()=0
Iterator over entries in this sketch.
bool is_estimation_mode() const
Definition tuple_sketch_impl.hpp:29
virtual Allocator get_allocator() const =0
virtual uint64_t get_theta64() const =0
Update Tuple sketch builder.
Definition tuple_sketch.hpp:665
builder(const P &policy=P(), const A &allocator=A())
Constructor Creates and instance of the builder with default parameters.
Definition tuple_sketch_impl.hpp:627
update_tuple_sketch< S, U, P, A > build() const
This is to create an instance of the sketch with predefined parameters.
Definition tuple_sketch_impl.hpp:631
Update Tuple sketch.
Definition tuple_sketch.hpp:222
void update(int16_t key, FwdUpdate &&value)
Update this sketch with a given signed 16-bit integer.
compact_tuple_sketch< Summary, Allocator > filter(const Predicate &predicate) const
Produces a Compact Tuple sketch from this sketch by applying a given predicate to each entry.
void trim()
Remove retained entries in excess of the nominal size k (if any).
Definition tuple_sketch_impl.hpp:227
void update(uint64_t key, FwdUpdate &&value)
Update this sketch with a given unsigned 64-bit integer.
virtual uint64_t get_theta64() const
Definition tuple_sketch_impl.hpp:120
virtual uint32_t get_num_retained() const
Definition tuple_sketch_impl.hpp:125
resize_factor get_rf() const
Definition tuple_sketch_impl.hpp:140
void update(int32_t key, FwdUpdate &&value)
Update this sketch with a given signed 32-bit integer.
virtual bool is_empty() const
Definition tuple_sketch_impl.hpp:110
virtual bool is_ordered() const
Definition tuple_sketch_impl.hpp:115
void update(float key, FwdUpdate &&value)
Update this sketch with a given floating point value.
virtual uint16_t get_seed_hash() const
Definition tuple_sketch_impl.hpp:130
virtual iterator end()
Iterator pointing past the valid range.
Definition tuple_sketch_impl.hpp:242
void update(int8_t key, FwdUpdate &&value)
Update this sketch with a given signed 8-bit integer.
compact_tuple_sketch< Summary, Allocator > compact(bool ordered=true) const
Converts this sketch to a compact sketch (ordered or unordered).
Definition tuple_sketch_impl.hpp:257
uint8_t get_lg_k() const
Definition tuple_sketch_impl.hpp:135
void update(const std::string &key, FwdUpdate &&value)
Update this sketch with a given string.
void update(uint8_t key, FwdUpdate &&value)
Update this sketch with a given unsigned 8-bit integer.
void update(uint32_t key, FwdUpdate &&value)
Update this sketch with a given unsigned 32-bit integer.
virtual Allocator get_allocator() const
Definition tuple_sketch_impl.hpp:105
void update(int64_t key, FwdUpdate &&value)
Update this sketch with a given signed 64-bit integer.
void reset()
Reset the sketch to the initial empty state.
Definition tuple_sketch_impl.hpp:232
virtual const_iterator begin() const
Const iterator over entries in this sketch.
Definition tuple_sketch_impl.hpp:247
void update(const void *key, size_t length, FwdUpdate &&value)
Update this sketch with given data of any type.
virtual iterator begin()
Iterator over entries in this sketch.
Definition tuple_sketch_impl.hpp:237
void update(double key, FwdUpdate &&value)
Update this sketch with a given double-precision floating point value.
virtual const_iterator end() const
Const iterator pointing past the valid range.
Definition tuple_sketch_impl.hpp:252
void update(uint16_t key, FwdUpdate &&value)
Update this sketch with a given unsigned 16-bit integer.
DataSketches namespace.
Definition binomial_bounds.hpp:38
Interface for serializing and deserializing items.
Definition serde.hpp:35