|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
HyperLogLog cardinality estimator. More...
#include <hyperloglog.H>
Public Member Functions | |
| HyperLogLog (const uint8_t b=12) | |
| Construct with precision parameter. | |
| HyperLogLog (const HyperLogLog &other)=default | |
| Copy the precision and the registers of another estimator. | |
| HyperLogLog (HyperLogLog &&other) | |
| Take the registers of another estimator, which is left as an empty estimator of the same precision. | |
| HyperLogLog & | operator= (const HyperLogLog &other)=default |
| Copy the precision and the registers of another estimator. | |
| HyperLogLog & | operator= (HyperLogLog &&other) noexcept |
| Exchange this estimator with another, which is left with the previous state of this one. | |
| void | swap (HyperLogLog &other) noexcept |
| Exchange in O(1) the state of this estimator and another. | |
| void | update (const T &val) |
| Add an element to the set. | |
| double | estimate () const noexcept |
| Estimate current cardinality. | |
| void | merge (const HyperLogLog &other) |
| Merge another HyperLogLog into this one (union of sets). | |
| void | clear () |
| Reset all registers to zero. | |
| size_t | num_registers () const noexcept |
| Number of HyperLogLog registers (2^b). | |
Static Private Member Functions | |
| static uint64_t | hash64 (const T &val) noexcept |
| Compute 64-bit hash using MurmurHash3. | |
| static size_t | compute_m_or_throw (const uint8_t b) |
| Validate b and compute m = 2^b. | |
| static double | compute_alpha (const size_t m) noexcept |
| Compute bias-correction constant alpha_m from Flajolet et al. | |
Private Attributes | |
| uint8_t | b_ |
| Number of bits for bucket index. | |
| size_t | m_ |
| Number of registers (2^b_). | |
| double | alpha_m_ |
| Bias correction constant. | |
| Array< uint8_t > | registers_ |
| Max leading zeros observed per bucket. | |
HyperLogLog cardinality estimator.
Estimates the count of distinct elements (NDV) in a stream. Typical error is 1.04/sqrt(m), where m is the number of registers.
This class is not thread-safe. External synchronization is required if multiple threads access the same HyperLogLog instance concurrently, especially if at least one thread modifies it (via update, merge, or clear).
| T | Type of elements to count. |
Definition at line 68 of file hyperloglog.H.
|
inlineexplicit |
Construct with precision parameter.
| [in] | b | Number of bits for index [4..16]. m = 2^b registers. |
| std::domain_error | if b is outside [4, 16]. |
Definition at line 113 of file hyperloglog.H.
|
default |
Copy the precision and the registers of another estimator.
| [in] | other | estimator to copy. |
| std::bad_alloc | if the registers cannot be allocated. |
|
inline |
Take the registers of another estimator, which is left as an empty estimator of the same precision.
The implicit move took the registers but copied their count: the moved-from estimator then read and wrote registers that it no longer had, and its clear() wrote through a null buffer.
| [in,out] | other | estimator to move from. |
| std::bad_alloc | if the registers left to other cannot be allocated; other is then unchanged. |
other. Definition at line 137 of file hyperloglog.H.
References Aleph::HyperLogLog< T >::swap().
|
inline |
Reset all registers to zero.
| none |
Definition at line 261 of file hyperloglog.H.
References Aleph::HyperLogLog< T >::m_, and Aleph::HyperLogLog< T >::registers_.
Referenced by TEST().
|
inlinestaticprivatenoexcept |
Compute bias-correction constant alpha_m from Flajolet et al.
Definition at line 98 of file hyperloglog.H.
References m.
|
inlinestaticprivate |
Validate b and compute m = 2^b.
Definition at line 88 of file hyperloglog.H.
References ah_domain_error_if.
|
inlinenoexcept |
Estimate current cardinality.
| none |
Definition at line 199 of file hyperloglog.H.
References Aleph::HyperLogLog< T >::alpha_m_, Aleph::HyperLogLog< T >::m_, and Aleph::HyperLogLog< T >::registers_.
|
inlinestaticprivatenoexcept |
Compute 64-bit hash using MurmurHash3.
Definition at line 78 of file hyperloglog.H.
References Aleph::murmur3hash().
Referenced by Aleph::HyperLogLog< T >::update().
|
inline |
Merge another HyperLogLog into this one (union of sets).
Combines two sketches by taking the element-wise maximum of their registers, which corresponds to the union of the underlying sets.
| [in] | other | HyperLogLog with the same precision parameter b. |
| std::domain_error | if the precision parameters (b) differ. |
Definition at line 247 of file hyperloglog.H.
References ah_domain_error_if, Aleph::HyperLogLog< T >::b_, Aleph::HyperLogLog< T >::m_, and Aleph::HyperLogLog< T >::registers_.
Referenced by TEST().
|
inlinenoexcept |
Number of HyperLogLog registers (2^b).
| none |
Definition at line 273 of file hyperloglog.H.
References Aleph::HyperLogLog< T >::m_.
Referenced by TEST().
|
default |
Copy the precision and the registers of another estimator.
| [in] | other | estimator to copy. |
| std::bad_alloc | if the registers cannot be allocated. |
|
inlinenoexcept |
Exchange this estimator with another, which is left with the previous state of this one.
| [in,out] | other | estimator to move from. |
Definition at line 155 of file hyperloglog.H.
References Aleph::HyperLogLog< T >::swap().
|
inlinenoexcept |
Exchange in O(1) the state of this estimator and another.
| [in,out] | other | estimator to exchange with. |
Definition at line 164 of file hyperloglog.H.
References Aleph::HyperLogLog< T >::alpha_m_, Aleph::HyperLogLog< T >::b_, Aleph::HyperLogLog< T >::m_, Aleph::HyperLogLog< T >::registers_, and Aleph::Array< T >::swap().
Referenced by Aleph::HyperLogLog< T >::HyperLogLog(), and Aleph::HyperLogLog< T >::operator=().
|
inline |
Add an element to the set.
| [in] | val | Element to incorporate into the cardinality sketch. |
| none |
Definition at line 179 of file hyperloglog.H.
References Aleph::HyperLogLog< T >::b_, Aleph::HyperLogLog< T >::hash64(), Aleph::HyperLogLog< T >::registers_, and w.
|
private |
Bias correction constant.
Definition at line 72 of file hyperloglog.H.
Referenced by Aleph::HyperLogLog< T >::estimate(), and Aleph::HyperLogLog< T >::swap().
|
private |
Number of bits for bucket index.
Definition at line 70 of file hyperloglog.H.
Referenced by Aleph::HyperLogLog< T >::merge(), Aleph::HyperLogLog< T >::swap(), and Aleph::HyperLogLog< T >::update().
|
private |
Number of registers (2^b_).
Definition at line 71 of file hyperloglog.H.
Referenced by Aleph::HyperLogLog< T >::clear(), Aleph::HyperLogLog< T >::estimate(), Aleph::HyperLogLog< T >::merge(), Aleph::HyperLogLog< T >::num_registers(), and Aleph::HyperLogLog< T >::swap().
|
private |
Max leading zeros observed per bucket.
Definition at line 73 of file hyperloglog.H.
Referenced by Aleph::HyperLogLog< T >::clear(), Aleph::HyperLogLog< T >::estimate(), Aleph::HyperLogLog< T >::merge(), Aleph::HyperLogLog< T >::swap(), and Aleph::HyperLogLog< T >::update().