From 0538000ffd0d2fc811ffef4930f2d984ac3b13dc Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Tue, 24 Feb 2026 16:40:45 -0800 Subject: [PATCH] [BasicContainers] Hashed containers: Add Equatable/Hashable conformances and related methods --- Sources/BasicContainers/CMakeLists.txt | 8 +++ .../HashTable/ProtocolGeneralizations.swift | 35 ++++++++-- .../BasicContainers/HashTable/_HTable.swift | 11 ++++ .../RigidDictionary+Equatable.swift | 65 +++++++++++++++++++ .../RigidDictionary+Hashable.swift | 41 ++++++++++++ .../RigidDictionary+Insertions.swift | 2 +- .../RigidDictionary+Removals.swift | 2 +- .../RigidDictionary+Resizing.swift | 4 +- .../RigidSet/RigidSet+Equatable.swift | 51 +++++++++++++++ .../RigidSet/RigidSet+Hashable.swift | 47 ++++++++++++++ .../RigidSet/RigidSet+Insertions.swift | 2 +- .../RigidSet/RigidSet+Removals.swift | 2 +- .../RigidSet/RigidSet+Resizing.swift | 4 +- .../BasicContainers/RigidSet/RigidSet.swift | 5 +- .../UniqueDictionary+Equatable.swift | 50 ++++++++++++++ .../UniqueDictionary+Hashable.swift | 28 ++++++++ .../UniqueSet/UniqueSet+Equatable.swift | 38 +++++++++++ .../UniqueSet/UniqueSet+Hashable.swift | 32 +++++++++ Xcode/Collections.xcodeproj/project.pbxproj | 32 +++++++++ 19 files changed, 445 insertions(+), 14 deletions(-) create mode 100644 Sources/BasicContainers/RigidDictionary/RigidDictionary+Equatable.swift create mode 100644 Sources/BasicContainers/RigidDictionary/RigidDictionary+Hashable.swift create mode 100644 Sources/BasicContainers/RigidSet/RigidSet+Equatable.swift create mode 100644 Sources/BasicContainers/RigidSet/RigidSet+Hashable.swift create mode 100644 Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Equatable.swift create mode 100644 Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Hashable.swift create mode 100644 Sources/BasicContainers/UniqueSet/UniqueSet+Equatable.swift create mode 100644 Sources/BasicContainers/UniqueSet/UniqueSet+Hashable.swift diff --git a/Sources/BasicContainers/CMakeLists.txt b/Sources/BasicContainers/CMakeLists.txt index c42c3007..d3c709c0 100644 --- a/Sources/BasicContainers/CMakeLists.txt +++ b/Sources/BasicContainers/CMakeLists.txt @@ -66,6 +66,8 @@ target_sources(${module_name} PRIVATE "RigidSet/RigidSet+BorrowingSequence.swift" "RigidSet/RigidSet+Consumption.swift" "RigidSet/RigidSet+Debugging.swift" + "RigidSet/RigidSet+Equatable.swift" + "RigidSet/RigidSet+Hashable.swift" "RigidSet/RigidSet+Initializers.swift" "RigidSet/RigidSet+Insertions.swift" "RigidSet/RigidSet+Lookup.swift" @@ -75,6 +77,8 @@ target_sources(${module_name} PRIVATE "UniqueSet/UniqueSet+BorrowingSequence.swift" "UniqueSet/UniqueSet+Consumption.swift" "UniqueSet/UniqueSet+Debugging.swift" + "UniqueSet/UniqueSet+Equatable.swift" + "UniqueSet/UniqueSet+Hashable.swift" "UniqueSet/UniqueSet+Initializers.swift" "UniqueSet/UniqueSet+Insertions.swift" "UniqueSet/UniqueSet+Lookup.swift" @@ -82,6 +86,8 @@ target_sources(${module_name} PRIVATE "UniqueSet/UniqueSet+Resizing.swift" "RigidDictionary/RigidDictionary.swift" "RigidDictionary/RigidDictionary+Consumption.swift" + "RigidDictionary/RigidDictionary+Equatable.swift" + "RigidDictionary/RigidDictionary+Hashable.swift" "RigidDictionary/RigidDictionary+Initializers.swift" "RigidDictionary/RigidDictionary+Insertions.swift" "RigidDictionary/RigidDictionary+Keys.swift" @@ -90,6 +96,8 @@ target_sources(${module_name} PRIVATE "RigidDictionary/RigidDictionary+Resizing.swift" "UniqueDictionary/UniqueDictionary.swift" "UniqueDictionary/UniqueDictionary+Consumption.swift" + "UniqueDictionary/UniqueDictionary+Equatable.swift" + "UniqueDictionary/UniqueDictionary+Hashable.swift" "UniqueDictionary/UniqueDictionary+Initializers.swift" "UniqueDictionary/UniqueDictionary+Insertions.swift" "UniqueDictionary/UniqueDictionary+Keys.swift" diff --git a/Sources/BasicContainers/HashTable/ProtocolGeneralizations.swift b/Sources/BasicContainers/HashTable/ProtocolGeneralizations.swift index 856f36cc..a7c818ee 100644 --- a/Sources/BasicContainers/HashTable/ProtocolGeneralizations.swift +++ b/Sources/BasicContainers/HashTable/ProtocolGeneralizations.swift @@ -46,10 +46,35 @@ public protocol GeneralizedHashable: GeneralizedEquatable, ~Copyable, ~Escapable /// of this instance. func hash(into hasher: inout Hasher) - // Raw top-level hashing interface. Some standard library types (mostly - // primitives) specialize this to eliminate small resiliency overheads. (This - // only matters for tiny keys.) - func _rawHashValue(seed: Int) -> Int + /// The temporary substitute to `Hashable._rawHashValue(seed:)`, the raw + /// top-level hashing interface. Some standard library types (mostly + /// primitives) specialize this to eliminate small resiliency overheads. (This + /// only matters for tiny keys.) + /// + /// We avoid using the stdlib name as the stdlib's default implementation is + /// overwhelmingly preferable but it isn't (easily) reimplementable outside + /// of the stdlib. + /// + /// This will be replaced by the actual `Hashable` method when its + /// generalization ships. + func _rawHashValue_temp(seed: Int) -> Int +} + +extension GeneralizedHashable where Self: ~Copyable & ~Escapable { + @inlinable + public func _rawHashValue_temp(seed: Int) -> Int { + var hasher = Hasher() + hasher.combine(seed) + self.hash(into: &hasher) + return hasher.finalize() + } +} + +extension GeneralizedHashable where Self: Hashable { + @inlinable + public func _rawHashValue_temp(seed: Int) -> Int { + _rawHashValue(seed: seed) + } } extension GeneralizedHashable where Self: ~Copyable & ~Escapable { @@ -60,7 +85,7 @@ extension GeneralizedHashable where Self: ~Copyable & ~Escapable { @_alwaysEmitIntoClient @inline(__always) public var hashValue: Int { - _rawHashValue(seed: 0) + _rawHashValue_temp(seed: 0) } } diff --git a/Sources/BasicContainers/HashTable/_HTable.swift b/Sources/BasicContainers/HashTable/_HTable.swift index 24ec42c9..54dab757 100644 --- a/Sources/BasicContainers/HashTable/_HTable.swift +++ b/Sources/BasicContainers/HashTable/_HTable.swift @@ -223,4 +223,15 @@ extension _HTable { self._maxProbeLength = 0 } } + +extension _HTable { + @usableFromInline + package func isTriviallyIdentical(to other: borrowing Self) -> Bool { + self._count == other._count + && self._capacity == other._capacity + && self._bitmap == other._bitmap + && self._maxProbeLength == other._maxProbeLength + && self.scale == other.scale + } +} #endif diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Equatable.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Equatable.swift new file mode 100644 index 00000000..80feb3ca --- /dev/null +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Equatable.swift @@ -0,0 +1,65 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +import ContainersPreview +#endif + + +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension RigidDictionary { + @inlinable + public func isTriviallyIdentical(to other: borrowing Self) -> Bool { + self._keys.isTriviallyIdentical(to: other._keys) + && self._values == other._values + } +} + +@available(SwiftStdlib 5.0, *) +extension RigidDictionary { + @inlinable + public func isEqual( + to other: borrowing Self, + by areEquivalent: (borrowing Value, borrowing Value) -> Bool + ) -> Bool { + if self.isTriviallyIdentical(to: other) { return true } + + guard self.count == other.count else { return false } + + var it = self._keys._table.makeBucketIterator() + while let next = it.nextOccupiedRegion() { + var lb = next.lowerBound + while lb < next.upperBound { + let res = other._find(self._keyPtr(at: lb).pointee) + guard let rb = res.bucket else { return false } + + let lp = self._valuePtr(at: lb) + let rp = other._valuePtr(at: rb) + guard areEquivalent(lp.pointee, rp.pointee) else { return false } + lb._offset &+= 1 + } + } + return true + } +} + +@available(SwiftStdlib 5.0, *) +extension RigidDictionary: GeneralizedEquatable where Value: GeneralizedEquatable { // Should be Equatable + @inlinable + public static func ==(left: borrowing Self, right: borrowing Self) -> Bool { + left.isEqual(to: right, by: ==) + } +} + +#endif diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Hashable.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Hashable.swift new file mode 100644 index 00000000..7fc8cf68 --- /dev/null +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Hashable.swift @@ -0,0 +1,41 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +import ContainersPreview +#endif + +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension RigidDictionary: GeneralizedHashable where Value: GeneralizedHashable { // Should be Hashable + @inlinable + public func hash(into hasher: inout Hasher) { + var commutativeHash = 0 + var it = self._keys._table.makeBucketIterator() + while let next = it.nextOccupiedRegion() { + var b = next.lowerBound + while b < next.upperBound { + // Note that we use a copy of our own hasher here. This makes hash values + // dependent on its state, eliminating static collision patterns. + var elementHasher = hasher + _keyPtr(at: b).pointee.hash(into: &elementHasher) + _valuePtr(at: b).pointee.hash(into: &elementHasher) + commutativeHash ^= elementHasher.finalize() + b._offset &+= 1 + } + } + hasher.combine(commutativeHash) + } +} + +#endif diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Insertions.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Insertions.swift index 405961f1..0df9d603 100644 --- a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Insertions.swift +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Insertions.swift @@ -54,7 +54,7 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { let bucket = self._keys._table.insertNew_Large( hashValue: hashValue, hashGenerator: { - keys[$0]._rawHashValue(seed: seed) + keys[$0]._rawHashValue_temp(seed: seed) }, swapper: { bucket in swap(&key, &keys[bucket]) diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Removals.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Removals.swift index 6cc8ff39..6a34b445 100644 --- a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Removals.swift +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Removals.swift @@ -32,7 +32,7 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { self._keys._table.resolveHole( at: bucket, hashGenerator: { - keys[$0.offset]._rawHashValue(seed: seed) + keys[$0.offset]._rawHashValue_temp(seed: seed) }, mover: { (keys + $1.offset).initialize(to: (keys + $0.offset).move()) diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Resizing.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Resizing.swift index e233e79b..52e3a3ac 100644 --- a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Resizing.swift +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Resizing.swift @@ -67,10 +67,10 @@ extension RigidDictionary where Key: ~Copyable, Value: ~Copyable { selector: { srcKey = sourceKeys + $0.offset srcValue = sourceValues + $0.offset - return srcKey.pointee._rawHashValue(seed: seed) + return srcKey.pointee._rawHashValue_temp(seed: seed) }, hashGenerator: { - targetKeys[$0.offset]._rawHashValue(seed: seed) + targetKeys[$0.offset]._rawHashValue_temp(seed: seed) }, swapper: { swap(&srcKey.pointee, &targetKeys[$0.offset]) diff --git a/Sources/BasicContainers/RigidSet/RigidSet+Equatable.swift b/Sources/BasicContainers/RigidSet/RigidSet+Equatable.swift new file mode 100644 index 00000000..7dca176e --- /dev/null +++ b/Sources/BasicContainers/RigidSet/RigidSet+Equatable.swift @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +import ContainersPreview +#endif + + +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension RigidSet { + @inlinable + public func isTriviallyIdentical(to other: borrowing Self) -> Bool { + self._members == other._members + && self._table.isTriviallyIdentical(to: other._table) + } +} + +@available(SwiftStdlib 5.0, *) +extension RigidSet: GeneralizedEquatable { // Should be Equatable + @inlinable + public static func ==(left: borrowing Self, right: borrowing Self) -> Bool { + if left.isTriviallyIdentical(to: right) { return true } + + guard left.count == right.count else { return false } + + var lit = left.makeBorrowingIterator() + while true { + let l = lit.nextSpan() + if l.isEmpty { break } + var i = 0 + while i < l.count { + guard right.contains(l[i]) else { return false } + i &+= 1 + } + } + return true + } +} + +#endif diff --git a/Sources/BasicContainers/RigidSet/RigidSet+Hashable.swift b/Sources/BasicContainers/RigidSet/RigidSet+Hashable.swift new file mode 100644 index 00000000..e2119b54 --- /dev/null +++ b/Sources/BasicContainers/RigidSet/RigidSet+Hashable.swift @@ -0,0 +1,47 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +import ContainersPreview +#endif + +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension RigidSet: GeneralizedHashable { // Should be Hashable + @inlinable + public func hash(into hasher: inout Hasher) { + // Generate a seed from a snapshot of the hasher. This makes members' hash + // values depend on the state of the hasher, which improves hashing + // quality. (E.g., it makes it possible to resolve collisions by passing in + // a different hasher.) + let copy = hasher + hasher.combine(_rawHashValue(seed: copy.finalize())) + } + + @inlinable + public func _rawHashValue(seed: Int) -> Int { + var hash = 0 + var it = self.makeBorrowingIterator() + while true { + let next = it.nextSpan() + var i = 0 + while i < next.count { + hash ^= next[i]._rawHashValue_temp(seed: seed) + i &+= 1 + } + } + return hash + } +} + +#endif diff --git a/Sources/BasicContainers/RigidSet/RigidSet+Insertions.swift b/Sources/BasicContainers/RigidSet/RigidSet+Insertions.swift index cda908df..0a2e1829 100644 --- a/Sources/BasicContainers/RigidSet/RigidSet+Insertions.swift +++ b/Sources/BasicContainers/RigidSet/RigidSet+Insertions.swift @@ -65,7 +65,7 @@ extension RigidSet where Element: ~Copyable { let bucket = _table.insertNew_Large( hashValue: hashValue, hashGenerator: { - storage[$0]._rawHashValue(seed: seed) + storage[$0]._rawHashValue_temp(seed: seed) }, swapper: { swap(&item, &storage[$0]) diff --git a/Sources/BasicContainers/RigidSet/RigidSet+Removals.swift b/Sources/BasicContainers/RigidSet/RigidSet+Removals.swift index 6c0a337a..f4d7297b 100644 --- a/Sources/BasicContainers/RigidSet/RigidSet+Removals.swift +++ b/Sources/BasicContainers/RigidSet/RigidSet+Removals.swift @@ -49,7 +49,7 @@ extension RigidSet where Element: ~Copyable { _table.resolveHole( at: bucket, hashGenerator: { - members[$0.offset]._rawHashValue(seed: seed) + members[$0.offset]._rawHashValue_temp(seed: seed) }, mover: { (members + $1.offset).initialize(to: (members + $0.offset).move()) diff --git a/Sources/BasicContainers/RigidSet/RigidSet+Resizing.swift b/Sources/BasicContainers/RigidSet/RigidSet+Resizing.swift index fce4c614..57ab7c35 100644 --- a/Sources/BasicContainers/RigidSet/RigidSet+Resizing.swift +++ b/Sources/BasicContainers/RigidSet/RigidSet+Resizing.swift @@ -64,10 +64,10 @@ extension RigidSet where Element: ~Copyable { from: &old._table, selector: { src = source + $0.offset - return src.pointee._rawHashValue(seed: seed) + return src.pointee._rawHashValue_temp(seed: seed) }, hashGenerator: { - target[$0.offset]._rawHashValue(seed: seed) + target[$0.offset]._rawHashValue_temp(seed: seed) }, swapper: { swap(&src.pointee, &target[$0.offset]) diff --git a/Sources/BasicContainers/RigidSet/RigidSet.swift b/Sources/BasicContainers/RigidSet/RigidSet.swift index db081d6f..ae916a76 100644 --- a/Sources/BasicContainers/RigidSet/RigidSet.swift +++ b/Sources/BasicContainers/RigidSet/RigidSet.swift @@ -14,7 +14,10 @@ import InternalCollectionsUtilities import ContainersPreview #endif + #if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS +/// A fixed-capacity, noncopyable, unordered hashed container of unique +/// elements. @available(SwiftStdlib 5.0, *) @frozen public struct RigidSet: ~Copyable { @@ -147,7 +150,7 @@ extension RigidSet where Element: ~Copyable { internal borrowing func _hashValue( for item: borrowing Element ) -> Int { - item._rawHashValue(seed: _seed) + item._rawHashValue_temp(seed: _seed) } } diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Equatable.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Equatable.swift new file mode 100644 index 00000000..eeb82643 --- /dev/null +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Equatable.swift @@ -0,0 +1,50 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +import ContainersPreview +#endif + + +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension UniqueDictionary { + @inlinable + @inline(__always) + public func isTriviallyIdentical(to other: borrowing Self) -> Bool { + self._storage.isTriviallyIdentical(to: other._storage) + } +} + +@available(SwiftStdlib 5.0, *) +extension UniqueDictionary { + @inlinable + @inline(__always) + public func isEqual( + to other: borrowing Self, + by areEquivalent: (borrowing Value, borrowing Value) -> Bool + ) -> Bool { + self._storage.isEqual(to: other._storage, by: areEquivalent) + } +} + +@available(SwiftStdlib 5.0, *) +extension UniqueDictionary: GeneralizedEquatable where Value: GeneralizedEquatable { // Should be Equatable + @inlinable + @inline(__always) + public static func ==(left: borrowing Self, right: borrowing Self) -> Bool { + left.isEqual(to: right, by: ==) + } +} + +#endif diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Hashable.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Hashable.swift new file mode 100644 index 00000000..ff98b4b4 --- /dev/null +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Hashable.swift @@ -0,0 +1,28 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +import ContainersPreview +#endif + +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension UniqueDictionary: GeneralizedHashable where Value: GeneralizedHashable { // Should be Hashable + @inlinable + @inline(__always) + public func hash(into hasher: inout Hasher) { + self._storage.hash(into: &hasher) + } +} + +#endif diff --git a/Sources/BasicContainers/UniqueSet/UniqueSet+Equatable.swift b/Sources/BasicContainers/UniqueSet/UniqueSet+Equatable.swift new file mode 100644 index 00000000..ba8bc592 --- /dev/null +++ b/Sources/BasicContainers/UniqueSet/UniqueSet+Equatable.swift @@ -0,0 +1,38 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +import ContainersPreview +#endif + + +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension UniqueSet { + @inlinable + @inline(__always) + public func isTriviallyIdentical(to other: borrowing Self) -> Bool { + self._storage.isTriviallyIdentical(to: other._storage) + } +} + +@available(SwiftStdlib 5.0, *) +extension UniqueSet: GeneralizedEquatable { // Should be Equatable + @inlinable + @inline(__always) + public static func ==(left: borrowing Self, right: borrowing Self) -> Bool { + left._storage == right._storage + } +} + +#endif diff --git a/Sources/BasicContainers/UniqueSet/UniqueSet+Hashable.swift b/Sources/BasicContainers/UniqueSet/UniqueSet+Hashable.swift new file mode 100644 index 00000000..f30389f3 --- /dev/null +++ b/Sources/BasicContainers/UniqueSet/UniqueSet+Hashable.swift @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2026 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 +// +//===----------------------------------------------------------------------===// + +#if !COLLECTIONS_SINGLE_MODULE +import InternalCollectionsUtilities +import ContainersPreview +#endif + +#if compiler(>=6.3) && COLLECTIONS_UNSTABLE_NONCOPYABLE_KEYS + +@available(SwiftStdlib 5.0, *) +extension UniqueSet: GeneralizedHashable { // Should be Hashable + @inlinable + public func hash(into hasher: inout Hasher) { + _storage.hash(into: &hasher) + } + + @inlinable + public func _rawHashValue(seed: Int) -> Int { + _storage._rawHashValue(seed: seed) + } +} + +#endif diff --git a/Xcode/Collections.xcodeproj/project.pbxproj b/Xcode/Collections.xcodeproj/project.pbxproj index 550d0e27..5800b969 100644 --- a/Xcode/Collections.xcodeproj/project.pbxproj +++ b/Xcode/Collections.xcodeproj/project.pbxproj @@ -102,6 +102,7 @@ 7D479CF92F4D07F200A8CB59 /* RigidDictionary+Removals.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CF72F4D07F200A8CB59 /* RigidDictionary+Removals.swift */; }; 7D479CFC2F4D080500A8CB59 /* UniqueDictionary+Removals.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CFB2F4D080500A8CB59 /* UniqueDictionary+Removals.swift */; }; 7D479CFD2F4D080500A8CB59 /* UniqueDictionary+Keys.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CFA2F4D080500A8CB59 /* UniqueDictionary+Keys.swift */; }; + 7D479CFF2F4E487C00A8CB59 /* RigidSet+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D479CFE2F4E487700A8CB59 /* RigidSet+Equatable.swift */; }; 7D48BCDC2E51554900D7F6F7 /* ClassBox.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCDB2E51554900D7F6F7 /* ClassBox.swift */; }; 7D48BCDF2E51558700D7F6F7 /* TestPrintable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCDD2E51558700D7F6F7 /* TestPrintable.swift */; }; 7D48BCE02E51558700D7F6F7 /* LifetimeTrackedStruct.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7D48BCDE2E51558700D7F6F7 /* LifetimeTrackedStruct.swift */; }; @@ -502,6 +503,13 @@ 7DE9221C29CA8576004483EB /* RangeReplaceableCollectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE921F729CA8576004483EB /* RangeReplaceableCollectionTests.swift */; }; 7DE9221D29CA8576004483EB /* DequeTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE921F829CA8576004483EB /* DequeTests.swift */; }; 7DE9221E29CA8576004483EB /* MutableCollectionTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DE921F929CA8576004483EB /* MutableCollectionTests.swift */; }; + 7DEB31522F4E720900F9AF28 /* RigidSet+Hashable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB31512F4E720500F9AF28 /* RigidSet+Hashable.swift */; }; + 7DEB31542F4E75B500F9AF28 /* UniqueSet+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB31532F4E75AF00F9AF28 /* UniqueSet+Equatable.swift */; }; + 7DEB31562F4E75BC00F9AF28 /* UniqueSet+Hashable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB31552F4E75B800F9AF28 /* UniqueSet+Hashable.swift */; }; + 7DEB31582F4E762200F9AF28 /* RigidDictionary+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB31572F4E761D00F9AF28 /* RigidDictionary+Equatable.swift */; }; + 7DEB315A2F4E78F800F9AF28 /* RigidDictionary+Hashable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB31592F4E78F200F9AF28 /* RigidDictionary+Hashable.swift */; }; + 7DEB315C2F4E7A2500F9AF28 /* UniqueDictionary+Equatable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB315B2F4E7A2000F9AF28 /* UniqueDictionary+Equatable.swift */; }; + 7DEB315E2F4E7B8700F9AF28 /* UniqueDictionary+Hashable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEB315D2F4E7B8200F9AF28 /* UniqueDictionary+Hashable.swift */; }; 7DEBDB6E29CCE44A00ADC226 /* SortedCollectionAPIChecker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEBDB3029CCE43600ADC226 /* SortedCollectionAPIChecker.swift */; }; 7DEBDB6F29CCE44A00ADC226 /* MinimalMutableRangeReplaceableRandomAccessCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEBDB3F29CCE43600ADC226 /* MinimalMutableRangeReplaceableRandomAccessCollection.swift */; }; 7DEBDB7029CCE44A00ADC226 /* ResettableValue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7DEBDB4229CCE43600ADC226 /* ResettableValue.swift */; }; @@ -653,6 +661,7 @@ 7D479CF72F4D07F200A8CB59 /* RigidDictionary+Removals.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidDictionary+Removals.swift"; sourceTree = ""; }; 7D479CFA2F4D080500A8CB59 /* UniqueDictionary+Keys.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Keys.swift"; sourceTree = ""; }; 7D479CFB2F4D080500A8CB59 /* UniqueDictionary+Removals.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Removals.swift"; sourceTree = ""; }; + 7D479CFE2F4E487700A8CB59 /* RigidSet+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidSet+Equatable.swift"; sourceTree = ""; }; 7D48BCC92E51524200D7F6F7 /* CMakeLists.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = CMakeLists.txt; sourceTree = ""; }; 7D48BCDB2E51554900D7F6F7 /* ClassBox.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ClassBox.swift; sourceTree = ""; }; 7D48BCDD2E51558700D7F6F7 /* TestPrintable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestPrintable.swift; sourceTree = ""; }; @@ -1090,6 +1099,13 @@ 7DE921F729CA8576004483EB /* RangeReplaceableCollectionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RangeReplaceableCollectionTests.swift; sourceTree = ""; }; 7DE921F829CA8576004483EB /* DequeTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DequeTests.swift; sourceTree = ""; }; 7DE921F929CA8576004483EB /* MutableCollectionTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MutableCollectionTests.swift; sourceTree = ""; }; + 7DEB31512F4E720500F9AF28 /* RigidSet+Hashable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidSet+Hashable.swift"; sourceTree = ""; }; + 7DEB31532F4E75AF00F9AF28 /* UniqueSet+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueSet+Equatable.swift"; sourceTree = ""; }; + 7DEB31552F4E75B800F9AF28 /* UniqueSet+Hashable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueSet+Hashable.swift"; sourceTree = ""; }; + 7DEB31572F4E761D00F9AF28 /* RigidDictionary+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidDictionary+Equatable.swift"; sourceTree = ""; }; + 7DEB31592F4E78F200F9AF28 /* RigidDictionary+Hashable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "RigidDictionary+Hashable.swift"; sourceTree = ""; }; + 7DEB315B2F4E7A2000F9AF28 /* UniqueDictionary+Equatable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Equatable.swift"; sourceTree = ""; }; + 7DEB315D2F4E7B8200F9AF28 /* UniqueDictionary+Hashable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UniqueDictionary+Hashable.swift"; sourceTree = ""; }; 7DEBDB2129CCE43600ADC226 /* Combinatorics.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Combinatorics.swift; sourceTree = ""; }; 7DEBDB2229CCE43600ADC226 /* TestContext.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestContext.swift; sourceTree = ""; }; 7DEBDB2329CCE43600ADC226 /* Assertions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Assertions.swift; sourceTree = ""; }; @@ -1264,6 +1280,8 @@ 7D479CCC2F46D23400A8CB59 /* UniqueSet+BorrowingSequence.swift */, 7D479CC42F46D20400A8CB59 /* UniqueSet+Consumption.swift */, 7D479CBA2F46A71500A8CB59 /* UniqueSet+Debugging.swift */, + 7DEB31532F4E75AF00F9AF28 /* UniqueSet+Equatable.swift */, + 7DEB31552F4E75B800F9AF28 /* UniqueSet+Hashable.swift */, 7D479CD32F46DD9600A8CB59 /* UniqueSet+Initializers.swift */, 7D479CC82F46D21800A8CB59 /* UniqueSet+Insertions.swift */, 7D479CCA2F46D22B00A8CB59 /* UniqueSet+Lookup.swift */, @@ -1278,6 +1296,8 @@ children = ( 7D479CD52F47CB4600A8CB59 /* UniqueDictionary.swift */, 7D479CEE2F47F4E300A8CB59 /* UniqueDictionary+Consumption.swift */, + 7DEB315B2F4E7A2000F9AF28 /* UniqueDictionary+Equatable.swift */, + 7DEB315D2F4E7B8200F9AF28 /* UniqueDictionary+Hashable.swift */, 7D479CDA2F47CD8900A8CB59 /* UniqueDictionary+Initializers.swift */, 7D479CE22F47D63300A8CB59 /* UniqueDictionary+Insertions.swift */, 7D479CFA2F4D080500A8CB59 /* UniqueDictionary+Keys.swift */, @@ -1334,6 +1354,8 @@ children = ( 7D7246832F3D9DDB00112FD0 /* RigidDictionary.swift */, 7D479CEC2F47F40700A8CB59 /* RigidDictionary+Consumption.swift */, + 7DEB31572F4E761D00F9AF28 /* RigidDictionary+Equatable.swift */, + 7DEB31592F4E78F200F9AF28 /* RigidDictionary+Hashable.swift */, 7D479CD82F47CBB400A8CB59 /* RigidDictionary+Initializers.swift */, 7D479CDE2F47D5C300A8CB59 /* RigidDictionary+Insertions.swift */, 7D479CF62F4D07F200A8CB59 /* RigidDictionary+Keys.swift */, @@ -1351,6 +1373,8 @@ 7D479CAF2F450E3F00A8CB59 /* RigidSet+BorrowingSequence.swift */, 7D479CC02F46D14800A8CB59 /* RigidSet+Consumption.swift */, 7D479CB82F46A6D700A8CB59 /* RigidSet+Debugging.swift */, + 7D479CFE2F4E487700A8CB59 /* RigidSet+Equatable.swift */, + 7DEB31512F4E720500F9AF28 /* RigidSet+Hashable.swift */, 7D479CD12F46DD8600A8CB59 /* RigidSet+Initializers.swift */, 7D479CBE2F46D09E00A8CB59 /* RigidSet+Insertions.swift */, 7D479CBC2F46D05000A8CB59 /* RigidSet+Lookup.swift */, @@ -2483,6 +2507,7 @@ 7D479CC92F46D22100A8CB59 /* UniqueSet+Insertions.swift in Sources */, 7D2A779C2F27F2BF005780C7 /* Deque+ExpressibleByArrayLiteral.swift in Sources */, 7DE9213B29CA70F4004483EB /* TreeSet+Hashable.swift in Sources */, + 7DEB31562F4E75BC00F9AF28 /* UniqueSet+Hashable.swift in Sources */, 7DE9200D29CA70F3004483EB /* OrderedDictionary+Equatable.swift in Sources */, 7DE9207C29CA70F4004483EB /* BigSubstring.swift in Sources */, 7DE9205F29CA70F4004483EB /* BigString+Index.swift in Sources */, @@ -2501,6 +2526,7 @@ 7D479CFD2F4D080500A8CB59 /* UniqueDictionary+Keys.swift in Sources */, 7DE9208A29CA70F4004483EB /* BigString+Comparable.swift in Sources */, 7DE9213629CA70F4004483EB /* TreeSet+SetAlgebra basics.swift in Sources */, + 7D479CFF2F4E487C00A8CB59 /* RigidSet+Equatable.swift in Sources */, 7DE9202C29CA70F3004483EB /* OrderedSet+Initializers.swift in Sources */, 7DE9213229CA70F4004483EB /* TreeSet+SetAlgebra formSymmetricDifference.swift in Sources */, 7DE9203429CA70F3004483EB /* OrderedSet+Partial SetAlgebra formUnion.swift in Sources */, @@ -2540,6 +2566,7 @@ 7DE9216029CA70F4004483EB /* _HashNode+Structural isDisjoint.swift in Sources */, 7D479CF32F47FB4100A8CB59 /* _HTable+Removal.swift in Sources */, 7DE9203529CA70F3004483EB /* OrderedSet+CustomReflectable.swift in Sources */, + 7DEB31582F4E762200F9AF28 /* RigidDictionary+Equatable.swift in Sources */, 7DE920C929CA70F4004483EB /* BitSet+BidirectionalCollection.swift in Sources */, 7DE920ED29CA70F4004483EB /* BitArray+Extras.swift in Sources */, 7DE9206829CA70F4004483EB /* BigString+Chunk+Append and Insert.swift in Sources */, @@ -2629,13 +2656,16 @@ 7D479CD22F46DD8600A8CB59 /* RigidSet+Initializers.swift in Sources */, 7DE920E129CA70F4004483EB /* UInt+Tricks.swift in Sources */, 7D479CDD2F47D59C00A8CB59 /* RigidDictionary+Lookup.swift in Sources */, + 7DEB31522F4E720900F9AF28 /* RigidSet+Hashable.swift in Sources */, 7DE920EB29CA70F4004483EB /* BitArray+Fill.swift in Sources */, 7DE9201929CA70F3004483EB /* OrderedDictionary+Values.swift in Sources */, 7DE9213929CA70F4004483EB /* TreeSet+SetAlgebra Initializers.swift in Sources */, + 7DEB315E2F4E7B8700F9AF28 /* UniqueDictionary+Hashable.swift in Sources */, 7DE9217729CA70F4004483EB /* TreeDictionary+Keys.swift in Sources */, 7D57A2BA2F340B58002543BD /* UniqueArray+Consumption.swift in Sources */, 7DE920DA29CA70F4004483EB /* BitSet._UnsafeHandle.swift in Sources */, 7DE920D129CA70F4004483EB /* BitSet.Index.swift in Sources */, + 7DEB315A2F4E78F800F9AF28 /* RigidDictionary+Hashable.swift in Sources */, 7DAD899E2E8B09C300DD5786 /* UInt+first and last set bit.swift in Sources */, 7DAD899F2E8B09C300DD5786 /* UInt+reversed.swift in Sources */, 7DAD89A02E8B09C300DD5786 /* FixedWidthInteger+roundUpToPowerOfTwo.swift in Sources */, @@ -2760,6 +2790,7 @@ 7DE9216429CA70F4004483EB /* _HashNode.swift in Sources */, 7DAD89A52E8B09D900DD5786 /* _UnsafeBitSet+_Word.swift in Sources */, 7DAD89A62E8B09D900DD5786 /* _UnsafeBitSet+Index.swift in Sources */, + 7DEB31542F4E75B500F9AF28 /* UniqueSet+Equatable.swift in Sources */, 7DAD89A72E8B09D900DD5786 /* _UnsafeBitSet.swift in Sources */, 7DE9201E29CA70F3004483EB /* OrderedSet+Partial SetAlgebra symmetricDifference.swift in Sources */, 7DE9207A29CA70F4004483EB /* BigString+UTF8View.swift in Sources */, @@ -2816,6 +2847,7 @@ 7DE9215A29CA70F4004483EB /* _HashNode+Structural subtracting.swift in Sources */, 7DE9217E29CA70F4004483EB /* Heap+ExpressibleByArrayLiteral.swift in Sources */, 7DE448452F2D3705005853A7 /* BinaryInteger extensions.swift in Sources */, + 7DEB315C2F4E7A2500F9AF28 /* UniqueDictionary+Equatable.swift in Sources */, 7DA23F5A2F32C68300E4C03A /* RigidArray+Hashable.swift in Sources */, 7DE9204029CA70F3004483EB /* OrderedSet+Partial SetAlgebra isStrictSuperset.swift in Sources */, 7DE920BF29CA70F4004483EB /* BitSet+Codable.swift in Sources */,