20#ifndef REQ_SKETCH_HPP_
21#define REQ_SKETCH_HPP_
25#include "req_common.hpp"
26#include "req_compactor.hpp"
27#include "quantiles_sorted_view.hpp"
28#include "optional.hpp"
88 typename Comparator = std::less<T>,
89 typename Allocator = std::allocator<T>
94 using comparator = Comparator;
95 using allocator_type = Allocator;
96 using Compactor = req_compactor<T, Comparator, Allocator>;
97 using AllocCompactor =
typename std::allocator_traits<Allocator>::template rebind_alloc<Compactor>;
98 using vector_double =
typename quantiles_sorted_view<T, Comparator, Allocator>::vector_double;
118 explicit req_sketch(uint16_t k,
bool hra =
true,
const Comparator& comparator = Comparator(),
119 const Allocator& allocator = Allocator());
125 req_sketch(
const req_sketch& other);
131 req_sketch(req_sketch&& other)
noexcept;
140 req_sketch&
operator=(
const req_sketch& other);
147 req_sketch&
operator=(req_sketch&& other);
155 template<
typename TT,
typename CC,
typename AA>
156 explicit req_sketch(
const req_sketch<TT, CC, AA>& other,
const Comparator& comparator = Comparator(),
157 const Allocator& allocator = Allocator());
163 uint16_t
get_k()
const;
181 uint64_t
get_n()
const;
202 template<
typename FwdT>
213 template<
typename FwdSk>
214 void merge(FwdSk&& other);
263 double get_rank(
const T& item,
bool inclusive =
true)
const;
284 vector_double
get_PMF(
const T* split_points, uint32_t size,
bool inclusive =
true)
const;
308 vector_double
get_CDF(
const T* split_points, uint32_t size,
bool inclusive =
true)
const;
349 static double get_RSE(uint16_t k,
double rank,
bool hra, uint64_t n);
357 template<
typename TT = T,
typename SerDe = serde<T>,
typename std::enable_if<std::is_arithmetic<TT>::value,
int>::type = 0>
366 template<
typename TT = T,
typename SerDe = serde<T>,
typename std::enable_if<!std::is_arithmetic<TT>::value,
int>::type = 0>
374 template<
typename SerDe = serde<T>>
375 void serialize(std::ostream& os,
const SerDe& sd = SerDe())
const;
379 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>>;
389 template<
typename SerDe = serde<T>>
390 vector_bytes
serialize(
unsigned header_size_bytes = 0,
const SerDe& sd = SerDe())
const;
400 template<
typename SerDe = serde<T>>
401 static req_sketch
deserialize(std::istream& is,
const SerDe& sd = SerDe(),
402 const Comparator& comparator = Comparator(),
const Allocator& allocator = Allocator());
413 template<
typename SerDe = serde<T>>
414 static req_sketch
deserialize(
const void* bytes,
size_t size,
const SerDe& sd = SerDe(),
415 const Comparator& comparator = Comparator(),
const Allocator& allocator = Allocator());
422 string<Allocator>
to_string(
bool print_levels =
false,
bool print_items =
false)
const;
424 class const_iterator;
431 const_iterator
begin()
const;
439 const_iterator
end()
const;
448 Comparator comparator_;
449 Allocator allocator_;
452 uint32_t max_nom_size_;
453 uint32_t num_retained_;
455 std::vector<Compactor, AllocCompactor> compactors_;
456 optional<T> min_item_;
457 optional<T> max_item_;
460 void setup_sorted_view()
const;
461 void reset_sorted_view();
463 static const bool LAZY_COMPRESSION =
false;
465 static const uint8_t SERIAL_VERSION = 1;
466 static const uint8_t FAMILY = 17;
467 static const size_t PREAMBLE_SIZE_BYTES = 8;
468 enum flags { RESERVED1, RESERVED2, IS_EMPTY, IS_HIGH_RANK, RAW_ITEMS, IS_LEVEL_ZERO_SORTED };
470 static constexpr double FIXED_RSE_FACTOR = 0.084;
471 static double relative_rse_factor();
473 uint8_t get_num_levels()
const;
475 void update_max_nom_size();
476 void update_num_retained();
479 void restore_counts();
481 void compress_levels();
483 void remove_empty_levels_above(
size_t num_levels);
485 static double get_rank_lb(uint16_t k, uint8_t num_levels,
double rank, uint8_t num_std_dev, uint64_t n,
bool hra);
486 static double get_rank_ub(uint16_t k, uint8_t num_levels,
double rank, uint8_t num_std_dev, uint64_t n,
bool hra);
487 static bool is_exact_rank(uint16_t k, uint8_t num_levels,
double rank, uint64_t n,
bool hra);
491 optional<T>&& min_item, optional<T>&& max_item,
492 std::vector<Compactor, AllocCompactor>&& compactors,
const Comparator& comparator);
494 static void check_k(uint16_t k);
495 static void check_preamble_ints(uint8_t preamble_ints, uint8_t num_levels);
496 static void check_num_levels(uint8_t num_levels,
bool raw_items);
497 static void check_serial_version(uint8_t serial_version);
498 static void check_family_id(uint8_t family_id);
500 static void check_n(uint64_t n,
const std::vector<Compactor, AllocCompactor>& compactors);
502 template<typename TT = T, typename std::enable_if<std::is_floating_point<TT>::value,
int>::type = 0>
503 static inline bool check_update_item(
const TT& item) {
504 return !std::isnan(item);
507 template<typename TT = T, typename std::enable_if<!std::is_floating_point<TT>::value,
int>::type = 0>
508 static inline bool check_update_item(
const TT&) {
513 template<
typename TT,
typename CC,
typename AA>
friend class req_sketch;
516template<
typename T,
typename C,
typename A>
519 using iterator_category = std::input_iterator_tag;
520 using value_type = std::pair<const T&, const uint64_t>;
521 using difference_type = void;
522 using pointer =
const return_value_holder<value_type>;
523 using reference =
const value_type;
525 const_iterator& operator++();
526 const_iterator& operator++(
int);
527 bool operator==(
const const_iterator& other)
const;
528 bool operator!=(
const const_iterator& other)
const;
529 reference operator*()
const;
530 pointer operator->()
const;
532 using LevelsIterator =
typename std::vector<Compactor, AllocCompactor>::const_iterator;
533 LevelsIterator levels_it_;
534 LevelsIterator levels_end_;
535 const T* compactor_it_;
536 friend class req_sketch<T, C, A>;
537 const_iterator(LevelsIterator
begin, LevelsIterator
end);
539 void skip_empty_levels();
544#include "req_sketch_impl.hpp"
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
Relative Error Quantiles Sketch.
Definition req_sketch.hpp:91
void reserve()
Allocates storage for items now, rather than on the first update or merge.
Definition req_sketch_impl.hpp:225
Comparator get_comparator() const
Returns an instance of the comparator for this sketch.
Definition req_sketch_impl.hpp:242
quantiles_sorted_view< T, Comparator, Allocator > get_sorted_view() const
Gets the sorted view of this sketch.
Definition req_sketch_impl.hpp:287
static req_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.
req_sketch(const req_sketch< TT, CC, AA > &other, const Comparator &comparator=Comparator(), const Allocator &allocator=Allocator())
Type converting constructor.
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 req_sketch_impl.hpp:269
uint32_t get_num_retained() const
Returns the number of retained items in the sketch.
Definition req_sketch_impl.hpp:164
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 req_sketch_impl.hpp:262
void merge(FwdSk &&other)
Merges another sketch into this one.
Definition req_sketch_impl.hpp:194
typename quantiles_sorted_view< T, Comparator, Allocator >::quantile_return_type quantile_return_type
Quantile return type.
Definition req_sketch.hpp:104
void update(FwdT &&item)
Updates this sketch with the given data item.
Definition req_sketch_impl.hpp:175
void serialize(std::ostream &os, const SerDe &sd=SerDe()) const
This method serializes the sketch into a given stream in a binary form.
Definition req_sketch_impl.hpp:387
string< Allocator > to_string(bool print_levels=false, bool print_items=false) const
Prints a summary of the sketch.
Definition req_sketch_impl.hpp:713
uint16_t get_k() const
Returns configured parameter K.
Definition req_sketch_impl.hpp:144
bool is_empty() const
Returns true if this sketch is empty.
Definition req_sketch_impl.hpp:154
const T & get_max_item() const
Returns the max item of the stream.
Definition req_sketch_impl.hpp:236
req_sketch(uint16_t k, bool hra=true, const Comparator &comparator=Comparator(), const Allocator &allocator=Allocator())
Constructor.
double get_rank_upper_bound(double rank, uint8_t num_std_dev) const
Returns an approximate upper bound of the given normalized rank.
Definition req_sketch_impl.hpp:307
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 req_sketch_impl.hpp:252
static double get_RSE(uint16_t k, double rank, bool hra, uint64_t n)
Returns an a priori estimate of relative standard error (RSE, expressed as a number in [0,...
Definition req_sketch_impl.hpp:312
double get_rank_lower_bound(double rank, uint8_t num_std_dev) const
Returns an approximate lower bound of the given normalized rank.
Definition req_sketch_impl.hpp:302
Allocator get_allocator() const
Returns an instance of the allocator for this sketch.
Definition req_sketch_impl.hpp:247
req_sketch & operator=(const req_sketch &other)
Copy assignment.
Definition req_sketch_impl.hpp:86
quantile_return_type get_quantile(double rank, bool inclusive=true) const
Returns an approximate quantile of the given normalized rank.
Definition req_sketch_impl.hpp:276
const_iterator begin() const
Iterator pointing to the first item in the sketch.
Definition req_sketch_impl.hpp:823
size_t get_serialized_size_bytes(const SerDe &sd=SerDe()) const
Computes size needed to serialize the current state of the sketch.
Definition req_sketch_impl.hpp:352
const_iterator end() const
Iterator pointing to the past-the-end item in the sketch.
Definition req_sketch_impl.hpp:828
static req_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.
bool is_estimation_mode() const
Returns true if this sketch is in estimation mode.
Definition req_sketch_impl.hpp:169
const T & get_min_item() const
Returns the min item of the stream.
Definition req_sketch_impl.hpp:230
bool is_HRA() const
Returns configured parameter High Rank Accuracy.
Definition req_sketch_impl.hpp:149
uint64_t get_n() const
Returns the length of the input stream.
Definition req_sketch_impl.hpp:159
DataSketches namespace.
Definition binomial_bounds.hpp:38