Skip to content

Commit 894937c

Browse files
pamburusreitermarkus
authored andcommitted
feat(indexmap): added get_index, get_index_mut and get_index_of
1 parent db6a443 commit 894937c

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
4242
- Make `String::from_utf8_unchecked` const.
4343
- Implemented `PartialEq` and `Eq` for `Deque`.
4444
- Added `truncate` to `IndexMap`.
45+
- Added `get_index` and `get_index_mut` to `IndexMap`
4546

4647
### Changed
4748

src/indexmap.rs

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,6 +1085,77 @@ where
10851085
}
10861086
}
10871087

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

0 commit comments

Comments
 (0)