diff --git a/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Elements.swift b/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Elements.swift index 253a9ae3..432030b3 100644 --- a/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Elements.swift +++ b/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Elements.swift @@ -511,8 +511,8 @@ extension OrderedDictionary.Elements { /// change when your program is compiled using a different version of /// Swift. @inlinable - public mutating func shuffle( - using generator: inout T + public mutating func shuffle( + using generator: inout some RandomNumberGenerator ) { _base.shuffle(using: &generator) } @@ -585,9 +585,9 @@ extension OrderedDictionary.Elements { /// /// - Complexity: O(`count`) @inlinable - public mutating func removeSubrange( - _ bounds: R - ) where R.Bound == Int { + public mutating func removeSubrange( + _ bounds: some RangeExpression + ) { _base.removeSubrange(bounds) } diff --git a/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Initializers.swift b/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Initializers.swift index 93352ef7..324d4064 100644 --- a/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Initializers.swift +++ b/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Initializers.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension OrderedDictionary { /// Creates an empty dictionary. /// @@ -77,10 +81,12 @@ extension OrderedDictionary { /// key-value pairs, if `Key` implements high-quality hashing. @_disfavoredOverload // https://github.com/apple/swift-collections/issues/125 @inlinable - public init( - uniqueKeysWithValues keysAndValues: S - ) where S.Element == (key: Key, value: Value) { - if S.self == Dictionary.self { + public init( + uniqueKeysWithValues keysAndValues: some Sequence<(key: Key, value: Value)> + ) { + if let keysAndValues = _specialize( + keysAndValues, for: Dictionary.self + ) { self.init(_uncheckedUniqueKeysWithValues: keysAndValues) return } @@ -113,9 +119,9 @@ extension OrderedDictionary { /// - Complexity: Expected O(*n*) on average, where *n* is the count if /// key-value pairs, if `Key` implements high-quality hashing. @inlinable - public init( - uniqueKeysWithValues keysAndValues: S - ) where S.Element == (Key, Value) { + public init( + uniqueKeysWithValues keysAndValues: some Sequence<(Key, Value)> + ) { self.init() reserveCapacity(keysAndValues.underestimatedCount) for (key, value) in keysAndValues { @@ -148,10 +154,10 @@ extension OrderedDictionary { /// - Complexity: Expected O(*n*) on average, where *n* is the count if /// key-value pairs, if `Key` implements high-quality hashing. @inlinable - public init( - uniqueKeys keys: Keys, - values: Values - ) where Keys.Element == Key, Values.Element == Value { + public init( + uniqueKeys keys: some Sequence, + values: some Sequence + ) { let keys = ContiguousArray(keys) let values = ContiguousArray(values) precondition(keys.count == values.count, @@ -199,10 +205,10 @@ extension OrderedDictionary { @_disfavoredOverload // https://github.com/apple/swift-collections/issues/125 @inlinable @inline(__always) - public init( - _ keysAndValues: S, + public init( + _ keysAndValues: some Sequence<(key: Key, value: Value)>, uniquingKeysWith combine: (Value, Value) throws -> Value - ) rethrows where S.Element == (key: Key, value: Value) { + ) rethrows { self.init() try self.merge(keysAndValues, uniquingKeysWith: combine) } @@ -241,10 +247,10 @@ extension OrderedDictionary { /// key-value pairs, if `Key` implements high-quality hashing. @inlinable @inline(__always) - public init( - _ keysAndValues: S, + public init( + _ keysAndValues: some Sequence<(Key, Value)>, uniquingKeysWith combine: (Value, Value) throws -> Value - ) rethrows where S.Element == (Key, Value) { + ) rethrows { self.init() try self.merge(keysAndValues, uniquingKeysWith: combine) } @@ -337,9 +343,10 @@ extension OrderedDictionary { extension OrderedDictionary { @inlinable - internal init( - _uncheckedUniqueKeysWithValues keysAndValues: S - ) where S.Element == (key: Key, value: Value) { + internal init( + _uncheckedUniqueKeysWithValues keysAndValues: + some Sequence<(key: Key, value: Value)> + ) { self.init() reserveCapacity(keysAndValues.underestimatedCount) for (key, value) in keysAndValues { @@ -372,9 +379,10 @@ extension OrderedDictionary { /// key-value pairs, if `Key` implements high-quality hashing. @_disfavoredOverload // https://github.com/apple/swift-collections/issues/125 @inlinable - public init( - uncheckedUniqueKeysWithValues keysAndValues: S - ) where S.Element == (key: Key, value: Value) { + public init( + uncheckedUniqueKeysWithValues keysAndValues: + some Sequence<(key: Key, value: Value)> + ) { #if DEBUG self.init(uniqueKeysWithValues: keysAndValues) #else @@ -404,9 +412,9 @@ extension OrderedDictionary { /// - Complexity: Expected O(*n*) on average, where *n* is the count if /// key-value pairs, if `Key` implements high-quality hashing. @inlinable - public init( - uncheckedUniqueKeysWithValues keysAndValues: S - ) where S.Element == (Key, Value) { + public init( + uncheckedUniqueKeysWithValues keysAndValues: some Sequence<(Key, Value)> + ) { // Add tuple labels let keysAndValues = keysAndValues.lazy.map { (key: $0.0, value: $0.1) } self.init(uncheckedUniqueKeysWithValues: keysAndValues) @@ -439,10 +447,10 @@ extension OrderedDictionary { /// key-value pairs, if `Key` implements high-quality hashing. @inlinable @inline(__always) - public init( - uncheckedUniqueKeys keys: Keys, - values: Values - ) where Keys.Element == Key, Values.Element == Value { + public init( + uncheckedUniqueKeys keys: some Sequence, + values: some Sequence + ) { #if DEBUG self.init(uniqueKeys: keys, values: values) #else diff --git a/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Partial MutableCollection.swift b/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Partial MutableCollection.swift index 35970a7f..898b30d2 100644 --- a/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Partial MutableCollection.swift +++ b/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Partial MutableCollection.swift @@ -160,8 +160,8 @@ extension OrderedDictionary { /// change when your program is compiled using a different version of /// Swift. @inlinable - public mutating func shuffle( - using generator: inout T + public mutating func shuffle( + using generator: inout some RandomNumberGenerator ) { guard count > 1 else { return } var keys = self._keys.elements diff --git a/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Partial RangeReplaceableCollection.swift b/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Partial RangeReplaceableCollection.swift index a681b813..74a32c2d 100644 --- a/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Partial RangeReplaceableCollection.swift +++ b/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary+Partial RangeReplaceableCollection.swift @@ -93,9 +93,9 @@ extension OrderedDictionary { /// /// - Complexity: O(`count`) @inlinable - public mutating func removeSubrange( - _ bounds: R - ) where R.Bound == Int { + public mutating func removeSubrange( + _ bounds: some RangeExpression + ) { removeSubrange(bounds.relative(to: elements)) } diff --git a/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary.swift b/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary.swift index 227dad23..ee791e04 100644 --- a/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary.swift +++ b/Sources/OrderedCollections/OrderedDictionary/OrderedDictionary.swift @@ -819,10 +819,10 @@ extension OrderedDictionary { /// elements in `keysAndValues`, if `Key` implements high-quality hashing. @_disfavoredOverload // https://github.com/apple/swift-collections/issues/125 @inlinable - public mutating func merge( - _ keysAndValues: __owned S, + public mutating func merge( + _ keysAndValues: __owned some Sequence<(key: Key, value: Value)>, uniquingKeysWith combine: (Value, Value) throws -> Value - ) rethrows where S.Element == (key: Key, value: Value) { + ) rethrows { for (key, value) in keysAndValues { let (index, bucket) = _keys._find(key) if let index = index { @@ -869,10 +869,10 @@ extension OrderedDictionary { /// - Complexity: Expected to be O(*n*) on average, where *n* is the number of /// elements in `keysAndValues`, if `Key` implements high-quality hashing. @inlinable - public mutating func merge( - _ keysAndValues: __owned S, + public mutating func merge( + _ keysAndValues: __owned some Sequence<(Key, Value)>, uniquingKeysWith combine: (Value, Value) throws -> Value - ) rethrows where S.Element == (Key, Value) { + ) rethrows { let mapped: LazyMapSequence = keysAndValues.lazy.map { (key: $0.0, value: $0.1) } try merge(mapped, uniquingKeysWith: combine) @@ -915,10 +915,10 @@ extension OrderedDictionary { /// hashing. @_disfavoredOverload // https://github.com/apple/swift-collections/issues/125 @inlinable - public __consuming func merging( - _ other: __owned S, + public __consuming func merging( + _ other: __owned some Sequence<(key: Key, value: Value)>, uniquingKeysWith combine: (Value, Value) throws -> Value - ) rethrows -> Self where S.Element == (key: Key, value: Value) { + ) rethrows -> Self { var copy = self try copy.merge(other, uniquingKeysWith: combine) return copy @@ -960,10 +960,10 @@ extension OrderedDictionary { /// number of elements in `keysAndValues`, if `Key` implements high-quality /// hashing. @inlinable - public __consuming func merging( - _ other: __owned S, + public __consuming func merging( + _ other: __owned some Sequence<(Key, Value)>, uniquingKeysWith combine: (Value, Value) throws -> Value - ) rethrows -> Self where S.Element == (Key, Value) { + ) rethrows -> Self { var copy = self try copy.merge(other, uniquingKeysWith: combine) return copy diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Initializers.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Initializers.swift index 12d6fc47..05dc05eb 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Initializers.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Initializers.swift @@ -32,8 +32,7 @@ extension OrderedSet { /// high-quality hashing. @inlinable @inline(__always) - public init(uncheckedUniqueElements elements: S) - where S.Element == Element { + public init(uncheckedUniqueElements elements: some Sequence) { let elements = ContiguousArray(elements) #if DEBUG let (table, firstDupe) = _HashTable.create(untilFirstDuplicateIn: elements) @@ -61,9 +60,9 @@ extension OrderedSet { /// is the number of elements in the sequence), provided that /// `Element` properly implements hashing. @inlinable - public init(_ elements: S) where S.Element == Element { - if S.self == Self.self { - self = elements as! Self + public init(_ elements: some Sequence) { + if let elements = _specialize(elements, for: Self.self) { + self = elements return } // Fast paths for when we know elements are all unique @@ -136,9 +135,7 @@ extension OrderedSet { /// in the sequence), provided that `Element` implements /// high-quality hashing. @inlinable - public init( - _ elements: C - ) where C.Element == Element { + public init(_ elements: some RandomAccessCollection) { // This code is careful not to copy storage if `C` is an Array // or ContiguousArray and the elements are already unique. let (table, firstDupe) = _HashTable.create( diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Insertions.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Insertions.swift index 6b054366..c8c9bd16 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Insertions.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Insertions.swift @@ -62,7 +62,9 @@ extension OrderedSet { @inlinable @discardableResult - internal mutating func _append(_ item: Element) -> (inserted: Bool, index: Int) { + internal mutating func _append( + _ item: Element + ) -> (inserted: Bool, index: Int) { let (index, bucket) = _find(item) if let index = index { return (false, index) } _appendNew(item, in: bucket) @@ -102,9 +104,9 @@ extension OrderedSet { /// hash, and compare operations on the `Element` type, if it implements /// high-quality hashing. @inlinable - public mutating func append( - contentsOf elements: S - ) where S.Element == Element { + public mutating func append( + contentsOf elements: some Sequence + ) { for item in elements { _append(item) } diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial MutableCollection.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial MutableCollection.swift index b917e04c..e4dc1411 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial MutableCollection.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial MutableCollection.swift @@ -332,8 +332,8 @@ extension OrderedSet { /// change when your program is compiled using a different version of /// Swift. @inlinable - public mutating func shuffle( - using generator: inout T + public mutating func shuffle( + using generator: inout some RandomNumberGenerator ) { _elements.shuffle(using: &generator) _regenerateExistingHashTable() diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial RangeReplaceableCollection.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial RangeReplaceableCollection.swift index fd8750d0..d075f4b7 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial RangeReplaceableCollection.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial RangeReplaceableCollection.swift @@ -111,9 +111,7 @@ extension OrderedSet { /// /// - Complexity: O(`count`) @inlinable - public mutating func removeSubrange( - _ bounds: R - ) where R.Bound == Int { + public mutating func removeSubrange(_ bounds: some RangeExpression) { removeSubrange(bounds.relative(to: self)) } diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra formIntersection.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra formIntersection.swift index 9fa4d8a0..f46793cd 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra formIntersection.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra formIntersection.swift @@ -77,9 +77,9 @@ extension OrderedSet { /// - Complexity: Expected to be O(*n*) on average where *n* is the number of /// elements in `other`, if `Element` implements high-quality hashing. @inlinable - public mutating func formIntersection( - _ other: S - ) where S.Element == Element { + public mutating func formIntersection( + _ other: some Sequence + ) { self = self.intersection(other) } } diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra formSymmetricDifference.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra formSymmetricDifference.swift index c028711d..4660213e 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra formSymmetricDifference.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra formSymmetricDifference.swift @@ -79,9 +79,9 @@ extension OrderedSet { /// the number of elements in `other`, if `Element` implements high-quality /// hashing. @inlinable - public mutating func formSymmetricDifference( - _ other: __owned S - ) where S.Element == Element { + public mutating func formSymmetricDifference( + _ other: __owned some Sequence + ) { self = self.symmetricDifference(other) } } diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra formUnion.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra formUnion.swift index 79dd20c8..361f1b87 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra formUnion.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra formUnion.swift @@ -94,9 +94,9 @@ extension OrderedSet { /// - Complexity: Expected to be O(`self.count` + `other.count`) on average, /// if `Element` implements high-quality hashing. @inlinable - public mutating func formUnion( - _ other: __owned S - ) where S.Element == Element { + public mutating func formUnion( + _ other: __owned some Sequence + ) { append(contentsOf: other) } } diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra intersection.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra intersection.swift index c71061a1..6a1b9a0a 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra intersection.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra intersection.swift @@ -94,9 +94,9 @@ extension OrderedSet { /// - Complexity: Expected to be O(*n*) on average where *n* is the number of /// elements in `other`, if `Element` implements high-quality hashing. @inlinable - public __consuming func intersection( - _ other: S - ) -> Self where S.Element == Element { + public __consuming func intersection( + _ other: some Sequence + ) -> Self { _UnsafeBitSet.withTemporaryBitSet(capacity: self.count) { bitset in for item in other { if let index = self._find_inlined(item).index { diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isDisjoint.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isDisjoint.swift index 60ed2b96..afdb20cf 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isDisjoint.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isDisjoint.swift @@ -118,9 +118,9 @@ extension OrderedSet { /// - Complexity: Expected to be O(*n*) on average, where *n* is the number of /// elements in `other`, if `Element` implements high-quality hashing. @inlinable - public func isDisjoint( - with other: S - ) -> Bool where S.Element == Element { + public func isDisjoint( + with other: some Sequence + ) -> Bool { guard !self.isEmpty else { return true } for item in other { if self.contains(item) { return false } diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isEqualSet.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isEqualSet.swift index 2f69d658..24e2a7e6 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isEqualSet.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isEqualSet.swift @@ -46,10 +46,9 @@ extension OrderedSet { /// /// - Complexity: O(*n*), where *n* is the number of items in /// `other`, as long as`Element` properly implements hashing. - public func isEqualSet(to other: S) -> Bool - where S.Element == Element { - 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: Self.self) { + return isEqualSet(to: other) } if self.isEmpty { @@ -67,7 +66,7 @@ extension OrderedSet { 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/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isStrictSubset.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isStrictSubset.swift index fcdbb951..edb4780e 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isStrictSubset.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isStrictSubset.swift @@ -115,14 +115,14 @@ extension OrderedSet { /// is the number of elements in `other`, if `Element` implements /// high-quality hashing. @inlinable - public func isStrictSubset( - of other: S - ) -> Bool where S.Element == Element { - if S.self == Self.self { - return self.isStrictSubset(of: other as! Self) + public func isStrictSubset( + of other: some Sequence + ) -> Bool { + if let other = _specialize(other, for: Self.self) { + return self.isStrictSubset(of: other) } - if S.self == Set.self { - return self.isStrictSubset(of: other as! Set) + if let other = _specialize(other, for: Set.self) { + return self.isStrictSubset(of: other) } var it = self.makeIterator() diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isStrictSuperset.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isStrictSuperset.swift index 61b70850..dfe84492 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isStrictSuperset.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isStrictSuperset.swift @@ -116,14 +116,14 @@ extension OrderedSet { /// is the number of elements in `other`, if `Element` implements /// high-quality hashing. @inlinable - public func isStrictSuperset( - of other: S - ) -> Bool where S.Element == Element { - if S.self == Self.self { - return self.isStrictSuperset(of: other as! Self) + public func isStrictSuperset( + of other: some Sequence + ) -> Bool { + if let other = _specialize(other, for: Self.self) { + return self.isStrictSuperset(of: other) } - if S.self == Set.self { - return self.isStrictSuperset(of: other as! Set) + if let other = _specialize(other, for: Set.self) { + return self.isStrictSuperset(of: other) } var it = self.makeIterator() diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isSubset.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isSubset.swift index 0b1c64f8..ef7386d2 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isSubset.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isSubset.swift @@ -115,13 +115,13 @@ extension OrderedSet { /// is the number of elements in `other`, if `Element` implements /// high-quality hashing. @inlinable - public func isSubset( - of other: S - ) -> Bool where S.Element == Element { + public func isSubset( + of other: some Sequence + ) -> Bool { guard !isEmpty else { return true } - if S.self == Self.self { - return isSubset(of: other as! Self) + if let other = _specialize(other, for: Self.self) { + return isSubset(of: other) } var it = self.makeIterator() diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isSuperset.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isSuperset.swift index 2cb62aca..9925d7a1 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isSuperset.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra isSuperset.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + // `OrderedSet` does not directly conform to `SetAlgebra` because its definition // of equality conflicts with `SetAlgebra` requirements. However, it still // implements most `SetAlgebra` requirements (except `insert`, which is replaced @@ -103,18 +107,14 @@ extension OrderedSet { /// - Complexity: Expected to be O(*n*) on average, where *n* is the number of /// elements in `other`, if `Element` implements high-quality hashing. @inlinable - public func isSuperset( - of other: S - ) -> Bool where S.Element == Element { + public func isSuperset(of other: some Sequence) -> Bool { _isSuperset(of: other) } @inlinable - internal func _isSuperset( - of other: S - ) -> Bool where S.Element == Element { - if S.self == Self.self { - return self.isSuperset(of: other as! Self) + internal func _isSuperset(of other: some Sequence) -> Bool { + if let other = _specialize(other, for: Self.self) { + return self.isSuperset(of: other) } for item in other { guard self.contains(item) else { return false } diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra subtract.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra subtract.swift index f21c9890..b100650d 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra subtract.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra subtract.swift @@ -68,9 +68,7 @@ extension OrderedSet { /// high-quality hashing. @inlinable @inline(__always) - public mutating func subtract( - _ other: S - ) where S.Element == Element { + public mutating func subtract(_ other: some Sequence) { self = _subtracting(other) } } diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra subtracting.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra subtracting.swift index f5e54d52..d69a628e 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra subtracting.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra subtracting.swift @@ -83,16 +83,12 @@ extension OrderedSet { /// `Element` implements high-quality hashing. @inlinable @inline(__always) - public __consuming func subtracting( - _ other: S - ) -> Self where S.Element == Element { + public __consuming func subtracting(_ other: some Sequence) -> Self { _subtracting(other) } @inlinable - __consuming func _subtracting( - _ other: S - ) -> Self where S.Element == Element { + __consuming func _subtracting(_ other: some Sequence) -> Self { guard count > 0 else { return Self() } return _UnsafeBitSet.withTemporaryBitSet(capacity: count) { difference in difference.insertAll(upTo: count) diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra symmetricDifference.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra symmetricDifference.swift index 327d8096..637d51bc 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra symmetricDifference.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra symmetricDifference.swift @@ -113,9 +113,9 @@ extension OrderedSet { /// the number of elements in `other`, if `Element` implements high-quality /// hashing. @inlinable - public __consuming func symmetricDifference( - _ other: __owned S - ) -> Self where S.Element == Element { + public __consuming func symmetricDifference( + _ other: __owned some Sequence + ) -> Self { _UnsafeBitSet.withTemporaryBitSet(capacity: self.count) { bitset in var new = Self() bitset.insertAll(upTo: self.count) diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra union.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra union.swift index 15c23b5f..f85378f5 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra union.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Partial SetAlgebra union.swift @@ -74,9 +74,9 @@ extension OrderedSet { /// - Complexity: Expected to be O(`self.count` + `other.count`) on average, /// if `Element` implements high-quality hashing. @inlinable - public __consuming func union( - _ other: __owned S - ) -> Self where S.Element == Element { + public __consuming func union( + _ other: __owned some Sequence + ) -> Self { var result = self result.formUnion(other) return result diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+Testing.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+Testing.swift index 96f81271..563a1b14 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+Testing.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+Testing.swift @@ -110,11 +110,11 @@ extension OrderedSet._UnstableInternals { extension OrderedSet { @_spi(Testing) - public init( + public init( _scale scale: Int, bias: Int, - contents: S - ) where S.Element == Element { + contents: some Sequence + ) { let contents = ContiguousArray(contents) precondition(scale >= _HashTable.scale(forCapacity: contents.count)) precondition(scale <= _HashTable.maximumScale) diff --git a/Sources/OrderedCollections/OrderedSet/OrderedSet+UnorderedView.swift b/Sources/OrderedCollections/OrderedSet/OrderedSet+UnorderedView.swift index 2c52231c..3a95a719 100644 --- a/Sources/OrderedCollections/OrderedSet/OrderedSet+UnorderedView.swift +++ b/Sources/OrderedCollections/OrderedSet/OrderedSet+UnorderedView.swift @@ -167,7 +167,7 @@ extension OrderedSet.UnorderedView { /// high-quality hashing. @inlinable @inline(__always) - public init(_ elements: S) where S.Element == Element { + public init(_ elements: some Sequence) { _base = OrderedSet(elements) } @@ -368,9 +368,7 @@ extension OrderedSet.UnorderedView { /// - Complexity: Expected to be O(`other.count`) on average, if `Element` /// implements high-quality hashing. @inlinable - public mutating func formUnion( - _ other: __owned S - ) where S.Element == Element { + public mutating func formUnion(_ other: __owned some Sequence) { _base.formUnion(other) } @@ -387,9 +385,9 @@ extension OrderedSet.UnorderedView { /// - Complexity: Expected to be O(`self.count` + `other.count`) on average, /// if `Element` implements high-quality hashing. @inlinable - public __consuming func union( - _ other: __owned S - ) -> Self where S.Element == Element { + public __consuming func union( + _ other: __owned some Sequence + ) -> Self { _base.union(other).unordered } } @@ -444,9 +442,9 @@ extension OrderedSet.UnorderedView { /// - Complexity: Expected to be O(*n*) on average where *n* is the number of /// elements in `other`, if `Element` implements high-quality hashing. @inlinable - public __consuming func intersection( - _ other: S - ) -> Self where S.Element == Element { + public __consuming func intersection( + _ other: some Sequence + ) -> Self { _base.intersection(other).unordered } @@ -461,9 +459,9 @@ extension OrderedSet.UnorderedView { /// - Complexity: Expected to be O(*n*) on average where *n* is the number of /// elements in `other`, if `Element` implements high-quality hashing. @inlinable - public mutating func formIntersection( - _ other: S - ) where S.Element == Element { + public mutating func formIntersection( + _ other: some Sequence + ) { _base.formIntersection(other) } } @@ -529,9 +527,9 @@ extension OrderedSet.UnorderedView { /// the number of elements in `other`, if `Element` implements high-quality /// hashing. @inlinable - public __consuming func symmetricDifference( - _ other: __owned S - ) -> Self where S.Element == Element { + public __consuming func symmetricDifference( + _ other: __owned some Sequence + ) -> Self { _base.symmetricDifference(other).unordered } @@ -552,9 +550,9 @@ extension OrderedSet.UnorderedView { /// the number of elements in `other`, if `Element` implements high-quality /// hashing. @inlinable - public mutating func formSymmetricDifference( - _ other: __owned S - ) where S.Element == Element { + public mutating func formSymmetricDifference( + _ other: __owned some Sequence + ) { _base.formSymmetricDifference(other) } } @@ -613,9 +611,7 @@ extension OrderedSet.UnorderedView { /// - Complexity: Expected to be O(`self.count + other.count`) on average, if /// `Element` implements high-quality hashing. @inlinable - public __consuming func subtracting( - _ other: S - ) -> Self where S.Element == Element { + public __consuming func subtracting(_ other: some Sequence) -> Self { _base.subtracting(other).unordered } @@ -631,9 +627,7 @@ extension OrderedSet.UnorderedView { /// is the number of elements in `other`, if `Element` implements /// high-quality hashing. @inlinable - public mutating func subtract( - _ other: S - ) where S.Element == Element { + public mutating func subtract(_ other: some Sequence) { _base.subtract(other) } } @@ -662,8 +656,7 @@ extension OrderedSet.UnorderedView { /// /// - Complexity: O(*n*), where *n* is the number of items in /// `other`, as long as`Element` properly implements hashing. - public func isEqualSet(to other: S) -> Bool - where S.Element == Element { + public func isEqualSet(to other: some Sequence) -> Bool { self._base.isEqualSet(to: other) } } @@ -731,9 +724,7 @@ extension OrderedSet.UnorderedView { /// is the number of elements in `other`, if `Element` implements /// high-quality hashing. @inlinable - public func isSubset( - of other: S - ) -> Bool where S.Element == Element { + public func isSubset(of other: some Sequence) -> Bool { _base.isSubset(of: other) } } @@ -800,9 +791,7 @@ extension OrderedSet.UnorderedView { /// - Complexity: Expected to be O(*n*) on average, where *n* is the number of /// elements in `other`, if `Element` implements high-quality hashing. @inlinable - public func isSuperset( - of other: S - ) -> Bool where S.Element == Element { + public func isSuperset(of other: some Sequence) -> Bool { _base.isSuperset(of: other) } } @@ -876,9 +865,7 @@ extension OrderedSet.UnorderedView { /// is the number of elements in `other`, if `Element` implements /// high-quality hashing. @inlinable - public func isStrictSubset( - of other: S - ) -> Bool where S.Element == Element { + public func isStrictSubset(of other: some Sequence) -> Bool { _base.isStrictSubset(of: other) } } @@ -952,9 +939,7 @@ extension OrderedSet.UnorderedView { /// is the number of elements in `other`, if `Element` implements /// high-quality hashing. @inlinable - public func isStrictSuperset( - of other: S - ) -> Bool where S.Element == Element { + public func isStrictSuperset(of other: some Sequence) -> Bool { _base.isStrictSuperset(of: other) } } @@ -1015,9 +1000,7 @@ extension OrderedSet.UnorderedView { /// - Complexity: Expected to be O(*n*) on average, where *n* is the number of /// elements in `other`, if `Element` implements high-quality hashing. @inlinable - public func isDisjoint( - with other: S - ) -> Bool where S.Element == Element { + public func isDisjoint(with other: some Sequence) -> Bool { _base.isDisjoint(with: other) } }