[Capsule] Implement Equatable protocol

This commit is contained in:
Michael Steindorfer committed 2022-08-09 17:20:28 +02:00
1 parent 8e2ec3c49b
commit b34c8dad97
3 files changed
+72 -23

No files matched your search

+34 -4
View File
@@ -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
}
}
+2 -2
View File
@@ -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)
}
}
+36 -17
View File
@@ -82,38 +82,43 @@ final class CapsuleSmokeTests: CollectionTestCase {
res1[CollidableInt(11, 1)] = CollidableInt(11, 1)
res1[CollidableInt(12, 1)] = CollidableInt(12, 1)
expectEqual(res1.count, 2)
expectTrue(res1.contains(CollidableInt(11, 1)))
expectTrue(res1.contains(CollidableInt(12, 1)))
expectEqual(res1.count, 2)
expectEqual(HashMap.init([CollidableInt(11, 1) : CollidableInt(11, 1), CollidableInt(12, 1) : CollidableInt(12, 1)]), res1)
var res2 = res1
res2[CollidableInt(12, 1)] = nil
expectTrue(res2.contains(CollidableInt(11, 1)))
expectFalse(res2.contains(CollidableInt(12, 1)))
expectEqual(res2.count, 1)
// expectEqual(HashMap.init([CollidableInt(11, 1) : CollidableInt(11, 1)]), res2)
expectEqual(HashMap.init([CollidableInt(11, 1) : CollidableInt(11, 1)]), res2)
var res3 = res1
res3[CollidableInt(11, 1)] = nil
expectFalse(res3.contains(CollidableInt(11, 1)))
expectTrue(res3.contains(CollidableInt(12, 1)))
expectEqual(res3.count, 1)
// expectEqual(HashMap.init([CollidableInt(12, 1) : CollidableInt(12, 1)]), res3)
expectEqual(HashMap.init([CollidableInt(12, 1) : CollidableInt(12, 1)]), res3)
var resX = res1
resX[CollidableInt(32769)] = CollidableInt(32769)
resX[CollidableInt(12, 1)] = nil
expectTrue(resX.contains(CollidableInt(11, 1)))
expectFalse(resX.contains(CollidableInt(12, 1)))
expectTrue(resX.contains(CollidableInt(32769)))
expectEqual(resX.count, 2)
// expectEqual(HashMap.init([CollidableInt(11, 1) : CollidableInt(11, 1), CollidableInt(32769) : CollidableInt(32769)]), resX)
expectEqual(HashMap.init([CollidableInt(11, 1) : CollidableInt(11, 1), CollidableInt(32769) : CollidableInt(32769)]), resX)
}
func testCompactionWhenDeletingFromHashCollisionNode2() {
@@ -124,29 +129,32 @@ final class CapsuleSmokeTests: CollectionTestCase {
res1[CollidableInt(32769_1, 32769)] = CollidableInt(32769_1, 32769)
res1[CollidableInt(32769_2, 32769)] = CollidableInt(32769_2, 32769)
expectEqual(res1.count, 2)
expectTrue(res1.contains(CollidableInt(32769_1, 32769)))
expectTrue(res1.contains(CollidableInt(32769_2, 32769)))
expectEqual(res1.count, 2)
expectEqual(HashMap.init([CollidableInt(32769_1, 32769) : CollidableInt(32769_1, 32769), CollidableInt(32769_2, 32769) : CollidableInt(32769_2, 32769)]), res1)
var res2 = res1
res2[CollidableInt(1)] = CollidableInt(1)
expectEqual(res2.count, 3)
expectTrue(res2.contains(CollidableInt(1)))
expectTrue(res2.contains(CollidableInt(32769_1, 32769)))
expectTrue(res2.contains(CollidableInt(32769_2, 32769)))
expectEqual(res2.count, 3)
expectEqual(HashMap.init([CollidableInt(1) : CollidableInt(1), CollidableInt(32769_1, 32769) : CollidableInt(32769_1, 32769), CollidableInt(32769_2, 32769) : CollidableInt(32769_2, 32769)]), res2)
var res3 = res2
res3[CollidableInt(32769_2, 32769)] = nil
expectEqual(res3.count, 2)
expectTrue(res3.contains(CollidableInt(1)))
expectTrue(res3.contains(CollidableInt(32769_1, 32769)))
// expectEqual(HashMap.init([CollidableInt(1) : CollidableInt(1), CollidableInt(32769_2, 32769) : CollidableInt(32769_2, 32769)]), res3)
expectEqual(res3.count, 2)
expectEqual(HashMap.init([CollidableInt(1) : CollidableInt(1), CollidableInt(32769_1, 32769) : CollidableInt(32769_1, 32769)]), res3)
}
func testCompactionWhenDeletingFromHashCollisionNode3() {
@@ -157,30 +165,35 @@ final class CapsuleSmokeTests: CollectionTestCase {
res1[CollidableInt(32769_1, 32769)] = CollidableInt(32769_1, 32769)
res1[CollidableInt(32769_2, 32769)] = CollidableInt(32769_2, 32769)
expectEqual(res1.count, 2)
expectTrue(res1.contains(CollidableInt(32769_1, 32769)))
expectTrue(res1.contains(CollidableInt(32769_2, 32769)))
expectEqual(res1.count, 2)
expectEqual(HashMap.init([CollidableInt(32769_1, 32769) : CollidableInt(32769_1, 32769), CollidableInt(32769_2, 32769) : CollidableInt(32769_2, 32769)]), res1)
var res2 = res1
res2[CollidableInt(1)] = CollidableInt(1)
expectEqual(res2.count, 3)
expectTrue(res2.contains(CollidableInt(1)))
expectTrue(res2.contains(CollidableInt(32769_1, 32769)))
expectTrue(res2.contains(CollidableInt(32769_2, 32769)))
expectEqual(res2.count, 3)
expectEqual(HashMap.init([CollidableInt(1) : CollidableInt(1), CollidableInt(32769_1, 32769) : CollidableInt(32769_1, 32769), CollidableInt(32769_2, 32769) : CollidableInt(32769_2, 32769)]), res2)
var res3 = res2
res3[CollidableInt(1)] = nil
expectEqual(res3.count, 2)
expectTrue(res3.contains(CollidableInt(32769_1, 32769)))
expectTrue(res3.contains(CollidableInt(32769_2, 32769)))
expectEqual(res3.count, 2)
expectEqual(HashMap.init([CollidableInt(32769_1, 32769) : CollidableInt(32769_1, 32769), CollidableInt(32769_2, 32769) : CollidableInt(32769_2, 32769)]), res3)
// expectEqual(HashMap.init([CollidableInt(32769_1, 32769) : CollidableInt(32769_1, 32769), CollidableInt(32769_2, 32769) : CollidableInt(32769_2, 32769)]), res3)
expectEqual(res1, res3)
}
func testCompactionWhenDeletingFromHashCollisionNode4() {
@@ -191,29 +204,35 @@ final class CapsuleSmokeTests: CollectionTestCase {
res1[CollidableInt(32769_1, 32769)] = CollidableInt(32769_1, 32769)
res1[CollidableInt(32769_2, 32769)] = CollidableInt(32769_2, 32769)
expectEqual(res1.count, 2)
expectTrue(res1.contains(CollidableInt(32769_1, 32769)))
expectTrue(res1.contains(CollidableInt(32769_2, 32769)))
expectEqual(res1.count, 2)
expectEqual(HashMap.init([CollidableInt(32769_1, 32769) : CollidableInt(32769_1, 32769), CollidableInt(32769_2, 32769) : CollidableInt(32769_2, 32769)]), res1)
var res2 = res1
res2[CollidableInt(5)] = CollidableInt(5)
expectEqual(res2.count, 3)
expectTrue(res2.contains(CollidableInt(5)))
expectTrue(res2.contains(CollidableInt(32769_1, 32769)))
expectTrue(res2.contains(CollidableInt(32769_2, 32769)))
expectEqual(res2.count, 3)
expectEqual(HashMap.init([CollidableInt(5) : CollidableInt(5), CollidableInt(32769_1, 32769) : CollidableInt(32769_1, 32769), CollidableInt(32769_2, 32769) : CollidableInt(32769_2, 32769)]), res2)
var res3 = res2
res3[CollidableInt(5)] = nil
expectEqual(res3.count, 2)
expectTrue(res3.contains(CollidableInt(32769_1, 32769)))
expectTrue(res3.contains(CollidableInt(32769_2, 32769)))
expectEqual(res3.count, 2)
expectEqual(HashMap.init([CollidableInt(32769_1, 32769) : CollidableInt(32769_1, 32769), CollidableInt(32769_2, 32769) : CollidableInt(32769_2, 32769)]), res3)
// expectEqual(res1, res3)
expectEqual(res1, res3)
}
}