Skip to content

Commit cc48332

Browse files
bwetherfieldjsbean
authored andcommitted
Add remove functions for node and edge (#189)
1 parent 0bd7339 commit cc48332

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

Sources/DataStructures/Graph/Protocols/GraphProtocol.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public protocol GraphProtocol {
2525
var edges: Set<Edge> { get }
2626

2727
// MARK: - Instance Methods
28-
29-
/// Removes the edge from the given `source` to the given `destination`.
30-
mutating func removeEdge(from source: Node, to destination: Node)
28+
29+
/// Removes `edge` from graph.
30+
mutating func remove(_ edge: Edge)
3131
}
3232

3333
extension GraphProtocol {
@@ -37,6 +37,19 @@ extension GraphProtocol {
3737
public mutating func insert(_ node: Node) {
3838
nodes.insert(node)
3939
}
40+
41+
/// Removes the edge from the given `source` to the given `destination`.
42+
@inlinable
43+
public mutating func removeEdge(from source: Node, to destination: Node) {
44+
remove(Edge(source, destination))
45+
}
46+
47+
/// Removes the given `node` and removes all edges that contain it.
48+
@inlinable
49+
public mutating func remove(_ node: Node) {
50+
nodes.remove(node)
51+
edges.filter { $0.contains(node) }.forEach { remove($0) }
52+
}
4053

4154
/// - Returns: `true` if the `GraphProtocol`-conforming type value contains the given `node`.
4255
/// Otherwise, `false`.

Sources/DataStructures/Graph/Protocols/UnweightedGraphProtocol.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ extension UnweightedGraphProtocol {
4343
insert(destination)
4444
edges.insert(Edge(source,destination))
4545
}
46-
47-
/// Removes the edge between the given `source` and `destination` nodes.
46+
47+
/// Removes `edge` from graph.
4848
@inlinable
49-
public mutating func removeEdge(from source: Node, to destination: Node) {
50-
edges.remove(Edge(source,destination))
49+
public mutating func remove(_ edge: Edge) {
50+
edges.remove(edge)
5151
}
5252
}
5353

Sources/DataStructures/Graph/Protocols/WeightedGraphProtocol.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,9 @@ extension WeightedGraphProtocol {
6969
weights[Edge(source,destination)] = weight
7070
}
7171

72-
/// Removes the edge between the given `source` and `destination` nodes.
72+
/// Remove `edge` from graph.
7373
@inlinable
74-
public mutating func removeEdge(from source: Node, to destination: Node) {
75-
removeEdge(Edge(source, destination))
76-
}
77-
78-
@inlinable
79-
public mutating func removeEdge(_ edge: Edge) {
74+
public mutating func remove(_ edge: Edge) {
8075
weights[edge] = nil
8176
}
8277

0 commit comments

Comments
 (0)