Skip to content

Commit

Permalink
Merge pull request #41 from sunshowers/difference-with
Browse files Browse the repository at this point in the history
Implement difference_with operation
  • Loading branch information
jrraymond committed Apr 19, 2020
2 parents 611e8f4 + 779b400 commit 4d39d16
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/lib.rs
Expand Up @@ -334,6 +334,8 @@ impl FixedBitSet
}

/// In-place union of two `FixedBitSet`s.
///
/// On calling this method, `self`'s capacity may be increased to match `other`'s.
pub fn union_with(&mut self, other: &FixedBitSet)
{
if other.len() >= self.len() {
Expand All @@ -345,6 +347,8 @@ impl FixedBitSet
}

/// In-place intersection of two `FixedBitSet`s.
///
/// On calling this method, `self`'s capacity will remain the same as before.
pub fn intersect_with(&mut self, other: &FixedBitSet)
{
for (x, y) in self.data.iter_mut().zip(other.data.iter()) {
Expand All @@ -356,7 +360,26 @@ impl FixedBitSet
}
}

/// In-place difference of two `FixedBitSet`s.
///
/// On calling this method, `self`'s capacity will remain the same as before.
pub fn difference_with(&mut self, other: &FixedBitSet)
{
for (x, y) in self.data.iter_mut().zip(other.data.iter()) {
*x &= !*y;
}

// There's no need to grow self or do any other adjustments.
//
// * If self is longer than other, the bits at the end of self won't be affected since other
// has them implicitly set to 0.
// * If other is longer than self, the bits at the end of other are irrelevant since self
// has them set to 0 anyway.
}

/// In-place symmetric difference of two `FixedBitSet`s.
///
/// On calling this method, `self`'s capacity may be increased to match `other`'s.
pub fn symmetric_difference_with(&mut self, other: &FixedBitSet)
{
if other.len() >= self.len() {
Expand Down Expand Up @@ -1055,7 +1078,7 @@ fn intersection() {
a.set_range(..a_end, true);
b.set_range(b_start.., true);

let ab = a.intersection(&b).collect::<FixedBitSet>();
let mut ab = a.intersection(&b).collect::<FixedBitSet>();

for i in 0..b_start {
assert!(!ab.contains(i));
Expand All @@ -1066,6 +1089,11 @@ fn intersection() {
for i in a_end..len {
assert!(!ab.contains(i));
}

a.intersect_with(&b);
// intersection + collect produces the same results but with a shorter length.
ab.grow(a.len());
assert_eq!(ab, a, "intersection and intersect_with produce the same results");
}

#[test]
Expand All @@ -1088,6 +1116,9 @@ fn union() {
for i in b_end..a_start {
assert!(!ab.contains(i));
}

a.union_with(&b);
assert_eq!(ab, a, "union and union_with produce the same results");
}

#[test]
Expand All @@ -1101,13 +1132,18 @@ fn difference() {
let mut b = FixedBitSet::with_capacity(b_len);
a.set_range(a_start..a_end, true);
b.set_range(b_start..b_len, true);
let a_diff_b = a.difference(&b).collect::<FixedBitSet>();
let mut a_diff_b = a.difference(&b).collect::<FixedBitSet>();
for i in a_start..b_start {
assert!(a_diff_b.contains(i));
}
for i in b_start..b_len {
assert!(!a_diff_b.contains(i));
}

a.difference_with(&b);
// difference + collect produces the same results but with a shorter length.
a_diff_b.grow(a.len());
assert_eq!(a_diff_b, a, "difference and difference_with produce the same results");
}

#[test]
Expand All @@ -1134,6 +1170,9 @@ fn symmetric_difference() {
for i in a_end..b_len {
assert!(a_sym_diff_b.contains(i));
}

a.symmetric_difference_with(&b);
assert_eq!(a_sym_diff_b, a, "symmetric_difference and _with produce the same results");
}

#[test]
Expand Down

0 comments on commit 4d39d16

Please sign in to comment.