Struct HashMap

Source
pub struct HashMap<K, V, S = DefaultHashBuilder> { /* private fields */ }
Expand description

A lockfree hash map implemented with bucket pointer arrays, open addressing, and linear probing.

The default hashing algorithm is currently AHash, though this is subject to change at any point in the future. This hash function is very fast for all types of keys, but this algorithm will typically not protect against attacks such as HashDoS.

The hashing algorithm can be replaced on a per-HashMap basis using the default, with_hasher, and with_capacity_and_hasher methods. Many alternative algorithms are available on crates.io, such as the fnv crate.

It is required that the keys implement the Eq and Hash traits, although this can frequently be achieved by using #[derive(PartialEq, Eq, Hash)]. If you implement these yourself, it is important that the following property holds:

k1 == k2 -> hash(k1) == hash(k2)

In other words, if two keys are equal, their hashes must be equal.

It is a logic error for a key to be modified in such a way that the key’s hash, as determined by the Hash trait, or its equality, as determined by the Eq trait, changes while it is in the map. This is normally only possible through Cell, RefCell, global state, I/O, or unsafe code.

Implementations§

Source§

impl<K, V> HashMap<K, V, DefaultHashBuilder>

Source

pub fn new() -> HashMap<K, V, DefaultHashBuilder>

Creates an empty HashMap.

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

Source

pub fn with_capacity(capacity: usize) -> HashMap<K, V, DefaultHashBuilder>

Creates an empty HashMap with the specified capacity.

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

Source§

impl<K, V, S> HashMap<K, V, S>

Source

pub fn with_hasher(build_hasher: S) -> HashMap<K, V, S>

Creates an empty HashMap which will use the given hash builder to hash keys.

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

Source

pub fn with_capacity_and_hasher( capacity: usize, build_hasher: S, ) -> HashMap<K, V, S>

Creates an empty HashMap with the specified capacity, using build_hasher to hash the keys.

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

Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

§Safety

This method on its own is safe, but other threads can add or remove elements at any time.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

§Safety

This method on its own is safe, but other threads can add or remove elements at any time.

Source

pub fn capacity(&self) -> usize

Returns the number of elements the map can hold without reallocating its bucket pointer array.

Note that all mutating operations except removal will result in a bucket being allocated or reallocated.

§Safety

This method on its own is safe, but other threads can increase the capacity at any time by adding elements.

Source§

impl<K: Hash + Eq, V, S: BuildHasher> HashMap<K, V, S>

Source

pub fn get<Q: Hash + Eq + ?Sized>(&self, key: &Q) -> Option<V>
where K: Borrow<Q>, V: Clone,

Returns a clone of 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.

Source

