Skip to content

Commit

Permalink
Swift 4 features code complete..
Browse files Browse the repository at this point in the history
- implemented subscripts for LinkedLists and Tries.
- removed numerous references to IUO’s.
- refactored various unit tests to support subscripts and regular optionals.
  • Loading branch information
waynewbishop committed Sep 22, 2017
1 parent 106cce8 commit fca4f76
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 175 deletions.
3 changes: 2 additions & 1 deletion Source/Factories/BSTree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class BSTree<T: Comparable>{
}


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

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



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

Expand Down
57 changes: 26 additions & 31 deletions Source/Factories/LinkedList.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// SwiftFactory.swift
// LinkedList.swift
// SwiftStructures
//
// Created by Wayne Bishop on 6/7/14.
Expand All @@ -9,42 +9,32 @@
import Foundation


public class LinkedList<T: Equatable> {
class LinkedList<T: Equatable> {


//new instance
private var head = LLNode<T>()
private var counter: Int = 0



var count: Int {

if head.key == nil {
return 0
}

else {

var current: LLNode = head
var x: Int = 1


//cycle through the list of items
while current.next != nil {
current = current.next!
x += 1
}

return x

}

//the number of items - O(1)
var count: Int {
return counter
}


//find subscript shortcut
subscript(index: Int) -> LLNode<T>? {
get {
return find(at: index)
}
}


//empty list check
func isEmpty() -> Bool! {
return self.count == 0 || head.key == nil
func isEmpty() -> Bool {
return counter == 0 || head.key == nil
}


Expand All @@ -56,6 +46,7 @@ public class LinkedList<T: Equatable> {
//trivial check
guard head.key != nil else {
head.key = key
counter += 1
return
}

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

} //end while

counter += 1
}


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


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


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


else {
var current: LLNode<T>! = head
var current: LLNode<T> = head
var x: Int = 0


//cycle through elements
while (index != x) {
current = current.next
current = current.next!
x += 1
}

Expand All @@ -150,6 +142,7 @@ public class LinkedList<T: Equatable> {
//establish the head node
guard head.key != nil else {
head.key = key
counter += 1
return
}

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

} //end while

counter += 1

}


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

} //end while

counter -= 1

} //end function

Expand Down Expand Up @@ -325,8 +321,7 @@ public class LinkedList<T: Equatable> {
if formula(current) == true {
results.append(element: current.key)
}



current = current.next
}

Expand Down
52 changes: 17 additions & 35 deletions Source/Factories/Queue.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@

import Foundation

public class Queue<T> {


private var top: Node<T>!

class Queue<T> {

private var top: Node<T>?
private var counter: Int = 0

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

//the number of items
var count: Int {

guard top.key != nil else {
return 0
}


var current: Node<T> = top
var x: Int = 1


//cycle through items
while current.next != nil {
current = current.next! //TODO: Add guard statement here..
x += 1
}

return x

return counter
}


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


//retrieve the top most item
func peek() -> T! {
return top.key
func peek() -> T? {
return top?.key
}



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

guard top.key != nil else {
guard top?.key != nil else {
return true
}

return false

}


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


//trivial case
guard top.key != nil else {
top.key = key
guard top?.key != nil else {
top?.key = key
counter += 1
return
}

Expand All @@ -92,7 +73,7 @@ public class Queue<T> {
//append new item
childToUse.key = key
current?.next = childToUse

counter += 1
}


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


//determine key instance
guard top.key != nil else {
guard top?.key != nil else {
return nil
}


//retrieve and queue the next item
let queueItem: T? = top.key
let queueItem: T? = top?.key


//use optional binding
if let nextItem = top.next {
if let nextItem = top?.next {
top = nextItem
}
else {
top = Node<T>()
}

counter -= 1

return queueItem

Expand Down
2 changes: 1 addition & 1 deletion Source/Factories/Stack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Stack<T> {
func pop() {

if self.count > 1 {
top = top.next! //TODO: Add guard statement here..
top = top.next!

//set counter
counter -= 1
Expand Down
16 changes: 10 additions & 6 deletions Source/Factories/Trie.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ import Foundation

public class Trie {

private var root: TrieNode!
private var root = TrieNode()


init(){
root = TrieNode()
//find subscript shortcut
subscript(word: String) -> Array<String>? {
get {
return find(word)
}
}




//builds a tree hierarchy of dictionary content
func append(word keyword: String) {

Expand Down Expand Up @@ -84,7 +88,7 @@ public class Trie {


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


//trivial case
Expand Down Expand Up @@ -129,15 +133,15 @@ public class Trie {

//retrieve the keyword and any descendants
if ((current.key == keyword) && (current.isFinal)) {
wordList.append(current.key)
wordList.append(current.key!)
}


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

if (child.isFinal == true) {
wordList.append(child.key)
wordList.append(child.key!)
}

}
Expand Down
4 changes: 2 additions & 2 deletions Source/Structures/TrieNode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import Foundation


public class TrieNode {

var key: String!
var key: String?
var children: Array<TrieNode>
var isFinal: Bool
var level: Int
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,22 @@
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftTests/TrieTest.swift"
timestampString = "527809210.612208"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "75"
endingLineNumber = "75"
landmarkName = "testFindNoExist()"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
Loading

0 comments on commit fca4f76

Please sign in to comment.