File tree 3 files changed +22
-14
lines changed
Sources/DataStructures/Graph/Protocols 3 files changed +22
-14
lines changed Original file line number Diff line number Diff line change @@ -25,9 +25,9 @@ public protocol GraphProtocol {
25
25
var edges : Set < Edge > { get }
26
26
27
27
// 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 )
31
31
}
32
32
33
33
extension GraphProtocol {
@@ -37,6 +37,19 @@ extension GraphProtocol {
37
37
public mutating func insert( _ node: Node ) {
38
38
nodes. insert ( node)
39
39
}
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
+ }
40
53
41
54
/// - Returns: `true` if the `GraphProtocol`-conforming type value contains the given `node`.
42
55
/// Otherwise, `false`.
Original file line number Diff line number Diff line change @@ -43,11 +43,11 @@ extension UnweightedGraphProtocol {
43
43
insert ( destination)
44
44
edges. insert ( Edge ( source, destination) )
45
45
}
46
-
47
- /// Removes the edge between the given `source` and `destination` nodes .
46
+
47
+ /// Removes ` edge` from graph .
48
48
@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 )
51
51
}
52
52
}
53
53
Original file line number Diff line number Diff line change @@ -69,14 +69,9 @@ extension WeightedGraphProtocol {
69
69
weights [ Edge ( source, destination) ] = weight
70
70
}
71
71
72
- /// Removes the edge between the given `source` and `destination` nodes .
72
+ /// Remove ` edge` from graph .
73
73
@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 ) {
80
75
weights [ edge] = nil
81
76
}
82
77
You can’t perform that action at this time.
0 commit comments