diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Hashable.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Hashable.swift index 12045608..30ed7048 100644 --- a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Hashable.swift +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Hashable.swift @@ -18,11 +18,11 @@ import InternalCollectionsUtilities #if compiler(>=6.4) && UnstableHashedContainers @available(SwiftStdlib 6.4, *) -extension RigidDictionary: Hashable where Value: Hashable & ~Copyable { +extension RigidDictionary: Hashable where Key: ~Copyable, Value: Hashable & ~Copyable { } @available(SwiftStdlib 5.0, *) -extension RigidDictionary where Value: Hashable & ~Copyable { +extension RigidDictionary where Key: ~Copyable, Value: Hashable & ~Copyable { @inlinable public func hash(into hasher: inout Hasher) { var commutativeHash = 0 diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Equatable.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Equatable.swift index 7f0f0beb..6ae2ebb5 100644 --- a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Equatable.swift +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Equatable.swift @@ -18,7 +18,7 @@ import InternalCollectionsUtilities #if compiler(>=6.4) && UnstableHashedContainers @available(SwiftStdlib 5.0, *) -extension UniqueDictionary { +extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable { @inlinable @inline(__always) public func isTriviallyIdentical(to other: borrowing Self) -> Bool { @@ -27,7 +27,7 @@ extension UniqueDictionary { } @available(SwiftStdlib 5.0, *) -extension UniqueDictionary { +extension UniqueDictionary where Key: ~Copyable, Value: ~Copyable { @inlinable @inline(__always) public func isEqual( @@ -38,8 +38,11 @@ extension UniqueDictionary { } } +@available(SwiftStdlib 6.4, *) +extension UniqueDictionary: Equatable where Key: ~Copyable, Value: Equatable & ~Copyable {} + @available(SwiftStdlib 5.0, *) -extension UniqueDictionary: Equatable where Value: Equatable { +extension UniqueDictionary where Key: ~Copyable, Value: Equatable & ~Copyable { @inlinable @inline(__always) public static func ==(left: borrowing Self, right: borrowing Self) -> Bool { diff --git a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Hashable.swift b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Hashable.swift index 025f43de..cd76dea1 100644 --- a/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Hashable.swift +++ b/Sources/BasicContainers/UniqueDictionary/UniqueDictionary+Hashable.swift @@ -17,8 +17,11 @@ import InternalCollectionsUtilities #if compiler(>=6.4) && UnstableHashedContainers +@available(SwiftStdlib 6.4, *) +extension UniqueDictionary: Hashable where Key: ~Copyable, Value: Hashable & ~Copyable {} + @available(SwiftStdlib 5.0, *) -extension UniqueDictionary: Hashable where Value: Hashable { +extension UniqueDictionary where Key: ~Copyable, Value: Hashable & ~Copyable { @inlinable @inline(__always) public func hash(into hasher: inout Hasher) { diff --git a/Tests/BasicContainersTests/RigidDictionaryTests.swift b/Tests/BasicContainersTests/RigidDictionaryTests.swift index d22a3131..50805a36 100644 --- a/Tests/BasicContainersTests/RigidDictionaryTests.swift +++ b/Tests/BasicContainersTests/RigidDictionaryTests.swift @@ -531,6 +531,54 @@ class RigidDictionaryTests: CollectionTestCase { } } } + + func test_hashable() { + typealias Key = LifetimeTrackedStruct + typealias Value = LifetimeTrackedStruct + let contents: [[Int: Int]] = [ + [:], [0: 0], [1: 0], [0: 1], [0: 0, 1: 1], [0: 1, 1: 0], [0: 0, 1: 1, 2: 2], + Dictionary(uniqueKeysWithValues: (0 ..< 50).map { ($0, 2 * $0) }), + ] + withEvery("i", in: contents.indices) { i in + withEvery("j", in: contents.indices) { j in + withLifetimeTracking { tracker in + var a = RigidDictionary(capacity: 100) + for (key, value) in contents[i].sorted(by: { $0.key < $1.key }) { + a.insertValue( + tracker.structInstance(for: value), + forKey: tracker.structInstance(for: key)) + } + var b = RigidDictionary(capacity: contents[j].count) + for (key, value) in contents[j].sorted(by: { $0.key > $1.key }) { + b.insertValue( + tracker.structInstance(for: value), + forKey: tracker.structInstance(for: key)) + } + // FIXME: Use checkHashable when possible + var goodHash = false + for seed in 0 ..< 10 { + var ha = Hasher() + ha.combine(seed) + a.hash(into: &ha) + let hashValueA = ha.finalize() + + var hb = Hasher() + hb.combine(seed) + b.hash(into: &hb) + let hashValueB = hb.finalize() + + if contents[i] == contents[j] { + expectEqual(hashValueA, hashValueB) + goodHash = true + } else if hashValueA != hashValueB { + goodHash = true + } + } + expectTrue(goodHash, "Hashing does not differentiate between inequal containers") + } + } + } + } } #endif diff --git a/Tests/BasicContainersTests/UniqueDictionaryTests.swift b/Tests/BasicContainersTests/UniqueDictionaryTests.swift index aba31e87..f4f4aff4 100644 --- a/Tests/BasicContainersTests/UniqueDictionaryTests.swift +++ b/Tests/BasicContainersTests/UniqueDictionaryTests.swift @@ -404,6 +404,86 @@ class UniqueDictionaryTests: CollectionTestCase { } } + func test_equatable() { + typealias Key = LifetimeTrackedStruct + typealias Value = LifetimeTrackedStruct + let contents: [[Int: Int]] = [ + [:], [0: 0], [1: 0], [0: 1], [0: 0, 1: 1], [0: 1, 1: 0], [0: 0, 1: 1, 2: 2], + Dictionary(uniqueKeysWithValues: (0 ..< 50).map { ($0, 2 * $0) }), + ] + withEvery("i", in: contents.indices) { i in + withEvery("j", in: contents.indices) { j in + withLifetimeTracking { tracker in + var a = UniqueDictionary() + for (key, value) in contents[i].sorted(by: { $0.key < $1.key }) { + a.insertValue( + tracker.structInstance(for: value), + forKey: tracker.structInstance(for: key)) + } + var b = UniqueDictionary(minimumCapacity: 100) + for (key, value) in contents[j].sorted(by: { $0.key > $1.key }) { + b.insertValue( + tracker.structInstance(for: value), + forKey: tracker.structInstance(for: key)) + } + expectTrue(a.isTriviallyIdentical(to: a)) + expectFalse(a.isTriviallyIdentical(to: b)) + expectEqual(a == b, contents[i] == contents[j]) + expectEqual( + a.isEqual(to: b, by: { $0.payload == $1.payload }), + contents[i] == contents[j]) + } + } + } + } + + func test_hashable() { + typealias Key = LifetimeTrackedStruct + typealias Value = LifetimeTrackedStruct + let contents: [[Int: Int]] = [ + [:], [0: 0], [1: 0], [0: 1], [0: 0, 1: 1], [0: 1, 1: 0], [0: 0, 1: 1, 2: 2], + Dictionary(uniqueKeysWithValues: (0 ..< 50).map { ($0, 2 * $0) }), + ] + withEvery("i", in: contents.indices) { i in + withEvery("j", in: contents.indices) { j in + withLifetimeTracking { tracker in + var a = UniqueDictionary() + for (key, value) in contents[i].sorted(by: { $0.key < $1.key }) { + a.insertValue( + tracker.structInstance(for: value), + forKey: tracker.structInstance(for: key)) + } + var b = UniqueDictionary(minimumCapacity: 100) + for (key, value) in contents[j].sorted(by: { $0.key > $1.key }) { + b.insertValue( + tracker.structInstance(for: value), + forKey: tracker.structInstance(for: key)) + } + // FIXME: Use checkHashable when possible + var goodHash = false + for seed in 0 ..< 10 { + var ha = Hasher() + ha.combine(seed) + a.hash(into: &ha) + let hashValueA = ha.finalize() + + var hb = Hasher() + hb.combine(seed) + b.hash(into: &hb) + let hashValueB = hb.finalize() + + if contents[i] == contents[j] { + expectEqual(hashValueA, hashValueB) + goodHash = true + } else if hashValueA != hashValueB { + goodHash = true + } + } + expectTrue(goodHash, "Hashing does not differentiate between inequal containers") + } + } + } + } } #endif