Skip to content

Commit 096f71b

Browse files
committed
Added the method clearEdges.
1 parent 8de6bd0 commit 096f71b

7 files changed

Lines changed: 55 additions & 7 deletions

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
## 0.5.4
2+
- Fixed documentation of graph method `clear`. This method removes all graph
3+
vertices.
4+
- Added the graph method `clearEdges`. This method removes only the graph
5+
edges, leaving the graph vertice in place.
16

27
## 0.5.3
38
- Added `WeightedDirectedGraph` methods: `weightedEdges` and `updateEdgeWeight`.
4-
- Updated docs and examples.
9+
- Updated docs and examples.
510

611
## 0.5.2
712
- Fixed grammar in CHANGELOG entry below.

lib/src/graph/directed_graph.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,15 @@ class DirectedGraph<T extends Object> extends DirectedGraphBase<T> {
142142
}
143143
}
144144

145-
/// Removes all graph edges.
145+
@override
146+
void clearEdges() {
147+
for (final vertex in _edges.keys) {
148+
_edges[vertex]!.clear();
149+
}
150+
updateCache();
151+
}
152+
153+
@override
146154
void clear() {
147155
_edges.clear();
148156
updateCache();

lib/src/graph/directed_graph_base.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ abstract class DirectedGraphBase<T extends Object> extends Iterable<T> {
3030
@override
3131
bool contains(Object? element);
3232

33+
/// Removes all graph vertices. After this method returns the graph
34+
/// will be empty.
35+
void clear();
36+
37+
/// Removes all graph edges (connections between vertices). The graph vertices
38+
/// are not removed. To remove all vertices and (implicitly) all graph edges
39+
/// use the method [clear].
40+
void clearEdges();
41+
3342
/// Returns the shortest path from the vertex [start] to the vertex [target].
3443
/// * Returns an empty list if [target] is not reachable from [start].
3544
List<T> shortestPath(T start, T target) => crawler.path(start, target);

lib/src/graph/weighted_directed_graph.dart

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ typedef Summation<W> = W Function(W left, W right);
1212
int defaultWeightComparator<W extends Comparable>(W left, W right) =>
1313
left.compareTo(right);
1414

15-
/// A directed graph with vertices of type `T` and a weight of type
16-
/// `W` associated with each directed edges.
17-
/// * `T` must be usable as a map key.
15+
/// A directed graph with vertices of type [T] and a weight of type
16+
/// [W] associated with each directed edge.
17+
/// * [T] must be usable as a map key.
1818
class WeightedDirectedGraph<T extends Object, W extends Comparable>
1919
extends DirectedGraphBase<T> {
2020
/// Constructs a weighted directed graph with vertices of type `T`
@@ -206,7 +206,15 @@ class WeightedDirectedGraph<T extends Object, W extends Comparable>
206206
updateCache();
207207
}
208208

209-
/// Removes all graph edges.
209+
@override
210+
void clearEdges() {
211+
for (final vertex in _edges.keys) {
212+
_edges[vertex]!.clear();
213+
}
214+
updateCache();
215+
}
216+
217+
@override
210218
void clear() {
211219
_edges.clear();
212220
updateCache();

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: directed_graph
33
description: Generic directed graph and weighted directed graph with algorithms
44
enabling sorting and topological ordering of vertices.
55

6-
version: 0.5.3
6+
version: 0.5.4
77

88
homepage: https://github.com/simphotonics/directed_graph
99

test/directed_graph_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,15 @@ void main() {
135135
graph.clear();
136136
expect(graph.isEmpty, true);
137137
});
138+
test('clearEdges', () {
139+
final graph = DirectedGraph.of(graph0);
140+
expect(graph.sortedVertices, graph0.sortedVertices);
141+
graph.clearEdges();
142+
expect(graph.sortedVertices, graph0.sortedVertices);
143+
for (var vertex in graph.sortedVertices) {
144+
expect(graph.edges(vertex), isEmpty);
145+
}
146+
});
138147
});
139148
group('Graph data:', () {
140149
final graph = DirectedGraph.of(graph0);

test/weighted_directed_graph_test.dart

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ void main() {
108108
graph.clear();
109109
expect(graph.isEmpty, true);
110110
});
111+
test('clearEdges', () {
112+
final graph = WeightedDirectedGraph.of(graph0);
113+
expect(graph.sortedVertices, graph0.sortedVertices);
114+
graph.clearEdges();
115+
expect(graph.sortedVertices, graph0.sortedVertices);
116+
for (var vertex in graph.sortedVertices) {
117+
expect(graph.edges(vertex), isEmpty);
118+
}
119+
});
111120
});
112121
group('Graph data:', () {
113122
test('edges().', () {

0 commit comments

Comments
 (0)