Skip to content

Commit

Permalink
Add an implementation of the BitOps for BTreeSets.
Browse files Browse the repository at this point in the history
Add initial attempt at implementing BitOr for BTreeSet.

Update the implementation of the bitor operator for BTreeSets.

`make check` ran fine through this.

Add implementations for BitAnd, BitXor, and Sub as well.

Remove the FIXME comment and add unstable flags.

Add doctests for the bitop functions.
  • Loading branch information
jbranchaud committed Dec 12, 2014
1 parent 207a508 commit cd008c4
Showing 1 changed file with 84 additions and 1 deletion.
85 changes: 84 additions & 1 deletion src/libcollections/btree/set.rs
Expand Up @@ -22,7 +22,6 @@ use core::iter::Peekable;
use core::fmt::Show;

// FIXME(conventions): implement bounded iterators
// FIXME(conventions): implement BitOr, BitAnd, BitXor, and Sub

/// A set based on a B-Tree.
///
Expand Down Expand Up @@ -340,6 +339,90 @@ impl<T: Ord> Default for BTreeSet<T> {
}
}

#[unstable = "matches collection reform specification, waiting for dust to settle"]
impl<T: Ord + Clone> Sub<BTreeSet<T>,BTreeSet<T>> for BTreeSet<T> {
/// Returns the difference of `self` and `rhs` as a new `BTreeSet<T>`.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let a: BTreeSet<int> = vec![1,2,3].into_iter().collect();
/// let b: BTreeSet<int> = vec![3,4,5].into_iter().collect();
///
/// let result: BTreeSet<int> = a - b;
/// let result_vec: Vec<int> = result.into_iter().collect();
/// assert_eq!(result_vec, vec![1,2]);
/// ```
fn sub(&self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
self.difference(rhs).cloned().collect()
}
}

#[unstable = "matches collection reform specification, waiting for dust to settle"]
impl<T: Ord + Clone> BitXor<BTreeSet<T>,BTreeSet<T>> for BTreeSet<T> {
/// Returns the symmetric difference of `self` and `rhs` as a new `BTreeSet<T>`.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let a: BTreeSet<int> = vec![1,2,3].into_iter().collect();
/// let b: BTreeSet<int> = vec![2,3,4].into_iter().collect();
///
/// let result: BTreeSet<int> = a ^ b;
/// let result_vec: Vec<int> = result.into_iter().collect();
/// assert_eq!(result_vec, vec![1,4]);
/// ```
fn bitxor(&self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
self.symmetric_difference(rhs).cloned().collect()
}
}

#[unstable = "matches collection reform specification, waiting for dust to settle"]
impl<T: Ord + Clone> BitAnd<BTreeSet<T>,BTreeSet<T>> for BTreeSet<T> {
/// Returns the intersection of `self` and `rhs` as a new `BTreeSet<T>`.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let a: BTreeSet<int> = vec![1,2,3].into_iter().collect();
/// let b: BTreeSet<int> = vec![2,3,4].into_iter().collect();
///
/// let result: BTreeSet<int> = a & b;
/// let result_vec: Vec<int> = result.into_iter().collect();
/// assert_eq!(result_vec, vec![2,3]);
/// ```
fn bitand(&self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
self.intersection(rhs).cloned().collect()
}
}

#[unstable = "matches collection reform specification, waiting for dust to settle"]
impl<T: Ord + Clone> BitOr<BTreeSet<T>,BTreeSet<T>> for BTreeSet<T> {
/// Returns the union of `self` and `rhs` as a new `BTreeSet<T>`.
///
/// # Examples
///
/// ```
/// use std::collections::BTreeSet;
///
/// let a: BTreeSet<int> = vec![1,2,3].into_iter().collect();
/// let b: BTreeSet<int> = vec![3,4,5].into_iter().collect();
///
/// let result: BTreeSet<int> = a | b;
/// let result_vec: Vec<int> = result.into_iter().collect();
/// assert_eq!(result_vec, vec![1,2,3,4,5]);
/// ```
fn bitor(&self, rhs: &BTreeSet<T>) -> BTreeSet<T> {
self.union(rhs).cloned().collect()
}
}

impl<T: Show> Show for BTreeSet<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
try!(write!(f, "{{"));
Expand Down

5 comments on commit cd008c4

@bors
Copy link
Contributor

@bors bors commented on cd008c4 Dec 12, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from Gankro
at jbranchaud@cd008c4

@bors
Copy link
Contributor

@bors bors commented on cd008c4 Dec 12, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging jbranchaud/rust/add-btree-set-bitor = cd008c4 into auto

@bors
Copy link
Contributor

@bors bors commented on cd008c4 Dec 12, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jbranchaud/rust/add-btree-set-bitor = cd008c4 merged ok, testing candidate = da83ad8

@bors
Copy link
Contributor

@bors bors commented on cd008c4 Dec 12, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = da83ad8

Please sign in to comment.