forked from apple/swift-collections
/
_Node+Equatable.swift
50 lines (46 loc) · 1.66 KB
/
_Node+Equatable.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//===----------------------------------------------------------------------===//
//
// 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 == (lhs: _Node, rhs: _Node) -> Bool {
if lhs.raw.storage === rhs.raw.storage { return true }
if lhs.isCollisionNode && rhs.isCollisionNode {
return lhs.read { lhs in
rhs.read { rhs in
let l = Dictionary(
uniqueKeysWithValues: lhs.reverseItems.lazy.map { ($0.key, $0.value) })
let r = Dictionary(
uniqueKeysWithValues: rhs.reverseItems.lazy.map { ($0.key, $0.value) })
return l == r
}
}
}
return deepContentEquality(lhs, rhs)
}
@inlinable
internal static func deepContentEquality(
_ left: _Node,
_ right: _Node
) -> Bool {
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
}
}
}
}