Skip to content

Commit

Permalink
auto merge of #18756 : jbcrail/rust/add-enum-set-bitxor, r=alexcrichton
Browse files Browse the repository at this point in the history
I implemented BitXor, and also added tests for BitAnd and BitXor.

cc #18424
  • Loading branch information
bors committed Nov 9, 2014
2 parents 946225d + 12db4de commit eeca3c7
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/libcollections/enum_set.rs
Expand Up @@ -16,7 +16,6 @@
use core::prelude::*;
use core::fmt;

// FIXME(conventions): implement BitXor
// FIXME(contentions): implement union family of methods? (general design may be wrong here)

#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -201,6 +200,12 @@ impl<E:CLike> BitAnd<EnumSet<E>, EnumSet<E>> for EnumSet<E> {
}
}

impl<E:CLike> BitXor<EnumSet<E>, EnumSet<E>> for EnumSet<E> {
fn bitxor(&self, e: &EnumSet<E>) -> EnumSet<E> {
EnumSet {bits: self.bits ^ e.bits}
}
}

/// An iterator over an EnumSet
pub struct Items<E> {
index: uint,
Expand Down Expand Up @@ -433,9 +438,29 @@ mod test {
let elems = e_intersection.iter().collect();
assert_eq!(vec![C], elems)

// Another way to express intersection
let e_intersection = e1 - (e1 - e2);
let elems = e_intersection.iter().collect();
assert_eq!(vec![C], elems)

let e_subtract = e1 - e2;
let elems = e_subtract.iter().collect();
assert_eq!(vec![A], elems)

// Bitwise XOR of two sets, aka symmetric difference
let e_symmetric_diff = e1 ^ e2;
let elems = e_symmetric_diff.iter().collect();
assert_eq!(vec![A,B], elems)

// Another way to express symmetric difference
let e_symmetric_diff = (e1 - e2) | (e2 - e1);
let elems = e_symmetric_diff.iter().collect();
assert_eq!(vec![A,B], elems)

// Yet another way to express symmetric difference
let e_symmetric_diff = (e1 | e2) - (e1 & e2);
let elems = e_symmetric_diff.iter().collect();
assert_eq!(vec![A,B], elems)
}

#[test]
Expand Down

0 comments on commit eeca3c7

Please sign in to comment.