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]

[src]

Creates an empty CountMap.

Examples

use countmap::CountMap;

let mut count_map: CountMap<&str> = CountMap::new();

[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]

[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");

[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");

[src]

Returns a reference to the map's BuildHasher.

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

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

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

[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);
}

[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);
}

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

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

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

[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);
}

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

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

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

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

[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())

[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"));

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

[src]

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]

[src]

Returns a copy of the value. Read more

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]

[src]

Formats the value using the given formatter.

impl<K> Default for CountMap<K> where
    K: Eq + Hash
[src]

[src]

Returns the "default value" for a type. Read more

impl<K> PartialEq for CountMap<K> where
    K: Eq + Hash
[src]

[src]

This method tests for self and other values to be equal, and is used by ==. Read more

1.0.0
[src]

This method tests for !=.

impl<K> Eq for CountMap<K> where
    K: Eq + Hash
[src]

impl<'a, K> IntoIterator for &'a CountMap<K> where
    K: Eq + Hash
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

impl<'a, K> IntoIterator for &'a mut CountMap<K> where
    K: Eq + Hash
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[src]

Creates an iterator from a value. Read more

impl<'a, K> IntoIterator for CountMap<K> where
    K: Eq + Hash
[src]

The type of the elements being iterated over.

Which kind of iterator are we turning this into?

[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]

The returned type after indexing.

[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]

[src]

Creates a value from an iterator. Read more

impl<K> Extend<(K, u64)> for CountMap<K> where
    K: Eq + Hash
[src]

[src]

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]

[src]

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