mirror of
https://github.com/apple/swift-collections
synced 2026-11-27 04:54:21 +00:00
[Capsule] Implement preliminary hash-collision resolution
This commit is contained in:
1 parent
77ef6122d3
commit
e09c8db428
5 files changed
+266
-4
No files matched your search
@@ -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<Key, Value>, rhs: HashMap<Key, Value>) -> Bool {
|
||||
lhs.cachedSize == rhs.cachedSize &&
|
||||
lhs.cachedKeySetHashCode == rhs.cachedKeySetHashCode &&
|
||||
|
||||
@@ -156,7 +156,7 @@ final class BitmapIndexedMapNode<Key, Value> : MapNode<Key, Value> where Key : H
|
||||
|
||||
func mergeTwoKeyValPairs(_ key0: Key, _ value0: Value, _ keyHash0: Int, _ key1: Key, _ value1: Value, _ keyHash1: Int, _ shift: Int) -> MapNode<Key, Value> {
|
||||
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<Key, Value> : MapNode<Key, Value> where Key : H
|
||||
}
|
||||
}
|
||||
|
||||
extension BitmapIndexedMapNode /* : Equatable */ {
|
||||
extension BitmapIndexedMapNode /* : Equatable where Value : Equatable */ {
|
||||
static func == (lhs: BitmapIndexedMapNode<Key, Value>, rhs: BitmapIndexedMapNode<Key, Value>) -> Bool {
|
||||
lhs === rhs ||
|
||||
lhs.nodeMap == rhs.nodeMap &&
|
||||
|
||||
@@ -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<Key, Value> : MapNode<Key, Value> 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<Key, Value> {
|
||||
|
||||
// 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..<index] + [(key, value)] + content[index+1..<content.count]
|
||||
|
||||
effect.setReplacedValue()
|
||||
// TODO check (performance of) slicing and materialization of array content
|
||||
return HashCollisionMapNode(hash, Array(updatedContent))
|
||||
} else {
|
||||
effect.setModified()
|
||||
return HashCollisionMapNode(hash, content + [(key, value)])
|
||||
}
|
||||
}
|
||||
|
||||
override func removed(_ key: Key, _ hash: Int, _ shift: Int, _ effect: inout MapEffect) -> MapNode<Key, Value> {
|
||||
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<Key, Value>(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<Key, Value> {
|
||||
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<Key, Value>, rhs: HashCollisionMapNode<Key, Value>) -> Bool {
|
||||
preconditionFailure("Not yet implemented")
|
||||
}
|
||||
}
|
||||
@@ -53,7 +53,7 @@ class MapNode<Key, Value> : Node where Key : Hashable {
|
||||
}
|
||||
}
|
||||
|
||||
extension MapNode : Equatable {
|
||||
extension MapNode : Equatable where Value : Equatable {
|
||||
static func == (lhs: MapNode<Key, Value>, rhs: MapNode<Key, Value>) -> Bool {
|
||||
preconditionFailure("Not yet implemented")
|
||||
}
|
||||
|
||||
@@ -73,4 +73,173 @@ final class CapsuleSmokeTests: CollectionTestCase {
|
||||
|
||||
expectEqual(inoutHasher.finalize(), expectedHashValue)
|
||||
}
|
||||
|
||||
func testCompactionWhenDeletingFromHashCollisionNode1() {
|
||||
let map: HashMap<CollidableInt, CollidableInt> = [:]
|
||||
|
||||
|
||||
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<CollidableInt, CollidableInt> = [:]
|
||||
|
||||
|
||||
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<CollidableInt, CollidableInt> = [:]
|
||||
|
||||
|
||||
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<CollidableInt, CollidableInt> = [:]
|
||||
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user