Skip to content

Commit 13529f2

Browse files
committed
Add into_{keys,values} methods for BTreeMap
1 parent e31116a commit 13529f2

File tree

1 file changed

+156
-0
lines changed
  • library/alloc/src/collections/btree

1 file changed

+156
-0
lines changed

library/alloc/src/collections/btree/map.rs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ignore-tidy-filelength
2+
13
use core::borrow::Borrow;
24
use core::cmp::Ordering;
35
use 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 = "55214")]
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 = "55214")]
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,6 +1317,52 @@ 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 = "55214")]
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 sorted order.
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 = "55214")]
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")]
@@ -1781,6 +1853,90 @@ impl<'a, K, V> Range<'a, K, V> {
17811853
}
17821854
}
17831855

1856+
#[unstable(feature = "map_into_keys_values", issue = "55214")]
1857+
impl<K, V> Iterator for IntoKeys<K, V> {
1858+
type Item = K;
1859+
1860+
fn next(&mut self) -> Option<K> {
1861+
self.inner.next().map(|(k, _)| k)
1862+
}
1863+
1864+
fn size_hint(&self) -> (usize, Option<usize>) {
1865+
self.inner.size_hint()
1866+
}
1867+
1868+
fn last(mut self) -> Option<K> {
1869+
self.next_back()
1870+
}
1871+
1872+
fn min(mut self) -> Option<K> {
1873+
self.next()
1874+
}
1875+
1876+
fn max(mut self) -> Option<K> {
1877+
self.next_back()
1878+
}
1879+
}
1880+
1881+
#[unstable(feature = "map_into_keys_values", issue = "55214")]
1882+
impl<K, V> DoubleEndedIterator for IntoKeys<K, V> {
1883+
fn next_back(&mut self) -> Option<K> {
1884+
self.inner.next_back().map(|(k, _)| k)
1885+
}
1886+
}
1887+
1888+
#[unstable(feature = "map_into_keys_values", issue = "55214")]
1889+
impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
1890+
fn len(&self) -> usize {
1891+
self.inner.len()
1892+
}
1893+
}
1894+
1895+
#[unstable(feature = "map_into_keys_values", issue = "55214")]
1896+
impl<K, V> FusedIterator for IntoKeys<K, V> {}
1897+
1898+
#[unstable(feature = "map_into_keys_values", issue = "55214")]
1899+
impl<K, V> Iterator for IntoValues<K, V> {
1900+
type Item = V;
1901+
1902+
fn next(&mut self) -> Option<V> {
1903+
self.inner.next().map(|(_, v)| v)
1904+
}
1905+
1906+
fn size_hint(&self) -> (usize, Option<usize>) {
1907+
self.inner.size_hint()
1908+
}
1909+
1910+
fn last(mut self) -> Option<V> {
1911+
self.next_back()
1912+
}
1913+
1914+
fn min(mut self) -> Option<V> {
1915+
self.next()
1916+
}
1917+
1918+
fn max(mut self) -> Option<V> {
1919+
self.next_back()
1920+
}
1921+
}
1922+
1923+
#[unstable(feature = "map_into_keys_values", issue = "55214")]
1924+
impl<K, V> DoubleEndedIterator for IntoValues<K, V> {
1925+
fn next_back(&mut self) -> Option<V> {
1926+
self.inner.next_back().map(|(_, v)| v)
1927+
}
1928+
}
1929+
1930+
#[unstable(feature = "map_into_keys_values", issue = "55214")]
1931+
impl<K, V> ExactSizeIterator for IntoValues<K, V> {
1932+
fn len(&self) -> usize {
1933+
self.inner.len()
1934+
}
1935+
}
1936+
1937+
#[unstable(feature = "map_into_keys_values", issue = "55214")]
1938+
impl<K, V> FusedIterator for IntoValues<K, V> {}
1939+
17841940
#[stable(feature = "btree_range", since = "1.17.0")]
17851941
impl<'a, K, V> DoubleEndedIterator for Range<'a, K, V> {
17861942
fn next_back(&mut self) -> Option<(&'a K, &'a V)> {

0 commit comments

Comments
 (0)