[][src]Struct flurry::HashMap

pub struct HashMap<K: 'static, V: 'static, S = DefaultHashBuilder> { /* fields omitted */ }

A concurrent hash table.

Flurry uses an Guards to control the lifetime of the resources that get stored and extracted from the map. Guards are acquired through the epoch::pin, HashMap::pin and HashMap::guard functions. For more information, see the notes in the crate-level documentation.

Methods

impl<K, V> HashMap<K, V, DefaultHashBuilder> where
    K: Sync + Send + Clone + Hash + Eq,
    V: Sync + Send
[src]

pub fn new() -> Self[src]

Creates an empty HashMap.

The hash map is initially created with a capacity of 0, so it will not allocate until it is first inserted into.

Examples

use flurry::HashMap;
let map: HashMap<&str, i32> = HashMap::new();

pub fn with_capacity(capacity: usize) -> Self[src]

Creates an empty HashMap with the specified capacity.

The hash map will be able to hold at least capacity elements without reallocating. If capacity is 0, the hash map will not allocate.

Examples

use flurry::HashMap;
let map: HashMap<&str, i32> = HashMap::with_capacity(10);

Notes

There is no guarantee that the HashMap will not resize if capacity elements are inserted. The map will resize based on key collision, so bad key distribution may cause a resize before capacity is reached. For more information see the resizing behavior

impl<K, V, S> HashMap<K, V, S> where
    K: Sync + Send + Clone + Hash + Eq,
    V: Sync + Send,
    S: BuildHasher
[src]

pub fn with_hasher(hash_builder: S) -> Self[src]

Creates an empty map which will use 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 the map 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 flurry::{HashMap, DefaultHashBuilder};

let map = HashMap::with_hasher(DefaultHashBuilder::default());
map.pin().insert(1, 2);

pub fn guard(&self) -> Guard[src]

Pin a Guard for use with this map.

Keep in mind that for as long as you hold onto this Guard, you are preventing the collection of garbage generated by the map.

pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self[src]

Creates an empty map with the specified capacity, using hash_builder to hash the keys.

The map will be sized to accommodate capacity elements with a low chance of reallocating (assuming uniformly distributed hashes). If capacity is 0, the call will not allocate, and is equivalent to HashMap::new.

Warning: hash_builder is normally randomly generated, and is designed to allow the map 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 flurry::HashMap;
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let map = HashMap::with_capacity_and_hasher(10, s);
map.pin().insert(1, 2);

impl<K, V, S> HashMap<K, V, S> where
    K: Sync + Send + Clone + Hash + Eq,
    V: Sync + Send,
    S: BuildHasher
[src]

pub fn contains_key<Q: ?Sized>(&self, key: &Q, guard: &Guard) -> bool where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Returns true if the map contains a value for the specified key.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples

use flurry::HashMap;

let map = HashMap::new();
let mref = map.pin();
mref.insert(1, "a");
assert_eq!(mref.contains_key(&1), true);
assert_eq!(mref.contains_key(&2), false);

pub fn get<'g, Q: ?Sized>(&'g self, key: &Q, guard: &'g Guard) -> Option<&'g V> where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

To obtain a Guard, use HashMap::guard.

Examples

use flurry::HashMap;

let map = HashMap::new();
let mref = map.pin();
mref.insert(1, "a");
assert_eq!(mref.get(&1), Some(&"a"));
assert_eq!(mref.get(&2), None);

pub fn get_and<Q: ?Sized, R, F>(
    &self,
    key: &Q,
    then: F,
    guard: &Guard
) -> Option<R> where
    K: Borrow<Q>,
    Q: Hash + Eq,
    F: FnOnce(&V) -> R, 
[src]

Apply then to the mapping for key and get its result. Returns None if this map contains no mapping for key.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples

use flurry::{HashMap, epoch};

let map = HashMap::new();
let guard = epoch::pin();
map.pin().insert(1, 42);
assert_eq!(map.get_and(&1, |num| num * 2, &guard), Some(84));
assert_eq!(map.get_and(&8, |num| num * 2, &guard), None);

pub fn get_key_value<'g, Q: ?Sized>(
    &'g self,
    key: &Q,
    guard: &'g Guard
) -> Option<(&'g K, &'g V)> where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Returns the key-value pair corresponding to key.

Returns None if this map contains no mapping for key.

The supplied key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

pub fn insert<'g>(&'g self, key: K, value: V, guard: &'g Guard) -> Option<&'g V>[src]

Inserts a key-value pair into the map.

