Function visioncortex::disjoint_sets::group_by
source · [−]Expand description
Groups items with a equivalence testing function.
The testing function should define a equivalence relation ~
on the set of elements
and return true for elements a
and b
if-and-only-if a ~ b
.
This implies that the function is commutative, i.e. should_group(a, b) == should_group(b, a
).
Example
use visioncortex::disjoint_sets::group_by;
let points = vec![1,1,7,9,24,1,4,7,3,8];
let groups = group_by(points, |&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]);
}
}