diff --git a/base/time_samples.cpp b/base/time_samples.cpp index 3a0d439e18..209385b060 100644 --- a/base/time_samples.cpp +++ b/base/time_samples.cpp @@ -4,21 +4,21 @@ namespace my { -void TimeSamples::Add(double s) +void TimeSamples::Add(double seconds) { - m_s += s; - m_s2 += s * s; + m_sum += seconds; + m_sum2 += seconds * seconds; ++m_total; } -double TimeSamples::GetMean() const { return m_total == 0 ? 0.0 : m_s / m_total; } +double TimeSamples::GetMean() const { return m_total == 0 ? 0.0 : m_sum / m_total; } -double TimeSamples::GetSd() const +double TimeSamples::GetSD() const { if (m_total < 2) return 0.0; - return std::max((m_s2 - m_s * m_s / static_cast(m_total)) / (m_total - 1), 0.0); + return std::max((m_sum2 - m_sum * m_sum / static_cast(m_total)) / (m_total - 1), 0.0); } -double TimeSamples::GetVar() const { return sqrt(GetSd()); } +double TimeSamples::GetVar() const { return sqrt(GetSD()); } } // namespace my diff --git a/base/time_samples.hpp b/base/time_samples.hpp index b37166b164..c4f1fdf072 100644 --- a/base/time_samples.hpp +++ b/base/time_samples.hpp @@ -6,21 +6,35 @@ namespace my { +// This class can be used in measurements of code blocks performance. +// It can accumulate time samples, and can calculate mean time and +// standard deviation. +// +// *NOTE* This class is NOT thread-safe. class TimeSamples final { public: - void Add(double s); + void Add(double seconds); + // Mean of the accumulated time samples. double GetMean() const; - double GetSd() const; + + // Unbiased standard deviation of the accumulated time samples. + double GetSD() const; + + // Unbiased variance of the accumulated time samples. double GetVar() const; private: - double m_s = 0.0; - double m_s2 = 0.0; + double m_sum = 0.0; + double m_sum2 = 0.0; size_t m_total = 0; }; +// This class can be used as a single scoped time sample. It +// automatically adds time sample on destruction. +// +// *NOTE* This class is NOT thread-safe. class ScopedTimeSample final { public: diff --git a/search/locality_finder.cpp b/search/locality_finder.cpp index 5774b267a4..7621b5f462 100644 --- a/search/locality_finder.cpp +++ b/search/locality_finder.cpp @@ -50,20 +50,23 @@ private: CBV m_cbv; }; -class DoLoader +class LocalitiesLoader { public: - DoLoader(MwmContext const & ctx, Filter const & filter, int8_t lang, - m4::Tree & localities, - map> & loadedIds) - : m_ctx(ctx), m_filter(filter), m_lang(lang), m_localities(localities), m_loadedIds(loadedIds) + LocalitiesLoader(MwmContext const & ctx, Filter const & filter, int8_t lang, + m4::Tree & localities, + map> & loadedIds) + : m_ctx(ctx) + , m_filter(filter) + , m_lang(lang) + , m_localities(localities) + , m_loadedIds(loadedIds[m_ctx.GetId()]) { } void operator()(uint32_t id) const { - auto const & mwmId = m_ctx.GetId(); - if (m_loadedIds[mwmId].count(id) != 0) + if (m_loadedIds.count(id) != 0) return; if (!m_filter.IsGood(id)) @@ -83,7 +86,7 @@ public: case TOWN: case VILLAGE: break; - default: // cache only cities and towns at this moment + default: return; } @@ -91,18 +94,16 @@ public: if (population == 0) return; - auto const center = ft.GetCenter(); - double const radius = ftypes::GetRadiusByPopulation(population); - m2::RectD const rect = MercatorBounds::RectByCenterXYAndSizeInMeters(center, radius); - // read item string name; if (!ft.GetName(m_lang, name) && !ft.GetName(0, name)) return; + auto const center = ft.GetCenter(); + LocalityFinder::Item item(name, center, population); - m_localities.Add(item, rect); - m_loadedIds[mwmId].insert(id); + m_localities.Add(item, m2::RectD(center, center)); + m_loadedIds.insert(id); } private: @@ -111,7 +112,7 @@ private: int8_t const m_lang; m4::Tree & m_localities; - map> & m_loadedIds; + unordered_set & m_loadedIds; }; } // namespace @@ -184,16 +185,16 @@ void LocalityFinder::LoadVicinity(m2::PointD const & pt) m_ranks = make_unique(); MwmContext ctx(move(handle)); - ctx.ForEachIndex(drect, - DoLoader(ctx, CityFilter(*m_ranks), m_lang, m_localities, m_loadedIds)); + ctx.ForEachIndex( + drect, LocalitiesLoader(ctx, CityFilter(*m_ranks), m_lang, m_localities, m_loadedIds)); break; } case feature::DataHeader::country: if (header.GetBounds().IsPointInside(pt)) { MwmContext ctx(move(handle)); - ctx.ForEachIndex(drect, DoLoader(ctx, VillageFilter(ctx, m_villagesCache), m_lang, - m_localities, m_loadedIds)); + ctx.ForEachIndex(drect, LocalitiesLoader(ctx, VillageFilter(ctx, m_villagesCache), m_lang, + m_localities, m_loadedIds)); } break; case feature::DataHeader::worldcoasts: break; diff --git a/search/search_tests/locality_finder_test.cpp b/search/search_tests/locality_finder_test.cpp index 50ca728dda..eb5847b6b5 100644 --- a/search/search_tests/locality_finder_test.cpp +++ b/search/search_tests/locality_finder_test.cpp @@ -16,12 +16,12 @@ namespace { -struct TestWithClassifier +struct TestWithClassificator { - TestWithClassifier() { classificator::Load(); } + TestWithClassificator() { classificator::Load(); } }; -class LocalityFinderTest : public TestWithClassifier +class LocalityFinderTest : public TestWithClassificator { platform::LocalCountryFile m_worldFile;