//===----------------------------------------------------------------------===// // // 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 } 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 } } 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 }) // } func updated(_ isStorageKnownUniquelyReferenced: Bool, _ key: Key, _ value: Value, _ hash: Int, _ shift: Int, _ effect: inout MapEffect) -> HashCollisionMapNode { // 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..= 2)` holds // TODO consider returning either type of `BitmapIndexedMapNode` and `HashCollisionMapNode` func removed(_ isStorageKnownUniquelyReferenced: Bool, _ key: Key, _ hash: Int, _ shift: Int, _ effect: inout MapEffect) -> HashCollisionMapNode { 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) // } } } var hasNodes: Bool { false } var nodeArity: Int { 0 } func getNode(_ index: Int) -> HashCollisionMapNode { preconditionFailure("No sub-nodes present in hash-collision leaf node") } var hasCollisionNodes: Bool { false } var collisionNodeArity: Int { 0 } func getCollisionNode(_ index: Int) -> HashCollisionMapNode { preconditionFailure("No sub-nodes present in hash-collision leaf node") } var hasAnyNodes: Bool { false } var anyNodeArity: Int { 0 } func getAnyNode(_ index: Int) -> AnyNode, HashCollisionMapNode> { preconditionFailure("No sub-nodes present in hash-collision leaf node") } var hasPayload: Bool { true } var payloadArity: Int { content.count } func getPayload(_ index: Int) -> (Key, Value) { content[index] } var sizePredicate: SizePredicate { SizePredicate(self) } } extension HashCollisionMapNode : Equatable where Value : Equatable { static func == (lhs: HashCollisionMapNode, rhs: HashCollisionMapNode) -> Bool { Dictionary.init(uniqueKeysWithValues: lhs.content) == Dictionary.init(uniqueKeysWithValues: rhs.content) } }