Struct GroupingMap
struct GroupingMap<I> { ... }
GroupingMap is an intermediate struct for efficient group-and-fold operations.
It groups elements by their key and at the same time fold each group
using some aggregating operation.
No method on this struct performs temporary allocations.
Implementations
impl<I, K, V> GroupingMap<I>
fn aggregate<FO, R>(self: Self, operation: FO) -> HashMap<K, R> where FO: FnMut(Option<R>, &K, V) -> Option<R>This is the generic way to perform any operation on a
GroupingMap. It's suggested to use this method only to implement custom operations when the already provided ones are not enough.Groups elements from the
GroupingMapsource by key and appliesoperationto the elements of each group sequentially, passing the previously accumulated value, a reference to the key and the current element as arguments, and stores the results in anHashMap.The
operationfunction is invoked on each element with the following parameters:- the current value of the accumulator of the group if there is currently one;
- a reference to the key of the group this element belongs to;
- the element from the source being aggregated;
If
operationreturnsSome(element)then the accumulator is updated withelement, otherwise the previous accumulation is discarded.Return a
HashMapassociating the key of each group with the result of aggregation of that group's elements. If the aggregation of the last element of a group discards the accumulator then there won't be an entry associated to that group's key.use Itertools; let data = vec!; let lookup = data.into_iter .into_grouping_map_by .aggregate; assert_eq!; // 0 resets the accumulator so only 4 is summed assert_eq!; assert_eq!; // 10 resets the accumulator and nothing is summed afterward assert_eq!; assert_eq!; // The final keys are only 0, 1 and 2fn fold_with<FI, FO, R>(self: Self, init: FI, operation: FO) -> HashMap<K, R> where FI: FnMut(&K, &V) -> R, FO: FnMut(R, &K, V) -> RGroups elements from the
GroupingMapsource by key and appliesoperationto the elements of each group sequentially, passing the previously accumulated value, a reference to the key and the current element as arguments, and stores the results in a new map.initis called to obtain the initial value of each accumulator.operationis a function that is invoked on each element with the following parameters:- the current value of the accumulator of the group;
- a reference to the key of the group this element belongs to;
- the element from the source being accumulated.
Return a
HashMapassociating the key of each group with the result of folding that group's elements.use Itertools; let lookup = .into_grouping_map_by .fold_with; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn fold<FO, R>(self: Self, init: R, operation: FO) -> HashMap<K, R> where R: Clone, FO: FnMut(R, &K, V) -> RGroups elements from the
GroupingMapsource by key and appliesoperationto the elements of each group sequentially, passing the previously accumulated value, a reference to the key and the current element as arguments, and stores the results in a new map.initis the value from which will be cloned the initial value of each accumulator.operationis a function that is invoked on each element with the following parameters:- the current value of the accumulator of the group;
- a reference to the key of the group this element belongs to;
- the element from the source being accumulated.
Return a
HashMapassociating the key of each group with the result of folding that group's elements.use Itertools; let lookup = .into_grouping_map_by .fold; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn reduce<FO>(self: Self, operation: FO) -> HashMap<K, V> where FO: FnMut(V, &K, V) -> VGroups elements from the
GroupingMapsource by key and appliesoperationto the elements of each group sequentially, passing the previously accumulated value, a reference to the key and the current element as arguments, and stores the results in a new map.This is similar to
foldbut the initial value of the accumulator is the first element of the group.operationis a function that is invoked on each element with the following parameters:- the current value of the accumulator of the group;
- a reference to the key of the group this element belongs to;
- the element from the source being accumulated.
Return a
HashMapassociating the key of each group with the result of folding that group's elements.use Itertools; let lookup = .into_grouping_map_by .reduce; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn fold_first<FO>(self: Self, operation: FO) -> HashMap<K, V> where FO: FnMut(V, &K, V) -> VSee
.reduce().fn collect<C>(self: Self) -> HashMap<K, C> where C: Default + Extend<V>Groups elements from the
GroupingMapsource by key and collects the elements of each group in an instance ofC. The iteration order is preserved when inserting elements.Return a
HashMapassociating the key of each group with the collection containing that group's elements.use Itertools; use HashSet; let lookup = vec!.into_iter .into_grouping_map_by .; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn max(self: Self) -> HashMap<K, V> where V: OrdGroups elements from the
GroupingMapsource by key and finds the maximum of each group.If several elements are equally maximum, the last element is picked.
Returns a
HashMapassociating the key of each group with the maximum of that group's elements.use Itertools; let lookup = vec!.into_iter .into_grouping_map_by .max; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn max_by<F>(self: Self, compare: F) -> HashMap<K, V> where F: FnMut(&K, &V, &V) -> OrderingGroups elements from the
GroupingMapsource by key and finds the maximum of each group with respect to the specified comparison function.If several elements are equally maximum, the last element is picked.
Returns a
HashMapassociating the key of each group with the maximum of that group's elements.use Itertools; let lookup = vec!.into_iter .into_grouping_map_by .max_by; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn max_by_key<F, CK>(self: Self, f: F) -> HashMap<K, V> where F: FnMut(&K, &V) -> CK, CK: OrdGroups elements from the
GroupingMapsource by key and finds the element of each group that gives the maximum from the specified function.If several elements are equally maximum, the last element is picked.
Returns a
HashMapassociating the key of each group with the maximum of that group's elements.use Itertools; let lookup = vec!.into_iter .into_grouping_map_by .max_by_key; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn min(self: Self) -> HashMap<K, V> where V: OrdGroups elements from the
GroupingMapsource by key and finds the minimum of each group.If several elements are equally minimum, the first element is picked.
Returns a
HashMapassociating the key of each group with the minimum of that group's elements.use Itertools; let lookup = vec!.into_iter .into_grouping_map_by .min; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn min_by<F>(self: Self, compare: F) -> HashMap<K, V> where F: FnMut(&K, &V, &V) -> OrderingGroups elements from the
GroupingMapsource by key and finds the minimum of each group with respect to the specified comparison function.If several elements are equally minimum, the first element is picked.
Returns a
HashMapassociating the key of each group with the minimum of that group's elements.use Itertools; let lookup = vec!.into_iter .into_grouping_map_by .min_by; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn min_by_key<F, CK>(self: Self, f: F) -> HashMap<K, V> where F: FnMut(&K, &V) -> CK, CK: OrdGroups elements from the
GroupingMapsource by key and finds the element of each group that gives the minimum from the specified function.If several elements are equally minimum, the first element is picked.
Returns a
HashMapassociating the key of each group with the minimum of that group's elements.use Itertools; let lookup = vec!.into_iter .into_grouping_map_by .min_by_key; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn minmax(self: Self) -> HashMap<K, MinMaxResult<V>> where V: OrdGroups elements from the
GroupingMapsource by key and find the maximum and minimum of each group.If several elements are equally maximum, the last element is picked. If several elements are equally minimum, the first element is picked.
See
Itertools::minmaxfor the non-grouping version.Differences from the non grouping version:
- It never produces a
MinMaxResult::NoElements - It doesn't have any speedup
Returns a
HashMapassociating the key of each group with the minimum and maximum of that group's elements.use Itertools; use ; let lookup = vec!.into_iter .into_grouping_map_by .minmax; assert_eq!; assert_eq!; assert_eq!; assert_eq!;- It never produces a
fn minmax_by<F>(self: Self, compare: F) -> HashMap<K, MinMaxResult<V>> where F: FnMut(&K, &V, &V) -> OrderingGroups elements from the
GroupingMapsource by key and find the maximum and minimum of each group with respect to the specified comparison function.If several elements are equally maximum, the last element is picked. If several elements are equally minimum, the first element is picked.
It has the same differences from the non-grouping version as
minmax.Returns a
HashMapassociating the key of each group with the minimum and maximum of that group's elements.use Itertools; use ; let lookup = vec!.into_iter .into_grouping_map_by .minmax_by; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn minmax_by_key<F, CK>(self: Self, f: F) -> HashMap<K, MinMaxResult<V>> where F: FnMut(&K, &V) -> CK, CK: OrdGroups elements from the
GroupingMapsource by key and find the elements of each group that gives the minimum and maximum from the specified function.If several elements are equally maximum, the last element is picked. If several elements are equally minimum, the first element is picked.
It has the same differences from the non-grouping version as
minmax.Returns a
HashMapassociating the key of each group with the minimum and maximum of that group's elements.use Itertools; use ; let lookup = vec!.into_iter .into_grouping_map_by .minmax_by_key; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn sum(self: Self) -> HashMap<K, V> where V: Add<V, Output = V>Groups elements from the
GroupingMapsource by key and sums them.This is just a shorthand for
self.reduce(|acc, _, val| acc + val). It is more limited thanIterator::sumsince it doesn't use theSumtrait.Returns a
HashMapassociating the key of each group with the sum of that group's elements.use Itertools; let lookup = vec!.into_iter .into_grouping_map_by .sum; assert_eq!; assert_eq!; assert_eq!; assert_eq!;fn product(self: Self) -> HashMap<K, V> where V: Mul<V, Output = V>Groups elements from the
GroupingMapsource by key and multiply them.This is just a shorthand for
self.reduce(|acc, _, val| acc * val). It is more limited thanIterator::productsince it doesn't use theProducttrait.Returns a
HashMapassociating the key of each group with the product of that group's elements.use Itertools; let lookup = vec!.into_iter .into_grouping_map_by .product; assert_eq!; assert_eq!; assert_eq!; assert_eq!;
impl<I> Freeze for GroupingMap<I>
impl<I> RefUnwindSafe for GroupingMap<I>
impl<I> Send for GroupingMap<I>
impl<I> Sync for GroupingMap<I>
impl<I> Unpin for GroupingMap<I>
impl<I> UnsafeUnpin for GroupingMap<I>
impl<I> UnwindSafe for GroupingMap<I>
impl<I: $crate::clone::Clone> Clone for GroupingMap<I>
fn clone(self: &Self) -> GroupingMap<I>
impl<I: $crate::fmt::Debug> Debug for GroupingMap<I>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<T> Any for GroupingMap<I>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for GroupingMap<I>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for GroupingMap<I>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for GroupingMap<I>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for GroupingMap<I>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for GroupingMap<I>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for GroupingMap<I>
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for GroupingMap<I>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for GroupingMap<I>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>