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

Add DelegatingMapSet to collection package #11

Closed
DartBot opened this issue Jun 5, 2015 · 4 comments
Closed

Add DelegatingMapSet to collection package #11

DartBot opened this issue Jun 5, 2015 · 4 comments
Assignees
Labels
type-enhancement A request for a change that isn't a bug

Comments

@DartBot
Copy link

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="96" height="96"hspace="10"> Issue by nex3
Originally opened as dart-lang/sdk#18705


Suppose there's a collection of objects that are indexed by properties on the objects themselves. Normally this would be represented by a Map, but in some cases it may be more useful to treat it as a Set. This is possible since the key is determinable from each value, so for example "add" could be implemented as "super[keyForValue(value)] = value". It would be nice to add a class to support this to the collection package.

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="48" height="48"hspace="10"> Comment by nex3


Set owner to @nex3.
Added Started label.

@DartBot DartBot added type-enhancement A request for a change that isn't a bug Fixed labels Jun 5, 2015
@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/4865287?v=3" align="left" width="48" height="48"hspace="10"> Comment by lrhn


You can do that using:

     new HashSet(equals: (a, b) => keyForValue(a) == keyForValue(b),
                 hash: (a) => keyForValue(a).hashCode);

Is there something a specialized map-backed-set would be able to do better than this?

If the keyForValue computation is expensive, I can see a reason to extract it once, and use it as the key of a map, but it's going to be used on all lookups as well, so it should probably be cached on the object instead (or maybe in an Expando).

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="48" height="48"hspace="10"> Comment by nex3


The goal is to provide a Set view of a map's keys, so it's important that the resulting set delegates to the backing map.

For example, in Barback we have a notion of assets that have ids. We store these assets in a Map<AssetId, Asset>, but sometimes it's useful to view this as a Set—it makes it easier to add and remove Assets, for example. In this case, keyForValue would be "(asset) => asset.id".

@DartBot
Copy link
Author

DartBot commented Jun 5, 2015

<img src="https://avatars.githubusercontent.com/u/188?v=3" align="left" width="48" height="48"hspace="10"> Comment by nex3


Fixed in dart-lang/sdk@39ba636, to be released in collection 0.9.3.


Added Fixed label.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

2 participants