From da50db0ca27036d0c5e7c5f99a37ef3d932268f1 Mon Sep 17 00:00:00 2001 From: Chase Southwood Date: Fri, 5 Dec 2014 01:14:43 -0600 Subject: [PATCH] Implement BitOps for TreeSet --- src/libcollections/tree/set.rs | 126 ++++++++++++++++++++++++++++++++- 1 file changed, 125 insertions(+), 1 deletion(-) diff --git a/src/libcollections/tree/set.rs b/src/libcollections/tree/set.rs index 6988b929df6..cf6a534cf19 100644 --- a/src/libcollections/tree/set.rs +++ b/src/libcollections/tree/set.rs @@ -21,7 +21,6 @@ use std::hash::{Writer, Hash}; use tree_map::{TreeMap, Entries, RevEntries, MoveEntries}; // FIXME(conventions): implement bounded iterators -// FIXME(conventions): implement BitOr, BitAnd, BitXor, and Sub // FIXME(conventions): replace rev_iter(_mut) by making iter(_mut) DoubleEnded /// An implementation of the `Set` trait on top of the `TreeMap` container. The @@ -666,6 +665,90 @@ impl<'a, T: Ord> Iterator<&'a T> for UnionItems<'a, T> { } } +#[unstable = "matches collection reform specification, waiting for dust to settle"] +impl BitOr, TreeSet> for TreeSet { + /// Returns the union of `self` and `rhs` as a new `TreeSet`. + /// + /// # Example + /// + /// ``` + /// use std::collections::TreeSet; + /// + /// let a: TreeSet = vec![1, 2, 3].into_iter().collect(); + /// let b: TreeSet = vec![3, 4, 5].into_iter().collect(); + /// + /// let set: TreeSet = a | b; + /// let v: Vec = set.into_iter().collect(); + /// assert_eq!(v, vec![1, 2, 3, 4, 5]); + /// ``` + fn bitor(&self, rhs: &TreeSet) -> TreeSet { + self.union(rhs).cloned().collect() + } +} + +#[unstable = "matches collection reform specification, waiting for dust to settle"] +impl BitAnd, TreeSet> for TreeSet { + /// Returns the intersection of `self` and `rhs` as a new `TreeSet`. + /// + /// # Example + /// + /// ``` + /// use std::collections::TreeSet; + /// + /// let a: TreeSet = vec![1, 2, 3].into_iter().collect(); + /// let b: TreeSet = vec![2, 3, 4].into_iter().collect(); + /// + /// let set: TreeSet = a & b; + /// let v: Vec = set.into_iter().collect(); + /// assert_eq!(v, vec![2, 3]); + /// ``` + fn bitand(&self, rhs: &TreeSet) -> TreeSet { + self.intersection(rhs).cloned().collect() + } +} + +#[unstable = "matches collection reform specification, waiting for dust to settle"] +impl BitXor, TreeSet> for TreeSet { + /// Returns the symmetric difference of `self` and `rhs` as a new `TreeSet`. + /// + /// # Example + /// + /// ``` + /// use std::collections::TreeSet; + /// + /// let a: TreeSet = vec![1, 2, 3].into_iter().collect(); + /// let b: TreeSet = vec![3, 4, 5].into_iter().collect(); + /// + /// let set: TreeSet = a ^ b; + /// let v: Vec = set.into_iter().collect(); + /// assert_eq!(v, vec![1, 2, 4, 5]); + /// ``` + fn bitxor(&self, rhs: &TreeSet) -> TreeSet { + self.symmetric_difference(rhs).cloned().collect() + } +} + +#[unstable = "matches collection reform specification, waiting for dust to settle"] +impl Sub, TreeSet> for TreeSet { + /// Returns the difference of `self` and `rhs` as a new `TreeSet`. + /// + /// # Example + /// + /// ``` + /// use std::collections::TreeSet; + /// + /// let a: TreeSet = vec![1, 2, 3].into_iter().collect(); + /// let b: TreeSet = vec![3, 4, 5].into_iter().collect(); + /// + /// let set: TreeSet = a - b; + /// let v: Vec = set.into_iter().collect(); + /// assert_eq!(v, vec![1, 2]); + /// ``` + fn sub(&self, rhs: &TreeSet) -> TreeSet { + self.difference(rhs).cloned().collect() + } +} + impl FromIterator for TreeSet { fn from_iter>(iter: Iter) -> TreeSet { let mut set = TreeSet::new(); @@ -695,6 +778,7 @@ impl> Hash for TreeSet { mod test { use std::prelude::*; use std::hash; + use vec::Vec; use super::TreeSet; @@ -932,6 +1016,46 @@ mod test { &[-2, 1, 3, 5, 9, 11, 13, 16, 19, 24]); } + #[test] + fn test_bit_or() { + let a: TreeSet = vec![1, 3, 5, 9, 11, 16, 19, 24].into_iter().collect(); + let b: TreeSet = vec![-2, 1, 5, 9, 13, 19].into_iter().collect(); + + let set: TreeSet = a | b; + let v: Vec = set.into_iter().collect(); + assert_eq!(v, vec![-2, 1, 3, 5, 9, 11, 13, 16, 19, 24]); + } + + #[test] + fn test_bit_and() { + let a: TreeSet = vec![11, 1, 3, 77, 103, 5, -5].into_iter().collect(); + let b: TreeSet = vec![2, 11, 77, -9, -42, 5, 3].into_iter().collect(); + + let set: TreeSet = a & b; + let v: Vec = set.into_iter().collect(); + assert_eq!(v, vec![3, 5, 11, 77]); + } + + #[test] + fn test_bit_xor() { + let a: TreeSet = vec![1, 3, 5, 9, 11].into_iter().collect(); + let b: TreeSet = vec![-2, 3, 9, 14, 22].into_iter().collect(); + + let set: TreeSet = a ^ b; + let v: Vec = set.into_iter().collect(); + assert_eq!(v, vec![-2, 1, 5, 11, 14, 22]); + } + + #[test] + fn test_sub() { + let a: TreeSet = vec![-5, 11, 22, 33, 40, 42].into_iter().collect(); + let b: TreeSet = vec![-12, -5, 14, 23, 34, 38, 39, 50].into_iter().collect(); + + let set: TreeSet = a - b; + let v: Vec = set.into_iter().collect(); + assert_eq!(v, vec![11, 22, 33, 40, 42]); + } + #[test] fn test_zip() { let mut x = TreeSet::new();