-
Notifications
You must be signed in to change notification settings - Fork 322
More generic maps (in GroupingMap
)
#901
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
Draft
Philippe-Cholet
wants to merge
15
commits into
rust-itertools:master
Choose a base branch
from
Philippe-Cholet:generic-map
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ca064c8
Generic container: new `Map` private trait
Philippe-Cholet f6da436
`grouping_map` module behind `use_alloc` feature
Philippe-Cholet 79ea22b
New `GroupingMap::aggregate_in`
Philippe-Cholet fc2fcb7
New `GroupingMap::fold_with_in`
Philippe-Cholet cb72ea1
New `GroupingMap::fold_in`
Philippe-Cholet 742a678
New `GroupingMap::reduce_in`
Philippe-Cholet f8c7d19
New `GroupingMap::collect_in`
Philippe-Cholet fbf63e8
New `GroupingMap::max[_by[_key]]_in`
Philippe-Cholet 99a0a64
New `GroupingMap::min[_by[_key]]_in`
Philippe-Cholet 41bdf4b
New `GroupingMap::minmax[_by[_key]]_in`
Philippe-Cholet 2cfc868
New `GroupingMap::sum_in`
Philippe-Cholet 4b5d32d
New `GroupingMap::product_in`
Philippe-Cholet 56213c1
Implementation of `Map` for simple `Vec<(_, _)>`
Philippe-Cholet bf8e4aa
`GroupingMap`: add doctests using `BTreeMap`
Philippe-Cholet 5a71e63
Update documentation of `GroupingMap`
Philippe-Cholet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//! **Private** generalizations of containers: | ||
//! - `Map`: `BTreeMap` and `HashMap` (any hasher). | ||
|
||
#![cfg(feature = "use_alloc")] | ||
|
||
use alloc::collections::BTreeMap; | ||
use alloc::vec::Vec; | ||
#[cfg(feature = "use_std")] | ||
use core::hash::{BuildHasher, Hash}; | ||
#[cfg(feature = "use_std")] | ||
use std::collections::HashMap; | ||
|
||
pub trait Map { | ||
type Key; | ||
type Value; | ||
fn insert(&mut self, key: Self::Key, value: Self::Value) -> Option<Self::Value>; | ||
fn remove(&mut self, key: &Self::Key) -> Option<Self::Value>; | ||
fn entry_or_default(&mut self, key: Self::Key) -> &mut Self::Value | ||
where | ||
Self::Value: Default; | ||
} | ||
|
||
impl<K, V> Map for BTreeMap<K, V> | ||
where | ||
K: Ord, | ||
{ | ||
type Key = K; | ||
type Value = V; | ||
fn insert(&mut self, key: K, value: V) -> Option<V> { | ||
self.insert(key, value) | ||
} | ||
fn remove(&mut self, key: &K) -> Option<V> { | ||
self.remove(key) | ||
} | ||
fn entry_or_default(&mut self, key: K) -> &mut V | ||
where | ||
V: Default, | ||
{ | ||
self.entry(key).or_default() | ||
} | ||
} | ||
|
||
#[cfg(feature = "use_std")] | ||
impl<K, V, S> Map for HashMap<K, V, S> | ||
where | ||
K: Eq + Hash, | ||
S: BuildHasher, | ||
{ | ||
type Key = K; | ||
type Value = V; | ||
fn insert(&mut self, key: K, value: V) -> Option<V> { | ||
self.insert(key, value) | ||
} | ||
fn remove(&mut self, key: &K) -> Option<V> { | ||
self.remove(key) | ||
} | ||
fn entry_or_default(&mut self, key: K) -> &mut V | ||
where | ||
V: Default, | ||
{ | ||
self.entry(key).or_default() | ||
} | ||
} | ||
|
||
impl<K, V> Map for Vec<(K, V)> | ||
where | ||
K: Eq, | ||
{ | ||
type Key = K; | ||
type Value = V; | ||
fn insert(&mut self, key: K, value: V) -> Option<V> { | ||
match self.iter_mut().find(|(k, _)| k == &key) { | ||
Some((_, v)) => Some(core::mem::replace(v, value)), | ||
None => { | ||
self.push((key, value)); | ||
None | ||
} | ||
} | ||
} | ||
fn remove(&mut self, key: &K) -> Option<V> { | ||
let index = self.iter().position(|(k, _)| k == key)?; | ||
Some(self.swap_remove(index).1) | ||
} | ||
fn entry_or_default(&mut self, key: K) -> &mut V | ||
where | ||
V: Default, | ||
{ | ||
let index = self.iter().position(|(k, _)| k == &key).unwrap_or_else(|| { | ||
self.push((key, V::default())); | ||
self.len() - 1 | ||
}); | ||
&mut self[index].1 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.