Skip to main content

HyperLogLog

Struct HyperLogLog 

Source
pub struct HyperLogLog<T, B = SipHasherBuilder> { /* private fields */ }
Expand description

A space-efficient probabilitic data structure to count the number of distinct items in a multiset.

A HyperLogLog<T> uses the observation that the cardinality of a multiset of uniformly distributed items can be estimated by calculating the maximum number of leading zeros in the hash of each item in the multiset. It also buckets each item in a register and takes the harmonic mean of the count in order to reduce the variance. Finally, it uses linear counting for small cardinalities and small correction for large cardinalities.

§Examples

use probabilistic_collections::hyperloglog::HyperLogLog;
use probabilistic_collections::SipHasherBuilder;

let mut hhl = HyperLogLog::<u32>::with_hasher(0.1, SipHasherBuilder::from_seed(0, 0));

assert!(hhl.is_empty());

for key in &[0, 1, 2, 0, 1, 2] {
    hhl.insert(&key);
}

assert!((hhl.len().round() - 3.0).abs() < EPSILON);

Implementations§

Source§

impl<T> HyperLogLog<T>

Source

pub fn new(error_probability: f64) -> Self

Constructs a new, empty HyperLogLog<T> with a given error probability.

§Panics

Panics if error_probability is not in (0, 1).

§Examples
use probabilistic_collections::hyperloglog::HyperLogLog;

let hhl = HyperLogLog::<u32>::new(0.1);
Source§

impl<T, B> HyperLogLog<T, B>
where B: BuildHasher,

Source

pub fn with_hasher(error_probability: f64, hash_builder: B) -> Self

Constructs a new, empty HyperLogLog<T> with a given error probability and hasher builder.

§Panics

Panics if error_probability is not in (0, 1).

§Examples
use probabilistic_collections::hyperloglog::HyperLogLog;
use probabilistic_collections::SipHasherBuilder;

let hhl = HyperLogLog::<u32, _>::with_hasher(0.1, SipHasherBuilder::from_entropy());
Source

pub fn insert<U>(&mut self, item: &U)
where T: Borrow<U>, U: Hash + ?Sized,

Inserts an item into the HyperLogLog<T>.

§Examples
use probabilistic_collections::hyperloglog::HyperLogLog;

let mut hhl = HyperLogLog::<u32>::new(0.1);

hhl.insert(&0);
Source

pub fn merge(&mut self, other: &HyperLogLog<T, B>)
where B: Debug + PartialEq,

Merges self with other.

§Panics

Panics if the error probability of self is not equal to the error probability of other or if the hash builder of self is not equal to the hash builder of other.

§Examples
use probabilistic_collections::hyperloglog::HyperLogLog;
use probabilistic_collections::SipHasherBuilder;

let mut hhl1 = HyperLogLog::<u32>::with_hasher(0.1, SipHasherBuilder::from_seed(0, 0));
hhl1.insert(&0);
hhl1.insert(&1);

let mut hhl2 = HyperLogLog::<u32>::with_hasher(0.1, *hhl1.hasher());
hhl2.insert(&0);
hhl2.insert(&2);

hhl1.merge(&hhl2);

assert!((hhl1.len().round() - 3.0).abs() < EPSILON);
Source

pub fn len(&self) -> f64

Returns the estimated number of distinct items in the HyperLogLog<T>.

§Examples
use probabilistic_collections::hyperloglog::HyperLogLog;

let mut hhl = HyperLogLog::<u32>::new(0.1);
assert!((hhl.len().round() - 0.0).abs() < EPSILON);

hhl.insert(&1);
assert!((hhl.len().round() - 1.0).abs() < EPSILON);
Source

pub fn is_empty(&self) -> bool

Returns true is the HyperLogLog<T> is empty.

§Examples
use probabilistic_collections::hyperloglog::HyperLogLog;

let mut hhl = HyperLogLog::<u32>::new(0.1);
assert!(hhl.is_empty());

hhl.insert(&1);
assert!(!hhl.is_empty());
Source

pub fn clear(&mut self)

Clears the HyperLogLog<T>, removing all items.

§Examples
use probabilistic_collections::hyperloglog::HyperLogLog;

let mut hhl = HyperLogLog::<u32>::new(0.1);
assert!(hhl.is_empty());

hhl.insert(&1);
assert!(!hhl.is_empty());

hhl.clear();
assert!(hhl.is_empty());
Source

pub fn hasher(&self) -> &B

Returns a reference to the HyperLogLog’s hasher builder.

§Examples
use probabilistic_collections::hyperloglog::HyperLogLog;

let hhl = HyperLogLog::<String>::new(0.1);
let hasher = hhl.hasher();

Auto Trait Implementations§

§

impl<T, B> Freeze for HyperLogLog<T, B>
where B: Freeze,

§

impl<T, B> RefUnwindSafe for HyperLogLog<T, B>

§

impl<T, B> Send for HyperLogLog<T, B>
where B: Send, T: Send,

§

impl<T, B> Sync for HyperLogLog<T, B>
where B: Sync, T: Sync,

§

impl<T, B> Unpin for HyperLogLog<T, B>
where B: Unpin, T: Unpin,

§

impl<T, B> UnsafeUnpin for HyperLogLog<T, B>
where B: UnsafeUnpin,

§

impl<T, B> UnwindSafe for HyperLogLog<T, B>
where B: UnwindSafe, T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V