Skip to content

Commit 07dc70e

Browse files
author
Peter Bødskov
committed
Migrates App-Pairs Shortest Paths to Swift3 syntax
1 parent 11566eb commit 07dc70e

File tree

8 files changed

+38
-26
lines changed

8 files changed

+38
-26
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ install:
88

99
script:
1010

11-
# - xcodebuild test -project ./All-Pairs\ Shortest\ Paths/APSP/APSP.xcodeproj -scheme APSPTests
11+
- xcodebuild test -project ./All-Pairs\ Shortest\ Paths/APSP/APSP.xcodeproj -scheme APSPTests
1212
- xcodebuild test -project ./Array2D/Tests/Tests.xcodeproj -scheme Tests
1313
- xcodebuild test -project ./AVL\ Tree/Tests/Tests.xcodeproj -scheme Tests
1414
- xcodebuild test -project ./Binary\ Search/Tests/Tests.xcodeproj -scheme Tests

All-Pairs Shortest Paths/APSP/APSP.xcodeproj/project.pbxproj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,16 @@
187187
isa = PBXProject;
188188
attributes = {
189189
LastSwiftUpdateCheck = 0730;
190-
LastUpgradeCheck = 0730;
190+
LastUpgradeCheck = 0820;
191191
ORGANIZATIONNAME = "Swift Algorithm Club";
192192
TargetAttributes = {
193193
493D8DDF1CDD2A1C0089795A = {
194194
CreatedOnToolsVersion = 7.3;
195+
LastSwiftMigration = 0820;
195196
};
196197
493D8DF01CDD5B960089795A = {
197198
CreatedOnToolsVersion = 7.3;
199+
LastSwiftMigration = 0820;
198200
};
199201
};
200202
};
@@ -305,8 +307,10 @@
305307
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
306308
CLANG_WARN_EMPTY_BODY = YES;
307309
CLANG_WARN_ENUM_CONVERSION = YES;
310+
CLANG_WARN_INFINITE_RECURSION = YES;
308311
CLANG_WARN_INT_CONVERSION = YES;
309312
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
313+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
310314
CLANG_WARN_UNREACHABLE_CODE = YES;
311315
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
312316
CODE_SIGN_IDENTITY = "-";
@@ -350,8 +354,10 @@
350354
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
351355
CLANG_WARN_EMPTY_BODY = YES;
352356
CLANG_WARN_ENUM_CONVERSION = YES;
357+
CLANG_WARN_INFINITE_RECURSION = YES;
353358
CLANG_WARN_INT_CONVERSION = YES;
354359
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
360+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
355361
CLANG_WARN_UNREACHABLE_CODE = YES;
356362
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
357363
CODE_SIGN_IDENTITY = "-";
@@ -370,6 +376,7 @@
370376
MACOSX_DEPLOYMENT_TARGET = 10.11;
371377
MTL_ENABLE_DEBUG_INFO = NO;
372378
SDKROOT = macosx;
379+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
373380
};
374381
name = Release;
375382
};
@@ -381,6 +388,7 @@
381388
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
382389
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.APSPTests";
383390
PRODUCT_NAME = "$(TARGET_NAME)";
391+
SWIFT_VERSION = 3.0;
384392
};
385393
name = Debug;
386394
};
@@ -392,6 +400,7 @@
392400
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
393401
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.APSPTests";
394402
PRODUCT_NAME = "$(TARGET_NAME)";
403+
SWIFT_VERSION = 3.0;
395404
};
396405
name = Release;
397406
};
@@ -411,6 +420,7 @@
411420
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.APSP";
412421
PRODUCT_NAME = "$(TARGET_NAME)";
413422
SKIP_INSTALL = YES;
423+
SWIFT_VERSION = 3.0;
414424
VERSIONING_SYSTEM = "apple-generic";
415425
VERSION_INFO_PREFIX = "";
416426
};
@@ -432,6 +442,7 @@
432442
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.APSP";
433443
PRODUCT_NAME = "$(TARGET_NAME)";
434444
SKIP_INSTALL = YES;
445+
SWIFT_VERSION = 3.0;
435446
VERSIONING_SYSTEM = "apple-generic";
436447
VERSION_INFO_PREFIX = "";
437448
};

All-Pairs Shortest Paths/APSP/APSP.xcodeproj/xcshareddata/xcschemes/APSP.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0730"
3+
LastUpgradeVersion = "0820"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

All-Pairs Shortest Paths/APSP/APSP.xcodeproj/xcshareddata/xcschemes/APSPTests.xcscheme

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0730"
3+
LastUpgradeVersion = "0820"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

