ngraph.path/a-star/makeSearchStatePool.js
2025-11-14 21:38:30 -08:00

63 lines
1.5 KiB
JavaScript

/**
* This class represents a single search node in the exploration tree for
* A* algorithm.
*
* @param {Object} node original node in the graph
*/
function NodeSearchState(node) {
this.node = node;
// How we came to this node?
this.parent = null;
this.closed = false;
this.open = 0;
this.distanceToSource = Number.POSITIVE_INFINITY;
// the f(n) = g(n) + h(n) value
this.fScore = Number.POSITIVE_INFINITY;
// used to reconstruct heap when fScore is updated.
this.heapIndex = -1;
};
export default function makeSearchStatePool() {
var currentInCache = 0;
var nodeCache = [];
return {
createNewState: createNewState,
reset: reset
};
function reset() {
currentInCache = 0;
}
function createNewState(node) {
var cached = nodeCache[currentInCache];
if (cached) {
// TODO: This almost duplicates constructor code. Not sure if
// it would impact performance if I move this code into a function
cached.node = node;
// How we came to this node?
cached.parent = null;
cached.closed = false;
cached.open = 0;
cached.distanceToSource = Number.POSITIVE_INFINITY;
// the f(n) = g(n) + h(n) value
cached.fScore = Number.POSITIVE_INFINITY;
// used to reconstruct heap when fScore is updated.
cached.heapIndex = -1;
} else {
cached = new NodeSearchState(node);
nodeCache[currentInCache] = cached;
}
currentInCache++;
return cached;
}
}