diff --git a/src/lib.rs b/src/lib.rs index f2f580eb2..5c3185b62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4688,7 +4688,19 @@ pub trait Itertools: Iterator { Self: Sized, Self::Item: Eq + Hash, { - let mut counts = HashMap::new(); + self.counts_with_hasher(RandomState::new()) + } + + /// Collect the items in this iterator and return a `HashMap` the same way + /// [.counts()](crate::Itertools::counts) does, but use the specified hash builder for hashing. + #[cfg(feature = "use_std")] + fn counts_with_hasher(self, hash_builder: S) -> HashMap + where + Self: Sized, + Self::Item: Eq + Hash, + S: BuildHasher, + { + let mut counts = HashMap::with_hasher(hash_builder); self.for_each(|item| *counts.entry(item).or_default() += 1); counts } @@ -4733,7 +4745,20 @@ pub trait Itertools: Iterator { K: Eq + Hash, F: FnMut(Self::Item) -> K, { - self.map(f).counts() + self.counts_by_with_hasher(f, RandomState::new()) + } + + /// Collect the items in this iterator and return a `HashMap` the same way + /// [.counts_by()](crate::Itertools::counts_by) does, but use the specified hash builder for hashing. + #[cfg(feature = "use_std")] + fn counts_by_with_hasher(self, f: F, hash_builder: S) -> HashMap + where + Self: Sized, + K: Eq + Hash, + F: FnMut(Self::Item) -> K, + S: BuildHasher, + { + self.map(f).counts_with_hasher(hash_builder) } /// Converts an iterator of tuples into a tuple of containers.