Skip to main content

SparseMap

Struct SparseMap 

Source
pub struct SparseMap<K, V, H = StdHash, E = StdEq, P = PowerOfTwo<2>, S = Medium> { /* private fields */ }
Expand description

A hash map that trades a little insert speed for low memory use.

Type parameters:

  • K, V: key and value types.
  • H: the hasher. Defaults to StdHash, which uses the standard BuildHasher. A hasher yields a usize directly.
  • E: the key comparator. Defaults to StdEq.
  • P: the growth policy. Defaults to PowerOfTwo with factor 2.
  • S: the sparsity level. Defaults to Medium.

Implementations§

Source§

impl<K, V> SparseMap<K, V, StdHash, StdEq, PowerOfTwo<2>, Medium>

Source

pub fn new() -> Self

An empty map with no allocation.

Construction places no bound on K. The Hash and Eq bounds belong to the operations that hash or compare a key.

Source

pub fn with_bucket_count(bucket_count: usize) -> Self

An empty map sized for at least bucket_count buckets.

§Panics

Panics when bucket_count exceeds the policy maximum. Use SparseMap::try_with_bucket_count for the fallible form.

Source

pub fn try_with_bucket_count(bucket_count: usize) -> Result<Self, LengthError>

An empty map sized for at least bucket_count buckets, fallibly.

Source§

impl<K, V, B, P, S> SparseMap<K, V, StdHash<B>, StdEq, P, S>
where K: Eq, B: BuildHasher + Default, P: GrowthPolicy, S: Sparsity, StdHash<B>: HashKey<K>,

Source

pub fn with_hasher_and_bucket_count(bucket_count: usize) -> Self

An empty map that hashes with B and uses policy P and sparsity S.

§Panics

Panics when bucket_count exceeds the policy maximum.

Source§

impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>
where H: HashKey<K> + Clone, E: EqKey<K, K> + Clone, P: GrowthPolicy, S: Sparsity,

Source

pub fn with_parts(bucket_count: usize, hash: H, key_eq: E) -> Self

Build a map from explicit hasher, comparator, policy, and sparsity.

§Panics

Panics when bucket_count exceeds the policy maximum.

Source

pub fn try_with_parts( bucket_count: usize, hash: H, key_eq: E, ) -> Result<Self, LengthError>

Build a map fallibly from explicit parts.

Source

pub fn len(&self) -> usize

Number of entries.

Source

pub fn is_empty(&self) -> bool

Whether the map holds no entries.

Source

pub fn max_size(&self) -> usize

The largest number of entries the map can hold.

Source

pub fn bucket_count(&self) -> usize

The logical bucket count. Zero for a fresh map.

Source

pub fn max_bucket_count(&self) -> usize

The largest bucket count the map can hold.

Source

pub fn load_factor(&self) -> f32

Ratio of entries to buckets. Zero for an empty map.

Source

pub fn max_load_factor(&self) -> f32

The maximum load factor before a grow.

Source

pub fn set_max_load_factor(&mut self, ml: f32)

Set the maximum load factor, clamped to [0.1, 0.8].

Source

pub fn hash_function(&self) -> &H

The hasher.

Source

pub fn key_eq(&self) -> &E

The key comparator.

Source

pub fn clear(&mut self)

Remove every entry. Keeps the bucket count.

Source

pub fn rehash(&mut self, count: usize)

Grow so the map holds at least count buckets.

Source

pub fn reserve(&mut self, count: usize)

Reserve room for count entries without exceeding the load factor.

Source

pub fn insert(&mut self, key: K, value: V) -> bool

Insert key with value, keeping the existing value on a collision.

Returns whether a new entry was created. insert never overwrites: when key is already present the stored value stays and the passed value is dropped. To keep the rejected value, use SparseMap::try_insert. To overwrite, use SparseMap::insert_or_assign.

Source

pub fn try_insert(&mut self, key: K, value: V) -> Result<&mut V, (K, V)>

Insert key with value only when key is absent.

Returns Ok(&mut V) with a reference to the newly stored value on a fresh insert. Returns Err((key, value)) with the rejected pair when key is already present, so an expensive or move-only value is never silently dropped. The stored value is left unchanged on collision.

Source

pub fn get<Q>(&self, key: &Q) -> Option<&V>
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

A reference to the value at key.

Source

pub fn get_precalc<Q>(&self, key: &Q, hash: usize) -> Option<&V>
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

A reference to the value at key, using a precomputed hash.

The hash must equal hash_function().hash_key(key) or the result is unspecified.

