Skip to content

Commit bf45aad

Browse files
committed
Make vertex connection views unmodifiable
1 parent d65074b commit bf45aad

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lib/graph/vertex.dart

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@ class Vertex<T> {
1515
final LinkedHashSet<Vertex> _incomingVertices;
1616

1717
/// Incoming connections from this [Vertex]
18-
LinkedHashSet<Vertex> get incomingVertices => _incomingVertices;
18+
List<Vertex> get incomingVertices =>
19+
List<Vertex>.unmodifiable(_incomingVertices);
1920

2021
final LinkedHashMap<Vertex, num> _outgoingConnections;
2122

2223
/// Outgoing connections from this [Vertex]
23-
LinkedHashMap<Vertex, num> get outgoingConnections => _outgoingConnections;
24+
UnmodifiableMapView<Vertex, num> get outgoingConnections =>
25+
Map<Vertex, num>.unmodifiable(_outgoingConnections);
2426

2527
/// Constructor
2628
Vertex(this._key, [T value])
@@ -31,7 +33,7 @@ class Vertex<T> {
3133

3234
/// Adds a connection with [Vertex] `dst` and with `weight`
3335
bool addConnection(Vertex dst, [num weight = 1]) {
34-
if (outgoingConnections.containsKey(dst)) {
36+
if (_outgoingConnections.containsKey(dst)) {
3537
return false;
3638
}
3739
_outgoingConnections[dst] = weight;
@@ -50,23 +52,24 @@ class Vertex<T> {
5052

5153
/// Checks if [Vertex] `other` is connected to this vertex
5254
bool containsConnectionTo(Vertex other) =>
53-
outgoingConnections.containsKey(other);
55+
_outgoingConnections.containsKey(other);
5456

5557
/// Checks if [Vertex] `other` is connected to this vertex
56-
bool containsConnectionFrom(Vertex other) => incomingVertices.contains(other);
58+
bool containsConnectionFrom(Vertex other) =>
59+
_incomingVertices.contains(other);
5760

5861
/// Get a list of adjacent outgoing vertices
5962
Set<Vertex> get outgoingVertices =>
60-
outgoingConnections.keys.map((connection) => connection).toSet();
63+
_outgoingConnections.keys.map((connection) => connection).toSet();
6164

6265
/// Is this vertex isolated?
63-
bool get isIsolated => outgoingConnections.isEmpty;
66+
bool get isIsolated => _outgoingConnections.isEmpty;
6467

6568
/// Calculate the inDegree of the vertex
66-
int get inDegree => incomingVertices.length;
69+
int get inDegree => _incomingVertices.length;
6770

6871
/// Calculate the outDegree of the vertex
69-
int get outDegree => outgoingConnections.length;
72+
int get outDegree => _outgoingConnections.length;
7073

7174
@override
7275
String toString() => key;

0 commit comments

Comments
 (0)