From cbf933665b327de80bcdd18ced7ceeb415704690 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 12 Aug 2022 19:22:24 -0700 Subject: [PATCH] [BitSet] Revert to non-generic BitSet Also, remove members that were generic over FixedWidthInteger. --- .../BitArray/BitArray+Initializers.swift | 4 +- .../BitCollections/BitArray/BitArray.swift | 2 + .../BitSet+BidirectionalCollection.swift | 42 ++--- .../BitSet/BitSet+Equatable.swift | 2 +- .../BitSet+ExpressibleByArrayLiteral.swift | 3 +- .../BitSet/BitSet+Hashable.swift | 2 +- .../BitSet/BitSet+Initializers.swift | 35 ++-- .../BitSet/BitSet+Invariants.swift | 2 +- .../BitSet/BitSet+SetAlgebra basics.swift | 52 +++--- .../BitSet+SetAlgebra formIntersection.swift | 56 ++---- ...t+SetAlgebra formSymmetricDifference.swift | 54 ++---- .../BitSet/BitSet+SetAlgebra formUnion.swift | 64 +++---- .../BitSet+SetAlgebra intersection.swift | 20 +- .../BitSet/BitSet+SetAlgebra isDisjoint.swift | 42 +---- .../BitSet+SetAlgebra isStrictSubset.swift | 37 +--- .../BitSet+SetAlgebra isStrictSuperset.swift | 43 +---- .../BitSet/BitSet+SetAlgebra isSubset.swift | 45 +---- .../BitSet/BitSet+SetAlgebra isSuperset.swift | 38 +--- .../BitSet/BitSet+SetAlgebra subtract.swift | 53 ++---- .../BitSet+SetAlgebra subtracting.swift | 33 +--- ...itSet+SetAlgebra symmetricDifference.swift | 20 +- .../BitSet/BitSet+SetAlgebra union.swift | 17 +- ...andle.swift => BitSet._UnsafeHandle.swift} | 22 +-- Sources/BitCollections/BitSet/BitSet.swift | 88 ++++++++- Sources/BitCollections/BitSet/_BitSet.swift | 105 ----------- Tests/BitCollectionsTests/BitSetTests.swift | 174 ++++++------------ .../xcschemes/BitCollections.xcscheme | 12 +- 27 files changed, 356 insertions(+), 711 deletions(-) rename Sources/BitCollections/BitSet/{_BitSet.UnsafeHandle.swift => BitSet._UnsafeHandle.swift} (97%) delete mode 100644 Sources/BitCollections/BitSet/_BitSet.swift diff --git a/Sources/BitCollections/BitArray/BitArray+Initializers.swift b/Sources/BitCollections/BitArray/BitArray+Initializers.swift index f0ffc5fa..d9e2a0a1 100644 --- a/Sources/BitCollections/BitArray/BitArray+Initializers.swift +++ b/Sources/BitCollections/BitArray/BitArray+Initializers.swift @@ -10,9 +10,9 @@ //===----------------------------------------------------------------------===// extension BitArray { - public init(_ set: BitSet) { + public init(_ set: BitSet) { guard let l = set.last else { self.init(); return } - self.init(_storage: set._core._storage, count: l + 1) + self.init(_storage: set._storage, count: l + 1) } public init(bitPattern value: I) { diff --git a/Sources/BitCollections/BitArray/BitArray.swift b/Sources/BitCollections/BitArray/BitArray.swift index 52c4ab27..a2f2d05b 100644 --- a/Sources/BitCollections/BitArray/BitArray.swift +++ b/Sources/BitCollections/BitArray/BitArray.swift @@ -13,6 +13,8 @@ public struct BitArray { @usableFromInline internal var _storage: [_Word] + /// The number of bits in the bit array. This may less than the number of bits + /// in `_storage` if the last word isn't fully filled. @usableFromInline internal var _count: UInt diff --git a/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift b/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift index 366d3e7f..1d06dc73 100644 --- a/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift +++ b/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift @@ -10,6 +10,8 @@ //===----------------------------------------------------------------------===// extension BitSet: Sequence { + public typealias Element = Int + @inlinable @inline(__always) public var underestimatedCount: Int { @@ -22,14 +24,14 @@ extension BitSet: Sequence { } public func _customContainsEquatableElement( - _ element: Element + _ element: Int ) -> Bool? { guard let element = UInt(exactly: element) else { return false } - return _core._read { $0.contains(element) } + return _contains(element) } public struct Iterator: IteratorProtocol { - internal typealias _UnsafeHandle = _BitSet.UnsafeHandle + internal typealias _UnsafeHandle = BitSet._UnsafeHandle internal let bitset: BitSet internal var index: Int @@ -39,25 +41,25 @@ extension BitSet: Sequence { internal init(_ bitset: BitSet) { self.bitset = bitset self.index = 0 - self.word = bitset._core._read { handle in + self.word = bitset._read { handle in guard handle.wordCount > 0 else { return .empty } return handle._words[0] } } @_effects(releasenone) - public mutating func next() -> Element? { + public mutating func next() -> Int? { if let bit = word.next() { let i = _UnsafeHandle.Index(word: index, bit: bit) - return Element(truncatingIfNeeded: i.value) + return Int(truncatingIfNeeded: i.value) } - return bitset._core._read { handle in + return bitset._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 Element(truncatingIfNeeded: i.value) + return Int(truncatingIfNeeded: i.value) } } return nil @@ -67,28 +69,28 @@ extension BitSet: Sequence { } extension BitSet: Collection, BidirectionalCollection { - public var isEmpty: Bool { _core._count == 0 } - public var count: Int { _core._count } + public var isEmpty: Bool { _count == 0 } + public var count: Int { _count } public var startIndex: Index { // Note: This is O(n) - Index(_position: _core._read { $0.startIndex }) + Index(_position: _read { $0.startIndex }) } public var endIndex: Index { - Index(_position: .init(word: _core._storage.count, bit: 0)) + Index(_position: .init(word: _storage.count, bit: 0)) } - public subscript(position: Index) -> Element { - Element(position._position.value) + public subscript(position: Index) -> Int { + Int(bitPattern: position._position.value) } public func index(after index: Index) -> Index { - Index(_position: _core._read { $0.index(after: index._position) }) + Index(_position: _read { $0.index(after: index._position) }) } public func index(before index: Index) -> Index { - Index(_position: _core._read { $0.index(before: index._position) }) + Index(_position: _read { $0.index(before: index._position) }) } #if false // TODO: Specialize these @@ -107,14 +109,12 @@ extension BitSet: Collection, BidirectionalCollection { } #endif - public func _customIndexOfEquatableElement(_ element: Element) -> Index?? { + public func _customIndexOfEquatableElement(_ element: Int) -> Index?? { guard contains(element) else { return .some(nil) } - return Index(_value: UInt(element)) + return Index(_value: UInt(bitPattern: element)) } - public func _customLastIndexOfEquatableElement( - _ element: Element - ) -> Index?? { + public func _customLastIndexOfEquatableElement(_ element: Int) -> Index?? { _customIndexOfEquatableElement(element) } } diff --git a/Sources/BitCollections/BitSet/BitSet+Equatable.swift b/Sources/BitCollections/BitSet/BitSet+Equatable.swift index 02631182..7f1d95cc 100644 --- a/Sources/BitCollections/BitSet/BitSet+Equatable.swift +++ b/Sources/BitCollections/BitSet/BitSet+Equatable.swift @@ -11,6 +11,6 @@ extension BitSet: Equatable { public static func ==(left: Self, right: Self) -> Bool { - left._core._storage == right._core._storage + left._storage == right._storage } } diff --git a/Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift b/Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift index e79ad9ba..91d6e694 100644 --- a/Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift +++ b/Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift @@ -10,7 +10,8 @@ //===----------------------------------------------------------------------===// extension BitSet: ExpressibleByArrayLiteral { - public init(arrayLiteral elements: Element...) { + @inlinable + public init(arrayLiteral elements: Int...) { self.init(elements) } } diff --git a/Sources/BitCollections/BitSet/BitSet+Hashable.swift b/Sources/BitCollections/BitSet/BitSet+Hashable.swift index b5a92cf9..16d7bee3 100644 --- a/Sources/BitCollections/BitSet/BitSet+Hashable.swift +++ b/Sources/BitCollections/BitSet/BitSet+Hashable.swift @@ -11,6 +11,6 @@ extension BitSet: Hashable { public func hash(into hasher: inout Hasher) { - hasher.combine(_core._storage) + hasher.combine(_storage) } } diff --git a/Sources/BitCollections/BitSet/BitSet+Initializers.swift b/Sources/BitCollections/BitSet/BitSet+Initializers.swift index be233fbb..c695563c 100644 --- a/Sources/BitCollections/BitSet/BitSet+Initializers.swift +++ b/Sources/BitCollections/BitSet/BitSet+Initializers.swift @@ -17,7 +17,7 @@ extension BitSet { @inlinable public init( _ elements: __owned S - ) where S.Element == Element { + ) where S.Element == Int { if S.self == BitSet.self { self = (elements as! BitSet) return @@ -31,45 +31,38 @@ extension BitSet { @inlinable internal init( _validMembersOf elements: __owned S - ) where S.Element == Element { + ) where S.Element == Int { if S.self == BitSet.self { self = (elements as! BitSet) return } - if S.self == Range.self { - let r = (elements as! Range) - self.init(_core: _BitSet(r._clampedToUInt())) + if S.self == Range.self { + let r = (elements as! Range) + self.init(_range: r._clampedToUInt()) return } self.init() for value in elements { guard let value = UInt(exactly: value) else { continue } - _ = self._core.insert(value) + self._insert(value) } } } extension BitSet { - @inlinable - public init(_ range: Range) { - guard - let lower = UInt(exactly: range.lowerBound), - let upper = UInt(exactly: range.upperBound) - else { + public init(_ range: Range) { + guard let range = range._toUInt() else { preconditionFailure("BitSet can only hold nonnegative integers") } - let range = Range(uncheckedBounds: (lower, upper)) - self.init(_core: _BitSet(range)) + self.init(_range: range) } -} -extension _BitSet { @usableFromInline - internal init(_ range: Range) { + internal init(_range range: Range) { _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)) } @@ -98,9 +91,9 @@ extension BitSet { } } -extension _BitSet { +extension BitSet { internal init( - _combining handles: (UnsafeHandle, UnsafeHandle), + _combining handles: (_UnsafeHandle, _UnsafeHandle), includingTail: Bool, using function: (_Word, _Word) -> _Word ) { diff --git a/Sources/BitCollections/BitSet/BitSet+Invariants.swift b/Sources/BitCollections/BitSet/BitSet+Invariants.swift index 6950d901..fa77f054 100644 --- a/Sources/BitCollections/BitSet/BitSet+Invariants.swift +++ b/Sources/BitCollections/BitSet/BitSet+Invariants.swift @@ -9,7 +9,7 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { +extension BitSet { #if COLLECTIONS_INTERNAL_CHECKS @inline(never) @_effects(releasenone) diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra basics.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra basics.swift index 2ff3f499..2435f544 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra basics.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra basics.swift @@ -9,65 +9,55 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { +extension BitSet { @usableFromInline - internal func contains(_ member: UInt) -> Bool { + internal func _contains(_ member: UInt) -> Bool { _read { $0.contains(member) } } -} -extension BitSet { - @inlinable - 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( - _ newMember: UInt - ) -> Bool { - _ensureCapacity(forValue: newMember) - return _update { $0.insert(newMember) } + public func contains(_ member: Int) -> Bool { + guard let member = UInt(exactly: member) else { return false } + return _contains(member) } } extension BitSet { - @inlinable + @discardableResult + public mutating func _insert(_ i: UInt) -> Bool { + _ensureCapacity(forValue: i) + let inserted = _update { $0.insert(i) } + return inserted + } + @discardableResult public mutating func insert( - _ newMember: Element - ) -> (inserted: Bool, memberAfterInsert: Element) { + _ newMember: Int + ) -> (inserted: Bool, memberAfterInsert: Int) { guard let i = UInt(exactly: newMember) else { preconditionFailure("Value out of range") } - return (_core.insert(i), newMember) + return (_insert(i), newMember) } - @inlinable @discardableResult - public mutating func update(with newMember: Element) -> Element? { + public mutating func update(with newMember: Int) -> Int? { insert(newMember).inserted ? newMember : nil } } -extension _BitSet { +extension BitSet { + @discardableResult @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? { + public mutating func remove(_ member: Int) -> Int? { guard let m = UInt(exactly: member) else { return nil } - return _core.remove(m) ? member : nil + return _remove(m) ? member : nil } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift index dfd930c2..5aa5f3d4 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift @@ -9,9 +9,8 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { - @usableFromInline - internal mutating func formIntersection(_ other: Self) { +extension BitSet { + public mutating func formIntersection(_ other: Self) { other._read { source in if source.wordCount < _storage.count { self._storage.removeLast(_storage.count - source.wordCount) @@ -23,13 +22,24 @@ extension _BitSet { } } - @usableFromInline - internal mutating func formIntersection(_ other: Range) { - guard let last = other.last else { - self = _BitSet() + @inlinable + public mutating func formIntersection( + _ other: __owned S + ) where S.Element == Int { + if S.self == Range.self { + formIntersection(other as! Range) return } - let lastWord = UnsafeHandle.Index(last).word + formIntersection(BitSet(_validMembersOf: other)) + } + + public mutating func formIntersection(_ other: Range) { + let other = other._clampedToUInt() + guard let last = other.last else { + self = BitSet() + return + } + let lastWord = _UnsafeHandle.Index(last).word if _storage.count - lastWord - 1 > 0 { _storage.removeLast(_storage.count - lastWord - 1) } @@ -38,33 +48,3 @@ extension _BitSet { } } } - -extension BitSet { - @inlinable - public mutating func formIntersection(_ other: Self) { - _core.formIntersection(other._core) - } - - @inlinable - public mutating func formIntersection( - _ other: BitSet - ) { - _core.formIntersection(other._core) - } - - @inlinable - public mutating func formIntersection( - _ other: __owned S - ) where S.Element == Element { - if S.self == Range.self { - formIntersection(other as! Range) - return - } - formIntersection(BitSet(_validMembersOf: other)) - } - - @inlinable - public mutating func formIntersection(_ other: Range) { - _core.formIntersection(other._clampedToUInt()) - } -} diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift index b1c95681..cec48a92 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift @@ -9,9 +9,8 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { - @usableFromInline - internal mutating func formSymmetricDifference(_ other: __owned Self) { +extension BitSet { + public mutating func formSymmetricDifference(_ other: __owned Self) { _ensureCapacity(limit: other._capacity) _updateThenShrink { target, shrink in other._read { source in @@ -21,8 +20,23 @@ extension _BitSet { } } - @usableFromInline - internal mutating func formSymmetricDifference(_ other: Range) { + @inlinable + public mutating func formSymmetricDifference( + _ other: __owned S + ) where S.Element == Int { + if S.self == Range.self { + formSymmetricDifference(other as! Range) + return + } + formSymmetricDifference(BitSet(other)) + } +} + +extension BitSet { + public mutating func formSymmetricDifference(_ other: Range) { + guard let other = other._toUInt() else { + preconditionFailure("Invalid range") + } guard !other.isEmpty else { return } _ensureCapacity(limit: other.upperBound) _updateThenShrink { handle, shrink in @@ -30,33 +44,3 @@ extension _BitSet { } } } - -extension BitSet { - @inlinable - public mutating func formSymmetricDifference(_ other: __owned Self) { - _core.formSymmetricDifference(other._core) - } - - @inlinable - public mutating func formSymmetricDifference( - _ other: __owned S - ) where S.Element == Element { - if S.self == Range.self { - formSymmetricDifference(other as! Range) - return - } - _core.formSymmetricDifference(BitSet(other)._core) - } -} - -extension BitSet { - @inlinable - public mutating func formSymmetricDifference( - _ other: Range - ) { - guard let other = other._toUInt() else { - preconditionFailure("Invalid range") - } - _core.formSymmetricDifference(other) - } -} diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift index 9c7ed3c2..4b407229 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift @@ -9,9 +9,14 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { +extension BitSet { @usableFromInline - internal mutating func formUnion(_ other: __owned Self) { + internal mutating func formUnion(_ other: Range) { + } +} + +extension BitSet { + public mutating func formUnion(_ other: __owned Self) { _ensureCapacity(limit: other._capacity) _update { target in other._read { source in @@ -20,8 +25,27 @@ extension _BitSet { } } - @usableFromInline - internal mutating func formUnion(_ other: Range) { + @inlinable + public mutating func formUnion( + _ other: __owned S + ) where S.Element == Int { + if S.self == BitSet.self { + formUnion(other as! BitSet) + return + } + if S.self == Range.self { + formUnion(other as! Range) + return + } + for value in other { + self.insert(value) + } + } + + public mutating func formUnion(_ other: Range) { + guard let other = other._toUInt() else { + preconditionFailure("Invalid range") + } guard !other.isEmpty else { return } _ensureCapacity(limit: other.upperBound) _update { handle in @@ -30,35 +54,3 @@ extension _BitSet { } } -extension BitSet { - @inlinable - public mutating func formUnion(_ other: __owned Self) { - _core.formUnion(other._core) - } - - @inlinable - public mutating func formUnion( - _ other: __owned S - ) where S.Element == Element { - if S.self == BitSet.self { - formUnion(other as! BitSet) - return - } - if S.self == Range.self { - formUnion(other as! Range) - return - } - for value in other { - self.insert(value) - } - } - - @inlinable - public mutating func formUnion(_ other: Range) { - guard let other = other._toUInt() else { - preconditionFailure("Invalid range") - } - _core.formUnion(other) - } -} - diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift index fd1f75f7..d1ce2833 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift @@ -9,9 +9,8 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { - @usableFromInline - internal __consuming func intersection(_ other: Self) -> Self { +extension BitSet { + public __consuming func intersection(_ other: Self) -> Self { self._read { first in other._read { second in Self( @@ -21,30 +20,23 @@ extension _BitSet { } } } -} - -extension BitSet { - public __consuming func intersection(_ other: Self) -> Self { - Self(_core: _core.intersection(other._core)) - } @inlinable public __consuming func intersection( _ other: __owned S ) -> Self - where S.Element == Element + where S.Element == Int { if S.self == Self.self { return intersection(other as! BitSet) } - if S.self == Range.self { - return intersection(other as! Range) + if S.self == Range.self { + return intersection(other as! Range) } return intersection(BitSet(_validMembersOf: other)) } - @inlinable - public __consuming func intersection(_ other: Range) -> Self { + public __consuming func intersection(_ other: Range) -> Self { var result = self result.formIntersection(other) return result diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift index 4b3115a2..da4a02b2 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift @@ -9,9 +9,8 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { - @usableFromInline - internal func isDisjoint(with other: Self) -> Bool { +extension BitSet { + public func isDisjoint(with other: Self) -> Bool { self._read { first in other._read { second in let w1 = first._words @@ -24,34 +23,15 @@ extension _BitSet { } } - @usableFromInline - internal func isDisjoint(with other: Range) -> 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( - with other: BitSet - ) -> Bool { - _core.isDisjoint(with: other._core) - } - @inlinable public func isDisjoint(with other: S) -> Bool - where S.Element == Element + where S.Element == Int { if S.self == BitSet.self { return self.isDisjoint(with: other as! BitSet) } - if S.self == Range.self { - return self.isDisjoint(with: other as! Range) + if S.self == Range.self { + return self.isDisjoint(with: other as! Range) } for value in other { guard !contains(value) else { return false } @@ -61,15 +41,7 @@ extension BitSet { } extension BitSet { - @inlinable - public func isDisjoint(with other: Range) -> Bool { - _core.isDisjoint(with: other._clampedToUInt()) - } - - @inlinable - public func isDisjoint( - with other: Range - ) -> Bool { - _core.isDisjoint(with: other._clampedToUInt()) + public func isDisjoint(with other: Range) -> Bool { + _read { $0.isDisjoint(with: other._clampedToUInt()) } } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift index 5a1999c9..c066ba97 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift @@ -9,9 +9,8 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { - @usableFromInline - internal func isStrictSubset(of other: Self) -> Bool { +extension BitSet { + public func isStrictSubset(of other: Self) -> Bool { self._read { first in other._read { second in let w1 = first._words @@ -30,30 +29,16 @@ extension _BitSet { } } } -} - -extension BitSet { - @inlinable - public func isStrictSubset(of other: Self) -> Bool { - _core.isStrictSubset(of: other._core) - } - - @inlinable - public func isStrictSubset( - of other: BitSet - ) -> Bool { - _core.isStrictSubset(of: other._core) - } @inlinable public func isStrictSubset(of other: S) -> Bool - where S.Element == Element + where S.Element == Int { if S.self == BitSet.self { return isStrictSubset(of: other as! BitSet) } - if S.self == Range.self { - return isStrictSubset(of: other as! Range) + if S.self == Range.self { + return isStrictSubset(of: other as! Range) } if isEmpty { @@ -62,7 +47,7 @@ extension BitSet { } return _UnsafeHandle.withTemporaryBitset( - wordCount: _core._storage.count + wordCount: _storage.count ) { seen in var strict = false var it = other.makeIterator() @@ -83,15 +68,7 @@ extension BitSet { } } - @inlinable - public func isStrictSubset(of other: Range) -> Bool { - isSubset(of: other) && !isSuperset(of: other) - } - - @inlinable - public func isStrictSubset( - of other: Range - ) -> Bool { + public func isStrictSubset(of other: Range) -> Bool { isSubset(of: other) && !isSuperset(of: other) } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift index 43ecbff5..c90cd7b2 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift @@ -9,64 +9,37 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { - @usableFromInline - internal func isStrictSuperset(of other: Range) -> 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( - of other: BitSet - ) -> Bool { - other.isStrictSubset(of: self) - } - @inlinable public func isStrictSuperset(of other: S) -> Bool - where S.Element == Element + where S.Element == Int { guard !isEmpty else { return false } if S.self == BitSet.self { return isStrictSuperset(of: other as! BitSet) } - if S.self == Range.self { - return isStrictSuperset(of: other as! Range) + if S.self == Range.self { + return isStrictSuperset(of: other as! Range) } return _UnsafeHandle.withTemporaryBitset( - wordCount: _core._storage.count + wordCount: _storage.count ) { seen in for i in other { - guard let i = UInt(exactly: i) else { return false } - if !_core.contains(i) { return false } - seen.insert(i) + guard contains(i) else { return false } + seen.insert(UInt(i)) } return seen._count < self.count } } - @inlinable - public func isStrictSuperset(of other: Range) -> Bool { + public func isStrictSuperset(of other: Range) -> 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( - of other: Range - ) -> Bool { - if other.isEmpty { return !isEmpty } - if isEmpty { return false } - guard let r = other._toUInt() else { return false } - return _core.isStrictSuperset(of: r) + return _read { $0.isSuperset(of: r) && !$0.isSubset(of: r) } } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift index ae964cfc..9046b179 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift @@ -9,9 +9,8 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { - @usableFromInline - internal func isSubset(of other: Self) -> Bool { +extension BitSet { + public func isSubset(of other: Self) -> Bool { self._read { first in other._read { second in let w1 = first._words @@ -29,56 +28,28 @@ extension _BitSet { } } - @usableFromInline - internal func isSubset(of other: Range) -> 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( - of other: BitSet - ) -> Bool { - _core.isSubset(of: other._core) - } - @inlinable public func isSubset(of other: S) -> Bool - where S.Element == Element + where S.Element == Int { if S.self == BitSet.self { return self.isSubset(of: other as! BitSet) } - if S.self == Range.self { - return self.isSubset(of: other as! Range) + if S.self == Range.self { + return self.isSubset(of: other as! Range) } guard !isEmpty else { return true } var t = self for i in other { guard let i = UInt(exactly: i) else { continue } - if t._core.remove(i), t.isEmpty { return true } + if t._remove(i), t.isEmpty { return true } } assert(!t.isEmpty) return false } - @inlinable - public func isSubset(of other: Range) -> Bool { + public func isSubset(of other: Range) -> Bool { guard !isEmpty else { return true } - return _core.isSubset(of: other._clampedToUInt()) - } - - @inlinable - public func isSubset( - of other: Range - ) -> Bool { - guard !isEmpty else { return true } - return _core.isSubset(of: other._clampedToUInt()) + return _read { $0.isSubset(of: other._clampedToUInt()) } } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift index 81ff0868..41863326 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift @@ -9,58 +9,32 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { - @usableFromInline - internal func isSuperset(of other: Range) -> Bool { - _read { $0.isSuperset(of: other) } - } -} - extension BitSet { - @inlinable public func isSuperset(of other: Self) -> Bool { other.isSubset(of: self) } - @inlinable - public func isSuperset( - of other: BitSet - ) -> Bool { - other.isSubset(of: self) - } - @inlinable public func isSuperset(of other: S) -> Bool - where S.Element == Element + where S.Element == Int { if S.self == BitSet.self { return self.isSuperset(of: other as! BitSet) } - if S.self == Range.self { - return self.isSuperset(of: other as! Range) + if S.self == Range.self { + return self.isSuperset(of: other as! Range) } for i in other { guard let i = UInt(exactly: i) else { return false } - if !_core.contains(i) { return false } + if !_contains(i) { return false } } return true } - @inlinable - public func isSuperset(of other: Range) -> Bool { + public func isSuperset(of other: Range) -> Bool { if other.isEmpty { return true } if isEmpty { return false } guard let r = other._toUInt() else { return false } - return _core.isSuperset(of: r) - } - - @inlinable - public func isSuperset( - of other: Range - ) -> Bool { - if other.isEmpty { return true } - if isEmpty { return false } - guard let r = other._toUInt() else { return false } - return _core.isSuperset(of: r) + return _read { $0.isSuperset(of: r) } } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift index 2903eee5..f93e62f4 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift @@ -9,9 +9,8 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { - @usableFromInline - internal mutating func subtract(_ other: Self) { +extension BitSet { + public mutating func subtract(_ other: Self) { _updateThenShrink { target, shrink in other._read { source in target.combineSharedPrefix( @@ -23,7 +22,7 @@ extension _BitSet { } @usableFromInline - internal mutating func subtract(_ next: () -> UInt?) { + internal mutating func _subtract(_ next: () -> UInt?) { _updateThenShrink { handle, shrink in while let value = next() { handle.remove(value) @@ -31,42 +30,20 @@ extension _BitSet { } } - @usableFromInline - internal mutating func subtract(_ other: Range) { - 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( - _ other: BitSet - ) { - _core.subtract(other._core) - } - @inlinable public mutating func subtract( _ other: __owned S - ) where S.Element: FixedWidthInteger { + ) where S.Element == Int { if S.self == BitSet.self { self.subtract(other as! BitSet) return } - if S.self == Range.self { - self.subtract(other as! Range) + if S.self == Range.self { + self.subtract(other as! Range) return } var it = other.makeIterator() - _core.subtract { + _subtract { while let value = it.next() { if let value = UInt(exactly: value) { return value @@ -76,15 +53,15 @@ extension BitSet { } } - @inlinable - public mutating func subtract(_ other: Range) { - _core.subtract(other._clampedToUInt()) + @usableFromInline + internal mutating func _subtract(_ other: Range) { + guard !other.isEmpty else { return } + _updateThenShrink { handle, shrink in + handle.subtract(other) + } } - @inlinable - public mutating func subtract( - _ other: Range - ) { - _core.subtract(other._clampedToUInt()) + public mutating func subtract(_ other: Range) { + _subtract(other._clampedToUInt()) } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift index 71e41bab..80530f82 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift @@ -9,9 +9,8 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { - @usableFromInline - internal __consuming func subtracting(_ other: Self) -> Self { +extension BitSet { + public __consuming func subtracting(_ other: Self) -> Self { self._read { first in other._read { second in Self( @@ -21,43 +20,19 @@ extension _BitSet { } } } -} - -extension BitSet { - @inlinable - public __consuming func subtracting(_ other: Self) -> Self { - BitSet(_core: _core.subtracting(other._core)) - } - - @inlinable - public __consuming func subtracting( - _ other: BitSet - ) -> Self { - BitSet(_core: _core.subtracting(other._core)) - } @inlinable public __consuming func subtracting( _ other: __owned S ) -> Self - where S.Element: FixedWidthInteger + where S.Element == Int { var result = self result.subtract(other) return result } - @inlinable - public __consuming func subtracting(_ other: Range) -> Self { - var result = self - result.subtract(other) - return result - } - - @inlinable - public __consuming func subtracting( - _ other: Range - ) -> Self { + public __consuming func subtracting(_ other: Range) -> Self { var result = self result.subtract(other) return result diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift index cac89382..8d524c6e 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift @@ -9,9 +9,8 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { - @usableFromInline - internal __consuming func symmetricDifference(_ other: __owned Self) -> Self { +extension BitSet { + public __consuming func symmetricDifference(_ other: __owned Self) -> Self { self._read { first in other._read { second in Self( @@ -21,27 +20,20 @@ extension _BitSet { } } } -} - -extension BitSet { - public __consuming func symmetricDifference(_ other: __owned Self) -> Self { - BitSet(_core: _core.symmetricDifference(other._core)) - } @inlinable public __consuming func symmetricDifference( _ other: __owned S ) -> Self - where S.Element == Element + where S.Element == Int { - if S.self == Range.self { - return symmetricDifference(other as! Range) + if S.self == Range.self { + return symmetricDifference(other as! Range) } return symmetricDifference(BitSet(other)) } - @inlinable - public __consuming func symmetricDifference(_ other: Range) -> Self { + public __consuming func symmetricDifference(_ other: Range) -> Self { var result = self result.formSymmetricDifference(other) return result diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift index d21b10db..993d5a8e 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift @@ -9,9 +9,8 @@ // //===----------------------------------------------------------------------===// -extension _BitSet { - @usableFromInline - internal __consuming func union(_ other: __owned Self) -> Self { +extension BitSet { + public __consuming func union(_ other: __owned Self) -> Self { self._read { first in other._read { second in Self( @@ -21,19 +20,12 @@ extension _BitSet { } } } -} - -extension BitSet { - @inlinable - public __consuming func union(_ other: __owned Self) -> Self { - BitSet(_core: _core.union(other._core)) - } @inlinable public __consuming func union( _ other: __owned S ) -> Self - where S.Element == Element + where S.Element == Int { if S.self == BitSet.self { return union(other as! BitSet) @@ -43,8 +35,7 @@ extension BitSet { return result } - @inlinable - public __consuming func union(_ other: Range) -> Self { + public __consuming func union(_ other: Range) -> Self { var result = self result.formUnion(other) return result diff --git a/Sources/BitCollections/BitSet/_BitSet.UnsafeHandle.swift b/Sources/BitCollections/BitSet/BitSet._UnsafeHandle.swift similarity index 97% rename from Sources/BitCollections/BitSet/_BitSet.UnsafeHandle.swift rename to Sources/BitCollections/BitSet/BitSet._UnsafeHandle.swift index b80a07f2..f03a106d 100644 --- a/Sources/BitCollections/BitSet/_BitSet.UnsafeHandle.swift +++ b/Sources/BitCollections/BitSet/BitSet._UnsafeHandle.swift @@ -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( @@ -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: _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,7 +414,7 @@ extension _BitSet.UnsafeHandle { } } -extension _BitSet.UnsafeHandle { +extension BitSet._UnsafeHandle { internal func count(in range: Range) -> Int { let l = Index(Swift.min(range.lowerBound, capacity)) let u = Index(Swift.min(range.upperBound, capacity)) diff --git a/Sources/BitCollections/BitSet/BitSet.swift b/Sources/BitCollections/BitSet/BitSet.swift index c0302b85..1c882b6d 100644 --- a/Sources/BitCollections/BitSet/BitSet.swift +++ b/Sources/BitCollections/BitSet/BitSet.swift @@ -14,20 +14,90 @@ /// /// Bit sets implement `SetAlgebra` and provide efficient implementations /// for set operations based on standard binary logic operations. -public struct BitSet { +public struct BitSet { @usableFromInline - internal typealias _UnsafeHandle = _BitSet.UnsafeHandle + internal var _storage: [_Word] @usableFromInline - internal var _core: _BitSet - - @usableFromInline - init(_core: _BitSet) { - self._core = _core - } + internal var _count: Int @usableFromInline init(_storage: [_Word], count: Int) { - self.init(_core: _BitSet(storage: _storage, count: count)) + self._storage = _storage + self._count = count + } +} + +extension BitSet { + @inline(__always) + internal func _read( + _ 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( + _ 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( + _ 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) + } } } diff --git a/Sources/BitCollections/BitSet/_BitSet.swift b/Sources/BitCollections/BitSet/_BitSet.swift deleted file mode 100644 index 6d0a5ebc..00000000 --- a/Sources/BitCollections/BitSet/_BitSet.swift +++ /dev/null @@ -1,105 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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( - _ 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( - _ 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( - _ 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) - } - } -} diff --git a/Tests/BitCollectionsTests/BitSetTests.swift b/Tests/BitCollectionsTests/BitSetTests.swift index c0bf6f8f..8c778c9d 100644 --- a/Tests/BitCollectionsTests/BitSetTests.swift +++ b/Tests/BitCollectionsTests/BitSetTests.swift @@ -15,14 +15,14 @@ import BitCollections final class BitSetTest: CollectionTestCase { func test_empty_initializer() { - let set = BitSet() + let set = BitSet() expectEqual(set.count, 0) expectTrue(set.isEmpty) expectEqualElements(set, []) } func test_array_literal_initializer() { - let set0: BitSet = [] + let set0: BitSet = [] 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(bitPattern: 0 as UInt) expectEqualElements(s1, []) - let s2 = BitSet(bitPattern: 1 as UInt) + let s2 = BitSet(bitPattern: 1 as UInt) expectEqualElements(s2, [0]) - let s3 = BitSet(bitPattern: 2 as UInt) + let s3 = BitSet(bitPattern: 2 as UInt) expectEqualElements(s3, [1]) - let s4 = BitSet(bitPattern: 23) + let s4 = BitSet(bitPattern: 23) expectEqualElements(s4, [0, 1, 2, 4]) - let s5 = BitSet(bitPattern: -1) + let s5 = BitSet(bitPattern: -1) expectEqualElements(s5, 0 ..< UInt.bitWidth) } @@ -158,7 +158,7 @@ final class BitSetTest: CollectionTestCase { checkHashable(equivalenceClasses: classes) } - func test_contains_Int() { + func test_contains() { withInterestingSets("input", maximum: 1000) { input in let bitset = BitSet(input) withEvery("value", in: 0 ..< 1000) { value in @@ -169,17 +169,6 @@ final class BitSetTest: CollectionTestCase { } } - func test_contains_Int32() { - withInterestingSets("input", maximum: 1000) { input in - let bitset = BitSet(input.map { Int32($0) }) - withEvery("value", in: 0 ..< 1000) { value in - expectEqual(bitset.contains(Int32(value)), input.contains(value)) - } - expectFalse(bitset.contains(-1 as Int32)) - expectFalse(bitset.contains(5000 as Int32)) - } - } - func test_contains_Sequence() { // This exercises the `_customContainsEquatableElement` // implementation in `BitSet`'s `Sequence` conformance. @@ -198,12 +187,13 @@ final class BitSetTest: CollectionTestCase { } } - func checkInsert(for type: F.Type, count: Int) { + func test_insert() { + let count = 100 withEvery("seed", in: 0 ..< 10) { seed in var rng = RepeatableRandomNumberGenerator(seed: seed) - var actual: BitSet = [] - var expected: Set = [] - let input = (0 ..< count).shuffled(using: &rng).map { F($0) } + var actual: BitSet = [] + var expected: Set = [] + let input = (0 ..< count).shuffled(using: &rng) withEvery("i", in: input.indices) { i in let (i1, m1) = actual.insert(input[i]) expected.insert(input[i]) @@ -220,22 +210,13 @@ final class BitSetTest: CollectionTestCase { } } - func test_insert_Int() { - checkInsert(for: Int.self, count: 100) - } - func test_insert_Int32() { - checkInsert(for: Int32.self, count: 100) - } - func test_insert_UInt16() { - checkInsert(for: UInt16.self, count: 100) - } - - func checkUpdate(for type: F.Type, count: Int) { + func test_update() { + let count = 100 withEvery("seed", in: 0 ..< 10) { seed in var rng = RepeatableRandomNumberGenerator(seed: seed) - var actual: BitSet = [] - var expected: Set = [] - let input = (0 ..< count).shuffled(using: &rng).map { F($0) } + var actual: BitSet = [] + var expected: Set = [] + let input = (0 ..< count).shuffled(using: &rng) withEvery("i", in: input.indices) { i in let old = actual.update(with: input[i]) expected.update(with: input[i]) @@ -249,47 +230,28 @@ final class BitSetTest: CollectionTestCase { } } - func test_update_Int() { - checkUpdate(for: Int.self, count: 100) - } - func test_update_Int32() { - checkUpdate(for: Int32.self, count: 100) - } - func test_update_UInt16() { - checkUpdate(for: UInt16.self, count: 100) - } - - func test_remove_Int() { - func checkRemove(for type: F.Type, count: Int) { - context.withTrace("\(type)") { - withEvery("seed", in: 0 ..< 10) { seed in - var rng = RepeatableRandomNumberGenerator(seed: seed) - var actual = BitSet(0 ..< F(count)) - var expected = Set(0 ..< F(count)) - let input = (0 ..< F(count)).shuffled(using: &rng) - - if F.isSigned { - expectNil(actual.remove(F(-1))) - } - - withEvery("i", in: input.indices) { i in - let v = input[i] - let old = actual.remove(F(v)) - expected.remove(v) - expectEqual(old, F(v)) - if i % 25 == 0 { - expectEqual(Array(actual), expected.sorted()) - } - expectNil(actual.remove(F(v))) - } + func test_remove() { + let count = 100 + withEvery("seed", in: 0 ..< 10) { seed in + var rng = RepeatableRandomNumberGenerator(seed: seed) + var actual = BitSet(0 ..< count) + var expected = Set(0 ..< count) + let input = (0 ..< count).shuffled(using: &rng) + + expectNil(actual.remove(-1)) + + withEvery("i", in: input.indices) { i in + let v = input[i] + let old = actual.remove(v) + expected.remove(v) + expectEqual(old, v) + if i % 25 == 0 { expectEqual(Array(actual), expected.sorted()) } + expectNil(actual.remove(v)) } + expectEqual(Array(actual), expected.sorted()) } - - checkRemove(for: Int.self, count: 100) - checkRemove(for: Int32.self, count: 100) - checkRemove(for: UInt16.self, count: 100) } func test_union_Self() { @@ -312,11 +274,8 @@ final class BitSetTest: CollectionTestCase { let actual = c.union(b) expectEqualElements(actual, expected) - func union( - _ first: BitSet, - _ second: S - ) -> BitSet - where S.Element == F { + func union(_ first: BitSet,_ second: S) -> BitSet + where S.Element == Int { first.union(second) } expectEqualElements(union(c, BitSet(b)), expected) @@ -326,7 +285,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() let b = a.union(0 ..< 5*step) expectEqualElements(b, 0 ..< 5*step) @@ -368,9 +327,8 @@ final class BitSetTest: CollectionTestCase { c.formUnion(b) expectEqualElements(c, expected) - func union( - _ first: inout BitSet, _ second: S) - where S.Element == F { + func union(_ first: inout BitSet, _ second: S) + where S.Element == Int { first.formUnion(second) } var d = BitSet(a) @@ -384,7 +342,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() a.formUnion(0 ..< 5*step) expectEqualElements(a, 0 ..< 5*step) @@ -429,11 +387,8 @@ final class BitSetTest: CollectionTestCase { expectEqualElements(c.intersection([-100]), []) expectEqualElements(c.intersection(-100 ..< -10), []) - func intersection( - _ first: BitSet, - _ second: S - ) -> BitSet - where S.Element == F { + func intersection(_ first: BitSet, _ second: S) -> BitSet + where S.Element == Int { first.intersection(second) } let d = intersection(c, BitSet(b)) @@ -480,11 +435,8 @@ final class BitSetTest: CollectionTestCase { } func test_formIntersection_Sequence() { - func intersection( - _ first: inout BitSet, - _ second: S - ) - where S.Element == F { + func intersection(_ first: inout BitSet, _ second: S) + where S.Element == Int { first.formIntersection(second) } @@ -568,11 +520,10 @@ final class BitSetTest: CollectionTestCase { let actual = c.symmetricDifference(b) expectEqualElements(actual, expected) - func symmetricDifference( - _ first: BitSet, - _ second: S - ) -> BitSet - where S.Element == F { + func symmetricDifference( + _ first: BitSet, _ second: S + ) -> BitSet + where S.Element == Int { first.symmetricDifference(second) } let d = symmetricDifference(c, BitSet(b)) @@ -589,7 +540,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() let b = a.symmetricDifference(0 ..< 10*step) expectEqualElements(b, 0 ..< 10*step) @@ -628,10 +579,9 @@ final class BitSetTest: CollectionTestCase { c.formSymmetricDifference(b) expectEqualElements(c, expected) - func symmetricDifference( - _ first: inout BitSet, - _ second: S - ) where S.Element == F { + func symmetricDifference( + _ first: inout BitSet, _ second: S + ) where S.Element == Int { first.formSymmetricDifference(second) } var d = BitSet(a) @@ -649,7 +599,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() a.formSymmetricDifference(0 ..< 10*step) expectEqualElements(a, 0 ..< 10*step) @@ -682,11 +632,8 @@ final class BitSetTest: CollectionTestCase { } func test_subtracting_Sequence() { - func subtracting( - _ first: BitSet, - _ second: S - ) -> BitSet - where S.Element: FixedWidthInteger { + func subtracting(_ first: BitSet, _ second: S) -> BitSet + where S.Element == Int { first.subtracting(second) } @@ -744,11 +691,8 @@ final class BitSetTest: CollectionTestCase { } func test_subtract_Sequence() { - func subtract( - _ first: inout BitSet, - _ second: S - ) - where S.Element == F { + func subtract(_ first: inout BitSet, _ second: S) + where S.Element == Int { first.subtract(second) } diff --git a/Utils/swift-collections.xcworkspace/xcshareddata/xcschemes/BitCollections.xcscheme b/Utils/swift-collections.xcworkspace/xcshareddata/xcschemes/BitCollections.xcscheme index 3a513fa1..b200392b 100644 --- a/Utils/swift-collections.xcworkspace/xcshareddata/xcschemes/BitCollections.xcscheme +++ b/Utils/swift-collections.xcworkspace/xcshareddata/xcschemes/BitCollections.xcscheme @@ -17,21 +17,21 @@ BlueprintIdentifier = "BitCollections" BuildableName = "BitCollections" BlueprintName = "BitCollections" - ReferencedContainer = "container:"> + ReferencedContainer = "container:.."> + buildForAnalyzing = "NO"> + ReferencedContainer = "container:.."> @@ -51,7 +51,7 @@ BlueprintIdentifier = "BitCollectionsTests" BuildableName = "BitCollectionsTests" BlueprintName = "BitCollectionsTests" - ReferencedContainer = "container:"> + ReferencedContainer = "container:.."> @@ -79,7 +79,7 @@ BlueprintIdentifier = "BitCollections" BuildableName = "BitCollections" BlueprintName = "BitCollections" - ReferencedContainer = "container:"> + ReferencedContainer = "container:..">