Skip to content

Commit e2e6727

Browse files
committed
Remove the lock experiment on graph
1 parent 52fb388 commit e2e6727

File tree

4 files changed

+2
-92
lines changed

4 files changed

+2
-92
lines changed

Diff for: lib/graph/simple_graph.dart

-4
Original file line numberDiff line numberDiff line change
@@ -23,22 +23,18 @@ class SimpleGraph<T> {
2323

2424
/// Adds an edge
2525
void addEdge(Vertex<T> src, Vertex<T> dst, [num weight = 1]) {
26-
unlockVertices(<Vertex>{src, dst});
2726
if (src.key == dst.key) throw Error();
2827

2928
src = _getOrAddVertex(src);
3029
dst = _getOrAddVertex(dst);
3130
src.addConnection(dst, weight);
3231

3332
if (!isDigraph) dst.addConnection(src, weight);
34-
lockVertices(<Vertex>{src, dst});
3533
}
3634

3735
/// Removes an Edge from [this], returns `false` if edge does not exists.
3836
bool removeEdge(Vertex src, Vertex dst) {
39-
unlockVertices(<Vertex>{src, dst});
4037
var removed = src.removeConnection(dst);
41-
lockVertices(<Vertex>{src, dst});
4238

4339
return removed;
4440
}

Diff for: lib/graph/vertex.dart

+1-29
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import 'dart:collection';
55
/// the vertex. By default, the `key` and `value` are the same.
66
class Vertex<T> {
77
final String _key;
8-
bool _isLocked;
98

109
/// Uniquely identifiable key to this [Vertex]
1110
String get key => _key;
@@ -27,21 +26,11 @@ class Vertex<T> {
2726

2827
/// Constructor
2928
Vertex(this._key, [this.value])
30-
: _isLocked = true,
31-
_incomingVertices = <Vertex>{} as LinkedHashSet<Vertex>,
29+
: _incomingVertices = <Vertex>{} as LinkedHashSet<Vertex>,
3230
_outgoingConnections = <Vertex, num>{} as LinkedHashMap<Vertex, num>;
3331

34-
/// Lock [this] vertex, cannot modify after it is locked
35-
void lock() => _isLocked = true;
36-
37-
/// Unlock [this] vertex, can modify after it is unlock
38-
void unlock() => _isLocked = false;
39-
4032
/// Adds a connection with [Vertex] `dst` and with `weight`
4133
bool addConnection(Vertex dst, [num weight = 1]) {
42-
if (_isLocked || dst._isLocked) {
43-
throw UnsupportedError('Cannot add to a locked vertex');
44-
}
4534
if (_outgoingConnections.containsKey(dst)) {
4635
return false;
4736
}
@@ -53,9 +42,6 @@ class Vertex<T> {
5342
/// Removes a connection with `other` with `weight`. `false` for non-existent
5443
/// connection.
5544
bool removeConnection(Vertex other, [num weight = 1]) {
56-
if (_isLocked || other._isLocked) {
57-
throw UnsupportedError('Cannot remove from a locked vertex');
58-
}
5945
var outgoingRemoved = _outgoingConnections.remove(other) != null;
6046
var incomingRemoved = other._incomingVertices.remove(this);
6147

@@ -92,17 +78,3 @@ class Vertex<T> {
9278
// ignore: avoid_equals_and_hash_code_on_mutable_classes
9379
int get hashCode => key.hashCode;
9480
}
95-
96-
/// Unlocks a set of vertices
97-
void unlockVertices(Set<Vertex> vertices) {
98-
for (var vertex in vertices) {
99-
vertex.unlock();
100-
}
101-
}
102-
103-
/// Locks a set of vertices
104-
void lockVertices(Set<Vertex> vertices) {
105-
for (var vertex in vertices) {
106-
vertex.lock();
107-
}
108-
}

Diff for: test/graph/simple_graph_test.dart

-8
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,4 @@ void main() {
167167
};
168168
expect(simpleGraph.edges.toSet(), equals(expectedEdges));
169169
});
170-
171-
test('addConnection on a cherry picked vertex throws error', () {
172-
var vertices = simpleGraph.vertices;
173-
var leak = Vertex('LEAK');
174-
leak.unlock();
175-
expect(() => vertices[0].addConnection(leak),
176-
throwsA(isA<UnsupportedError>()));
177-
});
178170
}

Diff for: test/graph/vertex_test.dart

+1-51
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,8 @@ void main() {
2222
c = Vertex('c');
2323
}
2424

25-
void _unlockVertices() {
26-
unlockVertices(<Vertex>{
27-
root,
28-
rootWithValue,
29-
connectedVertex,
30-
toBeAdded,
31-
anotherVertex,
32-
a,
33-
b,
34-
c,
35-
});
36-
}
37-
3825
setUp(() {
3926
_initializeVertices();
40-
_unlockVertices();
4127
connectedVertex.addConnection(toBeAdded);
4228
connectedVertex.addConnection(anotherVertex);
4329
});
@@ -65,7 +51,7 @@ void main() {
6551

6652
test('Cannot add vertex with the same key', () {
6753
expect(root.addConnection(b), isTrue);
68-
var bDuplicate = Vertex(b.key)..unlock();
54+
var bDuplicate = Vertex(b.key);
6955
expect(root.addConnection(bDuplicate), isFalse);
7056
});
7157

@@ -79,44 +65,8 @@ void main() {
7965
expect(connectedVertex.removeConnection(a), isFalse);
8066
});
8167

82-
test('Trying to add to a locked vertex throws error', () {
83-
var locked = Vertex('PROTECTED');
84-
expect(() => locked.addConnection(root), throwsA(isA<UnsupportedError>()));
85-
root.lock();
86-
expect(() => root.addConnection(root), throwsA(isA<UnsupportedError>()));
87-
});
88-
89-
test('Trying to add a locked vertex throws error', () {
90-
var locked = Vertex('PROTECTED');
91-
expect(() => root.addConnection(locked), throwsA(isA<UnsupportedError>()));
92-
locked.unlock();
93-
root.lock();
94-
expect(() => locked.addConnection(root), throwsA(isA<UnsupportedError>()));
95-
});
96-
97-
test('Trying to remove from a locked vertex throws error', () {
98-
toBeAdded.lock();
99-
expect(() => connectedVertex.removeConnection(toBeAdded),
100-
throwsA(isA<UnsupportedError>()));
101-
});
102-
103-
test('Trying to remove a locked vertex throws error', () {
104-
connectedVertex.lock();
105-
expect(() => connectedVertex.removeConnection(toBeAdded),
106-
throwsA(isA<UnsupportedError>()));
107-
});
108-
109-
test('Trying to remove a locked vertex throws error', () {
110-
var locked = Vertex('PROTECTED');
111-
expect(() => root.addConnection(locked), throwsA(isA<UnsupportedError>()));
112-
locked.unlock();
113-
root.lock();
114-
expect(() => locked.addConnection(root), throwsA(isA<UnsupportedError>()));
115-
});
116-
11768
test('Check for vertex containment', () {
11869
var newVertex = Vertex('DISCONNECTED');
119-
newVertex.unlock();
12070
expect(connectedVertex.containsConnectionTo(toBeAdded), isTrue);
12171
expect(connectedVertex.containsConnectionTo(newVertex), isFalse);
12272
});

0 commit comments

Comments
 (0)