pub struct LockFreeHashMap<'v, K, V: 'v, S = RandomState> { /* private fields */ }
Implementations§
Source§impl<'v, K, V, S> LockFreeHashMap<'v, K, V, S>
impl<'v, K, V, S> LockFreeHashMap<'v, K, V, S>
Sourcepub const DEFAULT_CAPACITY: usize = 8usize
pub const DEFAULT_CAPACITY: usize = 8usize
The default size of a new LockFreeHashMap
when created by LockFreeHashMap::new()
.
Source§impl<'guard, 'v: 'guard, K: Hash + Eq + 'guard, V: PartialEq> LockFreeHashMap<'v, K, V>
impl<'guard, 'v: 'guard, K: Hash + Eq + 'guard, V: PartialEq> LockFreeHashMap<'v, K, V>
Sourcepub fn with_capacity(size: usize) -> Self
pub fn with_capacity(size: usize) -> Self
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);
Sourcepub fn capacity(&self) -> usize
pub fn capacity(&self) -> usize
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);
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
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);
Sourcepub fn clear(&self)
pub fn clear(&self)
Clears the map, removing all key-value pairs. Keeps the allocated memory for reuse.
Sourcepub fn contains_key<Q>(&self, key: &Q) -> bool
pub fn contains_key<Q>(&self, key: &Q) -> bool
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));
Sourcepub fn get<Q>(&self, key: &Q, guard: &'guard Guard) -> Option<&'guard V>
pub fn get<Q>(&self, key: &Q, guard: &'guard Guard) -> Option<&'guard V>
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));
Sourcepub fn insert(
&self,
key: K,
value: V,
guard: &'guard Guard,
) -> Option<&'guard V>
pub fn insert( &self, key: K, value: V, guard: &'guard Guard, ) -> Option<&'guard V>
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`.
Sourcepub fn replace<Q>(
&self,
key: &Q,
value: V,
guard: &'guard Guard,
) -> Option<&'guard V>
pub fn replace<Q>( &self, key: &Q, value: V, guard: &'guard Guard, ) -> Option<&'guard V>
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));
Sourcepub fn remove<Q>(&self, key: &Q, guard: &'guard Guard) -> Option<&'guard V>
pub fn remove<Q>(&self, key: &Q, guard: &'guard Guard) -> Option<&'guard V>
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));