reserve when possible

This commit is contained in:
Moritz Kobitzsch 2016-11-11 14:09:04 +01:00
parent 12ded539aa
commit f88ac989ea
14 changed files with 51 additions and 42 deletions

View File

@ -42,7 +42,8 @@ std::string modeToString(const extractor::TravelMode mode);
} // namespace detail } // namespace detail
template <unsigned POLYLINE_PRECISION, typename ForwardIter> util::json::String makePolyline(ForwardIter begin, ForwardIter end) template <unsigned POLYLINE_PRECISION, typename ForwardIter>
util::json::String makePolyline(ForwardIter begin, ForwardIter end)
{ {
return {encodePolyline<POLYLINE_PRECISION>(begin, end)}; return {encodePolyline<POLYLINE_PRECISION>(begin, end)};
} }

View File

@ -196,16 +196,16 @@ class RouteAPI : public BaseAPI
[this, &leg_geometry](const guidance::RouteStep &step) { [this, &leg_geometry](const guidance::RouteStep &step) {
if (parameters.geometries == RouteParameters::GeometriesType::Polyline) if (parameters.geometries == RouteParameters::GeometriesType::Polyline)
{ {
return static_cast<util::json::Value>( return static_cast<util::json::Value>(json::makePolyline<100000>(
json::makePolyline<100000>(leg_geometry.locations.begin() + step.geometry_begin, leg_geometry.locations.begin() + step.geometry_begin,
leg_geometry.locations.begin() + step.geometry_end)); leg_geometry.locations.begin() + step.geometry_end));
} }
if (parameters.geometries == RouteParameters::GeometriesType::Polyline6) if (parameters.geometries == RouteParameters::GeometriesType::Polyline6)
{ {
return static_cast<util::json::Value>( return static_cast<util::json::Value>(json::makePolyline<1000000>(
json::makePolyline<1000000>(leg_geometry.locations.begin() + step.geometry_begin, leg_geometry.locations.begin() + step.geometry_begin,
leg_geometry.locations.begin() + step.geometry_end)); leg_geometry.locations.begin() + step.geometry_end));
} }
BOOST_ASSERT(parameters.geometries == RouteParameters::GeometriesType::GeoJSON); BOOST_ASSERT(parameters.geometries == RouteParameters::GeometriesType::GeoJSON);

View File

@ -233,7 +233,6 @@ inline std::vector<RouteStep> assembleSteps(const datafacade::BaseDataFacade &fa
WaypointType::Arrive, WaypointType::Arrive,
0}; 0};
BOOST_ASSERT(!leg_geometry.locations.empty()); BOOST_ASSERT(!leg_geometry.locations.empty());
steps.push_back(RouteStep{target_node.name_id, steps.push_back(RouteStep{target_node.name_id,
facade.GetNameForID(target_node.name_id), facade.GetNameForID(target_node.name_id),

View File

@ -3,8 +3,8 @@
#include "util/coordinate.hpp" #include "util/coordinate.hpp"
#include <boost/assert.hpp>
#include <algorithm> #include <algorithm>
#include <boost/assert.hpp>
#include <string> #include <string>
#include <vector> #include <vector>
@ -13,17 +13,17 @@ namespace osrm
namespace engine namespace engine
{ {
namespace detail namespace detail
{ {
constexpr double POLYLINE_DECODING_PRECISION = 1e5; constexpr double POLYLINE_DECODING_PRECISION = 1e5;
constexpr double POLYLINE_TO_COORDINATE = COORDINATE_PRECISION / POLYLINE_DECODING_PRECISION; constexpr double POLYLINE_TO_COORDINATE = COORDINATE_PRECISION / POLYLINE_DECODING_PRECISION;
std::string encode(std::vector<int> &numbers); std::string encode(std::vector<int> &numbers);
} }
using CoordVectorForwardIter = std::vector<util::Coordinate>::const_iterator; using CoordVectorForwardIter = std::vector<util::Coordinate>::const_iterator;
// Encodes geometry into polyline format. // Encodes geometry into polyline format.
// See: https://developers.google.com/maps/documentation/utilities/polylinealgorithm // See: https://developers.google.com/maps/documentation/utilities/polylinealgorithm
template<unsigned POLYLINE_PRECISION=100000> template <unsigned POLYLINE_PRECISION = 100000>
std::string encodePolyline(CoordVectorForwardIter begin, CoordVectorForwardIter end) std::string encodePolyline(CoordVectorForwardIter begin, CoordVectorForwardIter end)
{ {
double coordinate_to_polyline = POLYLINE_PRECISION / COORDINATE_PRECISION; double coordinate_to_polyline = POLYLINE_PRECISION / COORDINATE_PRECISION;
@ -39,7 +39,10 @@ std::string encodePolyline(CoordVectorForwardIter begin, CoordVectorForwardIter
int current_lat = 0; int current_lat = 0;
int current_lon = 0; int current_lon = 0;
std::for_each( std::for_each(
begin, end, [&delta_numbers, &current_lat, &current_lon, coordinate_to_polyline](const util::Coordinate loc) { begin,
end,
[&delta_numbers, &current_lat, &current_lon, coordinate_to_polyline](
const util::Coordinate loc) {
const int lat_diff = const int lat_diff =
std::round(static_cast<int>(loc.lat) * coordinate_to_polyline) - current_lat; std::round(static_cast<int>(loc.lat) * coordinate_to_polyline) - current_lat;
const int lon_diff = const int lon_diff =

View File

@ -87,7 +87,8 @@ struct LengthLimitedCoordinateAccumulator
}; };
/* /*
* The SelectRoadByNameOnlyChoiceAndStraightness tries to follow a given name along a route. We offer methods to skip * The SelectRoadByNameOnlyChoiceAndStraightness tries to follow a given name along a route. We
* offer methods to skip
* over bridges/similar situations if desired, following narrow turns * over bridges/similar situations if desired, following narrow turns
* This struct offers an example implementation of a possible road selector for traversing the * This struct offers an example implementation of a possible road selector for traversing the
* node-based graph using the NodeBasedGraphWalker * node-based graph using the NodeBasedGraphWalker
@ -115,14 +116,13 @@ struct SelectRoadByNameOnlyChoiceAndStraightness
// find the next intersection given a hop limit // find the next intersection given a hop limit
struct IntersectionFinderAccumulator struct IntersectionFinderAccumulator
{ {
IntersectionFinderAccumulator(const std::uint8_t hop_limit, const IntersectionGenerator &intersection_generator); IntersectionFinderAccumulator(const std::uint8_t hop_limit,
const IntersectionGenerator &intersection_generator);
// true if the path has traversed enough distance // true if the path has traversed enough distance
bool terminate(); bool terminate();
// update the accumulator // update the accumulator
void update(const NodeID from_node, void update(const NodeID from_node, const EdgeID via_edge, const NodeID to_node);
const EdgeID via_edge,
const NodeID to_node);
std::uint8_t hops; std::uint8_t hops;
const std::uint8_t hop_limit; const std::uint8_t hop_limit;
@ -136,8 +136,6 @@ struct IntersectionFinderAccumulator
Intersection intersection; Intersection intersection;
}; };
template <class accumulator_type, class selector_type> template <class accumulator_type, class selector_type>
boost::optional<std::pair<NodeID, EdgeID>> boost::optional<std::pair<NodeID, EdgeID>>
NodeBasedGraphWalker::TraverseRoad(NodeID current_node_id, NodeBasedGraphWalker::TraverseRoad(NodeID current_node_id,

View File

@ -25,8 +25,8 @@ inline void print(const engine::guidance::RouteStep &step)
{ {
std::cout << static_cast<int>(step.maneuver.instruction.type) << " " std::cout << static_cast<int>(step.maneuver.instruction.type) << " "
<< static_cast<int>(step.maneuver.instruction.direction_modifier) << " " << static_cast<int>(step.maneuver.instruction.direction_modifier) << " "
<< static_cast<int>(step.maneuver.waypoint_type) << " " << static_cast<int>(step.maneuver.waypoint_type) << " " << step.maneuver.location
<< step.maneuver.location << " " << " "
<< " Duration: " << step.duration << " Distance: " << step.distance << " Duration: " << step.duration << " Distance: " << step.distance
<< " Geometry: " << step.geometry_begin << " " << step.geometry_end << " Geometry: " << step.geometry_begin << " " << step.geometry_end
<< "\n\tIntersections: " << step.intersections.size() << " ["; << "\n\tIntersections: " << step.intersections.size() << " [";
@ -44,7 +44,8 @@ inline void print(const engine::guidance::RouteStep &step)
std::cout << " " << (entry ? "true" : "false"); std::cout << " " << (entry ? "true" : "false");
std::cout << ")"; std::cout << ")";
} }
std::cout << "] name[" << step.name_id << "]: " << step.name << " Ref: " << step.ref << " Pronunciation: " << step.pronunciation; std::cout << "] name[" << step.name_id << "]: " << step.name << " Ref: " << step.ref
<< " Pronunciation: " << step.pronunciation;
} }
inline void print(const std::vector<engine::guidance::RouteStep> &steps) inline void print(const std::vector<engine::guidance::RouteStep> &steps)

View File

@ -39,6 +39,8 @@ namespace guidance
class BearingClass class BearingClass
{ {
public: public:
BearingClass();
// Add a bearing to the set // Add a bearing to the set
void add(const DiscreteBearing bearing); void add(const DiscreteBearing bearing);

View File

@ -818,7 +818,7 @@ void collapseTurnAt(std::vector<RouteStep> &steps,
// //
bool isStaggeredIntersection(const RouteStep &previous, const RouteStep &current) bool isStaggeredIntersection(const RouteStep &previous, const RouteStep &current)
{ {
//don't touch roundabouts // don't touch roundabouts
if (entersRoundabout(previous.maneuver.instruction) || if (entersRoundabout(previous.maneuver.instruction) ||
entersRoundabout(current.maneuver.instruction)) entersRoundabout(current.maneuver.instruction))
return false; return false;

View File

@ -56,6 +56,8 @@ Intersection IntersectionGenerator::GetConnectedRoads(const NodeID from_node,
{ {
Intersection intersection; Intersection intersection;
const NodeID turn_node = node_based_graph.GetTarget(via_eid); const NodeID turn_node = node_based_graph.GetTarget(via_eid);
// reserve enough items (+ the possibly missing u-turn edge)
intersection.reserve(node_based_graph.GetOutDegree(turn_node) + 1);
const NodeID only_restriction_to_node = [&]() { const NodeID only_restriction_to_node = [&]() {
// If only restrictions refer to invalid ways somewhere far away, we rather ignore the // If only restrictions refer to invalid ways somewhere far away, we rather ignore the
// restriction than to not route over the intersection at all. // restriction than to not route over the intersection at all.

View File

@ -215,22 +215,21 @@ operator()(const NodeID, const EdgeID source_edge_id, Intersection intersection)
const bool is_through_street = const bool is_through_street =
!target_intersection.empty() && link_data.name_id != EMPTY_NAMEID && !target_intersection.empty() && link_data.name_id != EMPTY_NAMEID &&
target_intersection.end() != target_intersection.end() !=
std::find_if(target_intersection.begin() + 1, std::find_if(
target_intersection.end(), target_intersection.begin() + 1,
[this, &link_data](const ConnectedRoad &road) { target_intersection.end(),
const auto &road_edge_data = [this, &link_data](const ConnectedRoad &road) {
node_based_graph.GetEdgeData(road.eid); const auto &road_edge_data = node_based_graph.GetEdgeData(road.eid);
const auto same_name = const auto same_name =
road_edge_data.name_id != EMPTY_NAMEID && road_edge_data.name_id != EMPTY_NAMEID &&
!util::guidance::requiresNameAnnounced( !util::guidance::requiresNameAnnounced(road_edge_data.name_id,
road_edge_data.name_id, link_data.name_id,
link_data.name_id, name_table,
name_table, street_name_suffix_table);
street_name_suffix_table);
return same_name; return same_name;
}); });
// if the sliproad candidate is a through street, we cannot handle it as a sliproad // if the sliproad candidate is a through street, we cannot handle it as a sliproad
if (is_through_street) if (is_through_street)

View File

@ -106,6 +106,7 @@ LaneDataVector laneDataFromDescription(TurnLaneDescription turn_lane_description
// transform the map into the lane data vector // transform the map into the lane data vector
LaneDataVector lane_data; LaneDataVector lane_data;
lane_data.reserve(lane_map.size());
for (const auto tag : lane_map) for (const auto tag : lane_map)
lane_data.push_back({tag.first, tag.second.first, tag.second.second, false}); lane_data.push_back({tag.first, tag.second.first, tag.second.second, false});

View File

@ -619,6 +619,8 @@ std::pair<LaneDataVector, LaneDataVector> TurnLaneHandler::partitionLaneData(
}; };
LaneDataVector first, second; LaneDataVector first, second;
first.reserve(turn_lane_data.size());
second.reserve(turn_lane_data.size());
for (std::size_t lane = 0; lane < turn_lane_data.size(); ++lane) for (std::size_t lane = 0; lane < turn_lane_data.size(); ++lane)
{ {
if (matched_at_second[lane]) if (matched_at_second[lane])

View File

@ -11,6 +11,8 @@ namespace util
namespace guidance namespace guidance
{ {
BearingClass::BearingClass() { available_bearings.reserve(10); }
bool BearingClass::operator==(const BearingClass &other) const bool BearingClass::operator==(const BearingClass &other) const
{ {
BOOST_ASSERT(std::is_sorted(available_bearings.begin(), available_bearings.end())); BOOST_ASSERT(std::is_sorted(available_bearings.begin(), available_bearings.end()));

View File

@ -65,7 +65,6 @@ BOOST_AUTO_TEST_CASE(encode)
BOOST_CHECK_EQUAL(encodedPolyline, polyline); BOOST_CHECK_EQUAL(encodedPolyline, polyline);
} }
BOOST_AUTO_TEST_CASE(encode6) BOOST_AUTO_TEST_CASE(encode6)
{ {
// Coordinates; these would be the coordinates we give the loc parameter, // Coordinates; these would be the coordinates we give the loc parameter,