Histogram

Struct Histogram 

Source
pub struct Histogram<K: Hash + Eq, S: BuildHasher = DefaultHashBuilder> { /* private fields */ }
Expand description

A histogram that counts occurrences of keys.

§Examples

§Add and get one by one

use histongram::Histogram;

let mut hist = Histogram::<String>::new();

hist.add_ref("a");
assert_eq!(hist.count("a"), 1);
assert_eq!(hist.count("b"), 0);

§Filling from Iterators

use histongram::Histogram;
let mut hist: Histogram<String> = ["a", "a", "a"].into_iter().collect();

assert_eq!(hist.count("a"), 3);

hist.extend(["a", "b", "c"]);
assert_eq!(hist.count("a"), 4);
assert_eq!(hist.count("b"), 1);
assert_eq!(hist.count("c"), 1);

§Iterating the counts

use histongram::Histogram;
let hist: Histogram<String> = ["a", "a", "a"].into_iter().collect();

// NOTE: The order is arbitrary for multiple items

for (key, cnt) in &hist {
    assert_eq!(key, &"a");
    assert_eq!(cnt, 3);
}

for (key, portion) in hist.iter_rel() {
    assert_eq!(key, &"a");
    assert_eq!(portion, 1.0);
}

// This consumes hist but gives back ownership of the keys
for (key, cnt) in hist {
    assert_eq!(key, "a".to_string());
    assert_eq!(cnt, 3);
}

§Getting a sorted vector of occurences

This can be build by using Iterators or using Histogram::sorted_occurrences().

use std::cmp::Reverse;
use histongram::Histogram;
let hist: Histogram<String> = ["a","a","a","x","x","z","z","z","z","z"].into_iter().collect();

let mut counts: Vec<_> = hist.into_iter().collect();
counts.sort_by_key(|(_key, cnt)| Reverse(*cnt));

assert_eq!(counts, vec![
    ("z".to_string(), 5),
    ("a".to_string(), 3),
    ("x".to_string(), 2),
]);

§Using a pre-allocated hashbrown::HashMap

use hashbrown::HashMap;
use histongram::Histogram;

let mut hist: Histogram<_> = HashMap::<String, usize>::with_capacity(100).into();

hist.add_ref("foo");
assert_eq!(hist.count("foo"), 1);

Implementations§

Source§

impl<K: Hash + Eq> Histogram<K, DefaultHashBuilder>

Source

pub fn new() -> Self

Create a new empty Histogram

Source

pub fn from_counts(iter: impl IntoIterator<Item = (K, usize)>) -> Self

Collect the counts from iter into a new Histogram

This can be useful if you already counted occurences and just want to analyze it using Histogram.

§Example
§Create from std::collections::HashMap
use std::collections::HashMap;
use histongram::Histogram;

let counts = HashMap::from([
    ("foo", 5),
    ("bar", 2),
    ("baz", 3),
]);

let hist = Histogram::from_counts(counts);
assert_eq!(hist.num_instances(), 10);
§Create from raw counts
use histongram::Histogram;

let hist = Histogram::from_counts([
    ("foo", 5),
    ("bar", 2),
    ("baz", 3),
]);
assert_eq!(hist.num_instances(), 10);
Source§

impl<K: Hash + Eq, S: BuildHasher> Histogram<K, S>

Source

pub const fn with_hasher(hash_builder: S) -> Self

Create a new Histogram using the given hash_builder

This allows you to use different hashing algorithms that might fit your use-case better.

Source

pub fn num_categories(&self) -> usize

Number of categories in the histogram

§Example
let mut hist = Histogram::new();

hist.add_owned("abc");
hist.add_owned("abc");
hist.add_owned("other");
assert_eq!(hist.num_categories(), 2);
Source

pub fn num_instances(&self) -> usize

Total number of instances inserted so far

§Example
let mut hist = Histogram::new();

hist.add_owned("abc");
hist.add_owned("abc");
hist.add_owned("other");
assert_eq!(hist.num_instances(), 3);
Source

