use clone::Clone;
use cmp;
-use cmp::{Ord, PartialOrd, PartialEq};
+use cmp::{Ord, PartialOrd, PartialEq, Ordering};
use default::Default;
use marker;
use mem;
{
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<I>(mut self, other: I) -> Ordering where
+ I: IntoIterator<Item = Self::Item>,
+ 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<I>(mut self, other: I) -> Option<Ordering> where
+ I: IntoIterator,
+ Self::Item: PartialOrd<I::Item>,
+ 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<I>(mut self, other: I) -> bool where
+ I: IntoIterator,
+ Self::Item: PartialEq<I::Item>,
+ 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<I>(mut self, other: I) -> bool where
+ I: IntoIterator,
+ Self::Item: PartialEq<I::Item>,
+ 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<I>(mut self, other: I) -> bool where
+ I: IntoIterator,
+ Self::Item: PartialOrd<I::Item>,
+ 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<I>(mut self, other: I) -> bool where
+ I: IntoIterator,
+ Self::Item: PartialOrd<I::Item>,
+ 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<I>(mut self, other: I) -> bool where
+ I: IntoIterator,
+ Self::Item: PartialOrd<I::Item>,
+ 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<I>(mut self, other: I) -> bool where
+ I: IntoIterator,
+ Self::Item: PartialOrd<I::Item>,
+ 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
///
/// 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<A, L, R>(mut a: L, mut b: R) -> bool where
+ pub fn equals<A, L, R>(a: L, b: R) -> bool where
A: Eq,
L: Iterator<Item=A>,
R: Iterator<Item=A>,
{
- 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<A, L, R>(mut a: L, mut b: R) -> cmp::Ordering where
+ pub fn cmp<A, L, R>(a: L, b: R) -> cmp::Ordering where
A: Ord,
L: Iterator<Item=A>,
R: Iterator<Item=A>,
{
- 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<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> Option<cmp::Ordering> where
+ pub fn partial_cmp<L: Iterator, R: Iterator>(a: L, b: R) -> Option<cmp::Ordering> where
L::Item: PartialOrd<R::Item>
{
- 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<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> bool where
+ pub fn eq<L: Iterator, R: Iterator>(a: L, b: R) -> bool where
L::Item: PartialEq<R::Item>,
{
- 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<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> bool where
+ pub fn ne<L: Iterator, R: Iterator>(a: L, b: R) -> bool where
L::Item: PartialEq<R::Item>,
{
- 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<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> bool where
+ pub fn lt<L: Iterator, R: Iterator>(a: L, b: R) -> bool where
L::Item: PartialOrd<R::Item>,
{
- 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<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> bool where
+ pub fn le<L: Iterator, R: Iterator>(a: L, b: R) -> bool where
L::Item: PartialOrd<R::Item>,
{
- 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<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> bool where
+ pub fn gt<L: Iterator, R: Iterator>(a: L, b: R) -> bool where
L::Item: PartialOrd<R::Item>,
{
- 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<L: Iterator, R: Iterator>(mut a: L, mut b: R) -> bool where
+ pub fn ge<L: Iterator, R: Iterator>(a: L, b: R) -> bool where
L::Item: PartialOrd<R::Item>,
{
- 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)
}
}