diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index 2835e28a946..11d389d85ba 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -22,7 +22,7 @@ use core::fmt::Debug; use core::hash::{Hash, Hasher}; use core::iter::{Map, FromIterator}; use core::ops::Index; -use core::{iter, fmt, mem, usize}; +use core::{fmt, mem, usize}; use Bound::{self, Included, Excluded, Unbounded}; use borrow::Borrow; @@ -915,7 +915,7 @@ impl Eq for BTreeMap {} impl PartialOrd for BTreeMap { #[inline] fn partial_cmp(&self, other: &BTreeMap) -> Option { - iter::order::partial_cmp(self.iter(), other.iter()) + self.iter().partial_cmp(other.iter()) } } @@ -923,7 +923,7 @@ impl PartialOrd for BTreeMap { impl Ord for BTreeMap { #[inline] fn cmp(&self, other: &BTreeMap) -> Ordering { - iter::order::cmp(self.iter(), other.iter()) + self.iter().cmp(other.iter()) } } diff --git a/src/libcollections/linked_list.rs b/src/libcollections/linked_list.rs index 80ef2067819..891e8b7b2c9 100644 --- a/src/libcollections/linked_list.rs +++ b/src/libcollections/linked_list.rs @@ -25,7 +25,7 @@ use alloc::boxed::Box; use core::cmp::Ordering; use core::fmt; use core::hash::{Hasher, Hash}; -use core::iter::{self, FromIterator}; +use core::iter::FromIterator; use core::mem; use core::ptr; @@ -917,12 +917,12 @@ impl<'a, T: 'a + Copy> Extend<&'a T> for LinkedList { impl PartialEq for LinkedList { fn eq(&self, other: &LinkedList) -> bool { self.len() == other.len() && - iter::order::eq(self.iter(), other.iter()) + self.iter().eq(other.iter()) } fn ne(&self, other: &LinkedList) -> bool { self.len() != other.len() || - iter::order::ne(self.iter(), other.iter()) + self.iter().ne(other.iter()) } } @@ -932,7 +932,7 @@ impl Eq for LinkedList {} #[stable(feature = "rust1", since = "1.0.0")] impl PartialOrd for LinkedList { fn partial_cmp(&self, other: &LinkedList) -> Option { - iter::order::partial_cmp(self.iter(), other.iter()) + self.iter().partial_cmp(other.iter()) } } @@ -940,7 +940,7 @@ impl PartialOrd for LinkedList { impl Ord for LinkedList { #[inline] fn cmp(&self, other: &LinkedList) -> Ordering { - iter::order::cmp(self.iter(), other.iter()) + self.iter().cmp(other.iter()) } } diff --git a/src/libcollections/vec_deque.rs b/src/libcollections/vec_deque.rs index 96e24b412d5..79e89886791 100644 --- a/src/libcollections/vec_deque.rs +++ b/src/libcollections/vec_deque.rs @@ -20,7 +20,7 @@ use core::cmp::Ordering; use core::fmt; -use core::iter::{self, repeat, FromIterator}; +use core::iter::{repeat, FromIterator}; use core::ops::{Index, IndexMut}; use core::ptr; use core::slice; @@ -1676,7 +1676,7 @@ impl Eq for VecDeque {} #[stable(feature = "rust1", since = "1.0.0")] impl PartialOrd for VecDeque { fn partial_cmp(&self, other: &VecDeque) -> Option { - iter::order::partial_cmp(self.iter(), other.iter()) + self.iter().partial_cmp(other.iter()) } } @@ -1684,7 +1684,7 @@ impl PartialOrd for VecDeque { impl Ord for VecDeque { #[inline] fn cmp(&self, other: &VecDeque) -> Ordering { - iter::order::cmp(self.iter(), other.iter()) + self.iter().cmp(other.iter()) } } diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index ee32999ba8f..b54d8fceffd 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -58,7 +58,7 @@ use clone::Clone; use cmp; -use cmp::{Ord, PartialOrd, PartialEq}; +use cmp::{Ord, PartialOrd, PartialEq, Ordering}; use default::Default; use marker; use mem; @@ -1005,6 +1005,198 @@ pub trait Iterator { { self.fold(One::one(), |p, e| p * e) } + + /// Lexicographically compares the elements of this `Iterator` with those + /// of another. + #[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")] + fn cmp(mut self, other: I) -> Ordering where + I: IntoIterator, + Self::Item: Ord, + Self: Sized, + { + let mut other = other.into_iter(); + + loop { + match (self.next(), other.next()) { + (None, None) => return Ordering::Equal, + (None, _ ) => return Ordering::Less, + (_ , None) => return Ordering::Greater, + (Some(x), Some(y)) => match x.cmp(&y) { + Ordering::Equal => (), + non_eq => return non_eq, + }, + } + } + } + + /// Lexicographically compares the elements of this `Iterator` with those + /// of another. + #[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")] + fn partial_cmp(mut self, other: I) -> Option where + I: IntoIterator, + Self::Item: PartialOrd, + Self: Sized, + { + let mut other = other.into_iter(); + + loop { + match (self.next(), other.next()) { + (None, None) => return Some(Ordering::Equal), + (None, _ ) => return Some(Ordering::Less), + (_ , None) => return Some(Ordering::Greater), + (Some(x), Some(y)) => match x.partial_cmp(&y) { + Some(Ordering::Equal) => (), + non_eq => return non_eq, + }, + } + } + } + + /// Determines if the elements of this `Iterator` are equal to those of + /// another. + #[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")] + fn eq(mut self, other: I) -> bool where + I: IntoIterator, + Self::Item: PartialEq, + Self: Sized, + { + let mut other = other.into_iter(); + + loop { + match (self.next(), other.next()) { + (None, None) => return true, + (None, _) | (_, None) => return false, + (Some(x), Some(y)) => if x != y { return false }, + } + } + } + + /// Determines if the elements of this `Iterator` are unequal to those of + /// another. + #[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")] + fn ne(mut self, other: I) -> bool where + I: IntoIterator, + Self::Item: PartialEq, + Self: Sized, + { + let mut other = other.into_iter(); + + loop { + match (self.next(), other.next()) { + (None, None) => return false, + (None, _) | (_, None) => return true, + (Some(x), Some(y)) => if x.ne(&y) { return true }, + } + } + } + + /// Determines if the elements of this `Iterator` are lexicographically + /// less than those of another. + #[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")] + fn lt(mut self, other: I) -> bool where + I: IntoIterator, + Self::Item: PartialOrd, + Self: Sized, + { + let mut other = other.into_iter(); + + loop { + match (self.next(), other.next()) { + (None, None) => return false, + (None, _ ) => return true, + (_ , None) => return false, + (Some(x), Some(y)) => { + match x.partial_cmp(&y) { + Some(Ordering::Less) => return true, + Some(Ordering::Equal) => {} + Some(Ordering::Greater) => return false, + None => return false, + } + }, + } + } + } + + /// Determines if the elements of this `Iterator` are lexicographically + /// less or equal to those of another. + #[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")] + fn le(mut self, other: I) -> bool where + I: IntoIterator, + Self::Item: PartialOrd, + Self: Sized, + { + let mut other = other.into_iter(); + + loop { + match (self.next(), other.next()) { + (None, None) => return true, + (None, _ ) => return true, + (_ , None) => return false, + (Some(x), Some(y)) => { + match x.partial_cmp(&y) { + Some(Ordering::Less) => return true, + Some(Ordering::Equal) => {} + Some(Ordering::Greater) => return false, + None => return false, + } + }, + } + } + } + + /// Determines if the elements of this `Iterator` are lexicographically + /// greater than those of another. + #[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")] + fn gt(mut self, other: I) -> bool where + I: IntoIterator, + Self::Item: PartialOrd, + Self: Sized, + { + let mut other = other.into_iter(); + + loop { + match (self.next(), other.next()) { + (None, None) => return false, + (None, _ ) => return false, + (_ , None) => return true, + (Some(x), Some(y)) => { + match x.partial_cmp(&y) { + Some(Ordering::Less) => return false, + Some(Ordering::Equal) => {} + Some(Ordering::Greater) => return true, + None => return false, + } + } + } + } + } + + /// Determines if the elements of this `Iterator` are lexicographically + /// greater than or equal to those of another. + #[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")] + fn ge(mut self, other: I) -> bool where + I: IntoIterator, + Self::Item: PartialOrd, + Self: Sized, + { + let mut other = other.into_iter(); + + loop { + match (self.next(), other.next()) { + (None, None) => return true, + (None, _ ) => return false, + (_ , None) => return true, + (Some(x), Some(y)) => { + match x.partial_cmp(&y) { + Some(Ordering::Less) => return false, + Some(Ordering::Equal) => {} + Some(Ordering::Greater) => return true, + None => return false, + } + }, + } + } + } } /// Select an element from an iterator based on the given projection @@ -2654,146 +2846,79 @@ pub fn once(value: T) -> Once { /// /// If two sequences are equal up until the point where one ends, /// the shorter sequence compares less. +#[deprecated(since = "1.4.0", reason = "use the equivalent methods on `Iterator` instead")] #[unstable(feature = "iter_order", reason = "needs review and revision", issue = "27737")] pub mod order { use cmp; use cmp::{Eq, Ord, PartialOrd, PartialEq}; - use cmp::Ordering::{Equal, Less, Greater}; use option::Option; - use option::Option::{Some, None}; use super::Iterator; /// Compare `a` and `b` for equality using `Eq` - pub fn equals(mut a: L, mut b: R) -> bool where + pub fn equals(a: L, b: R) -> bool where A: Eq, L: Iterator, R: Iterator, { - loop { - match (a.next(), b.next()) { - (None, None) => return true, - (None, _) | (_, None) => return false, - (Some(x), Some(y)) => if x != y { return false }, - } - } + a.eq(b) } /// Order `a` and `b` lexicographically using `Ord` - pub fn cmp(mut a: L, mut b: R) -> cmp::Ordering where + pub fn cmp(a: L, b: R) -> cmp::Ordering where A: Ord, L: Iterator, R: Iterator, { - loop { - match (a.next(), b.next()) { - (None, None) => return Equal, - (None, _ ) => return Less, - (_ , None) => return Greater, - (Some(x), Some(y)) => match x.cmp(&y) { - Equal => (), - non_eq => return non_eq, - }, - } - } + a.cmp(b) } /// Order `a` and `b` lexicographically using `PartialOrd` - pub fn partial_cmp(mut a: L, mut b: R) -> Option where + pub fn partial_cmp(a: L, b: R) -> Option where L::Item: PartialOrd { - loop { - match (a.next(), b.next()) { - (None, None) => return Some(Equal), - (None, _ ) => return Some(Less), - (_ , None) => return Some(Greater), - (Some(x), Some(y)) => match x.partial_cmp(&y) { - Some(Equal) => (), - non_eq => return non_eq, - }, - } - } + a.partial_cmp(b) } /// Compare `a` and `b` for equality (Using partial equality, `PartialEq`) - pub fn eq(mut a: L, mut b: R) -> bool where + pub fn eq(a: L, b: R) -> bool where L::Item: PartialEq, { - loop { - match (a.next(), b.next()) { - (None, None) => return true, - (None, _) | (_, None) => return false, - (Some(x), Some(y)) => if !x.eq(&y) { return false }, - } - } + a.eq(b) } /// Compares `a` and `b` for nonequality (Using partial equality, `PartialEq`) - pub fn ne(mut a: L, mut b: R) -> bool where + pub fn ne(a: L, b: R) -> bool where L::Item: PartialEq, { - loop { - match (a.next(), b.next()) { - (None, None) => return false, - (None, _) | (_, None) => return true, - (Some(x), Some(y)) => if x.ne(&y) { return true }, - } - } + a.ne(b) } /// Returns `a` < `b` lexicographically (Using partial order, `PartialOrd`) - pub fn lt(mut a: L, mut b: R) -> bool where + pub fn lt(a: L, b: R) -> bool where L::Item: PartialOrd, { - loop { - match (a.next(), b.next()) { - (None, None) => return false, - (None, _ ) => return true, - (_ , None) => return false, - (Some(x), Some(y)) => if x.ne(&y) { return x.lt(&y) }, - } - } + a.lt(b) } /// Returns `a` <= `b` lexicographically (Using partial order, `PartialOrd`) - pub fn le(mut a: L, mut b: R) -> bool where + pub fn le(a: L, b: R) -> bool where L::Item: PartialOrd, { - loop { - match (a.next(), b.next()) { - (None, None) => return true, - (None, _ ) => return true, - (_ , None) => return false, - (Some(x), Some(y)) => if x.ne(&y) { return x.le(&y) }, - } - } + a.le(b) } /// Returns `a` > `b` lexicographically (Using partial order, `PartialOrd`) - pub fn gt(mut a: L, mut b: R) -> bool where + pub fn gt(a: L, b: R) -> bool where L::Item: PartialOrd, { - loop { - match (a.next(), b.next()) { - (None, None) => return false, - (None, _ ) => return false, - (_ , None) => return true, - (Some(x), Some(y)) => if x.ne(&y) { return x.gt(&y) }, - } - } + a.gt(b) } /// Returns `a` >= `b` lexicographically (Using partial order, `PartialOrd`) - pub fn ge(mut a: L, mut b: R) -> bool where + pub fn ge(a: L, b: R) -> bool where L::Item: PartialOrd, { - loop { - match (a.next(), b.next()) { - (None, None) => return true, - (None, _ ) => return false, - (_ , None) => return true, - (Some(x), Some(y)) => if x.ne(&y) { return x.ge(&y) }, - } - } + a.ge(b) } } diff --git a/src/libcore/num/flt2dec/bignum.rs b/src/libcore/num/flt2dec/bignum.rs index ee1f6ffdd0a..ee2ffbffab6 100644 --- a/src/libcore/num/flt2dec/bignum.rs +++ b/src/libcore/num/flt2dec/bignum.rs @@ -448,12 +448,10 @@ macro_rules! define_bignum { impl ::cmp::Ord for $name { fn cmp(&self, other: &$name) -> ::cmp::Ordering { use cmp::max; - use iter::order; - let sz = max(self.size, other.size); let lhs = self.base[..sz].iter().cloned().rev(); let rhs = other.base[..sz].iter().cloned().rev(); - order::cmp(lhs, rhs) + lhs.cmp(rhs) } } diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index e63eb9f4cf8..fdd5e61c8f2 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -1557,7 +1557,7 @@ impl Eq for [T] {} #[stable(feature = "rust1", since = "1.0.0")] impl Ord for [T] { fn cmp(&self, other: &[T]) -> Ordering { - order::cmp(self.iter(), other.iter()) + self.iter().cmp(other.iter()) } } @@ -1565,22 +1565,22 @@ impl Ord for [T] { impl PartialOrd for [T] { #[inline] fn partial_cmp(&self, other: &[T]) -> Option { - order::partial_cmp(self.iter(), other.iter()) + self.iter().partial_cmp(other.iter()) } #[inline] fn lt(&self, other: &[T]) -> bool { - order::lt(self.iter(), other.iter()) + self.iter().lt(other.iter()) } #[inline] fn le(&self, other: &[T]) -> bool { - order::le(self.iter(), other.iter()) + self.iter().le(other.iter()) } #[inline] fn ge(&self, other: &[T]) -> bool { - order::ge(self.iter(), other.iter()) + self.iter().ge(other.iter()) } #[inline] fn gt(&self, other: &[T]) -> bool { - order::gt(self.iter(), other.iter()) + self.iter().gt(other.iter()) } } diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 71aed040871..3ee4ce80bd5 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -882,7 +882,7 @@ impl<'a> DoubleEndedIterator for Components<'a> { #[stable(feature = "rust1", since = "1.0.0")] impl<'a> cmp::PartialEq for Components<'a> { fn eq(&self, other: &Components<'a>) -> bool { - iter::order::eq(self.clone(), other.clone()) + Iterator::eq(self.clone(), other.clone()) } } @@ -892,14 +892,14 @@ impl<'a> cmp::Eq for Components<'a> {} #[stable(feature = "rust1", since = "1.0.0")] impl<'a> cmp::PartialOrd for Components<'a> { fn partial_cmp(&self, other: &Components<'a>) -> Option { - iter::order::partial_cmp(self.clone(), other.clone()) + Iterator::partial_cmp(self.clone(), other.clone()) } } #[stable(feature = "rust1", since = "1.0.0")] impl<'a> cmp::Ord for Components<'a> { fn cmp(&self, other: &Components<'a>) -> cmp::Ordering { - iter::order::cmp(self.clone(), other.clone()) + Iterator::cmp(self.clone(), other.clone()) } } @@ -1162,14 +1162,14 @@ impl cmp::Eq for PathBuf {} #[stable(feature = "rust1", since = "1.0.0")] impl cmp::PartialOrd for PathBuf { fn partial_cmp(&self, other: &PathBuf) -> Option { - self.components().partial_cmp(&other.components()) + self.components().partial_cmp(other.components()) } } #[stable(feature = "rust1", since = "1.0.0")] impl cmp::Ord for PathBuf { fn cmp(&self, other: &PathBuf) -> cmp::Ordering { - self.components().cmp(&other.components()) + self.components().cmp(other.components()) } } @@ -1691,7 +1691,7 @@ impl<'a> fmt::Display for Display<'a> { #[stable(feature = "rust1", since = "1.0.0")] impl cmp::PartialEq for Path { fn eq(&self, other: &Path) -> bool { - iter::order::eq(self.components(), other.components()) + self.components().eq(other.components()) } } @@ -1701,14 +1701,14 @@ impl cmp::Eq for Path {} #[stable(feature = "rust1", since = "1.0.0")] impl cmp::PartialOrd for Path { fn partial_cmp(&self, other: &Path) -> Option { - self.components().partial_cmp(&other.components()) + self.components().partial_cmp(other.components()) } } #[stable(feature = "rust1", since = "1.0.0")] impl cmp::Ord for Path { fn cmp(&self, other: &Path) -> cmp::Ordering { - self.components().cmp(&other.components()) + self.components().cmp(other.components()) } }