indexmap-rs / indexmap

A hash table with consistent order and fast iteration; access items by key or sequence index
https://docs.rs/indexmap/
Other
1.67k stars 150 forks source link

Add indexing methods to raw entry #305

Closed cuviper closed 7 months ago

cuviper commented 7 months ago

The std-matching methods of RawEntryBuilder only return (&K, &V), but the index is also useful when working with IndexMap.

impl<'a, K, V, S> RawEntryBuilder<'a, K, V, S> {
    /// Access an entry by hash, including its index.
    pub fn from_hash_full<F>(self, hash: u64, is_match: F) -> Option<(usize, &'a K, &'a V)>
    where
        F: FnMut(&K) -> bool,

    /// Access the index of an entry by hash.
    pub fn index_from_hash<F>(self, hash: u64, is_match: F) -> Option<usize>
    where
        F: FnMut(&K) -> bool,
}

In addition, the RawEntryMut enum can report its index by forwarding to the existing methods on its variants.

impl<'a, K, V, S> RawEntryMut<'a, K, V, S> {
    /// Return the index where the key-value pair exists or may be inserted.
    pub fn index(&self) -> usize
}