about summary refs log tree commit diff
diff options
context:
space:
mode:
authornham <hamann.nick@gmail.com>2014-07-28 00:28:49 -0400
committernham <hamann.nick@gmail.com>2014-07-28 00:28:49 -0400
commit935c88ce1c863b6bfa3e4a295e6999a5593c08e2 (patch)
tree5d61b606d5f419dfef51dcb05319e46a2ab766d5
parent220f8f6dcb37fd4db4d489337890a4d900e50503 (diff)
downloadrust-935c88ce1c863b6bfa3e4a295e6999a5593c08e2.tar.gz
rust-935c88ce1c863b6bfa3e4a295e6999a5593c08e2.zip
Implement PartialOrd for Bitv and BitvSet
-rw-r--r--src/libcollections/bitv.rs41
1 files changed, 40 insertions, 1 deletions
diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs
index 44af3d52db9..55caf807b4f 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<Ordering> {
+        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<bool> 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 {
@@ -2190,6 +2198,37 @@ mod tests {
     }
 
     #[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();