@@ -556,6 +556,66 @@ impl<K, V, S> HashMap<K, V, S> {
556
556
}
557
557
}
558
558
559
+ /// Retains only the elements specified by the predicate.
560
+ ///
561
+ /// In other words, remove all pairs `(k, v)` such that `f(&k,&mut v)` returns `false`.
562
+ ///
563
+ /// # Examples
564
+ ///
565
+ /// ```
566
+ /// use hashbrown::HashMap;
567
+ ///
568
+ /// let mut map: HashMap<i32, i32> = (0..8).map(|x|(x, x*10)).collect();
569
+ /// map.retain(|&k, _| k % 2 == 0);
570
+ /// assert_eq!(map.len(), 4);
571
+ /// ```
572
+ pub fn retain < F > ( & mut self , mut f : F )
573
+ where
574
+ F : FnMut ( & K , & mut V ) -> bool ,
575
+ {
576
+ // Here we only use `iter` as a temporary, preventing use-after-free
577
+ unsafe {
578
+ for item in self . table . iter ( ) {
579
+ let & mut ( ref key, ref mut value) = item. as_mut ( ) ;
580
+ if !f ( key, value) {
581
+ // Erase the element from the table first since drop might panic.
582
+ self . table . erase_no_drop ( & item) ;
583
+ item. drop ( ) ;
584
+ }
585
+ }
586
+ }
587
+ }
588
+
589
+ /// Drains elements which are false under the given predicate,
590
+ /// and returns an iterator over the removed items.
591
+ ///
592
+ /// In other words, move all pairs `(k, v)` such that `f(&k,&mut v)` returns `false` out
593
+ /// into another iterator.
594
+ ///
595
+ /// When the returned DrainedFilter is dropped, the elements that don't satisfy
596
+ /// the predicate are dropped from the table.
597
+ ///
598
+ /// # Examples
599
+ ///
600
+ /// ```
601
+ /// use hashbrown::HashMap;
602
+ ///
603
+ /// let mut map: HashMap<i32, i32> = (0..8).map(|x|(x, x*10)).collect();
604
+ /// let drained = map.drain_filter(|&k, _| k % 2 == 0);
605
+ /// assert_eq!(drained.count(), 4);
606
+ /// assert_eq!(map.len(), 4);
607
+ /// ```
608
+ pub fn drain_filter < F > ( & mut self , f : F ) -> DrainFilter < ' _ , K , V , F >
609
+ where
610
+ F : FnMut ( & K , & mut V ) -> bool ,
611
+ {
612
+ DrainFilter {
613
+ f,
614
+ iter : unsafe { self . table . iter ( ) } ,
615
+ table : & mut self . table ,
616
+ }
617
+ }
618
+
559
619
/// Clears the map, removing all key-value pairs. Keeps the allocated memory
560
620
/// for reuse.
561
621
///
@@ -982,65 +1042,6 @@ where
982
1042
}
983
1043
}
984
1044
}
985
-
986
- /// Retains only the elements specified by the predicate.
987
- ///
988
- /// In other words, remove all pairs `(k, v)` such that `f(&k,&mut v)` returns `false`.
989
- ///
990
- /// # Examples
991
- ///
992
- /// ```
993
- /// use hashbrown::HashMap;
994
- ///
995
- /// let mut map: HashMap<i32, i32> = (0..8).map(|x|(x, x*10)).collect();
996
- /// map.retain(|&k, _| k % 2 == 0);
997
- /// assert_eq!(map.len(), 4);
998
- /// ```
999
- pub fn retain < F > ( & mut self , mut f : F )
1000
- where
1001
- F : FnMut ( & K , & mut V ) -> bool ,
1002
- {
1003
- // Here we only use `iter` as a temporary, preventing use-after-free
1004
- unsafe {
1005
- for item in self . table . iter ( ) {
1006
- let & mut ( ref key, ref mut value) = item. as_mut ( ) ;
1007
- if !f ( key, value) {
1008
- // Erase the element from the table first since drop might panic.
1009
- self . table . erase_no_drop ( & item) ;
1010
- item. drop ( ) ;
1011
- }
1012
- }
1013
- }
1014
- }
1015
- /// Drains elements which are false under the given predicate,
1016
- /// and returns an iterator over the removed items.
1017
- ///
1018
- /// In other words, move all pairs `(k, v)` such that `f(&k,&mut v)` returns `false` out
1019
- /// into another iterator.
1020
- ///
1021
- /// When the returned DrainedFilter is dropped, the elements that don't satisfy
1022
- /// the predicate are dropped from the table.
1023
- ///
1024
- /// # Examples
1025
- ///
1026
- /// ```
1027
- /// use hashbrown::HashMap;
1028
- ///
1029
- /// let mut map: HashMap<i32, i32> = (0..8).map(|x|(x, x*10)).collect();
1030
- /// let drained = map.drain_filter(|&k, _| k % 2 == 0);
1031
- /// assert_eq!(drained.count(), 4);
1032
- /// assert_eq!(map.len(), 4);
1033
- /// ```
1034
- pub fn drain_filter < F > ( & mut self , f : F ) -> DrainFilter < ' _ , K , V , F >
1035
- where
1036
- F : FnMut ( & K , & mut V ) -> bool ,
1037
- {
1038
- DrainFilter {
1039
- f,
1040
- iter : unsafe { self . table . iter ( ) } ,
1041
- table : & mut self . table ,
1042
- }
1043
- }
1044
1045
}
1045
1046
1046
1047
impl < K , V , S > HashMap < K , V , S > {
0 commit comments