pub fn add_ref<'a, Q>(&mut self, val: &'a Q)
where K: Borrow<Q> + From<&'a Q>, Q: ?Sized + Hash + Eq,

Add a new occurence of key to the Histogram

The value will be turned into an owned K if it is not yet present using From<&Q>. This is useful for example if you want to construct a 'static Histogram from slices out of a buffer (See example below). As you do not have to create new String instances if the key is already present in the Histogram.

See also Histogram::extend()

§Examples
use histongram::Histogram;
let mut hist = Histogram::<String>::new();

// During reading the file
{
    let my_local_buffer = "Hello world! Moin! Goedemiddag! Hoi! Moin!";
    for word in my_local_buffer.split_whitespace() {
        hist.add_ref(word);
    }
}
// my_local_bufer is now dropped

// But the Histogram has owned Strings with copies of the words
assert_eq!(hist.count("Moin!"), 2);
assert_eq!(hist.count("Hoi!"), 1);
Source

pub fn add_owned(&mut self, val: K)

Add a new occurrence of key where ownership of the key moves to the Histogram

This is useful for types that do not implement From<&Self> as hashbrown requires the use of the From<T> for its API.

Source

pub fn extend_from_owned<I: IntoIterator<Item = K>>(&mut self, iter: I)

Extend this Histogram by counting owned instances of K

This is similar to Histogram::extend() but taking owned instances instead of references. It is useful for types that are cheap to clone, such as types that implement Copy.

Source

pub fn append(&mut self, other: Self)

Add all the occurrences from other to self

Source

pub fn count<Q>(&self, key: &Q) -> usize
where Q: ?Sized + Hash + Eq, K: Borrow<Q>,

Get the number of times key was added to this histogram

Returns 0 for absent keys.

§Example
let mut hist = Histogram::<String>::new();

hist.add_ref("present");
assert_eq!(hist.count("present"), 1);
assert_eq!(hist.count("absent"), 0);
Source

pub fn count_rel<Q>(&self, key: &Q) -> f64
where Q: ?Sized + Hash + Eq, K: Borrow<Q>,

Get the relative number of times key was added to this histogram

Returns 0.0 for absent keys, so also if asked for any key in an empty Histogram. If all occurrences so far matched key count_rel will return 1.0.

§Example
let mut hist = Histogram::<String>::new();

hist.add_ref("present");
assert_eq!(hist.count_rel("present"), 1.0);
assert_eq!(hist.count_rel("absent"), 0.0);
Source

pub fn iter(&self) -> impl Iterator<Item = (&K, usize)>

Iterate over all keys and their counts in self that have occurred at least once.

The order of keys is arbitrary.

Source

pub fn iter_rel(&self) -> impl Iterator<Item = (&K, f64)>

Iterate over all keys which have occurred at least once, together with its relative number of occurrences.

The order of keys is arbitrary.

Source

pub fn sorted_occurrences(self) -> Vec<(K, usize)>

Get a vector of keys and counts sorted descending by count.

This consumes the Histogram to avoid cloning the keys.

use histongram::Histogram;
let hist: Histogram<_> = Histogram::from_owned_iter("aaaxxzzzzz".chars());

assert_eq!(hist.sorted_occurrences(), vec![
    ('z', 5),
    ('a', 3),
    ('x', 2),
]);
Source

pub fn into_std_hash_map(self) -> HashMap<K, usize>

Turn this histogram into a HashMap from std

This can be useful if you do not want to use another HashMap such as hashbrown::HashMap in your code base. Or you already have a struct containing std::collections::HashMaps.

§Example
use std::collections::HashMap;
use histongram::Histogram;

struct MyStruct {
    counts: HashMap<String, usize>,
}

let mut hist = Histogram::new();
hist.extend(["foo", "bar", "baz"]);

let ms = MyStruct { counts: hist.into_std_hash_map() };
assert_eq!(ms.counts["foo"], 1);
Source§

