diff --git a/Sources/Capsule/HashMap+Equatable.swift b/Sources/Capsule/HashMap+Equatable.swift index ee3f355e..c1c34776 100644 --- a/Sources/Capsule/HashMap+Equatable.swift +++ b/Sources/Capsule/HashMap+Equatable.swift @@ -9,7 +9,8 @@ // //===----------------------------------------------------------------------===// -extension HashMap : Equatable { +// TODO check Dictionary semantics of Equatable (i.e., if it only compares keys or also values) +extension HashMap : Equatable where Value : Equatable { public static func == (lhs: HashMap, rhs: HashMap) -> Bool { lhs.cachedSize == rhs.cachedSize && lhs.cachedKeySetHashCode == rhs.cachedKeySetHashCode && diff --git a/Sources/Capsule/_BitmapIndexedMapNode.swift b/Sources/Capsule/_BitmapIndexedMapNode.swift index 138a55f0..5a31a910 100644 --- a/Sources/Capsule/_BitmapIndexedMapNode.swift +++ b/Sources/Capsule/_BitmapIndexedMapNode.swift @@ -156,7 +156,7 @@ final class BitmapIndexedMapNode : MapNode where Key : H func mergeTwoKeyValPairs(_ key0: Key, _ value0: Value, _ keyHash0: Int, _ key1: Key, _ value1: Value, _ keyHash1: Int, _ shift: Int) -> MapNode { if (shift >= HashCodeLength) { - preconditionFailure("Not yet implemented") + return HashCollisionMapNode(keyHash0, [(key0, value0), (key1, value1)]) } else { let mask0 = maskFrom(keyHash0, shift) let mask1 = maskFrom(keyHash1, shift) @@ -279,7 +279,7 @@ final class BitmapIndexedMapNode : MapNode where Key : H } } -extension BitmapIndexedMapNode /* : Equatable */ { +extension BitmapIndexedMapNode /* : Equatable where Value : Equatable */ { static func == (lhs: BitmapIndexedMapNode, rhs: BitmapIndexedMapNode) -> Bool { lhs === rhs || lhs.nodeMap == rhs.nodeMap && diff --git a/Sources/Capsule/_HashCollisionMapNode.swift b/Sources/Capsule/_HashCollisionMapNode.swift new file mode 100644 index 00000000..57648cb6 --- /dev/null +++ b/Sources/Capsule/_HashCollisionMapNode.swift @@ -0,0 +1,92 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the Swift Collections open source project +// +// Copyright (c) 2021 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 +// +//===----------------------------------------------------------------------===// + +final class HashCollisionMapNode : MapNode where Key : Hashable { + let hash: Int + let content: Array<(Key, Value)> + + init(_ hash: Int, _ content: Array<(Key, Value)>) { + precondition(content.count >= 2) + precondition(content.map { $0.0 }.allSatisfy {$0.hashValue == hash}) + + self.hash = hash + self.content = content + } + + override func get(_ key: Key, _ hash: Int, _ shift: Int) -> Value? { + if (self.hash == hash) { + return content.first(where: { key == $0.0 }).map { $0.1 } + } else { return nil } + } + + override func containsKey(_ key: Key, _ hash: Int, _ shift: Int) -> Bool { + return self.hash == hash && content.contains(where: { key == $0.0 }) + } + +// // TODO requires Value to be Equatable +// func contains(_ key: Key, _ value: Value, _ hash: Int, _ shift: Int) -> Bool { +// return self.hash == hash && content.contains(where: { key == $0.0 && value == $0.1 }) +// } + + override func updated(_ key: Key, _ value: Value, _ hash: Int, _ shift: Int, _ effect: inout MapEffect) -> MapNode { + + // TODO check if key/value-pair check should be added (requires value to be Equitable) + if (self.containsKey(key, hash, shift)) { + let index = content.firstIndex(where: { key == $0.0 })! + let updatedContent = content[0.. MapNode { + if (!self.containsKey(key, hash, shift)) { + return self + } else { + effect.setModified() + let updatedContent = content.filter { $0.0 != key } + assert(updatedContent.count == content.count - 1) + + switch updatedContent.count { + case 1: + let (k, v) = updatedContent[0].self + return BitmapIndexedMapNode(bitposFrom(maskFrom(hash, 0)), 0, Array(arrayLiteral: k, v)) + default: + return HashCollisionMapNode(hash, updatedContent) + } + } + } + + override var hasNodes: Bool { false } + + override var nodeArity: Int { 0 } + + override func getNode(_ index: Int) -> MapNode { + preconditionFailure("No sub-nodes present in hash-collision leaf node") + } + + override var hasPayload: Bool { true } + + override var payloadArity: Int { content.count } + + override func getPayload(_ index: Int) -> (Key, Value) { content[index] } +} + +extension HashCollisionMapNode /* : Equatable where Value : Equatable */ { + static func == (lhs: HashCollisionMapNode, rhs: HashCollisionMapNode) -> Bool { + preconditionFailure("Not yet implemented") + } +} diff --git a/Sources/Capsule/_MapNode.swift b/Sources/Capsule/_MapNode.swift index 403168dd..b047d4a7 100644 --- a/Sources/Capsule/_MapNode.swift +++ b/Sources/Capsule/_MapNode.swift @@ -53,7 +53,7 @@ class MapNode : Node where Key : Hashable { } } -extension MapNode : Equatable { +extension MapNode : Equatable where Value : Equatable { static func == (lhs: MapNode, rhs: MapNode) -> Bool { preconditionFailure("Not yet implemented") } diff --git a/Tests/CapsuleTests/CapsuleSmokeTests.swift b/Tests/CapsuleTests/CapsuleSmokeTests.swift index 0861adef..b937d154 100644 --- a/Tests/CapsuleTests/CapsuleSmokeTests.swift +++ b/Tests/CapsuleTests/CapsuleSmokeTests.swift @@ -73,4 +73,173 @@ final class CapsuleSmokeTests: CollectionTestCase { expectEqual(inoutHasher.finalize(), expectedHashValue) } + + func testCompactionWhenDeletingFromHashCollisionNode1() { + let map: HashMap = [:] + + + var res1 = map + 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))) + + + 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) + + + 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) + + + 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) + } + + func testCompactionWhenDeletingFromHashCollisionNode2() { + let map: HashMap = [:] + + + var res1 = map + 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))) + + + 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))) + + + 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) + } + + func testCompactionWhenDeletingFromHashCollisionNode3() { + let map: HashMap = [:] + + + var res1 = map + 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))) + + + 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))) + + + 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(HashMap.init([CollidableInt(32769_1, 32769) : CollidableInt(32769_1, 32769), CollidableInt(32769_2, 32769) : CollidableInt(32769_2, 32769)]), res3) + + } + + func testCompactionWhenDeletingFromHashCollisionNode4() { + let map: HashMap = [:] + + + var res1 = map + 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))) + + + 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))) + + + 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(res1, res3) + } +} + +fileprivate final class CollidableInt : CustomStringConvertible, Equatable, Hashable { + let value: Int + let hashValue: Int + + fileprivate init(_ value: Int) { + self.value = value + self.hashValue = value + } + + fileprivate init(_ value: Int, _ hashValue: Int) { + self.value = value + self.hashValue = hashValue + } + + var description: String { + return "\(value) [hash = \(hashValue)]" + } + + func hash(into hasher: inout Hasher) { + hasher.combine(hashValue) + } + + static func == (lhs: CollidableInt, rhs: CollidableInt) -> Bool { + return lhs.value == rhs.value + } }