Skip to content

Commit

Permalink
Improve worst-case performance of HashSet.is_subset
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanieu committed Apr 12, 2019
1 parent 9068eb7 commit 9f558f5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/external_trait_impls/rayon/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,11 @@ where
///
/// This method runs in a potentially parallel fashion.
pub fn par_is_subset(&self, other: &Self) -> bool {
self.into_par_iter().all(|x| other.contains(x))
if self.len() <= other.len() {
self.into_par_iter().all(|x| other.contains(x))
} else {
false
}
}

/// Returns `true` if the set is a superset of another,
Expand Down
6 changes: 5 additions & 1 deletion src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,11 @@ where
/// assert_eq!(set.is_subset(&sup), false);
/// ```
pub fn is_subset(&self, other: &Self) -> bool {
self.iter().all(|v| other.contains(v))
if self.len() <= other.len() {
self.iter().all(|v| other.contains(v))
} else {
false
}
}

/// Returns `true` if the set is a superset of another,
Expand Down

0 comments on commit 9f558f5

Please sign in to comment.