diff --git a/inc/graph.hpp b/inc/graph.hpp index c915ab5..6c3422c 100644 --- a/inc/graph.hpp +++ b/inc/graph.hpp @@ -6,7 +6,7 @@ private: int *resource_counts; struct m{ int x, y; - int **data; + std::vector> data; } matrix; // Tell me, Mr. Anderson, what good is a phone call if you are unable to speak? public: diff --git a/src/graph.cpp b/src/graph.cpp index 077c828..5f02e3d 100644 --- a/src/graph.cpp +++ b/src/graph.cpp @@ -1,42 +1,26 @@ #include +#include #include "graph.hpp" -/* knotted: - Perform the knot detection algorithm on the adjacency matrix to detect deadlocks - (!THIS MODIFIES THE GRAPH!) - @params: none, uses class-internal data - @returns: - int: - true if graph is knotted (deadlock) - false if graph is not (no deadlock) -*/ -bool graph::knotted() { - return false; -} - graph::graph (const int processes, const int resources) { num_processes = processes; num_resources = resources; matrix.x = processes + resources; matrix.y = matrix.x; - matrix.data = new int*[matrix.x]; + // Reserve correct number of entries to minimize reallocation + matrix.data.reserve(matrix.x); for (int x = 0; x < matrix.x; x++) { - matrix.data[x] = new int[matrix.y]; + // Make a new x (vector) + matrix.data.push_back({}); + // Reserve correct number of entries + matrix.data[x].reserve(matrix.y); + for (int y = 0; y < matrix.y; y++) { + // Make a new y (int) + matrix.data[x].push_back({}); + } } } graph::~graph() { - /* clean up the 2D matrix */ - if (matrix.data) { - if (matrix.x || matrix.y) { - // delete y axes - for (int x = 0; x < matrix.x; x++) { - delete matrix.data[x]; - } - // delete x axis - delete matrix.data; - matrix.x = 0; matrix.y = 0; - } - } - free(resource_counts); + /* clean up..? */ } diff --git a/src/knotted.cpp b/src/knotted.cpp new file mode 100644 index 0000000..e49ce57 --- /dev/null +++ b/src/knotted.cpp @@ -0,0 +1,16 @@ +#include +#include +#include "graph.hpp" + +/* knotted: + Perform the knot detection algorithm on the adjacency matrix to detect deadlocks + (!THIS MODIFIES THE GRAPH!) + @params: none, uses class-internal data + @returns: + int: + true if graph is knotted (deadlock) + false if graph is not (no deadlock) +*/ +bool graph::knotted() { + return false; +} \ No newline at end of file diff --git a/src/reducible.cpp b/src/reducible.cpp index 53624fe..4bec6da 100644 --- a/src/reducible.cpp +++ b/src/reducible.cpp @@ -1,4 +1,5 @@ #include +#include #include "graph.hpp" /* reducible: