Struct IdMap

Source
pub struct IdMap<K: IntegerId, V, T: EntryTable<K, V> = DenseEntryTable<K, V>> { /* private fields */ }
Expand description

A map of mostly-contiguous IntegerId keys to values, backed by a Vec.

This is parametric over the type of the underlying EntryTable, which controls its behavior By default it’s equivalent to an OrderedIdMap, though you can explicitly request a DirectIdMap instead.

From the user’s perspective, this is equivalent to a nice wrapper around a Vec<Option<(K, V)>>, that preserves insertion order and saves some space for missing keys. More details on the possible internal representations are documented in the OrderedIdMap and DirectIdMap aliases.

Implementations§

Source§

impl<K: IntegerId, V> IdMap<K, V, DirectEntryTable<K, V>>

Source

pub fn new_direct() -> Self

Create a new direct IdMap.

This stores its entries directly in a Vector

Source

pub fn with_capacity_direct(capacity: usize) -> Self

Create new direct IdMap, initialized with the specified capacity

Because a direct id map stores its values directly, the capacity hints at the maximum id and not the length

Source§

impl<K: IntegerId, V> IdMap<K, V>

Source

pub fn new() -> Self

Create an empty IdMap using a DenseEntryTable and OrderedIdTable

Source

pub fn with_capacity(capacity: usize) -> Self

Create an IdMap with the specified capacity, using an OrderedIdTable

Source§

impl<K: IntegerId, V, T: EntryTable<K, V>> IdMap<K, V, T>

Source

pub fn new_other() -> Self

Create an empty IdMap with a custom entry table type.

Source

pub fn with_capacity_other(capacity: usize) -> Self

Create a new IdMap with the specified capacity but a custom entry table.

Source

pub fn is_empty(&self) -> bool

If this map is empty

Source

pub fn len(&self) -> usize

The length of this map

This doesn’t necessarily equal the maximum integer id

Source

pub fn max_id(&self) -> Option<u64>

The maximum id of all the elements in this map

Source

pub fn contains_key<Q: Borrow<K>>(&self, key: Q) -> bool

If this map contains the specified key

Source

pub fn get<Q: Borrow<K>>(&self, key: Q) -> Option<&V>

Retrieve the value associated with the specified key, or None if the value is not present

Keys that implement IntegerId are expected to be cheap, so we don’t borrow the key.

Source

pub fn get_mut<Q: Borrow<K>>(&mut self, key: Q) -> Option<&mut V>

Retrieve a mutable reference to the value associated with the specified key, or None if the value is not present

Source

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

Insert the specified value, associating it with a key

Returns the value previously associated with they key, or None if there wasn’t any

Source

pub fn remove<Q: Borrow<K>>(&mut self, key: Q) -> Option<V>

Remove the value associated with the specified key, or None if there isn’t any

Source

pub fn entry(&mut self, key: K) -> Entry<'_, K, V, T>

Retrieve the entry associated with the specified key

Mimics the HashMap entry aPI

Source

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

Iterate over all the keys and values in this map

Source

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

Iterate over all the entries in this map, giving mutable references to the keys

Source

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

Iterate over all the keys in the map

Source

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

Iterate over all the values in the map

Source

pub fn values_mut(&mut self) -> ValuesMut<'_, K, V, T>

Iterate over mutable references to all the values in the map

Source

pub fn clear(&mut self)

Clear all the entries the map

Like Vec::clear this should retain the underlying memory for future use.

Source

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

Retains only the elements specified by the predicate.

let mut map: IdMap<usize, usize> = (0..8).map(|x|(x, x*10)).collect();
map.retain(|k, _| k % 2 == 0);
assert_eq!(
    map.into_iter().collect::<Vec<_>>(),
    vec![(0, 0), (2, 20), (4, 40), (6, 60)]
);
Source

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

Reserve space for the specified number of additional elements

Source

pub fn raw_debug(&self) -> RawDebug<'_, K, V, T>
where K: Debug, V: Debug,

Give a wrapper that will debug the underlying representation of this IdMap

Trait Implementations§

Source§

impl<K, V, T> Clone for IdMap<K, V, T>
where K: IntegerId + Clone, V: Clone, T: EntryTable<K, V>,

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<K: IntegerId, V: Debug, T: EntryTable<K, V>> Debug for IdMap<K, V, T>

