Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use custom struct instead of std::pair in QueryHeap #6921

Merged
merged 4 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,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: Make constants in PackedVector constexpr. [#6917](https://github.com/Project-OSRM/osrm-backend/pull/6917)
- CHANGED: Use std::variant instead of mapbox::util::variant. [#6903](https://github.com/Project-OSRM/osrm-backend/pull/6903)
- CHANGED: Bump rapidjson to version f9d53419e912910fd8fa57d5705fa41425428c35 [#6906](https://github.com/Project-OSRM/osrm-backend/pull/6906)
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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems it guarantees that we will get keys with the same weight in insertion order and I added it only to preserve guarantees we had before with std::pair, but is it really needed? 🤔

{
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
Loading