Skip to main content

HashMap

Struct HashMap 

Source
pub struct HashMap<K: 'static, V: 'static, S = FixedState> { /* private fields */ }
Expand description

High-Performance Lock-Free Map with automatic grow/shrink.

Implementations§

Source§

impl<K, V> HashMap<K, V, FixedState>
where K: Hash + Eq + Clone + 'static, V: Clone + 'static,

Source

pub fn new() -> Self

Creates a new empty hash map with FoldHash (FixedState).

Source

pub fn with_capacity(capacity: usize) -> Self

Creates a new empty hash map with at least capacity buckets.

Source§

impl<K, V, S> HashMap<K, V, S>
where K: Hash + Eq + Clone + 'static, V: Clone + 'static, S: BuildHasher,

Source

pub fn with_hasher(hasher: S) -> Self

Creates a new hash map with custom hasher.

Source

pub fn with_capacity_and_hasher(capacity: usize, hasher: S) -> Self

Creates a new hash map with at least capacity buckets and a custom hasher.

The map grows when its load factor exceeds 0.75 and shrinks when it falls below 0.25 — but never below capacity.

Source

pub fn capacity(&self) -> usize

Returns the current number of buckets.

Source

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

Optimized get operation. Never blocks — reads the current table snapshot under a guard, even while a resize is in flight.

Source

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

Checks if the key exists.

Source

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

Insert a key-value pair.

Source

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

Insert a key-value pair only if the key does not exist. Returns None if inserted, Some(existing_value) if the key already exists.

Source

pub fn get_or_insert(&self, key: K, value: V) -> V

Returns the value corresponding to the key, or inserts the given value if the key is not present.

This is linearizable: concurrent callers for the same key are guaranteed to agree on which value was inserted (exactly one thread’s CAS succeeds at the list tail, and all others see that node on retry).

Source

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

Remove a key-value pair.

Source

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

Remove all nodes matching key, returning the most recent value if the key was present.

remove unlinks only the first matching entry. Insert/remove races can transiently leave more than one entry for the same key (“versions”); after a plain remove() an older version would become visible again. This method keeps removing until a full scan finds no match, so the key is guaranteed absent at the linearization point of the final scan.

Use remove() for single-version removal semantics and force_remove() when the key must be fully evicted.

Note: a concurrent insert of the same key can land after the final scan, as with any removal under contention.

Source

pub fn clear(&self)

Clear the map.

Source

pub fn is_empty(&self) -> bool

Returns true if the map is empty.

Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

O(1): maintained by insert/remove. Approximate while concurrent updates are in flight (exact in quiescence), like HopscotchMap.

Source

pub fn iter(&self) -> Iter<'_, K, V, S>

Returns an iterator over the map entries. Yields (K, V) clones from a table snapshot taken at creation.

Source

pub fn keys(&self) -> Keys<'_, K, V, S>

Returns an iterator over the map keys. Yields K clones.

Source

pub fn values(&self) -> Values<'_, K, V, S>

Returns an iterator over the map values (clones V).

Source

pub fn extend<I: IntoIterator<Item = (K, V)>>(&self, iter: I)

Insert all (K, V) pairs from iter. Takes &self (concurrent map).

Source

pub fn hasher(&self) -> &S

Get the underlying hasher itself.

Trait Implementations§

Source§

impl<K, V> Default for HashMap<K, V, FixedState>
where K: Hash + Eq + Clone + 'static, V: Clone + 'static,

Available on crate feature std only.
Source§

fn default() -> Self

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

impl<K: 'static, V: 'static, S> Drop for HashMap<K, V, S>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl<K, V, S> FromIterator<(K, V)> for HashMap<K, V, S>
where K: Hash + Eq + Clone + Send + 'static, V: Clone + Send + 'static, S: BuildHasher + Default,

Source§

fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self

Creates a value from an iterator. Read more
Source§

impl<'a, K, V, S> IntoIterator for &'a HashMap<K, V, S>
where K: Hash + Eq + Clone + 'static, V: Clone + 'static, S: BuildHasher,

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a, K, V, S>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V, S> IntoIterator for HashMap<K, V, S>
where K: 'static, V: 'static,

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter<K, V>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> IntoIter<K, V>

Creates an iterator from a value. Read more
Source§

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

Source§

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

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

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, 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.