pub struct Sketch { /* private fields */ }Expand description
Records the approximate number of unique elements it has seen over it’s lifetime.
Implementations§
Source§impl Sketch
impl Sketch
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if this Sketch has a cardinality of exactly zero
let mut sk = hyperminhash::Sketch::new();
assert!(sk.is_empty());
sk.add(0);
assert!(!sk.is_empty());Sourcepub fn add(&mut self, v: impl Hash)
pub fn add(&mut self, v: impl Hash)
Add an element to this Sketch using the element’s Hash-implementation
let mut sk = hyperminhash::Sketch::new();
sk.add(42);
sk.add("The answer");
sk.add(vec![1, 2, 3]);Sourcepub fn add_reader(&mut self, r: impl Read) -> Result<u64>
pub fn add_reader(&mut self, r: impl Read) -> Result<u64>
Add a single element using the content of the given io::Read
let mut sk = hyperminhash::Sketch::new();
sk.add_reader(std::io::empty());Sourcepub fn add_bytes(&mut self, v: &[u8])
pub fn add_bytes(&mut self, v: &[u8])
Add a single element given by raw bytes to this Sketch
let mut sk = hyperminhash::Sketch::new();
let buf: [u8; _] = [1, 2, 3];
sk.add_bytes(&buf);Sourcepub fn add_with_seed(&mut self, v: impl Hash, seed: u64)
pub fn add_with_seed(&mut self, v: impl Hash, seed: u64)
Add an element to this Sketch using the element’s Hash-implementation and a seed-value Elements that hash equally but use different seed values are seen as unique elements.
const KILOGRAM: u64 = 1;
const POUNDS: u64 = 2;
let mut sk = hyperminhash::Sketch::new();
sk.add_with_seed(100, KILOGRAM);
sk.add_with_seed(100, POUNDS);
assert!(sk.cardinality() > 1.0);Sourcepub fn add_bytes_with_seed(&mut self, v: &[u8], seed: u64)
pub fn add_bytes_with_seed(&mut self, v: &[u8], seed: u64)
Add a single element given by raw bytes and a see value to this Sketch Elements that hash equally but use different seed values are seen as unique elements.
Sourcepub fn add_reader_with_seed(&mut self, r: impl Read, seed: u64) -> Result<u64>
pub fn add_reader_with_seed(&mut self, r: impl Read, seed: u64) -> Result<u64>
Add a single element using the content of the given io::Read and a seed value
Sourcepub fn cardinality(&self) -> f64
pub fn cardinality(&self) -> f64
The approximate number of unique elements in the set.
let mut sk = hyperminhash::Sketch::new();
assert_eq!(sk.cardinality(), 0.0);
for e in [1, 2, 3, 4, 5] {
sk.add(e);
}
assert!(sk.cardinality() > 4.0);
assert!(sk.cardinality() < 6.0);Sourcepub fn union<'a>(&'a mut self, other: &Self) -> &'a Self
pub fn union<'a>(&'a mut self, other: &Self) -> &'a Self
Merge two sets, resulting in this set becoming the union-set.
let mut sk1 = hyperminhash::Sketch::new();
sk1.add(1);
sk1.add(2);
let mut sk2 = hyperminhash::Sketch::new();
sk2.add(3);
sk2.add(4);
sk1.union(&sk2);
assert_eq!(sk1, (1..=4).collect::<hyperminhash::Sketch>());Sourcepub fn similarity(&self, other: &Self) -> f64
pub fn similarity(&self, other: &Self) -> f64
The Jaccard Index similarity estimation
let sk1 = (0..=75).collect::<hyperminhash::Sketch>();
let sk2 = (50..=125).collect::<hyperminhash::Sketch>();
assert!((sk1.similarity(&sk2) - (25.0 / 125.0)).abs() < 1e-2);Sourcepub fn intersection(&self, other: &Self) -> f64
pub fn intersection(&self, other: &Self) -> f64
The approximate number of elements in both sets
let sk1 = (0..=750).collect::<hyperminhash::Sketch>();
let sk2 = (500..=1250).collect::<hyperminhash::Sketch>();
assert!((sk1.intersection(&sk2) - 250.0).abs() < 1.0);