HyperLogLogUtils class

HyperLogLog-lite cardinality sketch: add elements, read an approximate distinct count from cardinality.

Estimates the number of distinct elements seen using O(2^precision) memory instead of storing every element — the trade-off is a small, bounded error (roughly 1.04 / sqrt(registers)) in exchange for tiny fixed memory.

The hash source is Dart's Object.hashCode mixed into a 64-bit-ish value; because hashCode is only 32-ish bits and per-isolate, estimates are approximate and not stable across runs/isolates. Treat the result as a statistical estimate, never an exact count.

Web-safe: the 64-bit hash mixing, register-index/rank extraction, and the 2^-rank term are all computed in 32-bit limbs (see _mix64), so estimates are the same on the VM and on the web. A naive native int version would overflow the web's 53-bit-double int (and truncate shifts to 32 bits), collapsing the estimate; this implementation avoids that. See https://dart.dev/resources/language/number-representation.

Example:

final hll = HyperLogLogUtils(precision: 12);
for (var i = 0; i < 1000; i++) {
  hll.add('user_$i');
}
hll.cardinality(); // ~1000 (within a few percent)

Constructors

HyperLogLogUtils({int precision = 12})
Creates an empty sketch with 2^precision registers.

Properties

hashCode int
The hash code for this object.
no setterinherited
precision int
Number of register-index bits; register count is 2^precision.
final
registerCount int
Number of registers (2^precision). Audited: 2026-06-12 11:26 EDT
no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

add(Object? element) → void
Folds element into the sketch (idempotent for equal elements). Audited: 2026-06-12 11:26 EDT
cardinality() int
Approximate count of distinct elements added so far (0 for empty). Audited: 2026-06-12 11:26 EDT
merge(HyperLogLogUtils other) HyperLogLogUtils
Merges other into a new sketch (both must share this precision).
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited