From b34c8dad97fa04a4352b2df95ef6fdf17fb51bfb Mon Sep 17 00:00:00 2001 From: Michael Steindorfer Date: Tue, 4 May 2021 17:23:01 +0200 Subject: [PATCH] [Capsule] Implement `Equatable` protocol --- Sources/Capsule/_BitmapIndexedMapNode.swift | 38 +++++++++++++-- Sources/Capsule/_HashCollisionMapNode.swift | 4 +- Tests/CapsuleTests/CapsuleSmokeTests.swift | 53 ++++++++++++++------- 3 files changed, 72 insertions(+), 23 deletions(-) diff --git a/Sources/Capsule/_BitmapIndexedMapNode.swift b/Sources/Capsule/_BitmapIndexedMapNode.swift index 5ffe0bdd..4082eafe 100644 --- a/Sources/Capsule/_BitmapIndexedMapNode.swift +++ b/Sources/Capsule/_BitmapIndexedMapNode.swift @@ -248,6 +248,11 @@ final class BitmapIndexedMapNode : MapNode where Key : Hashable { content[content.count - 1 - index] as! HashCollisionMapNode } + // 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 : MapNode where Key : Hashable { } } -extension BitmapIndexedMapNode /* : Equatable where Value : Equatable */ { +extension BitmapIndexedMapNode : Equatable where Value : Equatable { static func == (lhs: BitmapIndexedMapNode, rhs: BitmapIndexedMapNode) -> 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, _ rhs: BitmapIndexedMapNode) -> Bool { + for index in 0.., + let rhsNode = rhs.getAnyNode(index) as? BitmapIndexedMapNode { + if (lhsNode != rhsNode) { + return false + } + } else if let lhsNode = lhs.getAnyNode(index) as? HashCollisionMapNode, + let rhsNode = rhs.getAnyNode(index) as? HashCollisionMapNode { + if (lhsNode != rhsNode) { + return false + } + } else { + return false + } + } + + return true } } diff --git a/Sources/Capsule/_HashCollisionMapNode.swift b/Sources/Capsule/_HashCollisionMapNode.swift index b8f43a93..296468eb 100644 --- a/Sources/Capsule/_HashCollisionMapNode.swift +++ b/Sources/Capsule/_HashCollisionMapNode.swift @@ -87,8 +87,8 @@ final class HashCollisionMapNode : 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, rhs: HashCollisionMapNode) -> Bool { - preconditionFailure("Not yet implemented") + Dictionary.init(uniqueKeysWithValues: lhs.content) == Dictionary.init(uniqueKeysWithValues: rhs.content) } } diff --git a/Tests/CapsuleTests/CapsuleSmokeTests.swift b/Tests/CapsuleTests/CapsuleSmokeTests.swift index b937d154..92d4b496 100644 --- a/Tests/CapsuleTests/CapsuleSmokeTests.swift +++ b/Tests/CapsuleTests/CapsuleSmokeTests.swift @@ -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) } }