Skip to content

Commit 37066d6

Browse files
committed
Add all_unique_with_hasher()
1 parent a1ca58d commit 37066d6

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

src/lib.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -2333,7 +2333,32 @@ pub trait Itertools: Iterator {
23332333
Self: Sized,
23342334
Self::Item: Eq + Hash,
23352335
{
2336-
let mut used = HashSet::new();
2336+
self.all_unique_with_hasher(RandomState::new())
2337+
}
2338+
2339+
/// Check whether all elements are unique (non equal). The specified hash builder is used for
2340+
/// hashing the elements. See [.all_unique](crate::Itertools::all_unique).
2341+
///
2342+
/// ```
2343+
/// use std::hash::RandomState;
2344+
/// use itertools::Itertools;
2345+
///
2346+
/// let data = vec![1, 2, 3, 4, 1, 5];
2347+
/// assert!(!data.iter().all_unique_with_hasher(RandomState::new()));
2348+
/// assert!(data[0..4].iter().all_unique_with_hasher(RandomState::new()));
2349+
/// assert!(data[1..6].iter().all_unique_with_hasher(RandomState::new()));
2350+
///
2351+
/// let data : Option<usize> = None;
2352+
/// assert!(data.into_iter().all_unique_with_hasher(RandomState::new()));
2353+
/// ```
2354+
#[cfg(feature = "use_std")]
2355+
fn all_unique_with_hasher<S>(&mut self, hash_builder: S) -> bool
2356+
where
2357+
Self: Sized,
2358+
Self::Item: Eq + Hash,
2359+
S: BuildHasher,
2360+
{
2361+
let mut used = HashSet::with_hasher(hash_builder);
23372362
self.all(move |elt| used.insert(elt))
23382363
}
23392364

0 commit comments

Comments
 (0)