Make RouteNameExtraction a free standing function

This commit is contained in:
Daniel J. Hofmann 2016-01-08 15:35:17 +01:00 committed by Patrick Niklaus
parent 0627c3443d
commit 03f64a6c20
2 changed files with 116 additions and 114 deletions

View File

@ -48,7 +48,6 @@ template <typename DataFacadeT> class ApiResponseGenerator
using DataFacade = DataFacadeT; using DataFacade = DataFacadeT;
using Segments = guidance::SegmentList<DataFacade>; using Segments = guidance::SegmentList<DataFacade>;
using Segment = detail::Segment; using Segment = detail::Segment;
using RouteNameExtractor = ExtractRouteNames<DataFacade, Segment>;
ApiResponseGenerator(DataFacade *facade); ApiResponseGenerator(DataFacade *facade);
@ -120,7 +119,6 @@ void ApiResponseGenerator<DataFacadeT>::DescribeRoute(const RouteParameters &con
} }
RouteNames route_names; RouteNames route_names;
RouteNameExtractor generate_route_names;
if (raw_route.has_alternative()) if (raw_route.has_alternative())
{ {
@ -156,7 +154,7 @@ void ApiResponseGenerator<DataFacadeT>::DescribeRoute(const RouteParameters &con
// generate names for both the main path and the alternative route // generate names for both the main path and the alternative route
auto path_segments = BuildRouteSegments(segment_list); auto path_segments = BuildRouteSegments(segment_list);
auto alternate_segments = BuildRouteSegments(alternate_segment_list); auto alternate_segments = BuildRouteSegments(alternate_segment_list);
route_names = generate_route_names(path_segments, alternate_segments, facade); route_names = extractRouteNames(path_segments, alternate_segments, facade);
util::json::Array json_alternate_names_array; util::json::Array json_alternate_names_array;
util::json::Array json_alternate_names; util::json::Array json_alternate_names;
@ -172,7 +170,7 @@ void ApiResponseGenerator<DataFacadeT>::DescribeRoute(const RouteParameters &con
// generate names for the main route on its own // generate names for the main route on its own
auto path_segments = BuildRouteSegments(segment_list); auto path_segments = BuildRouteSegments(segment_list);
std::vector<detail::Segment> alternate_segments; std::vector<detail::Segment> alternate_segments;
route_names = generate_route_names(path_segments, alternate_segments, facade); route_names = extractRouteNames(path_segments, alternate_segments, facade);
} }
util::json::Array json_route_names; util::json::Array json_route_names;

View File

@ -5,6 +5,7 @@
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <utility>
#include <vector> #include <vector>
namespace osrm namespace osrm
@ -20,12 +21,12 @@ struct RouteNames
std::string alternative_path_name_2; std::string alternative_path_name_2;
}; };
// construct routes names namespace detail
template <class DataFacadeT, class SegmentT> struct ExtractRouteNames
{ {
private:
SegmentT PickNextLongestSegment(const std::vector<SegmentT> &segment_list, template <class SegmentT>
const unsigned blocked_name_id) const SegmentT pickNextLongestSegment(const std::vector<SegmentT> &segment_list,
const unsigned blocked_name_id)
{ {
SegmentT result_segment; SegmentT result_segment;
result_segment.name_id = blocked_name_id; // make sure we get a valid name result_segment.name_id = blocked_name_id; // make sure we get a valid name
@ -42,33 +43,36 @@ template <class DataFacadeT, class SegmentT> struct ExtractRouteNames
return result_segment; return result_segment;
} }
public: } // ns detail
RouteNames operator()(std::vector<SegmentT> &shortest_path_segments,
template <class DataFacadeT, class SegmentT>
RouteNames extractRouteNames(std::vector<SegmentT> &shortest_path_segments,
std::vector<SegmentT> &alternative_path_segments, std::vector<SegmentT> &alternative_path_segments,
const DataFacadeT *facade) const const DataFacadeT *facade)
{ {
RouteNames route_names; RouteNames route_names;
SegmentT shortest_segment_1, shortest_segment_2;
SegmentT alternative_segment_1, alternative_segment_2;
auto length_comperator = [](const SegmentT &a, const SegmentT &b)
{
return a.length > b.length;
};
auto name_id_comperator = [](const SegmentT &a, const SegmentT &b)
{
return a.name_id < b.name_id;
};
if (shortest_path_segments.empty()) if (shortest_path_segments.empty())
{ {
return route_names; return route_names;
} }
SegmentT shortest_segment_1, shortest_segment_2;
SegmentT alternative_segment_1, alternative_segment_2;
const auto length_comperator = [](const SegmentT &a, const SegmentT &b)
{
return a.length > b.length;
};
const auto name_id_comperator = [](const SegmentT &a, const SegmentT &b)
{
return a.name_id < b.name_id;
};
// pick the longest segment for the shortest path. // pick the longest segment for the shortest path.
std::sort(shortest_path_segments.begin(), shortest_path_segments.end(), length_comperator); std::sort(shortest_path_segments.begin(), shortest_path_segments.end(), length_comperator);
shortest_segment_1 = shortest_path_segments[0]; shortest_segment_1 = shortest_path_segments[0];
if (!alternative_path_segments.empty()) if (!alternative_path_segments.empty())
{ {
std::sort(alternative_path_segments.begin(), alternative_path_segments.end(), std::sort(alternative_path_segments.begin(), alternative_path_segments.end(),
@ -91,15 +95,15 @@ template <class DataFacadeT, class SegmentT> struct ExtractRouteNames
std::sort(shortest_path_set_difference.begin(), shortest_path_set_difference.end(), std::sort(shortest_path_set_difference.begin(), shortest_path_set_difference.end(),
length_comperator); length_comperator);
shortest_segment_2 = shortest_segment_2 =
PickNextLongestSegment(shortest_path_set_difference, shortest_segment_1.name_id); pickNextLongestSegment(shortest_path_set_difference, shortest_segment_1.name_id);
// compute the set difference (for alternative path) depending on names between shortest and // compute the set difference (for alternative path) depending on names between shortest and
// alternative // alternative
// vectors are still sorted, no need to do again // vectors are still sorted, no need to do again
BOOST_ASSERT(std::is_sorted(shortest_path_segments.begin(), shortest_path_segments.end(), BOOST_ASSERT(std::is_sorted(shortest_path_segments.begin(), shortest_path_segments.end(),
name_id_comperator)); name_id_comperator));
BOOST_ASSERT(std::is_sorted(alternative_path_segments.begin(), BOOST_ASSERT(std::is_sorted(alternative_path_segments.begin(), alternative_path_segments.end(),
alternative_path_segments.end(), name_id_comperator)); name_id_comperator));
std::vector<SegmentT> alternative_path_set_difference(alternative_path_segments.size()); std::vector<SegmentT> alternative_path_set_difference(alternative_path_segments.size());
std::set_difference(alternative_path_segments.begin(), alternative_path_segments.end(), std::set_difference(alternative_path_segments.begin(), alternative_path_segments.end(),
@ -111,8 +115,8 @@ template <class DataFacadeT, class SegmentT> struct ExtractRouteNames
if (!alternative_path_segments.empty()) if (!alternative_path_segments.empty())
{ {
alternative_segment_2 = PickNextLongestSegment(alternative_path_set_difference, alternative_segment_2 =
alternative_segment_1.name_id); pickNextLongestSegment(alternative_path_set_difference, alternative_segment_1.name_id);
} }
// move the segments into the order in which they occur. // move the segments into the order in which they occur.
@ -136,9 +140,9 @@ template <class DataFacadeT, class SegmentT> struct ExtractRouteNames
route_names.alternative_path_name_2 = route_names.alternative_path_name_2 =
facade->get_name_for_id(alternative_segment_2.name_id); facade->get_name_for_id(alternative_segment_2.name_id);
} }
return route_names; return route_names;
} }
};
} }
} }