Skip to content

Commit f3c48d1

Browse files
committed
General refectoring..
- Revised LinkList.remove(at:) algorithm - Renamed methods for HashTable algorithm and Keyable Protocol - Revised content for Stacks & Queues - Revised content for Basic Sorting
1 parent 369d32b commit f3c48d1

File tree

9 files changed

+145
-29
lines changed

9 files changed

+145
-29
lines changed

Source/Extensions/Keyable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extension Keyable {
1515

1616

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

2020

2121
var remainder: Int = 0

Source/Extensions/String.swift

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,6 @@ extension String: Keyable {
9292
}
9393

9494

95-
//convert a string into a character array
96-
func characters() ->Array<Character> {
97-
return Array(self.characters)
98-
}
99-
100-
10195

10296
//reverse string order
10397
func reverse() -> String! {
@@ -108,7 +102,7 @@ extension String: Keyable {
108102
*/
109103

110104
//convert to array
111-
var characters = self.characters()
105+
var characters = Array(self)
112106

113107
var findex: Int = characters.startIndex
114108
var bindex: Int = characters.endIndex - 1

Source/Factories/Graph.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// GraphFactory.swift
2+
// Graph.swift
33
// SwiftStructures
44
//
55
// Created by Wayne Bishop on 6/7/14.
@@ -25,8 +25,7 @@ public class SwiftGraph {
2525

2626
//create a new vertex
2727
func addVertex(key: String) -> Vertex {
28-
29-
28+
3029
//set the key
3130
let childVertex: Vertex = Vertex()
3231
childVertex.key = key

Source/Factories/HashTable.swift

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,23 @@ class HashTable<T: Keyable> {
1919

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

22-
22+
/*
23+
note: initializing the hash table with an initial capactiyis shown
24+
for educational purposes only.
25+
*/
2326
init(capacity: Int) {
2427
self.buckets = Array<Node<T>?>(repeatElement(nil, count: capacity))
2528
}
2629

2730

2831
//add item to list
29-
func append(_ element: T) -> Result {
32+
func insert(_ element: T) -> Result {
3033

3134
let result: Result
3235

3336

3437
//compute hash
35-
let hashIndex = element.hashindex(for: element.keystring, using: buckets)
38+
let hashIndex = element.hashValue(for: element.keystring, using: buckets)
3639

3740

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

8083

8184
//obtain hash index
82-
let hashIndex = element.hashindex(for: element.keystring, using: buckets)
85+
let hashIndex = element.hashValue(for: element.keystring, using: buckets)
8386

8487

8588
guard hashIndex != -1 else {

Source/Factories/LinkedList.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import Foundation
1010

1111

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

1414

1515
//new instance
@@ -39,6 +39,7 @@ class LinkedList<T: Equatable> {
3939

4040

4141

42+
4243
//add link item
4344
func append(element key: T) {
4445

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

85+
//a dictionary - O(1)
86+
8487
var current: LLNode? = head
8588

8689
print("------------------")
@@ -222,7 +225,11 @@ class LinkedList<T: Equatable> {
222225
//determine if the removal is at the head
223226
if (index == 0) {
224227
current = current?.next
225-
head = current!
228+
229+
if let headitem = current {
230+
head = headitem
231+
counter -= 1
232+
}
226233
return
227234
}
228235

Source/Structures/Protocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,6 @@ protocol Keyable {
2323
var keystring: String {get}
2424

2525

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

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

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,117 @@
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/HashTest.swift"
13+
timestampString = "532990375.950905"
14+
startingColumnNumber = "9223372036854775807"
15+
endingColumnNumber = "9223372036854775807"
16+
startingLineNumber = "79"
17+
endingLineNumber = "79"
18+
landmarkName = "testMissingList()"
19+
landmarkType = "7">
20+
<Locations>
21+
<Location
22+
shouldBeEnabled = "No"
23+
ignoreCount = "0"
24+
continueAfterRunningActions = "No"
25+
symbolName = "SwiftTests.HashTest.testMissingList() -&gt; ()"
26+
moduleName = "SwiftTests"
27+
usesParentBreakpointCondition = "Yes"
28+
urlString = "file:///Users/waynebishop/Dropbox/iPhone%20Development/SwiftStructures/SwiftTests/HashTest.swift"
29+
timestampString = "533859967.12933"
30+
startingColumnNumber = "9223372036854775807"
31+
endingColumnNumber = "9223372036854775807"
32+
startingLineNumber = "79"
33+
endingLineNumber = "79"
34+
offsetFromSymbolStart = "131">
35+
</Location>
36+
<Location
37+
shouldBeEnabled = "No"
38+
ignoreCount = "0"
39+
continueAfterRunningActions = "No"
40+
symbolName = "implicit closure #1 : @autoclosure () throws -&gt; Swift.Bool in SwiftTests.HashTest.testMissingList() -&gt; ()"
41+
moduleName = "SwiftTests"
42+
usesParentBreakpointCondition = "Yes"
43+
urlString = "file:///Users/waynebishop/Dropbox/iPhone%20Development/SwiftStructures/SwiftTests/HashTest.swift"
44+
timestampString = "533859967.132433"
45+
startingColumnNumber = "9223372036854775807"
46+
endingColumnNumber = "9223372036854775807"
47+
startingLineNumber = "79"
48+
endingLineNumber = "79"
49+
offsetFromSymbolStart = "14">
50+
</Location>
51+
<Location
52+
shouldBeEnabled = "No"
53+
ignoreCount = "0"
54+
continueAfterRunningActions = "No"
55+
symbolName = "implicit closure #2 : @autoclosure () -&gt; Swift.String in SwiftTests.HashTest.testMissingList() -&gt; ()"
56+
moduleName = "SwiftTests"
57+
usesParentBreakpointCondition = "Yes"
58+
urlString = "file:///Users/waynebishop/Dropbox/iPhone%20Development/SwiftStructures/SwiftTests/HashTest.swift"
59+
timestampString = "533859967.135077"
60+
startingColumnNumber = "9223372036854775807"
61+
endingColumnNumber = "9223372036854775807"
62+
startingLineNumber = "79"
63+
endingLineNumber = "79"
64+
offsetFromSymbolStart = "4">
65+
</Location>
66+
</Locations>
67+
</BreakpointContent>
68+
</BreakpointProxy>
69+
<BreakpointProxy
70+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
71+
<BreakpointContent
72+
shouldBeEnabled = "No"
73+
ignoreCount = "0"
74+
continueAfterRunningActions = "No"
75+
filePath = "Source/Factories/BSTree.swift"
76+
timestampString = "533860009.986163"
77+
startingColumnNumber = "9223372036854775807"
78+
endingColumnNumber = "9223372036854775807"
79+
startingLineNumber = "43"
80+
endingLineNumber = "43"
81+
landmarkName = "append(element:)"
82+
landmarkType = "7">
83+
</BreakpointContent>
84+
</BreakpointProxy>
85+
<BreakpointProxy
86+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
87+
<BreakpointContent
88+
shouldBeEnabled = "No"
89+
ignoreCount = "0"
90+
continueAfterRunningActions = "No"
91+
filePath = "Source/Factories/BSTree.swift"
92+
timestampString = "533860009.986865"
93+
startingColumnNumber = "9223372036854775807"
94+
endingColumnNumber = "9223372036854775807"
95+
startingLineNumber = "139"
96+
endingLineNumber = "139"
97+
landmarkName = "push(element:)"
98+
landmarkType = "7">
99+
</BreakpointContent>
100+
</BreakpointProxy>
101+
<BreakpointProxy
102+
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
103+
<BreakpointContent
104+
shouldBeEnabled = "No"
105+
ignoreCount = "0"
106+
continueAfterRunningActions = "No"
107+
filePath = "Source/Extensions/Int.swift"
108+
timestampString = "535328261.598035"
109+
startingColumnNumber = "9223372036854775807"
110+
endingColumnNumber = "9223372036854775807"
111+
startingLineNumber = "25"
112+
endingLineNumber = "25"
113+
landmarkName = "unknown"
114+
landmarkType = "0">
115+
</BreakpointContent>
116+
</BreakpointProxy>
117+
</Breakpoints>
5118
</Bucket>

SwiftTests/HashTest.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,14 @@ class HashTest: XCTestCase {
3333
let slist = HashTable<String>(capacity: 3)
3434

3535

36-
_ = slist.append("Wayne Bishop")
37-
_ = slist.append("Frank Smith")
38-
_ = slist.append("Jennifer Hobbs")
39-
_ = slist.append("Tim Cook")
40-
_ = slist.append("Steve Jobs")
41-
_ = slist.append("Wayne Bishop") //should produce collision
42-
_ = slist.append("Larry Page")
43-
_ = slist.append("Albert Einstien")
36+
_ = slist.insert("Wayne Bishop")
37+
_ = slist.insert("Frank Smith")
38+
_ = slist.insert("Jennifer Hobbs")
39+
_ = slist.insert("Tim Cook")
40+
_ = slist.insert("Steve Jobs")
41+
_ = slist.insert("Wayne Bishop") //should produce collision
42+
_ = slist.insert("Larry Page")
43+
_ = slist.insert("Albert Einstien")
4444

4545

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

5858
let vList: HashTable = HashTable<Vertex>(capacity: 10)
59-
_ = vList.append(testVertex)
59+
_ = vList.insert(testVertex)
6060

6161

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

75-
_ = fList.append(10.2)
76-
_ = fList.append(8.6)
75+
_ = fList.insert(10.2)
76+
_ = fList.insert(8.6)
7777

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

0 commit comments

Comments
 (0)