Skip to content

Commit

Permalink
Implement PartialOrd for Bitv and BitvSet
Browse files Browse the repository at this point in the history
  • Loading branch information
nham committed Jul 28, 2014
1 parent 220f8f6 commit 935c88c
Showing 1 changed file with 40 additions and 1 deletion.
41 changes: 40 additions & 1 deletion src/libcollections/bitv.rs
Expand Up @@ -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;
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 935c88c

Please sign in to comment.