|
11 | 11 | // ignore-lexer-test FIXME #15883
|
12 | 12 |
|
13 | 13 | use borrow::BorrowFrom;
|
| 14 | +use clone::Clone; |
14 | 15 | use cmp::{Eq, Equiv, PartialEq};
|
15 | 16 | use core::kinds::Sized;
|
16 | 17 | use default::Default;
|
17 | 18 | use fmt::Show;
|
18 | 19 | use fmt;
|
19 | 20 | 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}; |
21 | 23 | use option::Option::{Some, None, mod};
|
22 | 24 | use result::Result::{Ok, Err};
|
23 | 25 |
|
24 | 26 | use super::map::{mod, HashMap, MoveEntries, Keys, INITIAL_CAPACITY};
|
25 | 27 |
|
26 |
| -// FIXME(conventions): implement BitOr, BitAnd, BitXor, and Sub |
27 |
| - |
28 |
| - |
29 | 28 | // Future Optimization (FIXME!)
|
30 | 29 | // =============================
|
31 | 30 | //
|
@@ -618,6 +617,118 @@ impl<T: Eq + Hash<S>, S, H: Hasher<S> + Default> Default for HashSet<T, H> {
|
618 | 617 | }
|
619 | 618 | }
|
620 | 619 |
|
| 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 | + |
621 | 732 | /// HashSet iterator
|
622 | 733 | pub struct Iter<'a, K: 'a> {
|
623 | 734 | iter: Keys<'a, K, ()>
|
|
0 commit comments