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

Set-based operations on HashMap #1893

Open
jethrogb opened this Issue Feb 9, 2017 · 2 comments

Comments

Projects
None yet
3 participants
@jethrogb
Copy link
Contributor

jethrogb commented Feb 9, 2017

HashSet has all kinds of useful set functionality, but these functions are incompatible with HashMap keys:

  • difference
  • symmetric_difference
  • intersection
  • union
  • is_disjoint
  • is_subset
  • is_superset

It would be nice to enable the use of these functions between HashMaps and HashSets, either by allowing semi-cheap conversions from HashMap.keys to HashSet or by enabling those functions to take either maps or sets.

@jethrogb jethrogb changed the title View HashMap keys as HashSet Enable set-based operations on HashMap Feb 9, 2017

@jethrogb jethrogb changed the title Enable set-based operations on HashMap Set-based operations on HashMap Feb 9, 2017

@frewsxcv

This comment has been minimized.

Copy link
Member

frewsxcv commented Feb 13, 2017

How about:

  • std::collections::btree_map::BTreeMap::keys_as_set(&self) -> BTreeSet<'a, K> { .. }
  • fn std::collections::hash_map::HashMap::keys_as_set(&self) -> HashSet<'a, K> { .. }

or possibly:

  • std::collections::btree_map::Keys::as_btree_set(&self) -> BTreeSet<'a, K> { .. }
  • fn std::collections::hash_map::Keys::as_hash_set(&self) -> HashSet<'a, K> { .. }

It might not be that trivial implementation-wise because {BTree,Hash}Set internally use their Map counterparts:

https://github.com/rust-lang/rust/blob/956e2bcbaa00c05e051718b1375375915064f1c3/src/libstd/collections/hash/set.rs#L118

https://github.com/rust-lang/rust/blob/956e2bcbaa00c05e051718b1375375915064f1c3/src/libcollections/btree/set.rs#L74

... but I think it should be possible (EDIT: nevermind, after looking into implementing this, it doesn't appear to be so straightforward).

@frewsxcv frewsxcv added the A-libs label Feb 13, 2017

@jethrogb

This comment has been minimized.

Copy link
Contributor Author

jethrogb commented Feb 13, 2017

I've been thinking about this some more, and I think it would be useful to have the all set operations supported on Map<K,V1> and Map<K,V2>. The difference iterator type is (&K,&V1). The intersection iterator type is (&K,&V1,&V2). The symmetric_difference and union iterator type is &K, or possibly (&K,Occurence<V1,V2>) with

enum Occurence<V1,V2> {
  InSelf(&V1),
  InOther(&V2),
  InBoth(&V1, &V2),
}

@petrochenkov petrochenkov added T-libs and removed A-libs labels Jan 28, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.