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

@@ -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) {