Current Vec offers this method:
pub fn partition(self, f: |&T| -> bool) -> (Vec<T>, Vec<T>);
That can be useful.
But sometimes you want to break up your data into more than just two groups. Maybe three; or maybe the count depends on some runtime criteria.
And also, sometimes the element itself is not the relevant predicate; sometimes the index of the element is the predicate that matters. (see e.g. #15418.)
Here's a quick sketch of a more general API:
pub fn scatter(self, num_targets: uint, g: |elem: &T, index: uint| -> uint) -> Vec<Vec<T>>;
This would work by creating N vectors, where N == num_targets, and then the function g would map each (element, index) to the vector that it should be moved to. (If g ever returns a uint >= num_targets, then the method fails.)
(Another variant of this API would not provide num_targets up front; it would instead dynamically grow the set of output vectors as necessary to accommodate the values returned by g. But I think the above API would cover a lot of cases efficiently; at worst, num_targets could be made an Option<uint> to cover both use cases.)
Current
Vecoffers this method:That can be useful.
But sometimes you want to break up your data into more than just two groups. Maybe three; or maybe the count depends on some runtime criteria.
And also, sometimes the element itself is not the relevant predicate; sometimes the index of the element is the predicate that matters. (see e.g. #15418.)
Here's a quick sketch of a more general API:
This would work by creating N vectors, where N ==
num_targets, and then the functiongwould map each (element, index) to the vector that it should be moved to. (Ifgever returns auint >= num_targets, then the method fails.)(Another variant of this API would not provide
num_targetsup front; it would instead dynamically grow the set of output vectors as necessary to accommodate the values returned byg. But I think the above API would cover a lot of cases efficiently; at worst,num_targetscould be made anOption<uint>to cover both use cases.)