Skip to content

Commit fe30abf

Browse files
committed
lab 4 update
1 parent e75c290 commit fe30abf

File tree

1 file changed

+19
-21
lines changed

1 file changed

+19
-21
lines changed

LibraryCPPTemplate/graph.h

+19-21
Original file line numberDiff line numberDiff line change
@@ -24,35 +24,27 @@ class Graph {
2424
}
2525

2626
~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;
3238
}
3339
}
3440
}
3541

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-
4942
Graph& operator=(const Graph& other) {
5043
if (this == &other) {
5144
return *this;
5245
}
53-
for (size_t i = 0; i < edgeMatrix.size(); i++) {
54-
delete edgeMatrix[i];
55-
}
46+
clearEdges();
47+
5648
vertices = other.vertices;
5749
edgeMatrix.resize(other.edgeMatrix.size());
5850
for (size_t i = 0; i < other.edgeMatrix.size(); ++i) {
@@ -284,6 +276,12 @@ class Graph {
284276

285277

286278
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+
}
287285
Vector<Vertex> vertices;
288286
Vector<Edge*> edgeMatrix;
289287
GraphType type;

0 commit comments

Comments
 (0)