@@ -1151,7 +1151,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
11511151 K : Ord ,
11521152 F : FnMut ( & K , & mut V ) -> bool ,
11531153 {
1154- self . extract_if ( |k, v| !f ( k, v) ) . for_each ( drop) ;
1154+ self . extract_if ( .. , |k, v| !f ( k, v) ) . for_each ( drop) ;
11551155 }
11561156
11571157 /// Moves all elements from `other` into `self`, leaving `other` empty.
@@ -1427,27 +1427,30 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
14271427 /// assert_eq!(odds.keys().copied().collect::<Vec<_>>(), [1, 3, 5, 7]);
14281428 /// ```
14291429 #[ unstable( feature = "btree_extract_if" , issue = "70530" ) ]
1430- pub fn extract_if < F > ( & mut self , pred : F ) -> ExtractIf < ' _ , K , V , F , A >
1430+ pub fn extract_if < F , R > ( & mut self , range : R , pred : F ) -> ExtractIf < ' _ , K , V , R , F , A >
14311431 where
14321432 K : Ord ,
1433+ R : RangeBounds < K > ,
14331434 F : FnMut ( & K , & mut V ) -> bool ,
14341435 {
1435- let ( inner, alloc) = self . extract_if_inner ( ) ;
1436+ let ( inner, alloc) = self . extract_if_inner ( range ) ;
14361437 ExtractIf { pred, inner, alloc }
14371438 }
14381439
1439- pub ( super ) fn extract_if_inner ( & mut self ) -> ( ExtractIfInner < ' _ , K , V > , A )
1440+ pub ( super ) fn extract_if_inner < R > ( & mut self , range : R ) -> ( ExtractIfInner < ' _ , K , V , R > , A )
14401441 where
14411442 K : Ord ,
1443+ R : RangeBounds < K > ,
14421444 {
14431445 if let Some ( root) = self . root . as_mut ( ) {
14441446 let ( root, dormant_root) = DormantMutRef :: new ( root) ;
1445- let front = root. borrow_mut ( ) . first_leaf_edge ( ) ;
1447+ let first = root. borrow_mut ( ) . lower_bound ( SearchBound :: from_range ( range . start_bound ( ) ) ) ;
14461448 (
14471449 ExtractIfInner {
14481450 length : & mut self . length ,
14491451 dormant_root : Some ( dormant_root) ,
1450- cur_leaf_edge : Some ( front) ,
1452+ cur_leaf_edge : Some ( first) ,
1453+ range,
14511454 } ,
14521455 ( * self . alloc ) . clone ( ) ,
14531456 )
@@ -1457,6 +1460,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
14571460 length : & mut self . length ,
14581461 dormant_root : None ,
14591462 cur_leaf_edge : None ,
1463+ range,
14601464 } ,
14611465 ( * self . alloc ) . clone ( ) ,
14621466 )
@@ -1915,18 +1919,19 @@ pub struct ExtractIf<
19151919 ' a ,
19161920 K ,
19171921 V ,
1922+ R ,
19181923 F ,
19191924 #[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator + Clone = Global ,
19201925> {
19211926 pred : F ,
1922- inner : ExtractIfInner < ' a , K , V > ,
1927+ inner : ExtractIfInner < ' a , K , V , R > ,
19231928 /// The BTreeMap will outlive this IntoIter so we don't care about drop order for `alloc`.
19241929 alloc : A ,
19251930}
19261931
19271932/// Most of the implementation of ExtractIf are generic over the type
19281933/// of the predicate, thus also serving for BTreeSet::ExtractIf.
1929- pub ( super ) struct ExtractIfInner < ' a , K , V > {
1934+ pub ( super ) struct ExtractIfInner < ' a , K , V , R > {
19301935 /// Reference to the length field in the borrowed map, updated live.
19311936 length : & ' a mut usize ,
19321937 /// Buried reference to the root field in the borrowed map.
@@ -1936,10 +1941,13 @@ pub(super) struct ExtractIfInner<'a, K, V> {
19361941 /// Empty if the map has no root, if iteration went beyond the last leaf edge,
19371942 /// or if a panic occurred in the predicate.
19381943 cur_leaf_edge : Option < Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > > ,
1944+ /// Range over which iteration was requested. We don't need the left side, but we
1945+ /// can't extract the right side without requiring K: Clone.
1946+ range : R ,
19391947}
19401948
19411949#[ unstable( feature = "btree_extract_if" , issue = "70530" ) ]
1942- impl < K , V , F , A > fmt:: Debug for ExtractIf < ' _ , K , V , F , A >
1950+ impl < K , V , R , F , A > fmt:: Debug for ExtractIf < ' _ , K , V , R , F , A >
19431951where
19441952 K : fmt:: Debug ,
19451953 V : fmt:: Debug ,
@@ -1951,8 +1959,10 @@ where
19511959}
19521960
19531961#[ unstable( feature = "btree_extract_if" , issue = "70530" ) ]
1954- impl < K , V , F , A : Allocator + Clone > Iterator for ExtractIf < ' _ , K , V , F , A >
1962+ impl < K , V , R , F , A : Allocator + Clone > Iterator for ExtractIf < ' _ , K , V , R , F , A >
19551963where
1964+ K : PartialOrd ,
1965+ R : RangeBounds < K > ,
19561966 F : FnMut ( & K , & mut V ) -> bool ,
19571967{
19581968 type Item = ( K , V ) ;
@@ -1966,7 +1976,7 @@ where
19661976 }
19671977}
19681978
1969- impl < ' a , K , V > ExtractIfInner < ' a , K , V > {
1979+ impl < ' a , K , V , R > ExtractIfInner < ' a , K , V , R > {
19701980 /// Allow Debug implementations to predict the next element.
19711981 pub ( super ) fn peek ( & self ) -> Option < ( & K , & V ) > {
19721982 let edge = self . cur_leaf_edge . as_ref ( ) ?;
@@ -1976,10 +1986,22 @@ impl<'a, K, V> ExtractIfInner<'a, K, V> {
19761986 /// Implementation of a typical `ExtractIf::next` method, given the predicate.
19771987 pub ( super ) fn next < F , A : Allocator + Clone > ( & mut self , pred : & mut F , alloc : A ) -> Option < ( K , V ) >
19781988 where
1989+ K : PartialOrd ,
1990+ R : RangeBounds < K > ,
19791991 F : FnMut ( & K , & mut V ) -> bool ,
19801992 {
19811993 while let Ok ( mut kv) = self . cur_leaf_edge . take ( ) ?. next_kv ( ) {
19821994 let ( k, v) = kv. kv_mut ( ) ;
1995+
1996+ // On creation, we navigated directly to the left bound, so we need only check the
1997+ // right bound here to decide whether to stop.
1998+ match self . range . end_bound ( ) {
1999+ Bound :: Included ( ref end) if ( * k) . le ( end) => ( ) ,
2000+ Bound :: Excluded ( ref end) if ( * k) . lt ( end) => ( ) ,
2001+ Bound :: Unbounded => ( ) ,
2002+ _ => return None ,
2003+ }
2004+
19832005 if pred ( k, v) {
19842006 * self . length -= 1 ;
19852007 let ( kv, pos) = kv. remove_kv_tracking (
@@ -2011,7 +2033,13 @@ impl<'a, K, V> ExtractIfInner<'a, K, V> {
20112033}
20122034
20132035#[ unstable( feature = "btree_extract_if" , issue = "70530" ) ]
2014- impl < K , V , F > FusedIterator for ExtractIf < ' _ , K , V , F > where F : FnMut ( & K , & mut V ) -> bool { }
2036+ impl < K , V , R , F > FusedIterator for ExtractIf < ' _ , K , V , R , F >
2037+ where
2038+ K : PartialOrd ,
2039+ R : RangeBounds < K > ,
2040+ F : FnMut ( & K , & mut V ) -> bool ,
2041+ {
2042+ }
20152043
20162044#[ stable( feature = "btree_range" , since = "1.17.0" ) ]
20172045impl < ' a , K , V > Iterator for Range < ' a , K , V > {
0 commit comments