diff --git a/routing/road_graph.cpp b/routing/road_graph.cpp index a7f64755bd..a21c28098a 100644 --- a/routing/road_graph.cpp +++ b/routing/road_graph.cpp @@ -229,6 +229,17 @@ void IRoadGraph::ResetFakes() m_fakeIngoingEdges.clear(); } +void IRoadGraph::AddEdge(Junction const & j, Edge const & e, map & edges) +{ + auto & cont = edges[j]; + ASSERT(is_sorted(cont.cbegin(), cont.cend()), ()); + auto const it = equal_range(cont.cbegin(), cont.cend(), e); + // Note. The "if" condition below is necessary to prevent duplicates which may be added when + // edges from |j| to "projection of |j|" and an edge in the opposite direction are added. + if (it.first == it.second) + cont.insert(it.second, e); +} + void IRoadGraph::AddFakeEdges(Junction const & junction, vector> const & vicinity) { @@ -253,17 +264,10 @@ void IRoadGraph::AddFakeEdges(Junction const & junction, { auto const & u = uv.GetStartJunction(); auto const & v = uv.GetEndJunction(); - m_fakeOutgoingEdges[u].push_back(uv); - m_fakeIngoingEdges[v].push_back(uv); + AddEdge(u, uv, m_fakeOutgoingEdges); + AddEdge(v, uv, m_fakeIngoingEdges); } } - - // The sort unique below is necessary to remove all duplicates which were added when - // edges from |junction| to |p| and from |p| to |junction| were added. - for (auto & m : m_fakeIngoingEdges) - my::SortUnique(m.second); - for (auto & m : m_fakeOutgoingEdges) - my::SortUnique(m.second); } double IRoadGraph::GetSpeedKMPH(Edge const & edge) const diff --git a/routing/road_graph.hpp b/routing/road_graph.hpp index 4274ada5ba..0dc9cd87c4 100644 --- a/routing/road_graph.hpp +++ b/routing/road_graph.hpp @@ -296,6 +296,8 @@ public: void GetFakeIngoingEdges(Junction const & junction, TEdgeVector & edges) const; private: + void AddEdge(Junction const & j, Edge const & e, map & edges); + template void ForEachFakeEdge(Fn && fn) { @@ -312,6 +314,8 @@ private: } } + /// \note |m_fakeIngoingEdges| and |m_fakeOutgoingEdges| are maps of sorted vectors. + /// Items to these maps should be inserted with AddEdge() method only. map m_fakeIngoingEdges; map m_fakeOutgoingEdges; };