Skip to content

Commit 50a1817

Browse files
Merge pull request swiftlang#39263 from LucianoPAlmeida/set-improvements
[stdlib] Improve set isDisjoint by iterating on smaller set
2 parents be98f6b + 00cb1c5 commit 50a1817

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

stdlib/public/core/Set.swift

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,15 @@ extension Set {
11231123
/// otherwise, `false`.
11241124
@inlinable
11251125
public func isDisjoint(with other: Set<Element>) -> Bool {
1126-
return _isDisjoint(with: other)
1126+
guard !isEmpty && !other.isEmpty else { return true }
1127+
let (smaller, larger) =
1128+
count < other.count ? (self, other) : (other, self)
1129+
for member in smaller {
1130+
if larger.contains(member) {
1131+
return false
1132+
}
1133+
}
1134+
return true
11271135
}
11281136

11291137
@inlinable

0 commit comments

Comments
 (0)