Skip to content

Commit

Permalink
Merge pull request #196 from cuviper/iter
Browse files Browse the repository at this point in the history
Iterator polish
  • Loading branch information
cuviper authored Aug 17, 2021
2 parents a6e0188 + e594967 commit 35b36eb
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 32 deletions.
4 changes: 4 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,9 @@ macro_rules! double_ended_iterator_methods {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map($map_elt)
}

fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
self.iter.nth_back(n).map($map_elt)
}
};
}
69 changes: 44 additions & 25 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::vec::{self, Vec};
use ::core::cmp::Ordering;
use ::core::fmt;
use ::core::hash::{BuildHasher, Hash, Hasher};
use ::core::iter::FromIterator;
use ::core::iter::{FromIterator, FusedIterator};
use ::core::ops::{Index, IndexMut, RangeBounds};
use ::core::slice::{Iter as SliceIter, IterMut as SliceIterMut};

Expand Down Expand Up @@ -813,9 +813,7 @@ impl<'a, K, V> Iterator for Keys<'a, K, V> {
}

impl<K, V> DoubleEndedIterator for Keys<'_, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(Bucket::key_ref)
}
double_ended_iterator_methods!(Bucket::key_ref);
}

impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
Expand All @@ -824,6 +822,8 @@ impl<K, V> ExactSizeIterator for Keys<'_, K, V> {
}
}

impl<K, V> FusedIterator for Keys<'_, K, V> {}

// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
impl<K, V> Clone for Keys<'_, K, V> {
fn clone(&self) -> Self {
Expand Down Expand Up @@ -857,9 +857,7 @@ impl<K, V> Iterator for IntoKeys<K, V> {
}

impl<K, V> DoubleEndedIterator for IntoKeys<K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(Bucket::key)
}
double_ended_iterator_methods!(Bucket::key);
}

impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
Expand All @@ -868,6 +866,8 @@ impl<K, V> ExactSizeIterator for IntoKeys<K, V> {
}
}

impl<K, V> FusedIterator for IntoKeys<K, V> {}

impl<K: fmt::Debug, V> fmt::Debug for IntoKeys<K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let iter = self.iter.as_slice().iter().map(Bucket::key_ref);
Expand All @@ -893,9 +893,7 @@ impl<'a, K, V> Iterator for Values<'a, K, V> {
}

impl<K, V> DoubleEndedIterator for Values<'_, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(Bucket::value_ref)
}
double_ended_iterator_methods!(Bucket::value_ref);
}

impl<K, V> ExactSizeIterator for Values<'_, K, V> {
Expand All @@ -904,6 +902,8 @@ impl<K, V> ExactSizeIterator for Values<'_, K, V> {
}
}

impl<K, V> FusedIterator for Values<'_, K, V> {}

// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
impl<K, V> Clone for Values<'_, K, V> {
fn clone(&self) -> Self {
Expand Down Expand Up @@ -937,9 +937,7 @@ impl<'a, K, V> Iterator for ValuesMut<'a, K, V> {
}

impl<K, V> DoubleEndedIterator for ValuesMut<'_, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(Bucket::value_mut)
}
double_ended_iterator_methods!(Bucket::value_mut);
}

impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
Expand All @@ -948,6 +946,10 @@ impl<K, V> ExactSizeIterator for ValuesMut<'_, K, V> {
}
}

impl<K, V> FusedIterator for ValuesMut<'_, K, V> {}

// TODO: `impl Debug for ValuesMut` once we have MSRV 1.53 for `slice::IterMut::as_slice`

/// An owning iterator over the values of a `IndexMap`.
///
/// This `struct` is created by the [`into_values`] method on [`IndexMap`].
Expand All @@ -966,9 +968,7 @@ impl<K, V> Iterator for IntoValues<K, V> {
}

impl<K, V> DoubleEndedIterator for IntoValues<K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(Bucket::value)
}
double_ended_iterator_methods!(Bucket::value);
}

impl<K, V> ExactSizeIterator for IntoValues<K, V> {
Expand All @@ -977,6 +977,8 @@ impl<K, V> ExactSizeIterator for IntoValues<K, V> {
}
}

