20#ifndef _QUANTILES_SKETCH_HPP_
21#define _QUANTILES_SKETCH_HPP_
27#include "quantiles_sorted_view.hpp"
28#include "common_defs.hpp"
30#include "optional.hpp"
35namespace quantiles_constants {
156 typename Comparator = std::less<T>,
157 typename Allocator = std::allocator<T>>
160 using value_type = T;
161 using allocator_type = Allocator;
162 using comparator = Comparator;
164 using vector_double =
typename quantiles_sorted_view<T, Comparator, Allocator>::vector_double;
173 const Comparator& comparator = Comparator(),
const Allocator& allocator = Allocator());
208 template<
typename From,
typename FC,
typename FA>
210 const Comparator& comparator = Comparator(),
const Allocator& allocator = Allocator());
218 template<
typename FwdT>
227 template<
typename FwdSk>
228 void merge(FwdSk&& other);
240 uint16_t
get_k()
const;
246 uint64_t
get_n()
const;
298 quantile_return_type
get_quantile(
double rank,
bool inclusive =
true)
const;
314 double get_rank(
const T& item,
bool inclusive =
true)
const;
338 vector_double
get_PMF(
const T* split_points, uint32_t size,
bool inclusive =
true)
const;
365 vector_double
get_CDF(
const T* split_points, uint32_t size,
bool inclusive =
true)
const;
373 template<
typename SerDe = serde<T>,
typename TT = T,
typename std::enable_if<std::is_arithmetic<TT>::value,
int>::type = 0>
382 template<
typename SerDe = serde<T>,
typename TT = T,
typename std::enable_if<!std::is_arithmetic<TT>::value,
int>::type = 0>
390 template<
typename SerDe = serde<T>>
391 void serialize(std::ostream& os,
const SerDe& sd = SerDe())
const;
395 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>>;
406 template<
typename SerDe = serde<T>>
407 vector_bytes
serialize(
unsigned header_size_bytes = 0,
const SerDe& sd = SerDe())
const;
417 template<
typename SerDe = serde<T>>
419 const Comparator& comparator = Comparator(),
const Allocator& allocator = Allocator());
430 template<
typename SerDe = serde<T>>
432 const Comparator& comparator = Comparator(),
const Allocator& allocator = Allocator());
458 string<Allocator>
to_string(
bool print_levels =
false,
bool print_items =
false)
const;
460 class const_iterator;
467 const_iterator
begin()
const;
475 const_iterator
end()
const;
484 using Level = std::vector<T, Allocator>;
485 using VectorLevels = std::vector<Level, typename std::allocator_traits<Allocator>::template rebind_alloc<Level>>;
500 static const size_t EMPTY_SIZE_BYTES = 8;
501 static const uint8_t SERIAL_VERSION_1 = 1;
502 static const uint8_t SERIAL_VERSION_2 = 2;
503 static const uint8_t SERIAL_VERSION = 3;
504 static const uint8_t FAMILY = 8;
506 enum flags { RESERVED0, RESERVED1, IS_EMPTY, IS_COMPACT, IS_SORTED };
508 static const uint8_t PREAMBLE_LONGS_SHORT = 1;
509 static const uint8_t PREAMBLE_LONGS_FULL = 2;
510 static const size_t DATA_START = 16;
512 Comparator comparator_;
513 Allocator allocator_;
514 bool is_base_buffer_sorted_;
517 uint64_t bit_pattern_;
519 VectorLevels levels_;
520 optional<T> min_item_;
521 optional<T> max_item_;
524 void setup_sorted_view()
const;
525 void reset_sorted_view();
530 Level&& base_buffer, VectorLevels&& levels,
531 optional<T>&& min_item, optional<T>&& max_item,
532 bool is_sorted,
const Comparator& comparator = Comparator(),
const Allocator& allocator = Allocator());
534 void grow_base_buffer();
535 void process_full_base_buffer();
538 bool grow_levels_if_needed();
541 template<
typename FwdV>
542 static void in_place_propagate_carry(uint8_t starting_level, FwdV&& buf_size_k,
543 Level& buf_size_2k,
bool apply_as_update,
545 static void zip_buffer(Level& buf_in, Level& buf_out);
546 static void merge_two_size_k_buffers(Level& arr_in_1, Level& arr_in_2, Level& arr_out,
const Comparator& comparator);
548 template<
typename SerDe>
549 static Level deserialize_array(std::istream& is, uint32_t num_items, uint32_t capacity,
const SerDe&
serde,
const Allocator& allocator);
551 template<
typename SerDe>
552 static std::pair<Level, size_t> deserialize_array(
const void* bytes,
size_t size, uint32_t num_items, uint32_t capacity,
const SerDe&
serde,
const Allocator& allocator);
554 static void check_k(uint16_t k);
555 static void check_serial_version(uint8_t serial_version);
556 static void check_header_validity(uint8_t preamble_longs, uint8_t flags_byte, uint8_t serial_version);
557 static void check_family_id(uint8_t family_id);
559 static uint32_t compute_retained_items(uint16_t k, uint64_t n);
560 static uint32_t compute_base_buffer_items(uint16_t k, uint64_t n);
561 static uint64_t compute_bit_pattern(uint16_t k, uint64_t n);
562 static uint32_t count_valid_levels(uint64_t bit_pattern);
563 static uint8_t compute_levels_needed(uint16_t k, uint64_t n);
569 template<
typename FwdSk>
578 template<
typename FwdSk>
581 template<
typename FwdV>
582 static void zip_buffer_with_stride(FwdV&& buf_in, Level& buf_out, uint16_t stride);
591 static uint8_t lowest_zero_bit_starting_at(uint64_t bits, uint8_t starting_bit);
593 template<typename TT = T, typename std::enable_if<std::is_floating_point<TT>::value,
int>::type = 0>
594 static inline bool check_update_item(TT item) {
595 return !std::isnan(item);
598 template<typename TT = T, typename std::enable_if<!std::is_floating_point<TT>::value,
int>::type = 0>
599 static inline bool check_update_item(TT) {
604 template<
typename From,
typename FC,
typename FA>
friend class quantiles_sketch;
608template<
typename T,
typename C,
typename A>
609class quantiles_sketch<T, C, A>::const_iterator {
611 using iterator_category = std::input_iterator_tag;
612 using value_type = std::pair<const T&, const uint64_t>;
613 using difference_type = void;
614 using pointer =
const return_value_holder<value_type>;
615 using reference =
const value_type;
617 const_iterator& operator++();
618 const_iterator& operator++(
int);
619 bool operator==(
const const_iterator& other)
const;
620 bool operator!=(
const const_iterator& other)
const;
621 reference operator*()
const;
622 pointer operator->()
const;
624 friend class quantiles_sketch<T, C, A>;
625 using Level = std::vector<T, A>;
626 using AllocLevel =
typename std::allocator_traits<A>::template rebind_alloc<Level>;
628 std::vector<Level, AllocLevel> levels_;
632 uint64_t bit_pattern_;
635 const_iterator(
const Level& base_buffer,
const std::vector<Level, AllocLevel>& levels, uint16_t k, uint64_t n,
bool is_end);
640#include "quantiles_sketch_impl.hpp"
This is a stochastic streaming sketch that enables near-real time analysis of the approximate distrib...
Definition quantiles_sketch.hpp:158
Comparator get_comparator() const
Returns an instance of the comparator for this sketch.
Definition quantiles_sketch_impl.hpp:690
quantiles_sorted_view< T, Comparator, Allocator > get_sorted_view() const
Gets the sorted view of this sketch.
Definition quantiles_sketch_impl.hpp:732
quantiles_sketch(uint16_t k=quantiles_constants::DEFAULT_K, const Comparator &comparator=Comparator(), const Allocator &allocator=Allocator())
Constructor.
quantiles_sketch & operator=(const quantiles_sketch &other)
Copy assignment.
Definition quantiles_sketch_impl.hpp:90
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 quantiles_sketch_impl.hpp:778
uint32_t get_num_retained() const
Returns the number of retained items (samples) in the sketch.
Definition quantiles_sketch_impl.hpp:673
double get_normalized_rank_error(bool is_pmf) const
Gets the normalized rank error for this sketch.
Definition quantiles_sketch_impl.hpp:720
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 quantiles_sketch_impl.hpp:771
void merge(FwdSk &&other)
Merges another sketch into this one.
Definition quantiles_sketch_impl.hpp:236
void update(FwdT &&item)
Updates this sketch with the given data item.
Definition quantiles_sketch_impl.hpp:213
void serialize(std::ostream &os, const SerDe &sd=SerDe()) const
This method serializes the sketch into a given stream in a binary form.
Definition quantiles_sketch_impl.hpp:278
string< Allocator > to_string(bool print_levels=false, bool print_items=false) const
Prints a summary of the sketch.
Definition quantiles_sketch_impl.hpp:604
uint16_t get_k() const
Returns configured parameter k.
Definition quantiles_sketch_impl.hpp:653
bool is_empty() const
Returns true if this sketch is empty.
Definition quantiles_sketch_impl.hpp:663
const T & get_max_item() const
Returns the max item of the stream.
Definition quantiles_sketch_impl.hpp:684
static quantiles_sketch deserialize(std::istream &is, const SerDe &sd=SerDe(), const Comparator &comparator=Comparator(), const Allocator &allocator=Allocator())
This method deserializes a sketch from a given stream.
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 quantiles_sketch_impl.hpp:764
static quantiles_sketch deserialize(const void *bytes, size_t size, const SerDe &sd=SerDe(), const Comparator &comparator=Comparator(), const Allocator &allocator=Allocator())
This method deserializes a sketch from a given array of bytes.
allocator_type get_allocator() const
Returns the allocator for this sketch.
Definition quantiles_sketch_impl.hpp:695
quantile_return_type get_quantile(double rank, bool inclusive=true) const
Returns an approximation to the data item associated with the given rank of a hypothetical sorted ver...
Definition quantiles_sketch_impl.hpp:753
const_iterator begin() const
Iterator pointing to the first item in the sketch.
Definition quantiles_sketch_impl.hpp:875
size_t get_serialized_size_bytes(const SerDe &sd=SerDe()) const
Computes size needed to serialize the current state of the sketch.
Definition quantiles_sketch_impl.hpp:702
bool is_estimation_mode() const
Returns true if this sketch is in estimation mode.
Definition quantiles_sketch_impl.hpp:668
quantiles_sketch(const quantiles_sketch< From, FC, FA > &other, const Comparator &comparator=Comparator(), const Allocator &allocator=Allocator())
Type converting constructor.
const T & get_min_item() const
Returns the min item of the stream.
Definition quantiles_sketch_impl.hpp:678
const_iterator end() const
Iterator pointing to the past-the-end item in the sketch.
Definition quantiles_sketch_impl.hpp:880
uint64_t get_n() const
Returns the length of the input stream.
Definition quantiles_sketch_impl.hpp:658
Sorted view for quantiles sketches (REQ, KLL and Quantiles)
Definition quantiles_sorted_view.hpp:38
typename std::conditional< std::is_arithmetic< T >::value, T, const T & >::type quantile_return_type
Quantile return type.
Definition quantiles_sorted_view.hpp:93
const uint16_t MAX_K
maximum value of parameter K
Definition quantiles_sketch.hpp:41
const uint16_t MIN_K
minimum value of parameter K
Definition quantiles_sketch.hpp:39
const uint16_t DEFAULT_K
default value of parameter K
Definition quantiles_sketch.hpp:37
DataSketches namespace.
Definition binomial_bounds.hpp:38
Interface for serializing and deserializing items.
Definition serde.hpp:34