@@ -420,9 +420,16 @@ impl<T, S> HashSet<T, S>
420
420
/// ```
421
421
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
422
422
pub fn intersection < ' a > ( & ' a self , other : & ' a HashSet < T , S > ) -> Intersection < ' a , T , S > {
423
- Intersection {
424
- iter : self . iter ( ) ,
425
- other,
423
+ if self . len ( ) <= other. len ( ) {
424
+ Intersection {
425
+ iter : self . iter ( ) ,
426
+ other,
427
+ }
428
+ } else {
429
+ Intersection {
430
+ iter : other. iter ( ) ,
431
+ other : self ,
432
+ }
426
433
}
427
434
}
428
435
@@ -446,7 +453,15 @@ impl<T, S> HashSet<T, S>
446
453
/// ```
447
454
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
448
455
pub fn union < ' a > ( & ' a self , other : & ' a HashSet < T , S > ) -> Union < ' a , T , S > {
449
- Union { iter : self . iter ( ) . chain ( other. difference ( self ) ) }
456
+ if self . len ( ) <= other. len ( ) {
457
+ Union {
458
+ iter : self . iter ( ) . chain ( other. difference ( self ) ) ,
459
+ }
460
+ } else {
461
+ Union {
462
+ iter : other. iter ( ) . chain ( self . difference ( other) ) ,
463
+ }
464
+ }
450
465
}
451
466
452
467
/// Returns the number of elements in the set.
@@ -1504,6 +1519,8 @@ mod test_set {
1504
1519
fn test_intersection ( ) {
1505
1520
let mut a = HashSet :: new ( ) ;
1506
1521
let mut b = HashSet :: new ( ) ;
1522
+ assert ! ( a. intersection( & b) . next( ) . is_none( ) ) ;
1523
+ assert ! ( b. intersection( & a) . next( ) . is_none( ) ) ;
1507
1524
1508
1525
assert ! ( a. insert( 11 ) ) ;
1509
1526
assert ! ( a. insert( 1 ) ) ;
@@ -1528,6 +1545,22 @@ mod test_set {
1528
1545
i += 1
1529
1546
}
1530
1547
assert_eq ! ( i, expected. len( ) ) ;
1548
+
1549
+ assert ! ( a. insert( 9 ) ) ; // make a bigger than b
1550
+
1551
+ i = 0 ;
1552
+ for x in a. intersection ( & b) {
1553
+ assert ! ( expected. contains( x) ) ;
1554
+ i += 1
1555
+ }
1556
+ assert_eq ! ( i, expected. len( ) ) ;
1557
+
1558
+ i = 0 ;
1559
+ for x in b. intersection ( & a) {
1560
+ assert ! ( expected. contains( x) ) ;
1561
+ i += 1
1562
+ }
1563
+ assert_eq ! ( i, expected. len( ) ) ;
1531
1564
}
1532
1565
1533
1566
#[ test]
@@ -1583,11 +1616,11 @@ mod test_set {
1583
1616
fn test_union ( ) {
1584
1617
let mut a = HashSet :: new ( ) ;
1585
1618
let mut b = HashSet :: new ( ) ;
1619
+ assert ! ( a. union ( & b) . next( ) . is_none( ) ) ;
1620
+ assert ! ( b. union ( & a) . next( ) . is_none( ) ) ;
1586
1621
1587
1622
assert ! ( a. insert( 1 ) ) ;
1588
1623
assert ! ( a. insert( 3 ) ) ;
1589
- assert ! ( a. insert( 5 ) ) ;
1590
- assert ! ( a. insert( 9 ) ) ;
1591
1624
assert ! ( a. insert( 11 ) ) ;
1592
1625
assert ! ( a. insert( 16 ) ) ;
1593
1626
assert ! ( a. insert( 19 ) ) ;
@@ -1607,6 +1640,23 @@ mod test_set {
1607
1640
i += 1
1608
1641
}
1609
1642
assert_eq ! ( i, expected. len( ) ) ;
1643
+
1644
+ assert ! ( a. insert( 9 ) ) ; // make a bigger than b
1645
+ assert ! ( a. insert( 5 ) ) ;
1646
+
1647
+ i = 0 ;
1648
+ for x in a. union ( & b) {
1649
+ assert ! ( expected. contains( x) ) ;
1650
+ i += 1
1651
+ }
1652
+ assert_eq ! ( i, expected. len( ) ) ;
1653
+
1654
+ i = 0 ;
1655
+ for x in b. union ( & a) {
1656
+ assert ! ( expected. contains( x) ) ;
1657
+ i += 1
1658
+ }
1659
+ assert_eq ! ( i, expected. len( ) ) ;
1610
1660
}
1611
1661
1612
1662
#[ test]
0 commit comments