From 5f4ed7a789dbe338a1113d5498f3038f42b6dfd6 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Thu, 29 Dec 2022 18:44:30 +0200 Subject: [SDP/P] Fixed memory leaks --- .../Project/exb/City.cpp | 83 -------------------- .../Project/exb/CityVisit.cpp | 88 ++++++++++++++++++++++ .../Project/exb/CityVisit.h | 2 + .../Project/exc/main.cpp | 4 + 4 files changed, 94 insertions(+), 83 deletions(-) delete mode 100644 Year 2/Structures and data algorithms/Project/exb/City.cpp create mode 100644 Year 2/Structures and data algorithms/Project/exb/CityVisit.cpp diff --git a/Year 2/Structures and data algorithms/Project/exb/City.cpp b/Year 2/Structures and data algorithms/Project/exb/City.cpp deleted file mode 100644 index 1fb04ca..0000000 --- a/Year 2/Structures and data algorithms/Project/exb/City.cpp +++ /dev/null @@ -1,83 +0,0 @@ -#include "CityVisit.h" - -CityVisit::Landmark* CityVisit::find(const std::string& name) { - for (Landmark* l : landmarks) { - if (l->name == name) - return l; - } - return nullptr; -} - -void CityVisit::pushLandmarks(const std::string& from, const std::string& to, unsigned time) { - Landmark* a = find(from); - if (a == nullptr) { - a = new Landmark(from); - landmarks.push_back(a); - } - Landmark* b = find(to); - if (b == nullptr) { - b = new Landmark(to); - landmarks.push_back(b); - } - - a->visitable.push_back({ b, time }); - b->visitable.push_back({ a, time }); -} - -void CityVisit::parse(std::ifstream& inFile) { - unsigned k = 0, r = 0; - inFile >> k >> r; - - std::string name1; - std::string name2; - unsigned time = 0; - while (r-- > 0) { - inFile >> name1 >> name2 >> time; - pushLandmarks(name1, name2, time); - } - inFile >> availableTime; -} - -void CityVisit::visitTheMostInTimeRec(Landmark* currentLandmark, std::deque currPath, std::deque unique, int timeLeft, std::deque& bestPath, unsigned& uniqueCount) { - if (timeLeft < 0) return; - - bool currentExists = false; - for (const Landmark* l : unique) { - if (l == currentLandmark) { - currentExists = true; - break; - } - } - if (!currentExists) - unique.push_back(currentLandmark); - - currPath.push_back(currentLandmark); - - if (currentLandmark->name == "Railstation" && unique.size() > uniqueCount) { - bestPath = currPath; - uniqueCount = unique.size(); - } - - for (auto lp : currentLandmark->visitable) { - visitTheMostInTimeRec(lp.first, currPath, unique, timeLeft - lp.second, bestPath, uniqueCount); - } -} - -std::deque CityVisit::visitTheMostInTime(std::ifstream& inFile) { - landmarks.clear(); - parse(inFile); - - Landmark* station = find("Railstation"); - if (station == nullptr) - throw std::logic_error("No landmark named Railstation exists!"); - - std::deque outLandmarks; - unsigned uniqueCount = 0; - visitTheMostInTimeRec(station, std::deque(), std::deque(), availableTime, outLandmarks, uniqueCount); - - std::deque out; - for (Landmark* l : outLandmarks) { - out.push_back(l->name); - } - return out; -} diff --git a/Year 2/Structures and data algorithms/Project/exb/CityVisit.cpp b/Year 2/Structures and data algorithms/Project/exb/CityVisit.cpp new file mode 100644 index 0000000..af5128d --- /dev/null +++ b/Year 2/Structures and data algorithms/Project/exb/CityVisit.cpp @@ -0,0 +1,88 @@ +#include "CityVisit.h" + +CityVisit::~CityVisit() { + for (Landmark* l : landmarks) + delete l; +} + +CityVisit::Landmark* CityVisit::find(const std::string& name) { + for (Landmark* l : landmarks) { + if (l->name == name) + return l; + } + return nullptr; +} + +void CityVisit::pushLandmarks(const std::string& from, const std::string& to, unsigned time) { + Landmark* a = find(from); + if (a == nullptr) { + a = new Landmark(from); + landmarks.push_back(a); + } + Landmark* b = find(to); + if (b == nullptr) { + b = new Landmark(to); + landmarks.push_back(b); + } + + a->visitable.push_back({ b, time }); + b->visitable.push_back({ a, time }); +} + +void CityVisit::parse(std::ifstream& inFile) { + unsigned k = 0, r = 0; + inFile >> k >> r; + + std::string name1; + std::string name2; + unsigned time = 0; + while (r-- > 0) { + inFile >> name1 >> name2 >> time; + pushLandmarks(name1, name2, time); + } + inFile >> availableTime; +} + +void CityVisit::visitTheMostInTimeRec(Landmark* currentLandmark, std::deque currPath, std::deque unique, int timeLeft, std::deque& bestPath, unsigned& uniqueCount) { + if (timeLeft < 0) return; + + bool currentExists = false; + for (const Landmark* l : unique) { + if (l == currentLandmark) { + currentExists = true; + break; + } + } + if (!currentExists) + unique.push_back(currentLandmark); + + currPath.push_back(currentLandmark); + + if (currentLandmark->name == "Railstation" && unique.size() > uniqueCount) { + bestPath = currPath; + uniqueCount = unique.size(); + } + + for (auto lp : currentLandmark->visitable) { + visitTheMostInTimeRec(lp.first, currPath, unique, timeLeft - lp.second, bestPath, uniqueCount); + } +} + +std::deque CityVisit::visitTheMostInTime(std::ifstream& inFile) { + landmarks.clear(); + parse(inFile); + + Landmark* station = find("Railstation"); + if (station == nullptr) + throw std::logic_error("No landmark named Railstation exists!"); + + std::deque outLandmarks; + unsigned uniqueCount = 0; + visitTheMostInTimeRec(station, std::deque(), std::deque(), availableTime, outLandmarks, uniqueCount); + + std::deque out; + for (Landmark* l : outLandmarks) { + out.push_back(l->name); + } + return out; +} diff --git a/Year 2/Structures and data algorithms/Project/exb/CityVisit.h b/Year 2/Structures and data algorithms/Project/exb/CityVisit.h index c513542..0dbe1fa 100644 --- a/Year 2/Structures and data algorithms/Project/exb/CityVisit.h +++ b/Year 2/Structures and data algorithms/Project/exb/CityVisit.h @@ -21,6 +21,8 @@ class CityVisit { void visitTheMostInTimeRec(Landmark* currentLandmark, std::deque currPath, std::deque unique, int timeLeft, std::deque& bestPath, unsigned& uniqueCount); public: + ~CityVisit(); + void pushLandmarks(const std::string& from, const std::string& to, unsigned time); void parse(std::ifstream& inFile); diff --git a/Year 2/Structures and data algorithms/Project/exc/main.cpp b/Year 2/Structures and data algorithms/Project/exc/main.cpp index 95636ce..d482b7e 100644 --- a/Year 2/Structures and data algorithms/Project/exc/main.cpp +++ b/Year 2/Structures and data algorithms/Project/exc/main.cpp @@ -69,4 +69,8 @@ int main() { cb->removeUselessSubBoxes(); cb->print(std::cout) << std::endl; } + + // Free memory + for (Box* b : boxes) + delete b; } -- cgit v1.3.1-23-gd82d