Skip to content

Commit

Permalink
Fix memory leak in Graph
Browse files Browse the repository at this point in the history
  • Loading branch information
jonasspinner committed Feb 11, 2024
1 parent df4db6d commit a18f449
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/main/cpp/ds/graph/Graph.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <memory>
#include "ds/set/ArrayBitset.hpp"
#include "ds/set/SortedVectorSet.hpp"

Expand All @@ -22,36 +23,36 @@ class Graph {
bool dense_;

/** Adjacency sets. */
std::vector<basic_set<int>*> adj_;
std::vector<std::unique_ptr<basic_set<int>>> adj_;

/** Masks for removed vertices. */
basic_set<int>* removed_;
std::unique_ptr<basic_set<int>> removed_;

bool is_valid(int v) const { return 0 <= v && v < static_cast<int>(adj_.size()) && !removed_->get(v); }

basic_set<int>* create_set(std::size_t n, bool dense) {
std::unique_ptr<basic_set<int>> create_set(std::size_t n, bool dense) {
if (dense) {
if (n <= 1 << 6) {
return new ArrayBitset6(n);
return std::make_unique<ArrayBitset6>(n);
} else if (n <= 1 << 7) {
return new ArrayBitset7(n);
return std::make_unique<ArrayBitset7>(n);
} else if (n <= 1 << 8) {
return new ArrayBitset8(n);
return std::make_unique<ArrayBitset8>(n);
} else if (n <= 1 << 9) {
return new ArrayBitset9(n);
return std::make_unique<ArrayBitset9>(n);
} else if (n <= 1 << 10) {
return new ArrayBitset10(n);
return std::make_unique<ArrayBitset10>(n);
} else if (n <= 1 << 11) {
return new ArrayBitset11(n);
return std::make_unique<ArrayBitset11>(n);
} else if (n <= 1 << 12) {
return new ArrayBitset12(n);
return std::make_unique<ArrayBitset12>(n);
} else if (n <= 1 << 13) {
return new ArrayBitset13(n);
return std::make_unique<ArrayBitset13>(n);
} else {
throw std::invalid_argument("Graph: n too large for dense representation");
}
} else {
return new SortedVectorSet();
return std::make_unique<SortedVectorSet>();
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main/cpp/ds/set/basic_set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ class basic_set {
virtual int pop_front() = 0; // -1: not found
virtual int back() const = 0; // -1: not found
virtual int pop_back() = 0; // -1: not found
virtual ~basic_set() = default;
};
} // namespace ds

0 comments on commit a18f449

Please sign in to comment.