Make RouteNameExtraction a free standing function
This commit is contained in:
parent
0627c3443d
commit
03f64a6c20
@ -48,7 +48,6 @@ template <typename DataFacadeT> class ApiResponseGenerator
|
||||
using DataFacade = DataFacadeT;
|
||||
using Segments = guidance::SegmentList<DataFacade>;
|
||||
using Segment = detail::Segment;
|
||||
using RouteNameExtractor = ExtractRouteNames<DataFacade, Segment>;
|
||||
|
||||
ApiResponseGenerator(DataFacade *facade);
|
||||
|
||||
@ -120,7 +119,6 @@ void ApiResponseGenerator<DataFacadeT>::DescribeRoute(const RouteParameters &con
|
||||
}
|
||||
|
||||
RouteNames route_names;
|
||||
RouteNameExtractor generate_route_names;
|
||||
|
||||
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
|
||||
auto path_segments = BuildRouteSegments(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;
|
||||
@ -172,7 +170,7 @@ void ApiResponseGenerator<DataFacadeT>::DescribeRoute(const RouteParameters &con
|
||||
// generate names for the main route on its own
|
||||
auto path_segments = BuildRouteSegments(segment_list);
|
||||
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;
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace osrm
|
||||
@ -20,12 +21,12 @@ struct RouteNames
|
||||
std::string alternative_path_name_2;
|
||||
};
|
||||
|
||||
// construct routes names
|
||||
template <class DataFacadeT, class SegmentT> struct ExtractRouteNames
|
||||
namespace detail
|
||||
{
|
||||
private:
|
||||
SegmentT PickNextLongestSegment(const std::vector<SegmentT> &segment_list,
|
||||
const unsigned blocked_name_id) const
|
||||
|
||||
template <class SegmentT>
|
||||
SegmentT pickNextLongestSegment(const std::vector<SegmentT> &segment_list,
|
||||
const unsigned blocked_name_id)
|
||||
{
|
||||
SegmentT result_segment;
|
||||
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;
|
||||
}
|
||||
|
||||
public:
|
||||
RouteNames operator()(std::vector<SegmentT> &shortest_path_segments,
|
||||
} // ns detail
|
||||
|
||||
template <class DataFacadeT, class SegmentT>
|
||||
RouteNames extractRouteNames(std::vector<SegmentT> &shortest_path_segments,
|
||||
std::vector<SegmentT> &alternative_path_segments,
|
||||
const DataFacadeT *facade) const
|
||||
const DataFacadeT *facade)
|
||||
{
|
||||
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())
|
||||
{
|
||||
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.
|
||||
std::sort(shortest_path_segments.begin(), shortest_path_segments.end(), length_comperator);
|
||||
shortest_segment_1 = shortest_path_segments[0];
|
||||
|
||||
if (!alternative_path_segments.empty())
|
||||
{
|
||||
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(),
|
||||
length_comperator);
|
||||
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
|
||||
// alternative
|
||||
// vectors are still sorted, no need to do again
|
||||
BOOST_ASSERT(std::is_sorted(shortest_path_segments.begin(), shortest_path_segments.end(),
|
||||
name_id_comperator));
|
||||
BOOST_ASSERT(std::is_sorted(alternative_path_segments.begin(),
|
||||
alternative_path_segments.end(), name_id_comperator));
|
||||
BOOST_ASSERT(std::is_sorted(alternative_path_segments.begin(), alternative_path_segments.end(),
|
||||
name_id_comperator));
|
||||
|
||||
std::vector<SegmentT> alternative_path_set_difference(alternative_path_segments.size());
|
||||
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())
|
||||
{
|
||||
alternative_segment_2 = PickNextLongestSegment(alternative_path_set_difference,
|
||||
alternative_segment_1.name_id);
|
||||
alternative_segment_2 =
|
||||
pickNextLongestSegment(alternative_path_set_difference, alternative_segment_1.name_id);
|
||||
}
|
||||
|
||||
// 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 =
|
||||
facade->get_name_for_id(alternative_segment_2.name_id);
|
||||
}
|
||||
|
||||
return route_names;
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user