Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add .duplicate() and .duplicate_by(..) operations
Uses a HashMap to detect duplicates in an iterator and emits them only once. Items are never cloned. Signed-off-by: Petros Angelatos <petrosagg@gmail.com>
- Loading branch information
Showing
3 changed files
with
238 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
use std::collections::HashMap; | ||
use std::hash::Hash; | ||
use std::fmt; | ||
|
||
/// An iterator adapter to filter out duplicate elements. | ||
/// | ||
/// See [`.duplicate_by()`](../trait.Itertools.html#method.duplicate) for more information. | ||
#[derive(Clone)] | ||
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"] | ||
pub struct DuplicateBy<I: Iterator, V, F> { | ||
iter: I, | ||
// Use a hashmap for the entry API | ||
used: HashMap<V, bool>, | ||
f: F, | ||
} | ||
|
||
impl<I, V, F> fmt::Debug for DuplicateBy<I, V, F> | ||
where I: Iterator + fmt::Debug, | ||
V: fmt::Debug + Hash + Eq, | ||
{ | ||
debug_fmt_fields!(DuplicateBy, iter, used); | ||
} | ||
|
||
/// Create a new `DuplicateBy` iterator. | ||
pub fn duplicate_by<I, V, F>(iter: I, f: F) -> DuplicateBy<I, V, F> | ||
where V: Eq + Hash, | ||
F: FnMut(&I::Item) -> V, | ||
I: Iterator, | ||
{ | ||
DuplicateBy { | ||
iter, | ||
used: HashMap::new(), | ||
f, | ||
} | ||
} | ||
|
||
impl<I, V, F> Iterator for DuplicateBy<I, V, F> | ||
where I: Iterator, | ||
V: Eq + Hash, | ||
F: FnMut(&I::Item) -> V | ||
{ | ||
type Item = I::Item; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
while let Some(v) = self.iter.next() { | ||
let key = (self.f)(&v); | ||
match self.used.get_mut(&key) { | ||
None => { self.used.insert(key, false); }, | ||
Some(true) => (), | ||
Some(produced) => { | ||
*produced = true; | ||
return Some(v); | ||
}, | ||
} | ||
} | ||
None | ||
} | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let (low, hi) = self.iter.size_hint(); | ||
((low > 0 && self.used.is_empty()) as usize, hi) | ||
} | ||
} | ||
|
||
impl<I, V, F> DoubleEndedIterator for DuplicateBy<I, V, F> | ||
where I: DoubleEndedIterator, | ||
V: Eq + Hash, | ||
F: FnMut(&I::Item) -> V | ||
{ | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
while let Some(v) = self.iter.next_back() { | ||
let key = (self.f)(&v); | ||
match self.used.get_mut(&key) { | ||
None => { self.used.insert(key, false); }, | ||
Some(true) => (), | ||
Some(produced) => { | ||
*produced = true; | ||
return Some(v); | ||
}, | ||
} | ||
} | ||
None | ||
} | ||
} | ||
|
||
impl<I> Iterator for Duplicate<I> | ||
where I: Iterator, | ||
I::Item: Eq + Hash | ||
{ | ||
type Item = I::Item; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
while let Some(v) = self.iter.iter.next() { | ||
match self.iter.used.get_mut(&v) { | ||
None => { self.iter.used.insert(v, false); }, | ||
Some(true) => (), | ||
Some(produced) => { | ||
*produced = true; | ||
return Some(v); | ||
}, | ||
} | ||
} | ||
None | ||
} | ||
|
||
#[inline] | ||
fn size_hint(&self) -> (usize, Option<usize>) { | ||
let (low, hi) = self.iter.iter.size_hint(); | ||
((low > 0 && self.iter.used.is_empty()) as usize, hi) | ||
} | ||
} | ||
|
||
impl<I> DoubleEndedIterator for Duplicate<I> | ||
where I: DoubleEndedIterator, | ||
I::Item: Eq + Hash | ||
{ | ||
fn next_back(&mut self) -> Option<Self::Item> { | ||
while let Some(v) = self.iter.iter.next_back() { | ||
match self.iter.used.get_mut(&v) { | ||
None => { self.iter.used.insert(v, false); }, | ||
Some(true) => (), | ||
Some(produced) => { | ||
*produced = true; | ||
return Some(v); | ||
}, | ||
} | ||
} | ||
None | ||
} | ||
} | ||
|
||
/// An iterator adapter to filter out duplicate elements. | ||
/// | ||
/// See [`.duplicate()`](../trait.Itertools.html#method.duplicate) for more information. | ||
#[derive(Clone)] | ||
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"] | ||
pub struct Duplicate<I: Iterator> { | ||
iter: DuplicateBy<I, I::Item, ()>, | ||
} | ||
|
||
impl<I> fmt::Debug for Duplicate<I> | ||
where I: Iterator + fmt::Debug, | ||
I::Item: Hash + Eq + fmt::Debug, | ||
{ | ||
debug_fmt_fields!(Duplicate, iter); | ||
} | ||
|
||
pub fn duplicate<I>(iter: I) -> Duplicate<I> | ||
where I: Iterator, | ||
I::Item: Eq + Hash, | ||
{ | ||
Duplicate { | ||
iter: DuplicateBy { | ||
iter, | ||
used: HashMap::new(), | ||
f: (), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters