From 079d9b10515f4d9016e32fa539f1792143e13b03 Mon Sep 17 00:00:00 2001 From: Tim Vermeulen Date: Sun, 17 Mar 2019 16:57:31 +0100 Subject: [PATCH 1/3] Forward Iterator::{ne, lt, le, gt, ge} to Iterator::{eq, partial_cmp} --- src/libcore/iter/traits/iterator.rs | 114 +++++----------------------- 1 file changed, 18 insertions(+), 96 deletions(-) diff --git a/src/libcore/iter/traits/iterator.rs b/src/libcore/iter/traits/iterator.rs index ca7feed0712d1..84db233c630fa 100644 --- a/src/libcore/iter/traits/iterator.rs +++ b/src/libcore/iter/traits/iterator.rs @@ -2435,145 +2435,67 @@ pub trait Iterator { /// Determines if the elements of this `Iterator` are unequal to those of /// another. #[stable(feature = "iter_order", since = "1.5.0")] - fn ne(mut self, other: I) -> bool where + fn ne(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialEq, Self: Sized, { - let mut other = other.into_iter(); - - loop { - let x = match self.next() { - None => return other.next().is_some(), - Some(val) => val, - }; - - let y = match other.next() { - None => return true, - Some(val) => val, - }; - - if x != y { return true } - } + !self.eq(other) } /// Determines if the elements of this `Iterator` are lexicographically /// less than those of another. #[stable(feature = "iter_order", since = "1.5.0")] - fn lt(mut self, other: I) -> bool where + fn lt(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd, Self: Sized, { - let mut other = other.into_iter(); - - loop { - let x = match self.next() { - None => return other.next().is_some(), - Some(val) => val, - }; - - let y = match other.next() { - None => return false, - Some(val) => val, - }; - - match x.partial_cmp(&y) { - Some(Ordering::Less) => return true, - Some(Ordering::Equal) => (), - Some(Ordering::Greater) => return false, - None => return false, - } + match self.partial_cmp(other) { + Some(Ordering::Less) => true, + _ => false, } } /// Determines if the elements of this `Iterator` are lexicographically /// less or equal to those of another. #[stable(feature = "iter_order", since = "1.5.0")] - fn le(mut self, other: I) -> bool where + fn le(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd, Self: Sized, { - let mut other = other.into_iter(); - - loop { - let x = match self.next() { - None => { other.next(); return true; }, - Some(val) => val, - }; - - let y = match other.next() { - None => return false, - Some(val) => val, - }; - - match x.partial_cmp(&y) { - Some(Ordering::Less) => return true, - Some(Ordering::Equal) => (), - Some(Ordering::Greater) => return false, - None => return false, - } + match self.partial_cmp(other) { + Some(Ordering::Less) | Some(Ordering::Equal) => true, + _ => false, } } /// Determines if the elements of this `Iterator` are lexicographically /// greater than those of another. #[stable(feature = "iter_order", since = "1.5.0")] - fn gt(mut self, other: I) -> bool where + fn gt(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd, Self: Sized, { - let mut other = other.into_iter(); - - loop { - let x = match self.next() { - None => { other.next(); return false; }, - Some(val) => val, - }; - - let y = match other.next() { - None => return true, - Some(val) => val, - }; - - match x.partial_cmp(&y) { - Some(Ordering::Less) => return false, - Some(Ordering::Equal) => (), - Some(Ordering::Greater) => return true, - None => return false, - } + match self.partial_cmp(other) { + Some(Ordering::Greater) => true, + _ => false, } } /// Determines if the elements of this `Iterator` are lexicographically /// greater than or equal to those of another. #[stable(feature = "iter_order", since = "1.5.0")] - fn ge(mut self, other: I) -> bool where + fn ge(self, other: I) -> bool where I: IntoIterator, Self::Item: PartialOrd, Self: Sized, { - let mut other = other.into_iter(); - - loop { - let x = match self.next() { - None => return other.next().is_none(), - Some(val) => val, - }; - - let y = match other.next() { - None => return true, - Some(val) => val, - }; - - match x.partial_cmp(&y) { - Some(Ordering::Less) => return false, - Some(Ordering::Equal) => (), - Some(Ordering::Greater) => return true, - None => return false, - } + match self.partial_cmp(other) { + Some(Ordering::Greater) | Some(Ordering::Equal) => true, + _ => false, } } From 67783964de78299475cf826b0021040ef783e373 Mon Sep 17 00:00:00 2001 From: Tim Vermeulen Date: Sun, 17 Mar 2019 19:13:44 +0100 Subject: [PATCH 2/3] Add iter::{bench_partial_cmp, bench_lt} benchmarks --- src/libcore/benches/iter.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/libcore/benches/iter.rs b/src/libcore/benches/iter.rs index 825bd368bdf1d..7dcfad8306fce 100644 --- a/src/libcore/benches/iter.rs +++ b/src/libcore/benches/iter.rs @@ -334,3 +334,13 @@ fn bench_filter_chain_ref_count(b: &mut Bencher) { (0i64..1000000).chain(0..1000000).map(black_box).by_ref().filter(|x| x % 3 == 0).count() }) } + +#[bench] +fn bench_partial_cmp(b: &mut Bencher) { + b.iter(|| (0..100000).map(black_box).partial_cmp((0..100000).map(black_box))) +} + +#[bench] +fn bench_lt(b: &mut Bencher) { + b.iter(|| (0..100000).map(black_box).lt((0..100000).map(black_box))) +} From 075b2697e47e9370dcf2d38f01469b38bd4d903e Mon Sep 17 00:00:00 2001 From: Tim Vermeulen Date: Mon, 18 Mar 2019 17:08:53 +0100 Subject: [PATCH 3/3] Simplify Iterator::{lt, gt} --- src/libcore/iter/traits/iterator.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/libcore/iter/traits/iterator.rs b/src/libcore/iter/traits/iterator.rs index 84db233c630fa..6df4a457655c5 100644 --- a/src/libcore/iter/traits/iterator.rs +++ b/src/libcore/iter/traits/iterator.rs @@ -2451,10 +2451,7 @@ pub trait Iterator { Self::Item: PartialOrd, Self: Sized, { - match self.partial_cmp(other) { - Some(Ordering::Less) => true, - _ => false, - } + self.partial_cmp(other) == Some(Ordering::Less) } /// Determines if the elements of this `Iterator` are lexicographically @@ -2479,10 +2476,7 @@ pub trait Iterator { Self::Item: PartialOrd, Self: Sized, { - match self.partial_cmp(other) { - Some(Ordering::Greater) => true, - _ => false, - } + self.partial_cmp(other) == Some(Ordering::Greater) } /// Determines if the elements of this `Iterator` are lexicographically