forked from apple/swift-collections
/
PersistentDictionary.swift
164 lines (144 loc) · 4.27 KB
/
PersistentDictionary.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2019 - 2022 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
//
//===----------------------------------------------------------------------===//
public struct PersistentDictionary<Key, Value> where Key: Hashable {
@usableFromInline
internal typealias _Node = PersistentCollections._Node<Key, Value>
@usableFromInline
var _root: _Node
/// The version number of this instance, used for quick index validation.
/// This is initialized to a (very weakly) random value and it gets
/// incremented on every mutation that needs to invalidate indices.
@usableFromInline
var _version: UInt
@inlinable
internal init(_root: _Node, version: UInt) {
self._root = _root
self._version = version
}
@inlinable
internal init(_new: _Node) {
self._root = _new
// Ideally we would simply just generate a true random number, but the
// memory address of the root node is a reasonable substitute.
let address = Unmanaged.passUnretained(_root.raw.storage).toOpaque()
self._version = UInt(bitPattern: address)
}
}
extension PersistentDictionary {
@inlinable
public init() {
self.init(_new: _Node(storage: _emptySingleton, count: 0))
}
@inlinable
public init(_ other: PersistentDictionary<Key, Value>) {
self = other
}
@inlinable
public func _invariantCheck() {
_root._fullInvariantCheck(.top, _Hash(_value: 0))
}
@inlinable
@inline(__always)
public init<S: Sequence>(
uniqueKeysWithValues keysAndValues: S
) where S.Element == (Key, Value) {
self.init()
for (key, value) in keysAndValues {
let unique = updateValue(value, forKey: key) == nil
precondition(unique, "Duplicate key: '\(key)'")
}
_invariantCheck()
}
@inlinable
@inline(__always)
public init<Keys: Sequence, Values: Sequence>(
uniqueKeys keys: Keys,
values: Values
) where Keys.Element == Key, Values.Element == Value {
self.init(uniqueKeysWithValues: zip(keys, values))
}
@inlinable
public subscript(key: Key) -> Value? {
get {
_root.get(.top, key, _Hash(key))
}
mutating set {
if let value = newValue {
updateValue(value, forKey: key)
} else {
removeValue(forKey: key)
}
}
}
@inlinable
public subscript(
key: Key,
default defaultValue: @autoclosure () -> Value
) -> Value {
get {
_root.get(.top, key, _Hash(key)) ?? defaultValue()
}
set {
updateValue(newValue, forKey: key)
}
@inline(__always) // https://github.com/apple/swift-collections/issues/164
_modify {
var state = _root.prepareDefaultedValueUpdate(
key, defaultValue, .top, _Hash(key))
if state.inserted { _invalidateIndices() }
defer {
_root.finalizeDefaultedValueUpdate(state)
}
yield &state.item.value
}
}
@inlinable
public func contains(_ key: Key) -> Bool {
_root.containsKey(.top, key, _Hash(key))
}
/// Returns the index for the given key.
@inlinable
public func index(forKey key: Key) -> Index? {
let (found, path) = _root.path(to: key, hash: _Hash(key))
guard found else { return nil }
return Index(_root: _root.unmanaged, version: _version, path: path)
}
@inlinable
@discardableResult
public mutating func updateValue(
_ value: __owned Value, forKey key: Key
) -> Value? {
let old = _root.updateValue(value, forKey: key, .top, _Hash(key))
if old == nil { _invalidateIndices() }
return old
}
// fluid/immutable API
@inlinable
public func updatingValue(_ value: Value, forKey key: Key) -> Self {
var copy = self
copy.updateValue(value, forKey: key)
return copy
}
@inlinable
@discardableResult
public mutating func removeValue(forKey key: Key) -> Value? {
let old = _root.remove(key, .top, _Hash(key))?.value
if old != nil { _invalidateIndices() }
return old
}
// fluid/immutable API
@inlinable
public func removingValue(forKey key: Key) -> Self {
var copy = self
copy.removeValue(forKey: key)
return copy
}
}