diff --git a/src/core/utils/algorithm.rs b/src/core/utils/algorithm.rs new file mode 100644 index 00000000..9bc1bc8a --- /dev/null +++ b/src/core/utils/algorithm.rs @@ -0,0 +1,25 @@ +use std::cmp::Ordering; + +#[allow(clippy::impl_trait_in_params)] +pub fn common_elements( + mut iterators: impl Iterator>>, check_order: impl Fn(&[u8], &[u8]) -> Ordering, +) -> Option>> { + let first_iterator = iterators.next()?; + let mut other_iterators = iterators.map(Iterator::peekable).collect::>(); + + 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 + }) + })) +} diff --git a/src/core/utils/mod.rs b/src/core/utils/mod.rs index 1556646e..29d0b87b 100644 --- a/src/core/utils/mod.rs +++ b/src/core/utils/mod.rs @@ -1,3 +1,4 @@ +pub mod algorithm; pub mod bytes; pub mod content_disposition; pub mod debug; @@ -13,9 +14,10 @@ pub mod sys; mod tests; pub mod time; -use std::cmp::{self, Ordering}; +use std::cmp; pub use ::ctor::{ctor, dtor}; +pub use algorithm::common_elements; pub use bytes::{increment, u64_from_bytes, u64_from_u8, u64_from_u8x8}; pub use conduit_macros::implement; pub use debug::slice_truncated as debug_slice_truncated; @@ -47,27 +49,3 @@ pub fn generate_keypair() -> Vec { ); value } - -#[allow(clippy::impl_trait_in_params)] -pub fn common_elements( - mut iterators: impl Iterator>>, check_order: impl Fn(&[u8], &[u8]) -> Ordering, -) -> Option>> { - let first_iterator = iterators.next()?; - let mut other_iterators = iterators.map(Iterator::peekable).collect::>(); - - 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 - }) - })) -}