diff --git a/storage/country_info.cpp b/storage/country_info.cpp index 7eaf7a5198..ea5fd4f577 100644 --- a/storage/country_info.cpp +++ b/storage/country_info.cpp @@ -13,7 +13,7 @@ namespace storage { CountryInfoGetter::CountryInfoGetter(ModelReaderPtr polyR, ModelReaderPtr countryR) - : m_reader(polyR) + : m_reader(polyR), m_cache(2) { ReaderSource src(m_reader.GetReader(PACKED_POLYGONS_INFO_TAG)); rw::Read(src, m_countries); @@ -34,16 +34,11 @@ namespace storage bool CountryInfoGetter::GetByPoint::operator() (size_t id) { - ReaderSource src(m_info.m_reader.GetReader(strings::to_string(id))); + vector const & rgnV = m_info.GetRegions(id); - uint32_t const count = ReadVarUint(src); - for (size_t i = 0; i < count; ++i) + for (size_t i = 0; i < rgnV.size(); ++i) { - vector points; - serial::LoadOuterPath(src, serial::CodingParams(), points); - - m2::RegionD rgn(points.begin(), points.end()); - if (rgn.Contains(m_pt)) + if (rgnV[i].Contains(m_pt)) { m_res = id; return false; @@ -53,6 +48,32 @@ namespace storage return true; } + vector const & CountryInfoGetter::GetRegions(size_t id) const + { + bool isFound = false; + vector & rgnV = m_cache.Find(id, isFound); + + if (!isFound) + { + rgnV.clear(); + + // load regions from file + ReaderSource src(m_reader.GetReader(strings::to_string(id))); + + uint32_t const count = ReadVarUint(src); + for (size_t i = 0; i < count; ++i) + { + vector points; + serial::LoadOuterPath(src, serial::CodingParams(), points); + + rgnV.push_back(m2::RegionD()); + rgnV.back().Assign(points.begin(), points.end()); + } + } + + return rgnV; + } + string CountryInfoGetter::GetRegionFile(m2::PointD const & pt) const { GetByPoint doGet(*this, pt); diff --git a/storage/country_info.hpp b/storage/country_info.hpp index 4c4a3fd41e..6923040b8e 100644 --- a/storage/country_info.hpp +++ b/storage/country_info.hpp @@ -1,8 +1,12 @@ #pragma once #include "country_polygon.hpp" +#include "../geometry/region2d.hpp" + #include "../coding/file_container.hpp" +#include "../base/cache.hpp" + namespace storage { @@ -13,6 +17,10 @@ namespace storage vector m_countries; map m_id2name; + mutable my::Cache > m_cache; + + vector const & GetRegions(size_t id) const; + template void ForEachCountry(m2::PointD const & pt, ToDo & toDo) const;