mirror of
https://github.com/apple/swift-collections
synced 2026-11-28 05:04:21 +00:00
[BitSet] Make BitSet generic over its Element
This gets rid of the weird asymmetry between reading the contents of a BitSet (where we used to always get Int values) and mutating it (where we used to be able to give it values fo any fixed-width integer type).
This commit is contained in:
1 parent
55f3b58437
commit
1d1fbc82d4
26 files changed
+621
-389
No files matched your search
@@ -10,9 +10,9 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitArray {
|
||||
public init(_ set: BitSet) {
|
||||
public init(_ set: BitSet<Index>) {
|
||||
guard let l = set.last else { self.init(); return }
|
||||
self.init(_storage: set._storage, count: l + 1)
|
||||
self.init(_storage: set._core._storage, count: l + 1)
|
||||
}
|
||||
|
||||
public init<I: BinaryInteger>(bitPattern value: I) {
|
||||
|
||||
@@ -10,8 +10,6 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet: Sequence {
|
||||
public typealias Element = Int
|
||||
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
public var underestimatedCount: Int {
|
||||
@@ -27,11 +25,11 @@ extension BitSet: Sequence {
|
||||
_ element: Element
|
||||
) -> Bool? {
|
||||
guard let element = UInt(exactly: element) else { return false }
|
||||
return _read { $0.contains(element) }
|
||||
return _core._read { $0.contains(element) }
|
||||
}
|
||||
|
||||
public struct Iterator: IteratorProtocol {
|
||||
internal typealias _UnsafeHandle = BitSet._UnsafeHandle
|
||||
internal typealias _UnsafeHandle = _BitSet.UnsafeHandle
|
||||
|
||||
internal let bitset: BitSet
|
||||
internal var index: Int
|
||||
@@ -41,25 +39,25 @@ extension BitSet: Sequence {
|
||||
internal init(_ bitset: BitSet) {
|
||||
self.bitset = bitset
|
||||
self.index = 0
|
||||
self.word = bitset._read { handle in
|
||||
self.word = bitset._core._read { handle in
|
||||
guard handle.wordCount > 0 else { return .empty }
|
||||
return handle._words[0]
|
||||
}
|
||||
}
|
||||
|
||||
@_effects(releasenone)
|
||||
public mutating func next() -> Int? {
|
||||
public mutating func next() -> Element? {
|
||||
if let bit = word.next() {
|
||||
let i = _UnsafeHandle.Index(word: index, bit: bit)
|
||||
return Int(truncatingIfNeeded: i.value)
|
||||
return Element(truncatingIfNeeded: i.value)
|
||||
}
|
||||
return bitset._read { handle in
|
||||
return bitset._core._read { handle in
|
||||
while (index + 1) < handle.wordCount {
|
||||
index += 1
|
||||
word = handle._words[index]
|
||||
if let bit = word.next() {
|
||||
let i = _UnsafeHandle.Index(word: index, bit: bit)
|
||||
return Int(truncatingIfNeeded: i.value)
|
||||
return Element(truncatingIfNeeded: i.value)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -69,28 +67,28 @@ extension BitSet: Sequence {
|
||||
}
|
||||
|
||||
extension BitSet: Collection, BidirectionalCollection {
|
||||
public var isEmpty: Bool { _count == 0 }
|
||||
public var count: Int { _count }
|
||||
public var isEmpty: Bool { _core._count == 0 }
|
||||
public var count: Int { _core._count }
|
||||
|
||||
public var startIndex: Index {
|
||||
// Note: This is O(n)
|
||||
Index(_position: _read { $0.startIndex })
|
||||
Index(_position: _core._read { $0.startIndex })
|
||||
}
|
||||
|
||||
public var endIndex: Index {
|
||||
Index(_position: .init(word: _storage.count, bit: 0))
|
||||
Index(_position: .init(word: _core._storage.count, bit: 0))
|
||||
}
|
||||
|
||||
public subscript(position: Index) -> Int {
|
||||
Int(position._position.value)
|
||||
public subscript(position: Index) -> Element {
|
||||
Element(position._position.value)
|
||||
}
|
||||
|
||||
public func index(after index: Index) -> Index {
|
||||
Index(_position: _read { $0.index(after: index._position) })
|
||||
Index(_position: _core._read { $0.index(after: index._position) })
|
||||
}
|
||||
|
||||
public func index(before index: Index) -> Index {
|
||||
Index(_position: _read { $0.index(before: index._position) })
|
||||
Index(_position: _core._read { $0.index(before: index._position) })
|
||||
}
|
||||
|
||||
#if false // TODO: Specialize these
|
||||
|
||||
@@ -11,6 +11,6 @@
|
||||
|
||||
extension BitSet: Equatable {
|
||||
public static func ==(left: Self, right: Self) -> Bool {
|
||||
left._storage == right._storage
|
||||
left._core._storage == right._core._storage
|
||||
}
|
||||
}
|
||||
@@ -10,7 +10,7 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet: ExpressibleByArrayLiteral {
|
||||
public init(arrayLiteral elements: Int...) {
|
||||
public init(arrayLiteral elements: Element...) {
|
||||
self.init(elements)
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,6 @@
|
||||
|
||||
extension BitSet: Hashable {
|
||||
public func hash(into hasher: inout Hasher) {
|
||||
hasher.combine(_storage)
|
||||
hasher.combine(_core._storage)
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ extension BitSet {
|
||||
@inlinable
|
||||
public init<S: Sequence>(
|
||||
_ elements: __owned S
|
||||
) where S.Element: FixedWidthInteger {
|
||||
) where S.Element == Element {
|
||||
if S.self == BitSet.self {
|
||||
self = (elements as! BitSet)
|
||||
return
|
||||
@@ -31,27 +31,27 @@ extension BitSet {
|
||||
@inlinable
|
||||
internal init<S: Sequence>(
|
||||
_validMembersOf elements: __owned S
|
||||
) where S.Element: FixedWidthInteger {
|
||||
) where S.Element == Element {
|
||||
if S.self == BitSet.self {
|
||||
self = (elements as! BitSet)
|
||||
return
|
||||
}
|
||||
if S.self == Range<S.Element>.self {
|
||||
let r = (elements as! Range<S.Element>)
|
||||
self.init(r._clampedToUInt())
|
||||
if S.self == Range<Element>.self {
|
||||
let r = (elements as! Range<Element>)
|
||||
self.init(_core: _BitSet(r._clampedToUInt()))
|
||||
return
|
||||
}
|
||||
self.init()
|
||||
for value in elements {
|
||||
guard let value = UInt(exactly: value) else { continue }
|
||||
_ = self._insert(value)
|
||||
_ = self._core.insert(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public init<I: FixedWidthInteger>(_ range: Range<I>) {
|
||||
public init(_ range: Range<Element>) {
|
||||
guard
|
||||
let lower = UInt(exactly: range.lowerBound),
|
||||
let upper = UInt(exactly: range.upperBound)
|
||||
@@ -59,15 +59,17 @@ extension BitSet {
|
||||
preconditionFailure("BitSet can only hold nonnegative integers")
|
||||
}
|
||||
let range = Range(uncheckedBounds: (lower, upper))
|
||||
self.init(_range: range)
|
||||
self.init(_core: _BitSet(range))
|
||||
}
|
||||
}
|
||||
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal init(_range range: Range<UInt>) {
|
||||
internal init(_ range: Range<UInt>) {
|
||||
_count = range.count
|
||||
_storage = []
|
||||
let lower = _UnsafeHandle.Index(range.lowerBound)
|
||||
let upper = _UnsafeHandle.Index(range.upperBound)
|
||||
let lower = UnsafeHandle.Index(range.lowerBound)
|
||||
let upper = UnsafeHandle.Index(range.upperBound)
|
||||
if lower.word > 0 {
|
||||
_storage.append(contentsOf: repeatElement(.empty, count: lower.word))
|
||||
}
|
||||
@@ -96,9 +98,9 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
extension _BitSet {
|
||||
internal init(
|
||||
_combining handles: (_UnsafeHandle, _UnsafeHandle),
|
||||
_combining handles: (UnsafeHandle, UnsafeHandle),
|
||||
includingTail: Bool,
|
||||
using function: (_Word, _Word) -> _Word
|
||||
) {
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
extension _BitSet {
|
||||
#if COLLECTIONS_INTERNAL_CHECKS
|
||||
@inline(never)
|
||||
@_effects(releasenone)
|
||||
|
||||
@@ -9,69 +9,65 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public func contains(_ member: Int) -> Bool {
|
||||
// Note: the generic overload over `FixedWidthInteger` below does not shadow
|
||||
// the default `contains` implementation from Sequence, so we provide this
|
||||
// overload that's dedicated to `Int`, the element type.
|
||||
//
|
||||
// `BitSet` does customize `_customContainsEquatableElement` so we'd get
|
||||
// an efficient implementation even without this, but it seems like a good
|
||||
// idea to make things explicit.
|
||||
guard let value = UInt(exactly: member) else { return false }
|
||||
return _contains(value)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func contains<I: FixedWidthInteger>(_ member: I) -> Bool {
|
||||
guard let value = UInt(exactly: member) else { return false }
|
||||
return _contains(value)
|
||||
}
|
||||
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal func _contains(_ member: UInt) -> Bool {
|
||||
internal func contains(_ member: UInt) -> Bool {
|
||||
_read { $0.contains(member) }
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
@discardableResult
|
||||
public mutating func insert<I: FixedWidthInteger>(
|
||||
_ newMember: I
|
||||
) -> (inserted: Bool, memberAfterInsert: I) {
|
||||
guard let i = UInt(exactly: newMember) else {
|
||||
preconditionFailure("BitSet can only hold nonnegative integers")
|
||||
}
|
||||
return (_insert(i), newMember)
|
||||
public func contains(_ member: Element) -> Bool {
|
||||
guard let value = UInt(exactly: member) else { return false }
|
||||
return _core.contains(value)
|
||||
}
|
||||
}
|
||||
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal mutating func _insert(
|
||||
internal mutating func insert(
|
||||
_ newMember: UInt
|
||||
) -> Bool {
|
||||
_ensureCapacity(forValue: newMember)
|
||||
return _update { $0.insert(newMember) }
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
@discardableResult
|
||||
public mutating func update<I: FixedWidthInteger>(
|
||||
with newMember: I
|
||||
) -> I? {
|
||||
public mutating func insert(
|
||||
_ newMember: Element
|
||||
) -> (inserted: Bool, memberAfterInsert: Element) {
|
||||
guard let i = UInt(exactly: newMember) else {
|
||||
preconditionFailure("Value out of range")
|
||||
}
|
||||
return (_core.insert(i), newMember)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
@discardableResult
|
||||
public mutating func update(with newMember: Element) -> Element? {
|
||||
insert(newMember).inserted ? newMember : nil
|
||||
}
|
||||
}
|
||||
|
||||
@discardableResult
|
||||
public mutating func remove<I: FixedWidthInteger>(
|
||||
_ member: I
|
||||
) -> I? {
|
||||
guard let m = UInt(exactly: member) else { return nil }
|
||||
return _remove(m) ? member : nil
|
||||
}
|
||||
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal mutating func _remove(_ member: UInt) -> Bool {
|
||||
internal mutating func remove(_ member: UInt) -> Bool {
|
||||
_updateThenShrink { handle, shrink in
|
||||
shrink = handle.remove(member)
|
||||
return shrink
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
@discardableResult
|
||||
public mutating func remove(_ member: Element) -> Element? {
|
||||
guard let m = UInt(exactly: member) else { return nil }
|
||||
return _core.remove(m) ? member : nil
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,9 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public mutating func formIntersection(_ other: Self) {
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal mutating func formIntersection(_ other: Self) {
|
||||
other._read { source in
|
||||
if source.wordCount < _storage.count {
|
||||
self._storage.removeLast(_storage.count - source.wordCount)
|
||||
@@ -22,31 +23,13 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
|
||||
@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>) {
|
||||
internal mutating func formIntersection(_ other: Range<UInt>) {
|
||||
guard let last = other.last else {
|
||||
self = []
|
||||
self = _BitSet()
|
||||
return
|
||||
}
|
||||
let lastWord = _UnsafeHandle.Index(last).word
|
||||
let lastWord = UnsafeHandle.Index(last).word
|
||||
if _storage.count - lastWord - 1 > 0 {
|
||||
_storage.removeLast(_storage.count - lastWord - 1)
|
||||
}
|
||||
@@ -55,3 +38,33 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public mutating func formIntersection(_ other: Self) {
|
||||
_core.formIntersection(other._core)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func formIntersection<I: FixedWidthInteger>(
|
||||
_ other: BitSet<I>
|
||||
) {
|
||||
_core.formIntersection(other._core)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func formIntersection<S: Sequence>(
|
||||
_ other: __owned S
|
||||
) where S.Element == Element {
|
||||
if S.self == Range<S.Element>.self {
|
||||
formIntersection(other as! Range<S.Element>)
|
||||
return
|
||||
}
|
||||
formIntersection(BitSet(_validMembersOf: other))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func formIntersection(_ other: Range<Element>) {
|
||||
_core.formIntersection(other._clampedToUInt())
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,9 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public mutating func formSymmetricDifference(_ other: __owned Self) {
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal mutating func formSymmetricDifference(_ other: __owned Self) {
|
||||
_ensureCapacity(limit: other._capacity)
|
||||
_updateThenShrink { target, shrink in
|
||||
other._read { source in
|
||||
@@ -20,29 +21,8 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func formSymmetricDifference<S: Sequence>(
|
||||
_ other: __owned S
|
||||
) where S.Element: FixedWidthInteger {
|
||||
if S.self == Range<S.Element>.self {
|
||||
formSymmetricDifference(other as! Range<S.Element>)
|
||||
return
|
||||
}
|
||||
formSymmetricDifference(BitSet(other))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func formSymmetricDifference<I: FixedWidthInteger>(
|
||||
_ other: Range<I>
|
||||
) {
|
||||
guard let other = other._toUInt() else {
|
||||
preconditionFailure("BitSet can only hold nonnegative integers")
|
||||
}
|
||||
_formSymmetricDifference(other)
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal mutating func _formSymmetricDifference(_ other: Range<UInt>) {
|
||||
internal mutating func formSymmetricDifference(_ other: Range<UInt>) {
|
||||
guard !other.isEmpty else { return }
|
||||
_ensureCapacity(limit: other.upperBound)
|
||||
_updateThenShrink { handle, shrink in
|
||||
@@ -50,3 +30,33 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public mutating func formSymmetricDifference(_ other: __owned Self) {
|
||||
_core.formSymmetricDifference(other._core)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func formSymmetricDifference<S: Sequence>(
|
||||
_ other: __owned S
|
||||
) where S.Element == Element {
|
||||
if S.self == Range<S.Element>.self {
|
||||
formSymmetricDifference(other as! Range<S.Element>)
|
||||
return
|
||||
}
|
||||
_core.formSymmetricDifference(BitSet(other)._core)
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public mutating func formSymmetricDifference(
|
||||
_ other: Range<Element>
|
||||
) {
|
||||
guard let other = other._toUInt() else {
|
||||
preconditionFailure("Invalid range")
|
||||
}
|
||||
_core.formSymmetricDifference(other)
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,9 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public mutating func formUnion(_ other: __owned Self) {
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal mutating func formUnion(_ other: __owned Self) {
|
||||
_ensureCapacity(limit: other._capacity)
|
||||
_update { target in
|
||||
other._read { source in
|
||||
@@ -19,10 +20,26 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal mutating func formUnion(_ other: Range<UInt>) {
|
||||
guard !other.isEmpty else { return }
|
||||
_ensureCapacity(limit: other.upperBound)
|
||||
_update { handle in
|
||||
handle.formUnion(other)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public mutating func formUnion(_ other: __owned Self) {
|
||||
_core.formUnion(other._core)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func formUnion<S: Sequence>(
|
||||
_ other: __owned S
|
||||
) where S.Element: FixedWidthInteger {
|
||||
) where S.Element == Element {
|
||||
if S.self == BitSet.self {
|
||||
formUnion(other as! BitSet)
|
||||
return
|
||||
@@ -37,22 +54,11 @@ extension BitSet {
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func formUnion<I: FixedWidthInteger>(
|
||||
_ other: Range<I>
|
||||
) {
|
||||
public mutating func formUnion(_ other: Range<Element>) {
|
||||
guard let other = other._toUInt() else {
|
||||
preconditionFailure("BitSet can only hold nonnegative integers")
|
||||
}
|
||||
_formUnion(other)
|
||||
}
|
||||
|
||||
|
||||
@usableFromInline
|
||||
internal mutating func _formUnion(_ other: Range<UInt>) {
|
||||
guard !other.isEmpty else { return }
|
||||
_ensureCapacity(limit: other.upperBound)
|
||||
_update { handle in
|
||||
handle.formUnion(other)
|
||||
preconditionFailure("Invalid range")
|
||||
}
|
||||
_core.formUnion(other)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,8 +9,9 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public __consuming func intersection(_ other: Self) -> Self {
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal __consuming func intersection(_ other: Self) -> Self {
|
||||
self._read { first in
|
||||
other._read { second in
|
||||
Self(
|
||||
@@ -20,26 +21,30 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
public __consuming func intersection(_ other: Self) -> Self {
|
||||
Self(_core: _core.intersection(other._core))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public __consuming func intersection<S: Sequence>(
|
||||
_ other: __owned S
|
||||
) -> Self
|
||||
where S.Element: FixedWidthInteger
|
||||
where S.Element == Element
|
||||
{
|
||||
if S.self == BitSet.self {
|
||||
if S.self == Self.self {
|
||||
return intersection(other as! BitSet)
|
||||
}
|
||||
if S.self == Range<S.Element>.self {
|
||||
return intersection(other as! Range<S.Element>)
|
||||
if S.self == Range<Element>.self {
|
||||
return intersection(other as! Range<Element>)
|
||||
}
|
||||
return intersection(BitSet(_validMembersOf: other))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public __consuming func intersection<I: FixedWidthInteger>(
|
||||
_ other: Range<I>
|
||||
) -> Self {
|
||||
public __consuming func intersection(_ other: Range<Element>) -> Self {
|
||||
var result = self
|
||||
result.formIntersection(other)
|
||||
return result
|
||||
|
||||
@@ -9,8 +9,9 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public func isDisjoint(with other: Self) -> Bool {
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal func isDisjoint(with other: Self) -> Bool {
|
||||
self._read { first in
|
||||
other._read { second in
|
||||
let w1 = first._words
|
||||
@@ -23,29 +24,52 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func isDisjoint(with other: Range<UInt>) -> Bool {
|
||||
_read { $0.isDisjoint(with: other) }
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public func isDisjoint(with other: Self) -> Bool {
|
||||
_core.isDisjoint(with: other._core)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isDisjoint<I: FixedWidthInteger>(
|
||||
with other: BitSet<I>
|
||||
) -> Bool {
|
||||
_core.isDisjoint(with: other._core)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isDisjoint<S: Sequence>(with other: S) -> Bool
|
||||
where S.Element: FixedWidthInteger
|
||||
where S.Element == Element
|
||||
{
|
||||
if S.self == BitSet.self {
|
||||
return self.isDisjoint(with: other as! BitSet)
|
||||
}
|
||||
if S.self == Range<S.Element>.self {
|
||||
return self.isDisjoint(with: other as! Range<S.Element>)
|
||||
if S.self == Range<Element>.self {
|
||||
return self.isDisjoint(with: other as! Range<Element>)
|
||||
}
|
||||
for value in other {
|
||||
guard !contains(value) else { return false }
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public func isDisjoint(with other: Range<Element>) -> Bool {
|
||||
_core.isDisjoint(with: other._clampedToUInt())
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isDisjoint<I: FixedWidthInteger>(with other: Range<I>) -> Bool {
|
||||
_isDisjoint(with: other._clampedToUInt())
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _isDisjoint(with other: Range<UInt>) -> Bool {
|
||||
_read { $0.isDisjoint(with: other) }
|
||||
public func isDisjoint<I: FixedWidthInteger>(
|
||||
with other: Range<I>
|
||||
) -> Bool {
|
||||
_core.isDisjoint(with: other._clampedToUInt())
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,9 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public func isStrictSubset(of other: Self) -> Bool {
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal func isStrictSubset(of other: Self) -> Bool {
|
||||
self._read { first in
|
||||
other._read { second in
|
||||
let w1 = first._words
|
||||
@@ -29,16 +30,30 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public func isStrictSubset(of other: Self) -> Bool {
|
||||
_core.isStrictSubset(of: other._core)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isStrictSubset<I: FixedWidthInteger>(
|
||||
of other: BitSet<I>
|
||||
) -> Bool {
|
||||
_core.isStrictSubset(of: other._core)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isStrictSubset<S: Sequence>(of other: S) -> Bool
|
||||
where S.Element: FixedWidthInteger
|
||||
where S.Element == Element
|
||||
{
|
||||
if S.self == BitSet.self {
|
||||
return isStrictSubset(of: other as! BitSet)
|
||||
}
|
||||
if S.self == Range<S.Element>.self {
|
||||
return isStrictSubset(of: other as! Range<S.Element>)
|
||||
if S.self == Range<Element>.self {
|
||||
return isStrictSubset(of: other as! Range<Element>)
|
||||
}
|
||||
|
||||
if isEmpty {
|
||||
@@ -47,7 +62,7 @@ extension BitSet {
|
||||
}
|
||||
|
||||
return _UnsafeHandle.withTemporaryBitset(
|
||||
wordCount: _storage.count
|
||||
wordCount: _core._storage.count
|
||||
) { seen in
|
||||
var strict = false
|
||||
var it = other.makeIterator()
|
||||
@@ -56,20 +71,27 @@ extension BitSet {
|
||||
strict = true
|
||||
continue
|
||||
}
|
||||
if seen.insert(UInt(i)), seen._count == self._count {
|
||||
if seen.insert(UInt(i)), seen._count == self.count {
|
||||
while !strict, let i = it.next() {
|
||||
strict = !self.contains(i)
|
||||
}
|
||||
return strict
|
||||
}
|
||||
}
|
||||
assert(seen._count < self._count)
|
||||
assert(seen._count < self.count)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isStrictSubset<I: FixedWidthInteger>(of other: Range<I>) -> Bool {
|
||||
public func isStrictSubset(of other: Range<Element>) -> Bool {
|
||||
isSubset(of: other) && !isSuperset(of: other)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isStrictSubset<I: FixedWidthInteger>(
|
||||
of other: Range<I>
|
||||
) -> Bool {
|
||||
isSubset(of: other) && !isSuperset(of: other)
|
||||
}
|
||||
}
|
||||
@@ -9,34 +9,57 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal func isStrictSuperset(of other: Range<UInt>) -> Bool {
|
||||
_read { $0.isSuperset(of: other) && !$0.isSubset(of: other) }
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public func isStrictSuperset(of other: Self) -> Bool {
|
||||
other.isStrictSubset(of: self)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isStrictSuperset<I: FixedWidthInteger>(
|
||||
of other: BitSet<I>
|
||||
) -> Bool {
|
||||
other.isStrictSubset(of: self)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isStrictSuperset<S: Sequence>(of other: S) -> Bool
|
||||
where S.Element: FixedWidthInteger
|
||||
where S.Element == Element
|
||||
{
|
||||
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>)
|
||||
if S.self == Range<Element>.self {
|
||||
return isStrictSuperset(of: other as! Range<Element>)
|
||||
}
|
||||
return _UnsafeHandle.withTemporaryBitset(
|
||||
wordCount: _storage.count
|
||||
wordCount: _core._storage.count
|
||||
) { seen in
|
||||
for i in other {
|
||||
guard let i = UInt(exactly: i) else { return false }
|
||||
if !contains(i) { return false }
|
||||
if !_core.contains(i) { return false }
|
||||
seen.insert(i)
|
||||
}
|
||||
return seen._count < self._count
|
||||
return seen._count < self.count
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isStrictSuperset(of other: Range<Element>) -> Bool {
|
||||
if other.isEmpty { return !isEmpty }
|
||||
if isEmpty { return false }
|
||||
guard let r = other._toUInt() else { return false }
|
||||
return _core.isStrictSuperset(of: r)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isStrictSuperset<I: FixedWidthInteger>(
|
||||
of other: Range<I>
|
||||
@@ -44,11 +67,6 @@ extension BitSet {
|
||||
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) }
|
||||
return _core.isStrictSuperset(of: r)
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,9 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public func isSubset(of other: Self) -> Bool {
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal func isSubset(of other: Self) -> Bool {
|
||||
self._read { first in
|
||||
other._read { second in
|
||||
let w1 = first._words
|
||||
@@ -28,34 +29,56 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func isSubset(of other: Range<UInt>) -> Bool {
|
||||
_read { $0.isSubset(of: other) }
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public func isSubset(of other: Self) -> Bool {
|
||||
_core.isSubset(of: other._core)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isSubset<I: FixedWidthInteger>(
|
||||
of other: BitSet<I>
|
||||
) -> Bool {
|
||||
_core.isSubset(of: other._core)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isSubset<S: Sequence>(of other: S) -> Bool
|
||||
where S.Element: FixedWidthInteger
|
||||
where S.Element == Element
|
||||
{
|
||||
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>)
|
||||
if S.self == Range<Element>.self {
|
||||
return self.isSubset(of: other as! Range<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 }
|
||||
if t._core.remove(i), t.isEmpty { return true }
|
||||
}
|
||||
assert(!t.isEmpty)
|
||||
return false
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isSubset<I: FixedWidthInteger>(of other: Range<I>) -> Bool {
|
||||
public func isSubset(of other: Range<Element>) -> Bool {
|
||||
guard !isEmpty else { return true }
|
||||
return _isSubset(of: other._clampedToUInt())
|
||||
return _core.isSubset(of: other._clampedToUInt())
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _isSubset(of other: Range<UInt>) -> Bool {
|
||||
_read { $0.isSubset(of: other) }
|
||||
@inlinable
|
||||
public func isSubset<I: FixedWidthInteger>(
|
||||
of other: Range<I>
|
||||
) -> Bool {
|
||||
guard !isEmpty else { return true }
|
||||
return _core.isSubset(of: other._clampedToUInt())
|
||||
}
|
||||
}
|
||||
@@ -9,38 +9,58 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal func isSuperset(of other: Range<UInt>) -> Bool {
|
||||
_read { $0.isSuperset(of: other) }
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public func isSuperset(of other: Self) -> Bool {
|
||||
other.isSubset(of: self)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isSuperset<I: FixedWidthInteger>(
|
||||
of other: BitSet<I>
|
||||
) -> Bool {
|
||||
other.isSubset(of: self)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isSuperset<S: Sequence>(of other: S) -> Bool
|
||||
where S.Element: FixedWidthInteger
|
||||
where S.Element == Element
|
||||
{
|
||||
if S.self == BitSet.self {
|
||||
return self.isSuperset(of: other as! BitSet)
|
||||
}
|
||||
if S.self == Range<S.Element>.self {
|
||||
return self.isSuperset(of: other as! Range<S.Element>)
|
||||
if S.self == Range<Element>.self {
|
||||
return self.isSuperset(of: other as! Range<Element>)
|
||||
}
|
||||
for i in other {
|
||||
guard let i = UInt(exactly: i) else { return false }
|
||||
if !contains(i) { return false }
|
||||
if !_core.contains(i) { return false }
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func isSuperset<I: FixedWidthInteger>(of other: Range<I>) -> Bool {
|
||||
public func isSuperset(of other: Range<Element>) -> Bool {
|
||||
if other.isEmpty { return true }
|
||||
if isEmpty { return false }
|
||||
guard let r = other._toUInt() else { return false }
|
||||
return _isSuperset(of: r)
|
||||
return _core.isSuperset(of: r)
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal func _isSuperset(of other: Range<UInt>) -> Bool {
|
||||
_read { $0.isSuperset(of: other) }
|
||||
@inlinable
|
||||
public func isSuperset<I: FixedWidthInteger>(
|
||||
of other: Range<I>
|
||||
) -> Bool {
|
||||
if other.isEmpty { return true }
|
||||
if isEmpty { return false }
|
||||
guard let r = other._toUInt() else { return false }
|
||||
return _core.isSuperset(of: r)
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,9 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public mutating func subtract(_ other: Self) {
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal mutating func subtract(_ other: Self) {
|
||||
_updateThenShrink { target, shrink in
|
||||
other._read { source in
|
||||
target.combineSharedPrefix(
|
||||
@@ -21,6 +22,37 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal mutating func subtract(_ next: () -> UInt?) {
|
||||
_updateThenShrink { handle, shrink in
|
||||
while let value = next() {
|
||||
handle.remove(value)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal mutating func subtract(_ other: Range<UInt>) {
|
||||
guard !other.isEmpty else { return }
|
||||
_updateThenShrink { handle, shrink in
|
||||
handle.subtract(other)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public mutating func subtract(_ other: Self) {
|
||||
_core.subtract(other._core)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func subtract<I: FixedWidthInteger>(
|
||||
_ other: BitSet<I>
|
||||
) {
|
||||
_core.subtract(other._core)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func subtract<S: Sequence>(
|
||||
_ other: __owned S
|
||||
@@ -29,12 +61,12 @@ extension BitSet {
|
||||
self.subtract(other as! BitSet)
|
||||
return
|
||||
}
|
||||
if S.self == Range<S.Element>.self {
|
||||
self.subtract(other as! Range<S.Element>)
|
||||
if S.self == Range<Element>.self {
|
||||
self.subtract(other as! Range<Element>)
|
||||
return
|
||||
}
|
||||
var it = other.makeIterator()
|
||||
_subtract {
|
||||
_core.subtract {
|
||||
while let value = it.next() {
|
||||
if let value = UInt(exactly: value) {
|
||||
return value
|
||||
@@ -44,27 +76,15 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal mutating func _subtract(_ next: () -> UInt?) {
|
||||
_updateThenShrink { handle, shrink in
|
||||
while let value = next() {
|
||||
handle.remove(value)
|
||||
}
|
||||
}
|
||||
@inlinable
|
||||
public mutating func subtract(_ other: Range<Element>) {
|
||||
_core.subtract(other._clampedToUInt())
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func subtract<I: FixedWidthInteger>(
|
||||
_ other: Range<I>
|
||||
) {
|
||||
_subtract(other._clampedToUInt())
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal mutating func _subtract(_ other: Range<UInt>) {
|
||||
guard !other.isEmpty else { return }
|
||||
_updateThenShrink { handle, shrink in
|
||||
handle.subtract(other)
|
||||
}
|
||||
_core.subtract(other._clampedToUInt())
|
||||
}
|
||||
}
|
||||
@@ -9,8 +9,9 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public __consuming func subtracting(_ other: Self) -> Self {
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal __consuming func subtracting(_ other: Self) -> Self {
|
||||
self._read { first in
|
||||
other._read { second in
|
||||
Self(
|
||||
@@ -20,6 +21,20 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public __consuming func subtracting(_ other: Self) -> Self {
|
||||
BitSet(_core: _core.subtracting(other._core))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public __consuming func subtracting<I: FixedWidthInteger>(
|
||||
_ other: BitSet<I>
|
||||
) -> Self {
|
||||
BitSet(_core: _core.subtracting(other._core))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public __consuming func subtracting<S: Sequence>(
|
||||
@@ -32,6 +47,13 @@ extension BitSet {
|
||||
return result
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public __consuming func subtracting(_ other: Range<Element>) -> Self {
|
||||
var result = self
|
||||
result.subtract(other)
|
||||
return result
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public __consuming func subtracting<I: FixedWidthInteger>(
|
||||
_ other: Range<I>
|
||||
|
||||
@@ -9,8 +9,9 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public __consuming func symmetricDifference(_ other: __owned Self) -> Self {
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal __consuming func symmetricDifference(_ other: __owned Self) -> Self {
|
||||
self._read { first in
|
||||
other._read { second in
|
||||
Self(
|
||||
@@ -20,23 +21,27 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
public __consuming func symmetricDifference(_ other: __owned Self) -> Self {
|
||||
BitSet(_core: _core.symmetricDifference(other._core))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public __consuming func symmetricDifference<S: Sequence>(
|
||||
_ other: __owned S
|
||||
) -> Self
|
||||
where S.Element: FixedWidthInteger
|
||||
where S.Element == Element
|
||||
{
|
||||
if S.self == Range<S.Element>.self {
|
||||
return symmetricDifference(other as! Range<S.Element>)
|
||||
if S.self == Range<Element>.self {
|
||||
return symmetricDifference(other as! Range<Element>)
|
||||
}
|
||||
return symmetricDifference(BitSet(other))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public __consuming func symmetricDifference<I: FixedWidthInteger>(
|
||||
_ other: Range<I>
|
||||
) -> Self {
|
||||
public __consuming func symmetricDifference(_ other: Range<Element>) -> Self {
|
||||
var result = self
|
||||
result.formSymmetricDifference(other)
|
||||
return result
|
||||
|
||||
@@ -9,8 +9,9 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
public __consuming func union(_ other: __owned Self) -> Self {
|
||||
extension _BitSet {
|
||||
@usableFromInline
|
||||
internal __consuming func union(_ other: __owned Self) -> Self {
|
||||
self._read { first in
|
||||
other._read { second in
|
||||
Self(
|
||||
@@ -20,12 +21,19 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inlinable
|
||||
public __consuming func union(_ other: __owned Self) -> Self {
|
||||
BitSet(_core: _core.union(other._core))
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public __consuming func union<S: Sequence>(
|
||||
_ other: __owned S
|
||||
) -> Self
|
||||
where S.Element: FixedWidthInteger
|
||||
where S.Element == Element
|
||||
{
|
||||
if S.self == BitSet.self {
|
||||
return union(other as! BitSet)
|
||||
@@ -36,9 +44,7 @@ extension BitSet {
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public __consuming func union<I: FixedWidthInteger>(
|
||||
_ other: Range<I>
|
||||
) -> Self {
|
||||
public __consuming func union(_ other: Range<Element>) -> Self {
|
||||
var result = self
|
||||
result.formUnion(other)
|
||||
return result
|
||||
|
||||
@@ -14,92 +14,20 @@
|
||||
///
|
||||
/// Bit sets implement `SetAlgebra` and provide efficient implementations
|
||||
/// for set operations based on standard binary logic operations.
|
||||
public struct BitSet {
|
||||
public struct BitSet<Element: FixedWidthInteger> {
|
||||
@usableFromInline
|
||||
internal var _storage: [_Word]
|
||||
internal typealias _UnsafeHandle = _BitSet.UnsafeHandle
|
||||
|
||||
@usableFromInline
|
||||
internal var _count: Int
|
||||
internal var _core: _BitSet
|
||||
|
||||
@usableFromInline
|
||||
init(_core: _BitSet) {
|
||||
self._core = _core
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
init(_storage: [_Word], count: Int) {
|
||||
self._storage = _storage
|
||||
self._count = count
|
||||
self.init(_core: _BitSet(storage: _storage, count: count))
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet {
|
||||
@inline(__always)
|
||||
internal func _read<R>(
|
||||
_ body: (_UnsafeHandle) throws -> R
|
||||
) rethrows -> R {
|
||||
try _storage.withUnsafeBufferPointer { words in
|
||||
let handle = _UnsafeHandle(words: words, count: _count, mutable: false)
|
||||
return try body(handle)
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal var _capacity: UInt {
|
||||
UInt(_storage.count) &* UInt(_Word.capacity)
|
||||
}
|
||||
|
||||
internal mutating func _ensureCapacity(limit capacity: UInt) {
|
||||
let desired = _UnsafeHandle.wordCount(forCapacity: capacity)
|
||||
guard _storage.count < desired else { return }
|
||||
_storage.append(
|
||||
contentsOf: repeatElement(.empty, count: desired - _storage.count))
|
||||
}
|
||||
|
||||
internal mutating func _ensureCapacity(forValue value: UInt) {
|
||||
let desiredWord = _UnsafeHandle.Index(value).word
|
||||
guard desiredWord >= _storage.count else { return }
|
||||
_storage.append(
|
||||
contentsOf:
|
||||
repeatElement(.empty, count: desiredWord - _storage.count + 1))
|
||||
}
|
||||
|
||||
internal mutating func _shrink() {
|
||||
let suffix = _read { $0.emptySuffix() }
|
||||
if suffix > 0 { _storage.removeLast(suffix) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
internal mutating func _update<R>(
|
||||
_ body: (inout _UnsafeHandle) throws -> R
|
||||
) rethrows -> R {
|
||||
var c = _count
|
||||
defer {
|
||||
_count = c
|
||||
_checkInvariants()
|
||||
}
|
||||
return try _storage.withUnsafeMutableBufferPointer { words in
|
||||
var handle = _UnsafeHandle(words: words, count: c, mutable: true)
|
||||
defer {
|
||||
assert(c <= words.count * _Word.capacity)
|
||||
c = handle.count
|
||||
}
|
||||
return try body(&handle)
|
||||
}
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
internal mutating func _updateThenShrink<R>(
|
||||
_ body: (_ handle: inout _UnsafeHandle, _ shrink: inout Bool) throws -> R
|
||||
) rethrows -> R {
|
||||
var shrink = true
|
||||
defer {
|
||||
if shrink { _shrink() }
|
||||
_checkInvariants()
|
||||
}
|
||||
return try _storage.withUnsafeMutableBufferPointer { words in
|
||||
var handle = _UnsafeHandle(
|
||||
words: words, count: _count, mutable: true)
|
||||
defer {
|
||||
_count = handle.count
|
||||
}
|
||||
return try body(&handle, &shrink)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+11
-12
@@ -9,12 +9,12 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
extension BitSet {
|
||||
extension _BitSet {
|
||||
/// An unsafe-unowned bitset view over `UInt` storage, providing bit set
|
||||
/// primitives.
|
||||
@usableFromInline
|
||||
@frozen
|
||||
internal struct _UnsafeHandle {
|
||||
internal struct UnsafeHandle {
|
||||
/// An unsafe-unowned storage view.
|
||||
@usableFromInline
|
||||
internal let _words: UnsafeBufferPointer<_Word>
|
||||
@@ -98,7 +98,7 @@ extension BitSet {
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet._UnsafeHandle {
|
||||
extension _BitSet.UnsafeHandle {
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
internal var wordCount: Int {
|
||||
@@ -112,7 +112,7 @@ extension BitSet._UnsafeHandle {
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet._UnsafeHandle {
|
||||
extension _BitSet.UnsafeHandle {
|
||||
@inlinable
|
||||
@inline(__always)
|
||||
static func withTemporaryBitset<R>(
|
||||
@@ -170,7 +170,7 @@ extension BitSet._UnsafeHandle {
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet._UnsafeHandle {
|
||||
extension _BitSet.UnsafeHandle {
|
||||
@usableFromInline
|
||||
@_effects(readnone)
|
||||
@inline(__always)
|
||||
@@ -275,7 +275,7 @@ extension BitSet._UnsafeHandle {
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet._UnsafeHandle: Sequence {
|
||||
extension _BitSet.UnsafeHandle: Sequence {
|
||||
@usableFromInline
|
||||
internal typealias Element = UInt
|
||||
|
||||
@@ -295,10 +295,10 @@ extension BitSet._UnsafeHandle: Sequence {
|
||||
@frozen
|
||||
internal struct Iterator: IteratorProtocol {
|
||||
@usableFromInline
|
||||
internal typealias _UnsafeHandle = BitSet._UnsafeHandle
|
||||
internal typealias _UnsafeHandle = _BitSet.UnsafeHandle
|
||||
|
||||
@usableFromInline
|
||||
internal let bitset: BitSet._UnsafeHandle
|
||||
internal let bitset: _BitSet.UnsafeHandle
|
||||
|
||||
@usableFromInline
|
||||
internal var index: Int
|
||||
@@ -331,7 +331,7 @@ extension BitSet._UnsafeHandle: Sequence {
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet._UnsafeHandle: BidirectionalCollection {
|
||||
extension _BitSet.UnsafeHandle: BidirectionalCollection {
|
||||
@usableFromInline
|
||||
internal typealias Index = _BitPosition
|
||||
|
||||
@@ -400,7 +400,7 @@ extension BitSet._UnsafeHandle: BidirectionalCollection {
|
||||
}
|
||||
}
|
||||
|
||||
extension BitSet._UnsafeHandle {
|
||||
extension _BitSet.UnsafeHandle {
|
||||
internal mutating func combineSharedPrefix(
|
||||
with other: Self,
|
||||
using function: (inout _Word, _Word) -> Void
|
||||
@@ -414,8 +414,7 @@ extension BitSet._UnsafeHandle {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension BitSet._UnsafeHandle {
|
||||
extension _BitSet.UnsafeHandle {
|
||||
internal func count(in range: Range<UInt>) -> Int {
|
||||
let l = Index(Swift.min(range.lowerBound, capacity))
|
||||
let u = Index(Swift.min(range.upperBound, capacity))
|
||||
@@ -0,0 +1,105 @@
|
||||
//===----------------------------------------------------------------------===//
|
||||
//
|
||||
// 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
public struct _BitSet {
|
||||
@usableFromInline
|
||||
internal var _storage: [_Word]
|
||||
|
||||
@usableFromInline
|
||||
internal var _count: Int
|
||||
|
||||
@usableFromInline
|
||||
init() {
|
||||
_storage = []
|
||||
_count = 0
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
init(storage: [_Word], count: Int) {
|
||||
self._storage = storage
|
||||
self._count = count
|
||||
}
|
||||
}
|
||||
|
||||
extension _BitSet {
|
||||
@inline(__always)
|
||||
internal func _read<R>(
|
||||
_ body: (UnsafeHandle) throws -> R
|
||||
) rethrows -> R {
|
||||
try _storage.withUnsafeBufferPointer { words in
|
||||
let handle = UnsafeHandle(words: words, count: _count, mutable: false)
|
||||
return try body(handle)
|
||||
}
|
||||
}
|
||||
|
||||
@usableFromInline
|
||||
internal var _capacity: UInt {
|
||||
UInt(_storage.count) &* UInt(_Word.capacity)
|
||||
}
|
||||
|
||||
internal mutating func _ensureCapacity(limit capacity: UInt) {
|
||||
let desired = UnsafeHandle.wordCount(forCapacity: capacity)
|
||||
guard _storage.count < desired else { return }
|
||||
_storage.append(
|
||||
contentsOf: repeatElement(.empty, count: desired - _storage.count))
|
||||
}
|
||||
|
||||
internal mutating func _ensureCapacity(forValue value: UInt) {
|
||||
let desiredWord = UnsafeHandle.Index(value).word
|
||||
guard desiredWord >= _storage.count else { return }
|
||||
_storage.append(
|
||||
contentsOf:
|
||||
repeatElement(.empty, count: desiredWord - _storage.count + 1))
|
||||
}
|
||||
|
||||
internal mutating func _shrink() {
|
||||
let suffix = _read { $0.emptySuffix() }
|
||||
if suffix > 0 { _storage.removeLast(suffix) }
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
internal mutating func _update<R>(
|
||||
_ body: (inout UnsafeHandle) throws -> R
|
||||
) rethrows -> R {
|
||||
var c = _count
|
||||
defer {
|
||||
_count = c
|
||||
_checkInvariants()
|
||||
}
|
||||
return try _storage.withUnsafeMutableBufferPointer { words in
|
||||
var handle = UnsafeHandle(words: words, count: c, mutable: true)
|
||||
defer {
|
||||
assert(c <= words.count * _Word.capacity)
|
||||
c = handle.count
|
||||
}
|
||||
return try body(&handle)
|
||||
}
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
internal mutating func _updateThenShrink<R>(
|
||||
_ body: (_ handle: inout UnsafeHandle, _ shrink: inout Bool) throws -> R
|
||||
) rethrows -> R {
|
||||
var shrink = true
|
||||
defer {
|
||||
if shrink { _shrink() }
|
||||
_checkInvariants()
|
||||
}
|
||||
return try _storage.withUnsafeMutableBufferPointer { words in
|
||||
var handle = UnsafeHandle(
|
||||
words: words, count: _count, mutable: true)
|
||||
defer {
|
||||
_count = handle.count
|
||||
}
|
||||
return try body(&handle, &shrink)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -15,14 +15,14 @@ import BitCollections
|
||||
|
||||
final class BitSetTest: CollectionTestCase {
|
||||
func test_empty_initializer() {
|
||||
let set = BitSet()
|
||||
let set = BitSet<Int>()
|
||||
expectEqual(set.count, 0)
|
||||
expectTrue(set.isEmpty)
|
||||
expectEqualElements(set, [])
|
||||
}
|
||||
|
||||
func test_array_literal_initializer() {
|
||||
let set0: BitSet = []
|
||||
let set0: BitSet<Int> = []
|
||||
expectEqual(set0.count, 0)
|
||||
expectTrue(set0.isEmpty)
|
||||
expectEqualElements(set0, [])
|
||||
@@ -54,19 +54,19 @@ final class BitSetTest: CollectionTestCase {
|
||||
}
|
||||
|
||||
func test_bitPattern_initializer() {
|
||||
let s1 = BitSet(bitPattern: 0 as UInt)
|
||||
let s1 = BitSet<Int>(bitPattern: 0 as UInt)
|
||||
expectEqualElements(s1, [])
|
||||
|
||||
let s2 = BitSet(bitPattern: 1 as UInt)
|
||||
let s2 = BitSet<Int>(bitPattern: 1 as UInt)
|
||||
expectEqualElements(s2, [0])
|
||||
|
||||
let s3 = BitSet(bitPattern: 2 as UInt)
|
||||
let s3 = BitSet<Int>(bitPattern: 2 as UInt)
|
||||
expectEqualElements(s3, [1])
|
||||
|
||||
let s4 = BitSet(bitPattern: 23)
|
||||
let s4 = BitSet<Int>(bitPattern: 23)
|
||||
expectEqualElements(s4, [0, 1, 2, 4])
|
||||
|
||||
let s5 = BitSet(bitPattern: -1)
|
||||
let s5 = BitSet<Int>(bitPattern: -1)
|
||||
expectEqualElements(s5, 0 ..< UInt.bitWidth)
|
||||
}
|
||||
|
||||
@@ -171,7 +171,7 @@ final class BitSetTest: CollectionTestCase {
|
||||
|
||||
func test_contains_Int32() {
|
||||
withInterestingSets("input", maximum: 1000) { input in
|
||||
let bitset = BitSet(input)
|
||||
let bitset = BitSet(input.map { Int32($0) })
|
||||
withEvery("value", in: 0 ..< 1000) { value in
|
||||
expectEqual(bitset.contains(Int32(value)), input.contains(value))
|
||||
}
|
||||
@@ -201,18 +201,18 @@ final class BitSetTest: CollectionTestCase {
|
||||
func checkInsert<F: FixedWidthInteger>(for type: F.Type, count: Int) {
|
||||
withEvery("seed", in: 0 ..< 10) { seed in
|
||||
var rng = RepeatableRandomNumberGenerator(seed: seed)
|
||||
var actual: BitSet = []
|
||||
var expected: Set<Int> = []
|
||||
let input = (0 ..< count).shuffled(using: &rng)
|
||||
var actual: BitSet<F> = []
|
||||
var expected: Set<F> = []
|
||||
let input = (0 ..< count).shuffled(using: &rng).map { F($0) }
|
||||
withEvery("i", in: input.indices) { i in
|
||||
let (i1, m1) = actual.insert(F(input[i]))
|
||||
let (i1, m1) = actual.insert(input[i])
|
||||
expected.insert(input[i])
|
||||
expectTrue(i1)
|
||||
expectEqual(m1, F(input[i]))
|
||||
expectEqual(m1, input[i])
|
||||
if i % 25 == 0 {
|
||||
expectEqual(Array(actual), expected.sorted())
|
||||
}
|
||||
let (i2, m2) = actual.insert(F(input[i]))
|
||||
let (i2, m2) = actual.insert(input[i])
|
||||
expectFalse(i2)
|
||||
expectEqual(m2, m1)
|
||||
}
|
||||
@@ -233,17 +233,17 @@ final class BitSetTest: CollectionTestCase {
|
||||
func checkUpdate<F: FixedWidthInteger>(for type: F.Type, count: Int) {
|
||||
withEvery("seed", in: 0 ..< 10) { seed in
|
||||
var rng = RepeatableRandomNumberGenerator(seed: seed)
|
||||
var actual: BitSet = []
|
||||
var expected: Set<Int> = []
|
||||
let input = (0 ..< count).shuffled(using: &rng)
|
||||
var actual: BitSet<F> = []
|
||||
var expected: Set<F> = []
|
||||
let input = (0 ..< count).shuffled(using: &rng).map { F($0) }
|
||||
withEvery("i", in: input.indices) { i in
|
||||
let old = actual.update(with: F(input[i]))
|
||||
let old = actual.update(with: input[i])
|
||||
expected.update(with: input[i])
|
||||
expectEqual(old, F(input[i]))
|
||||
expectEqual(old, input[i])
|
||||
if i % 25 == 0 {
|
||||
expectEqual(Array(actual), expected.sorted())
|
||||
}
|
||||
expectNil(actual.update(with: F(input[i])))
|
||||
expectNil(actual.update(with: input[i]))
|
||||
}
|
||||
expectEqual(Array(actual), expected.sorted())
|
||||
}
|
||||
@@ -264,9 +264,9 @@ final class BitSetTest: CollectionTestCase {
|
||||
context.withTrace("\(type)") {
|
||||
withEvery("seed", in: 0 ..< 10) { seed in
|
||||
var rng = RepeatableRandomNumberGenerator(seed: seed)
|
||||
var actual = BitSet(0 ..< count)
|
||||
var expected = Set<Int>(0 ..< count)
|
||||
let input = (0 ..< count).shuffled(using: &rng)
|
||||
var actual = BitSet<F>(0 ..< F(count))
|
||||
var expected = Set<F>(0 ..< F(count))
|
||||
let input = (0 ..< F(count)).shuffled(using: &rng)
|
||||
|
||||
if F.isSigned {
|
||||
expectNil(actual.remove(F(-1)))
|
||||
@@ -312,8 +312,11 @@ final class BitSetTest: CollectionTestCase {
|
||||
let actual = c.union(b)
|
||||
expectEqualElements(actual, expected)
|
||||
|
||||
func union<S: Sequence>(_ first: BitSet, _ second: S) -> BitSet
|
||||
where S.Element: FixedWidthInteger {
|
||||
func union<F: FixedWidthInteger, S: Sequence>(
|
||||
_ first: BitSet<F>,
|
||||
_ second: S
|
||||
) -> BitSet<F>
|
||||
where S.Element == F {
|
||||
first.union(second)
|
||||
}
|
||||
expectEqualElements(union(c, BitSet(b)), expected)
|
||||
@@ -323,7 +326,7 @@ final class BitSetTest: CollectionTestCase {
|
||||
|
||||
func test_union_Range() {
|
||||
withEvery("step", in: [1, 5, 16, 23, 24, UInt.bitWidth]) { step in
|
||||
let a = BitSet()
|
||||
let a = BitSet<Int>()
|
||||
|
||||
let b = a.union(0 ..< 5*step)
|
||||
expectEqualElements(b, 0 ..< 5*step)
|
||||
@@ -365,8 +368,9 @@ final class BitSetTest: CollectionTestCase {
|
||||
c.formUnion(b)
|
||||
expectEqualElements(c, expected)
|
||||
|
||||
func union<S: Sequence>(_ first: inout BitSet, _ second: S)
|
||||
where S.Element: FixedWidthInteger {
|
||||
func union<F: FixedWidthInteger, S: Sequence>(
|
||||
_ first: inout BitSet<F>, _ second: S)
|
||||
where S.Element == F {
|
||||
first.formUnion(second)
|
||||
}
|
||||
var d = BitSet(a)
|
||||
@@ -380,7 +384,7 @@ final class BitSetTest: CollectionTestCase {
|
||||
|
||||
func test_formUnion_Range() {
|
||||
withEvery("step", in: [1, 5, 16, 23, 24, UInt.bitWidth]) { step in
|
||||
var a = BitSet()
|
||||
var a = BitSet<Int>()
|
||||
|
||||
a.formUnion(0 ..< 5*step)
|
||||
expectEqualElements(a, 0 ..< 5*step)
|
||||
@@ -425,8 +429,11 @@ final class BitSetTest: CollectionTestCase {
|
||||
expectEqualElements(c.intersection([-100]), [])
|
||||
expectEqualElements(c.intersection(-100 ..< -10), [])
|
||||
|
||||
func intersection<S: Sequence>(_ first: BitSet, _ second: S) -> BitSet
|
||||
where S.Element: FixedWidthInteger {
|
||||
func intersection<F: FixedWidthInteger, S: Sequence>(
|
||||
_ first: BitSet<F>,
|
||||
_ second: S
|
||||
) -> BitSet<F>
|
||||
where S.Element == F {
|
||||
first.intersection(second)
|
||||
}
|
||||
let d = intersection(c, BitSet(b))
|
||||
@@ -473,8 +480,11 @@ final class BitSetTest: CollectionTestCase {
|
||||
}
|
||||
|
||||
func test_formIntersection_Sequence() {
|
||||
func intersection<S: Sequence>(_ first: inout BitSet, _ second: S)
|
||||
where S.Element: FixedWidthInteger {
|
||||
func intersection<F: FixedWidthInteger, S: Sequence>(
|
||||
_ first: inout BitSet<F>,
|
||||
_ second: S
|
||||
)
|
||||
where S.Element == F {
|
||||
first.formIntersection(second)
|
||||
}
|
||||
|
||||
@@ -558,11 +568,11 @@ final class BitSetTest: CollectionTestCase {
|
||||
let actual = c.symmetricDifference(b)
|
||||
expectEqualElements(actual, expected)
|
||||
|
||||
func symmetricDifference<S: Sequence>(
|
||||
_ first: BitSet,
|
||||
func symmetricDifference<F: FixedWidthInteger, S: Sequence>(
|
||||
_ first: BitSet<F>,
|
||||
_ second: S
|
||||
) -> BitSet
|
||||
where S.Element: FixedWidthInteger {
|
||||
) -> BitSet<F>
|
||||
where S.Element == F {
|
||||
first.symmetricDifference(second)
|
||||
}
|
||||
let d = symmetricDifference(c, BitSet(b))
|
||||
@@ -579,7 +589,7 @@ final class BitSetTest: CollectionTestCase {
|
||||
|
||||
func test_symmetricDifference_Range() {
|
||||
withEvery("step", in: [1, 5, 16, 23, 24, UInt.bitWidth]) { step in
|
||||
let a = BitSet()
|
||||
let a = BitSet<Int>()
|
||||
|
||||
let b = a.symmetricDifference(0 ..< 10*step)
|
||||
expectEqualElements(b, 0 ..< 10*step)
|
||||
@@ -618,10 +628,10 @@ final class BitSetTest: CollectionTestCase {
|
||||
c.formSymmetricDifference(b)
|
||||
expectEqualElements(c, expected)
|
||||
|
||||
func symmetricDifference<S: Sequence>(
|
||||
_ first: inout BitSet,
|
||||
func symmetricDifference<F: FixedWidthInteger, S: Sequence>(
|
||||
_ first: inout BitSet<F>,
|
||||
_ second: S
|
||||
) where S.Element: FixedWidthInteger {
|
||||
) where S.Element == F {
|
||||
first.formSymmetricDifference(second)
|
||||
}
|
||||
var d = BitSet(a)
|
||||
@@ -639,7 +649,7 @@ final class BitSetTest: CollectionTestCase {
|
||||
|
||||
func test_formSymmetricDifference_Range() {
|
||||
withEvery("step", in: [1, 5, 16, 23, 24, UInt.bitWidth]) { step in
|
||||
var a = BitSet()
|
||||
var a = BitSet<Int>()
|
||||
|
||||
a.formSymmetricDifference(0 ..< 10*step)
|
||||
expectEqualElements(a, 0 ..< 10*step)
|
||||
@@ -672,10 +682,10 @@ final class BitSetTest: CollectionTestCase {
|
||||
}
|
||||
|
||||
func test_subtracting_Sequence() {
|
||||
func subtracting<S: Sequence>(
|
||||
_ first: BitSet,
|
||||
func subtracting<F: FixedWidthInteger, S: Sequence>(
|
||||
_ first: BitSet<F>,
|
||||
_ second: S
|
||||
) -> BitSet
|
||||
) -> BitSet<F>
|
||||
where S.Element: FixedWidthInteger {
|
||||
first.subtracting(second)
|
||||
}
|
||||
@@ -734,11 +744,11 @@ final class BitSetTest: CollectionTestCase {
|
||||
}
|
||||
|
||||
func test_subtract_Sequence() {
|
||||
func subtract<S: Sequence>(
|
||||
_ first: inout BitSet,
|
||||
func subtract<F: FixedWidthInteger, S: Sequence>(
|
||||
_ first: inout BitSet<F>,
|
||||
_ second: S
|
||||
)
|
||||
where S.Element: FixedWidthInteger {
|
||||
where S.Element == F {
|
||||
first.subtract(second)
|
||||
}
|
||||
|
||||
@@ -819,7 +829,7 @@ final class BitSetTest: CollectionTestCase {
|
||||
let expected = test.expected.contains(input)
|
||||
|
||||
func forceSequence<S: Sequence>(_ other: S) -> Bool
|
||||
where S.Element: FixedWidthInteger {
|
||||
where S.Element == Int {
|
||||
set.isSubset(of: other)
|
||||
}
|
||||
|
||||
@@ -868,7 +878,7 @@ final class BitSetTest: CollectionTestCase {
|
||||
let expected = test.expected.contains(input)
|
||||
|
||||
func forceSequence<S: Sequence>(_ other: S) -> Bool
|
||||
where S.Element: FixedWidthInteger {
|
||||
where S.Element == Int {
|
||||
set.isStrictSubset(of: other)
|
||||
}
|
||||
|
||||
@@ -917,7 +927,7 @@ final class BitSetTest: CollectionTestCase {
|
||||
let expected = test.expected.contains(input)
|
||||
|
||||
func forceSequence<S: Sequence>(_ other: S) -> Bool
|
||||
where S.Element: FixedWidthInteger {
|
||||
where S.Element == Int {
|
||||
set.isSuperset(of: other)
|
||||
}
|
||||
|
||||
@@ -966,7 +976,7 @@ final class BitSetTest: CollectionTestCase {
|
||||
let expected = test.expected.contains(input)
|
||||
|
||||
func forceSequence<S: Sequence>(_ other: S) -> Bool
|
||||
where S.Element: FixedWidthInteger {
|
||||
where S.Element == Int {
|
||||
set.isStrictSuperset(of: other)
|
||||
}
|
||||
|
||||
@@ -1017,7 +1027,7 @@ final class BitSetTest: CollectionTestCase {
|
||||
let expected = test.expected.contains(input)
|
||||
|
||||
func forceSequence<S: Sequence>(_ other: S) -> Bool
|
||||
where S.Element: FixedWidthInteger {
|
||||
where S.Element == Int {
|
||||
set.isDisjoint(with: other)
|
||||
}
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@
|
||||
"repositoryURL": "https://github.com/apple/swift-argument-parser",
|
||||
"state": {
|
||||
"branch": null,
|
||||
"revision": "f3c9084a71ef4376f2fabbdf1d3d90a49f1fabdb",
|
||||
"version": "1.1.2"
|
||||
"revision": "df9ee6676cd5b3bf5b330ec7568a5644f547201b",
|
||||
"version": "1.1.3"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user