Skip to content

Commit

Permalink
Implement BitOps for HashSet
Browse files Browse the repository at this point in the history
  • Loading branch information
csouth3 committed Dec 22, 2014
1 parent 34d6800 commit db3989c
Showing 1 changed file with 115 additions and 4 deletions.
119 changes: 115 additions & 4 deletions src/libstd/collections/hash/set.rs
Expand Up @@ -11,21 +11,20 @@
// ignore-lexer-test FIXME #15883

use borrow::BorrowFrom;
use clone::Clone;
use cmp::{Eq, Equiv, PartialEq};
use core::kinds::Sized;
use default::Default;
use fmt::Show;
use fmt;
use hash::{Hash, Hasher, RandomSipHasher};
use iter::{Iterator, IteratorExt, FromIterator, Map, Chain, Extend};
use iter::{Iterator, IteratorExt, IteratorCloneExt, FromIterator, Map, Chain, Extend};
use ops::{BitOr, BitAnd, BitXor, Sub};
use option::Option::{Some, None, mod};
use result::Result::{Ok, Err};

use super::map::{mod, HashMap, MoveEntries, Keys, INITIAL_CAPACITY};

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


// Future Optimization (FIXME!)
// =============================
//
Expand Down Expand Up @@ -618,6 +617,118 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Default for HashSet<T, H> {
}
}

#[unstable = "matches collection reform specification, waiting for dust to settle"]
impl<'a, 'b, T: Eq + Hash<S> + Clone, S, H: Hasher<S> + Default>
BitOr<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
/// Returns the union of `self` and `rhs` as a new `HashSet<T, H>`.
///
/// # Examples
///
/// ```
/// use std::collections::HashSet;
///
/// let a: HashSet<int> = vec![1, 2, 3].into_iter().collect();
/// let b: HashSet<int> = vec![3, 4, 5].into_iter().collect();
///
/// let set: HashSet<int> = &a | &b;
///
/// let mut i = 0;
/// let expected = [1, 2, 3, 4, 5];
/// for x in set.iter() {
/// assert!(expected.contains(x));
/// i += 1;
/// }
/// assert_eq!(i, expected.len());
/// ```
fn bitor(self, rhs: &HashSet<T, H>) -> HashSet<T, H> {
self.union(rhs).cloned().collect()
}
}

#[unstable = "matches collection reform specification, waiting for dust to settle"]
impl<'a, 'b, T: Eq + Hash<S> + Clone, S, H: Hasher<S> + Default>
BitAnd<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
/// Returns the intersection of `self` and `rhs` as a new `HashSet<T, H>`.
///
/// # Examples
///
/// ```
/// use std::collections::HashSet;
///
/// let a: HashSet<int> = vec![1, 2, 3].into_iter().collect();
/// let b: HashSet<int> = vec![2, 3, 4].into_iter().collect();
///
/// let set: HashSet<int> = &a & &b;
///
/// let mut i = 0;
/// let expected = [2, 3];
/// for x in set.iter() {
/// assert!(expected.contains(x));
/// i += 1;
/// }
/// assert_eq!(i, expected.len());
/// ```
fn bitand(self, rhs: &HashSet<T, H>) -> HashSet<T, H> {
self.intersection(rhs).cloned().collect()
}
}

#[unstable = "matches collection reform specification, waiting for dust to settle"]
impl<'a, 'b, T: Eq + Hash<S> + Clone, S, H: Hasher<S> + Default>
BitXor<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
/// Returns the symmetric difference of `self` and `rhs` as a new `HashSet<T, H>`.
///
/// # Examples
///
/// ```
/// use std::collections::HashSet;
///
/// let a: HashSet<int> = vec![1, 2, 3].into_iter().collect();
/// let b: HashSet<int> = vec![3, 4, 5].into_iter().collect();
///
/// let set: HashSet<int> = &a ^ &b;
///
/// let mut i = 0;
/// let expected = [1, 2, 4, 5];
/// for x in set.iter() {
/// assert!(expected.contains(x));
/// i += 1;
/// }
/// assert_eq!(i, expected.len());
/// ```
fn bitxor(self, rhs: &HashSet<T, H>) -> HashSet<T, H> {
self.symmetric_difference(rhs).cloned().collect()
}
}

#[unstable = "matches collection reform specification, waiting for dust to settle"]
impl<'a, 'b, T: Eq + Hash<S> + Clone, S, H: Hasher<S> + Default>
Sub<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
/// Returns the difference of `self` and `rhs` as a new `HashSet<T, H>`.
///
/// # Examples
///
/// ```
/// use std::collections::HashSet;
///
/// let a: HashSet<int> = vec![1, 2, 3].into_iter().collect();
/// let b: HashSet<int> = vec![3, 4, 5].into_iter().collect();
///
/// let set: HashSet<int> = &a - &b;
///
/// let mut i = 0;
/// let expected = [1, 2];
/// for x in set.iter() {
/// assert!(expected.contains(x));
/// i += 1;
/// }
/// assert_eq!(i, expected.len());
/// ```
fn sub(self, rhs: &HashSet<T, H>) -> HashSet<T, H> {
self.difference(rhs).cloned().collect()
}
}

/// HashSet iterator
pub struct Iter<'a, K: 'a> {
iter: Keys<'a, K, ()>
Expand Down

0 comments on commit db3989c

Please sign in to comment.