Skip to content

removal of potentially harmful parameter. #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Sources/SwiftGraph/Graph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public protocol Graph: CustomStringConvertible, Collection, Codable {
var edges: [[E]] { get set }

init(vertices: [V])
func addEdge(_ e: E, directed: Bool)
func addEdge(_ e: E)
}

extension Graph {
Expand All @@ -39,6 +39,11 @@ extension Graph {
public var edgeCount: Int {
return edges.joined().count
}

@available(*, deprecated, renamed: "addEdge", message: "Use the addEdge method without the additional directed parameter instead, as the Edge contains already the information about direction. A double specification can only result in inconsistencies and errors.")
func addEdge(_ e: E, directed: Bool){
addEdge(e)
}

/// Returns a list of all the edges, undirected edges are only appended once.
public func edgeList() -> [E] {
Expand Down Expand Up @@ -137,12 +142,9 @@ extension Graph {
/// Add an edge to the graph.
///
/// - parameter e: The edge to add.
/// - parameter directed: If false, undirected edges are created.
/// If true, a reversed edge is also created.
/// Default is false.
public mutating func addEdge(_ e: E, directed: Bool = false) {
public mutating func addEdge(_ e: E) {
edges[e.u].append(e)
if !directed && e.u != e.v {
if !e.directed && e.u != e.v {
edges[e.v].append(e.reversed())
}
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGraph/Reversed.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ extension Graph {
public func reversed() -> Self {
let g = Self(vertices: self.vertices)
for e in self.edgeList() {
g.addEdge(e.reversed(), directed: e.directed)
g.addEdge(e.reversed())
}
return g
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/SwiftGraph/Union.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public extension UniqueElementsGraph where E == UnweightedEdge {
// When vertices are removed from Graph, edges might mutate,
// so we need to add new copies of them for the result graph.
for edge in firstGraph.edges.joined() {
union.addEdge(edge, directed: true)
union.addEdge(edge)
}

for g in others {
Expand Down
7 changes: 2 additions & 5 deletions Sources/SwiftGraph/UniqueElementsGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,11 @@ open class UniqueElementsGraph<V: Equatable & Codable, E: Edge & Equatable>: Gra
/// Add an edge to the graph. Only allow the edge to be added once
///
/// - parameter e: The edge to add.
/// - parameter directed: If false, undirected edges are created.
/// If true, a reversed edge is also created.
/// Default is false.
public func addEdge(_ e: E, directed: Bool = false) {
public func addEdge(_ e: E) {
if !self.edgeExists(e) {
edges[e.u].append(e)
}
if !directed {
if !e.directed {
let reversedEdge = e.reversed()
if !edgeExists(reversedEdge) {
edges[e.v].append(reversedEdge)
Expand Down
11 changes: 4 additions & 7 deletions Sources/SwiftGraph/UnweightedGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,9 @@ open class UnweightedGraph<V: Equatable & Codable>: Graph {
/// Add an edge to the graph.
///
/// - parameter e: The edge to add.
/// - parameter directed: If false, undirected edges are created.
/// If true, a reversed edge is also created.
/// Default is false.
public func addEdge(_ e: UnweightedEdge, directed: Bool) {
public func addEdge(_ e: UnweightedEdge) {
edges[e.u].append(e)
if !directed && e.u != e.v {
if !e.directed && e.u != e.v {
edges[e.v].append(e.reversed())
}
}
Expand Down Expand Up @@ -112,7 +109,7 @@ extension Graph where E == UnweightedEdge {
/// - parameter to: The ending vertex's index.
/// - parameter directed: Is the edge directed? (default `false`)
public func addEdge(fromIndex: Int, toIndex: Int, directed: Bool = false) {
addEdge(UnweightedEdge(u: fromIndex, v: toIndex, directed: directed), directed: directed)
addEdge(UnweightedEdge(u: fromIndex, v: toIndex, directed: directed))
}

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

Expand Down
9 changes: 3 additions & 6 deletions Sources/SwiftGraph/WeightedGraph.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,9 @@ open class WeightedGraph<V: Equatable & Codable, W: Equatable & Codable>: Graph
/// Add an edge to the graph.
///
/// - parameter e: The edge to add.
/// - parameter directed: If false, undirected edges are created.
/// If true, a reversed edge is also created.
/// Default is false.
public func addEdge(_ e: WeightedEdge<W>, directed: Bool) {
public func addEdge(_ e: WeightedEdge<W>) {
edges[e.u].append(e)
if !directed && e.u != e.v {
if !e.directed && e.u != e.v {
edges[e.v].append(e.reversed())
}
}
Expand Down Expand Up @@ -77,7 +74,7 @@ extension Graph where E: WeightedEdgeProtocol {
/// - parameter directed: Is the edge directed? (default false)
/// - parameter weight: the Weight of the edge to add.
public func addEdge(fromIndex: Int, toIndex: Int, weight: W, directed: Bool = false) {
addEdge(E(u: fromIndex, v: toIndex, directed: directed, weight: weight), directed: directed)
addEdge(E(u: fromIndex, v: toIndex, directed: directed, weight: weight))
}

/// This is a convenience method that adds a weighted edge between the first occurence of two vertices. It takes O(n) time.
Expand Down
2 changes: 1 addition & 1 deletion Tests/SwiftGraphTests/SwiftGraphTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class SwiftGraphTests: XCTestCase {
let arezzoIndex = g.addVertex("Arezzo")
g.addEdge(from: "Atlanta", to: "New York", directed: true)
let nyAtlantaEdge = UnweightedEdge(u: nyIndex, v: atlantaIndex, directed: true)
g.addEdge(nyAtlantaEdge, directed: true)
g.addEdge(nyAtlantaEdge)
g.addEdge(from: "Miami", to: "Atlanta", directed: true)
g.addEdge(from: "New York", to: "Miami", directed: false)
g.addEdge(from: "Atlanta", to: "Miami", directed: true)
Expand Down