diff options
| author | Nazım Can Altınova <canaltinova@gmail.com> | 2020-08-04 22:01:12 +0200 |
|---|---|---|
| committer | Nazım Can Altınova <canaltinova@gmail.com> | 2020-08-07 13:13:41 +0200 |
| commit | 13529f22ba574e723bea2514ba153bd0dc53bfbb (patch) | |
| tree | 32908c434381f183d111e9f977e5e2044496e879 | |
| parent | e31116af50f35ff81f83561ba607c610f42bbf4a (diff) | |
| download | rust-13529f22ba574e723bea2514ba153bd0dc53bfbb.tar.gz rust-13529f22ba574e723bea2514ba153bd0dc53bfbb.zip | |
Add `into_{keys,values}` methods for BTreeMap
| -rw-r--r-- | library/alloc/src/collections/btree/map.rs | 156 |
1 files changed, 156 insertions, 0 deletions
diff --git a/library/alloc/src/collections/btree/map.rs b/library/alloc/src/collections/btree/map.rs index 1d5fa73d228..3e7433dfbcf 100644 --- a/library/alloc/src/collections/btree/map.rs +++ b/library/alloc/src/collections/btree/map.rs @@ -1,3 +1,5 @@ +// ignore-tidy-filelength + use core::borrow::Borrow; use core::cmp::Ordering; use core::fmt::Debug; @@ -355,6 +357,30 @@ pub struct ValuesMut<'a, K: 'a, V: 'a> { inner: IterMut<'a, K, V>, } +/// An owning iterator over the keys of a `BTreeMap`. +/// +/// This `struct` is created by the [`into_keys`] method on [`BTreeMap`]. +/// See its documentation for more. +/// +/// [`into_keys`]: BTreeMap::into_keys +#[unstable(feature = "map_into_keys_values", issue = "55214")] +#[derive(Debug)] +pub struct IntoKeys<K, V> { + inner: IntoIter<K, V>, +} + +/// An owning iterator over the values of a `BTreeMap`. +/// +/// This `struct` is created by the [`into_values`] method on [`BTreeMap`]. +/// See its documentation for more. +/// +/// [`into_values`]: BTreeMap::into_values +#[unstable(feature = "map_into_keys_values", issue = "55214")] +#[derive(Debug)] +pub struct IntoValues<K, V> { + inner: IntoIter<K, V>, +} + /// An iterator over a sub-range of entries in a `BTreeMap`. /// /// This `struct` is created by the [`range`] method on [`BTreeMap`]. See its @@ -1291,6 +1317,52 @@ impl<K: Ord, V> BTreeMap<K, V> { self.length = dfs(self.root.as_ref().unwrap().as_ref()); } + + /// Creates a consuming iterator visiting all the keys, in sorted order. + /// The map cannot be used after calling this. + /// The iterator element type is `K`. + /// + /// # Examples + /// + /// ``` + /// #![feature(map_into_keys_values)] + /// use std::collections::BTreeMap; + /// + /// let mut a = BTreeMap::new(); + /// a.insert(2, "b"); + /// a.insert(1, "a"); + /// + /// let keys: Vec<i32> = a.into_keys().collect(); + /// assert_eq!(keys, [1, 2]); + /// ``` + #[inline] + #[unstable(feature = "map_into_keys_values", issue = "55214")] + pub fn into_keys(self) -> IntoKeys<K, V> { + IntoKeys { inner: self.into_iter() } + } + + /// Creates a consuming iterator visiting all the values, in sorted order. + /// The map cannot be used after calling this. + /// The iterator element type is `V`. + /// + /// # Examples + /// + /// ``` + /// #![feature(map_into_keys_values)] + /// use std::collections::BTreeMap; + /// + /// let mut a = BTreeMap::new(); + /// a.insert(1, "hello"); + /// a.insert(2, "goodbye"); + /// + /// let values: Vec<&str> = a.into_values().collect(); + /// assert_eq!(values, ["hello", "goodbye"]); + /// ``` + #[inline] + #[unstable(feature = "map_into_keys_values", issue = "55214")] + pub fn into_values(self) -> IntoValues<K, V> { + IntoValues { inner: self.into_iter() } + } } #[stable(feature = "rust1", since = "1.0.0")] @@ -1781,6 +1853,90 @@ impl<'a, K, V> Range<'a, K, V> { } } +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> Iterator for IntoKeys<K, V> { + type Item = K; + + fn next(&mut self) -> Option<K> { + self.inner.next().map(|(k, _)| k) + } + + fn size_hint(&self) -> (usize, Option<usize>) { + self.inner.size_hint() + } + + fn last(mut self) -> Option<K> { + self.next_back() + } + + fn min(mut self) -> Option<K> { + self.next() + } + + fn max(mut self) -> Option<K> { + self.next_back() + } +} + +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> DoubleEndedIterator for IntoKeys<K, V> { + fn next_back(&mut self) -> Option<K> { + self.inner.next_back().map(|(k, _)| k) + } +} + +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> ExactSizeIterator for IntoKeys<K, V> { + fn len(&self) -> usize { + self.inner.len() + } +} + +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> FusedIterator for IntoKeys<K, V> {} + +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> Iterator for IntoValues<K, V> { + type Item = V; + + fn next(&mut self) -> Option<V> { + self.inner.next().map(|(_, v)| v) + } + + fn size_hint(&self) -> (usize, Option<usize>) { + self.inner.size_hint() + } + + fn last(mut self) -> Option<V> { + self.next_back() + } + + fn min(mut self) -> Option<V> { + self.next() + } + + fn max(mut self) -> Option<V> { + self.next_back() + } +} + +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> DoubleEndedIterator for IntoValues<K, V> { + fn next_back(&mut self) -> Option<V> { + self.inner.next_back().map(|(_, v)| v) + } +} + +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> ExactSizeIterator for IntoValues<K, V> { + fn len(&self) -> usize { + self.inner.len() + } +} + +#[unstable(feature = "map_into_keys_values", issue = "55214")] +impl<K, V> FusedIterator for IntoValues<K, V> {} + #[stable(feature = "btree_range", since = "1.17.0")] impl<'a, K, V> DoubleEndedIterator for Range<'a, K, V> { fn next_back(&mut self) -> Option<(&'a K, &'a V)> { |
