Review fixes.

This commit is contained in:
Yuri Gorshenin 2016-09-27 19:20:43 +03:00
parent 5951f4c0f8
commit c9ef1fa366
4 changed files with 48 additions and 33 deletions

View file

@ -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<double>(m_total)) / (m_total - 1), 0.0);
return std::max((m_sum2 - m_sum * m_sum / static_cast<double>(m_total)) / (m_total - 1), 0.0);
}
double TimeSamples::GetVar() const { return sqrt(GetSd()); }
double TimeSamples::GetVar() const { return sqrt(GetSD()); }
} // namespace my

View file

@ -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:

View file

@ -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<LocalityFinder::Item> & localities,
map<MwmSet::MwmId, unordered_set<uint32_t>> & 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<LocalityFinder::Item> & localities,
map<MwmSet::MwmId, unordered_set<uint32_t>> & 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<LocalityFinder::Item> & m_localities;
map<MwmSet::MwmId, unordered_set<uint32_t>> & m_loadedIds;
unordered_set<uint32_t> & m_loadedIds;
};
} // namespace
@ -184,16 +185,16 @@ void LocalityFinder::LoadVicinity(m2::PointD const & pt)
m_ranks = make_unique<DummyRankTable>();
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;

View file

@ -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;