Skip to content

Commit 6e281b9

Browse files
committed
Migrate Graph to Swift3
1 parent 3960cf5 commit 6e281b9

File tree

10 files changed

+70
-51
lines changed

10 files changed

+70
-51
lines changed

Graph/Graph.playground/Contents.swift

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
import Graph
22

3-
// Create the vertices
4-
var adjacencyMatrixGraph = AdjacencyMatrixGraph<Int>()
5-
var adjacencyListGraph = AdjacencyListGraph<Int>()
6-
7-
for graph in [ adjacencyMatrixGraph, adjacencyListGraph ] {
8-
3+
for graph in [AdjacencyMatrixGraph<Int>(), AdjacencyListGraph<Int>()] {
4+
95
let v1 = graph.createVertex(1)
106
let v2 = graph.createVertex(2)
117
let v3 = graph.createVertex(3)
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Timeline
3+
version = "3.0">
4+
<TimelineItems>
5+
<LoggerValueHistoryTimelineItem
6+
documentLocation = "#CharacterRangeLen=12&amp;CharacterRangeLoc=1238&amp;EndingColumnNumber=15&amp;EndingLineNumber=38&amp;StartingColumnNumber=3&amp;StartingLineNumber=38&amp;Timestamp=498745868.545441"
7+
selectedRepresentationIndex = "1"
8+
shouldTrackSuperviewWidth = "NO">
9+
</LoggerValueHistoryTimelineItem>
10+
</TimelineItems>
11+
</Timeline>

Graph/Graph.xcodeproj/project.pbxproj

+11-1
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,12 @@
158158
isa = PBXProject;
159159
attributes = {
160160
LastSwiftUpdateCheck = 0730;
161-
LastUpgradeCheck = 0730;
161+
LastUpgradeCheck = 0800;
162162
ORGANIZATIONNAME = "Swift Algorithm Club";
163163
TargetAttributes = {
164164
49BFA2FC1CDF886B00522D66 = {
165165
CreatedOnToolsVersion = 7.3;
166+
LastSwiftMigration = 0800;
166167
};
167168
49BFA3061CDF886B00522D66 = {
168169
CreatedOnToolsVersion = 7.3;
@@ -250,8 +251,10 @@
250251
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
251252
CLANG_WARN_EMPTY_BODY = YES;
252253
CLANG_WARN_ENUM_CONVERSION = YES;
254+
CLANG_WARN_INFINITE_RECURSION = YES;
253255
CLANG_WARN_INT_CONVERSION = YES;
254256
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
257+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
255258
CLANG_WARN_UNREACHABLE_CODE = YES;
256259
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
257260
CODE_SIGN_IDENTITY = "-";
@@ -298,8 +301,10 @@
298301
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
299302
CLANG_WARN_EMPTY_BODY = YES;
300303
CLANG_WARN_ENUM_CONVERSION = YES;
304+
CLANG_WARN_INFINITE_RECURSION = YES;
301305
CLANG_WARN_INT_CONVERSION = YES;
302306
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
307+
CLANG_WARN_SUSPICIOUS_MOVE = YES;
303308
CLANG_WARN_UNREACHABLE_CODE = YES;
304309
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
305310
CODE_SIGN_IDENTITY = "-";
@@ -319,6 +324,7 @@
319324
MACOSX_DEPLOYMENT_TARGET = 10.11;
320325
MTL_ENABLE_DEBUG_INFO = NO;
321326
SDKROOT = macosx;
327+
SWIFT_OPTIMIZATION_LEVEL = "-Owholemodule";
322328
VERSIONING_SYSTEM = "apple-generic";
323329
VERSION_INFO_PREFIX = "";
324330
};
@@ -328,6 +334,7 @@
328334
isa = XCBuildConfiguration;
329335
buildSettings = {
330336
CLANG_ENABLE_MODULES = YES;
337+
CODE_SIGN_IDENTITY = "";
331338
COMBINE_HIDPI_IMAGES = YES;
332339
DEFINES_MODULE = YES;
333340
DYLIB_COMPATIBILITY_VERSION = 1;
@@ -341,13 +348,15 @@
341348
PRODUCT_NAME = "$(TARGET_NAME)";
342349
SKIP_INSTALL = YES;
343350
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
351+
SWIFT_VERSION = 3.0;
344352
};
345353
name = Debug;
346354
};
347355
49BFA3131CDF886B00522D66 /* Release */ = {
348356
isa = XCBuildConfiguration;
349357
buildSettings = {
350358
CLANG_ENABLE_MODULES = YES;
359+
CODE_SIGN_IDENTITY = "";
351360
COMBINE_HIDPI_IMAGES = YES;
352361
DEFINES_MODULE = YES;
353362
DYLIB_COMPATIBILITY_VERSION = 1;
@@ -360,6 +369,7 @@
360369
PRODUCT_BUNDLE_IDENTIFIER = "com.swift-algorithm-club.Graph";
361370
PRODUCT_NAME = "$(TARGET_NAME)";
362371
SKIP_INSTALL = YES;
372+
SWIFT_VERSION = 3.0;
363373
};
364374
name = Release;
365375
};

Graph/Graph.xcodeproj/xcshareddata/xcschemes/Graph.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 = "0730"
3+
LastUpgradeVersion = "0800"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Graph/Graph.xcodeproj/xcshareddata/xcschemes/GraphTests.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 = "0730"
3+
LastUpgradeVersion = "0800"
44
version = "1.3">
55
<BuildAction
66
parallelizeBuildables = "YES"

Graph/Graph/AdjacencyListGraph.swift

+19-17
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
import Foundation
99

10-
private class EdgeList<T where T: Equatable, T: Hashable> {
10+
11+
12+
private class EdgeList<T> where T: Equatable, T: Hashable {
1113

1214
var vertex: Vertex<T>
1315
var edges: [Edge<T>]? = nil
@@ -16,15 +18,15 @@ private class EdgeList<T where T: Equatable, T: Hashable> {
1618
self.vertex = vertex
1719
}
1820

19-
func addEdge(edge: Edge<T>) {
21+
func addEdge(_ edge: Edge<T>) {
2022
edges?.append(edge)
2123
}
2224

2325
}
2426

25-
public class AdjacencyListGraph<T where T: Equatable, T: Hashable>: AbstractGraph<T> {
27+
open class AdjacencyListGraph<T>: AbstractGraph<T> where T: Equatable, T: Hashable {
2628

27-
private var adjacencyList: [EdgeList<T>] = []
29+
fileprivate var adjacencyList: [EdgeList<T>] = []
2830

2931
public required init() {
3032
super.init()
@@ -34,7 +36,7 @@ public class AdjacencyListGraph<T where T: Equatable, T: Hashable>: AbstractGrap
3436
super.init(fromGraph: graph)
3537
}
3638

37-
public override var vertices: [Vertex<T>] {
39+
open override var vertices: [Vertex<T>] {
3840
get {
3941
var vertices = [Vertex<T>]()
4042
for edgeList in adjacencyList {
@@ -44,7 +46,7 @@ public class AdjacencyListGraph<T where T: Equatable, T: Hashable>: AbstractGrap
4446
}
4547
}
4648

47-
public override var edges: [Edge<T>] {
49+
open override var edges: [Edge<T>] {
4850
get {
4951
var allEdges = Set<Edge<T>>()
5052
for edgeList in adjacencyList {
@@ -60,7 +62,7 @@ public class AdjacencyListGraph<T where T: Equatable, T: Hashable>: AbstractGrap
6062
}
6163
}
6264

63-
public override func createVertex(data: T) -> Vertex<T> {
65+
open override func createVertex(_ data: T) -> Vertex<T> {
6466
// check if the vertex already exists
6567
let matchingVertices = vertices.filter() { vertex in
6668
return vertex.data == data
@@ -76,24 +78,24 @@ public class AdjacencyListGraph<T where T: Equatable, T: Hashable>: AbstractGrap
7678
return vertex
7779
}
7880

79-
public override func addDirectedEdge(from: Vertex<T>, to: Vertex<T>, withWeight weight: Double?) {
81+
open override func addDirectedEdge(_ from: Vertex<T>, to: Vertex<T>, withWeight weight: Double?) {
8082
// works
8183
let edge = Edge(from: from, to: to, weight: weight)
8284
let edgeList = adjacencyList[from.index]
83-
if edgeList.edges?.count > 0 {
84-
edgeList.addEdge(edge)
85+
if let _ = edgeList.edges {
86+
edgeList.addEdge(edge)
8587
} else {
86-
edgeList.edges = [edge]
88+
edgeList.edges = [edge]
8789
}
8890
}
8991

90-
public override func addUndirectedEdge(vertices: (Vertex<T>, Vertex<T>), withWeight weight: Double?) {
92+
open override func addUndirectedEdge(_ vertices: (Vertex<T>, Vertex<T>), withWeight weight: Double?) {
9193
addDirectedEdge(vertices.0, to: vertices.1, withWeight: weight)
9294
addDirectedEdge(vertices.1, to: vertices.0, withWeight: weight)
9395
}
9496

9597

96-
public override func weightFrom(sourceVertex: Vertex<T>, to destinationVertex: Vertex<T>) -> Double? {
98+
open override func weightFrom(_ sourceVertex: Vertex<T>, to destinationVertex: Vertex<T>) -> Double? {
9799
guard let edges = adjacencyList[sourceVertex.index].edges else {
98100
return nil
99101
}
@@ -107,11 +109,11 @@ public class AdjacencyListGraph<T where T: Equatable, T: Hashable>: AbstractGrap
107109
return nil
108110
}
109111

110-
public override func edgesFrom(sourceVertex: Vertex<T>) -> [Edge<T>] {
112+
open override func edgesFrom(_ sourceVertex: Vertex<T>) -> [Edge<T>] {
111113
return adjacencyList[sourceVertex.index].edges ?? []
112114
}
113115

114-
public override var description: String {
116+
open override var description: String {
115117
get {
116118
var rows = [String]()
117119
for edgeList in adjacencyList {
@@ -129,10 +131,10 @@ public class AdjacencyListGraph<T where T: Equatable, T: Hashable>: AbstractGrap
129131
row.append(value)
130132
}
131133

132-
rows.append("\(edgeList.vertex.data) -> [\(row.joinWithSeparator(", "))]")
134+
rows.append("\(edgeList.vertex.data) -> [\(row.joined(separator: ", "))]")
133135
}
134136

135-
return rows.joinWithSeparator("\n")
137+
return rows.joined(separator: "\n")
136138
}
137139
}
138140
}

Graph/Graph/AdjacencyMatrixGraph.swift

+13-13
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
import Foundation
99

10-
public class AdjacencyMatrixGraph<T where T: Equatable, T: Hashable>: AbstractGraph<T> {
10+
open class AdjacencyMatrixGraph<T>: AbstractGraph<T> where T: Equatable, T: Hashable {
1111

1212
// If adjacencyMatrix[i][j] is not nil, then there is an edge from
1313
// vertex i to vertex j.
14-
private var adjacencyMatrix: [[Double?]] = []
15-
private var _vertices: [Vertex<T>] = []
14+
fileprivate var adjacencyMatrix: [[Double?]] = []
15+
fileprivate var _vertices: [Vertex<T>] = []
1616

1717
public required init() {
1818
super.init()
@@ -22,13 +22,13 @@ public class AdjacencyMatrixGraph<T where T: Equatable, T: Hashable>: AbstractGr
2222
super.init(fromGraph: graph)
2323
}
2424

25-
public override var vertices: [Vertex<T>] {
25+
open override var vertices: [Vertex<T>] {
2626
get {
2727
return _vertices
2828
}
2929
}
3030

31-
public override var edges: [Edge<T>] {
31+
open override var edges: [Edge<T>] {
3232
get {
3333
var edges = [Edge<T>]()
3434
for row in 0 ..< adjacencyMatrix.count {
@@ -44,7 +44,7 @@ public class AdjacencyMatrixGraph<T where T: Equatable, T: Hashable>: AbstractGr
4444

4545
// Adds a new vertex to the matrix.
4646
// Performance: possibly O(n^2) because of the resizing of the matrix.
47-
public override func createVertex(data: T) -> Vertex<T> {
47+
open override func createVertex(_ data: T) -> Vertex<T> {
4848
// check if the vertex already exists
4949
let matchingVertices = vertices.filter() { vertex in
5050
return vertex.data == data
@@ -63,28 +63,28 @@ public class AdjacencyMatrixGraph<T where T: Equatable, T: Hashable>: AbstractGr
6363
}
6464

6565
// Add one new row at the bottom.
66-
let newRow = [Double?](count: adjacencyMatrix.count + 1, repeatedValue: nil)
66+
let newRow = [Double?](repeating: nil, count: adjacencyMatrix.count + 1)
6767
adjacencyMatrix.append(newRow)
6868

6969
_vertices.append(vertex)
7070

7171
return vertex
7272
}
7373

74-
public override func addDirectedEdge(from: Vertex<T>, to: Vertex<T>, withWeight weight: Double?) {
74+
open override func addDirectedEdge(_ from: Vertex<T>, to: Vertex<T>, withWeight weight: Double?) {
7575
adjacencyMatrix[from.index][to.index] = weight
7676
}
7777

78-
public override func addUndirectedEdge(vertices: (Vertex<T>, Vertex<T>), withWeight weight: Double?) {
78+
open override func addUndirectedEdge(_ vertices: (Vertex<T>, Vertex<T>), withWeight weight: Double?) {
7979
addDirectedEdge(vertices.0, to: vertices.1, withWeight: weight)
8080
addDirectedEdge(vertices.1, to: vertices.0, withWeight: weight)
8181
}
8282

83-
public override func weightFrom(sourceVertex: Vertex<T>, to destinationVertex: Vertex<T>) -> Double? {
83+
open override func weightFrom(_ sourceVertex: Vertex<T>, to destinationVertex: Vertex<T>) -> Double? {
8484
return adjacencyMatrix[sourceVertex.index][destinationVertex.index]
8585
}
8686

87-
public override func edgesFrom(sourceVertex: Vertex<T>) -> [Edge<T>] {
87+
open override func edgesFrom(_ sourceVertex: Vertex<T>) -> [Edge<T>] {
8888
var outEdges = [Edge<T>]()
8989
let fromIndex = sourceVertex.index
9090
for column in 0..<adjacencyMatrix.count {
@@ -95,7 +95,7 @@ public class AdjacencyMatrixGraph<T where T: Equatable, T: Hashable>: AbstractGr
9595
return outEdges
9696
}
9797

98-
public override var description: String {
98+
open override var description: String {
9999
get {
100100
var grid = [String]()
101101
let n = self.adjacencyMatrix.count
@@ -111,7 +111,7 @@ public class AdjacencyMatrixGraph<T where T: Equatable, T: Hashable>: AbstractGr
111111
}
112112
grid.append(row)
113113
}
114-
return (grid as NSArray).componentsJoinedByString("\n")
114+
return (grid as NSArray).componentsJoined(by: "\n")
115115
}
116116
}
117117

Graph/Graph/Edge.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import Foundation
99

10-
public struct Edge<T where T: Equatable, T: Hashable>: Equatable {
10+
public struct Edge<T>: Equatable where T: Equatable, T: Hashable {
1111

1212
public let from: Vertex<T>
1313
public let to: Vertex<T>
@@ -35,7 +35,7 @@ extension Edge: Hashable {
3535
get {
3636
var string = "\(from.description)\(to.description)"
3737
if weight != nil {
38-
string.appendContentsOf("\(weight!)")
38+
string.append("\(weight!)")
3939
}
4040
return string.hashValue
4141
}

0 commit comments

Comments
 (0)