From d1535e945e05b4b49084127d81340d00d36b8bf3 Mon Sep 17 00:00:00 2001 From: Philippe-Cholet Date: Mon, 12 Jun 2023 12:16:35 +0200 Subject: [PATCH] Fully replace MergePredicate by OrderingOrBool --- src/merge_join.rs | 55 +++++++++++++---------------------------------- 1 file changed, 15 insertions(+), 40 deletions(-) diff --git a/src/merge_join.rs b/src/merge_join.rs index e4f0f7500..15b17ce79 100644 --- a/src/merge_join.rs +++ b/src/merge_join.rs @@ -10,14 +10,6 @@ use crate::size_hint::{self, SizeHint}; #[cfg(doc)] use crate::Itertools; -pub trait MergePredicate { - type Item; - fn left(left: I) -> Self::Item; - fn right(right: J) -> Self::Item; - fn merge(&mut self, left: I, right: J) -> (Option, Option, Self::Item); - fn size_hint(left: SizeHint, right: SizeHint) -> SizeHint; -} - /// Return an iterator adaptor that merge-joins items from the two base iterators in ascending order. /// /// [`IntoIterator`] enabled version of [`Itertools::merge_join_by`]. @@ -100,24 +92,6 @@ impl OrderingOrBool for bool { } } -impl, F: FnMut(&I, &J) -> OoB> MergePredicate for F { - type Item = OoB::MergeResult; - fn left(left: I) -> Self::Item { - OoB::left(left) - } - fn right(right: J) -> Self::Item { - OoB::right(right) - } - fn merge(&mut self, left: I, right: J) -> (Option, Option, Self::Item) { - let cmp_result = self(&left, &right); - OoB::merge(cmp_result, left, right) - } - fn size_hint(left: SizeHint, right: SizeHint) -> SizeHint { - OoB::size_hint(left, right) - } - -} - impl Clone for MergeJoinBy where I: Iterator, J: Iterator, @@ -137,20 +111,21 @@ impl fmt::Debug for MergeJoinBy debug_fmt_fields!(MergeJoinBy, left, right); } -impl Iterator for MergeJoinBy +impl Iterator for MergeJoinBy where I: Iterator, J: Iterator, - F: MergePredicate, + F: FnMut(&I::Item, &J::Item) -> T, + T: OrderingOrBool, { - type Item = F::Item; + type Item = T::MergeResult; fn next(&mut self) -> Option { match (self.left.next(), self.right.next()) { (None, None) => None, - (Some(left), None) => Some(F::left(left)), - (None, Some(right)) => Some(F::right(right)), + (Some(left), None) => Some(T::left(left)), + (None, Some(right)) => Some(T::right(right)), (Some(left), Some(right)) => { - let (left, right, next) = self.cmp_fn.merge(left, right); + let (left, right, next) = (self.cmp_fn)(&left, &right).merge(left, right); if let Some(left) = left { self.left.put_back(left); } @@ -163,7 +138,7 @@ impl Iterator for MergeJoinBy } fn size_hint(&self) -> SizeHint { - F::size_hint(self.left.size_hint(), self.right.size_hint()) + T::size_hint(self.left.size_hint(), self.right.size_hint()) } fn count(mut self) -> usize { @@ -175,7 +150,7 @@ impl Iterator for MergeJoinBy (None, Some(_right)) => break count + 1 + self.right.into_parts().1.count(), (Some(left), Some(right)) => { count += 1; - let (left, right, _) = self.cmp_fn.merge(left, right); + let (left, right, _) = (self.cmp_fn)(&left, &right).merge(left, right); if let Some(left) = left { self.left.put_back(left); } @@ -193,17 +168,17 @@ impl Iterator for MergeJoinBy match (self.left.next(), self.right.next()) { (None, None) => break previous_element, (Some(left), None) => { - break Some(F::left( + break Some(T::left( self.left.into_parts().1.last().unwrap_or(left), )) } (None, Some(right)) => { - break Some(F::right( + break Some(T::right( self.right.into_parts().1.last().unwrap_or(right), )) } (Some(left), Some(right)) => { - let (left, right, elem) = self.cmp_fn.merge(left, right); + let (left, right, elem) = (self.cmp_fn)(&left, &right).merge(left, right); previous_element = Some(elem); if let Some(left) = left { self.left.put_back(left); @@ -224,10 +199,10 @@ impl Iterator for MergeJoinBy n -= 1; match (self.left.next(), self.right.next()) { (None, None) => break None, - (Some(_left), None) => break self.left.nth(n).map(F::left), - (None, Some(_right)) => break self.right.nth(n).map(F::right), + (Some(_left), None) => break self.left.nth(n).map(T::left), + (None, Some(_right)) => break self.right.nth(n).map(T::right), (Some(left), Some(right)) => { - let (left, right, _) = self.cmp_fn.merge(left, right); + let (left, right, _) = (self.cmp_fn)(&left, &right).merge(left, right); if let Some(left) = left { self.left.put_back(left); }