Skip to content

Implement Eq for Bitv and BitvSet #15050

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 25 additions & 25 deletions src/libcollections/bitv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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, "{{"));
Expand Down Expand Up @@ -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]
Expand All @@ -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]
Expand All @@ -1356,7 +1356,7 @@ mod tests {
b.set(i, true);
}

assert!(a.equal(&b));
assert_eq!(a, b);
}

#[test]
Expand Down