Source

pub fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut V>
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

A mutable reference to the value at key.

Source

pub fn get_key_value<Q>(&self, key: &Q) -> Option<(&K, &V)>
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

A reference to the key-value pair at key.

Source

pub fn contains_key<Q>(&self, key: &Q) -> bool
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

Whether key is present.

Source

pub fn contains_key_precalc<Q>(&self, key: &Q, hash: usize) -> bool
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

Whether key is present, using a precomputed hash.

Source

pub fn count<Q>(&self, key: &Q) -> usize
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

1 when key is present, 0 otherwise.

Membership reads more directly through SparseMap::contains_key. This count form mirrors the container contract where a key maps to at most one element.

Source

pub fn count_precalc<Q>(&self, key: &Q, hash: usize) -> usize
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

1 when key is present, 0 otherwise, using a precomputed hash.

Source

pub fn equal_range<Q>(&self, key: &Q) -> EqualRange<'_, K, V>
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

The range of entries equal to key.

A map holds at most one entry per key, so the range is empty or a single entry. The returned iterator yields (&K, &V) and has length 0 or 1.

Source

pub fn equal_range_precalc<Q>( &self, key: &Q, hash: usize, ) -> EqualRange<'_, K, V>
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

The range of entries equal to key, using a precomputed hash.

Source

pub fn at<Q>(&self, key: &Q) -> &V
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

A reference to the value at key, or a panic when absent.

Indexing with map[key] is the idiomatic panic-on-missing form and reads the same value. Use SparseMap::get to handle a missing key without panicking.

§Panics

Panics when key is not present.

Source

pub fn at_precalc<Q>(&self, key: &Q, hash: usize) -> &V
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

A reference to the value at key, using a precomputed hash.

§Panics

Panics when key is not present.

Source

pub fn remove<Q>(&mut self, key: &Q) -> Option<V>
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

Remove key and return its value.

Source

pub fn erase<Q>(&mut self, key: &Q) -> usize
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

Remove key. Returns 1 when erased, 0 otherwise.

Use SparseMap::remove to get the removed value back. erase skips moving the value out, so it does no work beyond the tombstone.

Source

pub fn erase_precalc<Q>(&mut self, key: &Q, hash: usize) -> usize
where K: Borrow<Q>, Q: ?Sized, H: HashKey<Q>, E: EqKey<K, Q>,

Remove key using a precomputed hash. Returns 1 when erased, 0 otherwise.

Source

pub fn pop_front(&mut self) -> Option<(K, V)>

Remove and return the first entry in iteration order.

Source

pub fn erase_range(&mut self, skip: usize, count: usize)

Remove count entries starting at iteration index skip.

Entries are erased in iteration order. Erasing leaves a tombstone, which the next grow or cleanup reclaims. The walk is a single forward pass.

Source

pub fn erase_all(&mut self)

Remove every entry, leaving tombstones. Keeps the bucket count.

Use SparseMap::clear to also reset the tombstones and counters.

Source§

impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>
where H: HashKey<K> + Clone, E: EqKey<K, K> + Clone, P: GrowthPolicy, S: Sparsity,

Source

pub fn entry_or_default(&mut self, key: K) -> &mut V
where V: Default,

The value at key, inserting V::default() when absent.

This is the index-access behavior of a map that default-inserts.

Source

pub fn try_emplace<F>(&mut self, key: K, make: F) -> (&mut V, bool)
where F: FnOnce() -> V,

Insert only when key is absent, building the value on demand.

Returns a reference to the value and whether it was newly inserted. The value closure runs only when the key is absent, so a redundant call does not build or consume a value.

Source

pub fn insert_or_assign(&mut self, key: K, value: V) -> (&mut V, bool)

Insert value at key, or overwrite the existing value.

Returns a reference to the value and whether it was newly inserted.

Source

pub fn retain<F>(&mut self, keep: F)
where F: FnMut(&K, &mut V) -> bool,

Keep only the entries for which keep returns true.

The key is shared and the value is mutable. Removed entries become tombstones. Each sparse array is scanned once.

Source§

impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>

Source

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

A forward iterator over (&K, &V) pairs.

Source

pub fn iter_mut(&mut self) -> IterMut<'_, K, V>

A forward iterator over (&K, &mut V) pairs.

The key stays shared so it cannot change under the map.

Source

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

A forward iterator over keys.

Source

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

A forward iterator over shared value references.

Source§

impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>

Source

pub fn serialize<Sz: Serializer>(&self, serializer: &mut Sz)

