From af3a06ed94434f4e417b56ad3a5c497c7e6d0c40 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Tue, 8 Nov 2016 09:00:17 +0300 Subject: [PATCH] Using SimpleTokenizer and some other review fixes. --- .../restriction_collector_test.cpp | 6 +-- generator/restriction_collector.cpp | 49 ++++++++----------- generator/restriction_collector.hpp | 4 +- indexer/routing.cpp | 5 +- indexer/routing.hpp | 9 ++-- 5 files changed, 31 insertions(+), 42 deletions(-) diff --git a/generator/generator_tests/restriction_collector_test.cpp b/generator/generator_tests/restriction_collector_test.cpp index c0d537ec29..562b10b2d9 100644 --- a/generator/generator_tests/restriction_collector_test.cpp +++ b/generator/generator_tests/restriction_collector_test.cpp @@ -123,13 +123,13 @@ UNIT_TEST(RestrictionTest_ParseFeatureId2OsmIdsMapping) restrictionCollector.ParseFeatureId2OsmIdsMapping( my::JoinFoldersToPath(platform.WritableDir(), kFeatureIdToOsmIdsPath)); - vector> const expectedOsmIds2FeatureId = { + vector> const expectedOsmIds2FeatureId = { {10, 1}, {20, 2}, {30, 3}, {5423239545, 779703}}; - vector> osmIds2FeatureId( + vector> osmIds2FeatureId( restrictionCollector.m_osmIds2FeatureId.cbegin(), restrictionCollector.m_osmIds2FeatureId.cend()); sort(osmIds2FeatureId.begin(), osmIds2FeatureId.end(), - my::LessBy(&pair::first)); + my::LessBy(&pair::first)); TEST_EQUAL(osmIds2FeatureId, expectedOsmIds2FeatureId, ()); } diff --git a/generator/restriction_collector.cpp b/generator/restriction_collector.cpp index 72eda3168f..774c08a787 100644 --- a/generator/restriction_collector.cpp +++ b/generator/restriction_collector.cpp @@ -7,29 +7,21 @@ #include "std/algorithm.hpp" #include "std/fstream.hpp" -#include "std/sstream.hpp" namespace { char const kNo[] = "No"; char const kOnly[] = "Only"; -bool ParseLineOfNumbers(istringstream & stream, vector & numbers) +bool ParseLineOfNumbers(strings::SimpleTokenizer & iter, vector & numbers) { - string numberStr; - uint64_t number; - - while (stream) + uint64_t number = 0; + while (iter) { - if (!getline(stream, numberStr, ',')) - return true; - if (numberStr.empty()) - return true; - - if (!strings::to_uint64(numberStr, number)) + if (!strings::to_uint64(*iter, number)) return false; - numbers.push_back(number); + ++iter; } return true; } @@ -72,17 +64,17 @@ bool RestrictionCollector::ParseFeatureId2OsmIdsMapping(string const & featureId if (!getline(featureId2OsmIdsStream, line)) return true; - istringstream lineStream(line); - vector ids; - if (!ParseLineOfNumbers(lineStream, ids)) + vector osmIds; + strings::SimpleTokenizer iter(line, ","); + if (!ParseLineOfNumbers(iter, osmIds)) return false; - if (ids.size() <= 1) + if (osmIds.size() < 2) return false; // Every line should contain at least feature id and osm id. - Restriction::FeatureId const featureId = static_cast(ids.front()); - ids.erase(ids.begin()); - AddFeatureId(featureId, ids); + uint32_t const featureId = static_cast(osmIds.front()); + osmIds.erase(osmIds.begin()); + AddFeatureId(featureId, osmIds); } return true; } @@ -98,15 +90,15 @@ bool RestrictionCollector::ParseRestrictions(string const & restrictionPath) string line; if (!getline(restrictionsStream, line)) return true; - istringstream lineStream(line); - string typeStr; - getline(lineStream, typeStr, ','); + + strings::SimpleTokenizer iter(line, ","); Restriction::Type type; - if (!FromString(typeStr, type)) + if (!FromString(*iter, type)) return false; + ++iter; vector osmIds; - if (!ParseLineOfNumbers(lineStream, osmIds)) + if (!ParseLineOfNumbers(iter, osmIds)) return false; AddRestriction(type, osmIds); @@ -116,7 +108,7 @@ bool RestrictionCollector::ParseRestrictions(string const & restrictionPath) void RestrictionCollector::ComposeRestrictions() { - // Going through all osm id saved in |m_restrictionIndex| (mentioned in restrictions). + // Going through all osm ids saved in |m_restrictionIndex| (mentioned in restrictions). size_t const numRestrictions = m_restrictions.size(); for (auto const & osmIdAndIndex : m_restrictionIndex) { @@ -138,7 +130,7 @@ void RestrictionCollector::ComposeRestrictions() if (distance(rangeId.first, rangeId.second) != 1) continue; // |osmId| mentioned in restrictions was included in more than one feature. - Restriction::FeatureId const & featureId = rangeId.first->second; + uint32_t const & featureId = rangeId.first->second; // Adding feature id to restriction coresponded to the osm id. restriction.m_links[index.m_linkNumber] = featureId; } @@ -163,8 +155,7 @@ void RestrictionCollector::AddRestriction(Restriction::Type type, vector const & osmIds) +void RestrictionCollector::AddFeatureId(uint32_t featureId, vector const & osmIds) { // Note. One |featureId| could correspond to several osm ids. // but for road feature |featureId| corresponds exactly one osm id. diff --git a/generator/restriction_collector.hpp b/generator/restriction_collector.hpp index 9b1e984d1d..1d22c85e2d 100644 --- a/generator/restriction_collector.hpp +++ b/generator/restriction_collector.hpp @@ -83,7 +83,7 @@ private: /// \brief Adds feature id and corresponding vector of |osmIds| to |m_osmId2FeatureId|. /// \note One feature id (|featureId|) may correspond to several osm ids (|osmIds|). - void AddFeatureId(Restriction::FeatureId featureId, vector const & osmIds); + void AddFeatureId(uint32_t featureId, vector const & osmIds); /// \brief Adds a restriction (vector of osm id). /// \param type is a type of restriction @@ -95,7 +95,7 @@ private: RestrictionVec m_restrictions; vector> m_restrictionIndex; - unordered_multimap m_osmIds2FeatureId; + unordered_multimap m_osmIds2FeatureId; }; string ToString(Restriction::Type const & type); diff --git a/indexer/routing.cpp b/indexer/routing.cpp index 17b53c8729..72656a9f47 100644 --- a/indexer/routing.cpp +++ b/indexer/routing.cpp @@ -2,15 +2,14 @@ namespace routing { -Restriction::FeatureId const Restriction::kInvalidFeatureId = - numeric_limits::max(); +uint32_t const Restriction::kInvalidFeatureId = numeric_limits::max(); Restriction::Restriction(Type type, size_t linkNumber) : m_type(type) { m_links.resize(linkNumber, kInvalidFeatureId); } -Restriction::Restriction(Type type, vector const & links) : m_links(links), m_type(type) +Restriction::Restriction(Type type, vector const & links) : m_links(links), m_type(type) { } diff --git a/indexer/routing.hpp b/indexer/routing.hpp index ef5fa2bf88..35c4badca6 100644 --- a/indexer/routing.hpp +++ b/indexer/routing.hpp @@ -16,8 +16,7 @@ namespace routing /// \brief Restriction to modify road graph. struct Restriction { - using FeatureId = uint32_t; - static FeatureId const kInvalidFeatureId; + static uint32_t const kInvalidFeatureId; /// \brief Types of road graph restrictions. /// \note Despite the fact more that 10 restriction tags are present in osm all of them @@ -34,13 +33,13 @@ struct Restriction }; Restriction(Type type, size_t linkNumber); - Restriction(Type type, vector const & links); + Restriction(Type type, vector const & links); bool IsValid() const; bool operator==(Restriction const & restriction) const; bool operator<(Restriction const & restriction) const; - vector m_links; + vector m_links; Type m_type; }; @@ -127,7 +126,7 @@ public: return false; } uint32_t const delta = biasedDelta - 1; - m_restriction.m_links[i] = static_cast( + m_restriction.m_links[i] = static_cast( bits::ZigZagDecode(delta) + prevRestriction.m_links[i]); } return true;