mirror of
https://github.com/apple/swift-collections
synced 2026-11-26 04:47:48 +00:00
Merge pull request #827 from unaltolga/fix-unique-dictionary-noncopyable-conformances
[BasicContainers] Make UniqueDictionary Equatable and Hashable with noncopyable contents
This commit is contained in:
5 files changed
+140
-6
No files matched your search
@@ -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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -531,6 +531,54 @@ class RigidDictionaryTests: CollectionTestCase {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_hashable() {
|
||||
typealias Key = LifetimeTrackedStruct<Int>
|
||||
typealias Value = LifetimeTrackedStruct<Int>
|
||||
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<Key, Value>(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<Key, Value>(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
|
||||
@@ -404,6 +404,86 @@ class UniqueDictionaryTests: CollectionTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_equatable() {
|
||||
typealias Key = LifetimeTrackedStruct<Int>
|
||||
typealias Value = LifetimeTrackedStruct<Int>
|
||||
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<Key, Value>()
|
||||
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<Key, Value>(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<Int>
|
||||
typealias Value = LifetimeTrackedStruct<Int>
|
||||
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<Key, Value>()
|
||||
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<Key, Value>(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
|
||||
Reference in new issue
Block a user