Source§

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

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

impl<K: IntegerId, V, T: EntryTable<K, V>> Default for IdMap<K, V, T>

Source§

fn default() -> Self

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

impl<'de, K, V, T> Deserialize<'de> for IdMap<K, V, T>
where K: Deserialize<'de> + IntegerId, T: EntryTable<K, V>, V: Deserialize<'de>,

Source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<'a, K, V, T> Extend<(&'a K, &'a V)> for IdMap<K, V, T>
where K: IntegerId + Clone + 'a, V: Clone + 'a, T: EntryTable<K, V>,

Source§

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

Extends a collection with the contents of an iterator. Read more
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: IntegerId, V, T: EntryTable<K, V>> Extend<(K, V)> for IdMap<K, V, T>

Source§

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

Extends a collection with the contents of an iterator. Read more
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<'a, K, V, T> FromIterator<(&'a K, &'a V)> for IdMap<K, V, T>
where K: IntegerId + Clone + 'a, V: Clone + 'a, T: EntryTable<K, V>,

Source§

fn from_iter<I>(iterable: I) -> Self
where I: IntoIterator<Item = (&'a K, &'a V)>,

Creates a value from an iterator. Read more
Source§

impl<K: IntegerId, V, T: EntryTable<K, V>> FromIterator<(K, V)> for IdMap<K, V, T>

Source§

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

Creates a value from an iterator. Read more
Source§

impl<'a, K: IntegerId, V, T: EntryTable<K, V>> Index<&'a K> for IdMap<K, V, T>

Source§

type Output = V

The returned type after indexing.
Source§

fn index(&self, key: &'a K) -> &V

Performs the indexing (container[index]) operation. Read more
Source§

impl<K: IntegerId, V, T: EntryTable<K, V>> Index<K> for IdMap<K, V, T>

Source§

type Output = V

The returned type after indexing.
Source§

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

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a, K: IntegerId, V, T: EntryTable<K, V>> IndexMut<&'a K> for IdMap<K, V, T>

Source§

fn index_mut(&mut self, key: &'a K) -> &mut V

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<K: IntegerId, V, T: EntryTable<K, V>> IndexMut<K> for IdMap<K, V, T>

Source§

fn index_mut(&mut self, key: K) -> &mut V

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a, K: IntegerId + 'a, V: 'a, T> IntoIterator for &'a IdMap<K, V, T>
where T: EntryTable<K, V> + 'a,

Source§

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

The type of the elements being iterated over.
Source§

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

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, T> IntoIterator for &'a mut IdMap<K, V, T>
where T: EntryTable<K, V> + 'a, K: IntegerId + 'a, V: 'a,

Source§

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

The type of the elements being iterated over.
Source§

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

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: IntegerId, V, T: EntryTable<K, V>> IntoIterator for IdMap<K, V, T>

Source§

type Item = (K, V)

The type of the elements being iterated over.
Source§

type IntoIter = <T as IntoIterator>::IntoIter

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, V1, T1, V2, T2> PartialEq<IdMap<K, V2, T2>> for IdMap<K, V1, T1>
where K: IntegerId, V1: PartialEq<V2>, T1: EntryTable<K, V1>, T2: EntryTable<K, V2>,

Checks if two have the same contents, ignoring the order.

Source§

fn eq(&self, other: &IdMap<K, V2, T2>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · 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.
Source§

impl<K, V, T> Serialize for IdMap<K, V, T>
where K: IntegerId + Serialize, V: Serialize, T: EntryTable<K, V>,

Source§

fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<K, V, T> Freeze for IdMap<K, V, T>
where T: Freeze,

§

impl<K, V, T> RefUnwindSafe for IdMap<K, V, T>

§

impl<K, V, T> Send for IdMap<K, V, T>
where T: Send, K: Send, V: Send,

§

impl<K, V, T> Sync for IdMap<K, V, T>
where T: Sync, K: Sync, V: Sync,

§

impl<K, V, T> Unpin for IdMap<K, V, T>
where T: Unpin, K: Unpin, V: Unpin,

§

impl<K, V, T> UnwindSafe for IdMap<K, V, T>
where T: 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> 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.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,