Skip to content

Commit

Permalink
General refectoring..
Browse files Browse the repository at this point in the history
- Revised LinkList.remove(at:) algorithm
- Renamed methods for HashTable algorithm and Keyable Protocol
- Revised content for Stacks & Queues
- Revised content for Basic Sorting
  • Loading branch information
waynewbishop committed Jan 9, 2018
1 parent 369d32b commit f3c48d1
Show file tree
Hide file tree
Showing 9 changed files with 145 additions and 29 deletions.
2 changes: 1 addition & 1 deletion Source/Extensions/Keyable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ extension Keyable {


//compute table index
func hashindex<T>(for key: String!, using buckets: Array<T>) -> Int {
func hashValue<T>(for key: String!, using buckets: Array<T>) -> Int {


var remainder: Int = 0
Expand Down
8 changes: 1 addition & 7 deletions Source/Extensions/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,6 @@ extension String: Keyable {
}


//convert a string into a character array
func characters() ->Array<Character> {
return Array(self.characters)
}



//reverse string order
func reverse() -> String! {
Expand All @@ -108,7 +102,7 @@ extension String: Keyable {
*/

//convert to array
var characters = self.characters()
var characters = Array(self)

var findex: Int = characters.startIndex
var bindex: Int = characters.endIndex - 1
Expand Down
5 changes: 2 additions & 3 deletions Source/Factories/Graph.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// GraphFactory.swift
// Graph.swift
// SwiftStructures
//
// Created by Wayne Bishop on 6/7/14.
Expand All @@ -25,8 +25,7 @@ public class SwiftGraph {

//create a new vertex
func addVertex(key: String) -> Vertex {



//set the key
let childVertex: Vertex = Vertex()
childVertex.key = key
Expand Down
11 changes: 7 additions & 4 deletions Source/Factories/HashTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,23 @@ class HashTable<T: Keyable> {

private var buckets: Array<Node<T>?>


/*
note: initializing the hash table with an initial capactiyis shown
for educational purposes only.
*/
init(capacity: Int) {
self.buckets = Array<Node<T>?>(repeatElement(nil, count: capacity))
}


//add item to list
func append(_ element: T) -> Result {
func insert(_ element: T) -> Result {

let result: Result


//compute hash
let hashIndex = element.hashindex(for: element.keystring, using: buckets)
let hashIndex = element.hashValue(for: element.keystring, using: buckets)


if hashIndex != -1 {
Expand Down Expand Up @@ -79,7 +82,7 @@ class HashTable<T: Keyable> {


//obtain hash index
let hashIndex = element.hashindex(for: element.keystring, using: buckets)
let hashIndex = element.hashValue(for: element.keystring, using: buckets)


guard hashIndex != -1 else {
Expand Down
11 changes: 9 additions & 2 deletions Source/Factories/LinkedList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation


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


//new instance
Expand Down Expand Up @@ -39,6 +39,7 @@ class LinkedList<T: Equatable> {




//add link item
func append(element key: T) {

Expand Down Expand Up @@ -81,6 +82,8 @@ class LinkedList<T: Equatable> {
//print all keys for the class
func printAllKeys() {

//a dictionary - O(1)

var current: LLNode? = head

print("------------------")
Expand Down Expand Up @@ -222,7 +225,11 @@ class LinkedList<T: Equatable> {
//determine if the removal is at the head
if (index == 0) {
current = current?.next
head = current!

if let headitem = current {
head = headitem
counter -= 1
}
return
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Structures/Protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ protocol Keyable {
var keystring: String {get}


func hashindex<T>(for key: String!, using buckets: Array<T>) -> Int
func hashValue<T>(for key: String!, using buckets: Array<T>) -> Int
}

Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,117 @@
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "SwiftTests/HashTest.swift"
timestampString = "532990375.950905"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "79"
endingLineNumber = "79"
landmarkName = "testMissingList()"
landmarkType = "7">
<Locations>
<Location
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "SwiftTests.HashTest.testMissingList() -&gt; ()"
moduleName = "SwiftTests"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/waynebishop/Dropbox/iPhone%20Development/SwiftStructures/SwiftTests/HashTest.swift"
timestampString = "533859967.12933"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "79"
endingLineNumber = "79"
offsetFromSymbolStart = "131">
</Location>
<Location
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "implicit closure #1 : @autoclosure () throws -&gt; Swift.Bool in SwiftTests.HashTest.testMissingList() -&gt; ()"
moduleName = "SwiftTests"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/waynebishop/Dropbox/iPhone%20Development/SwiftStructures/SwiftTests/HashTest.swift"
timestampString = "533859967.132433"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "79"
endingLineNumber = "79"
offsetFromSymbolStart = "14">
</Location>
<Location
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
symbolName = "implicit closure #2 : @autoclosure () -&gt; Swift.String in SwiftTests.HashTest.testMissingList() -&gt; ()"
moduleName = "SwiftTests"
usesParentBreakpointCondition = "Yes"
urlString = "file:///Users/waynebishop/Dropbox/iPhone%20Development/SwiftStructures/SwiftTests/HashTest.swift"
timestampString = "533859967.135077"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "79"
endingLineNumber = "79"
offsetFromSymbolStart = "4">
</Location>
</Locations>
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Source/Factories/BSTree.swift"
timestampString = "533860009.986163"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "43"
endingLineNumber = "43"
landmarkName = "append(element:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Source/Factories/BSTree.swift"
timestampString = "533860009.986865"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "139"
endingLineNumber = "139"
landmarkName = "push(element:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Source/Extensions/Int.swift"
timestampString = "535328261.598035"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "25"
endingLineNumber = "25"
landmarkName = "unknown"
landmarkType = "0">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>
22 changes: 11 additions & 11 deletions SwiftTests/HashTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ class HashTest: XCTestCase {
let slist = HashTable<String>(capacity: 3)


_ = slist.append("Wayne Bishop")
_ = slist.append("Frank Smith")
_ = slist.append("Jennifer Hobbs")
_ = slist.append("Tim Cook")
_ = slist.append("Steve Jobs")
_ = slist.append("Wayne Bishop") //should produce collision
_ = slist.append("Larry Page")
_ = slist.append("Albert Einstien")
_ = slist.insert("Wayne Bishop")
_ = slist.insert("Frank Smith")
_ = slist.insert("Jennifer Hobbs")
_ = slist.insert("Tim Cook")
_ = slist.insert("Steve Jobs")
_ = slist.insert("Wayne Bishop") //should produce collision
_ = slist.insert("Larry Page")
_ = slist.insert("Albert Einstien")


//test list compliance
Expand All @@ -56,7 +56,7 @@ class HashTest: XCTestCase {
testVertex.key = "A"

let vList: HashTable = HashTable<Vertex>(capacity: 10)
_ = vList.append(testVertex)
_ = vList.insert(testVertex)


//test list compliance
Expand All @@ -72,8 +72,8 @@ class HashTest: XCTestCase {
//new float list
let fList = HashTable<Double>(capacity: 5)

_ = fList.append(10.2)
_ = fList.append(8.6)
_ = fList.insert(10.2)
_ = fList.insert(8.6)

//element doesn't exist
XCTAssertFalse(fList.contains(3.7), "hash table element not found..")
Expand Down

0 comments on commit f3c48d1

Please sign in to comment.