forked from cii0/ShikishiOSS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTree.swift
244 lines (229 loc) · 7.63 KB
/
Tree.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
// Copyright 2021 Cii
//
// This file is part of Shikishi.
//
// Shikishi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Shikishi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Shikishi. If not, see <http://www.gnu.org/licenses/>.
typealias IndexPath = [Int]
protocol Tree: Sequence {
var children: [Self] { get set }
subscript(indexPath: IndexPath) -> Self { get set }
}
extension Tree {
typealias Index = IndexPath
subscript(indexPath: IndexPath) -> Self {
get { at(indexPath: indexPath, at: 0) }
set { self[keyPath: Self.keyPath(with: indexPath)] = newValue }
}
func at(indexPath: IndexPath, at i: Int) -> Self {
guard i < indexPath.count else { return self }
let index = indexPath[i]
let child = children[index]
return child.at(indexPath: indexPath, at: i + 1)
}
static func keyPath(with indexPath: IndexPath) -> WritableKeyPath<Self, Self> {
var indexPath = indexPath
let keyPath: WritableKeyPath<Self, Self> = \Self.children[indexPath[0]]
indexPath.removeFirst()
return indexPath.reduce(keyPath) {
$0.appending(path: \Self.children[indexPath[$1]])
}
}
func makeIterator() -> TreeIterator<Self> {
TreeIterator(rootTree: self)
}
func treeIndexEnumerated() -> TreeIndexSequence<Self> {
TreeIndexSequence(rootTree: self)
}
func elements(at treeIndex: Index) -> [Self] {
guard !treeIndex.isEmpty else { return [] }
var elements = [Self]()
elements.reserveCapacity(treeIndex.count)
self.elements(at: treeIndex, index: 0, elements: &elements)
return elements
}
private func elements(at treeIndex: Index, index: Int,
elements: inout [Self]) {
elements.append(children[treeIndex[index]])
let newIndex = index + 1
if newIndex < treeIndex.count {
self.elements(at: treeIndex, index: newIndex, elements: &elements)
}
}
func sortedIndexes(_ indexes: [Index]) -> [Index] {
var sortedIndexes = [Index]()
for (i, _) in treeIndexEnumerated() {
if indexes.contains(i) {
sortedIndexes.append(i)
}
}
return sortedIndexes
}
}
struct TreeIterator<T: Tree>: IteratorProtocol {
typealias Element = T
init(rootTree: T) {
treeValues = [(rootTree, 0)]
}
private var treeValues = [(tree: T, index: Int)]()
mutating func next() -> T? {
guard let lastTreeValue = treeValues.last else {
return nil
}
if lastTreeValue.index >= lastTreeValue.tree.children.count {
treeValues.removeLast()
if !treeValues.isEmpty {
treeValues[treeValues.count - 1].index += 1
}
return lastTreeValue.tree
} else {
let child = lastTreeValue.tree.children[lastTreeValue.index]
if child.children.isEmpty {
treeValues[treeValues.count - 1].index += 1
return child
} else {
var aChild = child
repeat {
treeValues.append((aChild, 0))
aChild = aChild.children[0]
} while !aChild.children.isEmpty
treeValues[treeValues.count - 1].index += 1
return aChild
}
}
}
}
struct TreeIndexSequence<T: Tree>: Sequence, IteratorProtocol {
typealias Element = (T.Index, T)
init(rootTree: T) {
trees = [rootTree]
indexPath = [0]
}
private var indexPath: IndexPath, trees: [T]
mutating func next() -> Element? {
guard let lastChildIndex = indexPath.last,
let lastTree = trees.last else {
return nil
}
if lastChildIndex >= lastTree.children.count {
indexPath.removeLast()
trees.removeLast()
let oldIndexPath = indexPath
if !indexPath.isEmpty {
indexPath[indexPath.count - 1] += 1
}
return (oldIndexPath, lastTree)
} else {
let child = lastTree.children[lastChildIndex]
if child.children.isEmpty {
let oldIndexPath = indexPath
indexPath[indexPath.count - 1] += 1
return (oldIndexPath, child)
} else {
var aChild = child
repeat {
indexPath.append(0)
trees.append(aChild)
aChild = aChild.children[0]
} while !aChild.children.isEmpty
let oldIndexPath = indexPath
indexPath[indexPath.count - 1] += 1
return (oldIndexPath, aChild)
}
}
}
}
private final class BinarySearchElement<T: Comparable> {
var value: T
var left, right: BinarySearchElement?
init(_ value: T,
left: BinarySearchElement? = nil,
right: BinarySearchElement? = nil) {
self.value = value
self.left = left
self.right = right
}
}
extension BinarySearchElement {
func insert(_ value: T) {
if value < self.value {
if let left = left {
left.insert(value)
} else {
left = BinarySearchElement(value)
}
} else {
if let right = right {
right.insert(value)
} else {
right = BinarySearchElement(value)
}
}
}
func remove(_ value: T) -> BinarySearchElement<T>? {
if value == self.value {
guard left != nil else {
return right
}
guard var element = right else {
return left
}
while let aElement = element.left {
element = aElement
}
self.value = element.value
right = self.right?.remove(element.value)
} else if value < self.value {
left = self.left?.remove(value)
} else {
right = self.right?.remove(value)
}
return self
}
func previous(at value: T) -> T? {
if value > self.value {
return right?.previous(at: value) ?? self.value
} else {
return left?.previous(at: value)
}
}
func copy() -> BinarySearchElement<T> {
BinarySearchElement(value, left: left?.copy(), right: right?.copy())
}
}
struct BinarySearchTree<T: Comparable> {
private var rootElement: BinarySearchElement<T>?
}
extension BinarySearchTree {
private mutating func copyIfShared() {
if isKnownUniquelyReferenced(&rootElement) { return }
rootElement = rootElement?.copy()
}
mutating func insert(_ value: T) {
copyIfShared()
if let rootElement = rootElement {
rootElement.insert(value)
} else {
rootElement = BinarySearchElement(value)
}
}
mutating func remove(_ value: T) {
copyIfShared()
rootElement = rootElement?.remove(value)
}
}
extension BinarySearchTree {
func previous(at value: T) -> T? {
rootElement?.previous(at: value)
}
}