Skip to content

Commit

Permalink
Fix poor worst case performance of is_disjoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ssomers committed Jan 9, 2019
1 parent ccba43d commit 8823bf0
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/libstd/collections/hash/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,11 @@ impl<T, S> HashSet<T, S>
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn is_disjoint(&self, other: &HashSet<T, S>) -> bool {
self.iter().all(|v| !other.contains(v))
if self.len() <= other.len() {
self.iter().all(|v| !other.contains(v))
} else {
other.iter().all(|v| !self.contains(v))
}
}

/// Returns `true` if the set is a subset of another,
Expand Down Expand Up @@ -1510,7 +1514,6 @@ mod test_set {
let mut a = HashSet::new();
let mut b = HashSet::new();
assert!(a.intersection(&b).next().is_none());
assert!(b.intersection(&a).next().is_none());

assert!(a.insert(11));
assert!(a.insert(1));
Expand Down

0 comments on commit 8823bf0

Please sign in to comment.