hll

package
v1.0.0-rc.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 13, 2026 License: Apache-2.0, Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package hll provides a HyperLogLog cardinality estimator.

HyperLogLog estimates the number of distinct elements in a multiset with approximately 2% standard error using only 2^p bytes of memory (e.g., 16 KB for precision 14). It is useful for counting unique items (developers, files, tokens) without maintaining a full set.

This implementation uses the LogLog-Beta bias correction from Qin et al. (2016), which provides accurate estimates across all cardinality ranges without the piecewise linear interpolation tables of HLL++.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrPrecisionOutOfRange is returned when precision is not in [4, 18].
	ErrPrecisionOutOfRange = errors.New("hll: precision must be in [4, 18]")

	// ErrPrecisionMismatch is returned when merging sketches with different precisions.
	ErrPrecisionMismatch = errors.New("hll: cannot merge sketches with different precisions")
)

Functions

This section is empty.

Types

type Sketch

type Sketch struct {
	// contains filtered or unexported fields
}

Sketch is a thread-safe HyperLogLog cardinality estimator.

func New

func New(precision uint8) (*Sketch, error)

New creates a HyperLogLog sketch with the given precision p. Precision must be in [4, 18]. The sketch allocates 2^p registers (bytes).

func (*Sketch) Add

func (s *Sketch) Add(data []byte)

Add inserts data into the sketch by hashing it and updating the appropriate register with the observed number of leading zeros.

func (*Sketch) Count

func (s *Sketch) Count() uint64

Count returns the estimated number of distinct elements that have been added to the sketch. Uses LogLog-Beta bias correction for accuracy across all cardinality ranges.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL