Skip to content

Commit 58ba1f5

Browse files
committed
Add Iterator comparison methods that take a comparison function
1 parent 1fb3c4e commit 58ba1f5

File tree

3 files changed

+160
-6
lines changed

3 files changed

+160
-6
lines changed

src/libcore/iter/traits/iterator.rs

+103-6
Original file line numberDiff line numberDiff line change
@@ -2557,10 +2557,40 @@ pub trait Iterator {
25572557
/// assert_eq!([1, 2].iter().cmp([1].iter()), Ordering::Greater);
25582558
/// ```
25592559
#[stable(feature = "iter_order", since = "1.5.0")]
2560-
fn cmp<I>(mut self, other: I) -> Ordering where
2560+
fn cmp<I>(self, other: I) -> Ordering
2561+
where
25612562
I: IntoIterator<Item = Self::Item>,
25622563
Self::Item: Ord,
25632564
Self: Sized,
2565+
{
2566+
self.cmp_by(other, |x, y| x.cmp(&y))
2567+
}
2568+
2569+
/// Lexicographically compares the elements of this `Iterator` with those
2570+
/// of another with respect to the specified comparison function.
2571+
///
2572+
/// # Examples
2573+
///
2574+
/// Basic usage:
2575+
///
2576+
/// ```
2577+
/// #![feature(iter_order_by)]
2578+
///
2579+
/// use std::cmp::Ordering;
2580+
///
2581+
/// let xs = [1, 2, 3, 4];
2582+
/// let ys = [1, 4, 9, 16];
2583+
///
2584+
/// assert_eq!(xs.iter().cmp_by(&ys, |&x, &y| x.cmp(&y)), Ordering::Less);
2585+
/// assert_eq!(xs.iter().cmp_by(&ys, |&x, &y| (x * x).cmp(&y)), Ordering::Equal);
2586+
/// assert_eq!(xs.iter().cmp_by(&ys, |&x, &y| (2 * x).cmp(&y)), Ordering::Greater);
2587+
/// ```
2588+
#[unstable(feature = "iter_order_by", issue = "0")]
2589+
fn cmp_by<I, F>(mut self, other: I, mut cmp: F) -> Ordering
2590+
where
2591+
Self: Sized,
2592+
I: IntoIterator,
2593+
F: FnMut(Self::Item, I::Item) -> Ordering,
25642594
{
25652595
let mut other = other.into_iter();
25662596

@@ -2579,7 +2609,7 @@ pub trait Iterator {
25792609
Some(val) => val,
25802610
};
25812611

2582-
match x.cmp(&y) {
2612+
match cmp(x, y) {
25832613
Ordering::Equal => (),
25842614
non_eq => return non_eq,
25852615
}
@@ -2601,10 +2631,49 @@ pub trait Iterator {
26012631
/// assert_eq!([std::f64::NAN].iter().partial_cmp([1.].iter()), None);
26022632
/// ```
26032633
#[stable(feature = "iter_order", since = "1.5.0")]
2604-
fn partial_cmp<I>(mut self, other: I) -> Option<Ordering> where
2634+
fn partial_cmp<I>(self, other: I) -> Option<Ordering>
2635+
where
26052636
I: IntoIterator,
26062637
Self::Item: PartialOrd<I::Item>,
26072638
Self: Sized,
2639+
{
2640+
self.partial_cmp_by(other, |x, y| x.partial_cmp(&y))
2641+
}
2642+
2643+
/// Lexicographically compares the elements of this `Iterator` with those
2644+
/// of another with respect to the specified comparison function.
2645+
///
2646+
/// # Examples
2647+
///
2648+
/// Basic usage:
2649+
///
2650+
/// ```
2651+
/// #![feature(iter_order_by)]
2652+
///
2653+
/// use std::cmp::Ordering;
2654+
///
2655+
/// let xs = [1.0, 2.0, 3.0, 4.0];
2656+
/// let ys = [1.0, 4.0, 9.0, 16.0];
2657+
///
2658+
/// assert_eq!(
2659+
/// xs.iter().partial_cmp_by(&ys, |&x, &y| x.partial_cmp(&y)),
2660+
/// Some(Ordering::Less)
2661+
/// );
2662+
/// assert_eq!(
2663+
/// xs.iter().partial_cmp_by(&ys, |&x, &y| (x * x).partial_cmp(&y)),
2664+
/// Some(Ordering::Equal)
2665+
/// );
2666+
/// assert_eq!(
2667+
/// xs.iter().partial_cmp_by(&ys, |&x, &y| (2.0 * x).partial_cmp(&y)),
2668+
/// Some(Ordering::Greater)
2669+
/// );
2670+
/// ```
2671+
#[unstable(feature = "iter_order_by", issue = "0")]
2672+
fn partial_cmp_by<I, F>(mut self, other: I, mut partial_cmp: F) -> Option<Ordering>
2673+
where
2674+
Self: Sized,
2675+
I: IntoIterator,
2676+
F: FnMut(Self::Item, I::Item) -> Option<Ordering>,
26082677
{
26092678
let mut other = other.into_iter();
26102679

@@ -2623,7 +2692,7 @@ pub trait Iterator {
26232692
Some(val) => val,
26242693
};
26252694

2626-
match x.partial_cmp(&y) {
2695+
match partial_cmp(x, y) {
26272696
Some(Ordering::Equal) => (),
26282697
non_eq => return non_eq,
26292698
}
@@ -2640,10 +2709,36 @@ pub trait Iterator {
26402709
/// assert_eq!([1].iter().eq([1, 2].iter()), false);
26412710
/// ```
26422711
#[stable(feature = "iter_order", since = "1.5.0")]
2643-
fn eq<I>(mut self, other: I) -> bool where
2712+
fn eq<I>(self, other: I) -> bool
2713+
where
26442714
I: IntoIterator,
26452715
Self::Item: PartialEq<I::Item>,
26462716
Self: Sized,
2717+
{
2718+
self.eq_by(other, |x, y| x == y)
2719+
}
2720+
2721+
/// Determines if the elements of this `Iterator` are equal to those of
2722+
/// another with respect to the specified equality function.
2723+
///
2724+
/// # Examples
2725+
///
2726+
/// Basic usage:
2727+
///
2728+
/// ```
2729+
/// #![feature(iter_order_by)]
2730+
///
2731+
/// let xs = [1, 2, 3, 4];
2732+
/// let ys = [1, 4, 9, 16];
2733+
///
2734+
/// assert!(xs.iter().eq_by(&ys, |&x, &y| x * x == y));
2735+
/// ```
2736+
#[unstable(feature = "iter_order_by", issue = "0")]
2737+
fn eq_by<I, F>(mut self, other: I, mut eq: F) -> bool
2738+
where
2739+
Self: Sized,
2740+
I: IntoIterator,
2741+
F: FnMut(Self::Item, I::Item) -> bool,
26472742
{
26482743
let mut other = other.into_iter();
26492744

@@ -2658,7 +2753,9 @@ pub trait Iterator {
26582753
Some(val) => val,
26592754
};
26602755

2661-
if x != y { return false }
2756+
if !eq(x, y) {
2757+
return false;
2758+
}
26622759
}
26632760
}
26642761

src/libcore/tests/iter.rs

+56
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,62 @@ fn test_multi_iter() {
5757
assert!(xs.iter().lt(xs.iter().skip(2)));
5858
}
5959

60+
#[test]
61+
fn test_cmp_by() {
62+
use core::cmp::Ordering;
63+
64+
let f = |x: i32, y: i32| (x * x).cmp(&y);
65+
let xs = || [1, 2, 3, 4].iter().copied();
66+
let ys = || [1, 4, 16].iter().copied();
67+
68+
assert_eq!(xs().cmp_by(ys(), f), Ordering::Less);
69+
assert_eq!(ys().cmp_by(xs(), f), Ordering::Greater);
70+
assert_eq!(xs().cmp_by(xs().map(|x| x * x), f), Ordering::Equal);
71+
assert_eq!(xs().rev().cmp_by(ys().rev(), f), Ordering::Greater);
72+
assert_eq!(xs().cmp_by(ys().rev(), f), Ordering::Less);
73+
assert_eq!(xs().cmp_by(ys().take(2), f), Ordering::Greater);
74+
}
75+
76+
#[test]
77+
fn test_partial_cmp_by() {
78+
use core::cmp::Ordering;
79+
use core::f64;
80+
81+
let f = |x: i32, y: i32| (x * x).partial_cmp(&y);
82+
let xs = || [1, 2, 3, 4].iter().copied();
83+
let ys = || [1, 4, 16].iter().copied();
84+
85+
assert_eq!(xs().partial_cmp_by(ys(), f), Some(Ordering::Less));
86+
assert_eq!(ys().partial_cmp_by(xs(), f), Some(Ordering::Greater));
87+
assert_eq!(xs().partial_cmp_by(xs().map(|x| x * x), f), Some(Ordering::Equal));
88+
assert_eq!(xs().rev().partial_cmp_by(ys().rev(), f), Some(Ordering::Greater));
89+
assert_eq!(xs().partial_cmp_by(xs().rev(), f), Some(Ordering::Less));
90+
assert_eq!(xs().partial_cmp_by(ys().take(2), f), Some(Ordering::Greater));
91+
92+
let f = |x: f64, y: f64| (x * x).partial_cmp(&y);
93+
let xs = || [1.0, 2.0, 3.0, 4.0].iter().copied();
94+
let ys = || [1.0, 4.0, f64::NAN, 16.0].iter().copied();
95+
96+
assert_eq!(xs().partial_cmp_by(ys(), f), None);
97+
assert_eq!(ys().partial_cmp_by(xs(), f), Some(Ordering::Greater));
98+
}
99+
100+
#[test]
101+
fn test_eq_by() {
102+
let f = |x: i32, y: i32| x * x == y;
103+
let xs = || [1, 2, 3, 4].iter().copied();
104+
let ys = || [1, 4, 9, 16].iter().copied();
105+
106+
assert!(xs().eq_by(ys(), f));
107+
assert!(!ys().eq_by(xs(), f));
108+
assert!(!xs().eq_by(xs(), f));
109+
assert!(!ys().eq_by(ys(), f));
110+
111+
assert!(!xs().take(3).eq_by(ys(), f));
112+
assert!(!xs().eq_by(ys().take(3), f));
113+
assert!(xs().take(3).eq_by(ys().take(3), f));
114+
}
115+
60116
#[test]
61117
fn test_counter_from_iter() {
62118
let it = (0..).step_by(5).take(10);

src/libcore/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#![feature(const_fn)]
3333
#![feature(iter_partition_in_place)]
3434
#![feature(iter_is_partitioned)]
35+
#![feature(iter_order_by)]
3536

3637
extern crate test;
3738

0 commit comments

Comments
 (0)