Write the map through serializer in protocol order.

Source§

impl<K, V, H, E, P, S> SparseMap<K, V, H, E, P, S>
where H: HashKey<K> + Clone, E: EqKey<K, K> + Clone, P: GrowthPolicy, S: Sparsity, (K, V): Serialize + Deserialize,

Source

pub fn deserialize_with<D: Deserializer>( deserializer: &mut D, hash_compatible: bool, hash: H, key_eq: E, ) -> Result<Self, DeserializeError>

Read a map written by SparseMap::serialize.

See the engine docs for the meaning of hash_compatible.

Trait Implementations§

Source§

impl<K, V, H, E, P, S> Clone for SparseMap<K, V, H, E, P, S>
where (K, V): Clone, H: Clone, E: Clone, P: Clone,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<K, V, H, E, P, S> Debug for SparseMap<K, V, H, E, P, S>
where K: Debug, V: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<K, V, H, E, P, S> Default for SparseMap<K, V, H, E, P, S>
where H: Default, E: Default, P: GrowthPolicy,

Source§

fn default() -> Self

An empty map with default parts and no allocation.

Available for any parameterization whose hasher, comparator, and policy are themselves Default. Places no bound on K.

Source§

impl<K, V, H, E, P, S> Eq for SparseMap<K, V, H, E, P, S>
where V: Eq, H: HashKey<K> + Clone, E: EqKey<K, K> + Clone, P: GrowthPolicy, S: Sparsity,

Source§

impl<K, V, H, E, P, S> Extend<(K, V)> for SparseMap<K, V, H, E, P, S>
where H: HashKey<K> + Clone, E: EqKey<K, K> + Clone, P: GrowthPolicy, S: Sparsity,

Source§

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

Insert every pair from iter. A key already present keeps its value.

Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl<K, V, const N: usize> From<[(K, V); N]> for SparseMap<K, V>
where K: Hash + Eq,

Source§

fn from(array: [(K, V); N]) -> Self

Converts to this type from the input type.
Source§

impl<K, V> FromIterator<(K, V)> for SparseMap<K, V>
where K: Hash + Eq,

Source§

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

Creates a value from an iterator. Read more
Source§

impl<K, V, H, E, P, S, Q> Index<&Q> for SparseMap<K, V, H, E, P, S>
where K: Borrow<Q>, Q: ?Sized, H: HashKey<K> + HashKey<Q> + Clone, E: EqKey<K, K> + EqKey<K, Q> + Clone, P: GrowthPolicy, S: Sparsity,

Source§

fn index(&self, key: &Q) -> &V

The value at key.

§Panics

Panics when key is not present.

Source§

type Output = V

The returned type after indexing.
Source§

impl<'a, K, V, H, E, P, S> IntoIterator for &'a SparseMap<K, V, H, E, P, S>

Source§

type Item = (&'a K, &'a V)

The type of the elements being iterated over.
Source§

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

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<'a, K, V, H, E, P, S> IntoIterator for &'a mut SparseMap<K, V, H, E, P, S>

Source§

type Item = (&'a K, &'a mut V)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a, K, V>

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, H, E, P, S> IntoIterator for SparseMap<K, V, H, E, P, S>

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) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<K, V, H, E, P, S> PartialEq for SparseMap<K, V, H, E, P, S>
where V: PartialEq, H: HashKey<K> + Clone, E: EqKey<K, K> + Clone, P: GrowthPolicy, S: Sparsity,

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Auto Trait Implementations§

§

impl<K, V, H, E, P, S> Freeze for SparseMap<K, V, H, E, P, S>
where H: Freeze, E: Freeze, P: Freeze,

§

impl<K, V, H, E, P, S> RefUnwindSafe for SparseMap<K, V, H, E, P, S>

§

impl<K, V, H, E, P, S> Send for SparseMap<K, V, H, E, P, S>
where H: Send, E: Send, P: Send, S: Send, K: Send, V: Send,

§

impl<K, V, H, E, P, S> Sync for SparseMap<K, V, H, E, P, S>
where H: Sync, E: Sync, P: Sync, S: Sync, K: Sync, V: Sync,

§

impl<K, V, H, E, P, S> Unpin for SparseMap<K, V, H, E, P, S>
where H: Unpin, E: Unpin, P: Unpin, S: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, H, E, P, S> UnsafeUnpin for SparseMap<K, V, H, E, P, S>

§

impl<K, V, H, E, P, S> UnwindSafe for SparseMap<K, V, H, E, P, 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.