mirror of
https://github.com/apple/swift-collections
synced 2026-11-24 04:27:53 +00:00
If the key implements hashing properly, then this would be a clever way to try another hash seed to differentiate between colliding keys. However, if Key has proper hashing, then collision nodes are expected to be tiny, so it’s better to simply do naive comparisons. If the Key type has broken (i.e., partial) hashing, then loading colliding items into a dictionary would just make == even slower. So we’re better off comparing collidingh items via nested loops — so let’s just unconditionally do that.
51 lines
1.7 KiB
Swift
51 lines
1.7 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Collections open source project
|
|
//
|
|
// Copyright (c) 2022 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TODO: `Equatable` needs more test coverage, apart from hash-collision smoke test
|
|
extension _Node: Equatable where Value: Equatable {
|
|
@inlinable
|
|
static func == (left: _Node, right: _Node) -> Bool {
|
|
if left.raw.storage === right.raw.storage { return true }
|
|
|
|
guard left.count == right.count else { return false }
|
|
|
|
if left.isCollisionNode {
|
|
guard right.isCollisionNode else { return false }
|
|
return left.read { lhs in
|
|
right.read { rhs in
|
|
let l = lhs.reverseItems
|
|
let r = rhs.reverseItems
|
|
guard l.count == r.count else { return false }
|
|
for i in l.indices {
|
|
guard r.contains(where: { $0 == l[i] }) else { return false }
|
|
}
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
guard !right.isCollisionNode else { return false }
|
|
|
|
guard left.count == right.count else { return false }
|
|
return left.read { l in
|
|
right.read { r in
|
|
guard l.itemMap == r.itemMap else { return false }
|
|
guard l.childMap == r.childMap else { return false }
|
|
|
|
guard l.reverseItems.elementsEqual(r.reverseItems, by: { $0 == $1 })
|
|
else { return false }
|
|
|
|
guard l._children.elementsEqual(r._children) else { return false }
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|