@@ -1213,6 +1213,66 @@ impl<K, V, S> RingMap<K, V, S> {
12131213 self . core . pop_front ( )
12141214 }
12151215
1216+ /// Removes and returns the last key-value pair from a map if the predicate
1217+ /// returns `true`, or [`None`] if the predicate returns false or the map
1218+ /// is empty (the predicate will not be called in that case).
1219+ ///
1220+ /// This preserves the order of the remaining elements.
1221+ ///
1222+ /// Computes in **O(1)** time (average).
1223+ ///
1224+ /// # Examples
1225+ ///
1226+ /// ```
1227+ /// use ringmap::RingMap;
1228+ ///
1229+ /// let init = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')];
1230+ /// let mut map = RingMap::from(init);
1231+ /// let pred = |key: &i32, _value: &mut char| *key % 2 == 0;
1232+ ///
1233+ /// assert_eq!(map.pop_back_if(pred), Some((4, 'd')));
1234+ /// assert_eq!(map.as_slices().0, &init[..3]);
1235+ /// assert_eq!(map.pop_back_if(pred), None);
1236+ /// ```
1237+ pub fn pop_back_if ( & mut self , predicate : impl FnOnce ( & K , & mut V ) -> bool ) -> Option < ( K , V ) > {
1238+ let ( last_key, last_value) = self . back_mut ( ) ?;
1239+ if predicate ( last_key, last_value) {
1240+ self . core . pop_back ( )
1241+ } else {
1242+ None
1243+ }
1244+ }
1245+
1246+ /// Removes and returns the first key-value pair from a map if the predicate
1247+ /// returns `true`, or [`None`] if the predicate returns false or the map
1248+ /// is empty (the predicate will not be called in that case).
1249+ ///
1250+ /// This preserves the order of the remaining elements.
1251+ ///
1252+ /// Computes in **O(1)** time (average).
1253+ ///
1254+ /// # Examples
1255+ ///
1256+ /// ```
1257+ /// use ringmap::RingMap;
1258+ ///
1259+ /// let init = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')];
1260+ /// let mut map = RingMap::from(init);
1261+ /// let pred = |key: &i32, _value: &mut char| *key % 2 == 1;
1262+ ///
1263+ /// assert_eq!(map.pop_front_if(pred), Some((1, 'a')));
1264+ /// assert_eq!(map.as_slices().0, &init[1..]);
1265+ /// assert_eq!(map.pop_front_if(pred), None);
1266+ /// ```
1267+ pub fn pop_front_if ( & mut self , predicate : impl FnOnce ( & K , & mut V ) -> bool ) -> Option < ( K , V ) > {
1268+ let ( first_key, first_value) = self . front_mut ( ) ?;
1269+ if predicate ( first_key, first_value) {
1270+ self . core . pop_front ( )
1271+ } else {
1272+ None
1273+ }
1274+ }
1275+
12161276 /// Scan through each key-value pair in the map and keep those where the
12171277 /// closure `keep` returns `true`.
12181278 ///
0 commit comments