diff --git a/include/engine/phantom_node.hpp b/include/engine/phantom_node.hpp index 515f9361549..49ad24f84c9 100644 --- a/include/engine/phantom_node.hpp +++ b/include/engine/phantom_node.hpp @@ -55,9 +55,9 @@ struct PhantomNode forward_duration_offset(0), reverse_duration_offset(0), packed_geometry_id(SPECIAL_GEOMETRYID), component{INVALID_COMPONENTID, false}, fwd_segment_position(0), forward_travel_mode(TRAVEL_MODE_INACCESSIBLE), - backward_travel_mode(TRAVEL_MODE_INACCESSIBLE), is_forward_valid_source(false), - is_forward_valid_target(false), is_reverse_valid_source(false), - is_reverse_valid_target(false) + backward_travel_mode(TRAVEL_MODE_INACCESSIBLE), is_valid_forward_source(false), + is_valid_forward_target(false), is_valid_reverse_source(false), + is_valid_reverse_target(false) { } @@ -105,21 +105,21 @@ struct PhantomNode bool IsValid() const { return location.IsValid() && (name_id != INVALID_NAMEID); } - bool IsForwardValidSource() const + bool IsValidForwardSource() const { - return forward_segment_id.enabled && is_forward_valid_source; + return forward_segment_id.enabled && is_valid_forward_source; } - bool IsForwardValidTarget() const + bool IsValidForwardTarget() const { - return forward_segment_id.enabled && is_forward_valid_target; + return forward_segment_id.enabled && is_valid_forward_target; } - bool IsReverseValidSource() const + bool IsValidReverseSource() const { - return reverse_segment_id.enabled && is_reverse_valid_source; + return reverse_segment_id.enabled && is_valid_reverse_source; } - bool IsReverseValidTarget() const + bool IsValidReverseTarget() const { - return reverse_segment_id.enabled && is_reverse_valid_target; + return reverse_segment_id.enabled && is_valid_reverse_target; } bool operator==(const PhantomNode &other) const { return location == other.location; } @@ -134,10 +134,10 @@ struct PhantomNode EdgeWeight reverse_duration, EdgeWeight forward_duration_offset, EdgeWeight reverse_duration_offset, - bool is_forward_valid_source, - bool is_forward_valid_target, - bool is_reverse_valid_source, - bool is_reverse_valid_target, + bool is_valid_forward_source, + bool is_valid_forward_target, + bool is_valid_reverse_source, + bool is_valid_reverse_target, const util::Coordinate location, const util::Coordinate input_location) : forward_segment_id{other.forward_segment_id}, @@ -152,10 +152,10 @@ struct PhantomNode input_location{input_location}, fwd_segment_position{other.fwd_segment_position}, forward_travel_mode{other.forward_travel_mode}, backward_travel_mode{other.backward_travel_mode}, - is_forward_valid_source{is_forward_valid_source}, - is_forward_valid_target{is_forward_valid_target}, - is_reverse_valid_source{is_reverse_valid_source}, - is_reverse_valid_target{is_reverse_valid_target} + is_valid_forward_source{is_valid_forward_source}, + is_valid_forward_target{is_valid_forward_target}, + is_valid_reverse_source{is_valid_reverse_source}, + is_valid_reverse_target{is_valid_reverse_target} { } @@ -187,10 +187,10 @@ struct PhantomNode extractor::TravelMode backward_travel_mode : 4; // is phantom node valid to be used as source or target private: - bool is_forward_valid_source : 1; - bool is_forward_valid_target : 1; - bool is_reverse_valid_source : 1; - bool is_reverse_valid_target : 1; + bool is_valid_forward_source : 1; + bool is_valid_forward_target : 1; + bool is_valid_reverse_source : 1; + bool is_valid_reverse_target : 1; }; static_assert(sizeof(PhantomNode) == 72, "PhantomNode has more padding then expected"); diff --git a/include/engine/routing_algorithms/routing_base.hpp b/include/engine/routing_algorithms/routing_base.hpp index c9e4e396a3f..e157652f96c 100644 --- a/include/engine/routing_algorithms/routing_base.hpp +++ b/include/engine/routing_algorithms/routing_base.hpp @@ -41,43 +41,24 @@ bool needsLoopForward(const PhantomNode &source_phantom, const PhantomNode &targ bool needsLoopBackwards(const PhantomNode &source_phantom, const PhantomNode &target_phantom); -template -void insertNodesInHeap(SearchEngineData::ManyToManyQueryHeap &heap, - const PhantomNode &phantom_node) -{ - BOOST_ASSERT(phantom_node.IsValid()); +void insertSourceInHeap(SearchEngineData::ManyToManyQueryHeap &heap, + const PhantomNode &phantom_node); - const auto weight_sign = DIRECTION == FORWARD_DIRECTION ? -1 : 1; - if ((DIRECTION == FORWARD_DIRECTION && phantom_node.IsForwardValidSource()) || - (DIRECTION == REVERSE_DIRECTION && phantom_node.IsForwardValidTarget())) - { - heap.Insert( - phantom_node.forward_segment_id.id, - weight_sign * phantom_node.GetForwardWeightPlusOffset(), - {phantom_node.forward_segment_id.id, weight_sign * phantom_node.GetForwardDuration()}); - } - if ((DIRECTION == FORWARD_DIRECTION && phantom_node.IsReverseValidSource()) || - (DIRECTION == REVERSE_DIRECTION && phantom_node.IsReverseValidTarget())) - { - heap.Insert( - phantom_node.reverse_segment_id.id, - weight_sign * phantom_node.GetReverseWeightPlusOffset(), - {phantom_node.reverse_segment_id.id, weight_sign * phantom_node.GetReverseDuration()}); - } -} +void insertTargetInHeap(SearchEngineData::ManyToManyQueryHeap &heap, + const PhantomNode &phantom_node); template void insertNodesInHeaps(Heap &forward_heap, Heap &reverse_heap, const PhantomNodes &nodes) { const auto &source = nodes.source_phantom; - if (source.IsForwardValidSource()) + if (source.IsValidForwardSource()) { forward_heap.Insert(source.forward_segment_id.id, -source.GetForwardWeightPlusOffset(), source.forward_segment_id.id); } - if (source.IsReverseValidSource()) + if (source.IsValidReverseSource()) { forward_heap.Insert(source.reverse_segment_id.id, -source.GetReverseWeightPlusOffset(), @@ -85,14 +66,14 @@ void insertNodesInHeaps(Heap &forward_heap, Heap &reverse_heap, const PhantomNod } const auto &target = nodes.target_phantom; - if (target.IsForwardValidTarget()) + if (target.IsValidForwardTarget()) { reverse_heap.Insert(target.forward_segment_id.id, target.GetForwardWeightPlusOffset(), target.forward_segment_id.id); } - if (target.IsReverseValidTarget()) + if (target.IsValidReverseTarget()) { reverse_heap.Insert(target.reverse_segment_id.id, target.GetReverseWeightPlusOffset(), diff --git a/src/engine/routing_algorithms/many_to_many.cpp b/src/engine/routing_algorithms/many_to_many.cpp index d9f21994b2f..674988cf1ea 100644 --- a/src/engine/routing_algorithms/many_to_many.cpp +++ b/src/engine/routing_algorithms/many_to_many.cpp @@ -176,7 +176,7 @@ manyToManySearch(SearchEngineData &engine_working_data, const auto search_target_phantom = [&](const PhantomNode &phantom) { // clear heap and insert target nodes query_heap.Clear(); - insertNodesInHeap(query_heap, phantom); + insertTargetInHeap(query_heap, phantom); // explore search space while (!query_heap.Empty()) @@ -191,7 +191,7 @@ manyToManySearch(SearchEngineData &engine_working_data, const auto search_source_phantom = [&](const PhantomNode &phantom) { // clear heap and insert source nodes query_heap.Clear(); - insertNodesInHeap(query_heap, phantom); + insertSourceInHeap(query_heap, phantom); // explore search space while (!query_heap.Empty()) diff --git a/src/engine/routing_algorithms/routing_base.cpp b/src/engine/routing_algorithms/routing_base.cpp index 02cb69ca6ed..8f834847fa2 100644 --- a/src/engine/routing_algorithms/routing_base.cpp +++ b/src/engine/routing_algorithms/routing_base.cpp @@ -9,7 +9,7 @@ namespace routing_algorithms bool needsLoopForward(const PhantomNode &source_phantom, const PhantomNode &target_phantom) { - return source_phantom.IsForwardValidSource() && target_phantom.IsForwardValidTarget() && + return source_phantom.IsValidForwardSource() && target_phantom.IsValidForwardTarget() && source_phantom.forward_segment_id.id == target_phantom.forward_segment_id.id && source_phantom.GetForwardWeightPlusOffset() > target_phantom.GetForwardWeightPlusOffset(); @@ -17,12 +17,46 @@ bool needsLoopForward(const PhantomNode &source_phantom, const PhantomNode &targ bool needsLoopBackwards(const PhantomNode &source_phantom, const PhantomNode &target_phantom) { - return source_phantom.IsReverseValidSource() && target_phantom.IsReverseValidTarget() && + return source_phantom.IsValidReverseSource() && target_phantom.IsValidReverseTarget() && source_phantom.reverse_segment_id.id == target_phantom.reverse_segment_id.id && source_phantom.GetReverseWeightPlusOffset() > target_phantom.GetReverseWeightPlusOffset(); } +void insertSourceInHeap(SearchEngineData::ManyToManyQueryHeap &heap, + const PhantomNode &phantom_node) +{ + if (phantom_node.IsValidForwardSource()) + { + heap.Insert(phantom_node.forward_segment_id.id, + -phantom_node.GetForwardWeightPlusOffset(), + {phantom_node.forward_segment_id.id, -phantom_node.GetForwardDuration()}); + } + if (phantom_node.IsValidReverseSource()) + { + heap.Insert(phantom_node.reverse_segment_id.id, + -phantom_node.GetReverseWeightPlusOffset(), + {phantom_node.reverse_segment_id.id, -phantom_node.GetReverseDuration()}); + } +} + +void insertTargetInHeap(SearchEngineData::ManyToManyQueryHeap &heap, + const PhantomNode &phantom_node) +{ + if (phantom_node.IsValidForwardTarget()) + { + heap.Insert(phantom_node.forward_segment_id.id, + phantom_node.GetForwardWeightPlusOffset(), + {phantom_node.forward_segment_id.id, phantom_node.GetForwardDuration()}); + } + if (phantom_node.IsValidReverseTarget()) + { + heap.Insert(phantom_node.reverse_segment_id.id, + phantom_node.GetReverseWeightPlusOffset(), + {phantom_node.reverse_segment_id.id, phantom_node.GetReverseDuration()}); + } +} + } // namespace routing_algorithms } // namespace engine } // namespace osrm diff --git a/src/engine/routing_algorithms/shortest_path.cpp b/src/engine/routing_algorithms/shortest_path.cpp index d07d50e0db0..72b122c1b6b 100644 --- a/src/engine/routing_algorithms/shortest_path.cpp +++ b/src/engine/routing_algorithms/shortest_path.cpp @@ -233,9 +233,9 @@ shortestPathSearch(SearchEngineData &engine_working_data, int total_weight_to_forward = 0; int total_weight_to_reverse = 0; bool search_from_forward_node = - phantom_nodes_vector.front().source_phantom.IsForwardValidSource(); + phantom_nodes_vector.front().source_phantom.IsValidForwardSource(); bool search_from_reverse_node = - phantom_nodes_vector.front().source_phantom.IsReverseValidSource(); + phantom_nodes_vector.front().source_phantom.IsValidReverseSource(); std::vector prev_packed_leg_to_forward; std::vector prev_packed_leg_to_reverse; @@ -259,11 +259,11 @@ shortestPathSearch(SearchEngineData &engine_working_data, const auto &source_phantom = phantom_node_pair.source_phantom; const auto &target_phantom = phantom_node_pair.target_phantom; - bool search_to_forward_node = target_phantom.IsForwardValidTarget(); - bool search_to_reverse_node = target_phantom.IsReverseValidTarget(); + bool search_to_forward_node = target_phantom.IsValidForwardTarget(); + bool search_to_reverse_node = target_phantom.IsValidReverseTarget(); - BOOST_ASSERT(!search_from_forward_node || source_phantom.IsForwardValidSource()); - BOOST_ASSERT(!search_from_reverse_node || source_phantom.IsReverseValidSource()); + BOOST_ASSERT(!search_from_forward_node || source_phantom.IsValidForwardSource()); + BOOST_ASSERT(!search_from_reverse_node || source_phantom.IsValidReverseSource()); if (search_to_reverse_node || search_to_forward_node) { @@ -285,9 +285,9 @@ shortestPathSearch(SearchEngineData &engine_working_data, packed_leg_to_forward); // if only the reverse node is valid (e.g. when using the match plugin) we // actually need to move - if (!target_phantom.IsForwardValidTarget()) + if (!target_phantom.IsValidForwardTarget()) { - BOOST_ASSERT(target_phantom.IsReverseValidTarget()); + BOOST_ASSERT(target_phantom.IsValidReverseTarget()); new_total_weight_to_reverse = new_total_weight_to_forward; packed_leg_to_reverse = std::move(packed_leg_to_forward); new_total_weight_to_forward = INVALID_EDGE_WEIGHT; @@ -297,7 +297,7 @@ shortestPathSearch(SearchEngineData &engine_working_data, // Below we have to check if new_total_weight_to_forward is invalid. // This prevents use-after-move on packed_leg_to_forward. } - else if (target_phantom.IsReverseValidTarget()) + else if (target_phantom.IsValidReverseTarget()) { new_total_weight_to_reverse = new_total_weight_to_forward; packed_leg_to_reverse = packed_leg_to_forward; @@ -385,7 +385,7 @@ shortestPathSearch(SearchEngineData &engine_working_data, if (new_total_weight_to_forward != INVALID_EDGE_WEIGHT) { - BOOST_ASSERT(target_phantom.IsForwardValidTarget()); + BOOST_ASSERT(target_phantom.IsValidForwardTarget()); packed_leg_to_forward_begin.push_back(total_packed_path_to_forward.size()); total_packed_path_to_forward.insert(total_packed_path_to_forward.end(), @@ -402,7 +402,7 @@ shortestPathSearch(SearchEngineData &engine_working_data, if (new_total_weight_to_reverse != INVALID_EDGE_WEIGHT) { - BOOST_ASSERT(target_phantom.IsReverseValidTarget()); + BOOST_ASSERT(target_phantom.IsValidReverseTarget()); packed_leg_to_reverse_begin.push_back(total_packed_path_to_reverse.size()); total_packed_path_to_reverse.insert(total_packed_path_to_reverse.end(),