Struct countmap::CountMap
[−]
[src]
pub struct CountMap<K, S = RandomState> where
K: Eq + Hash,
S: BuildHasher, { /* fields omitted */ }
A count map is a hash map where the value field is a constantly incremented counter. If a key
is inserted for the first time, the counter is set to 1. Every subsequent insert will increment
the counter by 1. This implementation just acts as a decorator around a HashMap
and exposes
almost the complete API of HashMap
except things like iter_mut()
or get_mut()
since it
doesn't make sense in this use case.
Methods
impl<K> CountMap<K, RandomState> where
K: Eq + Hash,
[src]
K: Eq + Hash,
fn new() -> Self
[src]
Creates an empty CountMap
.
Examples
use countmap::CountMap; let mut count_map: CountMap<&str> = CountMap::new();
fn with_capacity(cap: usize) -> Self
[src]
Creates an empty CountMap
with the specified capacity.
The created map can hold at least cap
elements before reallocating. If cap
is 0
the
map will not allocate.
Examples
use countmap::CountMap; let mut count_map: CountMap<&str> = CountMap::with_capacity(10);
impl<K, S> CountMap<K, S> where
K: Eq + Hash,
S: BuildHasher,
[src]
K: Eq + Hash,
S: BuildHasher,
fn with_hasher(hash_builder: S) -> Self
[src]
Creates an empty CountMap
which will use the given hash builder to hash keys.
The created map has the default initial capacity.
Warning: hash_builder
is normally randomly generated, and is designed to allow HashMaps
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.
Examples
use std::collections::hash_map::RandomState; use countmap::CountMap; let s = RandomState::new(); let mut map = CountMap::with_hasher(s); map.insert_or_increment("foo");
fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
[src]
Creates an empty CountMap
with the specified capacity, using hash_builder to hash the
keys.
The count map will be able to hold at least capacity
elements without reallocating. If
capacity
is 0, the hash map will not allocate.
Warning: hash_builder
is normally randomly generated, and is designed to allow HashMaps
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.
Examples
use std::collections::hash_map::RandomState; use countmap::CountMap; let s = RandomState::new(); let mut map = CountMap::with_capacity_and_hasher(10, s); map.insert_or_increment("foo");
fn hasher(&self) -> &S
[src]
Returns a reference to the map's BuildHasher
.
fn capacity(&self) -> usize
[src]
Returns the number of elements the map can hold without reallocating.
This number is a lower bound; the CountMap<K>
might be able to hold more, but is
guaranteed to be able to hold at least this many.
Examples
use countmap::CountMap; let map: CountMap<&str> = CountMap::with_capacity(100); assert!(map.capacity() >= 100);
fn reserve(&mut self, additional: usize)
[src]
Reserves capacity for at least additional
more elements to be inserted in the CountMap
.
The collection ma reserve more space to avoid frequent reallocations.
Panics
Panics if the new allocation size overflows usize.
Examples
use countmap::CountMap; let mut map: CountMap<&str> = CountMap::with_capacity(5); map.reserve(10); assert!(map.capacity() >= 15);
fn shrink_to_fit(&mut self)
[src]
Shrinks the capacity of the map as much as possible. It will drop down as much as possible while maintaining the internal rules and possibly leaving some space in accordance with the resize policy.
Examples
use countmap::CountMap; let mut map = CountMap::with_capacity(100); map.insert_or_increment("foo"); map.insert_or_increment("bar"); assert!(map.capacity() >= 100); map.shrink_to_fit(); assert!(map.capacity() >= 2);
fn keys(&self) -> Keys<K, u64>
[src]
An iterator visiting all keys in arbitrary order. The iterator element type is &'a K
.
Examples
use countmap::CountMap; let mut map = CountMap::new(); map.insert_or_increment("foo"); map.insert_or_increment("bar"); map.insert_or_increment("foo"); for key in map.keys() { println!("{}", key); }
fn values(&self) -> Values<K, u64>
[src]
An iterator visiting all values in arbitrary order. The iterator element type is &'a V
.
Examples
use countmap::CountMap; let mut map = CountMap::new(); map.insert_or_increment("foo"); map.insert_or_increment("bar"); map.insert_or_increment("foo"); for val in map.values() { println!("{}", val); }
fn insert_or_increment(&mut self, element: K) -> u64
[src]
Inserts or increments an element in the CountMap
. The new value of the counter is
returned.
Examples
use countmap::CountMap; let mut count_map: CountMap<&str> = CountMap::new(); assert_eq!(count_map.insert_or_increment("foo"), 1); assert_eq!(count_map.insert_or_increment("foo"), 2); assert_eq!(count_map.insert_or_increment("bar"), 1);
fn increment(&mut self, element: &K) -> Option<u64>
[src]
Increments an existing element in the CountMap
. Returns an Option
with the new value of
the counter or None
if the element doesn't exist.
Examples
use countmap::CountMap; let mut count_map: CountMap<&str> = CountMap::new(); assert_eq!(count_map.increment(&"foo"), None); count_map.insert_or_increment(&"foo"); assert_eq!(count_map.increment(&"foo"), Some(2));
fn get_count(&self, element: &K) -> Option<u64>
[src]
Returns an Option
containing the current counter value of the specified element or
None
.
Examples
use countmap::CountMap; let mut count_map: CountMap<&str> = CountMap::new(); count_map.insert_or_increment("foo"); assert_eq!(count_map.get_count(&"foo"), Some(1)); assert_eq!(count_map.get_count(&"bar"), None);
fn iter(&self) -> Iter<K, u64>
[src]
An iterator visiting all key-value pairs in arbitrary order. The iterator element type is (&'a K, &'a u64).
Examples
use countmap::CountMap; let mut map = CountMap::new(); map.insert_or_increment("foo"); map.insert_or_increment("foo"); map.insert_or_increment("bar"); for (key, count) in map { println!("key: {}, count: {}", key, count); }
fn iter_mut(&mut self) -> IterMut<K, u64>
[src]
An iterator visiting all key-value pairs in arbitrary order, with mutable references to the values. The iterator element type is (&'a K, &'a mut V).
Examples
use countmap::CountMap; let mut map = CountMap::new(); map.insert_or_increment("foo"); map.insert_or_increment("foo"); map.insert_or_increment("bar"); for (_, count) in map.iter_mut() { *count += 3; } assert_eq!(map.get_count(&"foo"), Some(5)); assert_eq!(map.get_count(&"bar"), Some(4));
fn len(&self) -> usize
[src]
Returns the number of elements in the map.
Examples
use countmap::CountMap; let mut map = CountMap::new(); assert_eq!(map.len(), 0); map.insert_or_increment("foo"); assert_eq!(map.len(), 1);
fn is_empty(&self) -> bool
[src]
Returns true if the map contains no elements.
Examples
use countmap::CountMap; let mut map = CountMap::new(); assert_eq!(map.is_empty(), true); map.insert_or_increment("foo"); assert_eq!(map.is_empty(), false);
fn drain(&mut self) -> Drain<K, u64>
[src]
Clears the map, returning all key-value pairs as an iterator. Keeps the allocated memory for reuse.
Examples
use countmap::CountMap; let mut map = CountMap::new(); map.insert_or_increment("foo"); map.insert_or_increment("bar"); for (k, c) in map.drain().take(1) { assert!(k == "foo" || k == "bar"); assert_eq!(c, 1); } assert!(map.is_empty());
fn clear(&mut self)
[src]
Clears the map, removing all key-counter pairs. Keeps the allocated memory for reuse.
Examples
use countmap::CountMap; let mut map = CountMap::new(); map.insert_or_increment("foo"); map.clear(); assert!(map.is_empty())
fn contains_key(&self, k: &K) -> bool
[src]
Returns true if the map contains a value for the specified key.
Examples
use countmap::CountMap; let mut map = CountMap::new(); map.insert_or_increment("foo"); assert!(map.contains_key(&"foo")); assert!(!map.contains_key(&"bar"));
fn remove(&mut self, k: &K) -> Option<u64>
[src]
Removes a key from the map, returning the value at the key if the key was previously in the map.
Examples
use countmap::CountMap; let mut map = CountMap::new(); map.insert_or_increment("foo"); assert_eq!(map.remove(&"foo"), Some(1)); assert_eq!(map.remove(&"bar"), None);
fn retain<F>(&mut self, f: F) where
F: FnMut(&K, &mut u64) -> bool,
[src]
F: FnMut(&K, &mut u64) -> bool,
Retains only the elements specified by the predicate.
In other words, remove all pairs (k, v)
such that f(&k,&mut v)
returns false
.
Examples
use countmap::CountMap; let mut map = CountMap::new(); map.insert_or_increment("foo"); map.insert_or_increment("foo"); map.insert_or_increment("foo"); map.insert_or_increment("bar"); map.retain(|_, c| *c == 3); assert_eq!(map.len(), 1);
Trait Implementations
impl<K: Clone, S: Clone> Clone for CountMap<K, S> where
K: Eq + Hash,
S: BuildHasher,
[src]
K: Eq + Hash,
S: BuildHasher,
fn clone(&self) -> CountMap<K, S>
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<K: Debug, S: Debug> Debug for CountMap<K, S> where
K: Eq + Hash,
S: BuildHasher,
[src]
K: Eq + Hash,
S: BuildHasher,
impl<K> Default for CountMap<K> where
K: Eq + Hash,
[src]
K: Eq + Hash,
impl<K> PartialEq for CountMap<K> where
K: Eq + Hash,
[src]
K: Eq + Hash,
fn eq(&self, other: &CountMap<K>) -> bool
[src]
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, other: &Rhs) -> bool
1.0.0[src]
This method tests for !=
.
impl<K> Eq for CountMap<K> where
K: Eq + Hash,
[src]
K: Eq + Hash,
impl<'a, K> IntoIterator for &'a CountMap<K> where
K: Eq + Hash,
[src]
K: Eq + Hash,
type Item = (&'a K, &'a u64)
The type of the elements being iterated over.
type IntoIter = Iter<'a, K, u64>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
Creates an iterator from a value. Read more
impl<'a, K> IntoIterator for &'a mut CountMap<K> where
K: Eq + Hash,
[src]
K: Eq + Hash,
type Item = (&'a K, &'a mut u64)
The type of the elements being iterated over.
type IntoIter = IterMut<'a, K, u64>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
Creates an iterator from a value. Read more
impl<'a, K> IntoIterator for CountMap<K> where
K: Eq + Hash,
[src]
K: Eq + Hash,
type Item = (K, u64)
The type of the elements being iterated over.
type IntoIter = IntoIter<K, u64>
Which kind of iterator are we turning this into?
fn into_iter(self) -> Self::IntoIter
[src]
Creates an iterator from a value. Read more
impl<'a, K, Q: ?Sized> Index<&'a Q> for CountMap<K> where
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash,
[src]
K: Eq + Hash + Borrow<Q>,
Q: Eq + Hash,
type Output = u64
The returned type after indexing.
fn index(&self, index: &Q) -> &Self::Output
[src]
Examples
use countmap::CountMap; let mut map = CountMap::new(); map.insert_or_increment("foo"); assert_eq!(map["foo"], 1);
impl<K> FromIterator<(K, u64)> for CountMap<K> where
K: Eq + Hash,
[src]
K: Eq + Hash,
fn from_iter<T>(iter: T) -> Self where
T: IntoIterator<Item = (K, u64)>,
[src]
T: IntoIterator<Item = (K, u64)>,
Creates a value from an iterator. Read more
impl<K> Extend<(K, u64)> for CountMap<K> where
K: Eq + Hash,
[src]
K: Eq + Hash,
fn extend<T>(&mut self, iter: T) where
T: IntoIterator<Item = (K, u64)>,
[src]
T: IntoIterator<Item = (K, u64)>,
Extends a collection with the contents of an iterator. Read more
impl<'a, K> Extend<(&'a K, &'a u64)> for CountMap<K> where
K: 'a + Eq + Hash + Copy,
[src]
K: 'a + Eq + Hash + Copy,