Skip to content

Commit 971ef14

Browse files
committed
Massive refactor of hash tables..
- Added new support for new Keyable protocol - Cosmetic changes..
1 parent bea4059 commit 971ef14

File tree

16 files changed

+207
-225
lines changed

16 files changed

+207
-225
lines changed

Source/Extensions/Double.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//
2+
// Float.swift
3+
// SwiftStructures
4+
//
5+
// Created by Wayne Bishop on 10/10/17.
6+
// Copyright © 2017 Arbutus Software Inc. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension Double: Keyable {
12+
13+
14+
//hash table requirement
15+
var keyitem: String {
16+
return String(self)
17+
}
18+
19+
}

Source/Extensions/Int.swift

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

1111

12-
extension Int {
12+
extension Int: Keyable {
1313

1414

15+
//hash table requirement
16+
var keyitem: String {
17+
return String(self)
18+
}
19+
20+
1521
//iterates the closure body a specified number of times
1622
func times(closure:(Int)-> Void) {
1723
for i in 0...self {

Source/Extensions/Keyable.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// Keyable.swift
3+
// SwiftStructures
4+
//
5+
// Created by Wayne Bishop on 10/10/17.
6+
// Copyright © 2017 Arbutus Software Inc. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
12+
//note: an extension on a protocol
13+
14+
extension Keyable {
15+
16+
17+
func hashkey<T>(for key: String!, using buckets: Array<T>) -> Int {
18+
19+
20+
var remainder: Int = 0
21+
var divisor: Int = 0
22+
23+
24+
//trivial case
25+
guard key != nil else {
26+
return -1
27+
}
28+
29+
30+
for item in key.unicodeScalars {
31+
divisor += Int(item.value)
32+
}
33+
34+
35+
/*
36+
note: modular math is used to calculate a hash value. The bucket count is used
37+
as the dividend to ensure all possible outcomes are between 0 and the collection size.
38+
*/
39+
40+
remainder = divisor % buckets.count
41+
42+
return remainder
43+
}
44+
45+
}
46+
47+
48+
49+

Source/Extensions/Sortable.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import Foundation
1414
extension Sortable {
1515

1616

17-
1817
func isSorted<T: Comparable>(_ sequence: Array<T>) -> Bool {
1918

2019

Source/Extensions/String.swift

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,22 @@ import Foundation
1010

1111

1212

13-
extension String {
13+
extension String: Keyable {
1414

15+
//hash table requirement
16+
var keyitem: String {
17+
return self
18+
}
19+
1520

1621
//compute the length
1722
var length: Int {
18-
//return self.characters.count - Swift 3.0
19-
return self.count
23+
return self.count
2024
}
2125

22-
23-
2426
//determine if all characters are unique
2527
func isStringUnique() -> Bool {
2628

27-
2829
//evaluate trival case
2930
guard self.characters.count < 128 else {
3031
return false

Source/Factories/Graph.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,6 @@ public class SwiftGraph {
158158
while frontier.count != 0 {
159159

160160
//support path changes using the greedy approach
161-
bestPath = Path()
162161
var pathIndex: Int = 0
163162

164163

@@ -228,7 +227,7 @@ public class SwiftGraph {
228227

229228

230229
///an optimized version of Dijkstra's shortest path algorthim
231-
func processDijkstraWithHeap(_ source: Vertex, destination: Vertex) -> Path! {
230+
func processDijkstraWithHeap(_ source: Vertex, destination: Vertex) -> Path? {
232231

233232

234233
let frontier: PathHeap = PathHeap()
@@ -253,14 +252,12 @@ public class SwiftGraph {
253252

254253

255254
//construct the best path
256-
var bestPath: Path = Path()
257-
258-
259255
while frontier.count != 0 {
260256

261257
//use the greedy approach to obtain the best path
262-
bestPath = Path()
263-
bestPath = frontier.peek()
258+
guard let bestPath: Path = frontier.peek() else {
259+
break
260+
}
264261

265262

266263
//enumerate the bestPath edges
@@ -294,7 +291,7 @@ public class SwiftGraph {
294291

295292

296293
//obtain the shortest path from the heap
297-
var shortestPath: Path! = Path()
294+
var shortestPath: Path? = Path()
298295
shortestPath = finalPaths.peek()
299296

300297

0 commit comments

Comments
 (0)