Skip to content

Commit a6e0188

Browse files
authored
Merge pull request #195 from taiki-e/into-keys-values
Add IndexMap::into_{keys,values}
2 parents e2ef299 + 49d4308 commit a6e0188

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ impl<K, V> Bucket<K, V> {
162162
fn key(self) -> K {
163163
self.key
164164
}
165+
fn value(self) -> V {
166+
self.value
167+
}
165168
fn key_value(self) -> (K, V) {
166169
(self.key, self.value)
167170
}

src/map.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,13 @@ impl<K, V, S> IndexMap<K, V, S> {
230230
}
231231
}
232232

233+
/// Return an owning iterator over the keys of the map, in their order
234+
pub fn into_keys(self) -> IntoKeys<K, V> {
235+
IntoKeys {
236+
iter: self.into_entries().into_iter(),
237+
}
238+
}
239+
233240
/// Return an iterator over the values of the map, in their order
234241
pub fn values(&self) -> Values<'_, K, V> {
235242
Values {
@@ -245,6 +252,13 @@ impl<K, V, S> IndexMap<K, V, S> {
245252
}
246253
}
247254

255+
/// Return an owning iterator over the values of the map, in their order
256+
pub fn into_values(self) -> IntoValues<K, V> {
257+
IntoValues {
258+
iter: self.into_entries().into_iter(),
259+
}
260+
}
261+
248262
/// Remove all key-value pairs in the map, while preserving its capacity.
249263
///
250264
/// Computes in **O(n)** time.
@@ -825,6 +839,42 @@ impl<K: fmt::Debug, V> fmt::Debug for Keys<'_, K, V> {
825839
}
826840
}
827841

842+
/// An owning iterator over the keys of a `IndexMap`.
843+
///
844+
/// This `struct` is created by the [`into_keys`] method on [`IndexMap`].
845+
/// See its documentation for more.
846+
///
847+
/// [`IndexMap`]: struct.IndexMap.html
848+
/// [`into_keys`]: struct.IndexMap.html#method.into_keys
849+
pub struct IntoKeys<K, V> {
850+
iter: vec::IntoIter<Bucket<K, V>>,
851+
}
852+
853+
impl<K, V> Iterator for IntoKeys<K, V> {
854+
type Item = K;
855+
856+
iterator_methods!(Bucket::key);
857+
}
858+
859+
impl<K, V> DoubleEndedIterator for IntoKeys<K, V> {
860+
fn next_back(&mut self) -> Option<Self::Item> {
861+
self.iter.next_back().map(Bucket::key)
862+
}
863+
}
864+
865+
impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
866+
fn len(&self) -> usize {
867+
self.iter.len()
868+
}
869+
}
870+
871+
impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> {
872+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
873+
let iter = self.iter.as_slice().iter().map(Bucket::key_ref);
874+
f.debug_list().entries(iter).finish()
875+
}
876+
}
877+
828878
/// An iterator over the values of a `IndexMap`.
829879
///
830880
/// This `struct` is created by the [`values`] method on [`IndexMap`]. See its
@@ -898,6 +948,42 @@ impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
898948
}
899949
}
900950

951+
/// An owning iterator over the values of a `IndexMap`.
952+
///
953+
/// This `struct` is created by the [`into_values`] method on [`IndexMap`].
954+
/// See its documentation for more.
955+
///
956+
/// [`IndexMap`]: struct.IndexMap.html
957+
/// [`into_values`]: struct.IndexMap.html#method.into_values
958+
pub struct IntoValues<K, V> {
959+
iter: vec::IntoIter<Bucket<K, V>>,
960+
}
961+
962+
impl<K, V> Iterator for IntoValues<K, V> {
963+
type Item = V;
964+
965+
iterator_methods!(Bucket::value);
966+
}
967+
968+
impl<K, V> DoubleEndedIterator for IntoValues<K, V> {
969+
fn next_back(&mut self) -> Option<Self::Item> {
970+
self.iter.next_back().map(Bucket::value)
971+
}
972+
}
973+
974+
impl<K, V> ExactSizeIterator for IntoValues<K, V> {
975+
fn len(&self) -> usize {
976+
self.iter.len()
977+
}
978+
}
979+
980+
impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> {
981+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
982+
let iter = self.iter.as_slice().iter().map(Bucket::value_ref);
983+
f.debug_list().entries(iter).finish()
984+
}
985+
}
986+
901987
/// An iterator over the entries of a `IndexMap`.
902988
///
903989
/// This `struct` is created by the [`iter`] method on [`IndexMap`]. See its
@@ -1683,6 +1769,17 @@ mod tests {
16831769
assert!(keys.contains(&3));
16841770
}
16851771

1772+
#[test]
1773+
fn into_keys() {
1774+
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
1775+
let map: IndexMap<_, _> = vec.into_iter().collect();
1776+
let keys: Vec<i32> = map.into_keys().collect();
1777+
assert_eq!(keys.len(), 3);
1778+
assert!(keys.contains(&1));
1779+
assert!(keys.contains(&2));
1780+
assert!(keys.contains(&3));
1781+
}
1782+
16861783
#[test]
16871784
fn values() {
16881785
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
@@ -1707,4 +1804,15 @@ mod tests {
17071804
assert!(values.contains(&4));
17081805
assert!(values.contains(&6));
17091806
}
1807+
1808+
#[test]
1809+
fn into_values() {
1810+
let vec = vec![(1, 'a'), (2, 'b'), (3, 'c')];
1811+
let map: IndexMap<_, _> = vec.into_iter().collect();
1812+
let values: Vec<char> = map.into_values().collect();
1813+
assert_eq!(values.len(), 3);
1814+
assert!(values.contains(&'a'));
1815+
assert!(values.contains(&'b'));
1816+
assert!(values.contains(&'c'));
1817+
}
17101818
}

0 commit comments

Comments
 (0)