Skip to content

Commit 67a2659

Browse files
committed
Migrated Topological Sort to Swift 3
1 parent bad00fe commit 67a2659

File tree

10 files changed

+30
-22
lines changed

10 files changed

+30
-22
lines changed

.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ script:
3838
# - xcodebuild test -project ./Shortest\ Path\ \(Unweighted\)/Tests/Tests.xcodeproj -scheme Tests
3939
# - xcodebuild test -project ./Single-Source\ Shortest\ Paths\ \(Weighted\)/SSSP.xcodeproj -scheme SSSPTests
4040
- xcodebuild test -project ./Stack/Tests/Tests.xcodeproj -scheme Tests
41-
# - xcodebuild test -project ./Topological\ Sort/Tests/Tests.xcodeproj -scheme Tests
41+
- xcodebuild test -project ./Topological\ Sort/Tests/Tests.xcodeproj -scheme Tests

Topological Sort/Graph.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Graph: CustomStringConvertible {
77
adjacencyLists = [Node : [Node]]()
88
}
99

10-
public func addNode(value: Node) -> Node {
10+
public func addNode(_ value: Node) -> Node {
1111
adjacencyLists[value] = []
1212
return value
1313
}

Topological Sort/Tests/Tests.xcodeproj/project.pbxproj

+9-1
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,12 @@
9292
isa = PBXProject;
9393
attributes = {
9494
LastSwiftUpdateCheck = 0720;
95-
LastUpgradeCheck = 0720;
95+
LastUpgradeCheck = 0800;
9696
ORGANIZATIONNAME = "Swift Algorithm Club";
9797
TargetAttributes = {
9898
7B2BBC7F1C779D720067B71D = {
9999
CreatedOnToolsVersion = 7.2;
100+
LastSwiftMigration = 0800;
100101
};
101102
};
102103
};
@@ -157,8 +158,10 @@
157158
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
158159
CLANG_WARN_EMPTY_BODY = YES;
159160
CLANG_WARN_ENUM_CONVERSION = YES;
161+
CLANG_WARN_INFINITE_RECURSION = YES;
160162
CLANG_WARN_INT_CONVERSION = YES;
161163
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
164+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
162165
CLANG_WARN_UNREACHABLE_CODE = YES;
163166
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
164167
CODE_SIGN_IDENTITY = "-";
@@ -201,8 +204,10 @@
201204
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
202205
CLANG_WARN_EMPTY_BODY = YES;
203206
CLANG_WARN_ENUM_CONVERSION = YES;
207+
CLANG_WARN_INFINITE_RECURSION = YES;
204208
CLANG_WARN_INT_CONVERSION = YES;
205209
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
210+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
206211
CLANG_WARN_UNREACHABLE_CODE = YES;
207212
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
208213
CODE_SIGN_IDENTITY = "-";
@@ -221,6 +226,7 @@
221226
MACOSX_DEPLOYMENT_TARGET = 10.11;
222227
MTL_ENABLE_DEBUG_INFO = NO;
223228
SDKROOT = macosx;
229+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
224230
};
225231
name = Release;
226232
};
@@ -232,6 +238,7 @@
232238
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
233239
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
234240
PRODUCT_NAME = "$(TARGET_NAME)";
241+
SWIFT_VERSION = 3.0;
235242
};
236243
name = Debug;
237244
};
@@ -243,6 +250,7 @@
243250
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks @loader_path/../Frameworks";
244251
PRODUCT_BUNDLE_IDENTIFIER = swift.algorithm.club.Tests;
245252
PRODUCT_NAME = "$(TARGET_NAME)";
253+
SWIFT_VERSION = 3.0;
246254
};
247255
name = Release;
248256
};

Topological Sort/Tests/Tests.xcodeproj/xcshareddata/xcschemes/Tests.xcscheme

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Scheme
3-
LastUpgradeVersion = "0720"
3+
LastUpgradeVersion = "0800"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Topological Sort/Tests/TopologicalSortTests.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import Foundation
22
import XCTest
33

