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
86template<
87 typename T,
88 typename Comparator = std::less<T>, // strict weak ordering function (see C++ named requirements: Compare)
89 typename Allocator = std::allocator<T>
90>
91class req_sketch {
92public:
93 using value_type = 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;
99
105
118 explicit req_sketch(uint16_t k, bool hra = true, const Comparator& comparator = Comparator(),
119 const Allocator& allocator = Allocator());
120
125 req_sketch(const req_sketch& other);
126
131 req_sketch(req_sketch&& other) noexcept;
132
133 ~req_sketch();
134
140 req_sketch& operator=(const req_sketch& other);
141
147 req_sketch& operator=(req_sketch&& other);
148
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());
158
163 uint16_t get_k() const;
164
169 bool is_HRA() const;
170
175 bool is_empty() const;
176
181 uint64_t get_n() const;
182
187 uint32_t get_num_retained() const;
188
193 bool is_estimation_mode() const;
194
202 template<typename FwdT>
203 void update(FwdT&& item);
204
213 template<typename FwdSk>
214 void merge(FwdSk&& other);
215
223 void reserve();
224
230 const T& get_min_item() const;
231
237 const T& get_max_item() const;
238
243 Comparator get_comparator() const;
244
249 Allocator get_allocator() const;
250
263 double get_rank(const T& item, bool inclusive = true) const;
264
284 vector_double get_PMF(const T* split_points, uint32_t size, bool inclusive = true) const;
285
308 vector_double get_CDF(const T* split_points, uint32_t size, bool inclusive = true) const;
309
320 quantile_return_type get_quantile(double rank, bool inclusive = true) const;
321
328 double get_rank_lower_bound(double rank, uint8_t num_std_dev) const;
329
336 double get_rank_upper_bound(double rank, uint8_t num_std_dev) const;
337
349 static double get_RSE(uint16_t k, double rank, bool hra, uint64_t n);
350
357 template<typename TT = T, typename SerDe = serde<T>, typename std::enable_if<std::is_arithmetic<TT>::value, int>::type = 0>
358 size_t get_serialized_size_bytes(const SerDe& sd = SerDe()) const;
359
366 template<typename TT = T, typename SerDe = serde<T>, typename std::enable_if<!std::is_arithmetic<TT>::value, int>::type = 0>
367 size_t get_serialized_size_bytes(const SerDe& sd = SerDe()) const;
368
374 template<typename SerDe = serde<T>>
375 void serialize(std::ostream& os, const SerDe& sd = SerDe()) const;
376
377 // This is a convenience alias for users
378 // The type returned by the following serialize method
379 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>>;
380
389 template<typename SerDe = serde<T>>
390 vector_bytes serialize(unsigned header_size_bytes = 0, const SerDe& sd = SerDe()) const;
391
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());
403
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());
416
422 string<Allocator> to_string(bool print_levels = false, bool print_items = false) const;
423
424 class const_iterator;
425
431 const_iterator begin() const;
432
439 const_iterator end() const;
440
446
447private:
448 Comparator comparator_;
449 Allocator allocator_;
450 uint16_t k_;
451 bool hra_;
452 uint32_t max_nom_size_;
453 uint32_t num_retained_;
454 uint64_t n_;
455 std::vector<Compactor, AllocCompactor> compactors_;
456 optional<T> min_item_;
457 optional<T> max_item_;
459
460 void setup_sorted_view() const; // modifies mutable state
461 void reset_sorted_view();
462
463 static const bool LAZY_COMPRESSION = false;
464
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 };
469
470 static constexpr double FIXED_RSE_FACTOR = 0.084;
471 static double relative_rse_factor();
472
473 uint8_t get_num_levels() const;
474 void grow();
475 void update_max_nom_size();
476 void update_num_retained();
477 // recomputes n, the number of retained items and the nominal size from the compactors,
478 // after an allocation failed partway through an update or a merge
479 void restore_counts();
480 void compress();
481 void compress_levels();
482 // removes the levels above the given number that hold no items, after a failed merge or compression
483 void remove_empty_levels_above(size_t num_levels);
484
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);
488
489 // for deserialization
490 req_sketch(uint16_t k, bool hra, uint64_t n,
491 optional<T>&& min_item, optional<T>&& max_item,
492 std::vector<Compactor, AllocCompactor>&& compactors, const Comparator& comparator);
493
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);
499 // in estimation mode, n is stored and must be the total weight of the retained items
500 static void check_n(uint64_t n, const std::vector<Compactor, AllocCompactor>& compactors);
501
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);
505 }
506
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&) {
509 return true;
510 }
511
512 // for type converting constructor
513 template<typename TT, typename CC, typename AA> friend class req_sketch;
514};
515
516template<typename T, typename C, typename A>
517class req_sketch<T, C, A>::const_iterator {
518public:
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;
524
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;
531private:
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);
538 // moves to the first item at or above the current level, skipping levels without items
539 void skip_empty_levels();
540};
541
542} /* namespace datasketches */
543
544#include "req_sketch_impl.hpp"
545
546#endif
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