diff --git a/Sources/BitCollections/BitArray/BitArray+Initializers.swift b/Sources/BitCollections/BitArray/BitArray+Initializers.swift index d9e2a0a1..f0ffc5fa 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._storage, count: l + 1) + self.init(_storage: set._core._storage, count: l + 1) } public init(bitPattern value: I) { diff --git a/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift b/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift index 98522b6f..366d3e7f 100644 --- a/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift +++ b/Sources/BitCollections/BitSet/BitSet+BidirectionalCollection.swift @@ -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 diff --git a/Sources/BitCollections/BitSet/BitSet+Equatable.swift b/Sources/BitCollections/BitSet/BitSet+Equatable.swift index 7f1d95cc..02631182 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._storage == right._storage + left._core._storage == right._core._storage } } diff --git a/Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift b/Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift index b1d66517..e79ad9ba 100644 --- a/Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift +++ b/Sources/BitCollections/BitSet/BitSet+ExpressibleByArrayLiteral.swift @@ -10,7 +10,7 @@ //===----------------------------------------------------------------------===// extension BitSet: ExpressibleByArrayLiteral { - public init(arrayLiteral elements: Int...) { + public init(arrayLiteral elements: Element...) { self.init(elements) } } diff --git a/Sources/BitCollections/BitSet/BitSet+Hashable.swift b/Sources/BitCollections/BitSet/BitSet+Hashable.swift index 16d7bee3..b5a92cf9 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(_storage) + hasher.combine(_core._storage) } } diff --git a/Sources/BitCollections/BitSet/BitSet+Initializers.swift b/Sources/BitCollections/BitSet/BitSet+Initializers.swift index 01008421..be233fbb 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: FixedWidthInteger { + ) where S.Element == Element { if S.self == BitSet.self { self = (elements as! BitSet) return @@ -31,27 +31,27 @@ extension BitSet { @inlinable internal init( _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.self { - let r = (elements as! Range) - self.init(r._clampedToUInt()) + if S.self == Range.self { + let r = (elements as! Range) + 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(_ range: Range) { + public init(_ range: Range) { 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) { + internal init(_ 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)) } @@ -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 ) { diff --git a/Sources/BitCollections/BitSet/BitSet+Invariants.swift b/Sources/BitCollections/BitSet/BitSet+Invariants.swift index fa77f054..6950d901 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 af423fce..2ff3f499 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra basics.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra basics.swift @@ -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(_ 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( - _ 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( - 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( - _ 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 + } +} diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift index 535960b8..dfd930c2 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift @@ -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( - _ other: __owned S - ) where S.Element: FixedWidthInteger { - if S.self == Range.self { - formIntersection(other as! Range) - return - } - formIntersection(BitSet(_validMembersOf: other)) - } - - @inlinable - public mutating func formIntersection( - _ other: Range - ) { - _formIntersection(other._clampedToUInt()) - } - @usableFromInline - internal mutating func _formIntersection(_ other: Range) { + internal mutating func formIntersection(_ other: Range) { 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( + _ 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 9efdbab6..b1c95681 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift @@ -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( - _ other: __owned S - ) where S.Element: FixedWidthInteger { - if S.self == Range.self { - formSymmetricDifference(other as! Range) - return - } - formSymmetricDifference(BitSet(other)) - } - - @inlinable - public mutating func formSymmetricDifference( - _ other: Range - ) { - guard let other = other._toUInt() else { - preconditionFailure("BitSet can only hold nonnegative integers") - } - _formSymmetricDifference(other) - } - @usableFromInline - internal mutating func _formSymmetricDifference(_ other: Range) { + internal mutating func formSymmetricDifference(_ other: Range) { 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( + _ 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 b6c4904a..9c7ed3c2 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift @@ -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) { + 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( _ 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( - _ other: Range - ) { + public mutating func formUnion(_ other: Range) { guard let other = other._toUInt() else { - preconditionFailure("BitSet can only hold nonnegative integers") - } - _formUnion(other) - } - - - @usableFromInline - internal mutating func _formUnion(_ other: Range) { - guard !other.isEmpty else { return } - _ensureCapacity(limit: other.upperBound) - _update { handle in - handle.formUnion(other) + 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 bf5e4e1a..fd1f75f7 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift @@ -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( _ 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.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 9a44e073..4b3115a2 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift @@ -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) -> 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: FixedWidthInteger + where S.Element == Element { 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 } } return true } +} + +extension BitSet { + @inlinable + public func isDisjoint(with other: Range) -> Bool { + _core.isDisjoint(with: other._clampedToUInt()) + } @inlinable - public func isDisjoint(with other: Range) -> Bool { - _isDisjoint(with: other._clampedToUInt()) - } - - @usableFromInline - internal func _isDisjoint(with other: Range) -> Bool { - _read { $0.isDisjoint(with: other) } + public func isDisjoint( + with other: Range + ) -> Bool { + _core.isDisjoint(with: other._clampedToUInt()) } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift index a88982ac..5a1999c9 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift @@ -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( + of other: BitSet + ) -> Bool { + _core.isStrictSubset(of: other._core) + } @inlinable public func isStrictSubset(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.self { - return isStrictSubset(of: other as! Range) + if S.self == Range.self { + return isStrictSubset(of: other as! Range) } 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(of other: Range) -> Bool { + public func isStrictSubset(of other: Range) -> Bool { + isSubset(of: other) && !isSuperset(of: other) + } + + @inlinable + 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 cab04d56..43ecbff5 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift @@ -9,34 +9,57 @@ // //===----------------------------------------------------------------------===// +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: 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.self { - return isStrictSuperset(of: other as! Range) + if S.self == Range.self { + return isStrictSuperset(of: other as! Range) } 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) -> 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 @@ -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) -> Bool { - _read { $0.isSuperset(of: other) && !$0.isSubset(of: other) } + return _core.isStrictSuperset(of: r) } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift index f7c552aa..ae964cfc 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift @@ -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) -> 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: FixedWidthInteger + where S.Element == Element { 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.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(of other: Range) -> Bool { + public func isSubset(of other: Range) -> Bool { guard !isEmpty else { return true } - return _isSubset(of: other._clampedToUInt()) + return _core.isSubset(of: other._clampedToUInt()) } - @usableFromInline - internal func _isSubset(of other: Range) -> Bool { - _read { $0.isSubset(of: other) } + @inlinable + public func isSubset( + of other: Range + ) -> Bool { + guard !isEmpty else { return true } + return _core.isSubset(of: other._clampedToUInt()) } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift index 55512b41..81ff0868 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift @@ -9,38 +9,58 @@ // //===----------------------------------------------------------------------===// +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: FixedWidthInteger + where S.Element == Element { 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 !contains(i) { return false } + if !_core.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 _isSuperset(of: r) + return _core.isSuperset(of: r) } - @usableFromInline - internal func _isSuperset(of other: Range) -> Bool { - _read { $0.isSuperset(of: other) } + @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) } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift index f5de56e3..2903eee5 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift @@ -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) { + 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 @@ -29,12 +61,12 @@ extension BitSet { 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() - _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) { + _core.subtract(other._clampedToUInt()) } @inlinable public mutating func subtract( _ other: Range ) { - _subtract(other._clampedToUInt()) - } - - @usableFromInline - internal mutating func _subtract(_ other: Range) { - guard !other.isEmpty else { return } - _updateThenShrink { handle, shrink in - handle.subtract(other) - } + _core.subtract(other._clampedToUInt()) } } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift index 54292235..71e41bab 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift @@ -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( + _ other: BitSet + ) -> Self { + BitSet(_core: _core.subtracting(other._core)) + } @inlinable public __consuming func subtracting( @@ -32,6 +47,13 @@ extension BitSet { 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 diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift index 05bb8cb8..cac89382 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift @@ -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( _ other: __owned S ) -> Self - where S.Element: FixedWidthInteger + where S.Element == Element { - 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 0a0e29be..d21b10db 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift @@ -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( _ 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( - _ 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.swift b/Sources/BitCollections/BitSet/BitSet.swift index 11f625e5..c0302b85 100644 --- a/Sources/BitCollections/BitSet/BitSet.swift +++ b/Sources/BitCollections/BitSet/BitSet.swift @@ -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 { @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( - _ 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._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 4c26a36d..b80a07f2 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: _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) -> 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 new file mode 100644 index 00000000..6d0a5ebc --- /dev/null +++ b/Sources/BitCollections/BitSet/_BitSet.swift @@ -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( + _ 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 2d1bb3f7..c0bf6f8f 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) } @@ -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(for type: F.Type, count: Int) { 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) + var actual: BitSet = [] + var expected: Set = [] + 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(for type: F.Type, count: Int) { 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) + var actual: BitSet = [] + var expected: Set = [] + 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(0 ..< count) - let input = (0 ..< count).shuffled(using: &rng) + 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))) @@ -312,8 +312,11 @@ final class BitSetTest: CollectionTestCase { let actual = c.union(b) expectEqualElements(actual, expected) - func union(_ first: BitSet, _ second: S) -> BitSet - where S.Element: FixedWidthInteger { + func union( + _ first: BitSet, + _ second: S + ) -> BitSet + 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() 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(_ first: inout BitSet, _ second: S) - where S.Element: FixedWidthInteger { + func union( + _ first: inout BitSet, _ 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() 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(_ first: BitSet, _ second: S) -> BitSet - where S.Element: FixedWidthInteger { + func intersection( + _ first: BitSet, + _ second: S + ) -> BitSet + 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(_ first: inout BitSet, _ second: S) - where S.Element: FixedWidthInteger { + func intersection( + _ first: inout BitSet, + _ 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( - _ first: BitSet, + func symmetricDifference( + _ first: BitSet, _ second: S - ) -> BitSet - where S.Element: FixedWidthInteger { + ) -> BitSet + 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() 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( - _ first: inout BitSet, + func symmetricDifference( + _ first: inout BitSet, _ 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() a.formSymmetricDifference(0 ..< 10*step) expectEqualElements(a, 0 ..< 10*step) @@ -672,10 +682,10 @@ final class BitSetTest: CollectionTestCase { } func test_subtracting_Sequence() { - func subtracting( - _ first: BitSet, + func subtracting( + _ first: BitSet, _ second: S - ) -> BitSet + ) -> BitSet where S.Element: FixedWidthInteger { first.subtracting(second) } @@ -734,11 +744,11 @@ final class BitSetTest: CollectionTestCase { } func test_subtract_Sequence() { - func subtract( - _ first: inout BitSet, + func subtract( + _ first: inout BitSet, _ 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(_ 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(_ 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(_ 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(_ 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(_ other: S) -> Bool - where S.Element: FixedWidthInteger { + where S.Element == Int { set.isDisjoint(with: other) } diff --git a/Utils/swift-collections.xcworkspace/xcshareddata/swiftpm/Package.resolved b/Utils/swift-collections.xcworkspace/xcshareddata/swiftpm/Package.resolved index 694bc3be..dce13b01 100644 --- a/Utils/swift-collections.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/Utils/swift-collections.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -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" } }, {