Skip to content

Commit e85f88f

Browse files
committed
Moved reversed functions as per PR
1 parent 0485693 commit e85f88f

File tree

5 files changed

+93
-52
lines changed

5 files changed

+93
-52
lines changed

Sources/SwiftGraph/Graph.swift

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,6 @@ extension Graph {
245245
return edges[edge.u].contains(edge)
246246
}
247247

248-
/// Returns a graph of the same type with all edges reversed.
249-
///
250-
/// - returns: Graph of the same type with all edges reversed.
251-
public func reversed() -> Self {
252-
let g = Self(vertices: self.vertices)
253-
for e in self.edgeList() {
254-
g.addEdge(e.reversed(), directed: e.directed)
255-
}
256-
return g
257-
}
258-
259-
260248
// MARK: Implement Printable protocol
261249
public var description: String {
262250
var d: String = ""

Sources/SwiftGraph/Reversed.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Reversed.swift
3+
// SwiftGraph
4+
//
5+
// Created by Matthew Paletta on 2021-02-18.
6+
// Copyright © 2021 Oak Snow Consulting. All rights reserved.
7+
//
8+
9+
import Foundation
10+
11+
extension Graph {
12+
/// Returns a graph of the same type with all edges reversed.
13+
///
14+
/// - returns: Graph of the same type with all edges reversed.
15+
public func reversed() -> Self {
16+
let g = Self(vertices: self.vertices)
17+
for e in self.edgeList() {
18+
g.addEdge(e.reversed(), directed: e.directed)
19+
}
20+
return g
21+
}
22+
}

SwiftGraph.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
343882F925DF04E7007D2401 /* Reversed.swift in Sources */ = {isa = PBXBuildFile; fileRef = 343882F825DF04E7007D2401 /* Reversed.swift */; };
11+
3438830525DF056C007D2401 /* SwiftGraphReversedTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 343882FD25DF0528007D2401 /* SwiftGraphReversedTests.swift */; };
1012
55DCCBF61F8ADA12001913F7 /* Cycle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55DCCBF51F8ADA12001913F7 /* Cycle.swift */; };
1113
55DCCBF81F8AE2F1001913F7 /* CycleTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55DCCBF71F8AE2F1001913F7 /* CycleTests.swift */; };
1214
55E620081A194C80000A5F7B /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55E620071A194C80000A5F7B /* AppDelegate.swift */; };
@@ -93,6 +95,8 @@
9395
/* End PBXCopyFilesBuildPhase section */
9496