If the map did not have this key present, None is returned.

If the map did have this key present, the value is updated, and the old value is returned. The key is not updated, though; this matters for types that can be == without being identical. See the std-collections documentation for more.

Examples

use flurry::HashMap;

let map = HashMap::new();
assert_eq!(map.pin().insert(37, "a"), None);
assert_eq!(map.pin().is_empty(), false);

// you can also re-use a map pin like so:
let mref = map.pin();

mref.insert(37, "b");
assert_eq!(mref.insert(37, "c"), Some(&"b"));
assert_eq!(mref.get(&37), Some(&"c"));

pub fn clear(&self, guard: &Guard)[src]

Clears the map, removing all key-value pairs.

Examples

use flurry::HashMap;

let map = HashMap::new();

map.pin().insert(1, "a");
map.pin().clear();
assert!(map.pin().is_empty());

pub fn compute_if_present<'g, Q: ?Sized, F>(
    &'g self,
    key: &Q,
    remapping_function: F,
    guard: &'g Guard
) -> Option<&'g V> where
    K: Borrow<Q>,
    Q: Hash + Eq,
    F: FnOnce(&K, &V) -> Option<V>, 
[src]

If the value for the specified key is present, attempts to compute a new mapping given the key and its current mapped value.

The new mapping is computed by the remapping_function, which may return None to signalize that the mapping should be removed. The entire method invocation is performed atomically. The supplied function is invoked exactly once per invocation of this method if the key is present, else not at all. Some attempted update operations on this map by other threads may be blocked while computation is in progress, so the computation should be short and simple.

Returns the new value associated with the specified key, or None if no value for the specified key is present.

pub fn reserve(&self, additional: usize, guard: &Guard)[src]

Tries to reserve capacity for at least additional more elements to be inserted in the HashMap. The collection may reserve more space to avoid frequent reallocations.

Examples

use flurry::HashMap;

let map: HashMap<&str, i32> = HashMap::new();

map.pin().reserve(10);

Notes

Reserving does not panic in flurry. If the new size is invalid, no reallocation takes place.

pub fn remove<'g, Q: ?Sized>(
    &'g self,
    key: &Q,
    guard: &'g Guard
) -> Option<&'g V> where
    K: Borrow<Q>,
    Q: Hash + Eq
[src]

Removes a key from the map, returning a reference to the value at the key if the key was previously in the map.

The key may be any borrowed form of the map's key type, but Hash and Eq on the borrowed form must match those for the key type.

Examples

use flurry::HashMap;

let map = HashMap::new();
map.pin().insert(1, "a");
assert_eq!(map.pin().remove(&1), Some(&"a"));
assert_eq!(map.pin().remove(&1), None);

pub fn retain<F>(&self, f: F, guard: &Guard) where
    F: FnMut(&K, &V) -> bool
[src]

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) such that f(&k,&v) returns false.

Examples

use flurry::HashMap;

let map = HashMap::new();

for i in 0..8 {
    map.pin().insert(i, i*10);
}
map.pin().retain(|&k, _| k % 2 == 0);
assert_eq!(map.pin().len(), 4);

Notes

If f returns false for a given key/value pair, but the value for that pair is concurrently modified before the removal takes place, the entry will not be removed. If you want the removal to happen even in the case of concurrent modification, use HashMap::retain_force.

pub fn retain_force<F>(&self, f: F, guard: &Guard) where
    F: FnMut(&K, &V) -> bool
[src]

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) such that f(&k,&v) returns false.

Examples

use flurry::HashMap;

let map = HashMap::new();

for i in 0..8 {
    map.pin().insert(i, i*10);
}
map.pin().retain_force(|&k, _| k % 2 == 0);
assert_eq!(map.pin().len(), 4);

Notes

This method always deletes any key/value pair that f returns false for, even if if the value is updated concurrently. If you do not want that behavior, use HashMap::retain.

Important traits for Iter<'g, K, V>
pub fn iter<'g>(&'g self, guard: &'g Guard) -> Iter<'g, K, V>[src]

An iterator visiting all key-value pairs in arbitrary order. The iterator element type is (&'g K, &'g V).

Important traits for Keys<'g, K, V>
pub fn keys<'g>(&'g self, guard: &'g Guard) -> Keys<'g, K, V>[src]

An iterator visiting all keys in arbitrary order. The iterator element type is &'g K.

Important traits for Values<'g, K, V>
pub fn values<'g>(&'g self, guard: &'g Guard) -> Values<'g, K, V>[src]

