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