2016-01-15 02:47:19 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
#include <vector>
|
|
|
|
#include <boost/noncopyable.hpp>
|
2017-04-16 05:40:17 +00:00
|
|
|
#include <common/Types.h>
|
2016-01-15 02:47:19 +00:00
|
|
|
|
|
|
|
|
2017-04-17 11:56:55 +00:00
|
|
|
/** A class that lets you know if a region belongs to one RegionID region with another RegionID.
|
|
|
|
* Information about the hierarchy of regions is downloaded from a text file.
|
|
|
|
* Can on request update the data.
|
2016-01-15 02:47:19 +00:00
|
|
|
*/
|
|
|
|
class RegionsHierarchy : private boost::noncopyable
|
|
|
|
{
|
|
|
|
private:
|
2017-04-01 07:20:54 +00:00
|
|
|
time_t file_modification_time = 0;
|
|
|
|
|
|
|
|
using RegionID = UInt32;
|
|
|
|
using RegionDepth = UInt8;
|
|
|
|
using RegionPopulation = UInt32;
|
|
|
|
|
2017-11-16 18:47:59 +00:00
|
|
|
enum class RegionType : Int8
|
|
|
|
{
|
|
|
|
Hidden = -1,
|
|
|
|
Continent = 1,
|
|
|
|
Country = 3,
|
|
|
|
District = 4,
|
|
|
|
Area = 5,
|
|
|
|
City = 6,
|
|
|
|
};
|
|
|
|
|
2017-04-17 11:56:55 +00:00
|
|
|
/// Relationship parent; 0, if there are no parents, the usual lookup table.
|
2017-04-01 07:20:54 +00:00
|
|
|
using RegionParents = std::vector<RegionID>;
|
2017-04-17 11:56:55 +00:00
|
|
|
/// type of region
|
2017-04-01 07:20:54 +00:00
|
|
|
using RegionTypes = std::vector<RegionType>;
|
2017-04-17 11:56:55 +00:00
|
|
|
/// depth in the tree, starting from the country (country: 1, root: 0)
|
2017-04-01 07:20:54 +00:00
|
|
|
using RegionDepths = std::vector<RegionDepth>;
|
2017-04-17 11:56:55 +00:00
|
|
|
/// population of the region. If more than 2 ^ 32 - 1, then it is equated to this maximum.
|
2017-04-01 07:20:54 +00:00
|
|
|
using RegionPopulations = std::vector<RegionPopulation>;
|
|
|
|
|
2017-04-17 11:56:55 +00:00
|
|
|
/// region -> parent region
|
2017-04-01 07:20:54 +00:00
|
|
|
RegionParents parents;
|
2017-04-17 11:56:55 +00:00
|
|
|
/// region -> city, including it or 0, if there is none
|
2017-04-01 07:20:54 +00:00
|
|
|
RegionParents city;
|
2017-04-17 11:56:55 +00:00
|
|
|
/// region -> country including it or 0, if there is none
|
2017-04-01 07:20:54 +00:00
|
|
|
RegionParents country;
|
2017-04-17 11:56:55 +00:00
|
|
|
/// region -> area that includes it or 0, if not
|
2017-04-01 07:20:54 +00:00
|
|
|
RegionParents area;
|
2017-04-17 11:56:55 +00:00
|
|
|
/// region -> district, including it or 0, if there is none
|
2017-04-01 07:20:54 +00:00
|
|
|
RegionParents district;
|
2017-04-17 11:56:55 +00:00
|
|
|
/// region -> the continent (the first when climbing in the hierarchy of regions), including it or 0, if there is none
|
2017-04-01 07:20:54 +00:00
|
|
|
RegionParents continent;
|
2017-04-17 11:56:55 +00:00
|
|
|
/// region -> the continent (the latter when you climb the hierarchy of regions), including it or 0, if there is none
|
2017-04-01 07:20:54 +00:00
|
|
|
RegionParents top_continent;
|
|
|
|
|
2017-04-17 11:56:55 +00:00
|
|
|
/// region -> population or 0, if unknown.
|
2017-04-01 07:20:54 +00:00
|
|
|
RegionPopulations populations;
|
|
|
|
|
2017-04-17 11:56:55 +00:00
|
|
|
/// region - depth in the tree
|
2017-04-01 07:20:54 +00:00
|
|
|
RegionDepths depths;
|
|
|
|
|
|
|
|
/// path to file with data
|
|
|
|
std::string path;
|
2016-01-15 02:47:19 +00:00
|
|
|
|
2017-01-21 04:24:28 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
RegionsHierarchy(const std::string & path_);
|
|
|
|
|
2017-04-17 11:56:55 +00:00
|
|
|
/// Reloads, if necessary, the hierarchy of regions. Not threadsafe.
|
2017-04-01 07:20:54 +00:00
|
|
|
void reload();
|
|
|
|
|
|
|
|
|
|
|
|
bool in(RegionID lhs, RegionID rhs) const
|
|
|
|
{
|
|
|
|
if (lhs >= parents.size())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
while (lhs != 0 && lhs != rhs)
|
|
|
|
lhs = parents[lhs];
|
|
|
|
|
|
|
|
return lhs != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegionID toCity(RegionID region) const
|
|
|
|
{
|
|
|
|
if (region >= city.size())
|
|
|
|
return 0;
|
|
|
|
return city[region];
|
|
|
|
}
|
|
|
|
|
|
|
|
RegionID toCountry(RegionID region) const
|
|
|
|
{
|
|
|
|
if (region >= country.size())
|
|
|
|
return 0;
|
|
|
|
return country[region];
|
|
|
|
}
|
|
|
|
|
|
|
|
RegionID toArea(RegionID region) const
|
|
|
|
{
|
|
|
|
if (region >= area.size())
|
|
|
|
return 0;
|
|
|
|
return area[region];
|
|
|
|
}
|
|
|
|
|
|
|
|
RegionID toDistrict(RegionID region) const
|
|
|
|
{
|
|
|
|
if (region >= district.size())
|
|
|
|
return 0;
|
|
|
|
return district[region];
|
|
|
|
}
|
|
|
|
|
|
|
|
RegionID toContinent(RegionID region) const
|
|
|
|
{
|
|
|
|
if (region >= continent.size())
|
|
|
|
return 0;
|
|
|
|
return continent[region];
|
|
|
|
}
|
|
|
|
|
|
|
|
RegionID toTopContinent(RegionID region) const
|
|
|
|
{
|
|
|
|
if (region >= top_continent.size())
|
|
|
|
return 0;
|
|
|
|
return top_continent[region];
|
|
|
|
}
|
|
|
|
|
|
|
|
RegionID toParent(RegionID region) const
|
|
|
|
{
|
|
|
|
if (region >= parents.size())
|
|
|
|
return 0;
|
|
|
|
return parents[region];
|
|
|
|
}
|
|
|
|
|
|
|
|
RegionDepth getDepth(RegionID region) const
|
|
|
|
{
|
|
|
|
if (region >= depths.size())
|
|
|
|
return 0;
|
|
|
|
return depths[region];
|
|
|
|
}
|
|
|
|
|
|
|
|
RegionPopulation getPopulation(RegionID region) const
|
|
|
|
{
|
|
|
|
if (region >= populations.size())
|
|
|
|
return 0;
|
|
|
|
return populations[region];
|
|
|
|
}
|
2016-01-15 02:47:19 +00:00
|
|
|
};
|