[BasicContainers] Hashed containers: Add Equatable/Hashable conformances and related methods

This commit is contained in:
Karoy Lorentey committed 2026-02-24 16:40:45 -08:00
1 parent 0e12c9e748
commit 0538000ffd
19 files changed
+445 -14

No files matched your search

+8
View File
@@ -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"
@@ -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)
}
}
@@ -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
@@ -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
@@ -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
@@ -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])
@@ -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())
@@ -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])
@@ -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
@@ -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
@@ -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])
@@ -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())
@@ -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])
@@ -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<Element: GeneralizedHashable & ~Copyable>: ~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)
}
}
@@ -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
@@ -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
@@ -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
@@ -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