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"
81 typename Comparator = std::less<T>,
82 typename Allocator = std::allocator<T>
87 using comparator = Comparator;
88 using allocator_type = Allocator;
89 using Compactor = req_compactor<T, Comparator, Allocator>;
90 using AllocCompactor =
typename std::allocator_traits<Allocator>::template rebind_alloc<Compactor>;
91 using vector_double =
typename quantiles_sorted_view<T, Comparator, Allocator>::vector_double;
110 explicit req_sketch(uint16_t k,
bool hra =
true,
const Comparator& comparator = Comparator(),
111 const Allocator& allocator = Allocator());
117 req_sketch(
const req_sketch& other);
123 req_sketch(req_sketch&& other)
noexcept;
132 req_sketch&
operator=(
const req_sketch& other);
139 req_sketch&
operator=(req_sketch&& other);
147 template<
typename TT,
typename CC,
typename AA>
148 explicit req_sketch(
const req_sketch<TT, CC, AA>& other,
const Comparator& comparator = Comparator(),
149 const Allocator& allocator = Allocator());
155 uint16_t
get_k()
const;
173 uint64_t
get_n()
const;
193 template<
typename FwdT>
202 template<
typename FwdSk>
203 void merge(FwdSk&& other);
243 double get_rank(
const T& item,
bool inclusive =
true)
const;
264 vector_double
get_PMF(
const T* split_points, uint32_t size,
bool inclusive =
true)
const;
288 vector_double
get_CDF(
const T* split_points, uint32_t size,
bool inclusive =
true)
const;
329 static double get_RSE(uint16_t k,
double rank,
bool hra, uint64_t n);
337 template<
typename TT = T,
typename SerDe = serde<T>,
typename std::enable_if<std::is_arithmetic<TT>::value,
int>::type = 0>
346 template<
typename TT = T,
typename SerDe = serde<T>,
typename std::enable_if<!std::is_arithmetic<TT>::value,
int>::type = 0>
354 template<
typename SerDe = serde<T>>
355 void serialize(std::ostream& os,
const SerDe& sd = SerDe())
const;
359 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>>;
369 template<
typename SerDe = serde<T>>
370 vector_bytes
serialize(
unsigned header_size_bytes = 0,
const SerDe& sd = SerDe())
const;
380 template<
typename SerDe = serde<T>>
381 static req_sketch
deserialize(std::istream& is,
const SerDe& sd = SerDe(),
382 const Comparator& comparator = Comparator(),
const Allocator& allocator = Allocator());
393 template<
typename SerDe = serde<T>>
394 static req_sketch
deserialize(
const void* bytes,
size_t size,
const SerDe& sd = SerDe(),
395 const Comparator& comparator = Comparator(),
const Allocator& allocator = Allocator());
402 string<Allocator>
to_string(
bool print_levels =
false,
bool print_items =
false)
const;
404 class const_iterator;
411 const_iterator
begin()
const;
419 const_iterator
end()
const;
428 Comparator comparator_;
429 Allocator allocator_;
432 uint32_t max_nom_size_;
433 uint32_t num_retained_;
435 std::vector<Compactor, AllocCompactor> compactors_;
436 optional<T> min_item_;
437 optional<T> max_item_;
440 void setup_sorted_view()
const;
441 void reset_sorted_view();
443 static const bool LAZY_COMPRESSION =
false;
445 static const uint8_t SERIAL_VERSION = 1;
446 static const uint8_t FAMILY = 17;
447 static const size_t PREAMBLE_SIZE_BYTES = 8;
448 enum flags { RESERVED1, RESERVED2, IS_EMPTY, IS_HIGH_RANK, RAW_ITEMS, IS_LEVEL_ZERO_SORTED };
450 static constexpr double FIXED_RSE_FACTOR = 0.084;
451 static double relative_rse_factor();
453 uint8_t get_num_levels()
const;
455 void update_max_nom_size();
456 void update_num_retained();
459 static double get_rank_lb(uint16_t k, uint8_t num_levels,
double rank, uint8_t num_std_dev, uint64_t n,
bool hra);
460 static double get_rank_ub(uint16_t k, uint8_t num_levels,
double rank, uint8_t num_std_dev, uint64_t n,
bool hra);
461 static bool is_exact_rank(uint16_t k, uint8_t num_levels,
double rank, uint64_t n,
bool hra);
465 optional<T>&& min_item, optional<T>&& max_item,
466 std::vector<Compactor, AllocCompactor>&& compactors,
const Comparator& comparator);
468 static void check_k(uint16_t k);
469 static void check_preamble_ints(uint8_t preamble_ints, uint8_t num_levels);
470 static void check_num_levels(uint8_t num_levels,
bool raw_items);
471 static void check_serial_version(uint8_t serial_version);
472 static void check_family_id(uint8_t family_id);
474 template<typename TT = T, typename std::enable_if<std::is_floating_point<TT>::value,
int>::type = 0>
475 static inline bool check_update_item(
const TT& item) {
476 return !std::isnan(item);
479 template<typename TT = T, typename std::enable_if<!std::is_floating_point<TT>::value,
int>::type = 0>
480 static inline bool check_update_item(
const TT&) {
485 template<
typename TT,
typename CC,
typename AA>
friend class req_sketch;
488template<
typename T,
typename C,
typename A>
491 using iterator_category = std::input_iterator_tag;
492 using value_type = std::pair<const T&, const uint64_t>;
493 using difference_type = void;
494 using pointer =
const return_value_holder<value_type>;
495 using reference =
const value_type;
497 const_iterator& operator++();
498 const_iterator& operator++(
int);
499 bool operator==(
const const_iterator& other)
const;
500 bool operator!=(
const const_iterator& other)
const;
501 reference operator*()
const;
502 pointer operator->()
const;
504 using LevelsIterator =
typename std::vector<Compactor, AllocCompactor>::const_iterator;
505 LevelsIterator levels_it_;
506 LevelsIterator levels_end_;
507 const T* compactor_it_;
508 friend class req_sketch<T, C, A>;
509 const_iterator(LevelsIterator
begin, LevelsIterator
end);
514#include "req_sketch_impl.hpp"
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
Relative Error Quantiles Sketch.
Definition req_sketch.hpp:84
Comparator get_comparator() const
Returns an instance of the comparator for this sketch.
Definition req_sketch_impl.hpp:228
quantiles_sorted_view< T, Comparator, Allocator > get_sorted_view() const
Gets the sorted view of this sketch.
Definition req_sketch_impl.hpp:273
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:255
uint32_t get_num_retained() const
Returns the number of retained items in the sketch.
Definition req_sketch_impl.hpp:163
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:248
void merge(FwdSk &&other)
Merges another sketch into this one.
Definition req_sketch_impl.hpp:192
typename quantiles_sorted_view< T, Comparator, Allocator >::quantile_return_type quantile_return_type
Quantile return type.
Definition req_sketch.hpp:97
void update(FwdT &&item)
Updates this sketch with the given data item.
Definition req_sketch_impl.hpp:174
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:373
string< Allocator > to_string(bool print_levels=false, bool print_items=false) const
Prints a summary of the sketch.
Definition req_sketch_impl.hpp:651
uint16_t get_k() const
Returns configured parameter K.
Definition req_sketch_impl.hpp:143
bool is_empty() const
Returns true if this sketch is empty.
Definition req_sketch_impl.hpp:153
const T & get_max_item() const
Returns the max item of the stream.
Definition req_sketch_impl.hpp:222
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:293
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:238
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:298
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:288
Allocator get_allocator() const
Returns an instance of the allocator for this sketch.
Definition req_sketch_impl.hpp:233
req_sketch & operator=(const req_sketch &other)
Copy assignment.
Definition req_sketch_impl.hpp:85
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:262
const_iterator begin() const
Iterator pointing to the first item in the sketch.
Definition req_sketch_impl.hpp:761
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:338
const_iterator end() const
Iterator pointing to the past-the-end item in the sketch.
Definition req_sketch_impl.hpp:766
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:168
const T & get_min_item() const
Returns the min item of the stream.
Definition req_sketch_impl.hpp:216
bool is_HRA() const
Returns configured parameter High Rank Accuracy.
Definition req_sketch_impl.hpp:148
uint64_t get_n() const
Returns the length of the input stream.
Definition req_sketch_impl.hpp:158
DataSketches namespace.
Definition binomial_bounds.hpp:38