From f3f153cb382bf045d5792590cd2a3ce44d13b937 Mon Sep 17 00:00:00 2001 From: Daniel Patterson Date: Tue, 1 Dec 2015 12:05:36 -0800 Subject: [PATCH] Reduce source/targets to a single phantom node based on the small/big components that are present. --- plugins/distance_table.hpp | 4 ++++ plugins/plugin_base.hpp | 49 ++++++++++++++++++++++++++++++++++++++ plugins/viaroute.hpp | 42 +------------------------------- 3 files changed, 54 insertions(+), 41 deletions(-) diff --git a/plugins/distance_table.hpp b/plugins/distance_table.hpp index 181493e1f..18dff21dc 100644 --- a/plugins/distance_table.hpp +++ b/plugins/distance_table.hpp @@ -175,11 +175,15 @@ template class DistanceTablePlugin final : public BasePlugin phantom_node_target_out_iter++; } } + BOOST_ASSERT((phantom_node_source_out_iter - phantom_node_source_vector.begin()) == number_of_sources); BOOST_ASSERT((phantom_node_target_out_iter - phantom_node_target_vector.begin()) == number_of_destination); + snapPhantomNodes(phantom_node_source_vector); + snapPhantomNodes(phantom_node_target_vector); + std::shared_ptr> result_table = search_engine_ptr->distance_table( phantom_node_source_vector, phantom_node_target_vector); diff --git a/plugins/plugin_base.hpp b/plugins/plugin_base.hpp index 5ae53632c..f9000b1c5 100644 --- a/plugins/plugin_base.hpp +++ b/plugins/plugin_base.hpp @@ -28,6 +28,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef BASE_PLUGIN_HPP #define BASE_PLUGIN_HPP +#include "../data_structures/phantom_node.hpp" + #include #include #include @@ -57,6 +59,53 @@ class BasePlugin } return true; } + + // Decides whether to use the phantom node from a big or small component if both are found. + // Returns true if all phantom nodes are in the same component after snapping. + bool snapPhantomNodes(std::vector> &phantom_node_pair_list) const + { + const auto check_component_id_is_tiny = [](const std::pair &phantom_pair) + { + return phantom_pair.first.component.is_tiny; + }; + + const bool every_phantom_is_in_tiny_cc = + std::all_of(std::begin(phantom_node_pair_list), std::end(phantom_node_pair_list), + check_component_id_is_tiny); + + // are all phantoms from a tiny cc? + const auto check_all_in_same_component = [](const std::vector> &nodes) + { + const auto component_id = nodes.front().first.component.id; + + return std::all_of(std::begin(nodes), std::end(nodes), + [component_id](const PhantomNodePair &phantom_pair) + { + return component_id == phantom_pair.first.component.id; + }); + }; + + auto swap_phantom_from_big_cc_into_front = [](std::pair &phantom_pair) + { + if (phantom_pair.first.component.is_tiny && phantom_pair.second.is_valid() && !phantom_pair.second.component.is_tiny) + { + using namespace std; + swap(phantom_pair.first, phantom_pair.second); + } + }; + + auto all_in_same_component = check_all_in_same_component(phantom_node_pair_list); + + // this case is true if we take phantoms from the big CC + if (every_phantom_is_in_tiny_cc && !all_in_same_component) + { + std::for_each(std::begin(phantom_node_pair_list), std::end(phantom_node_pair_list), + swap_phantom_from_big_cc_into_front); + + // update check with new component ids + all_in_same_component = check_all_in_same_component(phantom_node_pair_list); + } + } }; #endif /* BASE_PLUGIN_HPP */ diff --git a/plugins/viaroute.hpp b/plugins/viaroute.hpp index 430424140..4ef80d916 100644 --- a/plugins/viaroute.hpp +++ b/plugins/viaroute.hpp @@ -115,47 +115,7 @@ template class ViaRoutePlugin final : public BasePlugin BOOST_ASSERT(phantom_node_pair_list[i].second.is_valid(facade->GetNumberOfNodes())); } - const auto check_component_id_is_tiny = [](const PhantomNodePair &phantom_pair) - { - return phantom_pair.first.component.is_tiny; - }; - - const bool every_phantom_is_in_tiny_cc = - std::all_of(std::begin(phantom_node_pair_list), std::end(phantom_node_pair_list), - check_component_id_is_tiny); - - // are all phantoms from a tiny cc? - const auto check_all_in_same_component = [](const std::vector &nodes) - { - const auto component_id = nodes.front().first.component.id; - - return std::all_of(std::begin(nodes), std::end(nodes), - [component_id](const PhantomNodePair &phantom_pair) - { - return component_id == phantom_pair.first.component.id; - }); - }; - - auto swap_phantom_from_big_cc_into_front = [](PhantomNodePair &phantom_pair) - { - if (phantom_pair.first.component.is_tiny && phantom_pair.second.is_valid() && !phantom_pair.second.component.is_tiny) - { - using namespace std; - swap(phantom_pair.first, phantom_pair.second); - } - }; - - auto all_in_same_component = check_all_in_same_component(phantom_node_pair_list); - - // this case is true if we take phantoms from the big CC - if (every_phantom_is_in_tiny_cc && !all_in_same_component) - { - std::for_each(std::begin(phantom_node_pair_list), std::end(phantom_node_pair_list), - swap_phantom_from_big_cc_into_front); - - // update check with new component ids - all_in_same_component = check_all_in_same_component(phantom_node_pair_list); - } + auto all_in_same_component = snapPhantomNodes(phantom_node_pair_list); InternalRouteResult raw_route; auto build_phantom_pairs =