Skip to content

Add a .unique() method to Iterator. #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub use linspace::{linspace, Linspace};
pub use sources::{
RepeatCall,
};
pub use unique::{Unique};
pub use zip_longest::{ZipLongest, EitherOrBoth};
pub use ziptuple::{Zip};
#[cfg(feature = "unstable")]
Expand All @@ -84,6 +85,7 @@ pub mod size_hint;
mod stride;
mod tee;
mod times;
pub mod unique;
mod zip_longest;
mod ziptuple;
#[cfg(feature = "unstable")]
Expand Down
51 changes: 51 additions & 0 deletions src/unique.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//! Adds the method *.unique()* to **Iterator**.

use std::collections::HashSet;
use std::hash::Hash;

/// Struct for storing which elements we've already seen.
pub struct UniqueState<I>
where I: Iterator
{
seen: HashSet<I::Item>,
underlying: I,
}

/// An iterator that discards duplicates.
///
/// ```
/// use itertools::Unique;
///
/// let before = vec!["a", "b", "c", "b"];
/// let after: Vec<_> = before.into_iter().unique().collect();
///
/// assert_eq!(after, vec!["a", "b", "c"]);
/// ```
pub trait Unique: Iterator {
/// Create a new **Unique**.
fn unique(self) -> UniqueState<Self>
where Self::Item: Hash + Eq + Clone,
Self: Sized,
{
UniqueState { seen: HashSet::new(), underlying: self }
}
}

impl<I> Unique for I where I: Iterator {}

impl<I> Iterator for UniqueState<I>
where I: Iterator,
I::Item: Hash + Eq + Clone,
{
type Item = I::Item;

fn next(&mut self) -> Option<Self::Item> {
while let Some(x) = self.underlying.next() {
if !self.seen.contains(&x) {
self.seen.insert(x.clone());
return Some(x)
}
}
None
}
}
9 changes: 9 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ extern crate itertools as it;
use it::Itertools;
use it::Interleave;
use it::Zip;
use it::Unique;

#[test]
fn unique() {
let start: Vec<i32> = vec![1, 2, 2, 3, 2, 4];
let without_duplicates: Vec<i32> = start.into_iter().unique().collect();

assert_eq!(without_duplicates, vec![1, 2, 3, 4]);
}

#[test]
fn product2() {
Expand Down