Skip to content

Commit d1ec3b9

Browse files
committed
[stdlib] Make Set.isSubset(of:) self-contained; remove standalone function for same
1 parent d2d3c0c commit d1ec3b9

File tree

1 file changed

+7
-21
lines changed

1 file changed

+7
-21
lines changed

stdlib/public/core/Set.swift

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -391,25 +391,6 @@ extension Set: Collection {
391391
}
392392
}
393393

394-
/// Check for both subset and equality relationship between
395-
/// a set and some sequence (which may itself be a `Set`).
396-
///
397-
/// (isSubset: lhs ⊂ rhs, isEqual: lhs ⊂ rhs and |lhs| = |rhs|)
398-
@inlinable
399-
internal func _compareSets<Element>(
400-
_ lhs: Set<Element>,
401-
_ rhs: Set<Element>
402-
) -> (isSubset: Bool, isEqual: Bool) {
403-
// FIXME(performance): performance could be better if we start by comparing
404-
// counts.
405-
for member in lhs {
406-
if !rhs.contains(member) {
407-
return (false, false)
408-
}
409-
}
410-
return (true, lhs.count == rhs.count)
411-
}
412-
413394
// FIXME: rdar://problem/23549059 (Optimize == for Set)
414395
// Look into initially trying to compare the two sets by directly comparing the
415396
// contents of both buffers in order. If they happen to have the exact same
@@ -1316,8 +1297,13 @@ extension Set {
13161297
/// - Returns: `true` if the set is a subset of `other`; otherwise, `false`.
13171298
@inlinable
13181299
public func isSubset(of other: Set<Element>) -> Bool {
1319-
let (isSubset, isEqual) = _compareSets(self, other)
1320-
return isSubset || isEqual
1300+
guard self.count <= other.count else { return false }
1301+
for member in self {
1302+
if !other.contains(member) {
1303+
return false
1304+
}
1305+
}
1306+
return true
13211307
}
13221308

13231309
/// Returns a Boolean value that indicates whether this set is a superset of

0 commit comments

Comments
 (0)