mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2025-07-27 18:28:31 +00:00
26 lines
773 B
Rust
26 lines
773 B
Rust
|
use std::cmp::Ordering;
|
||
|
|
||
|
#[allow(clippy::impl_trait_in_params)]
|
||
|
pub fn common_elements(
|
||
|
mut iterators: impl Iterator<Item = impl Iterator<Item = Vec<u8>>>, check_order: impl Fn(&[u8], &[u8]) -> Ordering,
|
||
|
) -> Option<impl Iterator<Item = Vec<u8>>> {
|
||
|
let first_iterator = iterators.next()?;
|
||
|
let mut other_iterators = iterators.map(Iterator::peekable).collect::<Vec<_>>();
|
||
|
|
||
|
Some(first_iterator.filter(move |target| {
|
||
|
other_iterators.iter_mut().all(|it| {
|
||
|
while let Some(element) = it.peek() {
|
||
|
match check_order(element, target) {
|
||
|
Ordering::Greater => return false, // We went too far
|
||
|
Ordering::Equal => return true, // Element is in both iters
|
||
|
Ordering::Less => {
|
||
|
// Keep searching
|
||
|
it.next();
|
||
|
},
|
||
|
}
|
||
|
}
|
||
|
false
|
||
|
})
|
||
|
}))
|
||
|
}
|