Skip to content

Commit db3989c

Browse files
committed
Implement BitOps for HashSet
1 parent 34d6800 commit db3989c

File tree

1 file changed

+115
-4
lines changed
  • src/libstd/collections/hash

1 file changed

+115
-4
lines changed

src/libstd/collections/hash/set.rs

+115-4
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,20 @@
1111
// ignore-lexer-test FIXME #15883
1212

1313
use borrow::BorrowFrom;
14+
use clone::Clone;
1415
use cmp::{Eq, Equiv, PartialEq};
1516
use core::kinds::Sized;
1617
use default::Default;
1718
use fmt::Show;
1819
use fmt;
1920
use hash::{Hash, Hasher, RandomSipHasher};
20-
use iter::{Iterator, IteratorExt, FromIterator, Map, Chain, Extend};
21+
use iter::{Iterator, IteratorExt, IteratorCloneExt, FromIterator, Map, Chain, Extend};
22+
use ops::{BitOr, BitAnd, BitXor, Sub};
2123
use option::Option::{Some, None, mod};
2224
use result::Result::{Ok, Err};
2325

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

26-
// FIXME(conventions): implement BitOr, BitAnd, BitXor, and Sub
27-
28-
2928
// Future Optimization (FIXME!)
3029
// =============================
3130
//
@@ -618,6 +617,118 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Default for HashSet<T, H> {
618617
}
619618
}
620619

620+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
621+
impl<'a, 'b, T: Eq + Hash<S> + Clone, S, H: Hasher<S> + Default>
622+
BitOr<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
623+
/// Returns the union of `self` and `rhs` as a new `HashSet<T, H>`.
624+
///
625+
/// # Examples
626+
///
627+
/// ```
628+
/// use std::collections::HashSet;
629+
///
630+
/// let a: HashSet<int> = vec![1, 2, 3].into_iter().collect();
631+
/// let b: HashSet<int> = vec![3, 4, 5].into_iter().collect();
632+
///
633+
/// let set: HashSet<int> = &a | &b;
634+
///
635+
/// let mut i = 0;
636+
/// let expected = [1, 2, 3, 4, 5];
637+
/// for x in set.iter() {
638+
/// assert!(expected.contains(x));
639+
/// i += 1;
640+
/// }
641+
/// assert_eq!(i, expected.len());
642+
/// ```
643+
fn bitor(self, rhs: &HashSet<T, H>) -> HashSet<T, H> {
644+
self.union(rhs).cloned().collect()
645+
}
646+
}
647+
648+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
649+
impl<'a, 'b, T: Eq + Hash<S> + Clone, S, H: Hasher<S> + Default>
650+
BitAnd<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
651+
/// Returns the intersection of `self` and `rhs` as a new `HashSet<T, H>`.
652+
///
653+
/// # Examples
654+
///
655+
/// ```
656+
/// use std::collections::HashSet;
657+
///
658+
/// let a: HashSet<int> = vec![1, 2, 3].into_iter().collect();
659+
/// let b: HashSet<int> = vec![2, 3, 4].into_iter().collect();
660+
///
661+
/// let set: HashSet<int> = &a & &b;
662+
///
663+
/// let mut i = 0;
664+
/// let expected = [2, 3];
665+
/// for x in set.iter() {
666+
/// assert!(expected.contains(x));
667+
/// i += 1;
668+
/// }
669+
/// assert_eq!(i, expected.len());
670+
/// ```
671+
fn bitand(self, rhs: &HashSet<T, H>) -> HashSet<T, H> {
672+
self.intersection(rhs).cloned().collect()
673+
}
674+
}
675+
676+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
677+
impl<'a, 'b, T: Eq + Hash<S> + Clone, S, H: Hasher<S> + Default>
678+
BitXor<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
679+
/// Returns the symmetric difference of `self` and `rhs` as a new `HashSet<T, H>`.
680+
///
681+
/// # Examples
682+
///
683+
/// ```
684+
/// use std::collections::HashSet;
685+
///
686+
/// let a: HashSet<int> = vec![1, 2, 3].into_iter().collect();
687+
/// let b: HashSet<int> = vec![3, 4, 5].into_iter().collect();
688+
///
689+
/// let set: HashSet<int> = &a ^ &b;
690+
///
691+
/// let mut i = 0;
692+
/// let expected = [1, 2, 4, 5];
693+
/// for x in set.iter() {
694+
/// assert!(expected.contains(x));
695+
/// i += 1;
696+
/// }
697+
/// assert_eq!(i, expected.len());
698+
/// ```
699+
fn bitxor(self, rhs: &HashSet<T, H>) -> HashSet<T, H> {
700+
self.symmetric_difference(rhs).cloned().collect()
701+
}
702+
}
703+
704+
#[unstable = "matches collection reform specification, waiting for dust to settle"]
705+
impl<'a, 'b, T: Eq + Hash<S> + Clone, S, H: Hasher<S> + Default>
706+
Sub<&'b HashSet<T, H>, HashSet<T, H>> for &'a HashSet<T, H> {
707+
/// Returns the difference of `self` and `rhs` as a new `HashSet<T, H>`.
708+
///
709+
/// # Examples
710+
///
711+
/// ```
712+
/// use std::collections::HashSet;
713+
///
714+
/// let a: HashSet<int> = vec![1, 2, 3].into_iter().collect();
715+
/// let b: HashSet<int> = vec![3, 4, 5].into_iter().collect();
716+
///
717+
/// let set: HashSet<int> = &a - &b;
718+
///
719+
/// let mut i = 0;
720+
/// let expected = [1, 2];
721+
/// for x in set.iter() {
722+
/// assert!(expected.contains(x));
723+
/// i += 1;
724+
/// }
725+
/// assert_eq!(i, expected.len());
726+
/// ```
727+
fn sub(self, rhs: &HashSet<T, H>) -> HashSet<T, H> {
728+
self.difference(rhs).cloned().collect()
729+
}
730+
}
731+
621732
/// HashSet iterator
622733
pub struct Iter<'a, K: 'a> {
623734
iter: Keys<'a, K, ()>

0 commit comments

Comments
 (0)