pub struct RankMap<K, V, R, S = RandomState> { /* private fields */ }
Expand description
A hash map that supports key ranking. The rank can be used to get the element with the lowest rank in O(1) time, iterate over map’s keys and values from the lowest rank to the highest in O(N) or remove the element with the lowest rank in O(log(N)).
Implementation details:
Rank map is implemented using bi-directional references from the hash-table to the binary-heap and back which helps to get rid of hash-table lookups during binary-heap sift ups and downs:
MAP HEAP
╔═══╤══════╤═════════════════╗ ╔═══╤════════════════╗
║idx│ key │(value, heap-idx)║ ║idx│(value, map-idx)║
╠═══╪══════╪═════════════════╣ ╠═══╪════════════════╣
║ 0 │ key0 │ (value0, 0) ║ ◀━━━━━━━━━━━━━▶ ║ 0 │ (rank0, 0) ║
╟───┼──────┼─────────────────╢ ╟───┼────────────────╢
║ 1 │ key1 │ (value1, 3) ║ ◀━━━━┓ ┏━━━━━▶ ║ 1 │ (rank1, 2) ║
╟───┼──────┼─────────────────╢ ┃ ┃ ╟───┼────────────────╢
║ 2 │ key2 │ (value2, 1) ║ ◀━━━━━━━┛ ┏━━▶ ║ 2 │ (rank2, 3) ║
╟───┼──────┼─────────────────╢ ┃ ┃ ╟───┼────────────────╢
║ 3 │ key3 │ (value3, 2) ║ ◀━┓ ┗━━━━━━━━▶ ║ 3 │ (rank3, 1) ║
╟───┼──────┼─────────────────╢ ┃ ┃ ╟───┼────────────────╢
╏ ... ╏ ┗━━━━━━━━┛ ╏ ... ╏
╟───┼──────┼─────────────────╢ ╟───┼────────────────╢
║ N │ keyN │ (valueN, ...) ║ ║ N │ (rankN, ...) ║
╚═══╧══════╧═════════════════╝ ╚═══╧════════════════╝
Implementations§
source§impl<K, V, R, S> RankMap<K, V, R, S>where
R: PartialOrd + Copy,
impl<K, V, R, S> RankMap<K, V, R, S>where R: PartialOrd + Copy,
sourcepub fn keys(&self) -> Keys<'_, K, V, R, S> ⓘ
pub fn keys(&self) -> Keys<'_, K, V, R, S> ⓘ
Returns an iterator visiting all keys from the lowest rank to the highest.
sourcepub fn values(&self) -> Values<'_, K, V, R, S> ⓘ
pub fn values(&self) -> Values<'_, K, V, R, S> ⓘ
Returns an iterator visiting all values from the lowest rank to the highest.
sourcepub fn values_mut(&mut self) -> ValuesMut<'_, K, V, R, S> ⓘ
pub fn values_mut(&mut self) -> ValuesMut<'_, K, V, R, S> ⓘ
Returns a mutable iterator visiting all values from the lowest rank to the highest.
source§impl<K, V, R, S> RankMap<K, V, R, S>where
K: Hash + Eq,
R: PartialOrd + Copy,
S: BuildHasher,
impl<K, V, R, S> RankMap<K, V, R, S>where K: Hash + Eq, R: PartialOrd + Copy, S: BuildHasher,
sourcepub fn into_values(self) -> IntoValues<V> ⓘ
pub fn into_values(self) -> IntoValues<V> ⓘ
source§impl<K, V, R> RankMap<K, V, R>
impl<K, V, R> RankMap<K, V, R>
sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Creates an empty RankMap
pre-allocating memory for the specified number of elements.
source§impl<K, V, R, S> RankMap<K, V, R, S>
impl<K, V, R, S> RankMap<K, V, R, S>
sourcepub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
pub fn with_capacity_and_hasher(capacity: usize, hash_builder: S) -> Self
Creates an empty RankMap
pre-allocating memory for the specified number of elements,
using hash_builder
to hash the keys.
sourcepub fn with_hasher(hash_builder: S) -> Self
pub fn with_hasher(hash_builder: S) -> Self
Creates an empty RankMap
which will use the given hash builder to hash keys.
source§impl<K, V, R, S> RankMap<K, V, R, S>where
K: Hash + Eq,
S: BuildHasher,
impl<K, V, R, S> RankMap<K, V, R, S>where K: Hash + Eq, S: BuildHasher,
sourcepub fn get<Q>(&self, key: &Q) -> Option<&V>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get<Q>(&self, key: &Q) -> Option<&V>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Returns a reference to the value stored by the provided key if it is present,
otherwise returns None
.
Time complexity
Takes O(1) on average.
sourcepub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get_mut<Q>(&mut self, k: &Q) -> Option<&mut V>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Returns a mutable reference to the value stored by the provided key if it is present,
otherwise returns None
.
Time complexity
Takes O(1) on average.
sourcepub fn shrink_to_fit(&mut self)
pub fn shrink_to_fit(&mut self)
Shrink the capacity of the map as much as possible.
source§impl<K, V, R, S> RankMap<K, V, R, S>where
K: Hash + Eq,
R: PartialOrd + Copy,
S: BuildHasher,
impl<K, V, R, S> RankMap<K, V, R, S>where K: Hash + Eq, R: PartialOrd + Copy, S: BuildHasher,
sourcepub fn get_with_rank<Q>(&self, key: &Q) -> Option<(&V, R)>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn get_with_rank<Q>(&self, key: &Q) -> Option<(&V, R)>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Returns a reference to the value and the rank stored by the provided key if it is present,
otherwise returns None
.
Time complexity
Takes O(1) on average.
sourcepub fn insert(&mut self, key: K, value: V, rank: R) -> Option<(V, R)>
pub fn insert(&mut self, key: K, value: V, rank: R) -> Option<(V, R)>
Inserts the item in the map and returns None
or replaces the item
with the same key returning it.
Time complexity
Takes O(log(N)) on average.
sourcepub fn remove<Q>(&mut self, key: &Q) -> Option<(V, R)>where
K: Borrow<Q>,
Q: Hash + Eq + ?Sized,
pub fn remove<Q>(&mut self, key: &Q) -> Option<(V, R)>where K: Borrow<Q>, Q: Hash + Eq + ?Sized,
Removes the item from the map by the key and returns it, or None
if it is not found.
Time complexity
Takes O(log(N)) on average.
sourcepub fn pop(&mut self) -> Option<(K, V, R)>
pub fn pop(&mut self) -> Option<(K, V, R)>
Removes the smallest item from the map and returns it, or None
if it is empty.
Time complexity
Takes O(log(N)) on average.
Trait Implementations§
source§impl<K, V, R> Extend<(K, V, R)> for RankMap<K, V, R>where
K: Hash + Eq,
R: PartialOrd + Copy,
impl<K, V, R> Extend<(K, V, R)> for RankMap<K, V, R>where K: Hash + Eq, R: PartialOrd + Copy,
source§fn extend<I>(&mut self, iterable: I)where
I: IntoIterator<Item = (K, V, R)>,
fn extend<I>(&mut self, iterable: I)where I: IntoIterator<Item = (K, V, R)>,
source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one
)source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one
)