From c8d703ee2eac872c24f9d78440904a0788799c59 Mon Sep 17 00:00:00 2001 From: Vladimir Byko-Ianko Date: Wed, 9 Nov 2016 14:42:10 +0300 Subject: [PATCH] Reorganization restriction serialization and deserialization. --- generator/restriction_collector.cpp | 20 ----- generator/restriction_collector.hpp | 3 - generator/restriction_generator.cpp | 31 +------- indexer/restriction_loader.cpp | 26 +------ indexer/routing.cpp | 35 +++++++-- indexer/routing.hpp | 112 ++++++++++++++++++++-------- 6 files changed, 113 insertions(+), 114 deletions(-) diff --git a/generator/restriction_collector.cpp b/generator/restriction_collector.cpp index b454bc4745..6128b4838f 100644 --- a/generator/restriction_collector.cpp +++ b/generator/restriction_collector.cpp @@ -159,16 +159,6 @@ void RestrictionCollector::AddFeatureId(uint32_t featureId, vector con m_osmIds2FeatureId.insert(make_pair(osmId, featureId)); } -string ToString(Restriction::Type const & type) -{ - switch (type) - { - case Restriction::Type::No: return kNo; - case Restriction::Type::Only: return kOnly; - } - return "Unknown"; -} - bool FromString(string str, Restriction::Type & type) { if (str == kNo) @@ -185,8 +175,6 @@ bool FromString(string str, Restriction::Type & type) return false; } -string DebugPrint(Restriction::Type const & type) { return ToString(type); } - string DebugPrint(RestrictionCollector::LinkIndex const & index) { ostringstream out; @@ -194,12 +182,4 @@ string DebugPrint(RestrictionCollector::LinkIndex const & index) << " m_linkNumber:" << index.m_linkNumber << " "; return out.str(); } - -string DebugPrint(Restriction const & restriction) -{ - ostringstream out; - out << "m_featureIds:[" << ::DebugPrint(restriction.m_featureIds) - << "] m_type:" << DebugPrint(restriction.m_type) << " "; - return out.str(); -} } // namespace routing diff --git a/generator/restriction_collector.hpp b/generator/restriction_collector.hpp index a69c80830c..fcf401e3ed 100644 --- a/generator/restriction_collector.hpp +++ b/generator/restriction_collector.hpp @@ -100,9 +100,6 @@ private: unordered_multimap m_osmIds2FeatureId; }; -string ToString(Restriction::Type const & type); bool FromString(string str, Restriction::Type & type); -string DebugPrint(Restriction::Type const & type); string DebugPrint(RestrictionCollector::LinkIndex const & index); -string DebugPrint(Restriction const & restriction); } // namespace routing diff --git a/generator/restriction_generator.cpp b/generator/restriction_generator.cpp index b9e98db141..69cd8a5d94 100644 --- a/generator/restriction_generator.cpp +++ b/generator/restriction_generator.cpp @@ -12,33 +12,6 @@ #include "std/algorithm.hpp" using namespace feature; -using namespace routing; - -namespace -{ -/// \brief Serializes a range of restrictions form |begin| to |end| to |sink|. -/// \param begin is an iterator to the first item to serialize. -/// \param end is an iterator to the element after the last element to serialize. -/// \note All restrictions should have the same type. -void SerializeRestrictions(RestrictionVec::const_iterator begin, RestrictionVec::const_iterator end, - FileWriter & sink) -{ - if (begin == end) - return; - - Restriction::Type const type = begin->m_type; - - Restriction prevRestriction(type, 0 /* linkNumber */); - prevRestriction.m_featureIds.resize(feature::RestrictionSerializer::kSupportedLinkNumber, 0); - for (auto it = begin; it != end; ++it) - { - CHECK_EQUAL(type, it->m_type, ()); - RestrictionSerializer serializer(*it); - serializer.Serialize(prevRestriction, sink); - prevRestriction = serializer.GetRestriction(); - } -} -} // namespace namespace routing { @@ -69,8 +42,8 @@ bool BuildRoadRestrictions(string const & mwmPath, string const & restrictionPat FilesContainerW cont(mwmPath, FileWriter::OP_WRITE_EXISTING); FileWriter w = cont.GetWriter(ROUTING_FILE_TAG); header.Serialize(w); - SerializeRestrictions(restrictions.cbegin(), firstOnlyIt, w); - SerializeRestrictions(firstOnlyIt, restrictions.end(), w); + + RestrictionSerializer::Serialize(restrictions.cbegin(), firstOnlyIt, restrictions.cend(), w); return true; } diff --git a/indexer/restriction_loader.cpp b/indexer/restriction_loader.cpp index 9b110e7eff..314189d43b 100644 --- a/indexer/restriction_loader.cpp +++ b/indexer/restriction_loader.cpp @@ -1,27 +1,8 @@ #include "indexer/restriction_loader.hpp" - -#include "coding/reader.hpp" +#include "indexer/routing.hpp" using namespace routing; -namespace -{ -template -void DeserializeRestrictions(Restriction::Type type, uint32_t count, TSource & src, - RestrictionVec & restrictions) -{ - feature::RestrictionSerializer serializer(Restriction(type, 0 /* linkNumber */)); - Restriction prevRestriction(type, 0 /* linkNumber */); - prevRestriction.m_featureIds.resize(feature::RestrictionSerializer::kSupportedLinkNumber, 0); - for (size_t i = 0; i < count; ++i) - { - serializer.Deserialize(prevRestriction, src); - restrictions.push_back(serializer.GetRestriction()); - prevRestriction = serializer.GetRestriction(); - } -} -} // namespace - namespace feature { RestrictionLoader::RestrictionLoader(MwmValue const & mwmValue) @@ -36,10 +17,7 @@ RestrictionLoader::RestrictionLoader(MwmValue const & mwmValue) ReaderSource src(*m_reader); m_header.Deserialize(src); - DeserializeRestrictions(Restriction::Type::No, m_header.m_noRestrictionCount, src, - m_restrictions); - DeserializeRestrictions(Restriction::Type::Only, m_header.m_onlyRestrictionCount, src, - m_restrictions); + RestrictionSerializer::Deserialize(m_header, m_restrictions, src); } catch (Reader::OpenException const & e) { diff --git a/indexer/routing.cpp b/indexer/routing.cpp index 3ea711aefe..985c5db8f7 100644 --- a/indexer/routing.cpp +++ b/indexer/routing.cpp @@ -1,5 +1,11 @@ #include "indexer/routing.hpp" +namespace +{ +char const kNo[] = "No"; +char const kOnly[] = "Only"; +} // namespace + namespace routing { // static @@ -22,12 +28,31 @@ bool Restriction::operator<(Restriction const & restriction) const return m_type < restriction.m_type; return m_featureIds < restriction.m_featureIds; } + +string ToString(Restriction::Type const & type) +{ + switch (type) + { + case Restriction::Type::No: return kNo; + case Restriction::Type::Only: return kOnly; + } + return "Unknown"; +} + +string DebugPrint(Restriction::Type const & type) { return ToString(type); } + +string DebugPrint(Restriction const & restriction) +{ + ostringstream out; + out << "m_featureIds:[" << ::DebugPrint(restriction.m_featureIds) + << "] m_type:" << DebugPrint(restriction.m_type) << " "; + return out.str(); +} } // namespace routing namespace feature { -// For the time being only one kind of restrictions is supported. It's line-point-line -// restrictions in osm ids term. Such restrictions correspond to two feature ids -// restrictions in feature id terms. Because of it supported number of links is two. -size_t const RestrictionSerializer::kSupportedLinkNumber = 2; -} +// static +uint32_t const RestrictionSerializer::kDefaultFeatureId = 0; +} // feature + diff --git a/indexer/routing.hpp b/indexer/routing.hpp index 6256d17c57..0a7c1ef6e4 100644 --- a/indexer/routing.hpp +++ b/indexer/routing.hpp @@ -2,6 +2,7 @@ #include "coding/bit_streams.hpp" #include "coding/elias_coder.hpp" +#include "coding/file_writer.hpp" #include "coding/reader.hpp" #include "coding/varint.hpp" #include "coding/write_to_sink.hpp" @@ -9,6 +10,7 @@ #include "base/assert.hpp" #include "base/bits.hpp" +#include "std/string.hpp" #include "std/vector.hpp" namespace routing @@ -45,10 +47,19 @@ struct Restriction }; using RestrictionVec = vector; + +string ToString(Restriction::Type const & type); +string DebugPrint(Restriction::Type const & type); +string DebugPrint(Restriction const & restriction); } // namespace routing namespace feature { +// For the time being only one kind of restrictions is supported. It's line-point-line +// restrictions in osm ids term. Such restrictions correspond to two feature ids +// restrictions in feature id terms. Because of it supported number of links is two. +static size_t const kSupportedLinkNumber = 2; + struct RoutingHeader { RoutingHeader() { Reset(); } @@ -90,55 +101,90 @@ static_assert(sizeof(RoutingHeader) == 12, "Wrong header size of routing section class RestrictionSerializer { public: - static size_t const kSupportedLinkNumber; - - RestrictionSerializer() : m_restriction(routing::Restriction::Type::No, 0 /* linkNumber */) {} - - explicit RestrictionSerializer(routing::Restriction const & restriction) - : m_restriction(restriction) + template + static void Serialize(routing::RestrictionVec::const_iterator begin, + routing::RestrictionVec::const_iterator firstOnlyIt, + routing::RestrictionVec::const_iterator end, Sink & sink) { + SerializeSingleType(begin, firstOnlyIt, sink); + SerializeSingleType(firstOnlyIt, end, sink); } - template - void Serialize(routing::Restriction const & prevRestriction, Sink & sink) const + template + static void Deserialize(RoutingHeader const & header, routing::RestrictionVec & restrictions, + Source & src) { - CHECK(m_restriction.IsValid(), ()); - CHECK_EQUAL(m_restriction.m_featureIds.size(), kSupportedLinkNumber, - ("Only", kSupportedLinkNumber, "links restriction are supported.")); + DeserializeSingleType(routing::Restriction::Type::No, header.m_noRestrictionCount, + restrictions, src); + DeserializeSingleType(routing::Restriction::Type::Only, header.m_onlyRestrictionCount, + restrictions, src); + } - BitWriter bits(sink); - for (size_t i = 0; i < kSupportedLinkNumber; ++i) +private: + static uint32_t const kDefaultFeatureId; + + /// \brief Serializes a range of restrictions form |begin| to |end| to |sink|. + /// \param begin is an iterator to the first item to serialize. + /// \param end is an iterator to the element after the last element to serialize. + /// \note All restrictions should have the same type. + template + static void SerializeSingleType(routing::RestrictionVec::const_iterator begin, + routing::RestrictionVec::const_iterator end, Sink & sink) + { + if (begin == end) + return; + + routing::Restriction::Type const type = begin->m_type; + + routing::Restriction prevRestriction(type, 0 /* linkNumber */); + prevRestriction.m_featureIds.resize(kSupportedLinkNumber, kDefaultFeatureId); + for (auto it = begin; it != end; ++it) { - uint32_t const delta = bits::ZigZagEncode(static_cast(m_restriction.m_featureIds[i]) - - static_cast(prevRestriction.m_featureIds[i])); - coding::DeltaCoder::Encode(bits, delta + 1 /* making it greater than zero */); + CHECK_EQUAL(type, it->m_type, ()); + + routing::Restriction const & restriction = *it; + CHECK(restriction.IsValid(), ()); + CHECK_EQUAL(restriction.m_featureIds.size(), kSupportedLinkNumber, + ("Only", kSupportedLinkNumber, "links restriction are supported.")); + + BitWriter bits(sink); + for (size_t i = 0; i < kSupportedLinkNumber; ++i) + { + uint32_t const delta = bits::ZigZagEncode(static_cast(restriction.m_featureIds[i]) - + static_cast(prevRestriction.m_featureIds[i])); + coding::DeltaCoder::Encode(bits, delta + 1 /* making it greater than zero */); + } + prevRestriction = restriction; } } template - bool Deserialize(routing::Restriction const & prevRestriction, Source & src) + static bool DeserializeSingleType(routing::Restriction::Type type, uint32_t count, + routing::RestrictionVec & restrictions, Source & src) { - BitReader bits(src); - m_restriction.m_featureIds.resize(kSupportedLinkNumber); - for (size_t i = 0; i < kSupportedLinkNumber; ++i) + routing::Restriction prevRestriction(type, 0 /* linkNumber */); + prevRestriction.m_featureIds.resize(kSupportedLinkNumber, kDefaultFeatureId); + for (size_t i = 0; i < count; ++i) { - uint32_t const biasedDelta = coding::DeltaCoder::Decode(bits); - if (biasedDelta == 0) + BitReader bits(src); + routing::Restriction restriction(type, kSupportedLinkNumber); + for (size_t i = 0; i < kSupportedLinkNumber; ++i) { - LOG(LERROR, ("Decoded link restriction feature id delta is zero.")); - m_restriction.m_featureIds.clear(); - return false; + uint32_t const biasedDelta = coding::DeltaCoder::Decode(bits); + if (biasedDelta == 0) + { + LOG(LERROR, ("Decoded link restriction feature id delta is zero.")); + return false; + } + uint32_t const delta = biasedDelta - 1; + restriction.m_featureIds[i] = static_cast( + bits::ZigZagDecode(delta) + prevRestriction.m_featureIds[i]); } - uint32_t const delta = biasedDelta - 1; - m_restriction.m_featureIds[i] = static_cast( - bits::ZigZagDecode(delta) + prevRestriction.m_featureIds[i]); + + restrictions.push_back(restriction); + prevRestriction = restriction; } return true; } - - routing::Restriction const & GetRestriction() const { return m_restriction; } - -private: - routing::Restriction m_restriction; }; } // namespace feature