diff --git a/Package.swift b/Package.swift index 2782e6ef..b91a17e2 100644 --- a/Package.swift +++ b/Package.swift @@ -199,6 +199,7 @@ let targets: [CustomTarget] = [ "IntegerTricks/UInt+first and last set bit.swift.gyb", "IntegerTricks/UInt+reversed.swift.gyb", "RandomAccessCollection+Offsets.swift.gyb", + "Specialize.swift.gyb", "UnsafeBitSet/_UnsafeBitSet+Index.swift.gyb", "UnsafeBitSet/_UnsafeBitSet+_Word.swift.gyb", "UnsafeBitSet/_UnsafeBitSet.swift.gyb", diff --git a/Sources/BitCollections/BitArray/BitArray+BitwiseOperations.swift b/Sources/BitCollections/BitArray/BitArray+BitwiseOperations.swift index 1a1b92f5..0b2c1db4 100644 --- a/Sources/BitCollections/BitArray/BitArray+BitwiseOperations.swift +++ b/Sources/BitCollections/BitArray/BitArray+BitwiseOperations.swift @@ -187,9 +187,7 @@ extension BitArray { } @inlinable - public mutating func toggleAll( - in range: R - ) where R.Bound == Int { + public mutating func toggleAll(in range: some RangeExpression) { toggleAll(in: range.relative(to: self)) } } diff --git a/Sources/BitCollections/BitArray/BitArray+Copy.swift b/Sources/BitCollections/BitArray/BitArray+Copy.swift index b275434c..05c6012b 100644 --- a/Sources/BitCollections/BitArray/BitArray+Copy.swift +++ b/Sources/BitCollections/BitArray/BitArray+Copy.swift @@ -151,10 +151,10 @@ extension BitArray._UnsafeHandle { } extension BitArray._UnsafeHandle { - internal mutating func copy( - from source: S, + internal mutating func copy( + from source: some Sequence, to range: Range - ) where S.Element == Bool { + ) { assert(range.lowerBound >= 0 && range.upperBound <= self.count) var pos = _BitPosition(range.lowerBound) var it = source.makeIterator() diff --git a/Sources/BitCollections/BitArray/BitArray+Fill.swift b/Sources/BitCollections/BitArray/BitArray+Fill.swift index 6c8223f6..a33ce853 100644 --- a/Sources/BitCollections/BitArray/BitArray+Fill.swift +++ b/Sources/BitCollections/BitArray/BitArray+Fill.swift @@ -32,10 +32,15 @@ extension BitArray { } } - public mutating func fill( - in range: R, + /// Set every bit of this array within the specified range to `value` + /// (`true` by default). + /// + /// - Parameter range: The range whose elements to overwrite. + /// - Parameter value: The Boolean value to which to set the array's elements. + public mutating func fill( + in range: some RangeExpression, with value: Bool = true - ) where R.Bound == Int { + ) { fill(in: range.relative(to: self), with: value) } } diff --git a/Sources/BitCollections/BitArray/BitArray+Initializers.swift b/Sources/BitCollections/BitArray/BitArray+Initializers.swift index 5269a736..db8491b3 100644 --- a/Sources/BitCollections/BitArray/BitArray+Initializers.swift +++ b/Sources/BitCollections/BitArray/BitArray+Initializers.swift @@ -42,7 +42,7 @@ extension BitArray { /// BitArray(bitPattern: -1 as Int) // 1111111111...1111111111 /// /// - Complexity: O(value.bitWidth) - public init(bitPattern value: I) { + public init(bitPattern value: some BinaryInteger) { var words = value.words.map { _Word($0) } let count = value.bitWidth if words.isEmpty { diff --git a/Sources/BitCollections/BitArray/BitArray+LosslessStringConvertible.swift b/Sources/BitCollections/BitArray/BitArray+LosslessStringConvertible.swift index 2166b166..6a991130 100644 --- a/Sources/BitCollections/BitArray/BitArray+LosslessStringConvertible.swift +++ b/Sources/BitCollections/BitArray/BitArray+LosslessStringConvertible.swift @@ -32,7 +32,7 @@ extension BitArray: LosslessStringConvertible { self = bits } - internal init?(_utf8 utf8: C) where C.Element == UInt8 { + internal init?(_utf8 utf8: some Collection) { let c = utf8.count self.init(repeating: false, count: c) var i = c &- 1 diff --git a/Sources/BitCollections/BitArray/BitArray+RandomBits.swift b/Sources/BitCollections/BitArray/BitArray+RandomBits.swift index 9a13e78c..5a187d29 100644 --- a/Sources/BitCollections/BitArray/BitArray+RandomBits.swift +++ b/Sources/BitCollections/BitArray/BitArray+RandomBits.swift @@ -24,9 +24,9 @@ extension BitArray { /// /// - Parameter count: The number of random bits to generate. /// - Parameter rng: The random number generator to use. - public static func randomBits( + public static func randomBits( count: Int, - using rng: inout R + using rng: inout some RandomNumberGenerator ) -> BitArray { precondition(count >= 0, "Invalid count") guard count > 0 else { return BitArray() } diff --git a/Sources/BitCollections/BitArray/BitArray+RangeReplaceableCollection.swift b/Sources/BitCollections/BitArray/BitArray+RangeReplaceableCollection.swift index 755210cf..0bca6d17 100644 --- a/Sources/BitCollections/BitArray/BitArray+RangeReplaceableCollection.swift +++ b/Sources/BitCollections/BitArray/BitArray+RangeReplaceableCollection.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension BitArray: RangeReplaceableCollection {} extension BitArray { @@ -57,14 +61,14 @@ extension BitArray { /// - Complexity: O(*count*) where *count* is the number of values in /// `elements`. @inlinable - public init(_ elements: S) where S.Element == Bool { + public init(_ elements: some Sequence) { defer { _checkInvariants() } - if S.self == BitArray.self { - self = elements as! BitArray + if let elements = _specialize(elements, for: BitArray.self) { + self.init(elements) return } - if S.self == BitArray.SubSequence.self { - self.init(elements as! BitArray.SubSequence) + if let elements = _specialize(elements, for: BitArray.SubSequence.self) { + self.init(elements) return } self.init() @@ -80,6 +84,7 @@ extension BitArray { /// `elements` must be finite. /// - Complexity: O(*count*) where *count* is the number of values in /// `elements`. + @inlinable public init(_ elements: BitArray) { self = elements } @@ -143,16 +148,18 @@ extension BitArray { /// *m* is the length of `newElements`. If the call to this method simply /// appends the contents of `newElements` to the collection, this method is /// equivalent to `append(contentsOf:)`. - public mutating func replaceSubrange( + public mutating func replaceSubrange( _ range: Range, - with newElements: __owned C - ) where C.Element == Bool { + with newElements: __owned some Collection + ) { let c = newElements.count _prepareForReplaceSubrange(range, replacementCount: c) - if C.self == BitArray.self { - _copy(from: newElements as! BitArray, to: range.lowerBound) - } else if C.self == BitArray.SubSequence.self { - _copy(from: newElements as! BitArray.SubSequence, to: range.lowerBound) + if let newElements = _specialize(newElements, for: BitArray.self) { + _copy(from: newElements, to: range.lowerBound) + } else if let newElements = _specialize( + newElements, for: BitArray.SubSequence.self + ) { + _copy(from: newElements, to: range.lowerBound) } else { _copy(from: newElements, to: range.lowerBound ..< range.lowerBound + c) } @@ -259,15 +266,17 @@ extension BitArray { /// /// - Complexity: O(*m*), where *m* is the length of `newElements`, if /// `self` is the only copy of this bit array. Otherwise O(`count` + *m*). - public mutating func append( - contentsOf newElements: __owned S - ) where S.Element == Bool { - if S.self == BitArray.self { - self.append(contentsOf: newElements as! BitArray) + public mutating func append( + contentsOf newElements: __owned some Sequence + ) { + if let newElements = _specialize(newElements, for: BitArray.self) { + self.append(contentsOf: newElements) return } - if S.self == BitArray.SubSequence.self { - self.append(contentsOf: newElements as! BitArray.SubSequence) + if let newElements = _specialize( + newElements, for: BitArray.SubSequence.self + ) { + self.append(contentsOf: newElements) return } var it = newElements.makeIterator() @@ -372,20 +381,22 @@ extension BitArray { /// /// - Complexity: O(`self.count` + `newElements.count`). /// If `i == endIndex`, this method is equivalent to `append(contentsOf:)`. - public mutating func insert( - contentsOf newElements: __owned C, + public mutating func insert( + contentsOf newElements: __owned some Collection, at i: Int - ) where C.Element == Bool { + ) { precondition(i >= 0 && i <= count) let c = newElements.count guard c > 0 else { return } _extend(by: c) _copy(from: i ..< count - c, to: i + c) - if C.self == BitArray.self { - _copy(from: newElements as! BitArray, to: i) - } else if C.self == BitArray.SubSequence.self { - _copy(from: newElements as! BitArray.SubSequence, to: i) + if let newElements = _specialize(newElements, for: BitArray.self) { + _copy(from: newElements, to: i) + } else if let newElements = _specialize( + newElements, for: BitArray.SubSequence.self + ) { + _copy(from: newElements, to: i) } else { _copy(from: newElements, to: i ..< i + c) } diff --git a/Sources/BitCollections/BitSet/BitSet+Extras.swift b/Sources/BitCollections/BitSet/BitSet+Extras.swift index 181aaad3..2fdae959 100644 --- a/Sources/BitCollections/BitSet/BitSet+Extras.swift +++ b/Sources/BitCollections/BitSet/BitSet+Extras.swift @@ -140,9 +140,7 @@ extension BitSet { /// // Optional(2) /// /// - Complexity: Equivalent to two invocations of `index(after:)`. - public subscript(members bounds: R) -> Slice - where R.Bound == Int - { + public subscript(members bounds: some RangeExpression) -> Slice { let bounds = bounds.relative(to: Int.min ..< Int.max) return self[members: bounds] } diff --git a/Sources/BitCollections/BitSet/BitSet+Initializers.swift b/Sources/BitCollections/BitSet/BitSet+Initializers.swift index cf8b9987..2437c699 100644 --- a/Sources/BitCollections/BitSet/BitSet+Initializers.swift +++ b/Sources/BitCollections/BitSet/BitSet+Initializers.swift @@ -50,7 +50,7 @@ extension BitSet { /// /// - Complexity: O(`words.count`) @inlinable - public init(words: S) where S.Element == UInt { + public init(words: some Sequence) { self.init(_words: words.map { _Word($0) }) } @@ -65,7 +65,7 @@ extension BitSet { /// /// - Complexity: O(`x.bitWidth`) @inlinable - public init(bitPattern x: I) { + public init(bitPattern x: some BinaryInteger) { self.init(words: x.words) } @@ -88,15 +88,15 @@ extension BitSet { /// /// - Complexity: O(*n*), where *n* is the number of elements in the sequence. @inlinable - public init( - _ elements: __owned S - ) where S.Element == Int { - if S.self == BitSet.self { - self = (elements as! BitSet) + public init( + _ elements: __owned some Sequence + ) { + if let elements = _specialize(elements, for: BitSet.self) { + self = elements return } - if S.self == BitSet.Counted.self { - self = (elements as! BitSet.Counted)._bits + if let elements = _specialize(elements, for: BitSet.Counted.self) { + self = elements._bits return } self.init() @@ -115,19 +115,19 @@ extension BitSet { /// /// - Complexity: O(*n*), where *n* is the number of elements in the sequence. @inlinable - internal init( - _validMembersOf elements: __owned S - ) where S.Element == Int { - if S.self == BitSet.self { - self = (elements as! BitSet) + internal init( + _validMembersOf elements: __owned some Sequence + ) { + if let elements = _specialize(elements, for: BitSet.self) { + self = elements return } - if S.self == BitSet.Counted.self { - self = (elements as! BitSet.Counted)._bits + if let elements = _specialize(elements, for: BitSet.Counted.self) { + self = elements._bits return } - if S.self == Range.self { - let r = (elements as! Range) + if let elements = _specialize(elements, for: Range.self) { + let r = elements self.init(_range: r._clampedToUInt()) return } diff --git a/Sources/BitCollections/BitSet/BitSet+Random.swift b/Sources/BitCollections/BitSet/BitSet+Random.swift index f46e666b..4f03cdde 100644 --- a/Sources/BitCollections/BitSet/BitSet+Random.swift +++ b/Sources/BitCollections/BitSet/BitSet+Random.swift @@ -15,9 +15,9 @@ extension BitSet { return random(upTo: limit, using: &rng) } - public static func random( + public static func random( upTo limit: Int, - using rng: inout R + using rng: inout some RandomNumberGenerator ) -> BitSet { precondition(limit >= 0, "Invalid limit value") guard limit > 0 else { return BitSet() } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift index 7ea633e3..7eeba0af 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formIntersection.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension BitSet { /// Removes the elements of this set that aren't also in the given one. /// @@ -82,11 +86,11 @@ extension BitSet { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public mutating func formIntersection( - _ other: __owned S - ) where S.Element == Int { - if S.self == Range.self { - formIntersection(other as! Range) + public mutating func formIntersection( + _ other: __owned some Sequence + ) { + if let other = _specialize(other, for: Range.self) { + formIntersection(other) return } // Note: BitSet & BitSet.Counted are handled in the BitSet initializer below diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift index fdc0e116..785a76ad 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formSymmetricDifference.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension BitSet { /// Replace this set with the elements contained in this set or the given /// set, but not both. @@ -81,11 +85,11 @@ extension BitSet { /// input, and *k* is the complexity of iterating over all elements in /// `other`. @inlinable - public mutating func formSymmetricDifference( - _ other: __owned S - ) where S.Element == Int { - if S.self == Range.self { - formSymmetricDifference(other as! Range) + public mutating func formSymmetricDifference( + _ other: __owned some Sequence + ) { + if let other = _specialize(other, for: Range.self) { + formSymmetricDifference(other) return } // Note: BitSet & BitSet.Counted are handled in the BitSet initializer below diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift index b8706a53..89cf0573 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra formUnion.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension BitSet { /// Adds the elements of the given set to this set. /// @@ -76,19 +80,17 @@ extension BitSet { /// input, and *k* is the complexity of iterating over all elements in /// `other`. @inlinable - public mutating func formUnion( - _ other: __owned S - ) where S.Element == Int { - if S.self == BitSet.self { - formUnion(other as! BitSet) + public mutating func formUnion(_ other: __owned some Sequence) { + if let other = _specialize(other, for: BitSet.self) { + formUnion(other) return } - if S.self == BitSet.Counted.self { - formUnion(other as! BitSet.Counted) + if let other = _specialize(other, for: BitSet.Counted.self) { + formUnion(other) return } - if S.self == Range.self { - formUnion(other as! Range) + if let other = _specialize(other, for: Range.self) { + formUnion(other) return } for value in other { diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift index a63659b4..b9f12881 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra intersection.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension BitSet { /// Returns a new bit set with the elements that are common to both this set /// and the given set. @@ -77,13 +81,9 @@ extension BitSet { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public func intersection( - _ other: __owned S - ) -> Self - where S.Element == Int - { - if S.self == Range.self { - return intersection(other as! Range) + public func intersection(_ other: __owned some Sequence) -> Self { + if let other = _specialize(other, for: Range.self) { + return intersection(other) } // Note: BitSet & BitSet.Counted are handled in the BitSet initializer below return intersection(BitSet(_validMembersOf: other)) diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift index b92486dc..b65759b3 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isDisjoint.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension BitSet { /// Returns a Boolean value that indicates whether the set has no members in /// common with the given set. @@ -84,17 +88,15 @@ extension BitSet { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public func isDisjoint(with other: S) -> Bool - where S.Element == Int - { - if S.self == BitSet.self { - return self.isDisjoint(with: other as! BitSet) + public func isDisjoint(with other: some Sequence) -> Bool { + if let other = _specialize(other, for: BitSet.self) { + return self.isDisjoint(with: other) } - if S.self == BitSet.Counted.self { - return self.isDisjoint(with: other as! BitSet.Counted) + if let other = _specialize(other, for: BitSet.Counted.self) { + return self.isDisjoint(with: other) } - if S.self == Range.self { - return self.isDisjoint(with: other as! Range) + if let other = _specialize(other, for: Range.self) { + return self.isDisjoint(with: other) } for value in other { guard !contains(value) else { return false } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isEqualSet.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isEqualSet.swift index e0f4e2bc..be742e73 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isEqualSet.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isEqualSet.swift @@ -55,15 +55,15 @@ extension BitSet { /// bits.isEqualSet(to: other) // true /// /// - Complexity: O(*n*), where *n* is the number of items in `other`. - public func isEqualSet(to other: S) -> Bool where S.Element == Int { - if S.self == Self.self { - return isEqualSet(to: other as! Self) + public func isEqualSet(to other: some Sequence) -> Bool { + if let other = _specialize(other, for: BitSet.self) { + return isEqualSet(to: other) } - if S.self == BitSet.Counted.self { - return isEqualSet(to: other as! BitSet.Counted) + if let other = _specialize(other, for: BitSet.Counted.self) { + return isEqualSet(to: other) } - if S.self == Range.self { - return isEqualSet(to: other as! Range) + if let other = _specialize(other, for: Range.self) { + return isEqualSet(to: other) } if self.isEmpty { @@ -82,7 +82,7 @@ extension BitSet { precondition( seen <= self.count, // Otherwise other.underestimatedCount != other.count - "Invalid Collection '\(S.self)' (bad underestimatedCount)") + "Invalid Collection '\(type(of: other))' (bad underestimatedCount)") return seen == self.count } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift index fdfa7db9..60f9966f 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSubset.swift @@ -114,17 +114,15 @@ extension BitSet { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public func isStrictSubset(of other: S) -> Bool - where S.Element == Int - { - if S.self == BitSet.self { - return isStrictSubset(of: other as! BitSet) + public func isStrictSubset(of other: some Sequence) -> Bool { + if let other = _specialize(other, for: BitSet.self) { + return isStrictSubset(of: other) } - if S.self == BitSet.Counted.self { - return isStrictSubset(of: other as! BitSet.Counted) + if let other = _specialize(other, for: BitSet.Counted.self) { + return isStrictSubset(of: other) } - if S.self == Range.self { - return isStrictSubset(of: other as! Range) + if let other = _specialize(other, for: Range.self) { + return isStrictSubset(of: other) } if isEmpty { diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift index c84f8f18..f98a81fb 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isStrictSuperset.swift @@ -99,18 +99,16 @@ extension BitSet { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `other`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public func isStrictSuperset(of other: S) -> Bool - where S.Element == Int - { + public func isStrictSuperset(of other: some Sequence) -> Bool { guard !isEmpty else { return false } - if S.self == BitSet.self { - return isStrictSuperset(of: other as! BitSet) + if let other = _specialize(other, for: BitSet.self) { + return isStrictSuperset(of: other) } - if S.self == BitSet.Counted.self { - return isStrictSuperset(of: other as! BitSet.Counted) + if let other = _specialize(other, for: BitSet.Counted.self) { + return isStrictSuperset(of: other) } - if S.self == Range.self { - return isStrictSuperset(of: other as! Range) + if let other = _specialize(other, for: Range.self) { + return isStrictSuperset(of: other) } return _UnsafeHandle.withTemporaryBitSet( wordCount: _storage.count diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift index 25fdee81..8ec633d9 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSubset.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension BitSet { /// Returns a Boolean value that indicates whether this set is a subset of /// the given set. @@ -100,17 +104,15 @@ extension BitSet { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public func isSubset(of other: S) -> Bool - where S.Element == Int - { - if S.self == BitSet.self { - return self.isSubset(of: other as! BitSet) + public func isSubset(of other: some Sequence) -> Bool { + if let other = _specialize(other, for: BitSet.self) { + return self.isSubset(of: other) } - if S.self == BitSet.Counted.self { - return self.isSubset(of: other as! BitSet.Counted) + if let other = _specialize(other, for: BitSet.Counted.self) { + return self.isSubset(of: other) } - if S.self == Range.self { - return self.isSubset(of: other as! Range) + if let other = _specialize(other, for: Range.self) { + return self.isSubset(of: other) } var it = self.makeIterator() diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift index cc85e2b2..1fe93ea9 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra isSuperset.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension BitSet { /// Returns a Boolean value that indicates whether this set is a superset of /// the given set. @@ -88,17 +92,15 @@ extension BitSet { /// - Complexity: The same as the complexity of iterating over all elements /// in `other`. @inlinable - public func isSuperset(of other: S) -> Bool - where S.Element == Int - { - if S.self == BitSet.self { - return self.isSuperset(of: other as! BitSet) + public func isSuperset(of other: some Sequence) -> Bool { + if let other = _specialize(other, for: BitSet.self) { + return self.isSuperset(of: other) } - if S.self == BitSet.Counted.self { - return self.isSuperset(of: other as! BitSet.Counted) + if let other = _specialize(other, for: BitSet.Counted.self) { + return self.isSuperset(of: other) } - if S.self == Range.self { - return self.isSuperset(of: other as! Range) + if let other = _specialize(other, for: Range.self) { + return self.isSuperset(of: other) } for i in other { guard let i = UInt(exactly: i) else { return false } diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift index 06fcbfe9..b19e134e 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtract.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension BitSet { /// Removes the elements of the given bit set from this set. /// @@ -80,19 +84,17 @@ extension BitSet { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public mutating func subtract( - _ other: S - ) where S.Element == Int { - if S.self == BitSet.self { - self.subtract(other as! BitSet) + public mutating func subtract(_ other: some Sequence) { + if let other = _specialize(other, for: BitSet.self) { + self.subtract(other) return } - if S.self == BitSet.Counted.self { - self.subtract(other as! BitSet.Counted) + if let other = _specialize(other, for: BitSet.Counted.self) { + self.subtract(other) return } - if S.self == Range.self { - self.subtract(other as! Range) + if let other = _specialize(other, for: Range.self) { + self.subtract(other) return } var it = other.makeIterator() diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift index a58eee63..4749cea9 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra subtracting.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension BitSet { /// Returns a new set containing the elements of this set that do not occur /// in the given other set. @@ -80,11 +84,7 @@ extension BitSet { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public func subtracting( - _ other: __owned S - ) -> Self - where S.Element == Int - { + public func subtracting(_ other: __owned some Sequence) -> 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 79e8cbb8..b10327f2 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra symmetricDifference.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension BitSet { /// Returns a new bit set with the elements that are either in this set or in /// `other`, but not in both. @@ -79,13 +83,11 @@ extension BitSet { /// input, and *k* is the complexity of iterating over all elements in /// `other`. @inlinable - public func symmetricDifference( - _ other: __owned S - ) -> Self - where S.Element == Int - { - if S.self == Range.self { - return symmetricDifference(other as! Range) + public func symmetricDifference( + _ other: __owned some Sequence + ) -> Self { + if let other = _specialize(other, for: Range.self) { + return symmetricDifference(other) } // Note: BitSet & BitSet.Counted are handled in the BitSet initializer below return symmetricDifference(BitSet(other)) diff --git a/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift b/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift index 21e9187f..500eaeed 100644 --- a/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift +++ b/Sources/BitCollections/BitSet/BitSet+SetAlgebra union.swift @@ -71,19 +71,15 @@ extension BitSet { /// input, and *k* is the complexity of iterating over all elements in /// `other`. @inlinable - public func union( - _ other: __owned S - ) -> Self - where S.Element == Int - { - if S.self == BitSet.self { - return union(other as! BitSet) + public func union(_ other: __owned some Sequence) -> Self { + if let other = _specialize(other, for: BitSet.self) { + return union(other) } - if S.self == BitSet.Counted.self { - return union(other as! BitSet.Counted) + if let other = _specialize(other, for: BitSet.Counted.self) { + return union(other) } - if S.self == Range.self { - return union(other as! Range) + if let other = _specialize(other, for: Range.self) { + return union(other) } var result = self result.formUnion(other) diff --git a/Sources/BitCollections/BitSet/BitSet.Counted.swift b/Sources/BitCollections/BitSet/BitSet.Counted.swift index 6b2992db..077ce9a3 100644 --- a/Sources/BitCollections/BitSet/BitSet.Counted.swift +++ b/Sources/BitCollections/BitSet/BitSet.Counted.swift @@ -52,12 +52,12 @@ extension BitSet.Counted { } @inlinable - public init(words: S) where S.Element == UInt { + public init(words: some Sequence) { self.init(BitSet(words: words)) } @inlinable - public init(bitPattern x: I) { + public init(bitPattern x: some BinaryInteger) { self.init(words: x.words) } @@ -66,9 +66,7 @@ extension BitSet.Counted { } @inlinable - public init( - _ elements: __owned S - ) where S.Element == Int { + public init(_ elements: __owned some Sequence) { self.init(BitSet(elements)) } @@ -252,9 +250,7 @@ extension BitSet.Counted { // Extras _bits[members: bounds] } - public subscript(members bounds: R) -> Slice - where R.Bound == Int - { + public subscript(members bounds: some RangeExpression) -> Slice { _bits[members: bounds] } @@ -310,9 +306,9 @@ extension BitSet.Counted { BitSet.Counted(BitSet.random(upTo: limit)) } - public static func random( + public static func random( upTo limit: Int, - using rng: inout R + using rng: inout some RandomNumberGenerator ) -> BitSet.Counted { BitSet.Counted(BitSet.random(upTo: limit, using: &rng)) } @@ -398,10 +394,9 @@ extension BitSet.Counted { /// input, and *k* is the complexity of iterating over all elements in /// `other`. @inlinable - public func union( - _ other: __owned S - ) -> Self - where S.Element == Int { + public func union( + _ other: __owned some Sequence + ) -> Self { _bits.union(other).counted } } @@ -465,10 +460,9 @@ extension BitSet.Counted { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public func intersection( - _ other: __owned S - ) -> Self - where S.Element == Int { + public func intersection( + _ other: __owned some Sequence + ) -> Self { _bits.intersection(other).counted } } @@ -534,10 +528,9 @@ extension BitSet.Counted { /// input, and *k* is the complexity of iterating over all elements in /// `other`. @inlinable - public func symmetricDifference( - _ other: __owned S - ) -> Self - where S.Element == Int { + public func symmetricDifference( + _ other: __owned some Sequence + ) -> Self { _bits.symmetricDifference(other).counted } } @@ -604,10 +597,9 @@ extension BitSet.Counted { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public func subtracting( - _ other: __owned S - ) -> Self - where S.Element == Int { + public func subtracting( + _ other: __owned some Sequence + ) -> Self { _bits.subtracting(other).counted } } @@ -673,9 +665,9 @@ extension BitSet.Counted { /// input, and *k* is the complexity of iterating over all elements in /// `other`. @inlinable - public mutating func formUnion( - _ other: __owned S - ) where S.Element == Int { + public mutating func formUnion( + _ other: __owned some Sequence + ) { _bits.formUnion(other) _count = _bits.count _checkInvariants() @@ -742,9 +734,9 @@ extension BitSet.Counted { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public mutating func formIntersection( - _ other: __owned S - ) where S.Element == Int { + public mutating func formIntersection( + _ other: __owned some Sequence + ) { _bits.formIntersection(other) _count = _bits.count _checkInvariants() @@ -816,9 +808,9 @@ extension BitSet.Counted { /// input, and *k* is the complexity of iterating over all elements in /// `other`. @inlinable - public mutating func formSymmetricDifference( - _ other: __owned S - ) where S.Element == Int { + public mutating func formSymmetricDifference( + _ other: __owned some Sequence + ) { _bits.formSymmetricDifference(other) _count = _bits.count _checkInvariants() @@ -887,9 +879,9 @@ extension BitSet.Counted { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public mutating func subtract( - _ other: __owned S - ) where S.Element == Int { + public mutating func subtract( + _ other: __owned some Sequence + ) { _bits.subtract(other) _count = _bits.count _checkInvariants() @@ -939,9 +931,7 @@ extension BitSet.Counted { /// /// - Complexity: O(*n*), where *n* is the number of items in `other`. @inlinable - public func isEqualSet(to other: S) -> Bool - where S.Element == Int - { + public func isEqualSet(to other: some Sequence) -> Bool { guard self.count >= other.underestimatedCount else { return false } return _bits.isEqualSet(to: other) } @@ -1025,9 +1015,7 @@ extension BitSet.Counted { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public func isSubset(of other: S) -> Bool - where S.Element == Int - { + public func isSubset(of other: some Sequence) -> Bool { _bits.isSubset(of: other) } } @@ -1109,9 +1097,7 @@ extension BitSet.Counted { /// - Complexity: The same as the complexity of iterating over all elements /// in `other`. @inlinable - public func isSuperset(of other: S) -> Bool - where S.Element == Int - { + public func isSuperset(of other: some Sequence) -> Bool { _bits.isSuperset(of: other) } } @@ -1203,9 +1189,7 @@ extension BitSet.Counted { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public func isStrictSubset(of other: S) -> Bool - where S.Element == Int - { + public func isStrictSubset(of other: some Sequence) -> Bool { _bits.isStrictSubset(of: other) } } @@ -1289,9 +1273,7 @@ extension BitSet.Counted { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `other`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public func isStrictSuperset(of other: S) -> Bool - where S.Element == Int - { + public func isStrictSuperset(of other: some Sequence) -> Bool { _bits.isStrictSuperset(of: other) } } @@ -1362,9 +1344,7 @@ extension BitSet.Counted { /// - Complexity: O(*max*) + *k*, where *max* is the largest item in `self`, /// and *k* is the complexity of iterating over all elements in `other`. @inlinable - public func isDisjoint(with other: S) -> Bool - where S.Element == Int - { + public func isDisjoint(with other: some Sequence) -> Bool { _bits.isDisjoint(with: other) } }