pub struct Bjkst<T, S = BuildHasherDefault<DefaultHasher>> { /* private fields */ }
Expand description
A BJKST data structure to estimate the number of distinct elements in a data stream.
§Examples
use uniq_ch::Bjkst;
let mut bjkst = Bjkst::new();
// Add some elements, with duplicates.
bjkst.extend(0..75_000);
bjkst.extend(25_000..100_000);
// Count the distinct elements.
assert!((99_000..101_000).contains(&bjkst.len()));
Implementations§
Source§impl<T> Bjkst<T, BuildHasherDefault<DefaultHasher>>
impl<T> Bjkst<T, BuildHasherDefault<DefaultHasher>>
Source§impl<T, S> Bjkst<T, S>where
S: Default,
impl<T, S> Bjkst<T, S>where
S: Default,
Sourcepub fn with_precision(precision: Precision) -> Self
pub fn with_precision(precision: Precision) -> Self
Creates an empty Bjkst
with the specified precision.
§Examples
use uniq_ch::{Bjkst, Precision};
let bjkst = Bjkst::<usize>::with_precision(Precision::P10);
assert_eq!(bjkst.precision(), Precision::P10);
Source§impl<T, S> Bjkst<T, S>
impl<T, S> Bjkst<T, S>
Sourcepub fn with_precision_and_hasher(precision: Precision, hasher: S) -> Self
pub fn with_precision_and_hasher(precision: Precision, hasher: S) -> Self
Creates an empty Bjkst
with the specified precision, using hasher
to
hash values.
Warning: hasher
is normally randomly generated, and is designed to
allow Bjkst
s to be resistant to attacks that cause many collisions
and very poor performance. Setting it manually using this function
can expose a DoS attack vector.
The hasher
passed should implement the BuildHasher
trait for the
BJKST to be useful, see its documentation for details.
§Examples
use std::collections::hash_map::RandomState;
use uniq_ch::{Bjkst, Precision};
let hasher = RandomState::new();
let mut bjkst = Bjkst::with_precision_and_hasher(Precision::P10, hasher);
bjkst.insert(&2);
Sourcepub fn with_hasher(hasher: S) -> Self
pub fn with_hasher(hasher: S) -> Self
Creates a new empty Bjkst
which will use the given hasher to hash
values.
Warning: hasher
is normally randomly generated, and is designed to
allow Bjkst
s to be resistant to attacks that cause many collisions
and very poor performance. Setting it manually using this function
can expose a DoS attack vector.
The hasher
passed should implement the BuildHasher
trait for the
BJKST to be useful, see its documentation for details.
§Examples
use std::collections::hash_map::RandomState;
use uniq_ch::Bjkst;
let hasher = RandomState::new();
let mut bjkst = Bjkst::with_hasher(hasher);
bjkst.insert(&2);
Sourcepub const fn precision(&self) -> Precision
pub const fn precision(&self) -> Precision
Returns the precision of the Bjkst
.
§Examples
use uniq_ch::{Bjkst, Precision};
let bjkst = Bjkst::<usize>::with_precision(Precision::P10);
assert_eq!(bjkst.precision(), Precision::P10);
Sourcepub fn hasher(&self) -> &S
pub fn hasher(&self) -> &S
Returns a reference to the BJKST data structure’s BuildHasher
.
§Examples
use std::collections::hash_map::RandomState;
use uniq_ch::Bjkst;
let hasher = RandomState::new();
let bjkst = Bjkst::<usize, _>::with_hasher(hasher);
let hasher: &RandomState = bjkst.hasher();
Sourcepub fn clear(&mut self)
pub fn clear(&mut self)
Clears the BJKST data structure, removing all values.
§Examples
use uniq_ch::Bjkst;
let mut bjkst = Bjkst::new();
bjkst.insert(&1);
bjkst.clear();
assert!(bjkst.is_empty());
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the BJKST data structure contains no elements.
§Examples
use uniq_ch::Bjkst;
let mut bjkst = Bjkst::new();
assert!(bjkst.is_empty());
bjkst.insert(&1);
assert!(!bjkst.is_empty());
Sourcepub fn insert_hash(&mut self, hash: u64) -> bool
pub fn insert_hash(&mut self, hash: u64) -> bool
Adds a hash value to the BJKST data structure.
Returns whether the value was newly inserted. That is:
- If the value is not skipped, and the BJKST data structure did not
previously contain this value,
true
is returned. - If the value is skipped or the BJKST data structure already contained
this value,
false
is returned.
This may be handy when the hash is previously computed, to avoid computing twice. Hash values need to be uniformly distributed over u64 for an accurate total count.
In all other cases, use insert
instead.
§Examples
use uniq_ch::Bjkst;
let mut bjkst = Bjkst::<usize>::new();
assert_eq!(bjkst.insert_hash(0x12345678), true);
assert_eq!(bjkst.insert_hash(0x12345678), false);
assert_eq!(bjkst.len(), 1);
Source§impl<T, S> Bjkst<T, S>where
T: Hash,
S: BuildHasher,
impl<T, S> Bjkst<T, S>where
T: Hash,
S: BuildHasher,
Sourcepub fn insert(&mut self, value: &T) -> bool
pub fn insert(&mut self, value: &T) -> bool
Adds a value to the BJKST data structure.
Returns whether the value was newly inserted. That is:
- If the value is not skipped, and the BJKST data structure did not
previously contain this value,
true
is returned. - If the value is skipped or the BJKST data structure already contained
this value,
false
is returned.
§Examples
use uniq_ch::Bjkst;
let mut bjkst = Bjkst::new();
assert_eq!(bjkst.insert(&2), true);
assert_eq!(bjkst.insert(&2), false);
assert_eq!(bjkst.len(), 1);
Trait Implementations§
Source§impl<T, H> BitOr<&Bjkst<T, BuildHasherDefault<H>>> for &Bjkst<T, BuildHasherDefault<H>>
impl<T, H> BitOr<&Bjkst<T, BuildHasherDefault<H>>> for &Bjkst<T, BuildHasherDefault<H>>
Source§fn bitor(self, rhs: &Bjkst<T, BuildHasherDefault<H>>) -> Self::Output
fn bitor(self, rhs: &Bjkst<T, BuildHasherDefault<H>>) -> Self::Output
Returns the union of self
and rhs
as a new Bjkst<S, T>
.
§Examples
use uniq_ch::Bjkst;
let lhs = Bjkst::<i32>::from_iter(0..75_000);
let rhs = Bjkst::<i32>::from_iter(25_000..100_000);
let bjkst = &lhs | &rhs;
assert!((99_000..=101_000).contains(&bjkst.len()));
Source§type Output = Bjkst<T, BuildHasherDefault<H>>
type Output = Bjkst<T, BuildHasherDefault<H>>
|
operator.Source§impl<T, H> BitOrAssign<&Bjkst<T, BuildHasherDefault<H>>> for Bjkst<T, BuildHasherDefault<H>>
impl<T, H> BitOrAssign<&Bjkst<T, BuildHasherDefault<H>>> for Bjkst<T, BuildHasherDefault<H>>
Source§fn bitor_assign(&mut self, rhs: &Bjkst<T, BuildHasherDefault<H>>)
fn bitor_assign(&mut self, rhs: &Bjkst<T, BuildHasherDefault<H>>)
Merges self
and rhs
into self
.
§Examples
use uniq_ch::Bjkst;
let mut lhs = Bjkst::<i32>::from_iter(1..75_000);
let rhs = Bjkst::<i32>::from_iter(25_000..100_000);
lhs |= &rhs;
assert!((99_000..=101_000).contains(&lhs.len()));
Source§impl<'de, T, S> Deserialize<'de> for Bjkst<T, S>where
S: Default,
impl<'de, T, S> Deserialize<'de> for Bjkst<T, S>where
S: Default,
Source§fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl<'a, T, S> Extend<&'a T> for Bjkst<T, S>where
T: Hash + 'a,
S: BuildHasher,
impl<'a, T, S> Extend<&'a T> for Bjkst<T, S>where
T: Hash + 'a,
S: BuildHasher,
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = &'a T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = &'a T>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)Source§impl<T, S> Extend<T> for Bjkst<T, S>where
T: Hash,
S: BuildHasher,
impl<T, S> Extend<T> for Bjkst<T, S>where
T: Hash,
S: BuildHasher,
Source§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = T>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)