@@ -5,7 +5,9 @@ use alloc::borrow::ToOwned;
5
5
use core:: fmt;
6
6
use core:: hash:: { BuildHasher , Hash } ;
7
7
use core:: iter:: { Chain , FusedIterator } ;
8
- use core:: ops:: { Add , BitAnd , BitOr , BitXor , Sub } ;
8
+ use core:: ops:: {
9
+ Add , AddAssign , BitAnd , BitAndAssign , BitOr , BitOrAssign , BitXor , BitXorAssign , Sub , SubAssign ,
10
+ } ;
9
11
10
12
use super :: map:: { self , DefaultHashBuilder , HashMap , Keys } ;
11
13
use crate :: raw:: { Allocator , Global , RawExtractIf } ;
@@ -1566,6 +1568,170 @@ where
1566
1568
}
1567
1569
}
1568
1570
1571
+ impl < T , S > BitOrAssign < & HashSet < T , S > > for HashSet < T , S >
1572
+ where
1573
+ T : Eq + Hash + Clone ,
1574
+ S : BuildHasher + Default ,
1575
+ {
1576
+ /// Modifies this set to contain the union of `self` and `rhs`.
1577
+ ///
1578
+ /// # Examples
1579
+ ///
1580
+ /// ```
1581
+ /// use hashbrown::HashSet;
1582
+ ///
1583
+ /// let mut a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
1584
+ /// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect();
1585
+ ///
1586
+ /// a |= &b;
1587
+ ///
1588
+ /// let mut i = 0;
1589
+ /// let expected = [1, 2, 3, 4, 5];
1590
+ /// for x in &a {
1591
+ /// assert!(expected.contains(x));
1592
+ /// i += 1;
1593
+ /// }
1594
+ /// assert_eq!(i, expected.len());
1595
+ /// ```
1596
+ fn bitor_assign ( & mut self , rhs : & HashSet < T , S > ) {
1597
+ for item in rhs {
1598
+ if !self . contains ( item) {
1599
+ self . insert ( item. clone ( ) ) ;
1600
+ }
1601
+ }
1602
+ }
1603
+ }
1604
+
1605
+ impl < T , S > BitAndAssign < & HashSet < T , S > > for HashSet < T , S >
1606
+ where
1607
+ T : Eq + Hash + Clone ,
1608
+ S : BuildHasher + Default ,
1609
+ {
1610
+ /// Modifies this set to contain the intersection of `self` and `rhs`.
1611
+ ///
1612
+ /// # Examples
1613
+ ///
1614
+ /// ```
1615
+ /// use hashbrown::HashSet;
1616
+ ///
1617
+ /// let mut a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
1618
+ /// let b: HashSet<_> = vec![2, 3, 4].into_iter().collect();
1619
+ ///
1620
+ /// a &= &b;
1621
+ ///
1622
+ /// let mut i = 0;
1623
+ /// let expected = [2, 3];
1624
+ /// for x in &a {
1625
+ /// assert!(expected.contains(x));
1626
+ /// i += 1;
1627
+ /// }
1628
+ /// assert_eq!(i, expected.len());
1629
+ /// ```
1630
+ fn bitand_assign ( & mut self , rhs : & HashSet < T , S > ) {
1631
+ self . retain ( |item| rhs. contains ( item) ) ;
1632
+ }
1633
+ }
1634
+
1635
+ impl < T , S > BitXorAssign < & HashSet < T , S > > for HashSet < T , S >
1636
+ where
1637
+ T : Eq + Hash + Clone ,
1638
+ S : BuildHasher + Default ,
1639
+ {
1640
+ /// Modifies this set to contain the symmetric difference of `self` and `rhs`.
1641
+ ///
1642
+ /// # Examples
1643
+ ///
1644
+ /// ```
1645
+ /// use hashbrown::HashSet;
1646
+ ///
1647
+ /// let mut a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
1648
+ /// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect();
1649
+ ///
1650
+ /// a ^= &b;
1651
+ ///
1652
+ /// let mut i = 0;
1653
+ /// let expected = [1, 2, 4, 5];
1654
+ /// for x in &a {
1655
+ /// assert!(expected.contains(x));
1656
+ /// i += 1;
1657
+ /// }
1658
+ /// assert_eq!(i, expected.len());
1659
+ /// ```
1660
+ fn bitxor_assign ( & mut self , rhs : & HashSet < T , S > ) {
1661
+ for item in rhs {
1662
+ if self . contains ( item) {
1663
+ self . remove ( item) ;
1664
+ } else {
1665
+ self . insert ( item. clone ( ) ) ;
1666
+ }
1667
+ }
1668
+ }
1669
+ }
1670
+
1671
+ impl < T , S > AddAssign < & HashSet < T , S > > for HashSet < T , S >
1672
+ where
1673
+ T : Eq + Hash + Clone ,
1674
+ S : BuildHasher + Default ,
1675
+ {
1676
+ /// Modifies this set to contain the union of `self` and `rhs`.
1677
+ ///
1678
+ /// # Examples
1679
+ ///
1680
+ /// ```
1681
+ /// use hashbrown::HashSet;
1682
+ ///
1683
+ /// let mut a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
1684
+ /// let b: HashSet<_> = vec![2, 3, 4].into_iter().collect();
1685
+ ///
1686
+ /// a += &b;
1687
+ ///
1688
+ /// let mut i = 0;
1689
+ /// let expected = [1, 2, 3, 4];
1690
+ /// for x in &a {
1691
+ /// assert!(expected.contains(x));
1692
+ /// i += 1;
1693
+ /// }
1694
+ /// assert_eq!(i, expected.len());
1695
+ /// ```
1696
+ fn add_assign ( & mut self , rhs : & HashSet < T , S > ) {
1697
+ for item in rhs {
1698
+ if !self . contains ( item) {
1699
+ self . insert ( item. clone ( ) ) ;
1700
+ }
1701
+ }
1702
+ }
1703
+ }
1704
+
1705
+ impl < T , S > SubAssign < & HashSet < T , S > > for HashSet < T , S >
1706
+ where
1707
+ T : Eq + Hash + Clone ,
1708
+ S : BuildHasher + Default ,
1709
+ {
1710
+ /// Modifies this set to contain the difference of `self` and `rhs`.
1711
+ ///
1712
+ /// # Examples
1713
+ ///
1714
+ /// ```
1715
+ /// use hashbrown::HashSet;
1716
+ ///
1717
+ /// let mut a: HashSet<_> = vec![1, 2, 3].into_iter().collect();
1718
+ /// let b: HashSet<_> = vec![3, 4, 5].into_iter().collect();
1719
+ ///
1720
+ /// a -= &b;
1721
+ ///
1722
+ /// let mut i = 0;
1723
+ /// let expected = [1, 2];
1724
+ /// for x in &a {
1725
+ /// assert!(expected.contains(x));
1726
+ /// i += 1;
1727
+ /// }
1728
+ /// assert_eq!(i, expected.len());
1729
+ /// ```
1730
+ fn sub_assign ( & mut self , rhs : & HashSet < T , S > ) {
1731
+ self . retain ( |item| !rhs. contains ( item) ) ;
1732
+ }
1733
+ }
1734
+
1569
1735
/// An iterator over the items of a `HashSet`.
1570
1736
///
1571
1737
/// This `struct` is created by the [`iter`] method on [`HashSet`].
0 commit comments