Struct IndexedMap

Source
pub struct IndexedMap<K: Hash + Ord, V> { /* private fields */ }
Expand description

A map which can be iterated in a deterministic order.

This would traditionally be accomplished by simply using a BTreeMap, however B-Trees generally have very slow lookups. Because we use a nodes+channels map while finding routes across the network graph, our network graph backing map must be as performant as possible. However, because peers expect to sync the network graph from us (and we need to support that without holding a lock on the graph for the duration of the sync or dumping the entire graph into our outbound message queue), we need an iterable map with a consistent iteration order we can jump to a starting point on.

Thus, we have a custom data structure here - its API mimics that of Rust’s BTreeMap, but is actually backed by a HashMap, with some additional tracking to ensure we can iterate over keys in the order defined by Ord.

This is not exported to bindings users as bindings provide alternate accessors rather than exposing maps directly.

Implementations§

Source§

impl<K: Clone + Hash + Ord, V> IndexedMap<K, V>

Source

pub fn new() -> Self

Constructs a new, empty map

Source

pub fn with_capacity(capacity: usize) -> Self

Constructs a new, empty map with the given capacity pre-allocated

Source

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

Fetches the element with the given key, if one exists.

Source

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

Fetches a mutable reference to the element with the given key, if one exists.

Source

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

Fetches the key-value pair corresponding to the supplied key, if one exists.

Source

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

Returns true if an element with the given key exists in the map.

Source

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

Removes the element with the given key, returning it, if one exists.

Source

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

Inserts the given key/value pair into the map, returning the element that was previously stored at the given key, if one exists.

Source

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

Returns an Entry for the given key in the map, allowing access to the value.

Source

pub fn unordered_keys(&self) -> impl Iterator<Item = &K>

Returns an iterator which iterates over the keys in the map, in a random order.

Source

pub fn unordered_iter(&self) -> impl Iterator<Item = (&K, &V)>

Returns an iterator which iterates over the key/value pairs in a random order.

Source

pub fn unordered_iter_mut(&mut self) -> impl Iterator<Item = (&K, &mut V)>

Returns an iterator which iterates over the keys and mutable references to values in a random order.

Source

pub fn range<R: RangeBounds<K>>(&mut self, range: R) -> Range<'_, K, V>

Returns an iterator which iterates over the key/value pairs in a given range.

Source

pub fn len(&self) -> usize

Returns the number of key/value pairs in the map

Source

pub fn is_empty(&self) -> bool

Returns true if there are no elements in the map

Trait Implementations§

Source§

impl<K: Clone + Hash + Ord, V: Clone> Clone for IndexedMap<K, V>

Source§

fn clone(&self) -> IndexedMap<K, V>

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: Debug + Hash + Ord, V: Debug> Debug for IndexedMap<K, V>

Source§

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

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

impl<K: Hash + Ord + PartialEq, V: PartialEq> PartialEq for IndexedMap<K, V>

Source§

fn eq(&self, other: &Self) -> 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: Eq + Hash + Ord, V: Eq> Eq for IndexedMap<K, V>

Auto Trait Implementations§

§

impl<K, V> Freeze for IndexedMap<K, V>

§

impl<K, V> RefUnwindSafe for IndexedMap<K, V>

§

impl<K, V> Send for IndexedMap<K, V>
where K: Send, V: Send,

§

impl<K, V> Sync for IndexedMap<K, V>
where K: Sync, V: Sync,

§

impl<K, V> Unpin for IndexedMap<K, V>
where K: Unpin, V: Unpin,

§

impl<K, V> UnwindSafe for IndexedMap<K, V>
where 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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V