Skip to content

Commit

Permalink
Use custom struct instead of std::pair in QueryHeap (#6921)
Browse files Browse the repository at this point in the history
  • Loading branch information
SiarheiFedartsou authored May 31, 2024
1 parent c8de759 commit 42fafdc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
- NodeJS:
- CHANGED: Use node-api instead of NAN. [#6452](https://github.com/Project-OSRM/osrm-backend/pull/6452)
- Misc:
- CHANGED: Use custom struct instead of std::pair in QueryHeap. [#6921](https://github.com/Project-OSRM/osrm-backend/pull/6921)
- CHANGED: Use std::string_view::starts_with instead of boost::starts_with. [#6918](https://github.com/Project-OSRM/osrm-backend/pull/6918)
- 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)
- CHANGED: Make constants in PackedVector constexpr. [#6917](https://github.com/Project-OSRM/osrm-backend/pull/6917)
Expand Down
29 changes: 21 additions & 8 deletions include/util/query_heap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,20 @@ template <typename NodeID,
class QueryHeap
{
private:
using HeapData = std::pair<Weight, Key>;
struct HeapData
{
Weight weight;
Key index;

bool operator>(const HeapData &other) const
{
if (weight == other.weight)
{
return index > other.index;
}
return weight > other.weight;
}
};
using HeapContainer = boost::heap::d_ary_heap<HeapData,
boost::heap::arity<4>,
boost::heap::mutable_<true>,
Expand Down Expand Up @@ -232,7 +245,7 @@ class QueryHeap
{
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));
const auto handle = heap.emplace(HeapData{weight, index});
inserted_nodes.emplace_back(HeapNode{handle, node, weight, data});
node_index[node] = index;
}
Expand Down Expand Up @@ -315,19 +328,19 @@ class QueryHeap
NodeID Min() const
{
BOOST_ASSERT(!heap.empty());
return inserted_nodes[heap.top().second].node;
return inserted_nodes[heap.top().index].node;
}

Weight MinKey() const
{
BOOST_ASSERT(!heap.empty());
return heap.top().first;
return heap.top().weight;
}

NodeID DeleteMin()
{
BOOST_ASSERT(!heap.empty());
const Key removedIndex = heap.top().second;
const Key removedIndex = heap.top().index;
heap.pop();
inserted_nodes[removedIndex].handle = heap.s_handle_from_iterator(heap.end());
return inserted_nodes[removedIndex].node;
Expand All @@ -336,7 +349,7 @@ class QueryHeap
HeapNode &DeleteMinGetHeapNode()
{
BOOST_ASSERT(!heap.empty());
const Key removedIndex = heap.top().second;
const Key removedIndex = heap.top().index;
heap.pop();
inserted_nodes[removedIndex].handle = heap.s_handle_from_iterator(heap.end());
return inserted_nodes[removedIndex];
Expand All @@ -357,13 +370,13 @@ class QueryHeap
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));
heap.increase(reference.handle, HeapData{weight, static_cast<Key>(index)});
}

void DecreaseKey(const HeapNode &heapNode)
{
BOOST_ASSERT(!WasRemoved(heapNode.node));
heap.increase(heapNode.handle, std::make_pair(heapNode.weight, (*heapNode.handle).second));
heap.increase(heapNode.handle, HeapData{heapNode.weight, (*heapNode.handle).index});
}

private:
Expand Down

0 comments on commit 42fafdc

Please sign in to comment.