mirror of
https://github.com/apple/swift-collections
synced 2026-11-30 05:18:44 +00:00
[Capsule] Implement Equatable protocol
This commit is contained in:
1 parent
8e2ec3c49b
commit
b34c8dad97
3 files changed
+72
-23
No files matched your search
@@ -248,6 +248,11 @@ final class BitmapIndexedMapNode<Key, Value> : MapNode where Key : Hashable {
|
||||
content[content.count - 1 - index] as! HashCollisionMapNode<Key, Value>
|
||||
}
|
||||
|
||||
// TODO rework temporarily duplicated methods for type-safe access (requires changing protocol)
|
||||
func getAnyNode(_ index: Int) -> Any {
|
||||
content[content.count - 1 - index]
|
||||
}
|
||||
|
||||
var hasPayload: Bool { dataMap != 0 }
|
||||
|
||||
var payloadArity: Int { dataMap.nonzeroBitCount }
|
||||
@@ -346,15 +351,40 @@ final class BitmapIndexedMapNode<Key, Value> : MapNode where Key : Hashable {
|
||||
}
|
||||
}
|
||||
|
||||
extension BitmapIndexedMapNode /* : Equatable where Value : Equatable */ {
|
||||
extension BitmapIndexedMapNode : Equatable where Value : Equatable {
|
||||
static func == (lhs: BitmapIndexedMapNode<Key, Value>, rhs: BitmapIndexedMapNode<Key, Value>) -> Bool {
|
||||
lhs === rhs ||
|
||||
lhs.nodeMap == rhs.nodeMap &&
|
||||
lhs.dataMap == rhs.dataMap &&
|
||||
deepContentEquality(lhs.content, rhs.content, lhs.content.count)
|
||||
deepContentEquality(lhs, rhs)
|
||||
}
|
||||
|
||||
private static func deepContentEquality(_ a1: [Any], _ a2: [Any], _ length: Int) -> Bool {
|
||||
preconditionFailure("Not yet implemented")
|
||||
private static func deepContentEquality(_ lhs: BitmapIndexedMapNode<Key, Value>, _ rhs: BitmapIndexedMapNode<Key, Value>) -> Bool {
|
||||
for index in 0..<lhs.payloadArity {
|
||||
if (lhs.getPayload(index) != rhs.getPayload(index)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
/// `==` has no context on how deep the current node is located in the trie. Thus it would be beneficial making it explict
|
||||
/// how many regular and hash-collision nodes are stored on the current level.
|
||||
|
||||
for index in 0..<lhs.nodeArity {
|
||||
if let lhsNode = lhs.getAnyNode(index) as? BitmapIndexedMapNode<Key, Value>,
|
||||
let rhsNode = rhs.getAnyNode(index) as? BitmapIndexedMapNode<Key, Value> {
|
||||
if (lhsNode != rhsNode) {
|
||||
return false
|
||||
}
|
||||
} else if let lhsNode = lhs.getAnyNode(index) as? HashCollisionMapNode<Key, Value>,
|
||||
let rhsNode = rhs.getAnyNode(index) as? HashCollisionMapNode<Key, Value> {
|
||||
if (lhsNode != rhsNode) {
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -87,8 +87,8 @@ final class HashCollisionMapNode<Key, Value> : MapNode where Key : Hashable {
|
||||
func getPayload(_ index: Int) -> (Key, Value) { content[index] }
|
||||
}
|
||||
|
||||
extension HashCollisionMapNode /* : Equatable where Value : Equatable */ {
|
||||
extension HashCollisionMapNode : Equatable where Value : Equatable {
|
||||
static func == (lhs: HashCollisionMapNode<Key, Value>, rhs: HashCollisionMapNode<Key, Value>) -> Bool {
|
||||
preconditionFailure("Not yet implemented")
|
||||
Dictionary.init(uniqueKeysWithValues: lhs.content) == Dictionary.init(uniqueKeysWithValues: rhs.content)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user