Skip to content

Commit

Permalink
feat(indexmap): added get_index, get_index_mut and get_index_of
Browse files Browse the repository at this point in the history
  • Loading branch information
pamburus committed Dec 28, 2024
1 parent 0ebca23 commit 6be3e66
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Added `SortedLinkedListView`, the `!Sized` version of `SortedLinkedList`.
- Added implementation of `Borrow` and `BorrowMut` for `String` and `Vec`.
- Added `Deque::{swap, swap_unchecked, swap_remove_front, swap_remove_back}`.
- Added `get_index` and `get_index_mut` to `IndexMap`

### Changed

Expand Down
65 changes: 65 additions & 0 deletions src/indexmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,71 @@ where
}
}

/// Returns a tuple of references to the key and the value corresponding to the index.
///
/// Computes in *O*(1) time (average).
///
/// ```
/// use heapless::FnvIndexMap;
///
/// let mut map = FnvIndexMap::<_, _, 16>::new();
/// map.insert(1, "a").unwrap();
/// assert_eq!(map.get_index(0), Some((&1, &"a")));
/// assert_eq!(map.get_index(1), None);
/// ```
pub fn get_index(&self, index: usize) -> Option<(&K, &V)> {
self.core
.entries
.get(index)
.map(|entry| (&entry.key, &entry.value))
}

/// Returns a tuple of references to the key and the mutable value corresponding to the index.
///
/// Computes in *O*(1) time (average).
///
/// ```
/// use heapless::FnvIndexMap;
///
/// let mut map = FnvIndexMap::<_, _, 8>::new();
/// map.insert(1, "a").unwrap();
/// if let Some((_, x)) = map.get_index_mut(0) {
/// *x = "b";
/// }
/// assert_eq!(map[&1], "b");
/// ```
pub fn get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)> {
self.core
.entries
.get_mut(index)
.map(|entry| (&entry.key, &mut entry.value))
}

/// Returns the index of the key-value pair corresponding to the key.
///
/// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
/// form *must* match those for the key type.
///
/// Computes in *O*(1) time (average).
///
/// ```
/// use heapless::FnvIndexMap;
///
/// let mut map = FnvIndexMap::<_, _, 8>::new();
/// map.insert(1, "a").unwrap();
/// map.insert(0, "b").unwrap();
/// assert_eq!(map.get_index_of(&0), Some(1));
/// assert_eq!(map.get_index_of(&1), Some(0));
/// assert_eq!(map.get_index_of(&2), None);
/// ```
pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
where
K: Borrow<Q>,
Q: ?Sized + Hash + Eq,
{
self.find(key).map(|(_, found)| found)
}

/// Inserts a key-value pair into the map.
///
/// If an equivalent key already exists in the map: the key remains and retains in its place in
Expand Down

0 comments on commit 6be3e66

Please sign in to comment.