9597
/* Begin PBXFileReference section */
98+
343882F825DF04E7007D2401 /* Reversed.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Reversed.swift; path = Sources/SwiftGraph/Reversed.swift; sourceTree = SOURCE_ROOT; };
99+
343882FD25DF0528007D2401 /* SwiftGraphReversedTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SwiftGraphReversedTests.swift; sourceTree = "<group>"; };
96100
553746441DA56CC500C0E0F6 /* Sort.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Sort.swift; path = Sources/SwiftGraph/Sort.swift; sourceTree = SOURCE_ROOT; };
97101
557F55E91F8AB247002AF0BF /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; name = Info.plist; path = ../Info.plist; sourceTree = "<group>"; };
98102
5589B4621C0E72CC00D6664E /* Edge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = Edge.swift; path = Sources/SwiftGraph/Edge.swift; sourceTree = SOURCE_ROOT; };
@@ -284,6 +288,7 @@
284288
7985B91A1E5A4FCB00C100E7 /* SwiftGraphSearchTests.swift */,
285289
7985B91B1E5A4FCB00C100E7 /* SwiftGraphSortTests.swift */,
286290
7985B91C1E5A4FCB00C100E7 /* SwiftGraphTests.swift */,
291+
343882FD25DF0528007D2401 /* SwiftGraphReversedTests.swift */,
287292
B5100A4B208B97A800C7A73A /* UnweightedGraphTests.swift */,
288293
B52ABD37208955B500FBF10C /* UnionTests.swift */,
289294
55F5EA5A2151E33100DFC301 /* SwiftGraphCodableTests.swift */,
@@ -318,6 +323,7 @@
318323
isa = PBXGroup;
319324
children = (
320325
B51B460A2083E14200CD0463 /* Union.swift */,
326+
343882F825DF04E7007D2401 /* Reversed.swift */,
321327
);
322328
name = Operations;
323329
sourceTree = "<group>";
@@ -538,6 +544,7 @@
538544
7985B9211E5A503200C100E7 /* Edge.swift in Sources */,
539545
7985B9271E5A503200C100E7 /* SwiftPriorityQueue.swift in Sources */,
540546
55DCCBF61F8ADA12001913F7 /* Cycle.swift in Sources */,
547+
343882F925DF04E7007D2401 /* Reversed.swift in Sources */,
541548
);
542549
runOnlyForDeploymentPostprocessing = 0;
543550
};
@@ -560,6 +567,7 @@
560567
B5EF143321791348008FCC5C /* UniqueElementsGraphHashableTests.swift in Sources */,
561568
55F5EA5B2151E33100DFC301 /* SwiftGraphCodableTests.swift in Sources */,
562569
B523F2EA2094F0E2006587ED /* UniqueElementsGraphHashableInitTests.swift in Sources */,
570+
3438830525DF056C007D2401 /* SwiftGraphReversedTests.swift in Sources */,
563571
B5EF143121791009008FCC5C /* UniqueElementsGraphInitTests.swift in Sources */,
564572
B54FDA712172A34D00057C51 /* ConstructorsTests.swift in Sources */,
565573
B5D229BF207BF3A800151820 /* UniqueElementsGraphTests.swift in Sources */,
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//
2+
// SwiftGraphReversedTests.swift
3+
// SwiftGraph
4+
//
5+
// Created by Matthew Paletta on 2021-02-18.
6+
// Copyright © 2021 Oak Snow Consulting. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import SwiftGraph
11+
12+
class SwiftGraphReversedTests: XCTestCase {
13+
14+
override func setUp() {
15+
super.setUp()
16+
// Put setup code here. This method is called before the invocation of each test method in the class.
17+
}
18+
19+
override func tearDown() {
20+
// Put teardown code here. This method is called after the invocation of each test method in the class.
21+
super.tearDown()
22+
}
23+
24+
func testCitesReversedDirected() {
25+
let g = UnweightedGraph<String>(vertices: ["Atlanta", "New York", "Miami"])
26+
g.addEdge(from: "Atlanta", to: "New York", directed: true)
27+
g.addEdge(from: "Miami", to: "Atlanta", directed: true)
28+
g.addEdge(from: "New York", to: "Miami", directed: true)
29+
30+
XCTAssertTrue(g.edgeExists(from: "Atlanta", to: "New York"))
31+
XCTAssertTrue(g.edgeExists(from: "Miami", to: "Atlanta"))
32+
XCTAssertTrue(g.edgeExists(from: "New York", to: "Miami"))
33+
34+
let r: UnweightedGraph<String> = g.reversed()
35+
XCTAssertFalse(r.edgeExists(from: "Atlanta", to: "New York"))
36+
XCTAssertFalse(r.edgeExists(from: "Miami", to: "Atlanta"))
37+
XCTAssertFalse(r.edgeExists(from: "New York", to: "Miami"))
38+
39+
XCTAssertTrue(r.edgeExists(from: "New York", to: "Atlanta"))
40+
XCTAssertTrue(r.edgeExists(from: "Atlanta", to: "Miami"))
41+
XCTAssertTrue(r.edgeExists(from: "Miami", to: "New York"))
42+
}
43+
44+
func testCitesReversedUndirected() {
45+
let g = UnweightedGraph<String>(vertices: ["Atlanta", "New York", "Miami"])
46+
g.addEdge(from: "Atlanta", to: "New York", directed: false)
47+
g.addEdge(from: "Miami", to: "Atlanta", directed: false)
48+
g.addEdge(from: "New York", to: "Miami", directed: false)
49+
50+
XCTAssertTrue(g.edgeExists(from: "Atlanta", to: "New York"))
51+
XCTAssertTrue(g.edgeExists(from: "Miami", to: "Atlanta"))
52+
XCTAssertTrue(g.edgeExists(from: "New York", to: "Miami"))
53+
54+
let r: UnweightedGraph<String> = g.reversed()
55+
XCTAssertTrue(r.edgeExists(from: "Atlanta", to: "New York"))
56+
XCTAssertTrue(r.edgeExists(from: "Miami", to: "Atlanta"))
57+
XCTAssertTrue(r.edgeExists(from: "New York", to: "Miami"))
58+
59+
XCTAssertTrue(r.edgeExists(from: "New York", to: "Atlanta"))
60+
XCTAssertTrue(r.edgeExists(from: "Atlanta", to: "Miami"))
61+
XCTAssertTrue(r.edgeExists(from: "Miami", to: "New York"))
62+
}
63+
}

