Skip to content

Commit a33ea70

Browse files
committed
fix: update initializer list matrix constructor
1 parent 5a418d6 commit a33ea70

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

cpp/src/graph/graph.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <cstddef>
22
#include <memory>
3+
#include <set>
34

45
#include "algorithms_archive/graph/graph.h"
56

@@ -8,8 +9,16 @@ using namespace algorithms_archive::graph;
89
AdjacencyMatrixGraph::AdjacencyMatrixGraph(std::size_t size)
910
: size_{size}, matrix_{std::unique_ptr<int[]>{new int[size * size]}} {}
1011

11-
AdjacencyMatrixGraph::AdjacencyMatrixGraph(std::initializer_list<Edge> edges)
12-
: AdjacencyMatrixGraph{edges.size()} {
12+
AdjacencyMatrixGraph::AdjacencyMatrixGraph(std::initializer_list<Edge> edges) {
13+
// count number of unique nodes in the given edges list
14+
std::set<int> nodes;
15+
for (const auto &edge : edges) {
16+
nodes.insert(edge.from.id);
17+
nodes.insert(edge.to.id);
18+
}
19+
20+
size_ = nodes.size();
21+
matrix_ = std::unique_ptr<int[]>{new int[size_ * size_]};
1322
for (const auto &edge : edges) {
1423
addEdge(edge);
1524
}

0 commit comments

Comments
 (0)