File tree Expand file tree Collapse file tree 2 files changed +35
-4
lines changed Expand file tree Collapse file tree 2 files changed +35
-4
lines changed Original file line number Diff line number Diff line change @@ -77,6 +77,12 @@ extension _NativeSet { // Primitive fields
77
77
}
78
78
}
79
79
80
+ @_alwaysEmitIntoClient
81
+ @inline ( __always)
82
+ internal var bucketCount : Int {
83
+ _assumeNonNegative ( _storage. _bucketCount)
84
+ }
85
+
80
86
@inlinable
81
87
internal var hashTable : _HashTable {
82
88
@inline ( __always) get {
@@ -580,3 +586,26 @@ extension _NativeSet.Iterator: IteratorProtocol {
580
586
return base. uncheckedElement ( at: index)
581
587
}
582
588
}
589
+
590
+ extension _NativeSet {
591
+ @_alwaysEmitIntoClient
592
+ internal func isSubset< S: Sequence > ( of possibleSuperset: S ) -> Bool
593
+ where S. Element == Element {
594
+ _UnsafeBitset. withTemporaryBitset ( capacity: self . bucketCount) { seen in
595
+ // Mark elements in self that we've seen in `possibleSuperset`.
596
+ var seenCount = 0
597
+ for element in possibleSuperset {
598
+ let ( bucket, found) = find ( element)
599
+ guard found else { continue }
600
+ let inserted = seen. uncheckedInsert ( bucket. offset)
601
+ if inserted {
602
+ seenCount += 1
603
+ if seenCount == self . count {
604
+ return true
605
+ }
606
+ }
607
+ }
608
+ return false
609
+ }
610
+ }
611
+ }
Original file line number Diff line number Diff line change @@ -710,9 +710,11 @@ extension Set: SetAlgebra {
710
710
public func isSubset< S: Sequence > ( of possibleSuperset: S ) -> Bool
711
711
where S. Element == Element {
712
712
guard !isEmpty else { return true }
713
-
714
- let other = Set ( possibleSuperset)
715
- return isSubset ( of: other)
713
+ if self . count == 1 { return possibleSuperset. contains ( self . first!) }
714
+ if let s = possibleSuperset as? Set < Element > {
715
+ return isSubset ( of: s)
716
+ }
717
+ return _variant. convertedToNative. isSubset ( of: possibleSuperset)
716
718
}
717
719
718
720
/// Returns a Boolean value that indicates whether the set is a strict subset
@@ -1081,7 +1083,7 @@ extension Set {
1081
1083
public func isSubset( of other: Set < Element > ) -> Bool {
1082
1084
guard self . count <= other. count else { return false }
1083
1085
for member in self {
1084
- if ! other. contains ( member) {
1086
+ guard other. contains ( member) else {
1085
1087
return false
1086
1088
}
1087
1089
}
You can’t perform that action at this time.
0 commit comments