Skip to content

Commit e79f2fa

Browse files
authored
Merge pull request kodecocodes#356 from pbodsk/sssp-swift3-migration
migrates Single-Source Shortest Paths to Swift 3 syntax
2 parents 671aa3b + c260a34 commit e79f2fa

File tree

8 files changed

+37
-26
lines changed

8 files changed

+37
-26
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ script:
3939
- xcodebuild test -project ./Selection\ Sort/Tests/Tests.xcodeproj -scheme Tests
4040
# - xcodebuild test -project ./Shell\ Sort/Tests/Tests.xcodeproj -scheme Tests
4141
- xcodebuild test -project ./Shortest\ Path\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
42-
# - xcodebuild test -project ./Single-Source\ Shortest\ Paths\ \(Weighted\)/SSSP.xcodeproj -scheme SSSPTests
42+
- xcodebuild test -project ./Single-Source\ Shortest\ Paths\ \(Weighted\)/SSSP.xcodeproj -scheme SSSPTests
4343
- xcodebuild test -project ./Stack/Tests/Tests.xcodeproj -scheme Tests
4444
- xcodebuild test -project ./Topological\ Sort/Tests/Tests.xcodeproj -scheme Tests
4545
- xcodebuild test -project ./Treap/Treap/Treap.xcodeproj -scheme Tests

Single-Source Shortest Paths (Weighted)/SSSP.playground/Contents.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ graph.addDirectedEdge(z, to: x, withWeight: 7)
2525

2626
let result = BellmanFord<String>.apply(graph, source: s)!
2727

28-
let path = result.path(z, inGraph: graph)
28+
let path = result.path(to: z, inGraph: graph)

Single-Source Shortest Paths (Weighted)/SSSP.xcodeproj/project.pbxproj

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,16 @@
184184
isa = PBXProject;
185185
attributes = {
186186
LastSwiftUpdateCheck = 0730;
187-
LastUpgradeCheck = 0730;
187+
LastUpgradeCheck = 0810;
188188
ORGANIZATIONNAME = "Swift Algorithm Club";
189189
TargetAttributes = {
190190
49BFA2921CDF86E100522D66 = {
191191
CreatedOnToolsVersion = 7.3;
192+
LastSwiftMigration = 0810;
192193
};
193194
49BFA29C1CDF86E100522D66 = {
194195
CreatedOnToolsVersion = 7.3;
196+
LastSwiftMigration = 0810;
195197
};
196198
};
197199
};
@@ -301,8 +303,10 @@
301303
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
302304
CLANG_WARN_EMPTY_BODY = YES;
303305
CLANG_WARN_ENUM_CONVERSION = YES;
306+
CLANG_WARN_INFINITE_RECURSION = YES;
304307
CLANG_WARN_INT_CONVERSION = YES;
305308
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
309+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
306310
CLANG_WARN_UNREACHABLE_CODE = YES;
307311
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
308312
CODE_SIGN_IDENTITY = "-";
@@ -349,8 +353,10 @@
349353
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
350354
CLANG_WARN_EMPTY_BODY = YES;
351355
CLANG_WARN_ENUM_CONVERSION = YES;
356+
CLANG_WARN_INFINITE_RECURSION = YES;
352357
CLANG_WARN_INT_CONVERSION = YES;
353358
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
359+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
354360
CLANG_WARN_UNREACHABLE_CODE = YES;
355361
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
356362
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
VERSIONING_SYSTEM = "apple-generic";
374381
VERSION_INFO_PREFIX = "";
375382
};
@@ -392,6 +399,7 @@
392399
PRODUCT_NAME = "$(TARGET_NAME)";
393400
SKIP_INSTALL = YES;
394401
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
402+
SWIFT_VERSION = 3.0;
395403
};
396404
name = Debug;
397405
};
@@ -411,6 +419,7 @@
411419
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.SSSP";
412420
PRODUCT_NAME = "$(TARGET_NAME)";
413421
SKIP_INSTALL = YES;
422+
SWIFT_VERSION = 3.0;
414423
};
415424
name = Release;
416425
};
@@ -422,6 +431,7 @@
422431
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
423432
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.SSSPTests";
424433
PRODUCT_NAME = "$(TARGET_NAME)";
434+
SWIFT_VERSION = 3.0;
425435
};
426436
name = Debug;
427437
};
@@ -433,6 +443,7 @@
433443
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
434444
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.SSSPTests";
435445
PRODUCT_NAME = "$(TARGET_NAME)";
446+
SWIFT_VERSION = 3.0;
436447
};
437448
name = Release;
438449
};

Single-Source Shortest Paths (Weighted)/SSSP.xcodeproj/xcshareddata/xcschemes/SSSP.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 = "0810"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Single-Source Shortest Paths (Weighted)/SSSP.xcodeproj/xcshareddata/xcschemes/SSSPTests.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 = "0810"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Single-Source Shortest Paths (Weighted)/SSSP/BellmanFord.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import Foundation
99
import Graph
1010

