cardinality method
Approximate count of distinct elements added so far (0 for empty). Audited: 2026-06-12 11:26 EDT
Implementation
int cardinality() {
final int m = _registers.length;
double sum = 0;
int zeros = 0;
// Harmonic mean of 2^-register is the core HLL estimator; also tally empty
// registers so the small-range branch can use linear counting instead.
for (final int r in _registers) {
// 2^-r. The integer shift is exact and web-safe for realistic ranks; the
// pow fallback covers the astronomically rare r > 30, where `1 << r` would
// overflow the web's 32-bit shift and corrupt the sum.
sum += r <= 30 ? 1.0 / (1 << r) : math.pow(2, -r).toDouble();
if (r == 0) zeros++;
}
final double raw = _alpha(m) * m * m / sum;
return _corrected(raw, m, zeros).round();
}