Files
swift-collections/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift
T
Károly Lőrentey 8f2e0c79c3 Review & update BitSet
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.)
2022-01-10 23:35:33 -08:00

55 lines
1.6 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 isStrictSuperset(of other: Self) -> Bool {
other.isStrictSubset(of: self)
}
@inlinable
public func isStrictSuperset<S: Sequence>(of other: S) -> Bool
where S.Element: FixedWidthInteger
{
guard !isEmpty else { return false }
if S.self == BitSet.self {
return isStrictSuperset(of: other as! BitSet)
}
if S.self == Range<S.Element>.self {
return isStrictSuperset(of: other as! Range<S.Element>)
}
return _UnsafeHandle.withTemporaryBitset(
wordCount: _storage.count
) { seen in
for i in other {
guard let i = UInt(exactly: i) else { return false }
if !contains(i) { return false }
seen.insert(i)
}
return seen._count < self._count
}
}
@inlinable
public func isStrictSuperset<I: FixedWidthInteger>(
of other: Range<I>
) -> Bool {
if other.isEmpty { return !isEmpty }
if isEmpty { return false }
guard let r = other._toUInt() else { return false }
return _isStrictSuperset(of: r)
}
@usableFromInline
internal func _isStrictSuperset(of other: Range<UInt>) -> Bool {
_read { $0.isSuperset(of: other) && !$0.isSubset(of: other) }
}
}