]> git.lizzy.rs Git - rust.git/commitdiff
Fix poor worst case performance of is_disjoint
authorStein Somers <git@steinsomers.be>
Wed, 9 Jan 2019 21:19:54 +0000 (22:19 +0100)
committerStein Somers <git@steinsomers.be>
Wed, 9 Jan 2019 21:19:54 +0000 (22:19 +0100)
src/libstd/collections/hash/set.rs

index 627846411bc0930972651c57898b9fb34feae6f2..c55dd049ec60fd15ca924349e78042f31cd3ae85 100644 (file)
@@ -599,7 +599,11 @@ pub fn get<Q: ?Sized>(&self, value: &Q) -> Option<&T>
     /// ```
     #[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,
@@ -1510,7 +1514,6 @@ fn test_intersection() {
         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));