impl<K: Hash + Eq, S: BuildHasher + Default> Histogram<K, S>

Source

pub fn from_owned_iter<I>(iter: I) -> Self
where I: IntoIterator<Item = K>,

Create a new Histogram by counting owned instanes of K in iter.

This is similar to Histogram::from_iter() but taking owned values instead of references.

See also Histogram::extend_from_owned() for motivations.

Trait Implementations§

Source§

impl<K: Clone + Hash + Eq, S: Clone + BuildHasher> Clone for Histogram<K, S>

Source§

fn clone(&self) -> Histogram<K, S>

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<K: Debug + Hash + Eq, S: Debug + BuildHasher> Debug for Histogram<K, S>

Source§

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

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

impl<K: Hash + Eq, S: BuildHasher + Default> Default for Histogram<K, S>

Source§

fn default() -> Self

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

impl<'de, K, S> Deserialize<'de> for Histogram<K, S>
where K: Hash + Eq + Deserialize<'de>, S: BuildHasher + Default,

Available on crate feature serde only.
Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a, K, S, Q> Extend<&'a Q> for Histogram<K, S>
where K: Hash + Eq + Borrow<Q> + From<&'a Q>, Q: ?Sized + Hash + Eq + 'a, S: BuildHasher,

Source§

fn extend<T: IntoIterator<Item = &'a Q>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K: Hash + Eq, S: BuildHasher> From<HashMap<K, usize, S>> for Histogram<K, S>

Source§

fn from(map: HashMap<K, usize, S>) -> Self

Converts to this type from the input type.
Source§

impl<K: Hash + Eq, H: BuildHasher> From<Histogram<K, H>> for HashMap<K, usize, H>

Source§

fn from(hist: Histogram<K, H>) -> Self

Converts to this type from the input type.
Source§

impl<'a, K, S, Q> FromIterator<&'a Q> for Histogram<K, S>
where K: Hash + Eq + Borrow<Q> + From<&'a Q>, Q: ?Sized + Hash + Eq + 'a, S: BuildHasher + Default,

Source§

fn from_iter<T: IntoIterator<Item = &'a Q>>(iter: T) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, K: Hash + Eq + 'a, S: BuildHasher> IntoIterator for &'a Histogram<K, S>

Source§

type Item = (&'a K, usize)

The type of the elements being iterated over.
Source§

type IntoIter = Map<Iter<'a, K, usize>, fn((&'a K, &'a usize)) -> <&'a Histogram<K, S> as IntoIterator>::Item>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: Hash + Eq, S: BuildHasher> IntoIterator for Histogram<K, S>

Source§

type Item = (K, usize)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, usize>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K: PartialEq + Hash + Eq, S: PartialEq + BuildHasher> PartialEq for Histogram<K, S>

Source§

fn eq(&self, other: &Histogram<K, S>) -> 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<K, S> Serialize for Histogram<K, S>
where K: Hash + Eq + Serialize, S: BuildHasher,

Available on crate feature serde only.
Source§

fn serialize<Ser>(&self, serializer: Ser) -> Result<Ser::Ok, Ser::Error>
where Ser: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<K: Eq + Hash + Eq, S: Eq + BuildHasher> Eq for Histogram<K, S>

Source§

impl<K: Hash + Eq, S: BuildHasher> StructuralPartialEq for Histogram<K, S>

Auto Trait Implementations§

§

impl<K, S> Freeze for Histogram<K, S>
where S: Freeze,

§

impl<K, S> RefUnwindSafe for Histogram<K, S>

§

impl<K, S> Send for Histogram<K, S>
where S: Send, K: Send,

§

impl<K, S> Sync for Histogram<K, S>
where S: Sync, K: Sync,

§

impl<K, S> Unpin for Histogram<K, S>
where S: Unpin, K: Unpin,

§

impl<K, S> UnwindSafe for Histogram<K, S>
where S: UnwindSafe, K: 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> 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,