From fd4425cbad7e8604db9e70fb4d44cd6039c18332 Mon Sep 17 00:00:00 2001 From: tatiana-kondakova Date: Mon, 25 Dec 2017 00:24:51 +0300 Subject: [PATCH] Fix AStarAlgirithm::Params lifetime --- routing/base/astar_algorithm.hpp | 15 +++++----- routing/cross_mwm_router.cpp | 2 +- routing/index_router.cpp | 16 +++++------ routing/routing_algorithm.cpp | 2 +- .../routing_tests/astar_algorithm_test.cpp | 28 +++++++++++-------- routing/routing_tests/index_graph_tools.cpp | 13 +++++---- 6 files changed, 43 insertions(+), 33 deletions(-) diff --git a/routing/base/astar_algorithm.hpp b/routing/base/astar_algorithm.hpp index 44ccc24dbe..05b0130ca9 100644 --- a/routing/base/astar_algorithm.hpp +++ b/routing/base/astar_algorithm.hpp @@ -56,7 +56,7 @@ public: struct Params { Params(Graph & graph, Vertex const & startVertex, Vertex const & finalVertex, - std::vector const & prevRoute, my::Cancellable const & cancellable, + std::vector const * prevRoute, my::Cancellable const & cancellable, OnVisitedVertexCallback const & onVisitedVertexCallback, CheckLengthCallback const & checkLengthCallback) : m_graph(graph) @@ -73,14 +73,14 @@ public: } Graph & m_graph; - Vertex const & m_startVertex; + Vertex const m_startVertex; // Used for FindPath, FindPathBidirectional. - Vertex const & m_finalVertex; + Vertex const m_finalVertex; // Used for AdjustRoute. - std::vector const & m_prevRoute; + std::vector const * m_prevRoute; my::Cancellable const & m_cancellable; - OnVisitedVertexCallback m_onVisitedVertexCallback; - CheckLengthCallback m_checkLengthCallback; + OnVisitedVertexCallback const m_onVisitedVertexCallback; + CheckLengthCallback const m_checkLengthCallback; }; class Context final @@ -558,9 +558,10 @@ template typename AStarAlgorithm::Result AStarAlgorithm::AdjustRoute( Params & params, RoutingResult & result) const { + CHECK(params.m_prevRoute, ()); auto & graph = params.m_graph; auto const & startVertex = params.m_startVertex; - auto const & prevRoute = params.m_prevRoute; + auto const & prevRoute = *params.m_prevRoute; CHECK(!prevRoute.empty(), ()); diff --git a/routing/cross_mwm_router.cpp b/routing/cross_mwm_router.cpp index 8a4fa03446..2ceaa7091d 100644 --- a/routing/cross_mwm_router.cpp +++ b/routing/cross_mwm_router.cpp @@ -23,7 +23,7 @@ IRouter::ResultCode CalculateRoute(BorderCross const & startPos, BorderCross con delegate.OnPointCheck(MercatorBounds::FromLatLon(cross.fromNode.point)); }; - Algorithm::Params params(roadGraph, startPos, finalPos, {} /* prevRoute */, delegate, + Algorithm::Params params(roadGraph, startPos, finalPos, nullptr /* prevRoute */, delegate, onVisitedVertex, {} /* checkLengthCallback */); my::HighResTimer timer(true); diff --git a/routing/index_router.cpp b/routing/index_router.cpp index fa9d1f9365..5daa28fec2 100644 --- a/routing/index_router.cpp +++ b/routing/index_router.cpp @@ -564,9 +564,9 @@ IRouter::ResultCode IndexRouter::CalculateSubroute(Checkpoints const & checkpoin RoutingResult routingResult; - AStarAlgorithm::Params params(starter, starter.GetStartSegment(), - starter.GetFinishSegment(), {} /* prevRoute */, - delegate, onVisitJunction, checkLength); + AStarAlgorithm::Params params( + starter, starter.GetStartSegment(), starter.GetFinishSegment(), nullptr /* prevRoute */, + delegate, onVisitJunction, checkLength); IRouter::ResultCode const result = FindPath(params, routingResult); if (result != IRouter::NoError) return result; @@ -642,7 +642,7 @@ IRouter::ResultCode IndexRouter::AdjustRoute(Checkpoints const & checkpoints, AStarAlgorithm algorithm; AStarAlgorithm::Params params(starter, starter.GetStartSegment(), - {} /* finalVertex */, prevEdges, delegate, + {} /* finalVertex */, &prevEdges, delegate, onVisitJunction, checkLength); RoutingResult result; auto const resultCode = ConvertResult(algorithm.AdjustRoute(params, result)); @@ -791,9 +791,9 @@ IRouter::ResultCode IndexRouter::ProcessLeaps(vector const & input, // In case of leaps from the start to its mwm transition and from finish to mwm transition // route calculation should be made on the world graph (WorldGraph::Mode::NoLeaps). worldGraph.SetMode(WorldGraph::Mode::NoLeaps); - AStarAlgorithm::Params params(starter, current, next, {} /* prevRoute */, - delegate, {} /* onVisitedVertexCallback */, - checkLength); + AStarAlgorithm::Params params( + starter, current, next, nullptr /* prevRoute */, delegate, + {} /* onVisitedVertexCallback */, checkLength); result = FindPath(params, routingResult); } else @@ -808,7 +808,7 @@ IRouter::ResultCode IndexRouter::ProcessLeaps(vector const & input, worldGraph.SetMode(WorldGraph::Mode::SingleMwm); // It's not start-to-mwm-exit leap, we already have its first segment in previous mwm. dropFirstSegment = true; - AStarAlgorithm::Params params(worldGraph, current, next, {} /* prevRoute */, + AStarAlgorithm::Params params(worldGraph, current, next, nullptr /* prevRoute */, delegate, {} /* onVisitedVertexCallback */, checkLength); result = FindPath(params, routingResult); diff --git a/routing/routing_algorithm.cpp b/routing/routing_algorithm.cpp index b70ac25d99..3da9cffc27 100644 --- a/routing/routing_algorithm.cpp +++ b/routing/routing_algorithm.cpp @@ -157,7 +157,7 @@ IRoutingAlgorithm::Result AStarRoutingAlgorithm::CalculateRoute(IRoadGraph const my::Cancellable const & cancellable = delegate; progress.Initialize(startPos.GetPoint(), finalPos.GetPoint()); RoadGraph roadGraph(graph); - TAlgorithmImpl::Params params(roadGraph, startPos, finalPos, {} /* prevRoute */, cancellable, + TAlgorithmImpl::Params params(roadGraph, startPos, finalPos, nullptr /* prevRoute */, cancellable, onVisitJunctionFn, {} /* checkLength */); TAlgorithmImpl::Result const res = TAlgorithmImpl().FindPath(params, path); return Convert(res); diff --git a/routing/routing_tests/astar_algorithm_test.cpp b/routing/routing_tests/astar_algorithm_test.cpp index 59d843ea01..77258f4052 100644 --- a/routing/routing_tests/astar_algorithm_test.cpp +++ b/routing/routing_tests/astar_algorithm_test.cpp @@ -66,9 +66,10 @@ void TestAStar(UndirectedGraph & graph, vector const & expectedRoute, { TAlgorithm algo; - TAlgorithm::Params params(graph, 0u /* startVertex */, 4u /* finishVertex */, {} /* prevRoute */, - {} /* cancellable */, {} /* onVisitedVerteCallback */, - {} /* checkLengthCallback */); + my::Cancellable cancellable; + TAlgorithm::Params params(graph, 0u /* startVertex */, 4u /* finishVertex */, + nullptr /* prevRoute */, cancellable /* cancellable */, + {} /* onVisitedVerteCallback */, {} /* checkLengthCallback */); RoutingResult actualRoute; TEST_EQUAL(TAlgorithm::Result::OK, algo.FindPath(params, actualRoute), ()); @@ -109,8 +110,10 @@ UNIT_TEST(AStarAlgorithm_CheckLength) graph.AddEdge(3, 4, 3); TAlgorithm algo; - TAlgorithm::Params params(graph, 0u /* startVertex */, 4u /* finishVertex */, {} /* prevRoute */, - {} /* cancellable */, {} /* onVisitedVerteCallback */, + my::Cancellable cancellable; + TAlgorithm::Params params(graph, 0u /* startVertex */, 4u /* finishVertex */, + nullptr /* prevRoute */, cancellable /* cancellable */, + {} /* onVisitedVerteCallback */, [](double weight) { return weight < 23; }); RoutingResult routingResult; TAlgorithm::Result result = algo.FindPath(params, routingResult); @@ -138,8 +141,9 @@ UNIT_TEST(AdjustRoute) vector const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}}; TAlgorithm algo; - TAlgorithm::Params params(graph, 6 /* startVertex */, {} /* finishVertex */, prevRoute, - {} /* cancellable */, {} /* onVisitedVerteCallback */, + my::Cancellable cancellable; + TAlgorithm::Params params(graph, 6 /* startVertex */, {} /* finishVertex */, &prevRoute, + cancellable /* cancellable */, {} /* onVisitedVerteCallback */, [](double weight) { return weight <= 1.0; }); RoutingResult result; auto const code = algo.AdjustRoute(params, result); @@ -161,8 +165,9 @@ UNIT_TEST(AdjustRouteNoPath) vector const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}}; TAlgorithm algo; - TAlgorithm::Params params(graph, 6 /* startVertex */, {} /* finishVertex */, prevRoute, - {} /* cancellable */, {} /* onVisitedVerteCallback */, + my::Cancellable cancellable; + TAlgorithm::Params params(graph, 6 /* startVertex */, {} /* finishVertex */, &prevRoute, + cancellable /* cancellable */, {} /* onVisitedVerteCallback */, [](double weight) { return weight <= 1.0; }); RoutingResult result; auto const code = algo.AdjustRoute(params, result); @@ -184,8 +189,9 @@ UNIT_TEST(AdjustRouteOutOfLimit) vector const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}}; TAlgorithm algo; - TAlgorithm::Params params(graph, 6 /* startVertex */, {} /* finishVertex */, prevRoute, - {} /* cancellable */, {} /* onVisitedVerteCallback */, + my::Cancellable cancellable; + TAlgorithm::Params params(graph, 6 /* startVertex */, {} /* finishVertex */, &prevRoute, + cancellable /* cancellable */, {} /* onVisitedVerteCallback */, [](double weight) { return weight <= 1.0; }); RoutingResult result; auto const code = algo.AdjustRoute(params, result); diff --git a/routing/routing_tests/index_graph_tools.cpp b/routing/routing_tests/index_graph_tools.cpp index bbf67c4960..b51f46a698 100644 --- a/routing/routing_tests/index_graph_tools.cpp +++ b/routing/routing_tests/index_graph_tools.cpp @@ -172,9 +172,11 @@ bool TestIndexGraphTopology::FindPath(Vertex start, Vertex finish, double & path AStarAlgorithm algorithm; - AStarAlgorithm::Params params( - *worldGraph, startSegment, finishSegment, {} /* prevRoute */, {} /* cancellable */, - {} /* onVisitedVertexCallback */, {} /* checkLengthCallback */); + my::Cancellable cancellable; + AStarAlgorithm::Params params(*worldGraph, startSegment, finishSegment, + nullptr /* prevRoute */, cancellable /* cancellable */, + {} /* onVisitedVertexCallback */, + {} /* checkLengthCallback */); RoutingResult routingResult; auto const resultCode = algorithm.FindPathBidirectional(params, routingResult); @@ -364,9 +366,10 @@ AStarAlgorithm::Result CalculateRoute(IndexGraphStarter & sta AStarAlgorithm algorithm; RoutingResult routingResult; + my::Cancellable cancellable; AStarAlgorithm::Params params( - starter, starter.GetStartSegment(), starter.GetFinishSegment(), {} /* prevRoute */, - {} /* cancellable */, {} /* onVisitedVertexCallback */, + starter, starter.GetStartSegment(), starter.GetFinishSegment(), nullptr /* prevRoute */, + cancellable /* cancellable */, {} /* onVisitedVertexCallback */, [&](RouteWeight const & weight) { return starter.CheckLength(weight); }); auto const resultCode = algorithm.FindPathBidirectional(params, routingResult);