impl<K, V> FusedIterator for IntoValues<K, V> {}

impl<K, V: fmt::Debug> fmt::Debug for IntoValues<K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let iter = self.iter.as_slice().iter().map(Bucket::value_ref);
Expand All @@ -1002,9 +1004,7 @@ impl<'a, K, V> Iterator for Iter<'a, K, V> {
}

impl<K, V> DoubleEndedIterator for Iter<'_, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(Bucket::refs)
}
double_ended_iterator_methods!(Bucket::refs);
}

impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
Expand All @@ -1013,6 +1013,8 @@ impl<K, V> ExactSizeIterator for Iter<'_, K, V> {
}
}

impl<K, V> FusedIterator for Iter<'_, K, V> {}

// FIXME(#26925) Remove in favor of `#[derive(Clone)]`
impl<K, V> Clone for Iter<'_, K, V> {
fn clone(&self) -> Self {
Expand Down Expand Up @@ -1046,9 +1048,7 @@ impl<'a, K, V> Iterator for IterMut<'a, K, V> {
}

impl<K, V> DoubleEndedIterator for IterMut<'_, K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(Bucket::ref_mut)
}
double_ended_iterator_methods!(Bucket::ref_mut);
}

impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
Expand All @@ -1057,6 +1057,10 @@ impl<K, V> ExactSizeIterator for IterMut<'_, K, V> {
}
}

impl<K, V> FusedIterator for IterMut<'_, K, V> {}

// TODO: `impl Debug for IterMut` once we have MSRV 1.53 for `slice::IterMut::as_slice`

/// An owning iterator over the entries of a `IndexMap`.
///
/// This `struct` is created by the [`into_iter`] method on [`IndexMap`]
Expand All @@ -1075,9 +1079,7 @@ impl<K, V> Iterator for IntoIter<K, V> {
}

impl<K, V> DoubleEndedIterator for IntoIter<K, V> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(Bucket::key_value)
}
double_ended_iterator_methods!(Bucket::key_value);
}

impl<K, V> ExactSizeIterator for IntoIter<K, V> {
Expand All @@ -1086,6 +1088,8 @@ impl<K, V> ExactSizeIterator for IntoIter<K, V> {
}
}

impl<K, V> FusedIterator for IntoIter<K, V> {}

impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for IntoIter<K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let iter = self.iter.as_slice().iter().map(Bucket::refs);
Expand Down Expand Up @@ -1114,6 +1118,21 @@ impl<K, V> DoubleEndedIterator for Drain<'_, K, V> {
double_ended_iterator_methods!(Bucket::key_value);
}

impl<K, V> ExactSizeIterator for Drain<'_, K, V> {
fn len(&self) -> usize {
self.iter.len()
}
}

impl<K, V> FusedIterator for Drain<'_, K, V> {}

impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for Drain<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let iter = self.iter.as_slice().iter().map(Bucket::refs);
f.debug_list().entries(iter).finish()
}
}

impl<'a, K, V, S> IntoIterator for &'a IndexMap<K, V, S> {
type Item = (&'a K, &'a V);
type IntoIter = Iter<'a, K, V>;
Expand Down
14 changes: 14 additions & 0 deletions src/rayon/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ pub struct ParIterMut<'a, K, V> {
entries: &'a mut [Bucket<K, V>],
}

impl<K: fmt::Debug, V: fmt::Debug> fmt::Debug for ParIterMut<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let iter = self.entries.iter().map(Bucket::refs);
f.debug_list().entries(iter).finish()
}
}

impl<'a, K: Sync + Send, V: Send> ParallelIterator for ParIterMut<'a, K, V> {
type Item = (&'a K, &'a mut V);

Expand Down Expand Up @@ -339,6 +346,13 @@ pub struct ParValuesMut<'a, K, V> {
entries: &'a mut [Bucket<K, V>],
}

impl<K, V: fmt::Debug> fmt::Debug for ParValuesMut<'_, K, V> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let iter = self.entries.iter().map(Bucket::value_ref);
f.debug_list().entries(iter).finish()
}
}

