1
+ // ignore-tidy-filelength
2
+
1
3
use core:: borrow:: Borrow ;
2
4
use core:: cmp:: Ordering ;
3
5
use core:: fmt:: Debug ;
@@ -355,6 +357,30 @@ pub struct ValuesMut<'a, K: 'a, V: 'a> {
355
357
inner : IterMut < ' a , K , V > ,
356
358
}
357
359
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
+
358
384
/// An iterator over a sub-range of entries in a `BTreeMap`.
359
385
///
360
386
/// This `struct` is created by the [`range`] method on [`BTreeMap`]. See its
@@ -1291,10 +1317,56 @@ impl<K: Ord, V> BTreeMap<K, V> {
1291
1317
1292
1318
self . length = dfs ( self . root . as_ref ( ) . unwrap ( ) . as_ref ( ) ) ;
1293
1319
}
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
+ }
1294
1366
}
1295
1367
1296
1368
#[ 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 > {
1298
1370
type Item = ( & ' a K , & ' a V ) ;
1299
1371
type IntoIter = Iter < ' a , K , V > ;
1300
1372
@@ -1363,7 +1435,7 @@ impl<K, V> Clone for Iter<'_, K, V> {
1363
1435
}
1364
1436
1365
1437
#[ 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 > {
1367
1439
type Item = ( & ' a K , & ' a mut V ) ;
1368
1440
type IntoIter = IterMut < ' a , K , V > ;
1369
1441
@@ -1697,10 +1769,9 @@ impl<'a, K: 'a, V: 'a> DrainFilterInner<'a, K, V> {
1697
1769
let ( k, v) = kv. kv_mut ( ) ;
1698
1770
if pred ( k, v) {
1699
1771
* 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 ) ;
1701
1773
self . cur_leaf_edge = Some ( pos) ;
1702
- self . emptied_internal_root |= emptied_internal_root;
1703
- return Some ( old_kv) ;
1774
+ return Some ( kv) ;
1704
1775
}
1705
1776
self . cur_leaf_edge = Some ( kv. next_leaf_edge ( ) ) ;
1706
1777
}
@@ -1781,6 +1852,82 @@ impl<'a, K, V> Range<'a, K, V> {
1781
1852
}
1782
1853
}
1783
1854
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
+
1784
1931
#[ stable( feature = "btree_range" , since = "1.17.0" ) ]
1785
1932
impl < ' a , K , V > DoubleEndedIterator for Range < ' a , K , V > {
1786
1933
fn next_back ( & mut self ) -> Option < ( & ' a K , & ' a V ) > {
@@ -2645,35 +2792,28 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
2645
2792
fn remove_kv ( self ) -> ( K , V ) {
2646
2793
* self . length -= 1 ;
2647
2794
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 ( ) ) ;
2653
2797
old_kv
2654
2798
}
2655
2799
}
2656
2800
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
-
2667
2801
impl < ' a , K : ' a , V : ' a > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > , marker:: KV > {
2668
2802
/// Removes a key/value-pair from the tree, and returns that pair, as well as
2669
2803
/// the leaf edge corresponding to that former pair. It's possible this leaves
2670
2804
/// an empty internal root node, which the caller should subsequently pop from
2671
2805
/// 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 ( ) {
2674
2814
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 )
2677
2817
}
2678
2818
Internal ( mut internal) => {
2679
2819
// 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
2688
2828
let to_remove = internal. left_edge ( ) . descend ( ) . last_leaf_edge ( ) . left_kv ( ) . ok ( ) ;
2689
2829
let to_remove = unsafe { unwrap_unchecked ( to_remove) } ;
2690
2830
2691
- let ( hole , key , val ) = to_remove. remove ( ) ;
2831
+ let ( kv , pos ) = to_remove. remove ( ) ;
2692
2832
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 ) } ;
2695
2835
2696
- ( hole , old_key, old_val, true )
2836
+ ( ( old_key, old_val) , pos , true )
2697
2837
}
2698
2838
} ;
2699
2839
2700
2840
// Handle underflow
2701
- let mut emptied_internal_root = false ;
2702
2841
let mut cur_node = unsafe { ptr:: read ( & pos) . into_node ( ) . forget_type ( ) } ;
2703
2842
let mut at_leaf = true ;
2704
2843
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
2719
2858
2720
2859
let parent = edge. into_node ( ) ;
2721
2860
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) ;
2724
2865
break ;
2725
2866
} else {
2726
2867
cur_node = parent. forget_type ( ) ;
@@ -2747,7 +2888,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInter
2747
2888
pos = unsafe { unwrap_unchecked ( pos. next_kv ( ) . ok ( ) ) . next_leaf_edge ( ) } ;
2748
2889
}
2749
2890
2750
- RemoveResult { old_kv : ( old_key , old_val ) , pos, emptied_internal_root }
2891
+ ( old_kv , pos)
2751
2892
}
2752
2893
}
2753
2894
0 commit comments