Sketch

Struct Sketch 

Source
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

Source

pub fn new() -> Self

Construct an empty Sketch

Source

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());
Source

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]);
Source

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());
Source

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);
Source

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);
Source

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.

Source

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

Source

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);
Source

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>());
Source

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);
Source

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);
Source

pub fn save(&self, writer: impl Write) -> Result<()>

Serialize this Sketch to the given writer

let sk: hyperminhash::Sketch = (0..100).collect();

let mut buffer = Vec::new();
sk.save(&mut buffer).expect("Failed to write");
Source

pub fn load(reader: impl Read) -> Result<Self>

Deserialize a Sketch from the given reader

let reader = std::io::repeat(0);

let sk = hyperminhash::Sketch::load(reader).expect("Failed to load");
assert!(sk.is_empty());

Trait Implementations§

Source§

impl Clone for Sketch

Source§

fn clone(&self) -> Sketch

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Sketch

Source§

fn fmt(&self, fmt: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Sketch

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<T: Hash> FromIterator<T> for Sketch

Source§

fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl Hash for Sketch

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Sketch

Source§

fn eq(&self, other: &Sketch) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Sketch

Source§

impl StructuralPartialEq for Sketch

Auto Trait Implementations§

§

impl Freeze for Sketch

§

impl RefUnwindSafe for Sketch

§

impl Send for Sketch

§

impl Sync for Sketch

§

impl Unpin for Sketch

§

impl UnwindSafe for Sketch

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.