An iterator visiting all values in arbitrary order. The iterator element type is &'g V.

pub fn len(&self) -> usize[src]

Returns the number of entries in the map.

Examples

use flurry::HashMap;

let map = HashMap::new();

map.pin().insert(1, "a");
map.pin().insert(2, "b");
assert!(map.pin().len() == 2);

pub fn is_empty(&self) -> bool[src]

Returns true if the map is empty. Otherwise returns false.

Examples

use flurry::HashMap;

let map = HashMap::new();
assert!(map.pin().is_empty());
map.pin().insert("a", 1);
assert!(!map.pin().is_empty());

impl<K, V, S> HashMap<K, V, S> where
    K: Sync + Send + Clone + Hash + Eq,
    V: Sync + Send,
    S: BuildHasher
[src]

pub fn pin(&self) -> HashMapRef<K, V, S>[src]

Get a reference to this map with the current thread pinned.

Keep in mind that for as long as you hold onto this, you are preventing the collection of garbage generated by the map.

pub fn with_guard<'g>(&'g self, guard: &'g Guard) -> HashMapRef<'g, K, V, S>[src]

Get a reference to this map with the given guard.

Trait Implementations

impl<K, V, S> Clone for HashMap<K, V, S> where
    K: Sync + Send + Clone + Hash + Eq,
    V: Sync + Send + Clone,
    S: BuildHasher + Clone
[src]

impl<K, V, S> Debug for HashMap<K, V, S> where
    K: Sync + Send + Clone + Debug + Eq + Hash,
    V: Sync + Send + Debug,
    S: BuildHasher
[src]

impl<K, V, S> Default for HashMap<K, V, S> where
    K: Sync + Send + Clone + Hash + Eq,
    V: Sync + Send,
    S: BuildHasher + Default
[src]

impl<K, V, S> Drop for HashMap<K, V, S>[src]

impl<K, V, S> Eq for HashMap<K, V, S> where
    K: Sync + Send + Clone + Eq + Hash,
    V: Sync + Send + Eq,
    S: BuildHasher
[src]

impl<'a, '_, K, V, S> Extend<(&'a K, &'a V)> for &'_ HashMap<K, V, S> where
    K: Sync + Send + Copy + Hash + Eq,
    V: Sync + Send + Copy,
    S: BuildHasher
[src]

impl<'_, K, V, S> Extend<(K, V)> for &'_ HashMap<K, V, S> where
    K: Sync + Send + Clone + Hash + Eq,
    V: Sync + Send,
    S: BuildHasher
[src]

impl<'a, K, V, S> FromIterator<&'a (K, V)> for HashMap<K, V, S> where
    K: Sync + Send + Copy + Hash + Eq,
    V: Sync + Send + Copy,
    S: BuildHasher + Default
[src]

impl<'a, K, V, S> FromIterator<(&'a K, &'a V)> for HashMap<K, V, S> where
    K: Sync + Send + Copy + Hash + Eq,
    V: Sync + Send + Copy,
    S: BuildHasher + Default
[src]

impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S> where
    K: Sync + Send + Clone + Hash + Eq,
    V: Sync + Send,
    S: BuildHasher + Default
[src]

impl<K, V, S> PartialEq<HashMap<K, V, S>> for HashMap<K, V, S> where
    K: Sync + Send + Clone + Eq + Hash,
    V: Sync + Send + PartialEq,
    S: BuildHasher
[src]

impl<'_, K, V, S> PartialEq<HashMap<K, V, S>> for HashMapRef<'_, K, V, S> where
    K: Sync + Send + Clone + Hash + Eq,
    V: Sync + Send + PartialEq,
    S: BuildHasher
[src]

impl<'_, K, V, S> PartialEq<HashMapRef<'_, K, V, S>> for HashMap<K, V, S> where
    K: Sync + Send + Clone + Hash + Eq,
    V: Sync + Send + PartialEq,
    S: BuildHasher
[src]

Auto Trait Implementations

impl<K, V, S = RandomState> !RefUnwindSafe for HashMap<K, V, S>

impl<K, V, S> Send for HashMap<K, V, S> where
    K: Send + Sync,
    S: Send,
    V: Send + Sync

impl<K, V, S> Sync for HashMap<K, V, S> where
    K: Send + Sync,
    S: Sync,
    V: Send + Sync

impl<K, V, S> Unpin for HashMap<K, V, S> where
    S: Unpin

impl<K, V, S = RandomState> !UnwindSafe for HashMap<K, V, S>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.