From a886361819e6780b770a23ba9fdff6591f69057d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marin=20Ver=C5=A1i=C4=87?= Date: Tue, 22 Oct 2024 12:44:28 +0200 Subject: [PATCH] chore: remove riffle_iter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marin Veršić --- crates/iroha_primitives/src/lib.rs | 1 - crates/iroha_primitives/src/riffle_iter.rs | 144 --------------------- 2 files changed, 145 deletions(-) delete mode 100644 crates/iroha_primitives/src/riffle_iter.rs diff --git a/crates/iroha_primitives/src/lib.rs b/crates/iroha_primitives/src/lib.rs index 0face676e98..f2647272e66 100644 --- a/crates/iroha_primitives/src/lib.rs +++ b/crates/iroha_primitives/src/lib.rs @@ -20,7 +20,6 @@ pub mod conststr; pub mod json; pub mod must_use; pub mod numeric; -pub mod riffle_iter; pub mod small; #[cfg(feature = "std")] pub mod time; diff --git a/crates/iroha_primitives/src/riffle_iter.rs b/crates/iroha_primitives/src/riffle_iter.rs deleted file mode 100644 index 23d15a04154..00000000000 --- a/crates/iroha_primitives/src/riffle_iter.rs +++ /dev/null @@ -1,144 +0,0 @@ -//! Contains riffle iterator and related trait - -/// Iterator which combine two iterators into the single one. -/// Name is inspired by riffle shuffle of cards deck. -/// -/// ```ignore -/// [(a0,a1,a2,..),(b0,b1,b2,..)] -> (a0,b0,a1,b1,a2,b2,..) -/// ``` -#[derive(Clone)] -pub struct RiffleIterator { - left_iter: A, - right_iter: B, - state: RiffleState, -} - -#[derive(Clone, Copy)] -enum RiffleState { - CurrentLeft, - CurrentRight, - LeftExhausted, - RightExhausted, - BothExhausted, -} - -/// Trait to create [`RiffleIterator`] from two iterators. -pub trait RiffleIter: Iterator + Sized { - /// Create `RoundRobinIterator` from two iterators. - fn riffle>( - self, - iter: I, - ) -> RiffleIterator::IntoIter> { - RiffleIterator { - left_iter: self, - right_iter: iter.into_iter(), - state: RiffleState::CurrentLeft, - } - } -} - -impl RiffleIter for T {} - -impl Iterator for RiffleIterator -where - A: Iterator, - B: Iterator, -{ - type Item = T; - - fn next(&mut self) -> Option { - use RiffleState::*; - loop { - match self.state { - BothExhausted => break None, - LeftExhausted => { - let item = self.right_iter.next(); - if item.is_none() { - self.state = BothExhausted; - } - break item; - } - RightExhausted => { - let item = self.left_iter.next(); - if item.is_none() { - self.state = BothExhausted; - } - break item; - } - CurrentLeft => { - let item = self.left_iter.next(); - if item.is_none() { - self.state = LeftExhausted; - continue; - } - self.state = CurrentRight; - break item; - } - CurrentRight => { - let item = self.right_iter.next(); - if item.is_none() { - self.state = RightExhausted; - continue; - } - self.state = CurrentLeft; - break item; - } - } - } - } -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn riffle_iter_a_eq_b_size() { - let a = vec![0, 2, 4, 6, 8]; - let b = vec![1, 3, 5, 7, 9]; - assert_eq!( - vec![0, 1, 2, 3, 4, 5, 6, 7, 8, 9], - a.into_iter().riffle(b).collect::>() - ); - } - - #[test] - fn riffle_iter_a_gt_b_size() { - let a = vec![0, 2, 4, 6, 8]; - let b = vec![1, 3, 5]; - assert_eq!( - vec![0, 1, 2, 3, 4, 5, 6, 8], - a.into_iter().riffle(b).collect::>() - ); - } - - #[test] - fn riffle_iter_a_lt_b_size() { - let a = vec![0, 2, 4]; - let b = vec![1, 3, 5, 7, 9]; - assert_eq!( - vec![0, 1, 2, 3, 4, 5, 7, 9], - a.into_iter().riffle(b).collect::>() - ); - } - - #[test] - fn riffle_iter_a_empty() { - let a = vec![0, 2, 4, 6, 8]; - let b = Vec::new(); - assert_eq!( - vec![0, 2, 4, 6, 8], - a.into_iter().riffle(b).collect::>() - ); - } - - #[test] - fn riffle_iter_b_empty() { - let a = Vec::new(); - let b = vec![1, 3, 5, 7, 9]; - assert_eq!( - vec![1, 3, 5, 7, 9], - a.into_iter().riffle(b).collect::>() - ); - } -}