From 935c88ce1c863b6bfa3e4a295e6999a5593c08e2 Mon Sep 17 00:00:00 2001 From: nham Date: Mon, 28 Jul 2014 00:28:49 -0400 Subject: [PATCH] Implement PartialOrd for Bitv and BitvSet --- src/libcollections/bitv.rs | 41 +++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs index 44af3d52db982..55caf807b4fd4 100644 --- a/src/libcollections/bitv.rs +++ b/src/libcollections/bitv.rs @@ -67,6 +67,7 @@ use core::cmp; use core::default::Default; use core::fmt; use core::iter::Take; +use core::iter; use core::ops::Index; use core::slice; use core::uint; @@ -830,6 +831,13 @@ impl Clone for Bitv { } } +impl PartialOrd for Bitv { + #[inline] + fn partial_cmp(&self, other: &Bitv) -> Option { + iter::order::partial_cmp(self.iter(), other.iter()) + } +} + impl fmt::Show for Bitv { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { for bit in self.iter() { @@ -955,7 +963,7 @@ impl<'a> RandomAccessIterator for Bits<'a> { /// assert!(bv.eq_vec([true, true, false, true, /// false, false, false, false])); /// ``` -#[deriving(Clone, PartialEq, Eq)] +#[deriving(Clone, PartialEq, Eq, PartialOrd)] pub struct BitvSet(Bitv); impl Default for BitvSet { @@ -2189,6 +2197,37 @@ mod tests { assert_eq!(a.capacity(), uint::BITS); } + #[test] + fn test_bitv_lt() { + let mut a = Bitv::with_capacity(5u, false); + let mut b = Bitv::with_capacity(5u, false); + + assert!(!(a < b) && !(b < a)); + b.set(2, true); + assert!(a < b); + a.set(3, true); + assert!(a < b); + a.set(2, true); + assert!(!(a < b) && b < a); + b.set(0, true); + assert!(a < b); + } + + #[test] + fn test_ord() { + let mut a = Bitv::with_capacity(5u, false); + let mut b = Bitv::with_capacity(5u, false); + + assert!(a <= b && a >= b); + a.set(1, true); + assert!(a > b && a >= b); + assert!(b < a && b <= a); + b.set(1, true); + b.set(2, true); + assert!(b > a && b >= a); + assert!(a < b && a <= b); + } + #[test] fn test_bitv_clone() { let mut a = BitvSet::new();