1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 The `Ord` and `Eq` comparison traits
15 This module contains the definition of both `Ord` and `Eq` which define
16 the common interfaces for doing comparison. Both are language items
17 that the compiler uses to implement the comparison operators. Rust code
18 may implement `Ord` to overload the `<`, `<=`, `>`, and `>=` operators,
19 and `Eq` to overload the `==` and `!=` operators.
23 #[allow(missing_doc)];
26 * Trait for values that can be compared for equality and inequality.
28 * This trait allows partial equality, where types can be unordered instead of strictly equal or
29 * unequal. For example, with the built-in floating-point types `a == b` and `a != b` will both
30 * evaluate to false if either `a` or `b` is NaN (cf. IEEE 754-2008 section 5.11).
32 * Eq only requires the `eq` method to be implemented; `ne` is its negation by default.
34 * Eventually, this will be implemented by default for types that implement `TotalEq`.
38 fn eq(&self, other: &Self) -> bool;
41 fn ne(&self, other: &Self) -> bool { !self.eq(other) }
44 /// Trait for equality comparisons where `a == b` and `a != b` are strict inverses.
45 pub trait TotalEq: Eq {
46 /// This method must return the same value as `eq`. It exists to prevent
47 /// deriving `TotalEq` from fields not implementing the `TotalEq` trait.
48 fn equals(&self, other: &Self) -> bool {
53 macro_rules! totaleq_impl(
57 fn equals(&self, other: &$t) -> bool { *self == *other }
79 #[deriving(Clone, Eq, Show)]
80 pub enum Ordering { Less = -1, Equal = 0, Greater = 1 }
82 /// Trait for types that form a total order
83 pub trait TotalOrd: TotalEq + Ord {
84 fn cmp(&self, other: &Self) -> Ordering;
87 impl TotalEq for Ordering {
89 fn equals(&self, other: &Ordering) -> bool {
93 impl TotalOrd for Ordering {
95 fn cmp(&self, other: &Ordering) -> Ordering {
96 (*self as int).cmp(&(*other as int))
100 impl Ord for Ordering {
102 fn lt(&self, other: &Ordering) -> bool { (*self as int) < (*other as int) }
105 macro_rules! totalord_impl(
107 impl TotalOrd for $t {
109 fn cmp(&self, other: &$t) -> Ordering {
110 if *self < *other { Less }
111 else if *self > *other { Greater }
133 /// Compares (a1, b1) against (a2, b2), where the a values are more significant.
134 pub fn cmp2<A:TotalOrd,B:TotalOrd>(
136 a2: &A, b2: &B) -> Ordering
146 Return `o1` if it is not `Equal`, otherwise `o2`. Simulates the
147 lexical ordering on a type `(int, int)`.
150 pub fn lexical_ordering(o1: Ordering, o2: Ordering) -> Ordering {
158 * Trait for values that can be compared for a sort-order.
160 * Ord only requires implementation of the `lt` method,
161 * with the others generated from default implementations.
163 * However it remains possible to implement the others separately,
164 * for compatibility with floating-point NaN semantics
165 * (cf. IEEE 754-2008 section 5.11).
169 fn lt(&self, other: &Self) -> bool;
171 fn le(&self, other: &Self) -> bool { !other.lt(self) }
173 fn gt(&self, other: &Self) -> bool { other.lt(self) }
175 fn ge(&self, other: &Self) -> bool { !self.lt(other) }
178 /// The equivalence relation. Two values may be equivalent even if they are
179 /// of different types. The most common use case for this relation is
180 /// container types; e.g. it is often desirable to be able to use `&str`
181 /// values to look up entries in a container with `~str` keys.
183 fn equiv(&self, other: &T) -> bool;
187 pub fn min<T:Ord>(v1: T, v2: T) -> T {
188 if v1 < v2 { v1 } else { v2 }
192 pub fn max<T:Ord>(v1: T, v2: T) -> T {
193 if v1 > v2 { v1 } else { v2 }
198 use super::lexical_ordering;
201 fn test_int_totalord() {
202 assert_eq!(5.cmp(&10), Less);
203 assert_eq!(10.cmp(&5), Greater);
204 assert_eq!(5.cmp(&5), Equal);
205 assert_eq!((-5).cmp(&12), Less);
206 assert_eq!(12.cmp(-5), Greater);
211 assert_eq!(cmp2(1, 2, 3, 4), Less);
212 assert_eq!(cmp2(3, 2, 3, 4), Less);
213 assert_eq!(cmp2(5, 2, 3, 4), Greater);
214 assert_eq!(cmp2(5, 5, 5, 4), Greater);
218 fn test_int_totaleq() {
219 assert!(5.equals(&5));
220 assert!(!2.equals(&17));
224 fn test_ordering_order() {
225 assert!(Less < Equal);
226 assert_eq!(Greater.cmp(&Less), Greater);
230 fn test_lexical_ordering() {
231 fn t(o1: Ordering, o2: Ordering, e: Ordering) {
232 assert_eq!(lexical_ordering(o1, o2), e);
235 let xs = [Less, Equal, Greater];
236 for &o in xs.iter() {
239 t(Greater, o, Greater);