diff --git a/routing/routing_tests/astar_algorithm_test.cpp b/routing/routing_tests/astar_algorithm_test.cpp index 3b6373ad05..ef9bd802f0 100644 --- a/routing/routing_tests/astar_algorithm_test.cpp +++ b/routing/routing_tests/astar_algorithm_test.cpp @@ -4,6 +4,8 @@ #include "routing/base/astar_graph.hpp" #include "routing/base/routing_result.hpp" +#include "routing/routing_tests/routing_algorithm.hpp" + #include #include #include @@ -13,52 +15,7 @@ namespace routing_test using namespace routing; using namespace std; -struct Edge -{ - Edge(unsigned v, double w) : v(v), w(w) {} - - unsigned GetTarget() const { return v; } - double GetWeight() const { return w; } - - unsigned v; - double w; -}; - -class UndirectedGraph : public AStarGraph -{ -public: - - void AddEdge(unsigned u, unsigned v, unsigned w) - { - m_adjs[u].push_back(Edge(v, w)); - m_adjs[v].push_back(Edge(u, w)); - } - - void GetAdjacencyList(unsigned v, vector & adj) const - { - adj.clear(); - auto const it = m_adjs.find(v); - if (it != m_adjs.end()) - adj = it->second; - } - - void GetIngoingEdgesList(Vertex const & v, vector & adj) override - { - GetAdjacencyList(v, adj); - } - - void GetOutgoingEdgesList(Vertex const & v, vector & adj) override - { - GetAdjacencyList(v, adj); - } - - double HeuristicCostEstimate(Vertex const & v, Vertex const & w) override { return 0; } - -private: - map> m_adjs; -}; - -using Algorithm = AStarAlgorithm; +using Algorithm = AStarAlgorithm; void TestAStar(UndirectedGraph & graph, vector const & expectedRoute, double const & expectedDistance) { @@ -132,7 +89,7 @@ UNIT_TEST(AdjustRoute) graph.AddEdge(6, 2, 1); // Each edge contains {vertexId, weight}. - vector const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}}; + vector const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}}; Algorithm algo; Algorithm::ParamsForTests params(graph, 6 /* startVertex */, {} /* finishVertex */, &prevRoute, @@ -154,7 +111,7 @@ UNIT_TEST(AdjustRouteNoPath) graph.AddEdge(i /* from */, i + 1 /* to */, 1 /* weight */); // Each edge contains {vertexId, weight}. - vector const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}}; + vector const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}}; Algorithm algo; Algorithm::ParamsForTests params(graph, 6 /* startVertex */, {} /* finishVertex */, &prevRoute, @@ -176,7 +133,7 @@ UNIT_TEST(AdjustRouteOutOfLimit) graph.AddEdge(6, 2, 2); // Each edge contains {vertexId, weight}. - vector const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}}; + vector const prevRoute = {{0, 0}, {1, 1}, {2, 1}, {3, 1}, {4, 1}, {5, 1}}; Algorithm algo; Algorithm::ParamsForTests params(graph, 6 /* startVertex */, {} /* finishVertex */, &prevRoute, diff --git a/routing/routing_tests/routing_algorithm.cpp b/routing/routing_tests/routing_algorithm.cpp index 2b89beb314..6127a1dd1e 100644 --- a/routing/routing_tests/routing_algorithm.cpp +++ b/routing/routing_tests/routing_algorithm.cpp @@ -14,6 +14,59 @@ #include #include +namespace routing_test +{ +void UndirectedGraph::AddEdge(Vertex u, Vertex v, Weight w) +{ + m_adjs[u].emplace_back(v, w); + m_adjs[v].emplace_back(u, w); +} + +size_t UndirectedGraph::GetNodesNumber() const +{ + return m_adjs.size(); +} + +void UndirectedGraph::GetIngoingEdgesList(Vertex const & v, std::vector & adj) +{ + GetAdjacencyList(v, adj); +} + +void UndirectedGraph::GetOutgoingEdgesList(Vertex const & v, std::vector & adj) +{ + GetAdjacencyList(v, adj); +} + +double UndirectedGraph::HeuristicCostEstimate(Vertex const & v, Vertex const & w) +{ + return 0; +} + +void UndirectedGraph::GetAdjacencyList(Vertex v, std::vector & adj) const +{ + adj.clear(); + auto const it = m_adjs.find(v); + if (it != m_adjs.cend()) + adj = it->second; +} + +void DirectedGraph::AddEdge(Vertex from, Vertex to, Weight w) +{ + m_outgoing[from].emplace_back(to, w); + m_ingoing[to].emplace_back(from, w); +} + +void DirectedGraph::GetIngoingEdgesList(Vertex const & v, vector & adj) +{ + adj = m_ingoing[v]; +} + +void DirectedGraph::GetOutgoingEdgesList(Vertex const & v, vector & adj) +{ + adj = m_outgoing[v]; +} +} // namespace routing_tests + namespace routing { using namespace std; diff --git a/routing/routing_tests/routing_algorithm.hpp b/routing/routing_tests/routing_algorithm.hpp index 93168aeb49..6fd8a938a7 100644 --- a/routing/routing_tests/routing_algorithm.hpp +++ b/routing/routing_tests/routing_algorithm.hpp @@ -1,11 +1,67 @@ #pragma once +#include "routing/base/astar_algorithm.hpp" #include "routing/base/routing_result.hpp" + #include "routing/road_graph.hpp" #include "routing/router.hpp" +#include +#include #include +namespace routing_test +{ +using namespace routing; + +struct SimpleEdge +{ + SimpleEdge(uint32_t to, double weight) : m_to(to), m_weight(weight) {} + + uint32_t GetTarget() const { return m_to; } + double GetWeight() const { return m_weight; } + + uint32_t m_to; + double m_weight; +}; + +class UndirectedGraph : public AStarGraph +{ +public: + void AddEdge(Vertex u, Vertex v, Weight w); + size_t GetNodesNumber() const; + + // AStarGraph overrides + // @{ + void GetIngoingEdgesList(Vertex const & v, vector & adj) override; + void GetOutgoingEdgesList(Vertex const & v, vector & adj) override; + double HeuristicCostEstimate(Vertex const & v, Vertex const & w) override; + // @} + +private: + void GetAdjacencyList(Vertex v, vector & adj) const; + + std::map> m_adjs; +}; + +class DirectedGraph +{ +public: + using Vertex = uint32_t; + using Edge = SimpleEdge; + using Weight = double; + + void AddEdge(Vertex from, Vertex to, Weight w); + + void GetIngoingEdgesList(Vertex const & v, vector & adj); + void GetOutgoingEdgesList(Vertex const & v, vector & adj); + +private: + std::map> m_outgoing; + std::map> m_ingoing; +}; +} // namespace routing_tests + namespace routing { class TestAStarBidirectionalAlgo