dijkstra is now bleching fast
deleting from the grid is now bleaching fast added new helper methods many new test-cases many new methods for geo classes and others added a bunch of new grid-walkers
This commit is contained in:
128
grid/Grid.h
128
grid/Grid.h
@@ -250,61 +250,123 @@ public:
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* remove all nodes, marked for deletion.
|
||||
* BEWARE: this will invalidate all indices used externally!
|
||||
*/
|
||||
void cleanup() {
|
||||
|
||||
Log::add(name, "running grid cleanup");
|
||||
|
||||
// check every single node
|
||||
for (size_t i = 0; i < nodes.size(); ++i) {
|
||||
|
||||
// is this node marked as "deleted"? (idx == -1)
|
||||
if (nodes[i]._idx == -1) {
|
||||
|
||||
// remove this node
|
||||
deleteNode(i);
|
||||
--i;
|
||||
Log::add(name, "running grid cleanup", false);
|
||||
Log::tick();
|
||||
|
||||
// generate a look-up-table for oldIndex (before deletion) -> newIndex (after deletion)
|
||||
std::vector<int> oldToNew; oldToNew.resize(nodes.size());
|
||||
int newIdx = 0;
|
||||
for (size_t oldIdx = 0; oldIdx < nodes.size(); ++oldIdx) {
|
||||
if (nodes[oldIdx].getIdx() != -1) {
|
||||
oldToNew[oldIdx] = newIdx;
|
||||
++newIdx;
|
||||
}
|
||||
}
|
||||
|
||||
// rebuild hashes
|
||||
Log::add(name, "rebuilding UID hashes");
|
||||
// adjust all indices from the old to the new mapping
|
||||
for (size_t i = 0; i < nodes.size(); ++i) {
|
||||
|
||||
// skip the nodes actually marked for deletion
|
||||
if (nodes[i]._idx == -1) {continue;}
|
||||
|
||||
// adjust the node's index
|
||||
nodes[i]._idx = oldToNew[nodes[i]._idx];
|
||||
|
||||
// adjust its neighbor's indices
|
||||
for (int j = 0; j < nodes[i]._numNeighbors; ++j) {
|
||||
nodes[i]._neighbors[j] = oldToNew[nodes[i]._neighbors[j]];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// MUCH(!!!) faster than deleting nodes from the existing node-vector
|
||||
// is to build a new one and swap those two
|
||||
std::vector<T> newNodes;
|
||||
for (size_t i = 0; i < nodes.size(); ++i) {
|
||||
if (nodes[i]._idx != -1) {newNodes.push_back(nodes[i]);}
|
||||
}
|
||||
std::swap(nodes, newNodes);
|
||||
|
||||
Log::tock();
|
||||
|
||||
rebuildHashes();
|
||||
|
||||
}
|
||||
|
||||
/** rebuild the UID-hash-list */
|
||||
void rebuildHashes() {
|
||||
|
||||
Log::add(name, "rebuilding UID hashes", false);
|
||||
Log::tick();
|
||||
|
||||
hashes.clear();
|
||||
for (size_t idx = 0; idx < nodes.size(); ++idx) {
|
||||
hashes[getUID(nodes[idx])] = idx;
|
||||
}
|
||||
|
||||
Log::tock();
|
||||
|
||||
}
|
||||
|
||||
// /**
|
||||
// * remove all nodes, marked for deletion.
|
||||
// * BEWARE: this will invalidate all indices used externally!
|
||||
// */
|
||||
// void cleanupOld() {
|
||||
|
||||
// Log::add(name, "running grid cleanup");
|
||||
|
||||
// // check every single node
|
||||
// for (size_t i = 0; i < nodes.size(); ++i) {
|
||||
|
||||
// // is this node marked as "deleted"? (idx == -1)
|
||||
// if (nodes[i]._idx == -1) {
|
||||
|
||||
// // remove this node
|
||||
// deleteNode(i);
|
||||
// --i;
|
||||
|
||||
// }
|
||||
// }
|
||||
|
||||
// // rebuild hashes
|
||||
// Log::add(name, "rebuilding UID hashes", false);
|
||||
// Log::tick();
|
||||
// hashes.clear();
|
||||
// for (size_t idx = 0; idx < nodes.size(); ++idx) {
|
||||
// hashes[getUID(nodes[idx])] = idx;
|
||||
// }
|
||||
// Log::tock();
|
||||
|
||||
// }
|
||||
|
||||
private:
|
||||
|
||||
/** hard-delete the given node */
|
||||
void deleteNode(const int idx) {
|
||||
// /** hard-delete the given node */
|
||||
// void deleteNode(const int idx) {
|
||||
|
||||
_assertBetween(idx, 0, nodes.size()-1, "index out of bounds");
|
||||
// _assertBetween(idx, 0, nodes.size()-1, "index out of bounds");
|
||||
|
||||
// COMPLEX AND SLOW AS HELL.. BUT UGLY TO REWIRTE TO BE CORRECT
|
||||
// // COMPLEX AND SLOW AS HELL.. BUT UGLY TO REWIRTE TO BE CORRECT
|
||||
|
||||
// remove him from the node list (reclaim its memory and its index)
|
||||
nodes.erase(nodes.begin()+idx);
|
||||
// // remove him from the node list (reclaim its memory and its index)
|
||||
// nodes.erase(nodes.begin()+idx);
|
||||
|
||||
// decrement the index for all of the following nodes and adjust neighbor references
|
||||
for (size_t i = 0; i < nodes.size(); ++i) {
|
||||
// // decrement the index for all of the following nodes and adjust neighbor references
|
||||
// for (size_t i = 0; i < nodes.size(); ++i) {
|
||||
|
||||
// decrement the higher indices (reclaim the free one)
|
||||
if (nodes[i]._idx >= idx) { --nodes[i]._idx;}
|
||||
// // decrement the higher indices (reclaim the free one)
|
||||
// if (nodes[i]._idx >= idx) { --nodes[i]._idx;}
|
||||
|
||||
// adjust the neighbor references (decrement by one)
|
||||
for (int n = 0; n < nodes[i]._numNeighbors; ++n) {
|
||||
if (nodes[i]._neighbors[n] >= idx) {--nodes[i]._neighbors[n];}
|
||||
}
|
||||
// // adjust the neighbor references (decrement by one)
|
||||
// for (int n = 0; n < nodes[i]._numNeighbors; ++n) {
|
||||
// if (nodes[i]._neighbors[n] >= idx) {--nodes[i]._neighbors[n];}
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
// }
|
||||
// }
|
||||
|
||||
public:
|
||||
|
||||
|
||||
Reference in New Issue
Block a user