[][src]Struct lockfreehashmap::LockFreeHashMap

pub struct LockFreeHashMap<'v, K, V: 'v, S = RandomState> {
    inner: AtomicBox<MapInner<'v, K, V, S>>,
}

Fields

Points to the newest map (after it's been fully resized). Always non-null.

Methods

impl<'guard, 'v: 'guard, K, V, S> LockFreeHashMap<'v, K, V, S> where
    K: 'guard + Hash + Eq,
    V: PartialEq,
    S: 'guard + BuildHasher + Clone
[src]

The default size of a new LockFreeHashMap when created by LockFreeHashMap::new().

Creates an empty LockFreeHashMap with the specified capacity, using hasher to hash the keys.

The hash map will be able to hold at least capacity elements without reallocating. If capacity is 0, the hash map will use the next power of 2 (i.e. 1).

Examples

use lockfreehashmap::LockFreeHashMap;
use std::collections::hash_map::RandomState;

let s = RandomState::new();
let mut map = LockFreeHashMap::with_capacity_and_hasher(10, s);
let guard = lockfreehashmap::pin();
map.insert(1, 2, &guard);

Private helper method to load the inner field as a &[MapInner].

Returns the number of elements the map can hold without reallocating.

Examples

let map = LockFreeHashMap::<u32, String>::with_capacity(8);
assert_eq!(map.capacity(), 8);

Returns the number of elements in the map.

Examples

let map = LockFreeHashMap::<u32, String>::with_capacity(8);
assert_eq!(map.capacity(), 8);
assert_eq!(map.len(), 0);
let guard = lockfreehashmap::pin();
map.insert(5, String::from("five"), &guard);
assert_eq!(map.capacity(), 8);
assert_eq!(map.len(), 1);

Clears the entire map.

This has the same effects as if calling LockFreeHashMap::with_capacity(), but its effects are visible to all threads. Because of this, new memory is always allocated before the old map's memory is dropped.

Examples

let map = LockFreeHashMap::<u32, String>::with_capacity(8);
let guard = lockfreehashmap::pin();
map.insert(5, String::from("five"), &guard);
assert_eq!(map.capacity(), 8);
assert_eq!(map.len(), 1);
map.clear();
assert_eq!(map.capacity(), 8);
assert_eq!(map.len(), 0);

Clears the entire map.

This has the same effects as if calling LockFreeHashMap::with_capacity(), but its effects are visible to all threads. Because of this, new memory is always allocated before the old map's memory is dropped.

Examples

let map = LockFreeHashMap::<u32, String>::with_capacity(8);
let guard = lockfreehashmap::pin();
map.insert(5, String::from("five"), &guard);
assert_eq!(map.capacity(), 8);
assert_eq!(map.len(), 1);
map.clear_with_capacity(15);
assert_eq!(map.capacity(), 16);
assert_eq!(map.len(), 0);

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

let map = LockFreeHashMap::<i32, i32>::new();
assert!(!map.contains_key(&3));
let guard = lockfreehashmap::pin();
map.insert(3, 8934, &guard);
assert!(map.contains_key(&3));
map.remove(&3, &guard);
assert!(!map.contains_key(&3));

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.

Examples

let map = LockFreeHashMap::<i32, i32>::new();
let guard = lockfreehashmap::pin();
assert_eq!(map.get(&1, &guard), None);
map.insert(1, 15, &guard);
assert_eq!(map.get(&1, &guard), Some(&15));

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.

Examples

let map = LockFreeHashMap::<String, String>::new();
let guard = lockfreehashmap::pin();
let key = "key".to_string();
let equal_key = "key".to_string();
assert_eq!(key, equal_key); // The keys are equal
assert_ne!(&key as *const _, &equal_key as *const _); // But not identical
assert_eq!(map.insert(key, "value".to_string(), &guard), None);
assert_eq!(map.insert(equal_key, "other".to_string(), &guard), Some(&"value".to_string()));
// `map` now contains `key` as its key, rather than `equal_key`.

Inserts a key-value pair into the map, but only if there is already an existing value that corresponds to the key in 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.

Examples

let map = LockFreeHashMap::<i32, i32>::new();
let guard = lockfreehashmap::pin();
assert_eq!(map.replace(&1, 1, &guard), None);
assert_eq!(map.replace(&1, 1, &guard), None);
assert_eq!(map.insert(1, 1, &guard), None);
assert_eq!(map.replace(&1, 3, &guard), Some(&1));

Removes a key from the map, returning 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

let map = LockFreeHashMap::<i32, i32>::new();
let guard = lockfreehashmap::pin();
assert_eq!(map.remove(&1, &guard), None);
map.insert(1, 1, &guard);
assert_eq!(map.remove(&1, &guard), Some(&1));

Important traits for Keys<'guard, 'v, K, V, S>

Returns an iterator over the keys in the map at one point in time. Any keys inserted or removed after this point in time may or may not be returned by this iterator.

Examples

let map = LockFreeHashMap::<i32, String>::new();
let guard = lockfreehashmap::pin();
map.insert(4, "Four".to_string(), &guard);
map.insert(8, "Eight".to_string(), &guard);
map.insert(15, "Fifteen".to_string(), &guard);
map.insert(16, "Sixteen".to_string(), &guard);
map.insert(23, "TwentyThree".to_string(), &guard);
map.insert(42, "FortyTwo".to_string(), &guard);

let mut keys = map.keys(&guard).cloned().collect::<Vec<_>>();
keys.sort();
assert_eq!(vec![4, 8, 15, 16, 23, 42], keys);

map.remove(&16, &guard);
let mut keys = map.keys(&guard).cloned().collect::<Vec<_>>();
keys.sort();
assert_eq!(vec![4, 8, 15, 23, 42], keys);

impl<'guard, 'v: 'guard, K: Hash + Eq + 'guard, V: PartialEq> LockFreeHashMap<'v, K, V>
[src]

Creates a new LockFreeHashMap.

Examples

let map = LockFreeHashMap::<u32, String>::new();

Creates a new LockFreeHashMap of a given size. Uses the next power of two if size is not a power of two.

Examples

let map = LockFreeHashMap::<u32, String>::with_capacity(12);
assert_eq!(map.capacity(), 12usize.next_power_of_two());
assert_eq!(map.capacity(), 16);

Trait Implementations

impl<'v, K, V, S> Drop for LockFreeHashMap<'v, K, V, S>
[src]

Executes the destructor for this type. Read more

impl<'guard, 'v: 'guard, K: Hash + Eq + Debug, V: Debug + PartialEq> Debug for LockFreeHashMap<'v, K, V>
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl<'v, K, V, S> Send for LockFreeHashMap<'v, K, V, S> where
    K: Send + Sync,
    S: Send + Sync,
    V: Send + Sync

impl<'v, K, V, S> Sync for LockFreeHashMap<'v, K, V, S> where
    K: Send + Sync,
    S: Send + Sync,
    V: Send + Sync

Blanket Implementations

impl<T> From for T
[src]

Performs the conversion.

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

Performs the conversion.

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

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

Immutably borrows from an owned value. Read more

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

Mutably borrows from an owned value. Read more

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

🔬 This is a nightly-only experimental API. (try_from)

The type returned in the event of a conversion error.

🔬 This is a nightly-only experimental API. (try_from)

Performs the conversion.

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

🔬 This is a nightly-only experimental API. (get_type_id)

this method will likely be replaced by an associated static

Gets the TypeId of self. Read more