1+ // ignore-tidy-filelength
2+
13use core:: borrow:: Borrow ;
24use core:: cmp:: Ordering ;
35use core:: fmt:: Debug ;
@@ -355,6 +357,30 @@ pub struct ValuesMut<'a, K: 'a, V: 'a> {
355357 inner : IterMut < ' a , K , V > ,
356358}
357359
360+ /// An owning iterator over the keys of a `BTreeMap`.
361+ ///
362+ /// This `struct` is created by the [`into_keys`] method on [`BTreeMap`].
363+ /// See its documentation for more.
364+ ///
365+ /// [`into_keys`]: BTreeMap::into_keys
366+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
367+ #[ derive( Debug ) ]
368+ pub struct IntoKeys < K , V > {
369+ inner : IntoIter < K , V > ,
370+ }
371+
372+ /// An owning iterator over the values of a `BTreeMap`.
373+ ///
374+ /// This `struct` is created by the [`into_values`] method on [`BTreeMap`].
375+ /// See its documentation for more.
376+ ///
377+ /// [`into_values`]: BTreeMap::into_values
378+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
379+ #[ derive( Debug ) ]
380+ pub struct IntoValues < K , V > {
381+ inner : IntoIter < K , V > ,
382+ }
383+
358384/// An iterator over a sub-range of entries in a `BTreeMap`.
359385///
360386/// This `struct` is created by the [`range`] method on [`BTreeMap`]. See its
@@ -1291,10 +1317,56 @@ impl<K: Ord, V> BTreeMap<K, V> {
12911317
12921318 self . length = dfs ( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) ) ;
12931319 }
1320+
1321+ /// Creates a consuming iterator visiting all the keys, in sorted order.
1322+ /// The map cannot be used after calling this.
1323+ /// The iterator element type is `K`.
1324+ ///
1325+ /// # Examples
1326+ ///
1327+ /// ```
1328+ /// #![feature(map_into_keys_values)]
1329+ /// use std::collections::BTreeMap;
1330+ ///
1331+ /// let mut a = BTreeMap::new();
1332+ /// a.insert(2, "b");
1333+ /// a.insert(1, "a");
1334+ ///
1335+ /// let keys: Vec<i32> = a.into_keys().collect();
1336+ /// assert_eq!(keys, [1, 2]);
1337+ /// ```
1338+ #[ inline]
1339+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1340+ pub fn into_keys ( self ) -> IntoKeys < K , V > {
1341+ IntoKeys { inner : self . into_iter ( ) }
1342+ }
1343+
1344+ /// Creates a consuming iterator visiting all the values, in order by key.
1345+ /// The map cannot be used after calling this.
1346+ /// The iterator element type is `V`.
1347+ ///
1348+ /// # Examples
1349+ ///
1350+ /// ```
1351+ /// #![feature(map_into_keys_values)]
1352+ /// use std::collections::BTreeMap;
1353+ ///
1354+ /// let mut a = BTreeMap::new();
1355+ /// a.insert(1, "hello");
1356+ /// a.insert(2, "goodbye");
1357+ ///
1358+ /// let values: Vec<&str> = a.into_values().collect();
1359+ /// assert_eq!(values, ["hello", "goodbye"]);
1360+ /// ```
1361+ #[ inline]
1362+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1363+ pub fn into_values ( self ) -> IntoValues < K , V > {
1364+ IntoValues { inner : self . into_iter ( ) }
1365+ }
12941366}
12951367
12961368#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1297- impl < ' a , K : ' a , V : ' a > IntoIterator for & ' a BTreeMap < K , V > {
1369+ impl < ' a , K , V > IntoIterator for & ' a BTreeMap < K , V > {
12981370 type Item = ( & ' a K , & ' a V ) ;
12991371 type IntoIter = Iter < ' a , K , V > ;
13001372
@@ -1363,7 +1435,7 @@ impl<K, V> Clone for Iter<'_, K, V> {
13631435}
13641436
13651437#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1366- impl < ' a , K : ' a , V : ' a > IntoIterator for & ' a mut BTreeMap < K , V > {
1438+ impl < ' a , K , V > IntoIterator for & ' a mut BTreeMap < K , V > {
13671439 type Item = ( & ' a K , & ' a mut V ) ;
13681440 type IntoIter = IterMut < ' a , K , V > ;
13691441
@@ -1697,10 +1769,9 @@ impl<'a, K: 'a, V: 'a> DrainFilterInner<'a, K, V> {
16971769 let ( k, v) = kv. kv_mut ( ) ;
16981770 if pred ( k, v) {
16991771 * self . length -= 1 ;
1700- let RemoveResult { old_kv , pos, emptied_internal_root } = kv. remove_kv_tracking ( ) ;
1772+ let ( kv , pos) = kv. remove_kv_tracking ( |_| self . emptied_internal_root = true ) ;
17011773 self . cur_leaf_edge = Some ( pos) ;
1702- self . emptied_internal_root |= emptied_internal_root;
1703- return Some ( old_kv) ;
1774+ return Some ( kv) ;
17041775 }
17051776 self . cur_leaf_edge = Some ( kv. next_leaf_edge ( ) ) ;
17061777 }
@@ -1781,6 +1852,82 @@ impl<'a, K, V> Range<'a, K, V> {
17811852 }
17821853}
17831854
1855+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1856+ impl < K , V > Iterator for IntoKeys < K , V > {
1857+ type Item = K ;
1858+
1859+ fn next ( & mut self ) -> Option < K > {
1860+ self . inner . next ( ) . map ( |( k, _) | k)
1861+ }
1862+
1863+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1864+ self . inner . size_hint ( )
1865+ }
1866+
1867+ fn last ( mut self ) -> Option < K > {
1868+ self . next_back ( )
1869+ }
1870+
1871+ fn min ( mut self ) -> Option < K > {
1872+ self . next ( )
1873+ }
1874+
1875+ fn max ( mut self ) -> Option < K > {
1876+ self . next_back ( )
1877+ }
1878+ }
1879+
1880+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1881+ impl < K , V > DoubleEndedIterator for IntoKeys < K , V > {
1882+ fn next_back ( & mut self ) -> Option < K > {
1883+ self . inner . next_back ( ) . map ( |( k, _) | k)
1884+ }
1885+ }
1886+
1887+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1888+ impl < K , V > ExactSizeIterator for IntoKeys < K , V > {
1889+ fn len ( & self ) -> usize {
1890+ self . inner . len ( )
1891+ }
1892+ }
1893+
1894+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1895+ impl < K , V > FusedIterator for IntoKeys < K , V > { }
1896+
1897+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1898+ impl < K , V > Iterator for IntoValues < K , V > {
1899+ type Item = V ;
1900+
1901+ fn next ( & mut self ) -> Option < V > {
1902+ self . inner . next ( ) . map ( |( _, v) | v)
1903+ }
1904+
1905+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
1906+ self . inner . size_hint ( )
1907+ }
1908+
1909+ fn last ( mut self ) -> Option < V > {
1910+ self . next_back ( )
1911+ }
1912+ }
1913+
1914+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1915+ impl < K , V > DoubleEndedIterator for IntoValues < K , V > {
1916+ fn next_back ( & mut self ) -> Option < V > {
1917+ self . inner . next_back ( ) . map ( |( _, v) | v)
1918+ }
1919+ }
1920+
1921+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1922+ impl < K , V > ExactSizeIterator for IntoValues < K , V > {
1923+ fn len ( & self ) -> usize {
1924+ self . inner . len ( )
1925+ }
1926+ }
1927+
1928+ #[ unstable( feature = "map_into_keys_values" , issue = "75294" ) ]
1929+ impl < K , V > FusedIterator for IntoValues < K , V > { }
1930+
17841931#[ stable( feature = "btree_range" , since = "1.17.0" ) ]
17851932impl < ' a , K , V > DoubleEndedIterator for Range < ' a , K , V > {
17861933 fn next_back ( & mut self ) -> Option < ( & ' a K , & ' a V ) > {
@@ -2645,35 +2792,28 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
26452792 fn remove_kv ( self ) -> ( K , V ) {
26462793 * self . length -= 1 ;
26472794
2648- let RemoveResult { old_kv, pos, emptied_internal_root } = self . handle . remove_kv_tracking ( ) ;
2649- let root = pos. into_node ( ) . into_root_mut ( ) ;
2650- if emptied_internal_root {
2651- root. pop_internal_level ( ) ;
2652- }
2795+ let ( old_kv, _) =
2796+ self . handle . remove_kv_tracking ( |root| root. into_root_mut ( ) . pop_internal_level ( ) ) ;
26532797 old_kv
26542798 }
26552799}
26562800
2657- struct RemoveResult < ' a , K , V > {
2658- // Key and value removed.
2659- old_kv : ( K , V ) ,
2660- // Unique location at the leaf level that the removed KV lopgically collapsed into.
2661- pos : Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > ,
2662- // Whether the remove left behind and empty internal root node, that should be removed
2663- // using `pop_internal_level`.
2664- emptied_internal_root : bool ,
2665- }
2666-
26672801impl < ' a , K : ' a , V : ' a > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > , marker:: KV > {
26682802 /// Removes a key/value-pair from the tree, and returns that pair, as well as
26692803 /// the leaf edge corresponding to that former pair. It's possible this leaves
26702804 /// an empty internal root node, which the caller should subsequently pop from
26712805 /// the map holding the tree. The caller should also decrement the map's length.
2672- fn remove_kv_tracking ( self ) -> RemoveResult < ' a , K , V > {
2673- let ( mut pos, old_key, old_val, was_internal) = match self . force ( ) {
2806+ fn remove_kv_tracking < F > (
2807+ self ,
2808+ handle_emptied_internal_root : F ,
2809+ ) -> ( ( K , V ) , Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > )
2810+ where
2811+ F : FnOnce ( NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > ) ,
2812+ {
2813+ let ( old_kv, mut pos, was_internal) = match self . force ( ) {
26742814 Leaf ( leaf) => {
2675- let ( hole , old_key , old_val ) = leaf. remove ( ) ;
2676- ( hole , old_key , old_val , false )
2815+ let ( old_kv , pos ) = leaf. remove ( ) ;
2816+ ( old_kv , pos , false )
26772817 }
26782818 Internal ( mut internal) => {
26792819 // Replace the location freed in the internal node with the next KV,
@@ -2688,17 +2828,16 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
26882828 let to_remove = internal. left_edge ( ) . descend ( ) . last_leaf_edge ( ) . left_kv ( ) . ok ( ) ;
26892829 let to_remove = unsafe { unwrap_unchecked ( to_remove) } ;
26902830
2691- let ( hole , key , val ) = to_remove. remove ( ) ;
2831+ let ( kv , pos ) = to_remove. remove ( ) ;
26922832
2693- let old_key = unsafe { mem:: replace ( & mut * key_loc, key ) } ;
2694- let old_val = unsafe { mem:: replace ( & mut * val_loc, val ) } ;
2833+ let old_key = unsafe { mem:: replace ( & mut * key_loc, kv . 0 ) } ;
2834+ let old_val = unsafe { mem:: replace ( & mut * val_loc, kv . 1 ) } ;
26952835
2696- ( hole , old_key, old_val, true )
2836+ ( ( old_key, old_val) , pos , true )
26972837 }
26982838 } ;
26992839
27002840 // Handle underflow
2701- let mut emptied_internal_root = false ;
27022841 let mut cur_node = unsafe { ptr:: read ( & pos) . into_node ( ) . forget_type ( ) } ;
27032842 let mut at_leaf = true ;
27042843 while cur_node. len ( ) < node:: MIN_LEN {
@@ -2719,8 +2858,10 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
27192858
27202859 let parent = edge. into_node ( ) ;
27212860 if parent. len ( ) == 0 {
2722- // This empty parent must be the root, and should be popped off the tree.
2723- emptied_internal_root = true ;
2861+ // The parent that was just emptied must be the root,
2862+ // because nodes on a lower level would not have been
2863+ // left underfull. It has to be popped off the tree soon.
2864+ handle_emptied_internal_root ( parent) ;
27242865 break ;
27252866 } else {
27262867 cur_node = parent. forget_type ( ) ;
@@ -2747,7 +2888,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
27472888 pos = unsafe { unwrap_unchecked ( pos. next_kv ( ) . ok ( ) ) . next_leaf_edge ( ) } ;
27482889 }
27492890
2750- RemoveResult { old_kv : ( old_key , old_val ) , pos, emptied_internal_root }
2891+ ( old_kv , pos)
27512892 }
27522893}
27532894
0 commit comments