Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Add map and try_map methods to BoundedBTreeMap #11869

Merged
merged 7 commits into from
Aug 11, 2022

Conversation

benluelo
Copy link
Contributor

See #11866 for relevant discussion.

I debated on whether to have the signature of the functions passed to map/try_map pass K through, but I realized that this could cause the number of items in the map to decrease due to collisions (i.e. b.map(|(_, v)| (1, v))). While this wouldn't violate any of the guarantees of the datatype, it is odd and most likely unexpected behavior; use .retain().map() if that behavior is desired.

In implementing this, I was wondering if BoundedVec would benefit from this functionality as well. Would a trait make sense, or should it just be a method on the struct directly? I'm not sure if this operation would make sense on a BoundedBTreeSet for the same reason I kept the key as a reference in BoundedBTreeMap::{map/try_map}.

@cla-bot-2021
Copy link

cla-bot-2021 bot commented Jul 20, 2022

User @benluelo, please sign the CLA here.

@ggwpez ggwpez added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit labels Jul 20, 2022
Copy link
Member

@ggwpez ggwpez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would a trait make sense, or should it just be a method on the struct directly?

What do you think @KiChjang ? If we want to implement it for other Bounded* containers as well, probably a good idea, or?

@KiChjang
Copy link
Contributor

Would a trait make sense, or should it just be a method on the struct directly?

What do you think @KiChjang ? If we want to implement it for other Bounded* containers as well, probably a good idea, or?

I don't foresee anything wrong with implementing it on bounded types. Perhaps your hesitation comes from the fact that we might change the size of the elements, and hence the bounded size of the overall map? This wouldn't be an issue in my opinion, as the function returns a brand new BoundedBTreeMap, and so can be treated as a completely separate entity as the map that was operated over.

@ggwpez
Copy link
Member

ggwpez commented Aug 9, 2022

I don't foresee anything wrong with implementing it on bounded types. Perhaps your hesitation comes from the fact that we might change the size of the elements, and hence the bounded size of the overall map? This wouldn't be an issue in my opinion, as the function returns a brand new BoundedBTreeMap, and so can be treated as a completely separate entity as the map that was operated over.

Just wanted to hear your opinion, thanks 😄
I guess we can go ahead with this?

@KiChjang
Copy link
Contributor

Wait, I just realized... the map/try_map method are implemented directly as methods on the BoundedBTreeMap type, and I don't think this is required. We could've just implemented a iter_mut() method and gotten all of the map methods for free.

So unfortunately no, I don't think this is actually the right approach to the problem.

@benluelo
Copy link
Contributor Author

Added iter_mut, let me know what you think

@ggwpez
Copy link
Member

ggwpez commented Aug 10, 2022

So m.map(|(k, v)| 123) now becomes m.iter_mut().for_each(|(k, v)| *k = 123) and for try_ respectively?
Fair enough, then we dont need the dedicated map and try_map functions.

@KiChjang
Copy link
Contributor

Correct, I don't see the need for the try_map/map functions when we can simply get an iterator out of the BoundedBTreeMap.

@KiChjang
Copy link
Contributor

bot merge

@paritytech-processbot
Copy link

Waiting for commit status.

@paritytech-processbot paritytech-processbot bot merged commit 469189b into paritytech:master Aug 11, 2022
@benluelo
Copy link
Contributor Author

One issue with iter_mut is that it doesn't allow you to transform the item's type in the collection - you can alter the value, but if you want to go from a Map<A, B> to a Map<A, C> you still need to into_iter/map/try_collect/unwrap (which is not ideal if they're bounded by the same S). I think there's usecases for both, personally.

ark0f pushed a commit to gear-tech/substrate that referenced this pull request Feb 27, 2023
* Add map and try_map methods to BoundedBTreeMap

* Undo changes to basic_authorship.rs

* Remove unwrap and use unchecked_from instead

* Add iter_mut() method

* Remove map functions and add docs to iter_mut

* fmt

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D3-trivial 🧸 PR contains trivial changes in a runtime directory that do not require an audit
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants