Skip to content

Commit 6be3e66

Browse files
committed
feat(indexmap): added get_index, get_index_mut and get_index_of
1 parent 0ebca23 commit 6be3e66

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
3939
- Added `SortedLinkedListView`, the `!Sized` version of `SortedLinkedList`.
4040
- Added implementation of `Borrow` and `BorrowMut` for `String` and `Vec`.
4141
- Added `Deque::{swap, swap_unchecked, swap_remove_front, swap_remove_back}`.
42+
- Added `get_index` and `get_index_mut` to `IndexMap`
4243

4344
### Changed
4445

src/indexmap.rs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,71 @@ where
10841084
}
10851085
}
10861086

1087+
/// Returns a tuple of references to the key and the value corresponding to the index.
1088+
///
1089+
/// Computes in *O*(1) time (average).
1090+
///
1091+
/// ```
1092+
/// use heapless::FnvIndexMap;
1093+
///
1094+
/// let mut map = FnvIndexMap::<_, _, 16>::new();
1095+
/// map.insert(1, "a").unwrap();
1096+
/// assert_eq!(map.get_index(0), Some((&1, &"a")));
1097+
/// assert_eq!(map.get_index(1), None);
1098+
/// ```
1099+
pub fn get_index(&self, index: usize) -> Option<(&K, &V)> {
1100+
self.core
1101+
.entries
1102+
.get(index)
1103+
.map(|entry| (&entry.key, &entry.value))
1104+
}
1105+
1106+
/// Returns a tuple of references to the key and the mutable value corresponding to the index.
1107+
///
1108+
/// Computes in *O*(1) time (average).
1109+
///
1110+
/// ```
1111+
/// use heapless::FnvIndexMap;
1112+
///
1113+
/// let mut map = FnvIndexMap::<_, _, 8>::new();
1114+
/// map.insert(1, "a").unwrap();
1115+
/// if let Some((_, x)) = map.get_index_mut(0) {
1116+
/// *x = "b";
1117+
/// }
1118+
/// assert_eq!(map[&1], "b");
1119+
/// ```
1120+
pub fn get_index_mut(&mut self, index: usize) -> Option<(&K, &mut V)> {
1121+
self.core
1122+
.entries
1123+
.get_mut(index)
1124+
.map(|entry| (&entry.key, &mut entry.value))
1125+
}
1126+
1127+
/// Returns the index of the key-value pair corresponding to the key.
1128+
///
1129+
/// The key may be any borrowed form of the map's key type, but `Hash` and `Eq` on the borrowed
1130+
/// form *must* match those for the key type.
1131+
///
1132+
/// Computes in *O*(1) time (average).
1133+
///
1134+
/// ```
1135+
/// use heapless::FnvIndexMap;
1136+
///
1137+
/// let mut map = FnvIndexMap::<_, _, 8>::new();
1138+
/// map.insert(1, "a").unwrap();
1139+
/// map.insert(0, "b").unwrap();
1140+
/// assert_eq!(map.get_index_of(&0), Some(1));
1141+
/// assert_eq!(map.get_index_of(&1), Some(0));
1142+
/// assert_eq!(map.get_index_of(&2), None);
1143+
/// ```
1144+
pub fn get_index_of<Q>(&self, key: &Q) -> Option<usize>
1145+
where
1146+
K: Borrow<Q>,
1147+
Q: ?Sized + Hash + Eq,
1148+
{
1149+
self.find(key).map(|(_, found)| found)
1150+
}
1151+
10871152
/// Inserts a key-value pair into the map.
10881153
///
10891154
/// If an equivalent key already exists in the map: the key remains and retains in its place in

0 commit comments

Comments
 (0)