Description
Currently the method intersection of HashSet and BTreeSet only support an intersection between two sets. If you want to intersect multiple sets, for example 3, you'll need to calculate the intersection between the first two, collect them into another set and calculate the intersection between that immediate result and the third set. This is very intense on computation, memory usage and lines of code.
I'd like to suggest the following:
- Change the current implementations of the
Intersection
structs to contain a list of other sets to intersect with instead of a reference to a single other set. This applies tostd::collections::btree_set::Intersection
as well asstd::collections::hash_set::Intersection
. - Implement
intersect_many
(orintersect_multiple
) onHashSet
andBTreeSet
which take a list of other sets to all intersect with the current one. - Add
intersect
andintersect_many
(orintersect_multiple
) to theIntersection
structs as well. Those can only be called if theIntersection
hasn't been iterated over yet. Otherwise, they'll panic.
If (3.) is implemented, the "list of sets to intersect with" will need to be a Vec
in order to be growable after creation. For performance reasons, the third suggestion could be dropped and the "list" can instead be a reference to the provided slice.
The current implementation of {Hash,BTree}Set::intersection
would need to be changed to pass &[other]
instead of other
.
While this request should be relatively easy to implement for HashSet
(self.others.iter().all(|set| set.contains(elt))
, implementing it for BTreeSet
could result in a lot more code.
Unresolved Questions:
- Naming:
intersect_many
vsintersect_multiple
vs ??? - Don't implement the third suggestion (functions on the
Intersection
struct) in favor of using an allocation free slice-reference instead of aVec
?