-
Notifications
You must be signed in to change notification settings - Fork 310
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
More generic maps (in GroupingMap
)
#901
base: master
Are you sure you want to change the base?
More generic maps (in GroupingMap
)
#901
Conversation
Currently, we can do let my_map = HashMap::new();
let my_map = HashMap::with_capacity(100);
let my_map = FxHashMap::default(); // rustc_hash
let mut my_map = FxHashMap::default(); my_map.reserve(100);
let my_map = BTreeMap::new(); But where to give the map? (without breaking change)1. Initial idea: suffix all
For each 2. Suffix
Old That would be similar to other possible extensions like 3. A bit nested. But no
For each |
Hi @Philippe-Cholet, two thoughts:
|
@phimuemue Well, silly me...!! I don't know how it could work in a central place (cases 2 and 3 in my last comment). |
df7720d
to
5455078
Compare
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #901 +/- ##
==========================================
+ Coverage 94.38% 94.43% +0.04%
==========================================
Files 48 49 +1
Lines 6665 7187 +522
==========================================
+ Hits 6291 6787 +496
- Misses 374 400 +26 ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add tests using BTreeMap
.
EDIT: doctests added.
@SkiFire13 I see you are the author of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, silly me...!! I don't know how it could work in a central place (cases 2 and 3 in my last comment).
We would create a new structGroupingMapIn
thatGroupingMap
would alias, right?
pub type GroupingMap<I> = GroupingMapIn<I, HashMap<_, _>>;
... Simply blocked! The first one would be<I as Iterator>::Item.0
(which I can't write) and the second is still unknown since it's the method using it that will determine it...
You could add two generics (one for the key and one for the value) to GroupingMap
(which would feel a bit weird though, but I think this is fundamentally required if we want to move the "in" at the start of the chain) and then restrict the various methods to M: Map<Value = ExpectedValue>
.
Overall, this looks good to me. The duplication of the methods is a bit unfortunate though.
Could this be extended to into_group_map[_by]
too?
Note: `BTreeMap::remove<Q>` and `BTreeMap::remove<Q>` both accept a more general `Q` rather than just `Self::Key`. If ever needed, `Map<Q>` would be possible (with the same bound on `Q` than `Self::Key`: `Ord` or `Eq + Hash`).
Relax `K: Hash + Eq` bounds to soon accept `BTreeMap<K, V>`.
Note it uses `swap_remove`.
Copied from their non-suffixed variants and minimally updated.
c36f450
to
5a71e63
Compare
I totally agree, I'm gonna investigate your "add two generics" idea.
Yes, in the next PR. I intend to extend all Because I want to make all this work first, I'm gonna delay two of your ideas:
|
Understandable. As long as |
rust-itertools#901 (comment) SkiFire13 had the idea this could be optimized for vectors.
At the moment, this is only a proof of concept, but it would close #588 (and eventually solve some issues, see generic-containerGeneric vector/hasher/map
).
BTreeMap
andHashMap
(with any hasher).Maybe too much methods, or not enough. I will adjust it.
I don't think we should generalize to more than those two maps but I guess it's possible.
use_std
touse_alloc
where possible (becauseBTreeMap
only needsuse_alloc
whileHashMap
needsuse_std
) and relaxed someEq + Hash
bounds.aggregate_in
toaggregate
which takes one more argument:any empty map (or one we would
.clear()
).The last point is where I struggled the most but that's pretty much the only real possibility because there is so many possibilities of initializations:
BTreeMap::new()
,HashMap::new()
,HashMap::default()
for some hashers, andwith_capacity
shenanigans. And I think it's the most general solution (I can even imagine one singleHashMap
being used multiple times to avoid reallocations).This could extend to other areas than just
GroupingMap
, and aSet
trait could be wrote to generalizeBTreeSet
andHashSet
but it's for another PR.For now, I would like some feedback.
CI currently fails because of temporary unused imports.