pub struct HyperLogLogBuilder<H, W = usize, const BETA: bool = true> { /* private fields */ }Expand description
Builder for HyperLogLog cardinality-estimation logic.
The builder lets you configure:
- the upper bound on the number of distinct elements
(
new/num_elements); - the number of registers, either directly
(
log2_num_regs) or via a target relative standard deviation (rsd); - the backend word type (
word_type); - the hash function (
build_hasher); - whether LogLog-β bias correction is enabled
(
beta).
Call build to obtain the configured HyperLogLog
logic.
Implementations§
Source§impl HyperLogLogBuilder<BuildHasherDefault<DefaultHasher>>
impl HyperLogLogBuilder<BuildHasherDefault<DefaultHasher>>
Sourcepub const fn new(num_elements: usize) -> Self
pub const fn new(num_elements: usize) -> Self
Creates a new builder for a HyperLogLog logic with the default word
type (the fixed-size equivalent of usize).
§Panics
If num_elements is zero.
Source§impl<H, W: Word, const BETA: bool> HyperLogLogBuilder<H, W, BETA>
impl<H, W: Word, const BETA: bool> HyperLogLogBuilder<H, W, BETA>
Sourcepub fn rsd(self, rsd: f64) -> Self
pub fn rsd(self, rsd: f64) -> Self
Sets the desired relative standard deviation.
This is a high-level alternative to Self::log2_num_regs. Calling one
after the other invalidates the work done by the first one.
§Arguments
rsd: the relative standard deviation to be attained.
§Panics
If the resulting number of registers is less than 16 (i.e., rsd is
too large).
Sourcepub const fn log2_num_regs(self, log2_num_regs: u32) -> Self
pub const fn log2_num_regs(self, log2_num_regs: u32) -> Self
Sourcepub fn min_log2_num_regs(&self) -> u32
pub fn min_log2_num_regs(&self) -> u32
Returns the minimum value allowed for Self::log2_num_regs given the
current value of Self::num_elements.
Sourcepub fn word_type<W2>(self) -> HyperLogLogBuilder<H, W2, BETA>
pub fn word_type<W2>(self) -> HyperLogLogBuilder<H, W2, BETA>
Sets the type W to use to represent backends.
Note that the returned builder will have a different type if W2 is
different from W.
See the logic documentation for the limitations on the
choice of W2.
Sourcepub fn beta<const BETA2: bool>(self) -> HyperLogLogBuilder<H, W, BETA2>
pub fn beta<const BETA2: bool>(self) -> HyperLogLogBuilder<H, W, BETA2>
Enables or disables the LogLog-β bias correction in the estimate.
When enabled (the default), the estimate uses the LogLog-β formula for
log_num_regs 4–18, which provides better accuracy across the full
cardinality range without a separate linear-counting correction, at the
cost of roughly 20ns per estimate call when some registers are still
zero. When all registers are populated, the correction is skipped and
performance is identical to the classic formula.
When disabled, the classic HyperLogLog formula with linear-counting fallback is used.
// Disable LogLog-β correction
let logic = HyperLogLogBuilder::new(1_000_000)
.log2_num_regs(8)
.beta::<false>()
.build::<usize>().unwrap();Sourcepub const fn num_elements(self, num_elements: usize) -> Self
pub const fn num_elements(self, num_elements: usize) -> Self
Sourcepub fn build_hasher<H2>(
self,
build_hasher: H2,
) -> HyperLogLogBuilder<H2, W, BETA>
pub fn build_hasher<H2>( self, build_hasher: H2, ) -> HyperLogLogBuilder<H2, W, BETA>
Sets the BuildHasher to use.
Using this method you can select a specific hasher based on one or more seeds.
Sourcepub fn build<T>(self) -> Result<HyperLogLog<T, H, W, BETA>, HyperLogLogError>
pub fn build<T>(self) -> Result<HyperLogLog<T, H, W, BETA>, HyperLogLogError>
Builds the logic.
The type of objects the estimators keep track of is defined here by T,
but it is usually inferred by the compiler.
§Errors
Returns HyperLogLogError::RegisterSizeTooLarge if the register size
derived from num_elements exceeds the maximum supported by the hash
type.
Returns HyperLogLogError::UnalignedBackend if the estimator size in
bits is not divisible by the bit width of W.