From 7e2fd63f0a33a0373dbeea57290c156a4b58b820 Mon Sep 17 00:00:00 2001 From: Siarhei Fedartsou Date: Tue, 21 May 2024 20:10:27 +0200 Subject: [PATCH] use constexpr if --- .../routing_algorithms/routing_base_mld.hpp | 82 ++++++++----------- 1 file changed, 36 insertions(+), 46 deletions(-) diff --git a/include/engine/routing_algorithms/routing_base_mld.hpp b/include/engine/routing_algorithms/routing_base_mld.hpp index ad0924e70..b67e779b2 100644 --- a/include/engine/routing_algorithms/routing_base_mld.hpp +++ b/include/engine/routing_algorithms/routing_base_mld.hpp @@ -532,22 +532,19 @@ using UnpackedNodes = std::vector; using UnpackedEdges = std::vector; using UnpackedPath = std::tuple; -template -UnpackedPath search(SearchEngineData &engine_working_data, - const DataFacade &facade, - typename SearchEngineData::QueryHeap &forward_heap, - typename SearchEngineData::QueryHeap &reverse_heap, - const std::vector &force_step_nodes, - EdgeWeight weight_upper_bound, - const Args &...args) +template +std::optional> runSearch(const DataFacade &facade, + Heap &forward_heap, + Heap &reverse_heap, + const std::vector &force_step_nodes, + EdgeWeight weight_upper_bound, + const Args &...args) { if (forward_heap.Empty() || reverse_heap.Empty()) { - return std::make_tuple(INVALID_EDGE_WEIGHT, std::vector(), std::vector()); + return {}; } - const auto &partition = facade.GetMultiLevelPartition(); - BOOST_ASSERT(!forward_heap.Empty() && forward_heap.MinKey() < INVALID_EDGE_WEIGHT); BOOST_ASSERT(!reverse_heap.Empty() && reverse_heap.MinKey() < INVALID_EDGE_WEIGHT); @@ -577,10 +574,33 @@ UnpackedPath search(SearchEngineData &engine_working_data, // No path found for both target nodes? if (weight >= weight_upper_bound || SPECIAL_NODEID == middle) + { + return {}; + } + + return {{middle, weight}}; +} + +template +UnpackedPath search(SearchEngineData &engine_working_data, + const DataFacade &facade, + typename SearchEngineData::QueryHeap &forward_heap, + typename SearchEngineData::QueryHeap &reverse_heap, + const std::vector &force_step_nodes, + EdgeWeight weight_upper_bound, + const Args &...args) +{ + auto searchResult = runSearch( + facade, forward_heap, reverse_heap, force_step_nodes, weight_upper_bound, args...); + if (!searchResult) { return std::make_tuple(INVALID_EDGE_WEIGHT, std::vector(), std::vector()); } + auto [middle, weight] = *searchResult; + + const auto &partition = facade.GetMultiLevelPartition(); + // std::cerr << "Distance = " << forward_heap.GetData(middle).distance << " " << // reverse_heap.GetData(middle).distance << std::endl; @@ -652,45 +672,15 @@ searchDistance(SearchEngineData &, EdgeWeight weight_upper_bound, const Args &...args) { - if (forward_heap.Empty() || reverse_heap.Empty()) + + auto searchResult = runSearch( + facade, forward_heap, reverse_heap, force_step_nodes, weight_upper_bound, args...); + if (!searchResult) { return INVALID_EDGE_DISTANCE; } - // const auto &partition = facade.GetMultiLevelPartition(); - - BOOST_ASSERT(!forward_heap.Empty() && forward_heap.MinKey() < INVALID_EDGE_WEIGHT); - BOOST_ASSERT(!reverse_heap.Empty() && reverse_heap.MinKey() < INVALID_EDGE_WEIGHT); - - // run two-Target Dijkstra routing step. - NodeID middle = SPECIAL_NODEID; - EdgeWeight weight = weight_upper_bound; - EdgeWeight forward_heap_min = forward_heap.MinKey(); - EdgeWeight reverse_heap_min = reverse_heap.MinKey(); - while (forward_heap.Size() + reverse_heap.Size() > 0 && - forward_heap_min + reverse_heap_min < weight) - { - if (!forward_heap.Empty()) - { - routingStep( - facade, forward_heap, reverse_heap, middle, weight, force_step_nodes, args...); - if (!forward_heap.Empty()) - forward_heap_min = forward_heap.MinKey(); - } - if (!reverse_heap.Empty()) - { - routingStep( - facade, reverse_heap, forward_heap, middle, weight, force_step_nodes, args...); - if (!reverse_heap.Empty()) - reverse_heap_min = reverse_heap.MinKey(); - } - }; - - // No path found for both target nodes? - if (weight >= weight_upper_bound || SPECIAL_NODEID == middle) - { - return INVALID_EDGE_DISTANCE; - } + auto [middle, _] = *searchResult; auto distance = forward_heap.GetData(middle).distance + reverse_heap.GetData(middle).distance;