44
extension Graph {
5-
public func loadEdgeList(lines: [String]) {
5+
public func loadEdgeList(_ lines: [String]) {
66
for line in lines {
7-
let items = line.componentsSeparatedByString(" ").filter { s in !s.isEmpty }
7+
let items = line.components(separatedBy: " ").filter { s in !s.isEmpty }
88
if adjacencyList(forNode: items[0]) == nil {
99
addNode(items[0])
1010
}
@@ -20,10 +20,10 @@ class TopologicalSort: XCTestCase {
2020

2121
// The topological sort is valid if a node does not have any of its
2222
// predecessors in its adjacency list.
23-
func checkIsValidTopologicalSort(graph: Graph, _ a: [Graph.Node]) {
24-
for i in (a.count - 1).stride(to: 0, by: -1) {
23+
func checkIsValidTopologicalSort(_ graph: Graph, _ a: [Graph.Node]) {
24+
for i in stride(from: (a.count - 1), to: 0, by: -1) {
2525
if let neighbors = graph.adjacencyList(forNode: a[i]) {
26-
for j in (i - 1).stride(through: 0, by: -1) {
26+
for j in stride(from: (i - 1), through: 0, by: -1) {
2727
XCTAssertFalse(neighbors.contains(a[j]), "\(a) is not a valid topological sort")
2828
}
2929
}

Topological Sort/Topological Sort.playground/Sources/Graph.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Graph: CustomStringConvertible {
77
adjacencyLists = [Node : [Node]]()
88
}
99

10-
public func addNode(value: Node) -> Node {
10+
public func addNode(_ value: Node) -> Node {
1111
adjacencyLists[value] = []
1212
return value
1313
}

Topological Sort/Topological Sort.playground/Sources/TopologicalSort1.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
extension Graph {
2-
private func depthFirstSearch(source: Node, inout visited: [Node : Bool]) -> [Node] {
2+
private func depthFirstSearch(_ source: Node, visited: inout [Node : Bool]) -> [Node] {
33
var result = [Node]()
44

55
if let adjacencyList = adjacencyList(forNode: source) {
66
for nodeInAdjacencyList in adjacencyList {
7-
if let seen = visited[nodeInAdjacencyList] where !seen {
7+
if let seen = visited[nodeInAdjacencyList], !seen {
88
result = depthFirstSearch(nodeInAdjacencyList, visited: &visited) + result
99
}
1010
}

Topological Sort/Topological Sort.playground/Sources/TopologicalSort3.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ extension Graph {
1212
visited[node] = false
1313
}
1414

15-
func depthFirstSearch(source: Node) {
15+
func depthFirstSearch(_ source: Node) {
1616
if let adjacencyList = adjacencyList(forNode: source) {
1717
for neighbor in adjacencyList {
18-
if let seen = visited[neighbor] where !seen {
18+
if let seen = visited[neighbor], !seen {
1919
depthFirstSearch(neighbor)
2020
}
2121
}
@@ -25,11 +25,11 @@ extension Graph {
2525
}
2626

2727
for (node, _) in visited {
28-
if let seen = visited[node] where !seen {
28+
if let seen = visited[node], !seen {
2929
depthFirstSearch(node)
3030
}
3131
}
3232

33-
return stack.reverse()
33+
return stack.reversed()
3434
}
3535
}

Topological Sort/TopologicalSort1.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
extension Graph {
2-
private func depthFirstSearch(source: Node, inout visited: [Node : Bool]) -> [Node] {
2+
private func depthFirstSearch(_ source: Node, visited: inout [Node : Bool]) -> [Node] {
33
var result = [Node]()
44

55
if let adjacencyList = adjacencyList(forNode: source) {
66
for nodeInAdjacencyList in adjacencyList {
7-
if let seen = visited[nodeInAdjacencyList] where !seen {
7+
if let seen = visited[nodeInAdjacencyList], !seen {
88
result = depthFirstSearch(nodeInAdjacencyList, visited: &visited) + result
99
}
1010
}

Topological Sort/TopologicalSort3.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ extension Graph {
1212
visited[node] = false
1313
}
1414

15-
func depthFirstSearch(source: Node) {
15+
func depthFirstSearch(_ source: Node) {
1616
if let adjacencyList = adjacencyList(forNode: source) {
1717
for neighbor in adjacencyList {
18-
if let seen = visited[neighbor] where !seen {
18+
if let seen = visited[neighbor], !seen {
1919
depthFirstSearch(neighbor)
2020
}
2121
}
@@ -25,11 +25,11 @@ extension Graph {
2525
}
2626

2727
for (node, _) in visited {
28-
if let seen = visited[node] where !seen {
28+
if let seen = visited[node], !seen {
2929
depthFirstSearch(node)
3030
}
3131
}
3232

33-
return stack.reverse()
33+
return stack.reversed()
3434
}
3535
}

0 commit comments

Comments
 (0)