diff --git a/Sources/HashTreeCollections/HashNode/_Hash.swift b/Sources/HashTreeCollections/HashNode/_Hash.swift index 975ba561..9546bc76 100644 --- a/Sources/HashTreeCollections/HashNode/_Hash.swift +++ b/Sources/HashTreeCollections/HashNode/_Hash.swift @@ -17,7 +17,7 @@ internal struct _Hash { internal var value: UInt @inlinable - internal init(_ key: Key) { + internal init(_ key: some Hashable) { let hashValue = key._rawHashValue(seed: 0) self.value = UInt(bitPattern: hashValue) } diff --git a/Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Initializers.swift b/Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Initializers.swift index 21963c3c..90cb9628 100644 --- a/Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Initializers.swift +++ b/Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Initializers.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension TreeDictionary { /// Creates an empty dictionary. /// @@ -72,9 +76,9 @@ extension TreeDictionary { /// - Complexity: Expected O(*n*) on average, where *n* is the count if /// key-value pairs, if `Key` properly implements hashing. @inlinable - public init( - uniqueKeysWithValues keysAndValues: S - ) where S.Element == (Key, Value) { + public init( + uniqueKeysWithValues keysAndValues: some Sequence<(Key, Value)> + ) { self.init() for item in keysAndValues { let hash = _Hash(item.0) @@ -104,24 +108,26 @@ extension TreeDictionary { /// key-value pairs, if `Key` properly implements hashing. @_disfavoredOverload // https://github.com/apple/swift-collections/issues/125 @inlinable - public init( - uniqueKeysWithValues keysAndValues: S - ) where S.Element == Element { - if S.self == Self.self { - self = keysAndValues as! Self + public init( + uniqueKeysWithValues keysAndValues: some Sequence + ) { + if let keysAndValues = _specialize(keysAndValues, for: Self.self) { + self = keysAndValues return } - if S.self == Dictionary.self { - self.init(keysAndValues as! Dictionary) + if let keysAndValues = _specialize( + keysAndValues, for: Dictionary.self + ) { + self.init(keysAndValues) return } self.init(_uniqueKeysWithValues: keysAndValues) } @inlinable - internal init( - _uniqueKeysWithValues keysAndValues: S - ) where S.Element == Element { + internal init( + _uniqueKeysWithValues keysAndValues: some Sequence + ) { self.init() for item in keysAndValues { let hash = _Hash(item.key) @@ -163,9 +169,10 @@ extension TreeDictionary { /// /// - Complexity: Expected O(*n*) on average, where *n* is the count of /// key-value pairs, if `Key` properly implements hashing. - public init( - _ keysAndValues: S, uniquingKeysWith combine: (Value, Value) throws -> Value - ) rethrows where S.Element == (Key, Value) { + public init( + _ keysAndValues: some Sequence<(Key, Value)>, + uniquingKeysWith combine: (Value, Value) throws -> Value + ) rethrows { self.init() try self.merge(keysAndValues, uniquingKeysWith: combine) } @@ -203,9 +210,10 @@ extension TreeDictionary { /// - Complexity: Expected O(*n*) on average, where *n* is the count of /// key-value pairs, if `Key` properly implements hashing. @_disfavoredOverload // https://github.com/apple/swift-collections/issues/125 - public init( - _ keysAndValues: S, uniquingKeysWith combine: (Value, Value) throws -> Value - ) rethrows where S.Element == Element { + public init( + _ keysAndValues: some Sequence, + uniquingKeysWith combine: (Value, Value) throws -> Value + ) rethrows { try self.init( keysAndValues.lazy.map { ($0.key, $0.value) }, uniquingKeysWith: combine) diff --git a/Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Merge.swift b/Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Merge.swift index 17e600a2..ba33ee2f 100644 --- a/Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Merge.swift +++ b/Sources/HashTreeCollections/TreeDictionary/TreeDictionary+Merge.swift @@ -75,10 +75,10 @@ extension TreeDictionary { /// duplicate keys. The closure returns the desired value for the final /// dictionary. @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 { for (key, value) in keysAndValues { try self.updateValue(forKey: key) { target in if let old = target { @@ -120,10 +120,10 @@ extension TreeDictionary { /// dictionary. @_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, uniquingKeysWith combine: (Value, Value) throws -> Value - ) rethrows where S.Element == Element { + ) rethrows { try merge( keysAndValues.lazy.map { ($0.key, $0.value) }, uniquingKeysWith: combine) @@ -210,11 +210,10 @@ extension TreeDictionary { /// number of elements in `keysAndValues`, if `Key` implements high-quality /// hashing. @inlinable - public func merging( - _ other: __owned S, + public 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 @@ -257,11 +256,10 @@ extension TreeDictionary { /// hashing. @_disfavoredOverload // https://github.com/apple/swift-collections/issues/125 @inlinable - public func merging( - _ other: __owned S, + public func merging( + _ other: __owned some Sequence, uniquingKeysWith combine: (Value, Value) throws -> Value - ) rethrows -> Self - where S.Element == Element { + ) rethrows -> Self { var copy = self try copy.merge(other, uniquingKeysWith: combine) return copy diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra Initializers.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra Initializers.swift index b6147665..a0412e62 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra Initializers.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra Initializers.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension TreeSet { /// Creates an empty set. /// @@ -32,9 +36,9 @@ extension TreeSet { /// is the number of elements in the sequence), provided that /// `Element` properly implements hashing. @inlinable - public init(_ items: __owned S) where S.Element == Element { - if S.self == Self.self { - self = items as! Self + public init(_ items: __owned some Sequence) { + if let items = _specialize(items, for: Self.self) { + self = items return } self.init() diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra formIntersection.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra formIntersection.swift index 217930fe..960d22e5 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra formIntersection.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra formIntersection.swift @@ -70,9 +70,7 @@ extension TreeSet { /// - Parameter other: An arbitrary finite sequence of items, /// possibly containing duplicate values. @inlinable - public mutating func formIntersection( - _ other: S - ) where S.Element == Element { + public mutating func formIntersection(_ other: some Sequence) { self = intersection(other) } } diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra formSymmetricDifference.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra formSymmetricDifference.swift index 2bbf5690..a2bacf3e 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra formSymmetricDifference.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra formSymmetricDifference.swift @@ -63,8 +63,9 @@ extension TreeSet { /// - Parameter other: A finite sequence of elements, possibly containing /// duplicate values. @inlinable - public mutating func formSymmetricDifference(_ other: __owned S) - where S.Element == Element { + public mutating func formSymmetricDifference( + _ other: __owned some Sequence + ) { self = symmetricDifference(other) } } diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra formUnion.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra formUnion.swift index 0e39424f..e8ccf15a 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra formUnion.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra formUnion.swift @@ -86,8 +86,7 @@ extension TreeSet { /// hash tree structure to minimize work when possible, e.g. by linking /// parts of the input trees directly into the result. @inlinable - public mutating func formUnion(_ other: __owned S) - where S.Element == Element { + public mutating func formUnion(_ other: __owned some Sequence) { self = union(other) } } diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra intersection.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra intersection.swift index c76205aa..1477cc5c 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra intersection.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra intersection.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension TreeSet { /// Returns a new set with the elements that are common to both this set and /// the provided other one. @@ -81,12 +85,11 @@ extension TreeSet { /// - Parameter other: An arbitrary finite sequence of items, /// possibly containing duplicate values. @inlinable - public func intersection( - _ other: S - ) -> Self - where S.Element == Element { - if S.self == Self.self { - return intersection(other as! Self) + public func intersection( + _ other: some Sequence + ) -> Self { + if let other = _specialize(other, for: Self.self) { + return intersection(other) } guard let first = self.first else { return Self() } diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isDisjoint.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isDisjoint.swift index 09872594..d7bb4498 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isDisjoint.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isDisjoint.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension TreeSet { /// Returns a Boolean value that indicates whether the set has no members in /// common with the given set. @@ -66,12 +70,10 @@ extension TreeSet { /// - Complexity: In the worst case, this makes O(*n*) calls to /// `self.contains`, where *n* is the length of the sequence. @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 } - if S.self == Self.self { - return isDisjoint(with: other as! Self) + if let other = _specialize(other, for: Self.self) { + return isDisjoint(with: other) } return other.allSatisfy { !self.contains($0) } } diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isEqualSet.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isEqualSet.swift index ba1e906c..2a01c3f1 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isEqualSet.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isEqualSet.swift @@ -77,11 +77,9 @@ extension TreeSet { /// every available shortcut to reduce complexity, e.g. by skipping /// comparing elements in shared subtrees. @inlinable - 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 { @@ -99,7 +97,7 @@ extension TreeSet { 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/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isStrictSubset.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isStrictSubset.swift index b75ea452..cea533d9 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isStrictSubset.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isStrictSubset.swift @@ -90,11 +90,9 @@ extension TreeSet { /// and it constructs a temporary persistent set containing every /// element of the sequence. @inlinable - public func isStrictSubset(of other: S) -> Bool - where S.Element == Element - { - if S.self == Self.self { - return isStrictSubset(of: other as! Self) + public func isStrictSubset(of other: some Sequence) -> Bool { + if let other = _specialize(other, for: Self.self) { + return isStrictSubset(of: other) } do { diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isStrictSuperset.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isStrictSuperset.swift index ba7eb0a0..28e9b803 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isStrictSuperset.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isStrictSuperset.swift @@ -90,11 +90,9 @@ extension TreeSet { /// and it constructs a temporary persistent set containing every /// element of the sequence. @inlinable - public func isStrictSuperset(of other: S) -> Bool - where S.Element == Element - { - if S.self == Self.self { - return isStrictSuperset(of: other as! Self) + public func isStrictSuperset(of other: some Sequence) -> Bool { + if let other = _specialize(other, for: Self.self) { + return isStrictSuperset(of: other) } var it = self.makeIterator() diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isSubset.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isSubset.swift index 29bf895a..903891af 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isSubset.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isSubset.swift @@ -82,11 +82,9 @@ extension TreeSet { /// and it constructs a temporary persistent set containing every /// element of the sequence. @inlinable - public func isSubset(of other: S) -> Bool - where S.Element == Element - { - if S.self == Self.self { - return isSubset(of: other as! Self) + public func isSubset(of other: some Sequence) -> Bool { + if let other = _specialize(other, for: Self.self) { + return isSubset(of: other) } var it = self.makeIterator() diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isSuperset.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isSuperset.swift index b350dcd9..40ad1b6a 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isSuperset.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra isSuperset.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension TreeSet { /// Returns a Boolean value that indicates whether this set is a superset of /// the given set. @@ -76,11 +80,9 @@ extension TreeSet { /// - Complexity: O(*n*) calls to `self.contains`, where *n* is the number /// of elements in `other`. @inlinable - public func isSuperset(of other: S) -> Bool - where S.Element == Element - { - if S.self == Self.self { - return isSuperset(of: other as! Self) + public func isSuperset(of other: some Sequence) -> Bool { + if let other = _specialize(other, for: Self.self) { + return isSuperset(of: other) } return other.allSatisfy { self.contains($0) } diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra subtract.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra subtract.swift index 94a4d48f..12e3bcf5 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra subtract.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra subtract.swift @@ -65,8 +65,7 @@ extension TreeSet { /// - Complexity: O(*n*) where *n* is the number of elements in `other`, /// as long as `Element` properly implements hashing. @inlinable - public mutating func subtract(_ other: S) - where S.Element == Element { + public mutating func subtract(_ other: some Sequence) { self = subtracting(other) } } diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra subtracting.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra subtracting.swift index 3bcb7b7d..55fc48fd 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra subtracting.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra subtracting.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension TreeSet { /// Returns a new set containing the elements of this set that do not occur /// in the given other set. @@ -73,12 +77,9 @@ extension TreeSet { /// - Complexity: O(*n*) where *n* is the number of elements in `other`, /// as long as `Element` properly implements hashing. @inlinable - public __consuming func subtracting( - _ other: S - ) -> Self - where S.Element == Element { - if S.self == Self.self { - return subtracting(other as! Self) + public __consuming func subtracting(_ other: some Sequence) -> Self { + if let other = _specialize(other, for: Self.self) { + return subtracting(other) } guard let first = self.first else { return Self() } diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra symmetricDifference.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra symmetricDifference.swift index e00bbe24..99acbabf 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra symmetricDifference.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra symmetricDifference.swift @@ -84,10 +84,11 @@ extension TreeSet { /// hash tree structure to minimize work when possible, e.g. by linking /// parts of the input trees directly into the result. @inlinable - public func symmetricDifference(_ other: __owned S) -> Self - where S.Element == Element { - if S.self == Self.self { - return symmetricDifference(other as! Self) + public func symmetricDifference( + _ other: __owned some Sequence + ) -> Self { + if let other = _specialize(other, for: Self.self) { + return symmetricDifference(other) } if other is _UniqueCollection { diff --git a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra union.swift b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra union.swift index 2cdcad76..af29b316 100644 --- a/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra union.swift +++ b/Sources/HashTreeCollections/TreeSet/TreeSet+SetAlgebra union.swift @@ -9,6 +9,10 @@ // //===----------------------------------------------------------------------===// +#if !COLLECTIONS_SINGLE_MODULE +import _CollectionsUtilities +#endif + extension TreeSet { /// Returns a new set with the elements of both this and the given set. /// @@ -91,10 +95,9 @@ extension TreeSet { /// the worst case, where *n* is the number of items in `other`, /// as long as `Element` properly implements hashing. @inlinable - public func union(_ other: __owned S) -> Self - where S.Element == Element { - if S.self == Self.self { - return union(other as! Self) + public func union(_ other: __owned some Sequence) -> Self { + if let other = _specialize(other, for: Self.self) { + return union(other) } var root = self._root