@@ -1084,6 +1084,71 @@ where
1084
1084
}
1085
1085
}
1086
1086
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
+
1087
1152
/// Inserts a key-value pair into the map.
1088
1153
///
1089
1154
/// If an equivalent key already exists in the map: the key remains and retains in its place in
0 commit comments