Commit b28767a0 authored by Volker Krause's avatar Volker Krause
Browse files

Put all encoding parameters into the generated code

This avoids duplicating this in the lookup function, and possibly going
out of sync.
parent d5dadf11
......@@ -172,7 +172,8 @@ namespace KItinerary {
namespace KnowledgeDb {
""")
out.write('static constexpr uint8_t timezone_index_zDepth = ' + str(zDepth) + ';\n\n')
out.write('static constexpr const TimezoneZIndexParams timezone_index_params = {{ {xStart}, {xRange}, {yStart}, {yRange}, {zDepth} }};\n\n'
.format(xStart = xStart, xRange = xRange, yStart = yStart, yRange = yRange, zDepth = zDepth));
out.write('static constexpr TimezoneZIndexEntry timezone_index[] = {\n')
prevFeature = ""
......
......@@ -56,14 +56,14 @@ KnowledgeDb::CountryId KnowledgeDb::countryForTimezone(KnowledgeDb::Tz tz)
KnowledgeDb::Tz KnowledgeDb::timezoneForCoordinate(float lat, float lon, bool *ambiguous)
{
// see arctic latitude filter in the generator script, we only cover 65°S to 80°N
if (lat < -65.0f || lat > 80.0f) {
if (lat < timezone_index_params.yStart || lat > timezone_index_params.yEnd()) {
return Tz::Undefined;
}
const uint32_t x = ((lon + 180.0) / 360.0) * (1 << timezone_index_zDepth);
const uint32_t y = ((lat + 65.0) / 145.0) * (1 << timezone_index_zDepth);
const uint32_t x = ((lon - timezone_index_params.xStart) / timezone_index_params.xRange) * (1 << timezone_index_params.zDepth);
const uint32_t y = ((lat - timezone_index_params.yStart) / timezone_index_params.yRange) * (1 << timezone_index_params.zDepth);
uint32_t z = 0;
for (int i = timezone_index_zDepth - 1; i >= 0; --i) {
for (int i = timezone_index_params.zDepth - 1; i >= 0; --i) {
z <<= 1;
z += (y & (1 << i)) ? 1 : 0;
z <<= 1;
......
......@@ -37,6 +37,17 @@ inline constexpr bool operator<(const CountryTimezoneMap lhs, const CountryId rh
return lhs.country < rhs;
}
// encoding parameters for the timezone index
struct TimezoneZIndexParams {
float xStart;
float xRange;
float yStart;
float yRange;
uint8_t zDepth;
constexpr inline float yEnd() const { return xStart + xRange; }
};
// geo coordinate to timezone index entry
struct TimezoneZIndexEntry {
uint32_t z: 22;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment