mirror of
https://github.com/apple/swift-collections
synced 2026-11-25 04:37:48 +00:00
This is a work in progress; in particular, it adds a cached `count` to BitSet, which may turn out to be a bad idea. (It generates extra work for bulk operations even if the count is never needed.) I think replacing this with a cached `startIndex` might be more useful in practice. The `_UnsafeBitset` type from the OrderedCollections module is brought over as `BitSet._UnsafeHandle`. This implements an (internal-only) fixed-size bitset. `BitSet` now conforms to `SetAlgebra`. We decided against this in the initial implementation, but on reflection, it seems like the right move. (The somewhat nonsensical `insert` and `update` methods are unfortunate, but consistency is valuable.)
62 lines
1.7 KiB
Swift
62 lines
1.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2021-2022 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
extension BitSet {
|
|
public func isSubset(of other: Self) -> Bool {
|
|
self._read { first in
|
|
other._read { second in
|
|
let w1 = first._words
|
|
let w2 = second._words
|
|
if first.count > second.count || w1.count > w2.count {
|
|
return false
|
|
}
|
|
for i in 0 ..< w1.count {
|
|
if !w1[i].subtracting(w2[i]).isEmpty {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
@inlinable
|
|
public func isSubset<S: Sequence>(of other: S) -> Bool
|
|
where S.Element: FixedWidthInteger
|
|
{
|
|
if S.self == BitSet.self {
|
|
return self.isSubset(of: other as! BitSet)
|
|
}
|
|
if S.self == Range<S.Element>.self {
|
|
return self.isSubset(of: other as! Range<S.Element>)
|
|
}
|
|
guard !isEmpty else { return true }
|
|
var t = self
|
|
for i in other {
|
|
guard let i = UInt(exactly: i) else { continue }
|
|
if t.remove(i) != nil, t.isEmpty { return true }
|
|
}
|
|
assert(!t.isEmpty)
|
|
return false
|
|
}
|
|
|
|
@inlinable
|
|
public func isSubset<I: FixedWidthInteger>(of other: Range<I>) -> Bool {
|
|
guard !isEmpty else { return true }
|
|
return _isSubset(of: other._clampedToUInt())
|
|
}
|
|
|
|
@usableFromInline
|
|
internal func _isSubset(of other: Range<UInt>) -> Bool {
|
|
_read { $0.isSubset(of: other) }
|
|
}
|
|
}
|