11-
public struct BellmanFord<T where T: Hashable> {
11+
public struct BellmanFord<T> where T: Hashable {
1212
public typealias Q = T
1313
}
1414

@@ -30,12 +30,12 @@ extension BellmanFord: SSSPAlgorithm {
3030
- returns a `BellmanFordResult` struct which can be queried for
3131
shortest paths and their total weights, or `nil` if a negative weight cycle exists
3232
*/
33-
public static func apply(graph: AbstractGraph<T>, source: Vertex<Q>) -> BellmanFordResult<T>? {
33+
public static func apply(_ graph: AbstractGraph<T>, source: Vertex<Q>) -> BellmanFordResult<T>? {
3434
let vertices = graph.vertices
3535
let edges = graph.edges
3636

37-
var predecessors = Array<Int?>(count: vertices.count, repeatedValue: nil)
38-
var weights = Array(count: vertices.count, repeatedValue: Double.infinity)
37+
var predecessors = Array<Int?>(repeating: nil, count: vertices.count)
38+
var weights = Array(repeating: Double.infinity, count: vertices.count)
3939
predecessors[source.index] = source.index
4040
weights[source.index] = 0
4141

@@ -78,10 +78,10 @@ extension BellmanFord: SSSPAlgorithm {
7878
It conforms to the `SSSPResult` procotol which provides methods to
7979
retrieve distances and paths between given pairs of start and end nodes.
8080
*/
81-
public struct BellmanFordResult<T where T: Hashable> {
81+
public struct BellmanFordResult<T> where T: Hashable {
8282

83-
private var predecessors: [Int?]
84-
private var weights: [Double]
83+
fileprivate var predecessors: [Int?]
84+
fileprivate var weights: [Double]
8585

8686
}
8787

@@ -112,7 +112,7 @@ extension BellmanFordResult: SSSPResult {
112112
return nil
113113
}
114114

115-
guard let path = recursePath(to, inGraph: graph, path: [to]) else {
115+
guard let path = recursePath(to: to, inGraph: graph, path: [to]) else {
116116
return nil
117117
}
118118

@@ -126,7 +126,7 @@ extension BellmanFordResult: SSSPResult {
126126

127127
- returns: the list of predecessors discovered so far, or `nil` if the next vertex has no predecessor
128128
*/
129-
private func recursePath(to: Vertex<T>, inGraph graph: AbstractGraph<T>, path: [Vertex<T>]) -> [Vertex<T>]? {
129+
fileprivate func recursePath(to: Vertex<T>, inGraph graph: AbstractGraph<T>, path: [Vertex<T>]) -> [Vertex<T>]? {
130130
guard let predecessorIdx = predecessors[to.index] else {
131131
return nil
132132
}
@@ -136,7 +136,7 @@ extension BellmanFordResult: SSSPResult {
136136
return [ to ]
137137
}
138138

139-
guard let buildPath = recursePath(predecessor, inGraph: graph, path: path) else {
139+
guard let buildPath = recursePath(to: predecessor, inGraph: graph, path: path) else {
140140
return nil
141141
}
142142

Single-Source Shortest Paths (Weighted)/SSSP/SSSP.swift

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

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

2323
}
2424

Single-Source Shortest Paths (Weighted)/SSSPTests/SSSPTests.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ class SSSPTests: XCTestCase {
4040
let result = BellmanFord<String>.apply(graph, source: s)!
4141

4242
let expectedPath = ["s", "y", "x", "t", "z"]
43-
let computedPath = result.path(z, inGraph: graph)!
43+
let computedPath = result.path(to: z, inGraph: graph)!
4444
XCTAssertEqual(expectedPath, computedPath, "expected path of \(expectedPath) but got \(computedPath)")
4545

4646
let expectedWeight = -2.0
47-
let computedWeight = result.distance(z)
47+
let computedWeight = result.distance(to: z)
4848
XCTAssertEqual(expectedWeight, computedWeight, "expected weight of \(expectedWeight) but got \(computedWeight)")
4949
}
5050

@@ -71,11 +71,11 @@ class SSSPTests: XCTestCase {
7171
let result = BellmanFord<String>.apply(graph, source: a)!
7272

7373
let expectedPath = ["a", "b", "c", "e", "d"]
74-
let computedPath = result.path(d, inGraph: graph)!
74+
let computedPath = result.path(to: d, inGraph: graph)!
7575
XCTAssertEqual(expectedPath, computedPath, "expected path of \(expectedPath) but got \(computedPath)")
7676

7777
let expectedWeight = 7.0
78-
let computedWeight = result.distance(d)
78+
let computedWeight = result.distance(to: d)
7979
XCTAssertEqual(expectedWeight, computedWeight, "expected weight of \(expectedWeight) but got \(computedWeight)")
8080
}
8181

@@ -104,12 +104,12 @@ class SSSPTests: XCTestCase {
104104

105105
let result = BellmanFord<String>.apply(graph, source: a)!
106106

107-
XCTAssertNil(result.path(b, inGraph: graph), "a path should not be defined from a ~> b")
108-
XCTAssertNil(result.distance(b), "a path should not be defined from a ~> b")
109-
XCTAssertNotNil(result.path(s, inGraph: graph), "a path should be defined from a ~> s")
110-
XCTAssertNotNil(result.distance(s), "a path should be defined from a ~> s")
111-
XCTAssertNotNil(result.path(t, inGraph: graph), "a path should be defined from a ~> t")
112-
XCTAssertNotNil(result.distance(t), "a path should be defined from a ~> t")
107+
XCTAssertNil(result.path(to: b, inGraph: graph), "a path should not be defined from a ~> b")
108+
XCTAssertNil(result.distance(to: b), "a path should not be defined from a ~> b")
109+
XCTAssertNotNil(result.path(to: s, inGraph: graph), "a path should be defined from a ~> s")
110+
XCTAssertNotNil(result.distance(to: s), "a path should be defined from a ~> s")
111+
XCTAssertNotNil(result.path(to: t, inGraph: graph), "a path should be defined from a ~> t")
112+
XCTAssertNotNil(result.distance(to: t), "a path should be defined from a ~> t")
113113
}
114114

115115
func testNegativeWeightCycle() {

0 commit comments

Comments
 (0)