Skip to content

Commit d1de55a

Browse files
authored
Merge pull request #91 from Vithanco/master
removal of potentially harmful parameter.
2 parents 7ee1dd9 + 970a0b8 commit d1de55a

File tree

7 files changed

+21
-29
lines changed

7 files changed

+21
-29
lines changed

Sources/SwiftGraph/Graph.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public protocol Graph: CustomStringConvertible, Collection, Codable {
2626
var edges: [[E]] { get set }
2727

2828
init(vertices: [V])
29-
func addEdge(_ e: E, directed: Bool)
29+
func addEdge(_ e: E)
3030
}
3131

3232
extension Graph {
@@ -39,6 +39,12 @@ extension Graph {
3939
public var edgeCount: Int {
4040
return edges.joined().count
4141
}
42+
43+
44+
@available(*, deprecated, renamed: "addEdge(_:)", message: "Use the 'addEdge' method without the additional 'directed' parameter instead, as the Edge already contains the information about direction.")
45+
func addEdge(_ e: E, directed: Bool){
46+
addEdge(e)
47+
}
4248

4349
/// Returns a list of all the edges, undirected edges are only appended once.
4450
public func edgeList() -> [E] {
@@ -137,12 +143,9 @@ extension Graph {
137143
/// Add an edge to the graph.
138144
///
139145
/// - parameter e: The edge to add.
140-
/// - parameter directed: If false, undirected edges are created.
141-
/// If true, a reversed edge is also created.
142-
/// Default is false.
143-
public mutating func addEdge(_ e: E, directed: Bool = false) {
146+
public mutating func addEdge(_ e: E) {
144147
edges[e.u].append(e)
145-
if !directed && e.u != e.v {
148+
if !e.directed && e.u != e.v {
146149
edges[e.v].append(e.reversed())
147150
}
148151
}

Sources/SwiftGraph/Reversed.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616
// See the License for the specific language governing permissions and
1717
// limitations under the License.
1818

19-
import Foundation
20-
2119
extension Graph {
2220
/// Returns a graph of the same type with all edges reversed.
2321
///
2422
/// - returns: Graph of the same type with all edges reversed.
2523
public func reversed() -> Self {
2624
let g = Self(vertices: self.vertices)
2725
for e in self.edgeList() {
28-
g.addEdge(e.reversed(), directed: e.directed)
26+
g.addEdge(e.reversed())
2927
}
3028
return g
3129
}

Sources/SwiftGraph/Union.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public extension UniqueElementsGraph where E == UnweightedEdge {
4343
// When vertices are removed from Graph, edges might mutate,
4444
// so we need to add new copies of them for the result graph.
4545
for edge in firstGraph.edges.joined() {
46-
union.addEdge(edge, directed: true)
46+
union.addEdge(edge)
4747
}
4848

4949
for g in others {

Sources/SwiftGraph/UniqueElementsGraph.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,11 @@ open class UniqueElementsGraph<V: Equatable & Codable, E: Edge & Equatable>: Gra
5050
/// Add an edge to the graph. Only allow the edge to be added once
5151
///
5252
/// - parameter e: The edge to add.
53-
/// - parameter directed: If false, undirected edges are created.
54-
/// If true, a reversed edge is also created.
55-
/// Default is false.
56-
public func addEdge(_ e: E, directed: Bool = false) {
53+
public func addEdge(_ e: E) {
5754
if !self.edgeExists(e) {
5855
edges[e.u].append(e)
5956
}
60-
if !directed {
57+
if !e.directed {
6158
let reversedEdge = e.reversed()
6259
if !edgeExists(reversedEdge) {
6360
edges[e.v].append(reversedEdge)

Sources/SwiftGraph/UnweightedGraph.swift

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,9 @@ open class UnweightedGraph<V: Equatable & Codable>: Graph {
3333
/// Add an edge to the graph.
3434
///
3535
/// - parameter e: The edge to add.
36-
/// - parameter directed: If false, undirected edges are created.
37-
/// If true, a reversed edge is also created.
38-
/// Default is false.
39-
public func addEdge(_ e: UnweightedEdge, directed: Bool) {
36+
public func addEdge(_ e: UnweightedEdge) {
4037
edges[e.u].append(e)
41-
if !directed && e.u != e.v {
38+
if !e.directed && e.u != e.v {
4239
edges[e.v].append(e.reversed())
4340
}
4441
}
@@ -112,7 +109,7 @@ extension Graph where E == UnweightedEdge {
112109
/// - parameter to: The ending vertex's index.
113110
/// - parameter directed: Is the edge directed? (default `false`)
114111
public func addEdge(fromIndex: Int, toIndex: Int, directed: Bool = false) {
115-
addEdge(UnweightedEdge(u: fromIndex, v: toIndex, directed: directed), directed: directed)
112+
addEdge(UnweightedEdge(u: fromIndex, v: toIndex, directed: directed))
116113
}
117114

118115
/// This is a convenience method that adds an unweighted, undirected edge between the first occurence of two vertices. It takes O(n) time.
@@ -122,7 +119,7 @@ extension Graph where E == UnweightedEdge {
122119
/// - parameter directed: Is the edge directed? (default `false`)
123120
public func addEdge(from: V, to: V, directed: Bool = false) {
124121
if let u = indexOfVertex(from), let v = indexOfVertex(to) {
125-
addEdge(UnweightedEdge(u: u, v: v, directed: directed), directed: directed)
122+
addEdge(UnweightedEdge(u: u, v: v, directed: directed))
126123
}
127124
}
128125

Sources/SwiftGraph/WeightedGraph.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@ open class WeightedGraph<V: Equatable & Codable, W: Equatable & Codable>: Graph
3434
/// Add an edge to the graph.
3535
///
3636
/// - parameter e: The edge to add.
37-
/// - parameter directed: If false, undirected edges are created.
38-
/// If true, a reversed edge is also created.
39-
/// Default is false.
40-
public func addEdge(_ e: WeightedEdge<W>, directed: Bool) {
37+
public func addEdge(_ e: WeightedEdge<W>) {
4138
edges[e.u].append(e)
42-
if !directed && e.u != e.v {
39+
if !e.directed && e.u != e.v {
4340
edges[e.v].append(e.reversed())
4441
}
4542
}
@@ -77,7 +74,7 @@ extension Graph where E: WeightedEdgeProtocol {
7774
/// - parameter directed: Is the edge directed? (default false)
7875
/// - parameter weight: the Weight of the edge to add.
7976
public func addEdge(fromIndex: Int, toIndex: Int, weight: W, directed: Bool = false) {
80-
addEdge(E(u: fromIndex, v: toIndex, directed: directed, weight: weight), directed: directed)
77+
addEdge(E(u: fromIndex, v: toIndex, directed: directed, weight: weight))
8178
}
8279

8380
/// This is a convenience method that adds a weighted edge between the first occurence of two vertices. It takes O(n) time.

Tests/SwiftGraphTests/SwiftGraphTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class SwiftGraphTests: XCTestCase {
8181
let arezzoIndex = g.addVertex("Arezzo")
8282
g.addEdge(from: "Atlanta", to: "New York", directed: true)
8383
let nyAtlantaEdge = UnweightedEdge(u: nyIndex, v: atlantaIndex, directed: true)
84-
g.addEdge(nyAtlantaEdge, directed: true)
84+
g.addEdge(nyAtlantaEdge)
8585
g.addEdge(from: "Miami", to: "Atlanta", directed: true)
8686
g.addEdge(from: "New York", to: "Miami", directed: false)
8787
g.addEdge(from: "Atlanta", to: "Miami", directed: true)

0 commit comments

Comments
 (0)