Implement Eq for Bitv and BitvSet

This commit is contained in:
Kasey Carrothers 2014-06-19 21:13:39 -07:00
parent 282705c784
commit e1038819c2

View File

@ -376,27 +376,6 @@ impl Bitv {
}
}
/**
* Compares two bitvectors
*
* Both bitvectors must be the same length. Returns `true` if both
* bitvectors contain identical elements.
*/
#[inline]
pub fn equal(&self, v1: &Bitv) -> bool {
if self.nbits != v1.nbits { return false; }
match self.rep {
Small(ref b) => match v1.rep {
Small(ref b1) => b.equals(b1, self.nbits),
_ => false
},
Big(ref s) => match v1.rep {
Big(ref s1) => s.equals(s1, self.nbits),
Small(_) => return false
}
}
}
/// Set all bits to 0
#[inline]
pub fn clear(&mut self) {
@ -613,6 +592,25 @@ impl<S: hash::Writer> hash::Hash<S> for Bitv {
}
}
impl cmp::PartialEq for Bitv {
#[inline]
fn eq(&self, other: &Bitv) -> bool {
if self.nbits != other.nbits { return false; }
match self.rep {
Small(ref b) => match other.rep {
Small(ref b1) => b.equals(b1, self.nbits),
_ => false
},
Big(ref s) => match other.rep {
Big(ref s1) => s.equals(s1, self.nbits),
Small(_) => return false
}
}
}
}
impl cmp::Eq for Bitv {}
#[inline]
fn iterate_bits(base: uint, bits: uint, f: |uint| -> bool) -> bool {
if bits == 0 {
@ -841,6 +839,8 @@ impl cmp::PartialEq for BitvSet {
fn ne(&self, other: &BitvSet) -> bool { !self.eq(other) }
}
impl cmp::Eq for BitvSet {}
impl fmt::Show for BitvSet {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
try!(write!(fmt, "{{"));
@ -1323,14 +1323,14 @@ mod tests {
fn test_equal_differing_sizes() {
let v0 = Bitv::new(10u, false);
let v1 = Bitv::new(11u, false);
assert!(!v0.equal(&v1));
assert!(v0 != v1);
}
#[test]
fn test_equal_greatly_differing_sizes() {
let v0 = Bitv::new(10u, false);
let v1 = Bitv::new(110u, false);
assert!(!v0.equal(&v1));
assert!(v0 != v1);
}
#[test]
@ -1341,7 +1341,7 @@ mod tests {
let mut b = bitv::Bitv::new(1, true);
b.set(0, true);
assert!(a.equal(&b));
assert_eq!(a, b);
}
#[test]
@ -1356,7 +1356,7 @@ mod tests {
b.set(i, true);
}
assert!(a.equal(&b));
assert_eq!(a, b);
}
#[test]