All-Pairs Shortest Paths/APSP/APSP/APSP.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public protocol APSPAlgorithm {
1818
associatedtype Q: Hashable
1919
associatedtype P: APSPResult
2020

21-
static func apply(graph: AbstractGraph<Q>) -> P
21+
static func apply(_ graph: AbstractGraph<Q>) -> P
2222

2323
}
2424

All-Pairs Shortest Paths/APSP/APSP/FloydWarshall.swift

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private typealias StepResult = (distances: Distances, predecessors: Predecessors
1717

1818
- note: In all complexity bounds, `V` is the number of vertices in the graph, and `E` is the number of edges.
1919
*/
20-
public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
20+
public struct FloydWarshall<T>: APSPAlgorithm where T: Hashable {
2121

2222
public typealias Q = T
2323
public typealias P = FloydWarshallResult<T>
@@ -29,7 +29,7 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
2929
- complexity: `Θ(V^3)` time, `Θ(V^2)` space
3030
- returns a `FloydWarshallResult` struct which can be queried for shortest paths and their total weights
3131
*/
32-
public static func apply<T>(graph: AbstractGraph<T>) -> FloydWarshallResult<T> {
32+
public static func apply<T>(_ graph: AbstractGraph<T>) -> FloydWarshallResult<T> {
3333

3434
var previousDistance = constructInitialDistanceMatrix(graph)
3535
var previousPredecessor = constructInitialPredecessorMatrix(previousDistance)
@@ -59,12 +59,12 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
5959
- returns: a tuple containing the next distance matrix with weights of currently known
6060
shortest paths and the corresponding predecessor matrix
6161
*/
62-
static private func nextStep<T>(intermediateIdx: Int, previousDistances: Distances,
62+
static fileprivate func nextStep<T>(_ intermediateIdx: Int, previousDistances: Distances,
6363
previousPredecessors: Predecessors, graph: AbstractGraph<T>) -> StepResult {
6464

6565
let vertexCount = graph.vertices.count
66-
var nextDistances = Array(count: vertexCount, repeatedValue: Array(count: vertexCount, repeatedValue: Double.infinity))
67-
var nextPredecessors = Array(count: vertexCount, repeatedValue: Array<Int?>(count: vertexCount, repeatedValue: nil))
66+
var nextDistances = Array(repeating: Array(repeating: Double.infinity, count: vertexCount), count: vertexCount)
67+
var nextPredecessors = Array(repeating: Array<Int?>(repeating: nil, count: vertexCount), count: vertexCount)
6868

6969
for fromIdx in 0 ..< vertexCount {
7070
for toIndex in 0 ..< vertexCount {
@@ -97,12 +97,12 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
9797
- complexity: `Θ(V^2)` time/space
9898
- returns: weighted adjacency matrix in form ready for processing with Floyd-Warshall
9999
*/
100-
static private func constructInitialDistanceMatrix<T>(graph: AbstractGraph<T>) -> Distances {
100+
static fileprivate func constructInitialDistanceMatrix<T>(_ graph: AbstractGraph<T>) -> Distances {
101101

102102
let vertices = graph.vertices
103103

104104
let vertexCount = graph.vertices.count
105-
var distances = Array(count: vertexCount, repeatedValue: Array(count: vertexCount, repeatedValue: Double.infinity))
105+
var distances = Array(repeating: Array(repeating: Double.infinity, count: vertexCount), count: vertexCount)
106106

107107
for row in vertices {
108108
for col in vertices {
@@ -125,10 +125,10 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
125125

126126
- complexity: `Θ(V^2)` time/space
127127
*/
128-
static private func constructInitialPredecessorMatrix(distances: Distances) -> Predecessors {
128+
static fileprivate func constructInitialPredecessorMatrix(_ distances: Distances) -> Predecessors {
129129

130130
let vertexCount = distances.count
131-
var predecessors = Array(count: vertexCount, repeatedValue: Array<Int?>(count: vertexCount, repeatedValue: nil))
131+
var predecessors = Array(repeating: Array<Int?>(repeating: nil, count: vertexCount), count: vertexCount)
132132

133133
for fromIdx in 0 ..< vertexCount {
134134
for toIdx in 0 ..< vertexCount {
@@ -151,10 +151,10 @@ public struct FloydWarshall<T where T: Hashable>: APSPAlgorithm {
151151
It conforms to the `APSPResult` procotol which provides methods to retrieve
152152
distances and paths between given pairs of start and end nodes.
153153
*/
154-
public struct FloydWarshallResult<T where T: Hashable>: APSPResult {
154+
public struct FloydWarshallResult<T>: APSPResult where T: Hashable {
155155

156-
private var weights: Distances
157-
private var predecessors: Predecessors
156+
fileprivate var weights: Distances
157+
fileprivate var predecessors: Predecessors
158158

159159
/**
160160
- returns: the total weight of the path from a starting vertex to a destination.
@@ -190,7 +190,7 @@ public struct FloydWarshallResult<T where T: Hashable>: APSPResult {
190190

191191
- returns: the list of predecessors discovered so far
192192
*/
193-
private func recursePathFrom(fromVertex from: Vertex<T>, toVertex to: Vertex<T>, path: [Vertex<T>],
193+
fileprivate func recursePathFrom(fromVertex from: Vertex<T>, toVertex to: Vertex<T>, path: [Vertex<T>],
194194
inGraph graph: AbstractGraph<T>) -> [Vertex<T>]? {
195195

196196
if from.index == to.index {

All-Pairs Shortest Paths/APSP/APSP/Helpers.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77

88
import Foundation
99

10+
1011
/**
1112
Print a matrix, optionally specifying only the cells to display with the triplet (i, j, k) -> matrix[i][j], matrix[i][k], matrix[k][j]
1213
*/
13-
func printMatrix(matrix: [[Double]], i: Int = -1, j: Int = -1, k: Int = -1) {
14+
func printMatrix(_ matrix: [[Double]], i: Int = -1, j: Int = -1, k: Int = -1) {
1415

1516
if i >= 0 {
1617
print(" k: \(k); i: \(i); j: \(j)\n")
@@ -31,12 +32,12 @@ func printMatrix(matrix: [[Double]], i: Int = -1, j: Int = -1, k: Int = -1) {
3132
}
3233
grid.append(row)
3334
}
34-
print((grid as NSArray).componentsJoinedByString("\n"))
35+
print((grid as NSArray).componentsJoined(by: "\n"))
3536
print(" =======================")
3637

3738
}
3839

39-
func printIntMatrix(matrix: [[Int?]]) {
40+
func printIntMatrix(_ matrix: [[Int?]]) {
4041

4142
var grid = [String]()
4243

@@ -46,14 +47,14 @@ func printIntMatrix(matrix: [[Int?]]) {
4647
for y in 0..<n {
4748
if let value = matrix[x][y] {
4849
let valueString = NSString(format: "%i", value)
49-
row += "\(matrix[x][y] >= 0 ? " " : "")\(valueString) "
50+
row += "\(value >= 0 ? " " : "")\(valueString) "
5051
} else {
5152
row += " ø "
5253
}
5354
}
5455
grid.append(row)
5556
}
56-
print((grid as NSArray).componentsJoinedByString("\n"))
57+
print((grid as NSArray).componentsJoined(by: "\n"))
5758
print(" =======================")
5859

5960
}

All-Pairs Shortest Paths/APSP/APSPTests/APSPTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import APSP
99
import Graph
1010
import XCTest
1111

12-
struct TestCase<T where T: Hashable> {
12+
struct TestCase<T> where T: Hashable {
1313

1414
var from: Vertex<T>
1515
var to: Vertex<T>
@@ -71,7 +71,7 @@ class APSPTests: XCTestCase {
7171

7272
for testCase: TestCase<Int> in cases {
7373
if let computedPath = result.path(fromVertex: testCase.from, toVertex: testCase.to, inGraph: graph),
74-
computedDistance = result.distance(fromVertex: testCase.from, toVertex: testCase.to) {
74+
let computedDistance = result.distance(fromVertex: testCase.from, toVertex: testCase.to) {
7575
XCTAssert(computedDistance == testCase.expectedDistance, "expected distance \(testCase.expectedDistance) but got \(computedDistance)")
7676
XCTAssert(computedPath == testCase.expectedPath, "expected path \(testCase.expectedPath) but got \(computedPath)")
7777
}
@@ -111,7 +111,7 @@ class APSPTests: XCTestCase {
111111

112112
for testCase: TestCase<Int> in cases {
113113
if let computedPath = result.path(fromVertex: testCase.from, toVertex: testCase.to, inGraph: graph),
114-
computedDistance = result.distance(fromVertex: testCase.from, toVertex: testCase.to) {
114+
let computedDistance = result.distance(fromVertex: testCase.from, toVertex: testCase.to) {
115115
XCTAssert(computedDistance == testCase.expectedDistance, "expected distance \(testCase.expectedDistance) but got \(computedDistance)")
116116
XCTAssert(computedPath == testCase.expectedPath, "expected path \(testCase.expectedPath) but got \(computedPath)")
117117
}

0 commit comments

Comments
 (0)