pub fn get_key_value<Q: Hash + Eq + ?Sized>(&self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q> + Clone, V: Clone,

Returns a clone of the the key-value pair corresponding to the supplied 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.

Source

pub fn get_and<Q: Hash + Eq + ?Sized, F: FnOnce(&V) -> T, T>( &self, key: &Q, with_value: F, ) -> Option<T>
where K: Borrow<Q>,

Returns the result of invoking a function with 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.

Source

pub fn get_key_value_and<Q: Hash + Eq + ?Sized, F: FnOnce(&K, &V) -> T, T>( &self, key: &Q, with_entry: F, ) -> Option<T>
where K: Borrow<Q>,

Returns the result of invoking a function with a reference to the key-value pair corresponding to the supplied 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.

Source

pub fn insert(&self, key: K, value: V) -> Option<V>
where V: Clone,

Inserts a key-value pair into the map, returning a clone of the value previously corresponding to the key.

If the map did have this key present, both the key and value are updated.

Source

pub fn insert_entry(&self, key: K, value: V) -> Option<(K, V)>
where K: Clone, V: Clone,

Inserts a key-value pair into the map, returning a clone of the key-value pair previously corresponding to the supplied key.

If the map did have this key present, both the key and value are updated.

Source

pub fn insert_and<F: FnOnce(&V) -> T, T>( &self, key: K, value: V, with_previous_value: F, ) -> Option<T>

Inserts a key-value pair into the map, returning the result of invoking a function with a reference to the value previously corresponding to the key.

If the map did have this key present, both the key and value are updated.

Source

pub fn insert_entry_and<F: FnOnce(&K, &V) -> T, T>( &self, key: K, value: V, with_previous_entry: F, ) -> Option<T>

Inserts a key-value pair into the map, returning the result of invoking a function with a reference to the key-value pair previously corresponding to the supplied key.

If the map did have this key present, both the key and value are updated.

Source

pub fn remove<Q: Hash + Eq + ?Sized>(&self, key: &Q) -> Option<V>
where K: Borrow<Q>, V: Clone,

Removes a key from the map, returning a clone of the value previously 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.

Source

pub fn remove_entry<Q: Hash + Eq + ?Sized>(&self, key: &Q) -> Option<(K, V)>
where K: Borrow<Q> + Clone, V: Clone,

Removes a key from the map, returning a clone of the key-value pair previously 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.

Source

pub fn remove_and<Q: Hash + Eq + ?Sized, F: FnOnce(&V) -> T, T>( &self, key: &Q, with_previous_value: F, ) -> Option<T>
where K: Borrow<Q>,

Remove a key from the map, returning the result of invoking a function with a reference to the value previously 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.

Source

pub fn remove_entry_and<Q: Hash + Eq + ?Sized, F: FnOnce(&K, &V) -> T, T>( &self, key: &Q, with_previous_entry: F, ) -> Option<T>
where K: Borrow<Q>,

Removes a key from the map, returning the result of invoking a function with a reference to the key-value pair previously 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.

Source

pub fn remove_if<Q: Hash + Eq + ?Sized, F: FnMut(&K, &V) -> bool>( &self, key: &Q, condition: F, ) -> Option<V>
where K: Borrow<Q>, V: Clone,

Removes a key from the map if a condition is met, returning a clone of the value previously corresponding to the key.

condition will be invoked at least once if Some is returned. It may also be invoked one or more times if None is returned.

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.

Source

pub fn remove_entry_if<Q: Hash + Eq + ?Sized, F: FnMut(&K, &V) -> bool>( &self, key: &Q, condition: F, ) -> Option<(K, V)>
where K: Clone + Borrow<Q>, V: Clone,

Removes a key from the map if a condition is met, returning a clone of the key-value pair previously corresponding to the key.

condition will be invoked at least once if Some is returned. It may also be invoked one or more times if None is returned.

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.

Source

pub fn remove_if_and<Q: Hash + Eq + ?Sized, F: FnMut(&K, &V) -> bool, G: FnOnce(&V) -> T, T>( &self, key: &Q, condition: F, with_previous_value: G, ) -> Option<T>
where K: Borrow<Q>,

Remove a key from the map if a condition is met, returning the result of invoking a function with a reference to the value previously corresponding to the key.

condition will be invoked at least once if Some is returned. It may also be invoked one or more times if None is returned.

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.

Source

pub fn remove_entry_if_and<Q: Hash + Eq + ?Sized, F: FnMut(&K, &V) -> bool, G: FnOnce(&K, &V) -> T, T>( &self, key: &Q, condition: F, with_previous_entry: G, ) -> Option<T>
where K: Borrow<Q>,

Removes a key from the map if a condition is met, returning the result of invoking a function with a reference to the key-value pair previously corresponding to the key.

condition will be invoked at least once if Some is returned. It may also be invoked one or more times if None is returned.

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.

Source

pub fn insert_or_modify<F: FnMut(&K, &V) -> V>( &self, key: K, value: V, on_modify: F, ) -> Option<V>
where V: Clone,

If no value corresponds to the key, insert a new key-value pair into the map. Otherwise, modify the existing value and return a clone of the value previously corresponding to the key.

on_modify will be invoked at least once if Some is returned. It may also be invoked one or more times if None is returned.

Source

pub fn insert_or_modify_entry<F: FnMut(&K, &V) -> V>( &self, key: K, value: V, on_modify: F, ) -> Option<(K, V)>
where K: Clone, V: Clone,

If no value corresponds to the key, insert a new key-value pair into the map. Otherwise, modify the existing value and return a clone of the key-value pair previously corresponding to the key.

on_modify will be invoked at least once if Some is returned. It may also be invoked one or more times if None is returned.

Source

pub fn insert_with_or_modify<F: FnOnce() -> V, G: FnMut(&K, &V) -> V>( &self, key: K, on_insert: F, on_modify: G, ) -> Option<V>
where V: Clone,

If no value corresponds to the key, invoke a default function to insert a new key-value pair into the map. Otherwise, modify the existing value and return a clone of the value previously corresponding to the key.

on_insert may be invoked, even if None is returned.

on_modify will be invoked at least once if Some is returned. It may also be invoked one or more times if None is returned.

Source

pub fn insert_with_or_modify_entry<F: FnOnce() -> V, G: FnMut(&K, &V) -> V>( &self, key: K, on_insert: F, on_modify: G, ) -> Option<(K, V)>
where K: Clone, V: Clone,

If no value corresponds to the key, invoke a default function to insert a new key-value pair into the map. Otherwise, modify the existing value and return a clone of the key-value pair previously corresponding to the key.

on_insert may be invoked, even if None is returned.

on_modify will be invoked at least once if Some is returned. It may also be invoked one or more times if None is returned.

Source

pub fn insert_or_modify_and<F: FnMut(&K, &V) -> V, G: FnOnce(&V) -> T, T>( &self, key: K, value: V, on_modify: F, with_old_value: G, ) -> Option<T>

If no value corresponds to the key, insert a new key-value pair into the map. Otherwise, modify the existing value and return the result of invoking a function with a reference to the value previously corresponding to the key.

on_modify will be invoked at least once if Some is returned. It may also be invoked one or more times if None is returned.

Source

pub fn insert_or_modify_entry_and<F: FnMut(&K, &V) -> V, G: FnOnce(&K, &V) -> T, T>( &self, key: K, value: V, on_modify: F, with_old_entry: G, ) -> Option<T>

If no value corresponds to the key, insert a new key-value pair into the map. Otherwise, modify the existing value and return the result of invoking a function with a reference to the key-value pair previously corresponding to the supplied key.

on_modify will be invoked at least once if Some is returned. It may also be invoked one or more times if None is returned.

Source

pub fn insert_with_or_modify_and<F: FnOnce() -> V, G: FnMut(&K, &V) -> V, H: FnOnce(&V) -> T, T>( &self, key: K, on_insert: F, on_modify: G, with_old_value: H, ) -> Option<T>

If no value corresponds to the key, invoke a default function to insert a new key-value pair into the map. Otherwise, modify the existing value and return the result of invoking a function with a reference to the value previously corresponding to the key.

on_insert may be invoked, even if None is returned.

on_modify will be invoked at least once if Some is returned. It may also be invoked one or more times if None is returned.

Source

pub fn insert_with_or_modify_entry_and<F: FnOnce() -> V, G: FnMut(&K, &V) -> V, H: FnOnce(&K, &V) -> T, T>( &self, key: K, on_insert: F, on_modify: G, with_old_entry: H, ) -> Option<T>

If no value corresponds to the key, invoke a default function to insert a new key-value pair into the map. Otherwise, modify the existing value and return the result of invoking a function with a reference to the key-value pair previously corresponding to the supplied key.

on_insert may be invoked, even if None is returned.

on_modify will be invoked at least once if Some is returned. It may also be invoked one or more times if None is returned.

Source

pub fn modify<F: FnMut(&K, &V) -> V>(&self, key: K, on_modify: F) -> Option<V>
where V: Clone,

Modifies the value corresponding to a key, returning a clone of the value previously corresponding to that key.

Source

pub fn modify_entry<F: FnMut(&K, &V) -> V>( &self, key: K, on_modify: F, ) -> Option<(K, V)>
where K: Clone, V: Clone,

Modifies the value corresponding to a key, returning a clone of the key-value pair previously corresponding to that key.

Source

pub fn modify_and<F: FnMut(&K, &V) -> V, G: FnOnce(&V) -> T, T>( &self, key: K, on_modify: F, with_old_value: G, ) -> Option<T>

Modifies the value corresponding to a key, returning the result of invoking a function with a reference to the value previously corresponding to the key.

Source

pub fn modify_entry_and<F: FnMut(&K, &V) -> V, G: FnOnce(&K, &V) -> T, T>( &self, key: K, on_modify: F, with_old_entry: G, ) -> Option<T>

Modifies the value corresponding to a key, returning the result of invoking a function with a reference to the key-value pair previously corresponding to the supplied key.

Trait Implementations§

Source§

impl<K: Default, V: Default, S: Default> Default for HashMap<K, V, S>

Source§

fn default() -> HashMap<K, V, S>

Returns the “default value” for a type. Read more
Source§

impl<K, V, S> Drop for HashMap<K, V, S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

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

§

impl<K, V, S> RefUnwindSafe for HashMap<K, V, S>

§

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

§

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

§

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

§

impl<K, V, S> UnwindSafe for HashMap<K, V, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.