Skip to content

Commit

Permalink
Add counts_[by_]with_hasher()
Browse files Browse the repository at this point in the history
  • Loading branch information
424ever committed Dec 23, 2024
1 parent ef62da0 commit f7345cf
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(self, hash_builder: S) -> HashMap<Self::Item, usize, S>
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
}
Expand Down Expand Up @@ -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<K, F, S>(self, f: F, hash_builder: S) -> HashMap<K, usize, S>
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.
Expand Down

0 comments on commit f7345cf

Please sign in to comment.