forked from Project-OSRM/osrm-backend
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery_heap.hpp
334 lines (281 loc) · 8.53 KB
/
query_heap.hpp
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#ifndef OSRM_UTIL_QUERY_HEAP_HPP
#define OSRM_UTIL_QUERY_HEAP_HPP
#include <boost/assert.hpp>
#include <boost/heap/d_ary_heap.hpp>
#include <algorithm>
#include <limits>
#include <map>
#include <unordered_map>
#include <vector>
namespace osrm
{
namespace util
{
template <typename NodeID, typename Key> class GenerationArrayStorage
{
using GenerationCounter = std::uint16_t;
public:
explicit GenerationArrayStorage(std::size_t size)
: positions(size, 0), generation(1), generations(size, 0)
{
}
Key &operator[](NodeID node)
{
generation[node] = generation;
return positions[node];
}
Key peek_index(const NodeID node) const
{
if (generations[node] < generation)
{
return std::numeric_limits<Key>::max();
}
return positions[node];
}
void Clear()
{
generation++;
// if generation overflows we end up at 0 again and need to clear the vector
if (generation == 0)
{
generation = 1;
std::fill(generations.begin(), generations.end(), 0);
}
}
private:
GenerationCounter generation;
std::vector<GenerationCounter> generations;
std::vector<Key> positions;
};
template <typename NodeID, typename Key> class ArrayStorage
{
public:
explicit ArrayStorage(std::size_t size) : positions(size, 0) {}
~ArrayStorage() {}
Key &operator[](NodeID node) { return positions[node]; }
Key peek_index(const NodeID node) const { return positions[node]; }
void Clear() {}
private:
std::vector<Key> positions;
};
template <typename NodeID, typename Key> class MapStorage
{
public:
explicit MapStorage(std::size_t) {}
Key &operator[](NodeID node) { return nodes[node]; }
void Clear() { nodes.clear(); }
Key peek_index(const NodeID node) const
{
const auto iter = nodes.find(node);
if (nodes.end() != iter)
{
return iter->second;
}
return std::numeric_limits<Key>::max();
}
private:
std::map<NodeID, Key> nodes;
};
template <typename NodeID, typename Key> class UnorderedMapStorage
{
public:
explicit UnorderedMapStorage(std::size_t) { nodes.rehash(1000); }
Key &operator[](const NodeID node) { return nodes[node]; }
Key peek_index(const NodeID node) const
{
const auto iter = nodes.find(node);
if (std::end(nodes) != iter)
{
return iter->second;
}
return std::numeric_limits<Key>::max();
}
Key const &operator[](const NodeID node) const
{
auto iter = nodes.find(node);
return iter->second;
}
void Clear() { nodes.clear(); }
private:
std::unordered_map<NodeID, Key> nodes;
};
template <typename NodeID,
typename Key,
template <typename N, typename K> class BaseIndexStorage = UnorderedMapStorage,
template <typename N, typename K> class OverlayIndexStorage = ArrayStorage>
class TwoLevelStorage
{
public:
explicit TwoLevelStorage(std::size_t number_of_nodes, std::size_t number_of_overlay_nodes)
: number_of_overlay_nodes(number_of_overlay_nodes), base(number_of_nodes),
overlay(number_of_overlay_nodes)
{
}
Key &operator[](const NodeID node)
{
if (node < number_of_overlay_nodes)
{
return overlay[node];
}
else
{
return base[node];
}
}
Key peek_index(const NodeID node) const
{
if (node < number_of_overlay_nodes)
{
return overlay.peek_index(node);
}
else
{
return base.peek_index(node);
}
}
Key const &operator[](const NodeID node) const
{
if (node < number_of_overlay_nodes)
{
return overlay[node];
}
else
{
return base[node];
}
}
void Clear()
{
base.Clear();
overlay.Clear();
}
private:
const std::size_t number_of_overlay_nodes;
BaseIndexStorage<NodeID, Key> base;
OverlayIndexStorage<NodeID, Key> overlay;
};
template <typename NodeID,
typename Key,
typename Weight,
typename Data,
typename IndexStorage = ArrayStorage<NodeID, NodeID>>
class QueryHeap
{
public:
using WeightType = Weight;
using DataType = Data;
template <typename... StorageArgs> explicit QueryHeap(StorageArgs... args) : node_index(args...)
{
Clear();
}
void Clear()
{
heap.clear();
inserted_nodes.clear();
node_index.Clear();
}
std::size_t Size() const { return heap.size(); }
bool Empty() const { return 0 == Size(); }
void Insert(NodeID node, Weight weight, const Data &data)
{
BOOST_ASSERT(node < std::numeric_limits<NodeID>::max());
const auto index = static_cast<Key>(inserted_nodes.size());
const auto handle = heap.push(std::make_pair(weight, index));
inserted_nodes.emplace_back(HeapNode{handle, node, weight, data});
node_index[node] = index;
}
Data &GetData(NodeID node)
{
const auto index = node_index.peek_index(node);
BOOST_ASSERT((int)index >= 0 && (int)index < (int)inserted_nodes.size());
return inserted_nodes[index].data;
}
Data const &GetData(NodeID node) const
{
const auto index = node_index.peek_index(node);
BOOST_ASSERT((int)index >= 0 && (int)index < (int)inserted_nodes.size());
return inserted_nodes[index].data;
}
const Weight &GetKey(NodeID node) const
{
const auto index = node_index.peek_index(node);
return inserted_nodes[index].weight;
}
bool WasRemoved(const NodeID node) const
{
BOOST_ASSERT(WasInserted(node));
const Key index = node_index.peek_index(node);
// Use end iterator as a reliable "non-existent" handle.
// Default-constructed handles are singular and
// can only be checked-compared to another singular instance.
// Behaviour investigated at https://lists.boost.org/boost-users/2017/08/87787.php,
// eventually confirmation at https://stackoverflow.com/a/45622940/151641.
// Corrected in https://github.com/Project-OSRM/osrm-backend/pull/4396
auto const end_it = const_cast<HeapContainer &>(heap).end(); // non-const iterator
auto const none_handle = heap.s_handle_from_iterator(end_it); // from non-const iterator
return inserted_nodes[index].handle == none_handle;
}
bool WasInserted(const NodeID node) const
{
const auto index = node_index.peek_index(node);
if (index >= static_cast<decltype(index)>(inserted_nodes.size()))
{
return false;
}
return inserted_nodes[index].node == node;
}
NodeID Min() const
{
BOOST_ASSERT(!heap.empty());
return inserted_nodes[heap.top().second].node;
}
Weight MinKey() const
{
BOOST_ASSERT(!heap.empty());
return heap.top().first;
}
NodeID DeleteMin()
{
BOOST_ASSERT(!heap.empty());
const Key removedIndex = heap.top().second;
heap.pop();
inserted_nodes[removedIndex].handle = heap.s_handle_from_iterator(heap.end());
return inserted_nodes[removedIndex].node;
}
void DeleteAll()
{
auto const none_handle = heap.s_handle_from_iterator(heap.end());
std::for_each(inserted_nodes.begin(), inserted_nodes.end(), [&none_handle](auto &node) {
node.handle = none_handle;
});
heap.clear();
}
void DecreaseKey(NodeID node, Weight weight)
{
BOOST_ASSERT(!WasRemoved(node));
const auto index = node_index.peek_index(node);
auto &reference = inserted_nodes[index];
reference.weight = weight;
heap.increase(reference.handle, std::make_pair(weight, index));
}
private:
using HeapData = std::pair<Weight, Key>;
using HeapContainer = boost::heap::d_ary_heap<HeapData,
boost::heap::arity<4>,
boost::heap::mutable_<true>,
boost::heap::compare<std::greater<HeapData>>>;
using HeapHandle = typename HeapContainer::handle_type;
struct HeapNode
{
HeapHandle handle;
NodeID node;
Weight weight;
Data data;
};
std::vector<HeapNode> inserted_nodes;
HeapContainer heap;
IndexStorage node_index;
};
}
}
#endif // OSRM_UTIL_QUERY_HEAP_HPP