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,

source

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

Returns an iterator visiting all keys from the lowest rank to the highest.

source

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

Returns an iterator visiting all values from the lowest rank to the highest.

source

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

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

An iterator visiting all key-value pairs from the lowest rank to the highest.

source

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

Returns a mutable iterator visiting all key-value pairs 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,

source

pub fn into_keys(self) -> IntoKeys<K>

source

pub fn into_values(self) -> IntoValues<V>

source§

impl<K, V, R> RankMap<K, V, R>

source

pub fn new() -> Self

Creates an empty RankMap.

source

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>

source

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.

source

pub fn with_hasher(hash_builder: S) -> Self

Creates an empty RankMap which will use the given hash builder to hash keys.

source

pub fn hasher(&self) -> &S

Returns a reference to the map’s BuildHasher.

source§

impl<K, V, R, S> RankMap<K, V, R, S>

source

pub fn capacity(&self) -> usize

Returns the number of elements the map can hold without reallocation.

source

pub fn len(&self) -> usize

Returns the number of elements in the map.

source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

source§

impl<K, V, R, S> RankMap<K, V, R, S>where K: Hash + Eq, S: BuildHasher,

source

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.

source

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.

source

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

Checks if the item with the provided key exists in the map.

Time complexity

Takes O(1) on average.

source

pub fn shrink_to_fit(&mut self)

Shrink the capacity of the map as much as possible.

source

pub fn shrink_to(&mut self, min_capacity: usize)

Shrinks the capacity of the map with a lower limit.

source

pub fn clear(&mut self)

Clears the map.

Note: that this method has no effect on the map capacity.

source§

impl<K, V, R, S> RankMap<K, V, R, S>where K: Hash + Eq, R: PartialOrd + Copy, S: BuildHasher,

source

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.

source

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.

source

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.

source

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.

source

pub fn top(&self) -> Option<(&K, &V, R)>

Returns the smallest item in the map, or None if it is empty.

Time complexity

Takes O(1).

Trait Implementations§

source§

impl<K, V, R, S> Clone for RankMap<K, V, R, S>where K: Clone, V: Clone, S: Clone, R: Clone,

source§

fn clone(&self) -> Self

Returns a copy 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, V: Debug, R: Debug, S: Debug> Debug for RankMap<K, V, R, S>

source§

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

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

impl<K, V, R> Default for RankMap<K, V, R>

source§

fn default() -> Self

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

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

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<Q, K, V, R> Index<&Q> for RankMap<K, V, R>where K: Hash + Eq + Borrow<Q>, Q: Hash + Eq + ?Sized,

§

type Output = V

The returned type after indexing.
source§

fn index(&self, index: &Q) -> &Self::Output

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

impl<Q, K, V, R> IndexMut<&Q> for RankMap<K, V, R>where K: Hash + Eq + Borrow<Q>, Q: Hash + Eq + ?Sized,

source§

fn index_mut(&mut self, index: &Q) -> &mut Self::Output

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

impl<'m, K, V, R, S> IntoIterator for &'m RankMap<K, V, R, S>where R: PartialOrd + Copy,

§

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

The type of the elements being iterated over.
§

type IntoIter = Iter<'m, K, V, R, 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<'m, K, V, R, S> IntoIterator for &'m mut RankMap<K, V, R, S>where R: PartialOrd + Copy,

§

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

The type of the elements being iterated over.
§

type IntoIter = IterMut<'m, K, V, R, 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, R, S> IntoIterator for RankMap<K, V, R, S>where K: Hash + Eq, R: PartialOrd + Copy, S: BuildHasher,

§

type Item = (K, V)

The type of the elements being iterated over.
§

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

Auto Trait Implementations§

§

impl<K, V, R, S> RefUnwindSafe for RankMap<K, V, R, S>where K: RefUnwindSafe, R: RefUnwindSafe, S: RefUnwindSafe, V: RefUnwindSafe,

§

impl<K, V, R, S> Send for RankMap<K, V, R, S>where K: Send, R: Send, S: Send, V: Send,

§

impl<K, V, R, S> Sync for RankMap<K, V, R, S>where K: Sync, R: Sync, S: Sync, V: Sync,

§

impl<K, V, R, S> Unpin for RankMap<K, V, R, S>where K: Unpin, R: Unpin, S: Unpin, V: Unpin,

§

impl<K, V, R, S> UnwindSafe for RankMap<K, V, R, S>where K: UnwindSafe, R: UnwindSafe, S: UnwindSafe, V: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.