78 [[nodiscard]]
static uint64_t
hash64(
const T & val)
noexcept
80 static_assert(
sizeof(size_t) >= 8,
81 "HyperLogLog requires 64-bit size_t; murmur3hash is 32-bit on this platform");
82 return static_cast<uint64_t
>(
murmur3hash(val, 0x12345678));
91 <<
"HyperLogLog: b must be in range [4, 16] (got " <<
static_cast<int>(b) <<
")";
92 return static_cast<size_t>(1) << b;
100 if (
m == 16)
return 0.673;
101 if (
m == 32)
return 0.697;
102 if (
m == 64)
return 0.709;
103 return 0.7213 / (1.0 + 1.079 /
static_cast<double>(
m));
166 std::swap(
b_, other.b_);
167 std::swap(
m_, other.m_);
168 std::swap(
alpha_m_, other.alpha_m_);
181 const uint64_t x =
hash64(val);
182 const size_t j = x >> (64 -
b_);
183 const uint64_t
w = x << b_ | (static_cast<uint64_t>(1) << (
b_ - 1));
187 if (
const auto rho =
static_cast<uint8_t
>(std::countl_zero(
w) + 1); rho >
registers_[j])
203 for (
size_t i = 0; i <
m_; ++i)
206 z += std::ldexp(1.0, -
static_cast<int>(
registers_[i]));
212 double e =
alpha_m_ * (
static_cast<double>(
m_) *
static_cast<double>(
m_)) / z;
215 if (e <= 2.5 *
static_cast<double>(
m_))
218 e =
static_cast<double>(
m_) * std::log(
static_cast<double>(
m_) /
static_cast<double>(v));
223 static const double two_to_64 = std::ldexp(1.0, 64);
224 if (e > (1.0 / 30.0) * two_to_64)
226 const double ratio = e / two_to_64;
229 e = -two_to_64 * std::log1p(-ratio);
251 for (
size_t i = 0; i <
m_; ++i)
263 for (
size_t i = 0; i <
m_; ++i)
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Simple dynamic array with automatic resizing and functional operations.
void swap(Array &s) noexcept
Swap this with s
HyperLogLog cardinality estimator.
double estimate() const noexcept
Estimate current cardinality.
HyperLogLog(HyperLogLog &&other)
Take the registers of another estimator, which is left as an empty estimator of the same precision.
Array< uint8_t > registers_
Max leading zeros observed per bucket.
HyperLogLog(const uint8_t b=12)
Construct with precision parameter.
static size_t compute_m_or_throw(const uint8_t b)
Validate b and compute m = 2^b.
void clear()
Reset all registers to zero.
HyperLogLog & operator=(const HyperLogLog &other)=default
Copy the precision and the registers of another estimator.
double alpha_m_
Bias correction constant.
static uint64_t hash64(const T &val) noexcept
Compute 64-bit hash using MurmurHash3.
static double compute_alpha(const size_t m) noexcept
Compute bias-correction constant alpha_m from Flajolet et al.
void merge(const HyperLogLog &other)
Merge another HyperLogLog into this one (union of sets).
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.
size_t m_
Number of registers (2^b_).
size_t num_registers() const noexcept
Number of HyperLogLog registers (2^b).
uint8_t b_
Number of bits for bucket index.
HyperLogLog(const HyperLogLog &other)=default
Copy the precision and the registers of another estimator.
Standard hash functions for Aleph types.
Main namespace for Aleph-w library functions.
std::decay_t< typename HeadC::Item_Type > T
size_t murmur3hash(const Key &key, std::uint32_t seed)
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Dynamic array container with automatic resizing.