Function iter_set::intersection_by [−][src]
pub fn intersection_by<T, L, R, F>(
a: L,
b: R,
cmp: F
) -> impl Iterator<Item = T> where
L: IntoIterator<Item = T>,
R: IntoIterator<Item = T>,
F: FnMut(&mut T, &mut T) -> Ordering,
Expand description
Take the intersection of two sets represented by sorted, deduplicated iterators, using a comparator function.
Note that since this passes elements to the comparator function as &mut T
, you can swap them
to override the default behaviour of returning duplicate elements from a
.
See intersection()
.
Examples
Using the comparator function to choose which iterator to take from.
use std::cmp::Ordering::{self, Equal};
use std::mem::swap;
use iter_set::intersection_by;
let mut a = [(1, vec![2]), (2, vec![])];
let mut b = [(2, vec![1]), (3, vec![])];
fn compare(l: &mut (u32, Vec<i32>), r: &mut (u32, Vec<i32>)) -> Ordering {
match Ord::cmp(&l.0, &r.0) {
Equal => {
if r.1.len() > l.1.len() {
swap(r, l);
}
Equal
}
neq => neq,
}
}
assert!(intersection_by(&mut a, &mut b, |l, r| compare(*l, *r)).eq(&[(2, vec![1])]));