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"
34 namespace kll_constants {
37 const uint8_t DEFAULT_M = 8;
39 const uint16_t
MIN_K = DEFAULT_M;
41 const uint16_t
MAX_K = (1 << 16) - 1;
163 typename C = std::less<T>,
164 typename A = std::allocator<T>
168 using value_type = T;
169 using comparator = C;
170 using allocator_type = A;
171 using vector_u32 = std::vector<uint32_t, typename std::allocator_traits<A>::template rebind_alloc<uint32_t>>;
223 template<
typename TT,
typename CC,
typename AA>
230 template<
typename FwdT>
237 template<
typename FwdSk>
238 void merge(FwdSk&& other);
250 uint16_t
get_k()
const;
256 uint64_t
get_n()
const;
324 double get_rank(
const T& item,
bool inclusive =
true)
const;
348 vector_double
get_PMF(
const T* split_points, uint32_t size,
bool inclusive =
true)
const;
375 vector_double
get_CDF(
const T* split_points, uint32_t size,
bool inclusive =
true)
const;
392 template<
typename TT = T,
typename SerDe = serde<T>,
typename std::enable_if<std::is_arithmetic<TT>::value,
int>::type = 0>
401 template<
typename TT = T,
typename SerDe = serde<T>,
typename std::enable_if<!std::is_arithmetic<TT>::value,
int>::type = 0>
414 template<typename TT = T, typename std::enable_if<std::is_arithmetic<TT>::value,
int>::type = 0>
428 template<typename TT = T, typename std::enable_if<!std::is_arithmetic<TT>::value,
int>::type = 0>
436 template<
typename SerDe = serde<T>>
437 void serialize(std::ostream& os,
const SerDe& sd = SerDe())
const;
441 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<A>::template rebind_alloc<uint8_t>>;
452 template<
typename SerDe = serde<T>>
453 vector_bytes
serialize(
unsigned header_size_bytes = 0,
const SerDe& sd = SerDe())
const;
463 template<
typename SerDe = serde<T>>
465 const C& comparator = C(),
const A& allocator = A());
476 template<
typename SerDe = serde<T>>
478 const C& comparator = C(),
const A& allocator = A());
494 string<A>
to_string(
bool print_levels =
false,
bool print_items =
false)
const;
496 class const_iterator;
503 const_iterator
begin()
const;
511 const_iterator
end()
const;
530 static const size_t EMPTY_SIZE_BYTES = 8;
531 static const size_t DATA_START_SINGLE_ITEM = 8;
532 static const size_t DATA_START = 20;
534 static const uint8_t SERIAL_VERSION_1 = 1;
535 static const uint8_t SERIAL_VERSION_2 = 2;
536 static const uint8_t FAMILY = 15;
538 enum flags { IS_EMPTY, IS_LEVEL_ZERO_SORTED, IS_SINGLE_ITEM };
540 static const uint8_t PREAMBLE_INTS_SHORT = 2;
541 static const uint8_t PREAMBLE_INTS_FULL = 5;
549 bool is_level_zero_sorted_;
553 uint32_t items_size_;
554 optional<T> min_item_;
555 optional<T> max_item_;
560 kll_sketch(uint16_t k, uint16_t min_k, uint64_t n, uint8_t num_levels, vector_u32&& levels,
561 std::unique_ptr<T, items_deleter> items, uint32_t items_size, optional<T>&& min_item,
562 optional<T>&& max_item,
bool is_level_zero_sorted,
const C& comparator);
565 inline void update_min_max(
const T& item);
566 inline uint32_t internal_update();
570 void compress_while_updating(
void);
572 uint8_t find_level_to_compact()
const;
573 void add_empty_top_level_to_completely_full_sketch();
574 void sort_level_zero();
576 template<
typename O>
void merge_higher_levels(O&& other, uint64_t final_n);
578 template<
typename FwdSk>
579 void populate_work_arrays(FwdSk&& other, T* workbuf, uint32_t* worklevels, uint8_t provisional_num_levels);
581 void assert_correct_total_weight()
const;
582 uint32_t safe_level_size(uint8_t level)
const;
583 uint32_t get_num_retained_above_level_zero()
const;
585 static void check_m(uint8_t m);
586 static void check_preamble_ints(uint8_t preamble_ints, uint8_t flags_byte);
587 static void check_serial_version(uint8_t serial_version);
588 static void check_family_id(uint8_t family_id);
590 void check_sorting()
const;
592 template<typename TT = T, typename std::enable_if<std::is_floating_point<TT>::value,
int>::type = 0>
593 static inline bool check_update_item(TT item) {
594 return !std::isnan(item);
597 template<typename TT = T, typename std::enable_if<!std::is_floating_point<TT>::value,
int>::type = 0>
598 static inline bool check_update_item(TT) {
603 template<
typename TT,
typename CC,
typename AA>
friend class kll_sketch;
605 void setup_sorted_view()
const;
606 void reset_sorted_view();
609 template<
typename T,
typename C,
typename A>
610 class kll_sketch<T, C, A>::const_iterator {
612 using iterator_category = std::input_iterator_tag;
613 using value_type = std::pair<const T&, const uint64_t>;
614 using difference_type = void;
615 using pointer =
const return_value_holder<value_type>;
616 using reference =
const value_type;
618 friend class kll_sketch<T, C, A>;
619 const_iterator& operator++();
620 const_iterator& operator++(
int);
621 bool operator==(
const const_iterator& other)
const;
622 bool operator!=(
const const_iterator& other)
const;
623 reference operator*()
const;
624 pointer operator->()
const;
627 const uint32_t* levels;
628 const uint8_t num_levels;
632 const_iterator(
const T* items,
const uint32_t* levels,
const uint8_t num_levels);
637 #include "kll_sketch_impl.hpp"
Implementation of a very compact quantiles sketch with lazy compaction scheme and nearly optimal accu...
Definition: kll_sketch.hpp:166
C get_comparator() const
Returns an instance of the comparator for this sketch.
Definition: kll_sketch_impl.hpp:271
quantiles_sorted_view< T, C, A > get_sorted_view() const
Gets the sorted view of this sketch.
Definition: kll_sketch_impl.hpp:760
kll_sketch & operator=(const kll_sketch &other)
Copy assignment.
Definition: kll_sketch_impl.hpp:102
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:295
const_iterator begin() const
Iterator pointing to the first item in the sketch.
Definition: kll_sketch_impl.hpp:956
uint32_t get_num_retained() const
Returns the number of retained items (samples) in the sketch.
Definition: kll_sketch_impl.hpp:249
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:288
void merge(FwdSk &&other)
Merges another sketch into this one.
Definition: kll_sketch_impl.hpp:209
T get_max_item() const
Returns the max item of the stream.
Definition: kll_sketch_impl.hpp:265
const_iterator end() const
Iterator pointing to the past-the-end item in the sketch.
Definition: kll_sketch_impl.hpp:961
void update(FwdT &&item)
Updates this sketch with the given data item.
Definition: kll_sketch_impl.hpp:180
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:367
string< A > to_string(bool print_levels=false, bool print_items=false) const
Prints a summary of the sketch.
Definition: kll_sketch_impl.hpp:904
uint16_t get_k() const
Returns configured parameter k.
Definition: kll_sketch_impl.hpp:239
bool is_empty() const
Returns true if this sketch is empty.
Definition: kll_sketch_impl.hpp:234
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:281
A get_allocator() const
Returns an instance of the allocator for this sketch.
Definition: kll_sketch_impl.hpp:276
T get_min_item() const
Returns the min item of the stream.
Definition: kll_sketch_impl.hpp:259
typename quantiles_sorted_view< T, C, A >::quantile_return_type quantile_return_type
Quantile return type.
Definition: kll_sketch.hpp:178
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:302
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:320
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:348
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:313
bool is_estimation_mode() const
Returns true if this sketch is in estimation mode.
Definition: kll_sketch_impl.hpp:254
uint64_t get_n() const
Returns the length of the input stream.
Definition: kll_sketch_impl.hpp:244
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:38
const uint16_t MAX_K
max value of parameter K
Definition: kll_sketch.hpp:41
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