From f62a91e8aa8241b856423dc2ad2b52104fe86a8d Mon Sep 17 00:00:00 2001 From: ExMix Date: Mon, 13 Jan 2014 15:06:21 +0300 Subject: [PATCH] [drape] replace ProcessMWM method on functor. Preparing for read mwm index separately from the features --- indexer/feature_covering.cpp | 6 +- indexer/feature_covering.hpp | 15 +-- indexer/index.hpp | 174 +++++++++++++++++++++-------------- map/feature_vec_model.hpp | 7 ++ search/search_query.cpp | 2 +- 5 files changed, 123 insertions(+), 81 deletions(-) diff --git a/indexer/feature_covering.cpp b/indexer/feature_covering.cpp index a75a30840b..dbdba94730 100644 --- a/indexer/feature_covering.cpp +++ b/indexer/feature_covering.cpp @@ -231,11 +231,11 @@ IntervalsT const & CoveringGetter::Get(int scale) { switch (m_mode) { - case 0: + case ViewportWithLowLevels: CoverViewportAndAppendLowerLevels(m_rect, cellDepth, m_res[ind]); break; - case 1: + case LowLevelsOnly: { RectId id = GetRectIdAsIs(m_rect); while (id.Level() >= cellDepth) @@ -244,7 +244,7 @@ IntervalsT const & CoveringGetter::Get(int scale) break; } - case 2: + case FullCover: m_res[ind].push_back(IntervalsT::value_type(0, static_cast((1ULL << 63) - 1))); break; } diff --git a/indexer/feature_covering.hpp b/indexer/feature_covering.hpp index 593ecbc137..e72dc3e08a 100644 --- a/indexer/feature_covering.hpp +++ b/indexer/feature_covering.hpp @@ -33,19 +33,22 @@ namespace covering // Calculate cell coding depth according to max visual scale for mwm. int GetCodingDepth(int scale); + enum CoveringMode + { + ViewportWithLowLevels = 0, + LowLevelsOnly, + FullCover + }; + class CoveringGetter { IntervalsT m_res[2]; m2::RectD const & m_rect; - int m_mode; + CoveringMode m_mode; public: - /// @param[in] mode\n - /// - 0 - cover viewport with low lovels;\n - /// - 1 - cover append low levels only;\n - /// - 2 - make full cover\n - CoveringGetter(m2::RectD const & r, int mode) : m_rect(r), m_mode(mode) {} + CoveringGetter(m2::RectD const & r, CoveringMode mode) : m_rect(r), m_mode(mode) {} IntervalsT const & Get(int scale); }; diff --git a/indexer/index.hpp b/indexer/index.hpp index 6c43b8ac6c..ee677a7707 100644 --- a/indexer/index.hpp +++ b/indexer/index.hpp @@ -1,17 +1,17 @@ #pragma once #include "cell_id.hpp" #include "data_factory.hpp" +#include "mwm_set.hpp" #include "feature_covering.hpp" #include "features_vector.hpp" #include "scale_index.hpp" -#include "mwm_set.hpp" #include "../coding/file_container.hpp" #include "../../defines.hpp" -#include "../std/unordered_set.hpp" #include "../std/vector.hpp" +#include "../std/unordered_set.hpp" class MwmValue : public MwmSet::MwmValueBase @@ -61,22 +61,106 @@ public: bool DeleteMap(string const & fileName); bool UpdateMap(string const & fileName, m2::RectD & rect); +private: + + template + class ReadFeatureFunctor + { + FeaturesVector const & m_V; + F & m_F; + mutable unordered_set m_offsets; + MwmId m_mwmID; + + public: + ReadFeatureFunctor(FeaturesVector const & v, F & f, MwmId mwmID) + : m_V(v), m_F(f), m_mwmID(mwmID) + { + } + + void operator() (uint32_t offset) const + { + if (m_offsets.insert(offset).second) + { + FeatureType feature; + + m_V.Get(offset, feature); + feature.SetID(FeatureID(m_mwmID, offset)); + + m_F(feature); + } + } + }; + + /// old style mwm reading + template + class ReadMWMFunctor + { + public: + ReadMWMFunctor(F & f) + : m_f(f) + { + } + + void operator() (MwmLock const & lock, covering::CoveringGetter & cov, uint32_t scale) const + { + MwmValue * pValue = lock.GetValue(); + if (pValue) + { + feature::DataHeader const & header = pValue->GetHeader(); + + // Prepare needed covering. + int const lastScale = header.GetLastScale(); + + // In case of WorldCoasts we should pass correct scale in ForEachInIntervalAndScale. + if (scale > lastScale) scale = lastScale; + + // Use last coding scale for covering (see index_builder.cpp). + covering::IntervalsT const & interval = cov.Get(lastScale); + + // prepare features reading + FeaturesVector fv(pValue->m_cont, header); + ScaleIndex index(pValue->m_cont.GetReader(INDEX_FILE_TAG), + pValue->m_factory); + + // iterate through intervals + ReadFeatureFunctor f1(fv, m_f, lock.GetID()); + for (size_t i = 0; i < interval.size(); ++i) + index.ForEachInIntervalAndScale(f1, interval[i].first, interval[i].second, scale); + } + } + + private: + F & m_f; + }; + +public: + template void ForEachInRect(F & f, m2::RectD const & rect, uint32_t scale) const { - ForEachInIntervals(f, 0, rect, scale); + ReadMWMFunctor implFunctor(f); + ForEachInIntervals(implFunctor, covering::ViewportWithLowLevels, rect, scale); } template void ForEachInRect_TileDrawing(F & f, m2::RectD const & rect, uint32_t scale) const { - ForEachInIntervals(f, 1, rect, scale); + ReadMWMFunctor implFunctor(f); + ForEachInIntervals(implFunctor, covering::LowLevelsOnly, rect, scale); + } + + template + void ForEachFeatureIDInRect(F & f, m2::RectD const & rect, uint32_t scale) const + { + ///TODO + //ForEachInIntervals(ReadMWMFunctor(*this, f), rect, scale); } template void ForEachInScale(F & f, uint32_t scale) const { - ForEachInIntervals(f, 2, m2::RectD::GetInfiniteRect(), scale); + ReadMWMFunctor implFunctor(f); + ForEachInIntervals(implFunctor, covering::FullCover, m2::RectD::GetInfiniteRect(), scale); } /// Guard for loading features from particular MWM by demand. @@ -98,69 +182,8 @@ public: private: template - class ReadFeatureFunctor - { - FeaturesVector const & m_V; - F & m_F; - unordered_set & m_offsets; - MwmId m_mwmID; - - public: - ReadFeatureFunctor(FeaturesVector const & v, F & f, - unordered_set & offsets, MwmId mwmID) - : m_V(v), m_F(f), m_offsets(offsets), m_mwmID(mwmID) - { - } - - void operator() (uint32_t offset) const - { - if (m_offsets.insert(offset).second) - { - FeatureType feature; - - m_V.Get(offset, feature); - feature.SetID(FeatureID(m_mwmID, offset)); - - m_F(feature); - } - } - }; - - template - void ProcessMwm(F & f, MwmId id, covering::CoveringGetter & cov, uint32_t scale) const - { - MwmLock lock(*this, id); - MwmValue * pValue = lock.GetValue(); - if (pValue) - { - feature::DataHeader const & header = pValue->GetHeader(); - - // Prepare needed covering. - int const lastScale = header.GetLastScale(); - - // In case of WorldCoasts we should pass correct scale in ForEachInIntervalAndScale. - if (scale > lastScale) scale = lastScale; - - // Use last coding scale for covering (see index_builder.cpp). - covering::IntervalsT const & interval = cov.Get(lastScale); - - // prepare features reading - FeaturesVector fv(pValue->m_cont, header); - ScaleIndex index(pValue->m_cont.GetReader(INDEX_FILE_TAG), - pValue->m_factory); - - // iterate through intervals - unordered_set offsets; - ReadFeatureFunctor f1(fv, f, offsets, id); - for (size_t i = 0; i < interval.size(); ++i) - { - index.ForEachInIntervalAndScale(f1, interval[i].first, interval[i].second, scale); - } - } - } - - template - void ForEachInIntervals(F & f, int mode, m2::RectD const & rect, uint32_t scale) const + void ForEachInIntervals(F & f, covering::CoveringMode mode, + m2::RectD const & rect, uint32_t scale) const { vector mwm; GetMwmInfo(mwm); @@ -178,7 +201,10 @@ private: switch (mwm[id].GetType()) { case MwmInfo::COUNTRY: - ProcessMwm(f, id, cov, scale); + { + MwmLock lock(*this, id); + f(lock, cov, scale); + } break; case MwmInfo::COASTS: @@ -193,9 +219,15 @@ private: } if (worldID[0] < count) - ProcessMwm(f, worldID[0], cov, scale); + { + MwmLock lock(*this, worldID[0]); + f(lock, cov, scale); + } if (worldID[1] < count) - ProcessMwm(f, worldID[1], cov, scale); + { + MwmLock lock(*this, worldID[1]); + f(lock, cov, scale); + } } }; diff --git a/map/feature_vec_model.hpp b/map/feature_vec_model.hpp index bdb294ae87..81c5726d48 100644 --- a/map/feature_vec_model.hpp +++ b/map/feature_vec_model.hpp @@ -71,6 +71,13 @@ namespace model { m_multiIndex.ForEachInRect_TileDrawing(toDo, rect, scale); } + + template + void ForEachFeatureID(m2::RectD const & rect, ToDo & toDo, int scale) const + { + m_multiIndex.ForEachFeatureIDInRect(toDo, rect, scale); + } + //@} Index const & GetIndex() const { return m_multiIndex; } diff --git a/search/search_query.cpp b/search/search_query.cpp index e91acfd097..073c099b27 100644 --- a/search/search_query.cpp +++ b/search/search_query.cpp @@ -203,7 +203,7 @@ void Query::UpdateViewportOffsets(MWMVectorT const & mwmInfo, m2::RectD const & offsets.resize(mwmInfo.size()); int const viewScale = scales::GetScaleLevel(rect); - covering::CoveringGetter cov(rect, 0); + covering::CoveringGetter cov(rect, covering::ViewportWithLowLevels); for (MwmSet::MwmId mwmId = 0; mwmId < mwmInfo.size(); ++mwmId) {