[][src]Function iter_set::union_by

pub fn union_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

Take the union 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 union().

Examples

Using the comparator function to perform a 'deep union'

use std::cmp::Ordering::{self, Equal, Greater, Less};
use iter_set::union_by;

#[derive(Debug, Eq, PartialEq)]
enum Foo {
    Zero,
    Some(Vec<u32>),
}

fn combine(l: &mut Foo, r: &mut Foo) -> Ordering {
    match (l, r) {
        (Foo::Zero, Foo::Zero) => Equal,
        (Foo::Zero, Foo::Some(_)) => Less,
        (Foo::Some(_), Foo::Zero) => Greater,
        (Foo::Some(l), Foo::Some(r)) => {
            l.append(r);
            Equal
        }
    }
}

let lhs = vec![Foo::Zero, Foo::Some(vec![1, 2])];
let rhs = vec![Foo::Some(vec![3])];

let union: Vec<_> = union_by(lhs, rhs, combine).collect();
assert_eq!(union, vec![Foo::Zero, Foo::Some(vec![1, 2, 3])]);