Skip to content

Commit

Permalink
Refactor countryId naming
Browse files Browse the repository at this point in the history
  • Loading branch information
syershov committed Apr 20, 2016
1 parent b896b4e commit 34791b0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 41 deletions.
4 changes: 2 additions & 2 deletions generator/borders_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ void UnpackBorders(string const & baseDir, string const & targetDir)

for (size_t id = 0; id < countries.size(); id++)
{
ofstream poly(my::JoinFoldersToPath(targetDir, countries[id].m_name + ".poly"));
poly << countries[id].m_name << endl;
ofstream poly(my::JoinFoldersToPath(targetDir, countries[id].m_countryId + ".poly"));
poly << countries[id].m_countryId << endl;
src = reader.GetReader(strings::to_string(id));
uint32_t const count = ReadVarUint<uint32_t>(src);
for (size_t i = 0; i < count; ++i)
Expand Down
2 changes: 1 addition & 1 deletion search/v2/geocoder.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ class Geocoder : public my::Cancellable
{
Region(Locality const & l, RegionType type) : Locality(l), m_center(0, 0), m_type(type) {}

storage::CountryInfoGetter::IdSet m_ids;
storage::CountryInfoGetter::TRegionIdSet m_ids;
string m_enName;
m2::PointD m_center;
RegionType m_type;
Expand Down
6 changes: 3 additions & 3 deletions storage/country_decl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ namespace storage
struct CountryDef
{
/// File name without extension (equal to english name - used in search for region).
TCountryId m_name;
TCountryId m_countryId;
m2::RectD m_rect;

CountryDef() {}
CountryDef(string const & name, m2::RectD const & rect)
: m_name(name), m_rect(rect)
CountryDef(TCountryId const & countryId, m2::RectD const & rect)
: m_countryId(countryId), m_rect(rect)
{
}
};
Expand Down
48 changes: 27 additions & 21 deletions storage/country_info_getter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ class DoCalcUSA

void operator()(CountryDef const & c)
{
if (c.m_name == "USA_Alaska")
if (c.m_countryId == "USA_Alaska")
m_rects[1] = c.m_rect;
else if (c.m_name == "USA_Hawaii")
else if (c.m_countryId == "USA_Hawaii")
m_rects[2] = c.m_rect;
else
m_rects[0].Add(c.m_rect);
Expand All @@ -55,8 +55,8 @@ class DoCalcUSA
// CountryInfoGetter -------------------------------------------------------------------------------
TCountryId CountryInfoGetter::GetRegionCountryId(m2::PointD const & pt) const
{
IdType const id = FindFirstCountry(pt);
return id != kInvalidId ? m_countries[id].m_name : kInvalidCountryId;
TRegionId const id = FindFirstCountry(pt);
return id != kInvalidId ? m_countries[id].m_countryId : kInvalidCountryId;
}

void CountryInfoGetter::GetRegionsCountryId(m2::PointD const & pt, TCountriesVec & closestCoutryIds)
Expand All @@ -70,26 +70,26 @@ void CountryInfoGetter::GetRegionsCountryId(m2::PointD const & pt, TCountriesVec
for (size_t id = 0; id < m_countries.size(); ++id)
{
if (m_countries[id].m_rect.IsIntersect(lookupRect) && IsCloseEnough(id, pt, kLookupRadiusM))
closestCoutryIds.emplace_back(m_countries[id].m_name);
closestCoutryIds.emplace_back(m_countries[id].m_countryId);
}
}

void CountryInfoGetter::GetRegionInfo(m2::PointD const & pt, CountryInfo & info) const
{
IdType const id = FindFirstCountry(pt);
TRegionId const id = FindFirstCountry(pt);
if (id != kInvalidId)
GetRegionInfo(m_countries[id].m_name, info);
GetRegionInfo(m_countries[id].m_countryId, info);
}

void CountryInfoGetter::GetRegionInfo(string const & id, CountryInfo & info) const
void CountryInfoGetter::GetRegionInfo(TCountryId const & countryId, CountryInfo & info) const
{
auto const it = m_id2info.find(id);
auto const it = m_id2info.find(countryId);
if (it == m_id2info.end())
return;

info = it->second;
if (info.m_name.empty())
info.m_name = id;
info.m_name = countryId;

CountryInfo::FileName2FullName(info.m_name);
}
Expand Down Expand Up @@ -117,7 +117,7 @@ m2::RectD CountryInfoGetter::GetLimitRectForLeaf(TCountryId const & leafCountryI
return m_countries[it->second].m_rect;
}

void CountryInfoGetter::GetMatchedRegions(string const & affiliation, IdSet & regions) const
void CountryInfoGetter::GetMatchedRegions(string const & affiliation, TRegionIdSet & regions) const
{
CHECK(m_affiliations, ());
auto it = m_affiliations->find(affiliation);
Expand All @@ -126,12 +126,12 @@ void CountryInfoGetter::GetMatchedRegions(string const & affiliation, IdSet & re

for (size_t i = 0; i < m_countries.size(); ++i)
{
if (binary_search(it->second.begin(), it->second.end(), m_countries[i].m_name))
if (binary_search(it->second.begin(), it->second.end(), m_countries[i].m_countryId))
regions.push_back(i);
}
}

bool CountryInfoGetter::IsBelongToRegions(m2::PointD const & pt, IdSet const & regions) const
bool CountryInfoGetter::IsBelongToRegions(m2::PointD const & pt, TRegionIdSet const & regions) const
{
for (auto const & id : regions)
{
Expand All @@ -141,22 +141,28 @@ bool CountryInfoGetter::IsBelongToRegions(m2::PointD const & pt, IdSet const & r
return false;
}

bool CountryInfoGetter::IsBelongToRegions(string const & fileName, IdSet const & regions) const
bool CountryInfoGetter::IsBelongToRegions(TCountryId const & countryId, TRegionIdSet const & regions) const
{
for (auto const & id : regions)
{
if (m_countries[id].m_name == fileName)
if (m_countries[id].m_countryId == countryId)
return true;
}
return false;
}

void CountryInfoGetter::RegionIdsToCountryIds(TRegionIdSet const & regions, TCountriesVec & countries) const
{
for (auto const & id : regions)
countries.push_back(m_countries[id].m_countryId);
}

void CountryInfoGetter::InitAffiliationsInfo(TMappingAffiliations const * affiliations)
{
m_affiliations = affiliations;
}

CountryInfoGetter::IdType CountryInfoGetter::FindFirstCountry(m2::PointD const & pt) const
CountryInfoGetter::TRegionId CountryInfoGetter::FindFirstCountry(m2::PointD const & pt) const
{
for (size_t id = 0; id < m_countries.size(); ++id)
{
Expand All @@ -176,7 +182,7 @@ void CountryInfoGetter::ForEachCountry(string const & prefix, ToDo && toDo) cons
{
for (auto const & country : m_countries)
{
if (strings::StartsWith(country.m_name, prefix.c_str()))
if (strings::StartsWith(country.m_countryId, prefix.c_str()))
toDo(country);
}
}
Expand Down Expand Up @@ -234,7 +240,7 @@ CountryInfoReader::CountryInfoReader(ModelReaderPtr polyR, ModelReaderPtr countr
size_t const countrySz = m_countries.size();
m_countryIndex.reserve(countrySz);
for (size_t i = 0; i < countrySz; ++i)
m_countryIndex[m_countries[i].m_name] = i;
m_countryIndex[m_countries[i].m_countryId] = i;

string buffer;
countryR.ReadAsString(buffer);
Expand Down Expand Up @@ -318,16 +324,16 @@ CountryInfoGetterForTesting::CountryInfoGetterForTesting(vector<CountryDef> cons
void CountryInfoGetterForTesting::AddCountry(CountryDef const & country)
{
m_countries.push_back(country);
string const & name = country.m_name;
string const & name = country.m_countryId;
m_id2info[name].m_name = name;
}

void CountryInfoGetterForTesting::GetMatchedRegions(string const & affiliation,
IdSet & regions) const
TRegionIdSet & regions) const
{
for (size_t i = 0; i < m_countries.size(); ++i)
{
if (m_countries[i].m_name == affiliation)
if (m_countries[i].m_countryId == affiliation)
regions.push_back(i);
}
}
Expand Down
26 changes: 14 additions & 12 deletions storage/country_info_getter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class CountryInfoGetter
{
public:
// Identifier of a region (index in m_countries array).
using IdType = size_t;
using IdSet = vector<IdType>;
using TRegionId = size_t;
using TRegionIdSet = vector<TRegionId>;

CountryInfoGetter(bool isSingleMwm) : m_isSingleMwm(isSingleMwm) {}
virtual ~CountryInfoGetter() = default;
Expand All @@ -45,8 +45,8 @@ class CountryInfoGetter
// Returns info for a region |pt| belongs to.
void GetRegionInfo(m2::PointD const & pt, CountryInfo & info) const;

// Returns info for a country by file name without an extension.
void GetRegionInfo(string const & id, CountryInfo & info) const;
// Returns info for a country by id.
void GetRegionInfo(TCountryId const & countryId, CountryInfo & info) const;

// Return limit rects of USA:
// 0 - continental part
Expand All @@ -63,15 +63,17 @@ class CountryInfoGetter
m2::RectD GetLimitRectForLeaf(TCountryId const & leafCountryId) const;

// Returns identifiers for all regions matching to correspondent |affiliation|.
virtual void GetMatchedRegions(string const & affiliation, IdSet & regions) const;
virtual void GetMatchedRegions(string const & affiliation, TRegionIdSet & regions) const;

// Returns true when |pt| belongs to at least one of the specified
// |regions|.
bool IsBelongToRegions(m2::PointD const & pt, IdSet const & regions) const;
bool IsBelongToRegions(m2::PointD const & pt, TRegionIdSet const & regions) const;

// Returns true if there're at least one region with name equals to
// |fileName|.
bool IsBelongToRegions(string const & fileName, IdSet const & regions) const;
// Returns true if there're at least one region with id equals to
// |countryId|.
bool IsBelongToRegions(TCountryId const & countryId, TRegionIdSet const & regions) const;

void RegionIdsToCountryIds(TRegionIdSet const & regions, TCountriesVec & countries) const;

// Clears regions cache.
inline void ClearCaches() const { ClearCachesImpl(); }
Expand All @@ -82,7 +84,7 @@ class CountryInfoGetter
CountryInfoGetter() = default;

// Returns identifier of a first country containing |pt|.
IdType FindFirstCountry(m2::PointD const & pt) const;
TRegionId FindFirstCountry(m2::PointD const & pt) const;

// Invokes |toDo| on each country whose name starts with |prefix|.
template <typename ToDo>
Expand All @@ -102,7 +104,7 @@ class CountryInfoGetter
// List of all known countries.
vector<CountryDef> m_countries;
// Maps all leaf country id (file names) to their indices in m_countries.
unordered_map<TCountryId, IdType> m_countryIndex;
unordered_map<TCountryId, TRegionId> m_countryIndex;

TMappingAffiliations const * m_affiliations = nullptr;

Expand Down Expand Up @@ -164,7 +166,7 @@ class CountryInfoGetterForTesting : public CountryInfoGetter
void AddCountry(CountryDef const & country);

// CountryInfoGetter overrides:
void GetMatchedRegions(string const & affiliation, IdSet & regions) const override;
void GetMatchedRegions(string const & affiliation, TRegionIdSet & regions) const override;

protected:
// CountryInfoGetter overrides:
Expand Down
4 changes: 2 additions & 2 deletions storage/country_polygon.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace storage
{
template <class TSource> void Read(TSource & src, CountryDef & p)
{
rw::Read(src, p.m_name);
rw::Read(src, p.m_countryId);

pair<int64_t, int64_t> r;
r.first = ReadVarInt<int64_t>(src);
Expand All @@ -22,7 +22,7 @@ namespace storage

template <class TSink> void Write(TSink & sink, CountryDef const & p)
{
rw::Write(sink, p.m_name);
rw::Write(sink, p.m_countryId);

pair<int64_t, int64_t> const r = RectToInt64(p.m_rect, serial::CodingParams().GetCoordBits());
WriteVarInt(sink, r.first);
Expand Down

0 comments on commit 34791b0

Please sign in to comment.