Skip to content

Commit fca4f76

Browse files
committed
Swift 4 features code complete..
- implemented subscripts for LinkedLists and Tries. - removed numerous references to IUO’s. - refactored various unit tests to support subscripts and regular optionals.
1 parent 106cce8 commit fca4f76

File tree

14 files changed

+140
-175
lines changed

14 files changed

+140
-175
lines changed

Source/Factories/BSTree.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ class BSTree<T: Comparable>{
9090
}
9191

9292

93-
//equality test
93+
//equality test - O(log n)
9494
func contains(_ key: T) -> Bool {
9595

9696
var current: BSNode<T>? = root
@@ -194,6 +194,7 @@ class BSTree<T: Comparable>{
194194
//MARK: Balancing Methods
195195

196196

197+
197198
//determine if the tree is "balanced" - operations on a balanced tree is O(log n)
198199
func isTreeBalanced(for element: BSNode<T>?) -> Bool {
199200

Source/Factories/LinkedList.swift

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// SwiftFactory.swift
2+
// LinkedList.swift
33
// SwiftStructures
44
//
55
// Created by Wayne Bishop on 6/7/14.
@@ -9,42 +9,32 @@
99
import Foundation
1010

1111

12-
public class LinkedList<T: Equatable> {
12+
class LinkedList<T: Equatable> {
1313

1414

1515
//new instance
1616
private var head = LLNode<T>()
17+
private var counter: Int = 0
1718

1819

19-
20-
var count: Int {
21-
22-
if head.key == nil {
23-
return 0
24-
}
25-
26-
else {
27-
28-
var current: LLNode = head
29-
var x: Int = 1
30-
31-
32-
//cycle through the list of items
33-
while current.next != nil {
34-
current = current.next!
35-
x += 1
36-
}
37-
38-
return x
39-
40-
}
20+
21+
//the number of items - O(1)
22+
var count: Int {
23+
return counter
4124
}
25+
4226

27+
//find subscript shortcut
28+
subscript(index: Int) -> LLNode<T>? {
29+
get {
30+
return find(at: index)
31+
}
32+
}
4333

4434

4535
//empty list check
46-
func isEmpty() -> Bool! {
47-
return self.count == 0 || head.key == nil
36+
func isEmpty() -> Bool {
37+
return counter == 0 || head.key == nil
4838
}
4939

5040

@@ -56,6 +46,7 @@ public class LinkedList<T: Equatable> {
5646
//trivial check
5747
guard head.key != nil else {
5848
head.key = key
49+
counter += 1
5950
return
6051
}
6152

@@ -82,6 +73,7 @@ public class LinkedList<T: Equatable> {
8273

8374
} //end while
8475

76+
counter += 1
8577
}
8678

8779

@@ -107,7 +99,7 @@ public class LinkedList<T: Equatable> {
10799

108100

109101
//obtain link at a specific index
110-
func find(at index: Int) ->LLNode<T>! {
102+
func find(at index: Int) ->LLNode<T>? {
111103

112104

113105
//check empty conditions
@@ -117,13 +109,13 @@ public class LinkedList<T: Equatable> {
117109

118110

119111
else {
120-
var current: LLNode<T>! = head
112+
var current: LLNode<T> = head
121113
var x: Int = 0
122114

123115

124116
//cycle through elements
125117
while (index != x) {
126-
current = current.next
118+
current = current.next!
127119
x += 1
128120
}
129121

@@ -150,6 +142,7 @@ public class LinkedList<T: Equatable> {
150142
//establish the head node
151143
guard head.key != nil else {
152144
head.key = key
145+
counter += 1
153146
return
154147
}
155148

@@ -206,6 +199,8 @@ public class LinkedList<T: Equatable> {
206199

207200
} //end while
208201

202+
counter += 1
203+
209204
}
210205

211206

@@ -253,6 +248,7 @@ public class LinkedList<T: Equatable> {
253248

254249
} //end while
255250

251+
counter -= 1
256252

257253
} //end function
258254

@@ -325,8 +321,7 @@ public class LinkedList<T: Equatable> {
325321
if formula(current) == true {
326322
results.append(element: current.key)
327323
}
328-
329-
324+
330325
current = current.next
331326
}
332327

Source/Factories/Queue.swift

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@
88

99
import Foundation
1010

11-
public class Queue<T> {
12-
13-
14-
private var top: Node<T>!
15-
11+
class Queue<T> {
12+
13+
private var top: Node<T>?
14+
private var counter: Int = 0
1615

1716
init() {
1817
top = Node<T>()
@@ -21,24 +20,7 @@ public class Queue<T> {
2120

2221
//the number of items
2322
var count: Int {
24-
25-
guard top.key != nil else {
26-
return 0
27-
}
28-
29-
30-
var current: Node<T> = top
31-
var x: Int = 1
32-
33-
34-
//cycle through items
35-
while current.next != nil {
36-
current = current.next! //TODO: Add guard statement here..
37-
x += 1
38-
}
39-
40-
return x
41-
23+
return counter
4224
}
4325

4426

@@ -47,21 +29,19 @@ public class Queue<T> {
4729

4830

4931
//retrieve the top most item
50-
func peek() -> T! {
51-
return top.key
32+
func peek() -> T? {
33+
return top?.key
5234
}
5335

5436

5537

5638
//check for the presence of a value
5739
func isEmpty() -> Bool {
5840

59-
guard top.key != nil else {
41+
guard top?.key != nil else {
6042
return true
6143
}
62-
6344
return false
64-
6545
}
6646

6747

@@ -74,8 +54,9 @@ public class Queue<T> {
7454

7555

7656
//trivial case
77-
guard top.key != nil else {
78-
top.key = key
57+
guard top?.key != nil else {
58+
top?.key = key
59+
counter += 1
7960
return
8061
}
8162

@@ -92,7 +73,7 @@ public class Queue<T> {
9273
//append new item
9374
childToUse.key = key
9475
current?.next = childToUse
95-
76+
counter += 1
9677
}
9778

9879

@@ -102,23 +83,24 @@ public class Queue<T> {
10283

10384

10485
//determine key instance
105-
guard top.key != nil else {
86+
guard top?.key != nil else {
10687
return nil
10788
}
10889

109-
90+
11091
//retrieve and queue the next item
111-
let queueItem: T? = top.key
92+
let queueItem: T? = top?.key
11293

11394

11495
//use optional binding
115-
if let nextItem = top.next {
96+
if let nextItem = top?.next {
11697
top = nextItem
11798
}
11899
else {
119100
top = Node<T>()
120101
}
121102

103+
counter -= 1
122104

123105
return queueItem
124106

Source/Factories/Stack.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Stack<T> {
5959
func pop() {
6060

6161
if self.count > 1 {
62-
top = top.next! //TODO: Add guard statement here..
62+
top = top.next!
6363

6464
//set counter
6565
counter -= 1

Source/Factories/Trie.swift

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ import Foundation
1111

1212
public class Trie {
1313

14-
private var root: TrieNode!
14+
private var root = TrieNode()
1515

1616

17-
init(){
18-
root = TrieNode()
17+
//find subscript shortcut
18+
subscript(word: String) -> Array<String>? {
19+
get {
20+
return find(word)
21+
}
1922
}
2023

2124

2225

26+
2327
//builds a tree hierarchy of dictionary content
2428
func append(word keyword: String) {
2529

@@ -84,7 +88,7 @@ public class Trie {
8488

8589

8690
//find words based on the prefix
87-
func find(_ keyword: String) -> Array<String>! {
91+
func find(_ keyword: String) -> Array<String>? {
8892

8993

9094
//trivial case
@@ -129,15 +133,15 @@ public class Trie {
129133

130134
//retrieve the keyword and any descendants
131135
if ((current.key == keyword) && (current.isFinal)) {
132-
wordList.append(current.key)
136+
wordList.append(current.key!)
133137
}
134138

135139

136140
//include only children that are words
137141
for child in current.children {
138142

139143
if (child.isFinal == true) {
140-
wordList.append(child.key)
144+
wordList.append(child.key!)
141145
}
142146

143147
}

Source/Structures/TrieNode.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import Foundation
1010

1111

1212
public class TrieNode {
13-
14-
var key: String!
13+
14+
var key: String?
1515
var children: Array<TrieNode>
1616
var isFinal: Bool
1717
var level: Int

SwiftStructures.xcodeproj/xcuserdata/waynebishop.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,22 @@
22
<Bucket
33
type = "1"
44
version = "2.0">
5+
<Breakpoints>
6+
<BreakpointProxy
7+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
8+
<BreakpointContent
9+
shouldBeEnabled = "No"
10+
ignoreCount = "0"
11+
continueAfterRunningActions = "No"
12+
filePath = "SwiftTests/TrieTest.swift"
13+
timestampString = "527809210.612208"
14+
startingColumnNumber = "9223372036854775807"
15+
endingColumnNumber = "9223372036854775807"
16+
startingLineNumber = "75"
17+
endingLineNumber = "75"
18+
landmarkName = "testFindNoExist()"
19+
landmarkType = "7">
20+
</BreakpointContent>
21+
</BreakpointProxy>
22+
</Breakpoints>
523
</Bucket>

0 commit comments

Comments
 (0)