@@ -24,35 +24,27 @@ class Graph {
24
24
}
25
25
26
26
~Graph () {
27
- size_t qty_vertex = get_VertexAmount ();
28
- for (size_t i = 0 ; i < qty_vertex; ++i) {
29
- for (size_t j = 0 ; j < qty_vertex; ++j) {
30
- delete edgeMatrix[i * qty_vertex + j];
31
- edgeMatrix[i * qty_vertex + j] = nullptr ;
27
+ clearEdges ();
28
+ }
29
+
30
+ Graph (const Graph& other) : type(other.type), vertices(other.vertices) {
31
+ edgeMatrix.resize (other.edgeMatrix .size ());
32
+ for (size_t i = 0 ; i < other.edgeMatrix .size (); ++i) {
33
+ if (other.edgeMatrix [i] != nullptr ) {
34
+ edgeMatrix[i] = new Edge (*other.edgeMatrix [i]);
35
+ }
36
+ else {
37
+ edgeMatrix[i] = nullptr ;
32
38
}
33
39
}
34
40
}
35
41
36
-
37
- Graph (const Graph& a) : type(a.type), vertices(a.vertices) {
38
- edgeMatrix.resize (a.edgeMatrix .size ());
39
- for (size_t i = 0 ; i < a.edgeMatrix .size (); ++i) {
40
- if (a.edgeMatrix [i] != nullptr ) {
41
- edgeMatrix[i] = new Edge (*a.edgeMatrix [i]);
42
- } else {
43
- edgeMatrix[i] = nullptr ;
44
- }
45
- }
46
- }
47
-
48
-
49
42
Graph& operator =(const Graph& other) {
50
43
if (this == &other) {
51
44
return *this ;
52
45
}
53
- for (size_t i = 0 ; i < edgeMatrix.size (); i++) {
54
- delete edgeMatrix[i];
55
- }
46
+ clearEdges ();
47
+
56
48
vertices = other.vertices ;
57
49
edgeMatrix.resize (other.edgeMatrix .size ());
58
50
for (size_t i = 0 ; i < other.edgeMatrix .size (); ++i) {
@@ -284,6 +276,12 @@ class Graph {
284
276
285
277
286
278
private:
279
+ void clearEdges () {
280
+ for (size_t i = 0 ; i < edgeMatrix.size (); i++) {
281
+ delete edgeMatrix.get (i);
282
+ edgeMatrix.set (i, nullptr );
283
+ }
284
+ }
287
285
Vector<Vertex> vertices;
288
286
Vector<Edge*> edgeMatrix;
289
287
GraphType type;
0 commit comments