[−][src]Struct flurry::HashMap
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]
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send,
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]
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send,
S: BuildHasher,
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]
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send,
S: BuildHasher,
pub fn contains_key<Q: ?Sized>(&self, key: &Q, guard: &Guard) -> bool where
K: Borrow<Q>,
Q: Hash + Eq,
[src]
K: Borrow<Q>,
Q: Hash + Eq,
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]
K: Borrow<Q>,
Q: Hash + Eq,
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]
&self,
key: &Q,
then: F,
guard: &Guard
) -> Option<R> where
K: Borrow<Q>,
Q: Hash + Eq,
F: FnOnce(&V) -> R,
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]
&'g self,
key: &Q,
guard: &'g Guard
) -> Option<(&'g K, &'g V)> where
K: Borrow<Q>,
Q: Hash + Eq,
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]
&'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>,
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]
&'g self,
key: &Q,
guard: &'g Guard
) -> Option<&'g V> where
K: Borrow<Q>,
Q: Hash + Eq,
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]
F: FnMut(&K, &V) -> bool,
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]
F: FnMut(&K, &V) -> bool,
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]
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send,
S: BuildHasher,
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]
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send + Clone,
S: BuildHasher + Clone,
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]
K: Sync + Send + Clone + Debug + Eq + Hash,
V: Sync + Send + Debug,
S: BuildHasher,
impl<K, V, S> Default for HashMap<K, V, S> where
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send,
S: BuildHasher + Default,
[src]
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send,
S: BuildHasher + Default,
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]
K: Sync + Send + Clone + Eq + Hash,
V: Sync + Send + Eq,
S: BuildHasher,
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]
K: Sync + Send + Copy + Hash + Eq,
V: Sync + Send + Copy,
S: BuildHasher,
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]
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send,
S: BuildHasher,
fn extend<T: IntoIterator<Item = (K, V)>>(&mut self, iter: T)
[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]
K: Sync + Send + Copy + Hash + Eq,
V: Sync + Send + Copy,
S: BuildHasher + Default,
fn from_iter<T: IntoIterator<Item = &'a (K, V)>>(iter: T) -> Self
[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]
K: Sync + Send + Copy + Hash + Eq,
V: Sync + Send + Copy,
S: BuildHasher + Default,
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]
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send,
S: BuildHasher + Default,
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self
[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]
K: Sync + Send + Clone + Eq + Hash,
V: Sync + Send + PartialEq,
S: BuildHasher,
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]
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send + PartialEq,
S: BuildHasher,
fn eq(&self, other: &HashMap<K, V, S>) -> bool
[src]
#[must_use]
fn ne(&self, other: &Rhs) -> bool
1.0.0[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]
K: Sync + Send + Clone + Hash + Eq,
V: Sync + Send + PartialEq,
S: BuildHasher,
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,
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,
K: Send + Sync,
S: Sync,
V: Send + Sync,
impl<K, V, S> Unpin for HashMap<K, V, S> where
S: Unpin,
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]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> ToOwned for T where
T: Clone,
[src]
T: Clone,
type Owned = T
The resulting type after obtaining ownership.
fn to_owned(&self) -> T
[src]
fn clone_into(&self, target: &mut T)
[src]
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,