20#ifndef KLL_SKETCH_HPP_
21#define KLL_SKETCH_HPP_
26#include "common_defs.hpp"
28#include "quantiles_sorted_view.hpp"
29#include "optional.hpp"
37 const uint8_t DEFAULT_M = 8;
39 const uint16_t
MIN_K = DEFAULT_M;
41 const uint16_t
MAX_K = (1 << 16) - 1;
45 const uint64_t
MAX_N = (1ULL << 63) - 1;
179 typename C = std::less<T>,
180 typename A = std::allocator<T>
184 using value_type = T;
185 using comparator = C;
186 using allocator_type = A;
187 using vector_u32 = std::vector<uint32_t, typename std::allocator_traits<A>::template rebind_alloc<uint32_t>>;
188 using vector_double =
typename quantiles_sorted_view<T, C, A>::vector_double;
211 kll_sketch(
const kll_sketch& other);
217 kll_sketch(kll_sketch&& other)
noexcept;
227 kll_sketch&
operator=(
const kll_sketch& other);
234 kll_sketch&
operator=(kll_sketch&& other);
242 template<
typename TT,
typename CC,
typename AA>
243 explicit kll_sketch(
const kll_sketch<TT, CC, AA>& other,
const C& comparator = C(),
const A& allocator = A());
253 template<
typename FwdT>
270 template<
typename FwdT>
271 void update(FwdT&& item, uint64_t weight);
285 template<
typename FwdSk>
286 void merge(FwdSk&& other);
308 uint16_t
get_k()
const;
314 uint64_t
get_n()
const;
385 double get_rank(
const T& item,
bool inclusive =
true)
const;
409 vector_double
get_PMF(
const T* split_points, uint32_t size,
bool inclusive =
true)
const;
436 vector_double
get_CDF(
const T* split_points, uint32_t size,
bool inclusive =
true)
const;
453 template<
typename TT = T,
typename SerDe = serde<T>,
typename std::enable_if<std::is_arithmetic<TT>::value,
int>::type = 0>
462 template<
typename TT = T,
typename SerDe = serde<T>,
typename std::enable_if<!std::is_arithmetic<TT>::value,
int>::type = 0>
475 template<typename TT = T, typename std::enable_if<std::is_arithmetic<TT>::value,
int>::type = 0>
489 template<typename TT = T, typename std::enable_if<!std::is_arithmetic<TT>::value,
int>::type = 0>
497 template<
typename SerDe = serde<T>>
498 void serialize(std::ostream& os,
const SerDe& sd = SerDe())
const;
502 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<A>::template rebind_alloc<uint8_t>>;
513 template<
typename SerDe = serde<T>>
514 vector_bytes
serialize(
unsigned header_size_bytes = 0,
const SerDe& sd = SerDe())
const;
524 template<
typename SerDe = serde<T>>
525 static kll_sketch
deserialize(std::istream& is,
const SerDe& sd = SerDe(),
526 const C& comparator = C(),
const A& allocator = A());
537 template<
typename SerDe = serde<T>>
538 static kll_sketch
deserialize(
const void* bytes,
size_t size,
const SerDe& sd = SerDe(),
539 const C& comparator = C(),
const A& allocator = A());
555 string<A>
to_string(
bool print_levels =
false,
bool print_items =
false)
const;
557 class const_iterator;
564 const_iterator
begin()
const;
572 const_iterator
end()
const;
594 static const size_t EMPTY_SIZE_BYTES = 8;
595 static const size_t DATA_START_SINGLE_ITEM = 8;
596 static const size_t DATA_START = 20;
598 static const uint8_t SERIAL_VERSION_1 = 1;
599 static const uint8_t SERIAL_VERSION_2 = 2;
600 static const uint8_t FAMILY = 15;
602 enum flags { IS_EMPTY, IS_LEVEL_ZERO_SORTED, IS_SINGLE_ITEM };
604 static const uint8_t PREAMBLE_INTS_SHORT = 2;
605 static const uint8_t PREAMBLE_INTS_FULL = 5;
608 static const resize_factor RESIZE_FACTOR = resize_factor::X8;
609 static_assert(RESIZE_FACTOR >= resize_factor::X1,
"RESIZE_FACTOR must be at least X1");
617 bool is_level_zero_sorted_;
621 uint32_t items_size_;
622 optional<T> min_item_;
623 optional<T> max_item_;
628 kll_sketch(uint16_t k, uint16_t min_k, uint64_t n, uint8_t num_levels, vector_u32&& levels,
629 std::unique_ptr<T, items_deleter> items, uint32_t items_size, optional<T>&& min_item,
630 optional<T>&& max_item,
bool is_level_zero_sorted,
const C& comparator);
633 kll_sketch(uint16_t k,
const T& item, uint64_t weight,
const C& comparator,
const A& allocator);
636 inline void update_min_max(
const T& item);
639 inline void allocate_items();
641 template<
typename O>
void copy_items(
const O& other);
643 inline T* item_ptr(uint32_t index)
const;
644 inline uint32_t internal_update();
646 template<
typename FwdT>
void insert_level_zero(FwdT&& item);
648 inline void check_n_increase(uint64_t increase)
const;
650 void destroy_items();
654 void compress_while_updating(
void);
656 uint8_t find_level_to_compact()
const;
657 void add_empty_top_level_to_completely_full_sketch();
658 void grow_items(uint32_t new_size);
659 void sort_level_zero();
661 template<
typename O>
void merge_higher_levels(O&& other, uint64_t final_n);
663 template<
typename FwdSk>
664 void populate_work_arrays(FwdSk&& other, T* workbuf,
const uint32_t* worklevels, uint8_t provisional_num_levels);
666 void assert_correct_total_weight()
const;
667 uint32_t safe_level_size(uint8_t level)
const;
668 uint32_t get_num_retained_above_level_zero()
const;
669 bool is_min_retained()
const;
671 static void check_k(uint16_t k);
672 static void check_m(uint8_t m);
673 static void check_preamble_ints(uint8_t preamble_ints, uint8_t flags_byte);
674 static void check_serial_version(uint8_t serial_version);
675 static void check_family_id(uint8_t family_id);
676 static void check_num_levels(uint8_t num_levels);
677 static void check_n(uint64_t n, uint32_t num_retained);
678 static void check_levels(
const vector_u32& levels);
680 void check_sorting()
const;
682 template<typename TT = T, typename std::enable_if<std::is_floating_point<TT>::value,
int>::type = 0>
683 static inline bool check_update_item(TT item) {
684 return !std::isnan(item);
687 template<typename TT = T, typename std::enable_if<!std::is_floating_point<TT>::value,
int>::type = 0>
688 static inline bool check_update_item(TT) {
693 template<
typename TT,
typename CC,
typename AA>
friend class kll_sketch;
695 void setup_sorted_view()
const;
696 void reset_sorted_view();
699template<
typename T,
typename C,
typename A>
702 using iterator_category = std::input_iterator_tag;
703 using value_type = std::pair<const T&, const uint64_t>;
704 using difference_type = void;
705 using pointer =
const return_value_holder<value_type>;
706 using reference =
const value_type;
708 friend class kll_sketch<T, C, A>;
709 const_iterator& operator++();
710 const_iterator& operator++(
int);
711 bool operator==(
const const_iterator& other)
const;
712 bool operator!=(
const const_iterator& other)
const;
713 reference operator*()
const;
714 pointer operator->()
const;
717 const uint32_t* levels;
718 const uint8_t num_levels;
722 const_iterator(
const T* items,
const uint32_t* levels,
const uint8_t num_levels);
727#include "kll_sketch_impl.hpp"
Implementation of a very compact quantiles sketch with lazy compaction scheme and nearly optimal accu...
Definition kll_sketch.hpp:182
void reserve()
Allocates storage for k items now, rather than on the first update or merge, or as the buffer grows.
Definition kll_sketch_impl.hpp:295
C get_comparator() const
Returns an instance of the comparator for this sketch.
Definition kll_sketch_impl.hpp:415
quantiles_sorted_view< T, C, A > get_sorted_view() const
Gets the sorted view of this sketch.
Definition kll_sketch_impl.hpp:944
kll_sketch & operator=(const kll_sketch &other)
Copy assignment.
Definition kll_sketch_impl.hpp:103
vector_bytes serialize(unsigned header_size_bytes=0, const SerDe &sd=SerDe()) const
This method serializes the sketch as a vector of bytes.
vector_double get_CDF(const T *split_points, uint32_t size, bool inclusive=true) const
Returns an approximation to the Cumulative Distribution Function (CDF), which is the cumulative analo...
Definition kll_sketch_impl.hpp:439
const_iterator begin() const
Iterator pointing to the first item in the sketch.
Definition kll_sketch_impl.hpp:1230
uint32_t get_num_retained() const
Returns the number of retained items (samples) in the sketch.
Definition kll_sketch_impl.hpp:393
static kll_sketch deserialize(std::istream &is, const SerDe &sd=SerDe(), const C &comparator=C(), const A &allocator=A())
This method deserializes a sketch from a given stream.
vector_double get_PMF(const T *split_points, uint32_t size, bool inclusive=true) const
Returns an approximation to the Probability Mass Function (PMF) of the input stream given a set of sp...
Definition kll_sketch_impl.hpp:432
void merge(FwdSk &&other)
Merges another sketch into this one.
Definition kll_sketch_impl.hpp:351
T get_max_item() const
Returns the max item of the stream.
Definition kll_sketch_impl.hpp:409
const_iterator end() const
Iterator pointing to the past-the-end item in the sketch.
Definition kll_sketch_impl.hpp:1235
void update(FwdT &&item)
Updates this sketch with the given data item.
Definition kll_sketch_impl.hpp:191
void serialize(std::ostream &os, const SerDe &sd=SerDe()) const
This method serializes the sketch into a given stream in a binary form.
Definition kll_sketch_impl.hpp:511
string< A > to_string(bool print_levels=false, bool print_items=false) const
Prints a summary of the sketch.
Definition kll_sketch_impl.hpp:1177
uint16_t get_k() const
Returns configured parameter k.
Definition kll_sketch_impl.hpp:383
bool is_empty() const
Returns true if this sketch is empty.
Definition kll_sketch_impl.hpp:378
double get_rank(const T &item, bool inclusive=true) const
Returns an approximation to the normalized rank of the given item from 0 to 1, inclusive.
Definition kll_sketch_impl.hpp:425
A get_allocator() const
Returns an instance of the allocator for this sketch.
Definition kll_sketch_impl.hpp:420
T get_min_item() const
Returns the min item of the stream.
Definition kll_sketch_impl.hpp:403
typename quantiles_sorted_view< T, C, A >::quantile_return_type quantile_return_type
Quantile return type.
Definition kll_sketch.hpp:194
quantile_return_type get_quantile(double rank, bool inclusive=true) const
Returns an item from the sketch that is the best approximation to an item from the original stream wi...
Definition kll_sketch_impl.hpp:446
size_t get_serialized_size_bytes(const SerDe &sd=SerDe()) const
Computes size needed to serialize the current state of the sketch.
Definition kll_sketch_impl.hpp:464
static size_t get_max_serialized_size_bytes(uint16_t k, uint64_t n)
Returns upper bound on the serialized size of a sketch given a parameter k and stream length.
Definition kll_sketch_impl.hpp:492
double get_normalized_rank_error(bool pmf) const
Gets the approximate rank error of this sketch normalized as a fraction between zero and one.
Definition kll_sketch_impl.hpp:457
bool is_estimation_mode() const
Returns true if this sketch is in estimation mode.
Definition kll_sketch_impl.hpp:398
uint64_t get_n() const
Returns the length of the input stream.
Definition kll_sketch_impl.hpp:388
static kll_sketch deserialize(const void *bytes, size_t size, const SerDe &sd=SerDe(), const C &comparator=C(), const A &allocator=A())
This method deserializes a sketch from a given array of bytes.
Sorted view for quantiles sketches (REQ, KLL and Quantiles).
Definition quantiles_sorted_view.hpp:40
typename std::conditional< std::is_arithmetic< T >::value, T, const T & >::type quantile_return_type
Quantile return type.
Definition quantiles_sorted_view.hpp:101
KLL sketch constants.
Definition kll_sketch.hpp:34
const uint16_t MAX_K
max value of parameter K
Definition kll_sketch.hpp:41
const uint64_t MAX_N
maximum stream length n, the largest signed 64-bit integer, as in datasketches-java
Definition kll_sketch.hpp:45
const uint64_t MAX_WEIGHT
maximum weight of a weighted update, the same as in datasketches-java
Definition kll_sketch.hpp:43
const uint16_t MIN_K
min value of parameter K
Definition kll_sketch.hpp:39
const uint16_t DEFAULT_K
default value of parameter K
Definition kll_sketch.hpp:36
DataSketches namespace.
Definition binomial_bounds.hpp:38