Function visioncortex::disjoint_sets::group_by_cached_key
source · pub fn group_by_cached_key<T, Key, Extract, Group>(
items: Vec<T>,
extract_key: Extract,
should_group: Group
) -> Vec<Vec<T>>
Expand description
Groups items with a key extraction function and a equivalence testing function on the keys.
See the documentation of group_by
for the requirements of the testing function.
During grouping, the key function is called only once per element.
For simple key functions, group_by
is likely to be faster.
§Example
use visioncortex::disjoint_sets::group_by_cached_key;
let points = vec![1,1,7,9,24,1,4,7,3,8];
let groups = group_by_cached_key(points, |&x| x, |&x, &y| {
(x - y) * (x - y) < 2
});
// should be grouped as below:
// {1, 1, 1}, {3, 4}, {7, 7, 8, 9}, {24}
for mut group in groups {
println!("{:?}", group);
group.sort();
if group.len() == 4 {
assert_eq!(group, [7, 7, 8, 9]);
} else if group.len() == 3 {
assert_eq!(group, [1, 1, 1]);
} else if group.len() == 2 {
assert_eq!(group, [3, 4]);
} else {
assert_eq!(group, [24]);
}
}