Skip to content

Commit 42fafdc

Browse files
Use custom struct instead of std::pair in QueryHeap (#6921)
1 parent c8de759 commit 42fafdc

File tree

2 files changed

+22
-8
lines changed

2 files changed

+22
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
- NodeJS:
2424
- CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452)
2525
- Misc:
26+
- CHANGED: Use custom struct instead of std::pair in QueryHeap. [#6921](https://github.com/Project-OSRM/osrm-backend/pull/6921)
2627
- CHANGED: Use std::string_view::starts_with instead of boost::starts_with. [#6918](https://github.com/Project-OSRM/osrm-backend/pull/6918)
2728
- CHANGED: Get rid of boost::math::constants::* and M_PI in favor of std::numbers. [#6916](https://github.com/Project-OSRM/osrm-backend/pull/6916)
2829
- CHANGED: Make constants in PackedVector constexpr. [#6917](https://github.com/Project-OSRM/osrm-backend/pull/6917)

include/util/query_heap.hpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,20 @@ template <typename NodeID,
193193
class QueryHeap
194194
{
195195
private:
196-
using HeapData = std::pair<Weight, Key>;
196+
struct HeapData
197+
{
198+
Weight weight;
199+
Key index;
200+
201+
bool operator>(const HeapData &other) const
202+
{
203+
if (weight == other.weight)
204+
{
205+
return index > other.index;
206+
}
207+
return weight > other.weight;
208+
}
209+
};
197210
using HeapContainer = boost::heap::d_ary_heap<HeapData,
198211
boost::heap::arity<4>,
199212
boost::heap::mutable_<true>,
@@ -232,7 +245,7 @@ class QueryHeap
232245
{
233246
BOOST_ASSERT(node < std::numeric_limits<NodeID>::max());
234247
const auto index = static_cast<Key>(inserted_nodes.size());
235-
const auto handle = heap.push(std::make_pair(weight, index));
248+
const auto handle = heap.emplace(HeapData{weight, index});
236249
inserted_nodes.emplace_back(HeapNode{handle, node, weight, data});
237250
node_index[node] = index;
238251
}
@@ -315,19 +328,19 @@ class QueryHeap
315328
NodeID Min() const
316329
{
317330
BOOST_ASSERT(!heap.empty());
318-
return inserted_nodes[heap.top().second].node;
331+
return inserted_nodes[heap.top().index].node;
319332
}
320333

321334
Weight MinKey() const
322335
{
323336
BOOST_ASSERT(!heap.empty());
324-
return heap.top().first;
337+
return heap.top().weight;
325338
}
326339

327340
NodeID DeleteMin()
328341
{
329342
BOOST_ASSERT(!heap.empty());
330-
const Key removedIndex = heap.top().second;
343+
const Key removedIndex = heap.top().index;
331344
heap.pop();
332345
inserted_nodes[removedIndex].handle = heap.s_handle_from_iterator(heap.end());
333346
return inserted_nodes[removedIndex].node;
@@ -336,7 +349,7 @@ class QueryHeap
336349
HeapNode &DeleteMinGetHeapNode()
337350
{
338351
BOOST_ASSERT(!heap.empty());
339-
const Key removedIndex = heap.top().second;
352+
const Key removedIndex = heap.top().index;
340353
heap.pop();
341354
inserted_nodes[removedIndex].handle = heap.s_handle_from_iterator(heap.end());
342355
return inserted_nodes[removedIndex];
@@ -357,13 +370,13 @@ class QueryHeap
357370
const auto index = node_index.peek_index(node);
358371
auto &reference = inserted_nodes[index];
359372
reference.weight = weight;
360-
heap.increase(reference.handle, std::make_pair(weight, index));
373+
heap.increase(reference.handle, HeapData{weight, static_cast<Key>(index)});
361374
}
362375

363376
void DecreaseKey(const HeapNode &heapNode)
364377
{
365378
BOOST_ASSERT(!WasRemoved(heapNode.node));
366-
heap.increase(heapNode.handle, std::make_pair(heapNode.weight, (*heapNode.handle).second));
379+
heap.increase(heapNode.handle, HeapData{heapNode.weight, (*heapNode.handle).index});
367380
}
368381

369382
private:

0 commit comments

Comments
 (0)