mirror of
https://github.com/apple/swift-collections
synced 2026-11-28 05:04:21 +00:00
116 lines
4.6 KiB
Swift
116 lines
4.6 KiB
Swift
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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 HashCollisionDictionaryNode<Key, Value>: DictionaryNode where Key: Hashable {
|
|
|
|
typealias ReturnPayload = (key: Key, value: Value)
|
|
|
|
let hash: Int
|
|
let content: [(key: Key, value: Value)]
|
|
|
|
init(_ hash: Int, _ content: [(key: Key, value: Value)]) {
|
|
// precondition(content.count >= 2)
|
|
precondition(content.map { $0.key }.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.key }).map { $0.value }
|
|
} else { return nil }
|
|
}
|
|
|
|
func containsKey(_ key: Key, _ hash: Int, _ shift: Int) -> Bool {
|
|
return self.hash == hash && content.contains(where: { key == $0.key })
|
|
}
|
|
|
|
// // 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.key && value == $0.value })
|
|
// }
|
|
|
|
func updateOrUpdating(_ isStorageKnownUniquelyReferenced: Bool, _ key: Key, _ value: Value, _ hash: Int, _ shift: Int, _ effect: inout DictionaryEffect) -> HashCollisionDictionaryNode<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.key })!
|
|
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 HashCollisionDictionaryNode(hash, Array(updatedContent))
|
|
} else {
|
|
effect.setModified()
|
|
return HashCollisionDictionaryNode(hash, content + [(key, value)])
|
|
}
|
|
}
|
|
|
|
// TODO rethink such that `precondition(content.count >= 2)` holds
|
|
// TODO consider returning either type of `BitmapIndexedDictionaryNode` and `HashCollisionDictionaryNode`
|
|
func removeOrRemoving(_ isStorageKnownUniquelyReferenced: Bool, _ key: Key, _ hash: Int, _ shift: Int, _ effect: inout DictionaryEffect) -> HashCollisionDictionaryNode<Key, Value> {
|
|
if !self.containsKey(key, hash, shift) {
|
|
return self
|
|
} else {
|
|
effect.setModified()
|
|
let updatedContent = content.filter { $0.key != key }
|
|
assert(updatedContent.count == content.count - 1)
|
|
|
|
// switch updatedContent.count {
|
|
// case 1:
|
|
// let (k, v) = updatedContent[0].self
|
|
// return BitmapIndexedDictionaryNode<Key, Value>(bitposFrom(maskFrom(hash, 0)), 0, Array(arrayLiteral: k, v))
|
|
// default:
|
|
return HashCollisionDictionaryNode(hash, updatedContent)
|
|
// }
|
|
}
|
|
}
|
|
|
|
var hasBitmapIndexedNodes: Bool { false }
|
|
|
|
var bitmapIndexedNodeArity: Int { 0 }
|
|
|
|
func getBitmapIndexedNode(_ index: Int) -> HashCollisionDictionaryNode<Key, Value> {
|
|
preconditionFailure("No sub-nodes present in hash-collision leaf node")
|
|
}
|
|
|
|
var hasHashCollisionNodes: Bool { false }
|
|
|
|
var hashCollisionNodeArity: Int { 0 }
|
|
|
|
func getHashCollisionNode(_ index: Int) -> HashCollisionDictionaryNode<Key, Value> {
|
|
preconditionFailure("No sub-nodes present in hash-collision leaf node")
|
|
}
|
|
|
|
var hasNodes: Bool { false }
|
|
|
|
var nodeArity: Int { 0 }
|
|
|
|
func getNode(_ index: Int) -> TrieNode<HashCollisionDictionaryNode<Key, Value>, HashCollisionDictionaryNode<Key, Value>> {
|
|
preconditionFailure("No sub-nodes present in hash-collision leaf node")
|
|
}
|
|
|
|
var hasPayload: Bool { true }
|
|
|
|
var payloadArity: Int { content.count }
|
|
|
|
func getPayload(_ index: Int) -> (key: Key, value: Value) { content[index] }
|
|
|
|
var sizePredicate: SizePredicate { SizePredicate(self) }
|
|
}
|
|
|
|
extension HashCollisionDictionaryNode: Equatable where Value: Equatable {
|
|
static func == (lhs: HashCollisionDictionaryNode<Key, Value>, rhs: HashCollisionDictionaryNode<Key, Value>) -> Bool {
|
|
Dictionary.init(uniqueKeysWithValues: lhs.content) == Dictionary.init(uniqueKeysWithValues: rhs.content)
|
|
}
|
|
}
|