datasketches-cpp
Loading...
Searching...
No Matches
hll.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 _HLL_HPP_
21#define _HLL_HPP_
22
23#include "common_defs.hpp"
24#include "HllUtil.hpp"
25
26#include <iostream>
27#include <memory>
28#include <string>
29#include <vector>
30
31namespace datasketches {
32
33// forward declarations
34template<typename A> class hll_sketch_alloc;
35template<typename A> class hll_union_alloc;
36
38using hll_sketch = hll_sketch_alloc<std::allocator<uint8_t>>;
41
77
113
114// forward declaration
115template<typename A> class HllSketchImpl;
116
117template<typename A = std::allocator<uint8_t> >
118class hll_sketch_alloc final {
119 public:
132 explicit hll_sketch_alloc(uint8_t lg_config_k, target_hll_type tgt_type = HLL_4, bool start_full_size = false, const A& allocator = A());
133
138 hll_sketch_alloc(const hll_sketch_alloc<A>& that);
139
145 hll_sketch_alloc(const hll_sketch_alloc<A>& that, target_hll_type tgt_type);
146
151 hll_sketch_alloc(hll_sketch_alloc<A>&& that) noexcept;
152
158 static hll_sketch_alloc deserialize(std::istream& is, const A& allocator = A());
159
166 static hll_sketch_alloc deserialize(const void* bytes, size_t len, const A& allocator = A());
167
169 ~hll_sketch_alloc();
170
176 hll_sketch_alloc& operator=(const hll_sketch_alloc<A>& other);
177
183 hll_sketch_alloc& operator=(hll_sketch_alloc<A>&& other);
184
193 void reset(bool full_size = false);
194
195 // This is a convenience alias for users
196 // The type returned by the following serialize method
197 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<A>::template rebind_alloc<uint8_t>>;
198
205 vector_bytes serialize_compact(unsigned header_size_bytes = 0) const;
206
212 vector_bytes serialize_updatable() const;
213
219 void serialize_compact(std::ostream& os) const;
220
226 void serialize_updatable(std::ostream& os) const;
227
236 string<A> to_string(bool summary = true,
237 bool detail = false,
238 bool aux_detail = false,
239 bool all = false) const;
240
247 void update(const std::string& datum);
248
253 void update(uint64_t datum);
254
259 void update(uint32_t datum);
260
265 void update(uint16_t datum);
266
271 void update(uint8_t datum);
272
277 void update(int64_t datum);
278
283 void update(int32_t datum);
284
289 void update(int16_t datum);
290
295 void update(int8_t datum);
296
301 void update(double datum);
302
307 void update(float datum);
308
314 void update(const void* data, size_t length_bytes);
315
320 double get_estimate() const;
321
333 double get_composite_estimate() const;
334
341 double get_lower_bound(uint8_t num_std_dev) const;
342
349 double get_upper_bound(uint8_t num_std_dev) const;
350
355 uint8_t get_lg_config_k() const;
356
361 target_hll_type get_target_type() const;
362
367 bool is_compact() const;
368
373 bool is_empty() const;
374
379 uint32_t get_compact_serialization_bytes() const;
380
385 uint32_t get_updatable_serialization_bytes() const;
386
398 static uint32_t get_max_updatable_serialization_bytes(uint8_t lg_k, target_hll_type tgt_type);
399
410 static double get_rel_err(bool upper_bound, bool unioned,
411 uint8_t lg_config_k, uint8_t num_std_dev);
412
413 private:
414 explicit hll_sketch_alloc(HllSketchImpl<A>* that);
415
416 void coupon_update(uint32_t coupon);
417
418 std::string type_as_string() const;
419 std::string mode_as_string() const;
420
421 hll_mode get_current_mode() const;
422 uint8_t get_serialization_version() const;
423 bool is_out_of_order_flag() const;
424 bool is_estimation_mode() const;
425
426 HllSketchImpl<A>* sketch_impl;
427 friend hll_union_alloc<A>;
428};
429
461template<typename A = std::allocator<uint8_t> >
463 public:
470 explicit hll_union_alloc(uint8_t lg_max_k, const A& allocator = A());
471
476 hll_union_alloc(const hll_union_alloc& that) = default;
477
483
490
497
502 double get_estimate() const;
503
516
523 double get_lower_bound(uint8_t num_std_dev) const;
524
531 double get_upper_bound(uint8_t num_std_dev) const;
532
537 uint8_t get_lg_config_k() const;
538
544
549 bool is_empty() const;
550
555 void reset();
556
563 hll_sketch_alloc<A> get_result(target_hll_type tgt_type = HLL_4) const;
564
569 void update(const hll_sketch_alloc<A>& sketch);
570
575 void update(hll_sketch_alloc<A>&& sketch);
576
583 void update(const std::string& datum);
584
589 void update(uint64_t datum);
590
595 void update(uint32_t datum);
596
601 void update(uint16_t datum);
602
607 void update(uint8_t datum);
608
613 void update(int64_t datum);
614
619 void update(int32_t datum);
620
625 void update(int16_t datum);
626
631 void update(int8_t datum);
632
637 void update(double datum);
638
643 void update(float datum);
644
650 void update(const void* data, size_t length_bytes);
651
662 static double get_rel_err(bool upper_bound, bool unioned,
663 uint8_t lg_config_k, uint8_t num_std_dev);
664
665 private:
666
676 inline void union_impl(const hll_sketch_alloc<A>& sketch, uint8_t lg_max_k);
677
678 static HllSketchImpl<A>* copy_or_downsample(const HllSketchImpl<A>* src_impl, uint8_t tgt_lg_k, const A& allocator);
679
680 void coupon_update(uint32_t coupon);
681
682 hll_mode get_current_mode() const;
683 bool is_out_of_order_flag() const;
684 bool is_estimation_mode() const;
685
686 uint8_t lg_max_k_;
687 hll_sketch_alloc<A> gadget_;
688};
689
690} // namespace datasketches
691
692#include "hll.private.hpp"
693
694#endif // _HLL_HPP_
This is a high performance implementation of Phillipe Flajolet's HLL sketch but with significantly im...
Definition HllSketchImpl.hpp:31
This performs union operations for HLL sketches.
Definition hll.hpp:462
target_hll_type get_target_type() const
Returns the union's target HLL mode (from target_hll_type).
Definition HllUnion-internal.hpp:210
hll_union_alloc(hll_union_alloc &&that)=default
Move constructor.
double get_composite_estimate() const
This is less accurate than the get_estimate() method and is automatically used when the union has gon...
Definition HllUnion-internal.hpp:154
hll_union_alloc(const hll_union_alloc &that)=default
Copy constructor.
void update(uint16_t datum)
Present the given unsigned 16-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:91
double get_estimate() const
Returns the current cardinality estimate.
Definition HllUnion-internal.hpp:146
void update(const hll_sketch_alloc< A > &sketch)
Update this union operator with the given sketch.
Definition HllUnion-internal.hpp:57
void update(const std::string &datum)
Present the given std::string as a potential unique item.
Definition HllUnion-internal.hpp:76
void update(int8_t datum)
Present the given signed 8-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:116
hll_sketch_alloc< A > get_result(target_hll_type tgt_type=HLL_4) const
Returns the result of this union operator with the specified target_hll_type.
Definition HllUnion-internal.hpp:49
bool is_empty() const
Indicates if the union is currently empty.
Definition HllUnion-internal.hpp:190
void update(float datum)
Present the given 32-bit floating point value as a potential unique item.
Definition HllUnion-internal.hpp:126
void update(hll_sketch_alloc< A > &&sketch)
Update this union operator with the given temporary sketch.
Definition HllUnion-internal.hpp:63
void update(uint32_t datum)
Present the given unsigned 32-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:86
void update(int16_t datum)
Present the given signed 16-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:111
void update(int32_t datum)
Present the given signed 32-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:106
uint8_t get_lg_config_k() const
Returns union's configured lg_k value.
Definition HllUnion-internal.hpp:178
double get_lower_bound(uint8_t num_std_dev) const
Returns the approximate lower error bound given the specified number of standard deviations.
Definition HllUnion-internal.hpp:162
void update(const void *data, size_t length_bytes)
Present the given data array as a potential unique item.
Definition HllUnion-internal.hpp:131
hll_union_alloc(uint8_t lg_max_k, const A &allocator=A())
Construct an hll_union operator with the given maximum log2 of k.
Definition HllUnion-internal.hpp:35
hll_union_alloc & operator=(hll_union_alloc &&other)=default
Move assignment operator.
hll_union_alloc & operator=(const hll_union_alloc &other)
Copy assignment operator.
void reset()
Resets the union to an empty state in coupon collection mode.
Definition HllUnion-internal.hpp:183
void update(double datum)
Present the given 64-bit floating point value as a potential unique item.
Definition HllUnion-internal.hpp:121
void update(int64_t datum)
Present the given signed 64-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:101
void update(uint8_t datum)
Present the given unsigned 8-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:96
void update(uint64_t datum)
Present the given unsigned 64-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:81
static double get_rel_err(bool upper_bound, bool unioned, uint8_t lg_config_k, uint8_t num_std_dev)
Gets the current (approximate) Relative Error (RE) asymptotic values given several parameters.
Definition HllUnion-internal.hpp:215
double get_upper_bound(uint8_t num_std_dev) const
Returns the approximate upper error bound given the specified number of standard deviations.
Definition HllUnion-internal.hpp:170
DataSketches namespace.
Definition binomial_bounds.hpp:38
target_hll_type
Specifies the target type of HLL sketch to be created.
Definition hll.hpp:72
@ HLL_6
6 bits per entry (fixed size)
Definition hll.hpp:74
@ HLL_8
8 bits per entry (fastest, fixed size)
Definition hll.hpp:75
@ HLL_4
4 bits per entry (most compact, size may vary)
Definition hll.hpp:73
hll_union_alloc< std::allocator< uint8_t > > hll_union
HLL union alias with default allocator.
Definition hll.hpp:40
hll_sketch_alloc< std::allocator< uint8_t > > hll_sketch
HLL sketch alias with default allocator.
Definition hll.hpp:38