Skip to content

Commit beb6477

Browse files
committed
Auto merge of #48363 - varkor:hashset-methods-generic-over-hasher, r=<try>
Allow binary HashSet operations to be generic over hashers Allow the two sets that are parameters in `HashSet::{difference, intersection, is_disjoint, is_subset, is_superset}` to have different hashing algorithms. Fixes #31712.
2 parents 1733a61 + 2e55a0b commit beb6477

File tree

1 file changed

+34
-13
lines changed
  • src/libstd/collections/hash

1 file changed

+34
-13
lines changed

src/libstd/collections/hash/set.rs

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,9 @@ impl<T, S> HashSet<T, S>
337337
/// assert_eq!(diff, [4].iter().collect());
338338
/// ```
339339
#[stable(feature = "rust1", since = "1.0.0")]
340-
pub fn difference<'a>(&'a self, other: &'a HashSet<T, S>) -> Difference<'a, T, S> {
340+
pub fn difference<'a, U: BuildHasher>(&'a self,
341+
other: &'a HashSet<T, U>)
342+
-> Difference<'a, T, U> {
341343
Difference {
342344
iter: self.iter(),
343345
other,
@@ -391,7 +393,9 @@ impl<T, S> HashSet<T, S>
391393
/// assert_eq!(intersection, [2, 3].iter().collect());
392394
/// ```
393395
#[stable(feature = "rust1", since = "1.0.0")]
394-
pub fn intersection<'a>(&'a self, other: &'a HashSet<T, S>) -> Intersection<'a, T, S> {
396+
pub fn intersection<'a, U: BuildHasher>(&'a self,
397+
other: &'a HashSet<T, U>)
398+
-> Intersection<'a, T, U> {
395399
Intersection {
396400
iter: self.iter(),
397401
other,
@@ -565,7 +569,7 @@ impl<T, S> HashSet<T, S>
565569
/// assert_eq!(a.is_disjoint(&b), false);
566570
/// ```
567571
#[stable(feature = "rust1", since = "1.0.0")]
568-
pub fn is_disjoint(&self, other: &HashSet<T, S>) -> bool {
572+
pub fn is_disjoint<U: BuildHasher>(&self, other: &HashSet<T, U>) -> bool {
569573
self.iter().all(|v| !other.contains(v))
570574
}
571575

@@ -587,7 +591,7 @@ impl<T, S> HashSet<T, S>
587591
/// assert_eq!(set.is_subset(&sup), false);
588592
/// ```
589593
#[stable(feature = "rust1", since = "1.0.0")]
590-
pub fn is_subset(&self, other: &HashSet<T, S>) -> bool {
594+
pub fn is_subset<U: BuildHasher>(&self, other: &HashSet<T, U>) -> bool {
591595
self.iter().all(|v| other.contains(v))
592596
}
593597

@@ -613,7 +617,7 @@ impl<T, S> HashSet<T, S>
613617
/// ```
614618
#[inline]
615619
#[stable(feature = "rust1", since = "1.0.0")]
616-
pub fn is_superset(&self, other: &HashSet<T, S>) -> bool {
620+
pub fn is_superset<U: BuildHasher>(&self, other: &HashSet<T, U>) -> bool {
617621
other.is_subset(self)
618622
}
619623

@@ -1375,6 +1379,23 @@ fn assert_covariance() {
13751379
mod test_set {
13761380
use super::HashSet;
13771381
use super::super::map::RandomState;
1382+
use hash::BuildHasher;
1383+
1384+
struct AltRandomState(RandomState);
1385+
1386+
impl AltRandomState {
1387+
fn new() -> AltRandomState {
1388+
AltRandomState(RandomState::new())
1389+
}
1390+
}
1391+
1392+
impl BuildHasher for AltRandomState {
1393+
type Hasher = <RandomState as BuildHasher>::Hasher;
1394+
1395+
fn build_hasher(&self) -> Self::Hasher {
1396+
self.0.build_hasher()
1397+
}
1398+
}
13781399

13791400
#[test]
13801401
fn test_zero_capacities() {
@@ -1410,8 +1431,8 @@ mod test_set {
14101431

14111432
#[test]
14121433
fn test_disjoint() {
1413-
let mut xs = HashSet::new();
1414-
let mut ys = HashSet::new();
1434+
let mut xs = HashSet::with_hasher(RandomState::new());
1435+
let mut ys = HashSet::with_hasher(AltRandomState::new());
14151436
assert!(xs.is_disjoint(&ys));
14161437
assert!(ys.is_disjoint(&xs));
14171438
assert!(xs.insert(5));
@@ -1432,13 +1453,13 @@ mod test_set {
14321453

14331454
#[test]
14341455
fn test_subset_and_superset() {
1435-
let mut a = HashSet::new();
1456+
let mut a = HashSet::with_hasher(RandomState::new());
14361457
assert!(a.insert(0));
14371458
assert!(a.insert(5));
14381459
assert!(a.insert(11));
14391460
assert!(a.insert(7));
14401461

1441-
let mut b = HashSet::new();
1462+
let mut b = HashSet::with_hasher(AltRandomState::new());
14421463
assert!(b.insert(0));
14431464
assert!(b.insert(7));
14441465
assert!(b.insert(19));
@@ -1474,8 +1495,8 @@ mod test_set {
14741495

14751496
#[test]
14761497
fn test_intersection() {
1477-
let mut a = HashSet::new();
1478-
let mut b = HashSet::new();
1498+
let mut a = HashSet::with_hasher(RandomState::new());
1499+
let mut b = HashSet::with_hasher(AltRandomState::new());
14791500

14801501
assert!(a.insert(11));
14811502
assert!(a.insert(1));
@@ -1504,8 +1525,8 @@ mod test_set {
15041525

15051526
#[test]
15061527
fn test_difference() {
1507-
let mut a = HashSet::new();
1508-
let mut b = HashSet::new();
1528+
let mut a = HashSet::with_hasher(RandomState::new());
1529+
let mut b = HashSet::with_hasher(AltRandomState::new());
15091530

15101531
assert!(a.insert(1));
15111532
assert!(a.insert(3));

0 commit comments

Comments
 (0)