Use custom struct instead of std::pair in QueryHeap
This commit is contained in:
parent
1ff096ac5c
commit
4ae5213d9d
@ -193,7 +193,20 @@ template <typename NodeID,
|
|||||||
class QueryHeap
|
class QueryHeap
|
||||||
{
|
{
|
||||||
private:
|
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,
|
using HeapContainer = boost::heap::d_ary_heap<HeapData,
|
||||||
boost::heap::arity<4>,
|
boost::heap::arity<4>,
|
||||||
boost::heap::mutable_<true>,
|
boost::heap::mutable_<true>,
|
||||||
@ -232,7 +245,7 @@ class QueryHeap
|
|||||||
{
|
{
|
||||||
BOOST_ASSERT(node < std::numeric_limits<NodeID>::max());
|
BOOST_ASSERT(node < std::numeric_limits<NodeID>::max());
|
||||||
const auto index = static_cast<Key>(inserted_nodes.size());
|
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});
|
inserted_nodes.emplace_back(HeapNode{handle, node, weight, data});
|
||||||
node_index[node] = index;
|
node_index[node] = index;
|
||||||
}
|
}
|
||||||
@ -315,19 +328,19 @@ class QueryHeap
|
|||||||
NodeID Min() const
|
NodeID Min() const
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(!heap.empty());
|
BOOST_ASSERT(!heap.empty());
|
||||||
return inserted_nodes[heap.top().second].node;
|
return inserted_nodes[heap.top().index].node;
|
||||||
}
|
}
|
||||||
|
|
||||||
Weight MinKey() const
|
Weight MinKey() const
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(!heap.empty());
|
BOOST_ASSERT(!heap.empty());
|
||||||
return heap.top().first;
|
return heap.top().weight;
|
||||||
}
|
}
|
||||||
|
|
||||||
NodeID DeleteMin()
|
NodeID DeleteMin()
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(!heap.empty());
|
BOOST_ASSERT(!heap.empty());
|
||||||
const Key removedIndex = heap.top().second;
|
const Key removedIndex = heap.top().index;
|
||||||
heap.pop();
|
heap.pop();
|
||||||
inserted_nodes[removedIndex].handle = heap.s_handle_from_iterator(heap.end());
|
inserted_nodes[removedIndex].handle = heap.s_handle_from_iterator(heap.end());
|
||||||
return inserted_nodes[removedIndex].node;
|
return inserted_nodes[removedIndex].node;
|
||||||
@ -336,7 +349,7 @@ class QueryHeap
|
|||||||
HeapNode &DeleteMinGetHeapNode()
|
HeapNode &DeleteMinGetHeapNode()
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(!heap.empty());
|
BOOST_ASSERT(!heap.empty());
|
||||||
const Key removedIndex = heap.top().second;
|
const Key removedIndex = heap.top().index;
|
||||||
heap.pop();
|
heap.pop();
|
||||||
inserted_nodes[removedIndex].handle = heap.s_handle_from_iterator(heap.end());
|
inserted_nodes[removedIndex].handle = heap.s_handle_from_iterator(heap.end());
|
||||||
return inserted_nodes[removedIndex];
|
return inserted_nodes[removedIndex];
|
||||||
@ -357,13 +370,13 @@ class QueryHeap
|
|||||||
const auto index = node_index.peek_index(node);
|
const auto index = node_index.peek_index(node);
|
||||||
auto &reference = inserted_nodes[index];
|
auto &reference = inserted_nodes[index];
|
||||||
reference.weight = weight;
|
reference.weight = weight;
|
||||||
heap.increase(reference.handle, std::make_pair(weight, index));
|
heap.increase(reference.handle, HeapData{weight, index});
|
||||||
}
|
}
|
||||||
|
|
||||||
void DecreaseKey(const HeapNode &heapNode)
|
void DecreaseKey(const HeapNode &heapNode)
|
||||||
{
|
{
|
||||||
BOOST_ASSERT(!WasRemoved(heapNode.node));
|
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:
|
private:
|
||||||
|
Loading…
Reference in New Issue
Block a user