Struct counter::Counter
[−]
[src]
pub struct Counter<T: Hash + Eq> { pub map: HashMap<T, usize>, }
Fields
map: HashMap<T, usize>
HashMap backing this Counter
Public to expose the HashMap API for direct manipulation. That said, this may change in the future to some other mapping type / trait.
Methods
impl<T> Counter<T> where
T: Hash + Eq,
[src]
T: Hash + Eq,
fn new() -> Counter<T>
Create a new, empty Counter
fn init<I>(iterable: I) -> Counter<T> where
I: IntoIterator<Item = T>,
I: IntoIterator<Item = T>,
Create a new Counter
initialized with the given iterable
fn update<I>(&mut self, iterable: I) where
I: IntoIterator<Item = T>,
I: IntoIterator<Item = T>,
Add the counts of the elements from the given iterable to this counter
fn subtract<I>(&mut self, iterable: I) where
I: IntoIterator<Item = T>,
I: IntoIterator<Item = T>,
Remove the counts of the elements from the given iterable to this counter
Non-positive counts are automatically removed
impl<T> Counter<T> where
T: Hash + Eq + Clone,
[src]
T: Hash + Eq + Clone,
fn most_common(&self) -> IntoIter<(T, usize)>
Create an iterator over (frequency, elem)
pairs, sorted most to least common.
FIXME: This is pretty inefficient: it copies everything into a vector, sorts
the vector, and returns an iterator over the vector. It would be much better
to create some kind of MostCommon struct which implements Iterator
which
does all the necessary work on demand. PRs appreciated here!
fn most_common_tiebreaker<F>(&self, tiebreaker: F) -> IntoIter<(T, usize)> where
F: Fn(&T, &T) -> Ordering,
F: Fn(&T, &T) -> Ordering,
Create an iterator over (frequency, elem)
pairs, sorted most to least common.
In the event that two keys have an equal frequency, use the supplied ordering function to further arrange the results.
FIXME: This is pretty inefficient: it copies everything into a vector, sorts
the vector, and returns an iterator over the vector. It would be much better
to create some kind of MostCommon struct which implements Iterator
which
does all the necessary work on demand. PRs appreciated here!
impl<T> Counter<T> where
T: Hash + Eq + Clone + Ord,
[src]
T: Hash + Eq + Clone + Ord,
fn most_common_ordered(&self) -> IntoIter<(T, usize)>
Create an iterator over (frequency, elem)
pairs, sorted most to least common.
In the event that two keys have an equal frequency, use the natural ordering of the keys to further sort the results.
FIXME: This is pretty inefficient: it copies everything into a vector, sorts
the vector, and returns an iterator over the vector. It would be much better
to create some kind of MostCommon struct which implements Iterator
which
does all the necessary work on demand. PRs appreciated here!
Trait Implementations
impl<T: Clone + Hash + Eq> Clone for Counter<T>
[src]
fn clone(&self) -> Counter<T>
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl<T: PartialEq + Hash + Eq> PartialEq for Counter<T>
[src]
fn eq(&self, __arg_0: &Counter<T>) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &Counter<T>) -> bool
This method tests for !=
.
impl<T: Eq + Hash + Eq> Eq for Counter<T>
[src]
impl<T> Add for Counter<T> where
T: Clone + Hash + Eq,
[src]
T: Clone + Hash + Eq,
type Output = Counter<T>
The resulting type after applying the +
operator
fn add(self, rhs: Counter<T>) -> Counter<T>
Add two counters together.
out = c + d;
-> out[x] == c[x] + d[x]
impl<T> Sub for Counter<T> where
T: Clone + Hash + Eq,
[src]
T: Clone + Hash + Eq,
type Output = Counter<T>
The resulting type after applying the -
operator
fn sub(self, rhs: Counter<T>) -> Counter<T>
Subtract (keeping only positive values).
out = c - d;
-> out[x] == c[x] - d[x]
impl<T> BitAnd for Counter<T> where
T: Clone + Hash + Eq,
[src]
T: Clone + Hash + Eq,
type Output = Counter<T>
The resulting type after applying the &
operator
fn bitand(self, rhs: Counter<T>) -> Counter<T>
Intersection
out = c & d;
-> out[x] == min(c[x], d[x])
impl<T> BitOr for Counter<T> where
T: Clone + Hash + Eq,
[src]
T: Clone + Hash + Eq,