From 6b630d30e2a2bd2760a68fb9e589cfd03e9b42c0 Mon Sep 17 00:00:00 2001 From: tatiana-yan Date: Thu, 25 Mar 2021 12:34:19 +0300 Subject: [PATCH] [transit] Reuse direction information from ProjectStopsToShape. --- transit/world_feed/world_feed.cpp | 52 +++++++++---------------------- transit/world_feed/world_feed.hpp | 7 +++-- 2 files changed, 19 insertions(+), 40 deletions(-) diff --git a/transit/world_feed/world_feed.cpp b/transit/world_feed/world_feed.cpp index 406d1fc5b6..9a1b956087 100644 --- a/transit/world_feed/world_feed.cpp +++ b/transit/world_feed/world_feed.cpp @@ -2,7 +2,6 @@ #include "transit/transit_entities.hpp" #include "transit/world_feed/date_time_helpers.hpp" -#include "transit/world_feed/feed_helpers.hpp" #include "platform/platform.hpp" @@ -273,35 +272,6 @@ Link::Link(transit::TransitId lineId, transit::TransitId shapeId, size_t shapeSi : m_lineId(lineId), m_shapeId(shapeId), m_shapeSize(shapeSize) { } - -transit::Direction GetDirection( - transit::StopsOnLines const & stopsOnLines, - std::unordered_map> const & stopIndexes) -{ - auto const & stopIds = stopsOnLines.m_stopSeq; - - if (stopIds.size() <= 1 || !stopsOnLines.m_isValid) - return transit::Direction::Forward; - - for (size_t i = 0; i < stopIds.size() - 1; ++i) - { - auto const id1 = stopIds[i]; - auto const id2 = stopIds[i + 1]; - auto const indexes1 = stopIndexes.find(id1); - auto const indexes2 = stopIndexes.find(id2); - CHECK(indexes1 != stopIndexes.end(), ()); - CHECK(indexes2 != stopIndexes.end(), ()); - if (indexes1->second.size() != 1 || indexes2->second.size() != 1) - continue; - auto const index1 = indexes1->second[0]; - auto const index2 = indexes2->second[0]; - if (index2 == index1) - continue; - return index2 > index1 ? transit::Direction::Forward : transit::Direction::Backward; - } - - return transit::Direction::Forward; -} } // namespace namespace transit @@ -940,7 +910,7 @@ void WorldFeed::FillLinesSchedule() } } -bool WorldFeed::ProjectStopsToShape( +std::optional WorldFeed::ProjectStopsToShape( ShapesIter & itShape, StopsOnLines const & stopsOnLines, std::unordered_map> & stopsToIndexes) { @@ -1006,7 +976,13 @@ bool WorldFeed::ProjectStopsToShape( return true; }; - return tryProject(Direction::Forward) || tryProject(Direction::Backward); + if (tryProject(Direction::Forward)) + return Direction::Forward; + + if (tryProject(Direction::Backward)) + return Direction::Backward; + + return {}; } std::unordered_map> WorldFeed::GetStopsForShapeMatching() @@ -1069,14 +1045,15 @@ std::pair WorldFeed::ModifyShapes() stopsOnLines.m_isValid = false; ++invalidStopSequences; } - else if (!ProjectStopsToShape(itShape, stopsOnLines, stopToShapeIndex)) + else if (auto const direction = ProjectStopsToShape(itShape, stopsOnLines, stopToShapeIndex)) { - stopsOnLines.m_isValid = false; - ++invalidStopSequences; + stopsOnLines.m_direction = *direction; + ++validStopSequences; } else { - ++validStopSequences; + stopsOnLines.m_isValid = false; + ++invalidStopSequences; } if (invalidStopSequences > kMaxInvalidShapesCount) @@ -1088,8 +1065,7 @@ std::pair WorldFeed::ModifyShapes() IdList const & stopIds = stopsOnLines.m_stopSeq; auto const & lineIds = stopsOnLines.m_lines; auto indexes = stopToShapeIndex; - - auto const direction = GetDirection(stopsOnLines, indexes); + auto const direction = stopsOnLines.m_direction; size_t lastIndex = direction == Direction::Forward ? 0 : std::numeric_limits::max(); for (size_t i = 0; i < stopIds.size() - 1; ++i) diff --git a/transit/world_feed/world_feed.hpp b/transit/world_feed/world_feed.hpp index 2fdd99cd0c..efaea7e665 100644 --- a/transit/world_feed/world_feed.hpp +++ b/transit/world_feed/world_feed.hpp @@ -5,6 +5,7 @@ #include "transit/transit_entities.hpp" #include "transit/transit_schedule.hpp" #include "transit/world_feed/color_picker.hpp" +#include "transit/world_feed/feed_helpers.hpp" #include "geometry/mercator.hpp" #include "geometry/point2d.hpp" @@ -244,6 +245,7 @@ struct StopsOnLines IdList m_stopSeq; IdSet m_lines; bool m_isValid = true; + transit::Direction m_direction = Direction::Forward; }; using IdsInRegion = std::unordered_map; @@ -338,8 +340,9 @@ private: // Recalculates 0-weights of edges based on the shape length. bool UpdateEdgeWeights(); - bool ProjectStopsToShape(ShapesIter & itShape, StopsOnLines const & stopsOnLines, - std::unordered_map> & stopsToIndexes); + std::optional ProjectStopsToShape( + ShapesIter & itShape, StopsOnLines const & stopsOnLines, + std::unordered_map> & stopsToIndexes); // Splits data into regions. void SplitFeedIntoRegions();