Skip to content
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

type MapForGrouping<...> = MapSpecialCase<...> #876

Merged
merged 3 commits into from
Feb 13, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/adaptors/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::marker::PhantomData;
#[derive(Clone, Debug)]
#[must_use = "iterator adaptors are lazy and do nothing unless consumed"]
pub struct MapSpecialCase<I, F> {
iter: I,
f: F,
pub(crate) iter: I,
pub(crate) f: F,
}

pub trait MapSpecialCaseFn<T> {
Expand Down
2 changes: 1 addition & 1 deletion src/adaptors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! except according to those terms.

mod coalesce;
mod map;
pub(crate) mod map;
mod multi_product;
pub use self::coalesce::*;
#[allow(deprecated)]
Expand Down
46 changes: 20 additions & 26 deletions src/grouping_map.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
#![cfg(feature = "use_std")]

use crate::MinMaxResult;
use crate::{
adaptors::map::{MapSpecialCase, MapSpecialCaseFn},
MinMaxResult,
};
use std::cmp::Ordering;
use std::collections::HashMap;
use std::fmt;
use std::hash::Hash;
use std::iter::Iterator;
use std::ops::{Add, Mul};

/// A wrapper to allow for an easy [`into_grouping_map_by`](crate::Itertools::into_grouping_map_by)
pub type MapForGrouping<I, F> = MapSpecialCase<I, GroupingMapFn<F>>;

#[derive(Clone)]
pub struct MapForGrouping<I, F>(I, F);
pub struct GroupingMapFn<F>(F);
Philippe-Cholet marked this conversation as resolved.
Show resolved Hide resolved

impl<I: fmt::Debug, F> fmt::Debug for MapForGrouping<I, F> {
debug_fmt_fields!(MapForGrouping, 0);
impl<F> std::fmt::Debug for GroupingMapFn<F> {
debug_fmt_fields!(GroupingMapFn,);
}

impl<I, F> MapForGrouping<I, F> {
pub(crate) fn new(iter: I, key_mapper: F) -> Self {
Self(iter, key_mapper)
impl<V, K, F: FnMut(&V) -> K> MapSpecialCaseFn<V> for GroupingMapFn<F> {
type Out = (K, V);
fn call(&mut self, v: V) -> Self::Out {
((self.0)(&v), v)
}
}

#[allow(clippy::missing_trait_methods)]
impl<K, V, I, F> Iterator for MapForGrouping<I, F>
where
I: Iterator<Item = V>,
K: Hash + Eq,
F: FnMut(&V) -> K,
{
type Item = (K, V);
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|val| ((self.1)(&val), val))
}

fn fold<B, G>(self, init: B, f: G) -> B
where
G: FnMut(B, Self::Item) -> B,
{
let mut key_mapper = self.1;
self.0.map(|val| (key_mapper(&val), val)).fold(init, f)
pub(crate) fn new_map_for_grouping<K, I: Iterator, F: FnMut(&I::Item) -> K>(
iter: I,
key_mapper: F,
) -> MapForGrouping<I, F> {
MapSpecialCase {
iter,
f: GroupingMapFn(key_mapper),
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3194,7 +3194,7 @@ pub trait Itertools: Iterator {
K: Hash + Eq,
F: FnMut(&V) -> K,
{
grouping_map::new(grouping_map::MapForGrouping::new(self, key_mapper))
grouping_map::new(grouping_map::new_map_for_grouping(self, key_mapper))
}

/// Return all minimum elements of an iterator.
Expand Down