-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathmakeNBASearchStatePool.js
118 lines (97 loc) · 2.65 KB
/
makeNBASearchStatePool.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
module.exports = makeNBASearchStatePool;
/**
* Creates new instance of NBASearchState. The instance stores information
* about search state, and is used by NBA* algorithm.
*
* @param {Object} node - original graph node
*/
function NBASearchState(node) {
/**
* Original graph node.
*/
this.node = node;
/**
* Parent of this node in forward search
*/
this.p1 = null;
/**
* Parent of this node in reverse search
*/
this.p2 = null;
/**
* If this is set to true, then the node was already processed
* and we should not touch it anymore.
*/
this.closed = false;
/**
* Actual distance from this node to its parent in forward search
*/
this.g1 = Number.POSITIVE_INFINITY;
/**
* Actual distance from this node to its parent in reverse search
*/
this.g2 = Number.POSITIVE_INFINITY;
/**
* Underestimated distance from this node to the path-finding source.
*/
this.f1 = Number.POSITIVE_INFINITY;
/**
* Underestimated distance from this node to the path-finding target.
*/
this.f2 = Number.POSITIVE_INFINITY;
// used to reconstruct heap when fScore is updated. TODO: do I need them both?
/**
* Index of this node in the forward heap.
*/
this.h1 = -1;
/**
* Index of this node in the reverse heap.
*/
this.h2 = -1;
}
/**
* As path-finding is memory-intensive process, we want to reduce pressure on
* garbage collector. This class helps us to recycle path-finding nodes and significantly
* reduces the search time (~20% faster than without it).
*/
function makeNBASearchStatePool() {
var currentInCache = 0;
var nodeCache = [];
return {
/**
* Creates a new NBASearchState instance
*/
createNewState: createNewState,
/**
* Marks all created instances available for recycling.
*/
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.p1 = null;
cached.p2 = null;
cached.closed = false;
cached.g1 = Number.POSITIVE_INFINITY;
cached.g2 = Number.POSITIVE_INFINITY;
cached.f1 = Number.POSITIVE_INFINITY;
cached.f2 = Number.POSITIVE_INFINITY;
// used to reconstruct heap when fScore is updated.
cached.h1 = -1;
cached.h2 = -1;
} else {
cached = new NBASearchState(node);
nodeCache[currentInCache] = cached;
}
currentInCache++;
return cached;
}
}