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
110
111// forward declaration
112template<typename A> class HllSketchImpl;
113
114template<typename A = std::allocator<uint8_t> >
115class hll_sketch_alloc final {
116 public:
129 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());
130
135 hll_sketch_alloc(const hll_sketch_alloc<A>& that);
136
142 hll_sketch_alloc(const hll_sketch_alloc<A>& that, target_hll_type tgt_type);
143
148 hll_sketch_alloc(hll_sketch_alloc<A>&& that) noexcept;
149
155 static hll_sketch_alloc deserialize(std::istream& is, const A& allocator = A());
156
163 static hll_sketch_alloc deserialize(const void* bytes, size_t len, const A& allocator = A());
164
166 ~hll_sketch_alloc();
167
173 hll_sketch_alloc& operator=(const hll_sketch_alloc<A>& other);
174
180 hll_sketch_alloc& operator=(hll_sketch_alloc<A>&& other);
181
190 void reset(bool full_size = false);
191
192 // This is a convenience alias for users
193 // The type returned by the following serialize method
194 using vector_bytes = std::vector<uint8_t, typename std::allocator_traits<A>::template rebind_alloc<uint8_t>>;
195
202 vector_bytes serialize_compact(unsigned header_size_bytes = 0) const;
203
209 vector_bytes serialize_updatable() const;
210
216 void serialize_compact(std::ostream& os) const;
217
223 void serialize_updatable(std::ostream& os) const;
224
233 string<A> to_string(bool summary = true,
234 bool detail = false,
235 bool aux_detail = false,
236 bool all = false) const;
237
244 void update(const std::string& datum);
245
250 void update(uint64_t datum);
251
256 void update(uint32_t datum);
257
262 void update(uint16_t datum);
263
268 void update(uint8_t datum);
269
274 void update(int64_t datum);
275
280 void update(int32_t datum);
281
286 void update(int16_t datum);
287
292 void update(int8_t datum);
293
298 void update(double datum);
299
304 void update(float datum);
305
311 void update(const void* data, size_t length_bytes);
312
317 double get_estimate() const;
318
330 double get_composite_estimate() const;
331
338 double get_lower_bound(uint8_t num_std_dev) const;
339
346 double get_upper_bound(uint8_t num_std_dev) const;
347
352 uint8_t get_lg_config_k() const;
353
358 target_hll_type get_target_type() const;
359
364 bool is_compact() const;
365
370 bool is_empty() const;
371
376 uint32_t get_compact_serialization_bytes() const;
377
382 uint32_t get_updatable_serialization_bytes() const;
383
395 static uint32_t get_max_updatable_serialization_bytes(uint8_t lg_k, target_hll_type tgt_type);
396
407 static double get_rel_err(bool upper_bound, bool unioned,
408 uint8_t lg_config_k, uint8_t num_std_dev);
409
410 private:
411 explicit hll_sketch_alloc(HllSketchImpl<A>* that);
412
413 void coupon_update(uint32_t coupon);
414
415 std::string type_as_string() const;
416 std::string mode_as_string() const;
417
418 hll_mode get_current_mode() const;
419 uint8_t get_serialization_version() const;
420 bool is_out_of_order_flag() const;
421 bool is_estimation_mode() const;
422
423 HllSketchImpl<A>* sketch_impl;
424 friend hll_union_alloc<A>;
425};
426
452template<typename A = std::allocator<uint8_t> >
454 public:
461 explicit hll_union_alloc(uint8_t lg_max_k, const A& allocator = A());
462
467 double get_estimate() const;
468
481
488 double get_lower_bound(uint8_t num_std_dev) const;
489
496 double get_upper_bound(uint8_t num_std_dev) const;
497
502 uint8_t get_lg_config_k() const;
503
509
514 bool is_empty() const;
515
520 void reset();
521
528 hll_sketch_alloc<A> get_result(target_hll_type tgt_type = HLL_4) const;
529
534 void update(const hll_sketch_alloc<A>& sketch);
535
540 void update(hll_sketch_alloc<A>&& sketch);
541
548 void update(const std::string& datum);
549
554 void update(uint64_t datum);
555
560 void update(uint32_t datum);
561
566 void update(uint16_t datum);
567
572 void update(uint8_t datum);
573
578 void update(int64_t datum);
579
584 void update(int32_t datum);
585
590 void update(int16_t datum);
591
596 void update(int8_t datum);
597
602 void update(double datum);
603
608 void update(float datum);
609
615 void update(const void* data, size_t length_bytes);
616
627 static double get_rel_err(bool upper_bound, bool unioned,
628 uint8_t lg_config_k, uint8_t num_std_dev);
629
630 private:
631
641 inline void union_impl(const hll_sketch_alloc<A>& sketch, uint8_t lg_max_k);
642
643 static HllSketchImpl<A>* copy_or_downsample(const HllSketchImpl<A>* src_impl, uint8_t tgt_lg_k);
644
645 void coupon_update(uint32_t coupon);
646
647 hll_mode get_current_mode() const;
648 bool is_out_of_order_flag() const;
649 bool is_estimation_mode() const;
650
651 // calls couponUpdate on sketch, freeing the old sketch upon changes in hll_mode
652 static HllSketchImpl<A>* leak_free_coupon_update(HllSketchImpl<A>* impl, uint32_t coupon);
653
654 uint8_t lg_max_k_;
655 hll_sketch_alloc<A> gadget_;
656};
657
658} // namespace datasketches
659
660#include "hll.private.hpp"
661
662#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:453
target_hll_type get_target_type() const
Returns the union's target HLL mode (from target_hll_type).
Definition HllUnion-internal.hpp:200
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:144
void update(uint16_t datum)
Present the given unsigned 16-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:81
double get_estimate() const
Returns the current cardinality estimate.
Definition HllUnion-internal.hpp:136
void update(const hll_sketch_alloc< A > &sketch)
Update this union operator with the given sketch.
Definition HllUnion-internal.hpp:49
void update(const std::string &datum)
Present the given std::string as a potential unique item.
Definition HllUnion-internal.hpp:66
void update(int8_t datum)
Present the given signed 8-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:106
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:41
bool is_empty() const
Indicates if the union is currently empty.
Definition HllUnion-internal.hpp:180
void update(float datum)
Present the given 32-bit floating point value as a potential unique item.
Definition HllUnion-internal.hpp:116
void update(hll_sketch_alloc< A > &&sketch)
Update this union operator with the given temporary sketch.
Definition HllUnion-internal.hpp:55
void update(uint32_t datum)
Present the given unsigned 32-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:76
void update(int16_t datum)
Present the given signed 16-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:101
void update(int32_t datum)
Present the given signed 32-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:96
uint8_t get_lg_config_k() const
Returns union's configured lg_k value.
Definition HllUnion-internal.hpp:168
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:152
void update(const void *data, size_t length_bytes)
Present the given data array as a potential unique item.
Definition HllUnion-internal.hpp:121
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
void reset()
Resets the union to an empty state in coupon collection mode.
Definition HllUnion-internal.hpp:173
void update(double datum)
Present the given 64-bit floating point value as a potential unique item.
Definition HllUnion-internal.hpp:111
void update(int64_t datum)
Present the given signed 64-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:91
void update(uint8_t datum)
Present the given unsigned 8-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:86
void update(uint64_t datum)
Present the given unsigned 64-bit integer as a potential unique item.
Definition HllUnion-internal.hpp:71
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:205
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:160
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