fixed dijkstra memleak

new dijkstra interfaces and abort options
refactored all files accordingly
enable logging via compile-time defines
This commit is contained in:
2016-03-18 10:18:17 +01:00
parent f8d7f768f1
commit d0801606b7
8 changed files with 44 additions and 14 deletions

View File

@@ -69,6 +69,7 @@ ADD_DEFINITIONS(
-DWITH_TESTS
-DWITH_ASSERTIONS
-DWITH_DEBUG_LOG
-march=native
)

View File

@@ -49,7 +49,7 @@ public:
gen.seed(1234);
// build all shortest path to reach th target
dijkstra.build(target, target, acc);
dijkstra.build(&target, acc);
// attach a corresponding weight-information to each user-grid-node
for (T& node : grid) {

View File

@@ -45,7 +45,7 @@ public:
gen.seed(1234);
// build all shortest path to reach th target
dijkstra.build(target, target, acc);
dijkstra.build(&target, acc);
// attach a corresponding weight-information to each user-grid-node
for (T& node : grid) {

View File

@@ -108,7 +108,7 @@ public:
gen.seed(1234);
// build all shortest path to reach th target
dijkstra.build(target, target, acc);
dijkstra.build(&target, acc);
}

View File

@@ -9,12 +9,12 @@
/** quick and dirty workaround */
static decltype(Time::tick()) LogLastTick;
#ifdef WITH_DEBUG_LOG
class Log {
public:
static void add(const char* comp, const std::string what, const bool nl = true) {
addComp(comp);
std::cout << what;
@@ -50,4 +50,16 @@ private:
};
#else
class Log {
public:
static void add(const char* comp, const std::string what, const bool nl = true) { (void)comp; (void) what; (void) nl; }
static void add(const std::string& component, const std::string what, const bool nl = true) { (void)component; (void) what; (void) nl; }
static void tick() {;}
static void tock() {;}
};
#endif
#endif // DEBUG_H

View File

@@ -22,20 +22,34 @@ template <typename T> class Dijkstra {
public:
/** dtor: cleanup */
~Dijkstra() {
for (auto it : nodes) {delete it.second;}
}
/** get the dijkstra-pendant for the given user-node. null if none matches */
DijkstraNode<T>* getNode(const T& userNode) const {
auto it = nodes.find(&userNode);
return (unlikely(it == nodes.end())) ? (nullptr) : (it->second);
}
/** build shortest path from start to end using the provided wrapper-class */
template <typename Access> void build(const T& start, const T& end, const Access& acc) {
/** calculate all shortest paths from ANY node to the given destination */
template <typename Access> void build(const T* end, const Access& acc) {
build(end, nullptr, acc, NAN);
}
/**
* build the shortest path from start to end using the provided access-wrapper-class.
* if end is null, the algorithm will terminate only if every possible node was checked.
* if given, the algorithm will also terminate if the current distance is already > the given maximum
*/
template <typename Access> void build(const T* start, const T* end, const Access& acc, const float maxWeight = 0) {
// NOTE: end is currently ignored!
// runs until all nodes were evaluated
(void) end;
Log::add("Dijkstra", "calculating dijkstra from " + (std::string)start + " to ALL OTHER nodes", false);
Log::add("Dijkstra", "calculating dijkstra from " + (std::string)*start + " to ALL OTHER nodes", false);
Log::tick();
// cleanup previous runs
@@ -45,7 +59,7 @@ public:
ToProcess toBeProcessedNodes;
// run from start
const T* cur = &start;
const T* cur = start;
// create a node for the start element
DijkstraNode<T>* dnStart = getOrCreateNode(cur);
@@ -60,8 +74,11 @@ public:
// get the next to-be-processed node
DijkstraNode<T>* dnSrc = toBeProcessedNodes.pop();
// stop when end was reached??
//if (dnSrc->element == &end) {break;}
// when an end is given, stop when end was reached
if (end != nullptr && dnSrc->element == end) {break;}
// when a maximum weight is given, stop when current cum-dist > maxWeight
if (maxWeight != 0 && dnSrc->cumWeight > maxWeight) {break;}
// visit (and maybe update) each neighbor of the current element
for (int i = 0; i < acc.getNumNeighbors(*dnSrc->element); ++i) {

View File

@@ -53,7 +53,7 @@ TEST(TestAll, Nav) {
const GP& start = g.getNodeFor(GridPoint(500,200,20));
const GP& end = g.getNodeFor(GridPoint(1200,200,340));
//const GP& end = g.getNodeFor(GridPoint(1300,1300,20));
d.build(start, end, tmp);
d.build(&start, &end, tmp);
// add the path's importance to the grid
gi.addImportance(g, d.getNode(start), d.getNode(end));

View File

@@ -31,7 +31,7 @@ TEST(Dijkstra, build) {
} tmp(grid);
Dijkstra<GP> d;
d.build(grid[idx5], grid[idx3], tmp);
d.build(&grid[idx5], &grid[idx3], tmp, 99999);
// start node must be "idx5"
DijkstraNode<GP>* n = d.getNode(grid[idx5]);
@@ -98,7 +98,7 @@ void dijkstra(Grid<GP>& grid) {
} tmp(grid);
Dijkstra<GP> d;
d.build(grid[0], grid[0], tmp);
d.build(&grid[0], tmp);
}