Tests/SwiftGraphTests/SwiftGraphTests.swift

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -95,44 +95,4 @@ class SwiftGraphTests: XCTestCase {
9595
XCTAssertFalse(graph.edgeExists(fromIndex: 2, toIndex: 3))
9696
XCTAssertFalse(graph.edgeExists(fromIndex: 3, toIndex: 2))
9797
}
98-
99-
func testCitesReversedDirected() {
100-
let g = UnweightedGraph<String>(vertices: ["Atlanta", "New York", "Miami"])
101-
g.addEdge(from: "Atlanta", to: "New York", directed: true)
102-
g.addEdge(from: "Miami", to: "Atlanta", directed: true)
103-
g.addEdge(from: "New York", to: "Miami", directed: true)
104-
105-
XCTAssertTrue(g.edgeExists(from: "Atlanta", to: "New York"))
106-
XCTAssertTrue(g.edgeExists(from: "Miami", to: "Atlanta"))
107-
XCTAssertTrue(g.edgeExists(from: "New York", to: "Miami"))
108-
109-
let r: UnweightedGraph<String> = g.reversed()
110-
XCTAssertFalse(r.edgeExists(from: "Atlanta", to: "New York"))
111-
XCTAssertFalse(r.edgeExists(from: "Miami", to: "Atlanta"))
112-
XCTAssertFalse(r.edgeExists(from: "New York", to: "Miami"))
113-
114-
XCTAssertTrue(r.edgeExists(from: "New York", to: "Atlanta"))
115-
XCTAssertTrue(r.edgeExists(from: "Atlanta", to: "Miami"))
116-
XCTAssertTrue(r.edgeExists(from: "Miami", to: "New York"))
117-
}
118-
119-
func testCitesReversedUndirected() {
120-
let g = UnweightedGraph<String>(vertices: ["Atlanta", "New York", "Miami"])
121-
g.addEdge(from: "Atlanta", to: "New York", directed: false)
122-
g.addEdge(from: "Miami", to: "Atlanta", directed: false)
123-
g.addEdge(from: "New York", to: "Miami", directed: false)
124-
125-
XCTAssertTrue(g.edgeExists(from: "Atlanta", to: "New York"))
126-
XCTAssertTrue(g.edgeExists(from: "Miami", to: "Atlanta"))
127-
XCTAssertTrue(g.edgeExists(from: "New York", to: "Miami"))
128-
129-
let r: UnweightedGraph<String> = g.reversed()
130-
XCTAssertTrue(r.edgeExists(from: "Atlanta", to: "New York"))
131-
XCTAssertTrue(r.edgeExists(from: "Miami", to: "Atlanta"))
132-
XCTAssertTrue(r.edgeExists(from: "New York", to: "Miami"))
133-
134-
XCTAssertTrue(r.edgeExists(from: "New York", to: "Atlanta"))
135-
XCTAssertTrue(r.edgeExists(from: "Atlanta", to: "Miami"))
136-
XCTAssertTrue(r.edgeExists(from: "Miami", to: "New York"))
137-
}
13898
}

0 commit comments

Comments
 (0)