datasketches-cpp
Loading...
Searching...
No Matches
theta_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 THETA_SKETCH_HPP_
21#define THETA_SKETCH_HPP_
22
23#include "theta_update_sketch_base.hpp"
24#include "compact_theta_sketch_parser.hpp"
25
26namespace datasketches {
27
28// forward declarations
29template<typename A> class theta_sketch_alloc;
30template<typename A> class update_theta_sketch_alloc;
31template<typename A> class compact_theta_sketch_alloc;
32template<typename A> class wrapped_compact_theta_sketch_alloc;
33
42
44template<typename Allocator = std::allocator<uint64_t>>
46public:
47
48 virtual ~base_theta_sketch_alloc() = default;
49
53 virtual Allocator get_allocator() const = 0;
54
58 virtual bool is_empty() const = 0;
59
63 double get_estimate() const;
64
72 double get_lower_bound(uint8_t num_std_devs) const;
73
81 double get_upper_bound(uint8_t num_std_devs) const;
82
86 bool is_estimation_mode() const;
87
91 double get_theta() const;
92
96 virtual uint64_t get_theta64() const = 0;
97
101 virtual uint32_t get_num_retained() const = 0;
102
106 virtual uint16_t get_seed_hash() const = 0;
107
111 virtual bool is_ordered() const = 0;
112
118 virtual string<Allocator> to_string(bool print_items = false) const;
119
120protected:
121 virtual void print_specifics(std::ostringstream& os) const = 0;
122 virtual void print_items(std::ostringstream& os) const = 0;
123};
124
126template<typename Allocator = std::allocator<uint64_t>>
128public:
129 using Entry = uint64_t;
130 using ExtractKey = trivial_extract_key;
131 using iterator = theta_iterator<Entry, ExtractKey>;
132 using const_iterator = theta_const_iterator<Entry, ExtractKey>;
133
134 virtual ~theta_sketch_alloc() = default;
135
140 virtual iterator begin() = 0;
141
147 virtual iterator end() = 0;
148
153 virtual const_iterator begin() const = 0;
154
160 virtual const_iterator end() const = 0;
161
162protected:
163 virtual void print_items(std::ostringstream& os) const;
164};
165
166// forward declaration
167template<typename A> class compact_theta_sketch_alloc;
168
174template<typename Allocator = std::allocator<uint64_t>>
176public:
178 using Entry = typename Base::Entry;
179 using ExtractKey = typename Base::ExtractKey;
180 using iterator = typename Base::iterator;
181 using const_iterator = typename Base::const_iterator;
182 using theta_table = theta_update_sketch_base<Entry, ExtractKey, Allocator>;
183 using resize_factor = typename theta_table::resize_factor;
184
185 // No constructor here. Use builder instead.
186 class builder;
187
193
199
200 virtual ~update_theta_sketch_alloc() = default;
201
208
215
216 virtual Allocator get_allocator() const;
217 virtual bool is_empty() const;
218 virtual bool is_ordered() const;
219 virtual uint16_t get_seed_hash() const;
220 virtual uint64_t get_theta64() const;
221 virtual uint32_t get_num_retained() const;
222
226 uint8_t get_lg_k() const;
227
231 resize_factor get_rf() const;
232
237 void update(const std::string& value);
238
243 void update(uint64_t value);
244
249 void update(int64_t value);
250
256 void update(uint32_t value);
257
263 void update(int32_t value);
264
270 void update(uint16_t value);
271
277 void update(int16_t value);
278
284 void update(uint8_t value);
285
291 void update(int8_t value);
292
298 void update(double value);
299
305 void update(float value);
306
320 void update(const void* data, size_t length);
321
325 void trim();
326
330 void reset();
331
351 compact_theta_sketch_alloc<Allocator> compact(bool ordered = true, bool trim = false) const;
352
353 virtual iterator begin();
354 virtual iterator end();
355 virtual const_iterator begin() const;
356 virtual const_iterator end() const;
357
358private:
359 theta_table table_;
360
361 // for builder
362 update_theta_sketch_alloc(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, float p,
363 uint64_t theta, uint64_t seed, const Allocator& allocator);
364
365 virtual void print_specifics(std::ostringstream& os) const;
366};
367
372template<typename Allocator = std::allocator<uint64_t>>
374public:
376 using iterator = typename Base::iterator;
377 using const_iterator = typename Base::const_iterator;
378 using AllocBytes = typename std::allocator_traits<Allocator>::template rebind_alloc<uint8_t>;
379 using vector_bytes = std::vector<uint8_t, AllocBytes>;
380
381 static const uint8_t UNCOMPRESSED_SERIAL_VERSION = 3;
382 static const uint8_t COMPRESSED_SERIAL_VERSION = 4;
383 static const uint8_t SKETCH_TYPE = 3;
384
385 // Instances of this type can be obtained:
386 // - by compacting an update_theta_sketch_alloc
387 // - as a result of a set operation
388 // - by deserializing a previously serialized compact sketch
389
396 template<typename Other>
397 compact_theta_sketch_alloc(const Other& other, bool ordered);
398
404
410
411 virtual ~compact_theta_sketch_alloc() = default;
412
419
426
427 virtual Allocator get_allocator() const;
428 virtual bool is_empty() const;
429 virtual bool is_ordered() const;
430 virtual uint64_t get_theta64() const;
431 virtual uint32_t get_num_retained() const;
432 virtual uint16_t get_seed_hash() const;
433
438 static size_t get_max_serialized_size_bytes(uint8_t lg_k);
439
446 size_t get_serialized_size_bytes(bool compressed = false) const;
447
452 void serialize(std::ostream& os) const;
453
461 vector_bytes serialize(unsigned header_size_bytes = 0) const;
462
469 void serialize_compressed(std::ostream& os) const;
470
480 vector_bytes serialize_compressed(unsigned header_size_bytes = 0) const;
481
482 virtual iterator begin();
483 virtual iterator end();
484 virtual const_iterator begin() const;
485 virtual const_iterator end() const;
486
495 uint64_t seed = DEFAULT_SEED, const Allocator& allocator = Allocator());
496
505 static compact_theta_sketch_alloc deserialize(const void* bytes, size_t size,
506 uint64_t seed = DEFAULT_SEED, const Allocator& allocator = Allocator());
507
508private:
509 enum flags { IS_BIG_ENDIAN, IS_READ_ONLY, IS_EMPTY, IS_COMPACT, IS_ORDERED, IS_SINGLE_ITEM };
510
511 bool is_empty_;
512 bool is_ordered_;
513 uint16_t seed_hash_;
514 uint64_t theta_;
515 std::vector<uint64_t, Allocator> entries_;
516
517 uint8_t get_preamble_longs(bool compressed) const;
518 bool is_single_item() const;
519 bool is_suitable_for_compression() const;
520 uint8_t compute_entry_bits() const;
521 uint8_t get_num_entries_bytes() const;
522 size_t get_compressed_serialized_size_bytes(uint8_t entry_bits, uint8_t num_entries_bytes) const;
523 void serialize_version_4(std::ostream& os) const;
524 vector_bytes serialize_version_4(unsigned header_size_bytes = 0) const;
525
526 static compact_theta_sketch_alloc deserialize_v1(uint8_t preamble_longs, std::istream& is, uint64_t seed, const Allocator& allocator);
527 static compact_theta_sketch_alloc deserialize_v2(uint8_t preamble_longs, std::istream& is, uint64_t seed, const Allocator& allocator);
528 static compact_theta_sketch_alloc deserialize_v3(uint8_t preamble_longs, std::istream& is, uint64_t seed, const Allocator& allocator);
529 static compact_theta_sketch_alloc deserialize_v4(uint8_t preamble_longs, std::istream& is, uint64_t seed, const Allocator& allocator);
530
531 virtual void print_specifics(std::ostringstream& os) const;
532
533 template<typename E, typename EK, typename P, typename S, typename CS, typename A> friend class theta_union_base;
534 template<typename E, typename EK, typename P, typename S, typename CS, typename A> friend class theta_intersection_base;
535 template<typename E, typename EK, typename CS, typename A> friend class theta_set_difference_base;
536 template<typename A> friend class update_theta_sketch_alloc;
537 compact_theta_sketch_alloc(bool is_empty, bool is_ordered, uint16_t seed_hash, uint64_t theta, std::vector<uint64_t, Allocator>&& entries);
538};
539
541template<typename Allocator>
542class update_theta_sketch_alloc<Allocator>::builder: public theta_base_builder<builder, Allocator> {
543public:
548 builder(const Allocator& allocator = Allocator());
551};
552
558template<typename Allocator = std::allocator<uint64_t>>
559class wrapped_compact_theta_sketch_alloc: public base_theta_sketch_alloc<Allocator> {
560public:
561 class const_iterator;
562
563 Allocator get_allocator() const;
564 bool is_empty() const;
565 bool is_ordered() const;
566 uint64_t get_theta64() const;
567 uint32_t get_num_retained() const;
568 uint16_t get_seed_hash() const;
569
574 const_iterator begin() const;
575
581 const_iterator end() const;
582
591 static const wrapped_compact_theta_sketch_alloc wrap(const void* bytes, size_t size, uint64_t seed = DEFAULT_SEED, bool dump_on_error = false);
592
593protected:
594 virtual void print_specifics(std::ostringstream& os) const;
595 virtual void print_items(std::ostringstream& os) const;
596
597private:
598 using data_type = compact_theta_sketch_parser<true>::compact_theta_sketch_data;
599 data_type data_;
600
601 wrapped_compact_theta_sketch_alloc(const data_type& data);
602};
603
604template<typename Allocator>
605class wrapped_compact_theta_sketch_alloc<Allocator>::const_iterator {
606public:
607 using iterator_category = std::input_iterator_tag;
608 using value_type = const uint64_t;
609 using difference_type = void;
610 using pointer = value_type*;
611 using reference = uint64_t;
612
613 const_iterator(const void* ptr, uint8_t entry_bits, uint32_t num_entries, uint32_t index);
614 const_iterator& operator++();
615 const_iterator operator++(int);
616 bool operator==(const const_iterator& other) const;
617 bool operator!=(const const_iterator& other) const;
618 reference operator*() const;
619 pointer operator->() const;
620
621private:
622 const void* ptr_;
623 uint8_t entry_bits_;
624 uint32_t num_entries_;
625 uint32_t index_;
626 uint64_t previous_;
627 bool is_block_mode_;
628 uint8_t offset_;
629 uint64_t buffer_[8];
630
631 inline void unpack1();
632 inline void unpack8();
633};
634
635} /* namespace datasketches */
636
637#include "theta_sketch_impl.hpp"
638
639#endif
Abstract base class for Theta sketch.
Definition theta_sketch.hpp:45
double get_estimate() const
Definition theta_sketch_impl.hpp:48
double get_lower_bound(uint8_t num_std_devs) const
Returns the approximate lower error bound given a number of standard deviations.
Definition theta_sketch_impl.hpp:53
virtual bool is_ordered() const =0
virtual bool is_empty() const =0
virtual string< Allocator > to_string(bool print_items=false) const
Provides a human-readable summary of this sketch as a string.
Definition theta_sketch_impl.hpp:65
virtual uint32_t get_num_retained() const =0
double get_upper_bound(uint8_t num_std_devs) const
Returns the approximate upper error bound given a number of standard deviations.
Definition theta_sketch_impl.hpp:59
double get_theta() const
Definition theta_sketch_impl.hpp:42
virtual uint16_t get_seed_hash() const =0
bool is_estimation_mode() const
Definition theta_sketch_impl.hpp:37
virtual Allocator get_allocator() const =0
virtual uint64_t get_theta64() const =0
Compact Theta sketch.
Definition theta_sketch.hpp:373
compact_theta_sketch_alloc & operator=(compact_theta_sketch_alloc &&other)=default
Move assignment.
compact_theta_sketch_alloc(const Other &other, bool ordered)
Copy constructor.
Definition theta_sketch_impl.hpp:281
compact_theta_sketch_alloc(const compact_theta_sketch_alloc &other)=default
Copy constructor.
compact_theta_sketch_alloc & operator=(const compact_theta_sketch_alloc &other)=default
Copy assignment.
void serialize(std::ostream &os) const
This method serializes the sketch into a given stream in a binary form.
Definition theta_sketch_impl.hpp:392
vector_bytes serialize(unsigned header_size_bytes=0) const
This method serializes the sketch as a vector of bytes.
Definition theta_sketch_impl.hpp:416
virtual uint64_t get_theta64() const
Definition theta_sketch_impl.hpp:321
static compact_theta_sketch_alloc deserialize(const void *bytes, size_t size, uint64_t seed=DEFAULT_SEED, const Allocator &allocator=Allocator())
This method deserializes a sketch from a given array of bytes.
virtual uint32_t get_num_retained() const
Definition theta_sketch_impl.hpp:326
virtual bool is_empty() const
Definition theta_sketch_impl.hpp:311
virtual bool is_ordered() const
Definition theta_sketch_impl.hpp:316
virtual uint16_t get_seed_hash() const
Definition theta_sketch_impl.hpp:331
virtual iterator end()
Iterator pointing past the valid range.
Definition theta_sketch_impl.hpp:341
static size_t get_max_serialized_size_bytes(uint8_t lg_k)
Computes maximum serialized size in bytes.
Definition theta_sketch_impl.hpp:367
static compact_theta_sketch_alloc deserialize(std::istream &is, uint64_t seed=DEFAULT_SEED, const Allocator &allocator=Allocator())
This method deserializes a sketch from a given stream.
virtual Allocator get_allocator() const
Definition theta_sketch_impl.hpp:306
size_t get_serialized_size_bytes(bool compressed=false) const
Computes size in bytes required to serialize the current state of the sketch.
Definition theta_sketch_impl.hpp:372
compact_theta_sketch_alloc(compact_theta_sketch_alloc &&other) noexcept=default
Move constructor.
void serialize_compressed(std::ostream &os) const
This method serializes the sketch into a given stream in a compressed binary form.
Definition theta_sketch_impl.hpp:457
virtual const_iterator begin() const
Const iterator over hash values in this sketch.
Definition theta_sketch_impl.hpp:346
virtual iterator begin()
Iterator over hash values in this sketch.
Definition theta_sketch_impl.hpp:336
virtual const_iterator end() const
Const iterator pointing past the valid range.
Definition theta_sketch_impl.hpp:351
vector_bytes serialize_compressed(unsigned header_size_bytes=0) const
This method serializes the sketch as a vector of bytes.
Definition theta_sketch_impl.hpp:463
theta_base_builder(const Allocator &allocator)
Definition theta_update_sketch_base_impl.hpp:304
Base class for the Theta Sketch, a generalization of the Kth Minimum Value (KMV) sketch.
Definition theta_sketch.hpp:127
virtual const_iterator begin() const =0
Const iterator over hash values in this sketch.
virtual const_iterator end() const =0
Const iterator pointing past the valid range.
virtual iterator end()=0
Iterator pointing past the valid range.
virtual iterator begin()=0
Iterator over hash values in this sketch.
Update Theta sketch builder.
Definition theta_sketch.hpp:542
builder(const Allocator &allocator=Allocator())
Constructor.
Definition theta_sketch_impl.hpp:270
update_theta_sketch_alloc build() const
Definition theta_sketch_impl.hpp:273
Update Theta sketch.
Definition theta_sketch.hpp:175
void update(int16_t value)
Update this sketch with a given signed 16-bit integer.
Definition theta_sketch_impl.hpp:172
void trim()
Remove retained entries in excess of the nominal size k (if any).
Definition theta_sketch_impl.hpp:213
virtual uint64_t get_theta64() const
Definition theta_sketch_impl.hpp:122
virtual uint32_t get_num_retained() const
Definition theta_sketch_impl.hpp:127
resize_factor get_rf() const
Definition theta_sketch_impl.hpp:142
void update(int64_t value)
Update this sketch with a given signed 64-bit integer.
Definition theta_sketch_impl.hpp:152
void update(const std::string &value)
Update this sketch with a given string.
Definition theta_sketch_impl.hpp:197
virtual bool is_empty() const
Definition theta_sketch_impl.hpp:112
void update(uint64_t value)
Update this sketch with a given unsigned 64-bit integer.
Definition theta_sketch_impl.hpp:147
virtual bool is_ordered() const
Definition theta_sketch_impl.hpp:117
void update(double value)
Update this sketch with a given double-precision floating point value.
Definition theta_sketch_impl.hpp:187
virtual uint16_t get_seed_hash() const
Definition theta_sketch_impl.hpp:132
update_theta_sketch_alloc(update_theta_sketch_alloc &&other) noexcept=default
Move constructor.
compact_theta_sketch_alloc< Allocator > compact(bool ordered=true, bool trim=false) const
Converts this sketch to a compact sketch (ordered or unordered, trimmed or not trimmed).
Definition theta_sketch_impl.hpp:243
virtual iterator end()
Iterator pointing past the valid range.
Definition theta_sketch_impl.hpp:228
void update(float value)
Update this sketch with a given floating point value.
Definition theta_sketch_impl.hpp:192
uint8_t get_lg_k() const
Definition theta_sketch_impl.hpp:137
virtual Allocator get_allocator() const
Definition theta_sketch_impl.hpp:107
update_theta_sketch_alloc & operator=(const update_theta_sketch_alloc &other)=default
Copy assignment.
void update(uint32_t value)
Update this sketch with a given unsigned 32-bit integer.
Definition theta_sketch_impl.hpp:157
update_theta_sketch_alloc & operator=(update_theta_sketch_alloc &&other)=default
Move assignment.
void update(int32_t value)
Update this sketch with a given signed 32-bit integer.
Definition theta_sketch_impl.hpp:162
void reset()
Reset the sketch to the initial empty state.
Definition theta_sketch_impl.hpp:218
virtual const_iterator begin() const
Const iterator over hash values in this sketch.
Definition theta_sketch_impl.hpp:233
void update(const void *data, size_t length)
Update this sketch with given data of any type.
Definition theta_sketch_impl.hpp:203
virtual iterator begin()
Iterator over hash values in this sketch.
Definition theta_sketch_impl.hpp:223
virtual const_iterator end() const
Const iterator pointing past the valid range.
Definition theta_sketch_impl.hpp:238
void update(int8_t value)
Update this sketch with a given signed 8-bit integer.
Definition theta_sketch_impl.hpp:182
void update(uint8_t value)
Update this sketch with a given unsigned 8-bit integer.
Definition theta_sketch_impl.hpp:177
void update(uint16_t value)
Update this sketch with a given unsigned 16-bit integer.
Definition theta_sketch_impl.hpp:167
update_theta_sketch_alloc(const update_theta_sketch_alloc &other)=default
Copy constructor.
Wrapped Compact Theta sketch.
Definition theta_sketch.hpp:559
uint64_t get_theta64() const
Definition theta_sketch_impl.hpp:798
uint32_t get_num_retained() const
Definition theta_sketch_impl.hpp:803
bool is_empty() const
Definition theta_sketch_impl.hpp:788
bool is_ordered() const
Definition theta_sketch_impl.hpp:793
uint16_t get_seed_hash() const
Definition theta_sketch_impl.hpp:808
static const wrapped_compact_theta_sketch_alloc wrap(const void *bytes, size_t size, uint64_t seed=DEFAULT_SEED, bool dump_on_error=false)
This method wraps a serialized compact sketch as an array of bytes.
Definition theta_sketch_impl.hpp:778
Allocator get_allocator() const
Definition theta_sketch_impl.hpp:783
const_iterator begin() const
Const iterator over hash values in this sketch.
Definition theta_sketch_impl.hpp:813
const_iterator end() const
Const iterator pointing past the valid range.
Definition theta_sketch_impl.hpp:818
DataSketches namespace.
Definition binomial_bounds.hpp:38
wrapped_compact_theta_sketch_alloc< std::allocator< uint64_t > > wrapped_compact_theta_sketch
Wrapped Compact Theta sketch alias with default allocator.
Definition theta_sketch.hpp:41
update_theta_sketch_alloc< std::allocator< uint64_t > > update_theta_sketch
Update Theta sketch alias with default allocator.
Definition theta_sketch.hpp:37
compact_theta_sketch_alloc< std::allocator< uint64_t > > compact_theta_sketch
Compact Theta sketch alias with default allocator.
Definition theta_sketch.hpp:39
theta_sketch_alloc< std::allocator< uint64_t > > theta_sketch
Theta sketch alias with default allocator.
Definition theta_sketch.hpp:35