Skip to content

Commit 55d1532

Browse files
author
root
committed
Unique: Edit docs & remove clone bound, fix size_hint
1 parent 3c1cd9a commit 55d1532

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

src/adaptors.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -975,7 +975,7 @@ impl<I> Iterator for Combinations<I> where I: Iterator + Clone, I::Item: Clone{
975975
}
976976
}
977977

978-
/// An iterator adapter to filter non-unique elements.
978+
/// An iterator adapter to filter out duplicate elements.
979979
///
980980
/// See [*.unique()*](trait.Itertools.html#method.unique) for more information.
981981
#[derive(Clone)]
@@ -985,7 +985,10 @@ pub struct UniqueBy<I: Iterator, V, F> {
985985
f: F,
986986
}
987987

988-
impl<I: Iterator, V, F> UniqueBy<I, V, F> where V: Clone + Eq + Hash, F: FnMut(&I::Item) -> V {
988+
impl<I: Iterator, V, F> UniqueBy<I, V, F>
989+
where V: Eq + Hash,
990+
F: FnMut(&I::Item) -> V
991+
{
989992
/// Create a new **UniqueBy** iterator.
990993
pub fn new(iter: I, f: F) -> UniqueBy<I, V, F> {
991994
UniqueBy {
@@ -998,7 +1001,7 @@ impl<I: Iterator, V, F> UniqueBy<I, V, F> where V: Clone + Eq + Hash, F: FnMut(&
9981001

9991002
impl<I, V, F> Iterator for UniqueBy<I, V, F> where
10001003
I: Iterator,
1001-
V: Clone + Eq + Hash,
1004+
V: Eq + Hash,
10021005
F: FnMut(&I::Item) -> V
10031006
{
10041007
type Item = I::Item;
@@ -1019,10 +1022,11 @@ impl<I, V, F> Iterator for UniqueBy<I, V, F> where
10191022

10201023
#[inline]
10211024
fn size_hint(&self) -> (usize, Option<usize>) {
1022-
(0, self.iter.size_hint().1)
1025+
let (low, hi) = self.iter.size_hint();
1026+
((low > 0 && self.used.is_empty()) as usize, hi)
10231027
}
10241028
}
10251029

1026-
/// An iterator adapter to filter non-unique elements.
1030+
/// An iterator adapter to filter out duplicate elements.
10271031
pub type Unique<I> where I: Iterator =
10281032
UniqueBy<I, I::Item, fn(&I::Item) -> I::Item>;

src/lib.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -628,9 +628,11 @@ pub trait Itertools : Iterator {
628628
Coalesce::new(self, eq)
629629
}
630630

631-
/// Filter non-unique elements from the iterator.
631+
/// Return an iterator adaptor that filters out elements that have
632+
/// already been produced once during the iteration. Duplicates
633+
/// are detected using hash and equality.
632634
///
633-
/// Copies of visited elements are stored in a hash set in the
635+
/// Clones of visited elements are stored in a hash set in the
634636
/// iterator.
635637
///
636638
/// ```
@@ -647,11 +649,12 @@ pub trait Itertools : Iterator {
647649
self.unique_by(Clone::clone)
648650
}
649651

650-
/// Filter non-unique elements from the iterator.
652+
/// Return an iterator adaptor that filters out elements that have
653+
/// already been produced once during the iteration.
651654
///
652-
/// Elemens are considered the same if supplied function returns
653-
/// equal values for them. Those values are stored in a hash set in
654-
/// the iterator.
655+
/// Duplicates are detected by comparing the key they map to
656+
/// with the keying function **f** by hash and equality.
657+
/// The keys are stored in a hash set in the iterator.
655658
///
656659
/// ```
657660
/// use itertools::Itertools;
@@ -662,7 +665,7 @@ pub trait Itertools : Iterator {
662665
/// ```
663666
fn unique_by<V, F>(self, f: F) -> UniqueBy<Self, V, F> where
664667
Self: Sized,
665-
V: Clone + Eq + Hash,
668+
V: Eq + Hash,
666669
F: FnMut(&Self::Item) -> V
667670
{
668671
UniqueBy::new(self, f)

tests/quick.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,4 +467,9 @@ fn size_pad_tail2(it: Iter<i8>, pad: u8) -> bool {
467467
exact_size(it.pad_using(pad as usize, |_| 0))
468468
}
469469

470+
#[quickcheck]
471+
fn size_unique(it: Iter<i8>) -> bool {
472+
correct_size_hint(it.unique())
473+
}
474+
470475
}

0 commit comments

Comments
 (0)