@@ -1151,7 +1151,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
1151
1151
K : Ord ,
1152
1152
F : FnMut ( & K , & mut V ) -> bool ,
1153
1153
{
1154
- self . extract_if ( |k, v| !f ( k, v) ) . for_each ( drop) ;
1154
+ self . extract_if ( .. , |k, v| !f ( k, v) ) . for_each ( drop) ;
1155
1155
}
1156
1156
1157
1157
/// Moves all elements from `other` into `self`, leaving `other` empty.
@@ -1429,27 +1429,30 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
1429
1429
/// assert_eq!(odds.keys().copied().collect::<Vec<_>>(), [1, 3, 5, 7]);
1430
1430
/// ```
1431
1431
#[ unstable( feature = "btree_extract_if" , issue = "70530" ) ]
1432
- pub fn extract_if < F > ( & mut self , pred : F ) -> ExtractIf < ' _ , K , V , F , A >
1432
+ pub fn extract_if < F , R > ( & mut self , range : R , pred : F ) -> ExtractIf < ' _ , K , V , R , F , A >
1433
1433
where
1434
1434
K : Ord ,
1435
+ R : RangeBounds < K > ,
1435
1436
F : FnMut ( & K , & mut V ) -> bool ,
1436
1437
{
1437
- let ( inner, alloc) = self . extract_if_inner ( ) ;
1438
+ let ( inner, alloc) = self . extract_if_inner ( range ) ;
1438
1439
ExtractIf { pred, inner, alloc }
1439
1440
}
1440
1441
1441
- pub ( super ) fn extract_if_inner ( & mut self ) -> ( ExtractIfInner < ' _ , K , V > , A )
1442
+ pub ( super ) fn extract_if_inner < R > ( & mut self , range : R ) -> ( ExtractIfInner < ' _ , K , V , R > , A )
1442
1443
where
1443
1444
K : Ord ,
1445
+ R : RangeBounds < K > ,
1444
1446
{
1445
1447
if let Some ( root) = self . root . as_mut ( ) {
1446
1448
let ( root, dormant_root) = DormantMutRef :: new ( root) ;
1447
- let front = root. borrow_mut ( ) . first_leaf_edge ( ) ;
1449
+ let first = root. borrow_mut ( ) . lower_bound ( SearchBound :: from_range ( range . start_bound ( ) ) ) ;
1448
1450
(
1449
1451
ExtractIfInner {
1450
1452
length : & mut self . length ,
1451
1453
dormant_root : Some ( dormant_root) ,
1452
- cur_leaf_edge : Some ( front) ,
1454
+ cur_leaf_edge : Some ( first) ,
1455
+ range,
1453
1456
} ,
1454
1457
( * self . alloc ) . clone ( ) ,
1455
1458
)
@@ -1459,6 +1462,7 @@ impl<K, V, A: Allocator + Clone> BTreeMap<K, V, A> {
1459
1462
length : & mut self . length ,
1460
1463
dormant_root : None ,
1461
1464
cur_leaf_edge : None ,
1465
+ range,
1462
1466
} ,
1463
1467
( * self . alloc ) . clone ( ) ,
1464
1468
)
@@ -1917,18 +1921,19 @@ pub struct ExtractIf<
1917
1921
' a ,
1918
1922
K ,
1919
1923
V ,
1924
+ R ,
1920
1925
F ,
1921
1926
#[ unstable( feature = "allocator_api" , issue = "32838" ) ] A : Allocator + Clone = Global ,
1922
1927
> {
1923
1928
pred : F ,
1924
- inner : ExtractIfInner < ' a , K , V > ,
1929
+ inner : ExtractIfInner < ' a , K , V , R > ,
1925
1930
/// The BTreeMap will outlive this IntoIter so we don't care about drop order for `alloc`.
1926
1931
alloc : A ,
1927
1932
}
1928
1933
1929
1934
/// Most of the implementation of ExtractIf are generic over the type
1930
1935
/// of the predicate, thus also serving for BTreeSet::ExtractIf.
1931
- pub ( super ) struct ExtractIfInner < ' a , K , V > {
1936
+ pub ( super ) struct ExtractIfInner < ' a , K , V , R > {
1932
1937
/// Reference to the length field in the borrowed map, updated live.
1933
1938
length : & ' a mut usize ,
1934
1939
/// Buried reference to the root field in the borrowed map.
@@ -1938,10 +1943,13 @@ pub(super) struct ExtractIfInner<'a, K, V> {
1938
1943
/// Empty if the map has no root, if iteration went beyond the last leaf edge,
1939
1944
/// or if a panic occurred in the predicate.
1940
1945
cur_leaf_edge : Option < Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > > ,
1946
+ /// Range over which iteration was requested. We don't need the left side, but we
1947
+ /// can't extract the right side without requiring K: Clone.
1948
+ range : R ,
1941
1949
}
1942
1950
1943
1951
#[ unstable( feature = "btree_extract_if" , issue = "70530" ) ]
1944
- impl < K , V , F , A > fmt:: Debug for ExtractIf < ' _ , K , V , F , A >
1952
+ impl < K , V , R , F , A > fmt:: Debug for ExtractIf < ' _ , K , V , R , F , A >
1945
1953
where
1946
1954
K : fmt:: Debug ,
1947
1955
V : fmt:: Debug ,
@@ -1953,8 +1961,10 @@ where
1953
1961
}
1954
1962
1955
1963
#[ unstable( feature = "btree_extract_if" , issue = "70530" ) ]
1956
- impl < K , V , F , A : Allocator + Clone > Iterator for ExtractIf < ' _ , K , V , F , A >
1964
+ impl < K , V , R , F , A : Allocator + Clone > Iterator for ExtractIf < ' _ , K , V , R , F , A >
1957
1965
where
1966
+ K : PartialOrd ,
1967
+ R : RangeBounds < K > ,
1958
1968
F : FnMut ( & K , & mut V ) -> bool ,
1959
1969
{
1960
1970
type Item = ( K , V ) ;
@@ -1968,7 +1978,7 @@ where
1968
1978
}
1969
1979
}
1970
1980
1971
- impl < ' a , K , V > ExtractIfInner < ' a , K , V > {
1981
+ impl < ' a , K , V , R > ExtractIfInner < ' a , K , V , R > {
1972
1982
/// Allow Debug implementations to predict the next element.
1973
1983
pub ( super ) fn peek ( & self ) -> Option < ( & K , & V ) > {
1974
1984
let edge = self . cur_leaf_edge . as_ref ( ) ?;
@@ -1978,10 +1988,22 @@ impl<'a, K, V> ExtractIfInner<'a, K, V> {
1978
1988
/// Implementation of a typical `ExtractIf::next` method, given the predicate.
1979
1989
pub ( super ) fn next < F , A : Allocator + Clone > ( & mut self , pred : & mut F , alloc : A ) -> Option < ( K , V ) >
1980
1990
where
1991
+ K : PartialOrd ,
1992
+ R : RangeBounds < K > ,
1981
1993
F : FnMut ( & K , & mut V ) -> bool ,
1982
1994
{
1983
1995
while let Ok ( mut kv) = self . cur_leaf_edge . take ( ) ?. next_kv ( ) {
1984
1996
let ( k, v) = kv. kv_mut ( ) ;
1997
+
1998
+ // On creation, we navigated directly to the left bound, so we need only check the
1999
+ // right bound here to decide whether to stop.
2000
+ match self . range . end_bound ( ) {
2001
+ Bound :: Included ( ref end) if ( * k) . le ( end) => ( ) ,
2002
+ Bound :: Excluded ( ref end) if ( * k) . lt ( end) => ( ) ,
2003
+ Bound :: Unbounded => ( ) ,
2004
+ _ => return None ,
2005
+ }
2006
+
1985
2007
if pred ( k, v) {
1986
2008
* self . length -= 1 ;
1987
2009
let ( kv, pos) = kv. remove_kv_tracking (
@@ -2013,7 +2035,13 @@ impl<'a, K, V> ExtractIfInner<'a, K, V> {
2013
2035
}
2014
2036
2015
2037
#[ unstable( feature = "btree_extract_if" , issue = "70530" ) ]
2016
- impl < K , V , F > FusedIterator for ExtractIf < ' _ , K , V , F > where F : FnMut ( & K , & mut V ) -> bool { }
2038
+ impl < K , V , R , F > FusedIterator for ExtractIf < ' _ , K , V , R , F >
2039
+ where
2040
+ K : PartialOrd ,
2041
+ R : RangeBounds < K > ,
2042
+ F : FnMut ( & K , & mut V ) -> bool ,
2043
+ {
2044
+ }
2017
2045
2018
2046
#[ stable( feature = "btree_range" , since = "1.17.0" ) ]
2019
2047
impl < ' a , K , V > Iterator for Range < ' a , K , V > {
0 commit comments