-
Notifications
You must be signed in to change notification settings - Fork 77
/
BTreeBuilder.swift
290 lines (266 loc) · 10.8 KB
/
BTreeBuilder.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
//
// BTreeBuilder.swift
// BTree
//
// Created by Károly Lőrentey on 2016-02-28.
// Copyright © 2016–2017 Károly Lőrentey.
//
extension BTree {
//MARK: Bulk loading initializers
/// Create a new B-tree from elements of an unsorted sequence, using a stable sort algorithm.
///
/// - Parameter elements: An unsorted sequence of arbitrary length.
/// - Parameter order: The desired B-tree order. If not specified (recommended), the default order is used.
/// - Complexity: O(count * log(`count`))
/// - SeeAlso: `init(sortedElements:order:fillFactor:)` for a (faster) variant that can be used if the sequence is already sorted.
public init<S: Sequence>(_ elements: S, dropDuplicates: Bool = false, order: Int? = nil)
where S.Element == Element {
let order = order ?? Node.defaultOrder
self.init(Node(order: order))
withCursorAtEnd { cursor in
for element in elements {
cursor.move(to: element.0, choosing: .last)
let match = !cursor.isAtEnd && cursor.key == element.0
if match {
if dropDuplicates {
cursor.element = element
}
else {
cursor.insertAfter(element)
}
}
else {
cursor.insert(element)
}
}
}
}
/// Create a new B-tree from elements of a sequence sorted by key.
///
/// - Parameter sortedElements: A sequence of arbitrary length, sorted by key.
/// - Parameter order: The desired B-tree order. If not specified (recommended), the default order is used.
/// - Parameter fillFactor: The desired fill factor in each node of the new tree. Must be between 0.5 and 1.0.
/// If not specified, a value of 1.0 is used, i.e., nodes will be loaded with as many elements as possible.
/// - Complexity: O(count)
/// - SeeAlso: `init(elements:order:fillFactor:)` for a (slower) unsorted variant.
public init<S: Sequence>(sortedElements elements: S, dropDuplicates: Bool = false, order: Int? = nil, fillFactor: Double = 1) where S.Element == Element {
var iterator = elements.makeIterator()
self.init(order: order ?? Node.defaultOrder, fillFactor: fillFactor, dropDuplicates: dropDuplicates, next: { iterator.next() })
}
internal init(order: Int, fillFactor: Double = 1, dropDuplicates: Bool = false, next: () -> Element?) {
precondition(order > 1)
precondition(fillFactor >= 0.5 && fillFactor <= 1)
let keysPerNode = Int(fillFactor * Double(order - 1) + 0.5)
assert(keysPerNode >= (order - 1) / 2 && keysPerNode <= order - 1)
var builder = BTreeBuilder<Key, Value>(order: order, keysPerNode: keysPerNode)
if dropDuplicates {
guard var buffer = next() else {
self.init(Node(order: order))
return
}
while let element = next() {
precondition(buffer.0 <= element.0)
if buffer.0 < element.0 {
builder.append(buffer)
}
buffer = element
}
builder.append(buffer)
}
else {
var lastKey: Key? = nil
while let element = next() {
precondition(lastKey == nil || lastKey! <= element.0)
lastKey = element.0
builder.append(element)
}
}
self.init(builder.finish())
}
}
private enum BuilderState {
/// The builder needs a separator element.
case separator
/// The builder is filling up a seedling node.
case element
}
/// A construct for efficiently building a fully loaded B-tree from a series of elements.
///
/// The bulk loading algorithm works growing a line of perfectly loaded saplings, in order of decreasing depth,
/// with a separator element between each of them.
///
/// Added elements are collected into a separator and a new leaf node (called the "seedling").
/// When the seedling becomes full it is appended to or recursively merged into the list of saplings.
///
/// When `finish` is called, the final list of saplings plus the last partial seedling is joined
/// into a single tree, which becomes the root.
internal struct BTreeBuilder<Key: Comparable, Value> {
typealias Node = BTreeNode<Key, Value>
typealias Element = Node.Element
typealias Splinter = Node.Splinter
private let order: Int
private let keysPerNode: Int
private var saplings: [Node]
private var separators: [Element]
private var seedling: Node
private var state: BuilderState
init(order: Int) {
self.init(order: order, keysPerNode: order - 1)
}
init(order: Int, keysPerNode: Int) {
precondition(order > 1)
precondition(keysPerNode >= (order - 1) / 2 && keysPerNode <= order - 1)
self.order = order
self.keysPerNode = keysPerNode
self.saplings = []
self.separators = []
self.seedling = Node(order: order)
self.state = .element
}
var lastKey: Key? {
switch state {
case .separator:
return saplings.last?.last?.0
case .element:
return seedling.last?.0 ?? separators.last?.0
}
}
func isValidNextKey(_ key: Key) -> Bool {
guard let last = lastKey else { return true }
return last <= key
}
mutating func append(_ element: Element) {
assert(isValidNextKey(element.0))
switch state {
case .separator:
separators.append(element)
state = .element
case .element:
seedling.append(element)
if seedling.count == keysPerNode {
closeSeedling()
state = .separator
}
}
}
private mutating func closeSeedling() {
append(sapling: seedling)
seedling = Node(order: order)
}
mutating func append(_ node: Node) {
appendWithoutCloning(node.clone())
}
mutating func appendWithoutCloning(_ node: Node) {
assert(node.order == order)
if node.isEmpty { return }
assert(isValidNextKey(node.first!.0))
if node.depth == 0 {
if state == .separator {
assert(seedling.isEmpty)
separators.append(node.elements.removeFirst())
node.count -= 1
state = .element
if node.isEmpty { return }
seedling = node
}
else if seedling.count > 0 {
let sep = seedling.elements.removeLast()
seedling.count -= 1
if let splinter = seedling.shiftSlots(separator: sep, node: node, target: keysPerNode) {
closeSeedling()
separators.append(splinter.separator)
seedling = splinter.node
}
}
else {
seedling = node
}
if seedling.count >= keysPerNode {
closeSeedling()
state = .separator
}
return
}
if state == .element && seedling.count > 0 {
let sep = seedling.elements.removeLast()
seedling.count -= 1
closeSeedling()
separators.append(sep)
}
if state == .separator {
let cursor = BTreeCursor(BTreeCursorPath(endOf: saplings.removeLast()))
cursor.moveBackward()
let separator = cursor.remove()
saplings.append(cursor.finish())
separators.append(separator)
}
assert(seedling.isEmpty)
append(sapling: node)
state = .separator
}
private mutating func append(sapling: Node) {
var sapling = sapling
while !saplings.isEmpty {
assert(saplings.count == separators.count)
var previous = saplings.removeLast()
let separator = separators.removeLast()
// Join previous saplings together until they grow at least as deep as the new one.
while previous.depth < sapling.depth {
if saplings.isEmpty {
// If the single remaining sapling is too shallow, just join it to the new sapling and call it a day.
saplings.append(Node.join(left: previous, separator: separator, right: sapling))
return
}
previous = Node.join(left: saplings.removeLast(), separator: separators.removeLast(), right: previous)
}
let fullPrevious = previous.elements.count >= keysPerNode
let fullSapling = sapling.elements.count >= keysPerNode
if previous.depth == sapling.depth + 1 && !fullPrevious && fullSapling {
// Graft node under the last sapling, as a new child branch.
previous.elements.append(separator)
previous.children.append(sapling)
previous.count += sapling.count + 1
sapling = previous
}
else if previous.depth == sapling.depth && fullPrevious && fullSapling {
// We have two full nodes; add them as two branches of a new, deeper node.
sapling = Node(left: previous, separator: separator, right: sapling)
}
else if previous.depth > sapling.depth || fullPrevious {
// The new sapling can be appended to the line and we're done.
saplings.append(previous)
separators.append(separator)
break
}
else if let splinter = previous.shiftSlots(separator: separator, node: sapling, target: keysPerNode) {
// We have made the previous sapling full; add it as a new one before trying again with the remainder.
assert(previous.elements.count == keysPerNode)
append(sapling: previous)
separators.append(splinter.separator)
sapling = splinter.node
}
else {
// We've combined the two saplings; try again with the result.
sapling = previous
}
}
saplings.append(sapling)
}
mutating func finish() -> Node {
// Merge all saplings and the seedling into a single tree.
var root: Node
if separators.count == saplings.count - 1 {
assert(seedling.count == 0)
root = saplings.removeLast()
}
else {
root = seedling
}
assert(separators.count == saplings.count)
while !saplings.isEmpty {
root = Node.join(left: saplings.removeLast(), separator: separators.removeLast(), right: root)
}
state = .element
return root
}
}