Skip to content

Commit

Permalink
Implement DoubleEndedIterator for Union, Intersection, Difference, an…
Browse files Browse the repository at this point in the history
…d SymmetricDifference (#104)
  • Loading branch information
james7132 committed Mar 16, 2024
1 parent ddf0dd8 commit 1be930c
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,17 @@ impl<'a> Iterator for Difference<'a> {
}
}

impl<'a> DoubleEndedIterator for Difference<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
for nxt in self.iter.by_ref().rev() {
if !self.other.contains(nxt) {
return Some(nxt);
}
}
None
}
}

// Difference will continue to return None once it first returns None.
impl<'a> FusedIterator for Difference<'a> {}

Expand All @@ -756,6 +767,12 @@ impl<'a> Iterator for SymmetricDifference<'a> {
}
}

impl<'a> DoubleEndedIterator for SymmetricDifference<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}

// SymmetricDifference will continue to return None once it first returns None.
impl<'a> FusedIterator for SymmetricDifference<'a> {}

Expand Down Expand Up @@ -787,6 +804,17 @@ impl<'a> Iterator for Intersection<'a> {
}
}

impl<'a> DoubleEndedIterator for Intersection<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
for nxt in self.iter.by_ref().rev() {
if self.other.contains(nxt) {
return Some(nxt);
}
}
None
}
}

// Intersection will continue to return None once it first returns None.
impl<'a> FusedIterator for Intersection<'a> {}

Expand All @@ -811,6 +839,12 @@ impl<'a> Iterator for Union<'a> {
}
}

impl<'a> DoubleEndedIterator for Union<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}

// Union will continue to return None once it first returns None.
impl<'a> FusedIterator for Union<'a> {}

Expand Down

0 comments on commit 1be930c

Please sign in to comment.