Skip to content

Commit 559c0b1

Browse files
author
Chris Pilcher
committed
Xcode auto migration to Swift 3
1 parent 116fb5d commit 559c0b1

File tree

3 files changed

+46
-43
lines changed

3 files changed

+46
-43
lines changed

AVL Tree/AVLTree.swift

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2121
// SOFTWARE.
2222

23-
public class TreeNode<Key: Comparable, Payload> {
23+
open class TreeNode<Key: Comparable, Payload> {
2424
public typealias Node = TreeNode<Key, Payload>
2525

26-
public var payload: Payload?
26+
open var payload: Payload?
2727

28-
private var key: Key
28+
fileprivate var key: Key
2929
internal var leftChild: Node?
3030
internal var rightChild: Node?
31-
private var height: Int
32-
weak private var parent: Node?
31+
fileprivate var height: Int
32+
weak fileprivate var parent: Node?
3333

3434
public init(key: Key, payload: Payload?, leftChild: Node?, rightChild: Node?, parent: Node?, height: Int) {
3535
self.key = key
@@ -51,46 +51,46 @@ public class TreeNode<Key: Comparable, Payload> {
5151
self.init(key: key, payload: nil)
5252
}
5353

54-
public var isRoot: Bool {
54+
open var isRoot: Bool {
5555
return parent == nil
5656
}
5757

58-
public var isLeaf: Bool {
58+
open var isLeaf: Bool {
5959
return rightChild == nil && leftChild == nil
6060
}
6161

62-
public var isLeftChild: Bool {
62+
open var isLeftChild: Bool {
6363
return parent?.leftChild === self
6464
}
6565

66-
public var isRightChild: Bool {
66+
open var isRightChild: Bool {
6767
return parent?.rightChild === self
6868
}
6969

70-
public var hasLeftChild: Bool {
70+
open var hasLeftChild: Bool {
7171
return leftChild != nil
7272
}
7373

74-
public var hasRightChild: Bool {
74+
open var hasRightChild: Bool {
7575
return rightChild != nil
7676
}
7777

78-
public var hasAnyChild: Bool {
78+
open var hasAnyChild: Bool {
7979
return leftChild != nil || rightChild != nil
8080
}
8181

82-
public var hasBothChildren: Bool {
82+
open var hasBothChildren: Bool {
8383
return leftChild != nil && rightChild != nil
8484
}
8585
}
8686

8787
// MARK: - The AVL tree
8888

89-
public class AVLTree<Key: Comparable, Payload> {
89+
open class AVLTree<Key: Comparable, Payload> {
9090
public typealias Node = TreeNode<Key, Payload>
9191

92-
private(set) public var root: Node?
93-
private(set) public var size = 0
92+
fileprivate(set) open var root: Node?
93+
fileprivate(set) open var size = 0
9494

9595
public init() { }
9696
}
@@ -119,15 +119,15 @@ extension AVLTree {
119119
set { insert(key, newValue) }
120120
}
121121

122-
public func search(input: Key) -> Payload? {
122+
public func search(_ input: Key) -> Payload? {
123123
if let result = search(input, root) {
124124
return result.payload
125125
} else {
126126
return nil
127127
}
128128
}
129129

130-
private func search(key: Key, _ node: Node?) -> Node? {
130+
fileprivate func search(_ key: Key, _ node: Node?) -> Node? {
131131
if let node = node {
132132
if key == node.key {
133133
return node
@@ -144,7 +144,7 @@ extension AVLTree {
144144
// MARK: - Inserting new items
145145

146146
extension AVLTree {
147-
public func insert(key: Key, _ payload: Payload? = nil) {
147+
public func insert(_ key: Key, _ payload: Payload? = nil) {
148148
if let root = root {
149149
insert(key, payload, root)
150150
} else {
@@ -153,7 +153,7 @@ extension AVLTree {
153153
size += 1
154154
}
155155

156-
private func insert(input: Key, _ payload: Payload?, _ node: Node) {
156+
fileprivate func insert(_ input: Key, _ payload: Payload?, _ node: Node) {
157157
if input < node.key {
158158
if let child = node.leftChild {
159159
insert(input, payload, child)
@@ -177,7 +177,7 @@ extension AVLTree {
177177
// MARK: - Balancing tree
178178

179179
extension AVLTree {
180-
private func updateHeightUpwards(node: Node?) {
180+
fileprivate func updateHeightUpwards(_ node: Node?) {
181181
if let node = node {
182182
let lHeight = node.leftChild?.height ?? 0
183183
let rHeight = node.rightChild?.height ?? 0
@@ -186,22 +186,22 @@ extension AVLTree {
186186
}
187187
}
188188

189-
private func lrDifference(node: Node?) -> Int {
189+
fileprivate func lrDifference(_ node: Node?) -> Int {
190190
let lHeight = node?.leftChild?.height ?? 0
191191
let rHeight = node?.rightChild?.height ?? 0
192192
return lHeight - rHeight
193193
}
194194

195-
private func balance(node: Node?) {
195+
fileprivate func balance(_ node: Node?) {
196196
guard let node = node else {
197197
return
198198
}
199199

200200
updateHeightUpwards(node.leftChild)
201201
updateHeightUpwards(node.rightChild)
202202

203-
var nodes = [Node?](count: 3, repeatedValue: nil)
204-
var subtrees = [Node?](count: 4, repeatedValue: nil)
203+
var nodes = [Node?](repeating: nil, count: 3)
204+
var subtrees = [Node?](repeating: nil, count: 4)
205205
let nodeParent = node.parent
206206

207207
let lrFactor = lrDifference(node)
@@ -295,7 +295,7 @@ extension AVLTree {
295295
// MARK: - Displaying tree
296296

297297
extension AVLTree {
298-
private func display(node: Node?, level: Int) {
298+
fileprivate func display(_ node: Node?, level: Int) {
299299
if let node = node {
300300
display(node.rightChild, level: level + 1)
301301
print("")
@@ -310,7 +310,7 @@ extension AVLTree {
310310
}
311311
}
312312

313-
public func display(node: Node) {
313+
public func display(_ node: Node) {
314314
display(node, level: 0)
315315
print("")
316316
}
@@ -319,7 +319,7 @@ extension AVLTree {
319319
// MARK: - Delete node
320320

321321
extension AVLTree {
322-
public func delete(key: Key) {
322+
public func delete(_ key: Key) {
323323
if size == 1 {
324324
root = nil
325325
size -= 1
@@ -329,7 +329,7 @@ extension AVLTree {
329329
}
330330
}
331331

332-
private func delete(node: Node) {
332+
fileprivate func delete(_ node: Node) {
333333
if node.isLeaf {
334334
// Just remove and balance up
335335
if let parent = node.parent {
@@ -351,11 +351,11 @@ extension AVLTree {
351351
}
352352
} else {
353353
// Handle stem cases
354-
if let replacement = node.leftChild?.maximum() where replacement !== node {
354+
if let replacement = node.leftChild?.maximum() , replacement !== node {
355355
node.key = replacement.key
356356
node.payload = replacement.payload
357357
delete(replacement)
358-
} else if let replacement = node.rightChild?.minimum() where replacement !== node {
358+
} else if let replacement = node.rightChild?.minimum() , replacement !== node {
359359
node.key = replacement.key
360360
node.payload = replacement.payload
361361
delete(replacement)

AVL Tree/Tests/AVLTreeTests.swift

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,25 +67,25 @@ class AVLTreeTests: XCTestCase {
6767
}
6868

6969
func testSingleInsertionPerformance() {
70-
self.measureBlock {
70+
self.measure {
7171
self.tree?.insert(5, "E")
7272
}
7373
}
7474

7575
func testMultipleInsertionsPerformance() {
76-
self.measureBlock {
76+
self.measure {
7777
self.tree?.autopopulateWithNodes(50)
7878
}
7979
}
8080

8181
func testSearchExistentOnSmallTreePerformance() {
82-
self.measureBlock {
82+
self.measure {
8383
self.tree?.search(2)
8484
}
8585
}
8686

8787
func testSearchExistentElementOnLargeTreePerformance() {
88-
self.measureBlock {
88+
self.measure {
8989
self.tree?.autopopulateWithNodes(500)
9090
self.tree?.search(400)
9191
}
@@ -150,8 +150,8 @@ class AVLTreeTests: XCTestCase {
150150
}
151151
}
152152

153-
extension AVLTree where Key : SignedIntegerType {
154-
func autopopulateWithNodes(count: Int) {
153+
extension AVLTree where Key : SignedInteger {
154+
func autopopulateWithNodes(_ count: Int) {
155155
var k: Key = 1
156156
for _ in 0...count {
157157
self.insert(k)
@@ -160,12 +160,12 @@ extension AVLTree where Key : SignedIntegerType {
160160
}
161161
}
162162

163-
enum AVLTreeError: ErrorType {
164-
case NotBalanced
163+
enum AVLTreeError: Error {
164+
case notBalanced
165165
}
166166

167-
extension AVLTree where Key : SignedIntegerType {
168-
func height(node: Node?) -> Int {
167+
extension AVLTree where Key : SignedInteger {
168+
func height(_ node: Node?) -> Int {
169169
if let node = node {
170170
let lHeight = height(node.leftChild)
171171
let rHeight = height(node.rightChild)
@@ -175,10 +175,10 @@ extension AVLTree where Key : SignedIntegerType {
175175
return 0
176176
}
177177

178-
func inOrderCheckBalanced(node: Node?) throws {
178+
func inOrderCheckBalanced(_ node: Node?) throws {
179179
if let node = node {
180180
guard abs(height(node.leftChild) - height(node.rightChild)) <= 1 else {
181-
throw AVLTreeError.NotBalanced
181+
throw AVLTreeError.notBalanced
182182
}
183183
try inOrderCheckBalanced(node.leftChild)
184184
try inOrderCheckBalanced(node.rightChild)

AVL Tree/Tests/Tests.xcodeproj/project.pbxproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
TargetAttributes = {
9292
7B2BBC7F1C779D720067B71D = {
9393
CreatedOnToolsVersion = 7.2;
94+
LastSwiftMigration = 0800;
9495
};
9596
};
9697
};
@@ -224,6 +225,7 @@
224225
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
225226
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
226227
PRODUCT_NAME = "$(TARGET_NAME)";
228+
SWIFT_VERSION = 3.0;
227229
};
228230
name = Debug;
229231
};
@@ -235,6 +237,7 @@
235237
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
236238
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
237239
PRODUCT_NAME = "$(TARGET_NAME)";
240+
SWIFT_VERSION = 3.0;
238241
};
239242
name = Release;
240243
};

0 commit comments

Comments
 (0)