impl<'a, K: Send, V: Send> ParallelIterator for ParValuesMut<'a, K, V> {
type Item = &'a mut V;

Expand Down
72 changes: 65 additions & 7 deletions src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::vec::{self, Vec};
use core::cmp::Ordering;
use core::fmt;
use core::hash::{BuildHasher, Hash};
use core::iter::{Chain, FromIterator};
use core::iter::{Chain, FromIterator, FusedIterator};
use core::ops::{BitAnd, BitOr, BitXor, Index, RangeBounds, Sub};
use core::slice;

Expand Down Expand Up @@ -708,9 +708,7 @@ impl<T> Iterator for IntoIter<T> {
}

impl<T> DoubleEndedIterator for IntoIter<T> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(Bucket::key)
}
double_ended_iterator_methods!(Bucket::key);
}

impl<T> ExactSizeIterator for IntoIter<T> {
Expand All @@ -719,6 +717,8 @@ impl<T> ExactSizeIterator for IntoIter<T> {
}
}

impl<T> FusedIterator for IntoIter<T> {}

impl<T: fmt::Debug> fmt::Debug for IntoIter<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let iter = self.iter.as_slice().iter().map(Bucket::key_ref);
Expand All @@ -744,9 +744,7 @@ impl<'a, T> Iterator for Iter<'a, T> {
}

impl<T> DoubleEndedIterator for Iter<'_, T> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back().map(Bucket::key_ref)
}
double_ended_iterator_methods!(Bucket::key_ref);
}

impl<T> ExactSizeIterator for Iter<'_, T> {
Expand All @@ -755,6 +753,8 @@ impl<T> ExactSizeIterator for Iter<'_, T> {
}
}

impl<T> FusedIterator for Iter<'_, T> {}

impl<T> Clone for Iter<'_, T> {
fn clone(&self) -> Self {
Iter {
Expand Down Expand Up @@ -790,6 +790,21 @@ impl<T> DoubleEndedIterator for Drain<'_, T> {
double_ended_iterator_methods!(Bucket::key);
}

impl<T> ExactSizeIterator for Drain<'_, T> {
fn len(&self) -> usize {
self.iter.len()
}
}

impl<T> FusedIterator for Drain<'_, T> {}

impl<T: fmt::Debug> fmt::Debug for Drain<'_, T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let iter = self.iter.as_slice().iter().map(Bucket::key_ref);
f.debug_list().entries(iter).finish()
}
}

impl<'a, T, S> IntoIterator for &'a IndexSet<T, S> {
type Item = &'a T;
type IntoIter = Iter<'a, T>;
Expand Down Expand Up @@ -957,6 +972,13 @@ where
}
}

impl<T, S> FusedIterator for Difference<'_, T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
}

impl<T, S> Clone for Difference<'_, T, S> {
fn clone(&self) -> Self {
Difference {
Expand Down Expand Up @@ -1024,6 +1046,13 @@ where
}
}

impl<T, S> FusedIterator for Intersection<'_, T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
}

impl<T, S> Clone for Intersection<'_, T, S> {
fn clone(&self) -> Self {
Intersection {
Expand Down Expand Up @@ -1087,6 +1116,21 @@ where
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}

fn rfold<B, F>(self, init: B, f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
self.iter.rfold(init, f)
}
}

impl<T, S1, S2> FusedIterator for SymmetricDifference<'_, T, S1, S2>
where
T: Eq + Hash,
S1: BuildHasher,
S2: BuildHasher,
{
}

impl<T, S1, S2> Clone for SymmetricDifference<'_, T, S1, S2> {
Expand Down Expand Up @@ -1150,6 +1194,20 @@ where
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}

fn rfold<B, F>(self, init: B, f: F) -> B
where
F: FnMut(B, Self::Item) -> B,
{
self.iter.rfold(init, f)
}
}

impl<T, S> FusedIterator for Union<'_, T, S>
where
T: Eq + Hash,
S: BuildHasher,
{
}

impl<T, S> Clone for Union<'_, T, S> {
Expand Down

0 comments on commit 35b36eb

Please sign in to comment.