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.)
58 lines
1.7 KiB
Swift
58 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 mutating func formIntersection(_ other: Self) {
|
|
other._read { source in
|
|
if source.wordCount < _storage.count {
|
|
self._storage.removeLast(_storage.count - source.wordCount)
|
|
}
|
|
_updateThenShrink { target, shrink in
|
|
target.combineSharedPrefix(
|
|
with: source, using: { $0.formIntersection($1) })
|
|
}
|
|
}
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func formIntersection<S: Sequence>(
|
|
_ other: __owned S
|
|
) where S.Element: FixedWidthInteger {
|
|
if S.self == Range<S.Element>.self {
|
|
formIntersection(other as! Range<S.Element>)
|
|
return
|
|
}
|
|
formIntersection(BitSet(_validMembersOf: other))
|
|
}
|
|
|
|
@inlinable
|
|
public mutating func formIntersection<I: FixedWidthInteger>(
|
|
_ other: Range<I>
|
|
) {
|
|
_formIntersection(other._clampedToUInt())
|
|
}
|
|
|
|
@usableFromInline
|
|
internal mutating func _formIntersection(_ other: Range<UInt>) {
|
|
guard let last = other.last else {
|
|
self = []
|
|
return
|
|
}
|
|
let lastWord = _UnsafeHandle.Index(last).word
|
|
if _storage.count - lastWord - 1 > 0 {
|
|
_storage.removeLast(_storage.count - lastWord - 1)
|
|
}
|
|
_updateThenShrink { handle, shrink in
|
|
handle.formIntersection(other)
|
|
}
|
|
}
|
|
}
|