-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
ArrayBasedLegacyWitnessPathFinder.java
232 lines (204 loc) · 8.26 KB
/
ArrayBasedLegacyWitnessPathFinder.java
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.graphhopper.routing.ch;
import com.carrotsearch.hppc.IntArrayList;
import com.carrotsearch.hppc.IntObjectMap;
import com.carrotsearch.hppc.cursors.IntObjectCursor;
import com.graphhopper.apache.commons.collections.IntDoubleBinaryHeap;
import com.graphhopper.routing.util.TraversalMode;
import com.graphhopper.routing.weighting.Weighting;
import com.graphhopper.storage.CHGraph;
import com.graphhopper.util.EdgeIterator;
import com.graphhopper.util.EdgeIteratorState;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayBasedLegacyWitnessPathFinder extends LegacyWitnessPathFinder {
private double[] weights;
private int[] edges;
private int[] incEdges;
private int[] parents;
private int[] adjNodes;
private boolean[] onOrigPaths;
private List<WitnessSearchEntry> rootParents;
private IntDoubleBinaryHeap heap;
private IntArrayList changedEdges;
public ArrayBasedLegacyWitnessPathFinder(CHGraph graph, Weighting weighting, TraversalMode traversalMode, int maxLevel) {
super(graph, weighting, traversalMode, maxLevel);
final int numOriginalEdges = graph.getBaseGraph().getAllEdges().length();
final int numEntries = 2 * numOriginalEdges;
initStorage(numEntries);
initCollections();
}
@Override
protected void initEntries(IntObjectMap<WitnessSearchEntry> initialEntries) {
int parentId = -1;
for (IntObjectCursor<WitnessSearchEntry> e : initialEntries) {
if (e.value.isDirectCenterNodePath) {
numOnOrigPath++;
avoidNode = e.value.adjNode;
}
// we use descending negative keys for the parents, so we can retrieve them later
setInitEntry(e.key, e.value, --parentId);
changedEdges.add(e.key);
heap.insert_(e.value.weight, e.key);
}
}
@Override
public WitnessSearchEntry getFoundEntry(int origEdge, int adjNode) {
int edgeKey = getEdgeKey(origEdge, adjNode);
if (parents[edgeKey] == -1) {
return new WitnessSearchEntry(origEdge, origEdge, adjNode, Double.POSITIVE_INFINITY, false);
}
WitnessSearchEntry result = getEntryForKey(edgeKey);
WitnessSearchEntry entry = result;
while (parents[edgeKey] >= 0) {
edgeKey = parents[edgeKey];
WitnessSearchEntry parent = getEntryForKey(edgeKey);
entry.parent = parent;
entry = parent;
}
entry.parent = rootParents.get(-parents[edgeKey] - 2);
return result;
}
@Override
public WitnessSearchEntry getFoundEntryNoParents(int origEdge, int adjNode) {
int edgeKey = getEdgeKey(origEdge, adjNode);
if (parents[edgeKey] == -1) {
return null;
} else {
return getEntryForKey(edgeKey);
}
}
@Override
public void findTarget(int targetEdge, int targetNode) {
boolean targetDiscoveredByOrigPath = false;
int targetKey = getEdgeKey(targetEdge, targetNode);
if (heap.isEmpty() || weights[targetKey] < heap.peek_key()) {
return;
}
while (!heap.isEmpty()) {
final int currKey = heap.peek_element();
if (incEdges[currKey] == targetEdge && adjNodes[currKey] == targetNode) {
// important: we only peeked so far so we keep the entry for future searches
break;
}
heap.poll_element();
numEntriesPolled++;
pollCount++;
if (onOrigPaths[currKey]) {
numOnOrigPath--;
}
if (numSettledEdges > maxSettledEdges && !onOrigPaths[currKey]) {
continue;
}
EdgeIterator iter = outEdgeExplorer.setBaseNode(adjNodes[currKey]);
while (iter.next()) {
if ((!traversalMode.hasUTurnSupport() && iter.getFirstOrigEdge() == incEdges[currKey]) ||
isContracted(iter.getAdjNode())) {
continue;
}
double weight = weighting.calcWeight(iter, false, incEdges[currKey]) + weights[currKey];
if (Double.isInfinite(weight)) {
continue;
}
boolean onOrigPath = onOrigPaths[currKey] && iter.getBaseNode() == iter.getAdjNode();
int key = getEdgeKey(iter.getLastOrigEdge(), iter.getAdjNode());
if (edges[key] == -1) {
setEntry(key, iter, weight, currKey, onOrigPath);
if (targetDiscoveredByOrigPath(targetEdge, targetNode, currKey, iter)) {
targetDiscoveredByOrigPath = true;
}
changedEdges.add(key);
heap.insert_(weight, key);
} else if (weight < weights[key]) {
updateEntry(currKey, iter, weight, key, onOrigPath);
if (targetDiscoveredByOrigPath(targetEdge, targetNode, currKey, iter)) {
targetDiscoveredByOrigPath = true;
}
heap.update_(weight, key);
}
}
numSettledEdges++;
if (numOnOrigPath < 1 && !targetDiscoveredByOrigPath) {
break;
}
}
}
private WitnessSearchEntry getEntryForKey(int edgeKey) {
return new WitnessSearchEntry(edges[edgeKey], incEdges[edgeKey], adjNodes[edgeKey], weights[edgeKey], onOrigPaths[edgeKey]);
}
private void setInitEntry(int key, WitnessSearchEntry entry, int parentId) {
edges[key] = entry.edge;
incEdges[key] = entry.incEdge;
adjNodes[key] = entry.adjNode;
weights[key] = entry.weight;
parents[key] = parentId;
rootParents.add(entry.getParent());
onOrigPaths[key] = entry.isDirectCenterNodePath;
}
private void setEntry(int key, EdgeIteratorState iter, double weight, int parent, boolean onOrigPath) {
edges[key] = iter.getEdge();
incEdges[key] = iter.getLastOrigEdge();
adjNodes[key] = iter.getAdjNode();
weights[key] = weight;
parents[key] = parent;
if (onOrigPath) {
onOrigPaths[key] = true;
numOnOrigPath++;
}
}
private void updateEntry(int currKey, EdgeIteratorState iter, double weight, int key, boolean onOrigPath) {
edges[key] = iter.getEdge();
weights[key] = weight;
parents[key] = currKey;
if (onOrigPath) {
if (!onOrigPaths[key]) {
numOnOrigPath++;
}
} else {
if (onOrigPaths[key]) {
numOnOrigPath--;
}
}
onOrigPaths[key] = onOrigPath;
}
@Override
protected void doReset() {
for (int i = 0; i < changedEdges.size(); ++i) {
resetEntry(changedEdges.get(i));
}
rootParents.clear();
changedEdges.elementsCount = 0;
heap.clear();
}
private void initStorage(int numEntries) {
weights = new double[numEntries];
Arrays.fill(weights, Double.POSITIVE_INFINITY);
edges = new int[numEntries];
Arrays.fill(edges, EdgeIterator.NO_EDGE);
incEdges = new int[numEntries];
Arrays.fill(incEdges, EdgeIterator.NO_EDGE);
parents = new int[numEntries];
Arrays.fill(parents, -1);
adjNodes = new int[numEntries];
Arrays.fill(adjNodes, -1);
onOrigPaths = new boolean[numEntries];
Arrays.fill(onOrigPaths, false);
}
private void resetEntry(int key) {
weights[key] = Double.POSITIVE_INFINITY;
edges[key] = EdgeIterator.NO_EDGE;
incEdges[key] = EdgeIterator.NO_EDGE;
parents[key] = -1;
adjNodes[key] = -1;
onOrigPaths[key] = false;
}
private boolean targetDiscoveredByOrigPath(int targetEdge, int targetNode, int currKey, EdgeIteratorState iter) {
return onOrigPaths[currKey] && iter.getLastOrigEdge() == targetEdge && iter.getAdjNode() == targetNode;
}
private void initCollections() {
// todo: so far these initial capacities are purely guessed
rootParents = new ArrayList<>(10);
changedEdges = new IntArrayList(1000);
heap = new IntDoubleBinaryHeap(1000);
}
}