datasketches-cpp
Loading...
Searching...
No Matches
req_sketch.hpp
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#ifndef REQ_SKETCH_HPP_
21#define REQ_SKETCH_HPP_
22
23#include <iterator>
24
25#include "req_common.hpp"
26#include "req_compactor.hpp"
27#include "quantiles_sorted_view.hpp"
28#include "optional.hpp"
29
30namespace datasketches {
31
79template<
80 typename T,
81 typename Comparator = std::less<T>, // strict weak ordering function (see C++ named requirements: Compare)
82 typename Allocator = std::allocator<T>
83>
85public:
86 using value_type = 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;
92
98
108 explicit req_sketch(uint16_t k, bool hra = true, const Comparator& comparator = Comparator(),
109 const Allocator& allocator = Allocator());
110
115 req_sketch(const req_sketch& other);
116
121 req_sketch(req_sketch&& other) noexcept;
122
123 ~req_sketch();
124
130 req_sketch& operator=(const req_sketch& other);
131
138
145 template<typename TT, typename CC, typename AA>
146 explicit req_sketch(const req_sketch<TT, CC, AA>& other, const Comparator& comparator = Comparator(),
147 const Allocator& allocator = Allocator());
148
153 uint16_t get_k() const;
154
159 bool is_HRA() const;
160
165 bool is_empty() const;
166
171 uint64_t get_n() const;
172
177 uint32_t get_num_retained() const;
178
183 bool is_estimation_mode() const;
184
191 template<typename FwdT>
192 void update(FwdT&& item);
193
200 template<typename FwdSk>
201 void merge(FwdSk&& other);
202
208 const T& get_min_item() const;
209
215 const T& get_max_item() const;
216
221 Comparator get_comparator() const;
222
227 Allocator get_allocator() const;
228
241 double get_rank(const T& item, bool inclusive = true) const;
242
262 vector_double get_PMF(const T* split_points, uint32_t size, bool inclusive = true) const;
263
286 vector_double get_CDF(const T* split_points, uint32_t size, bool inclusive = true) const;
287
298 quantile_return_type get_quantile(double rank, bool inclusive = true) const;
299
306 double get_rank_lower_bound(double rank, uint8_t num_std_dev) const;
307
314 double get_rank_upper_bound(double rank, uint8_t num_std_dev) const;
315
327 static double get_RSE(uint16_t k, double rank, bool hra, uint64_t n);
328
335 template<typename TT = T, typename SerDe = serde<T>, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type = 0>
336 size_t get_serialized_size_bytes(const SerDe& sd = SerDe()) const;
337
344 template<typename TT = T, typename SerDe = serde<T>, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type = 0>
345 size_t get_serialized_size_bytes(const SerDe& sd = SerDe()) const;
346
352 template<typename SerDe = serde<T>>
353 void serialize(std::ostream& os, const SerDe& sd = SerDe()) const;
354
355 // This is a convenience alias for users
356 // The type returned by the following serialize method
357 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>>;
358
367 template<typename SerDe = serde<T>>
368 vector_bytes serialize(unsigned header_size_bytes = 0, const SerDe& sd = SerDe()) const;
369
378 template<typename SerDe = serde<T>>
379 static req_sketch deserialize(std::istream& is, const SerDe& sd = SerDe(),
380 const Comparator& comparator = Comparator(), const Allocator& allocator = Allocator());
381
391 template<typename SerDe = serde<T>>
392 static req_sketch deserialize(const void* bytes, size_t size, const SerDe& sd = SerDe(),
393 const Comparator& comparator = Comparator(), const Allocator& allocator = Allocator());
394
400 string<Allocator> to_string(bool print_levels = false, bool print_items = false) const;
401
402 class const_iterator;
403
409 const_iterator begin() const;
410
417 const_iterator end() const;
418
424
425private:
426 Comparator comparator_;
427 Allocator allocator_;
428 uint16_t k_;
429 bool hra_;
430 uint32_t max_nom_size_;
431 uint32_t num_retained_;
432 uint64_t n_;
433 std::vector<Compactor, AllocCompactor> compactors_;
434 optional<T> min_item_;
435 optional<T> max_item_;
437
438 void setup_sorted_view() const; // modifies mutable state
439 void reset_sorted_view();
440
441 static const bool LAZY_COMPRESSION = false;
442
443 static const uint8_t SERIAL_VERSION = 1;
444 static const uint8_t FAMILY = 17;
445 static const size_t PREAMBLE_SIZE_BYTES = 8;
446 enum flags { RESERVED1, RESERVED2, IS_EMPTY, IS_HIGH_RANK, RAW_ITEMS, IS_LEVEL_ZERO_SORTED };
447
448 static constexpr double FIXED_RSE_FACTOR = 0.084;
449 static double relative_rse_factor();
450
451 uint8_t get_num_levels() const;
452 void grow();
453 void update_max_nom_size();
454 void update_num_retained();
455 void compress();
456
457 static double get_rank_lb(uint16_t k, uint8_t num_levels, double rank, uint8_t num_std_dev, uint64_t n, bool hra);
458 static double get_rank_ub(uint16_t k, uint8_t num_levels, double rank, uint8_t num_std_dev, uint64_t n, bool hra);
459 static bool is_exact_rank(uint16_t k, uint8_t num_levels, double rank, uint64_t n, bool hra);
460
461 // for deserialization
462 req_sketch(uint16_t k, bool hra, uint64_t n,
463 optional<T>&& min_item, optional<T>&& max_item,
464 std::vector<Compactor, AllocCompactor>&& compactors, const Comparator& comparator);
465
466 static void check_preamble_ints(uint8_t preamble_ints, uint8_t num_levels);
467 static void check_serial_version(uint8_t serial_version);
468 static void check_family_id(uint8_t family_id);
469
470 template<typename TT = T, typename std::enable_if<std::is_floating_point<TT>::value, int>::type = 0>
471 static inline bool check_update_item(const TT& item) {
472 return !std::isnan(item);
473 }
474
475 template<typename TT = T, typename std::enable_if<!std::is_floating_point<TT>::value, int>::type = 0>
476 static inline bool check_update_item(const TT&) {
477 return true;
478 }
479
480 // for type converting constructor
481 template<typename TT, typename CC, typename AA> friend class req_sketch;
482};
483
484template<typename T, typename C, typename A>
485class req_sketch<T, C, A>::const_iterator {
486public:
487 using iterator_category = std::input_iterator_tag;
488 using value_type = std::pair<const T&, const uint64_t>;
489 using difference_type = void;
490 using pointer = const return_value_holder<value_type>;
491 using reference = const value_type;
492
493 const_iterator& operator++();
494 const_iterator& operator++(int);
495 bool operator==(const const_iterator& other) const;
496 bool operator!=(const const_iterator& other) const;
497 reference operator*() const;
498 pointer operator->() const;
499private:
500 using LevelsIterator = typename std::vector<Compactor, AllocCompactor>::const_iterator;
501 LevelsIterator levels_it_;
502 LevelsIterator levels_end_;
503 const T* compactor_it_;
504 friend class req_sketch<T, C, A>;
505 const_iterator(LevelsIterator begin, LevelsIterator end);
506};
507
508} /* namespace datasketches */
509
510#include "req_sketch_impl.hpp"
511
512#endif
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:225
quantiles_sorted_view< T, Comparator, Allocator > get_sorted_view() const
Gets the sorted view of this sketch.
Definition req_sketch_impl.hpp:270
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:252
uint32_t get_num_retained() const
Returns the number of retained items in the sketch.
Definition req_sketch_impl.hpp:160
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:245
void merge(FwdSk &&other)
Merges another sketch into this one.
Definition req_sketch_impl.hpp:189
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:171
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:370
string< Allocator > to_string(bool print_levels=false, bool print_items=false) const
Prints a summary of the sketch.
Definition req_sketch_impl.hpp:640
uint16_t get_k() const
Returns configured parameter K.
Definition req_sketch_impl.hpp:140
bool is_empty() const
Returns true if this sketch is empty.
Definition req_sketch_impl.hpp:150
const T & get_max_item() const
Returns the max item of the stream.
Definition req_sketch_impl.hpp:219
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:290
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:235
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:295
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:285
Allocator get_allocator() const
Returns an instance of the allocator for this sketch.
Definition req_sketch_impl.hpp:230
req_sketch & operator=(const req_sketch &other)
Copy assignment.
Definition req_sketch_impl.hpp:82
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:259
const_iterator begin() const
Iterator pointing to the first item in the sketch.
Definition req_sketch_impl.hpp:733
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:335
const_iterator end() const
Iterator pointing to the past-the-end item in the sketch.
Definition req_sketch_impl.hpp:738
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:165
const T & get_min_item() const
Returns the min item of the stream.
Definition req_sketch_impl.hpp:213
bool is_HRA() const
Returns configured parameter High Rank Accuracy.
Definition req_sketch_impl.hpp:145
uint64_t get_n() const
Returns the length of the input stream.
Definition req_sketch_impl.hpp:155
DataSketches namespace.
Definition binomial_bounds.hpp:38