[libcalamares] Refactor finding-TZ algorithm

- introduce a distance function and use that, rather than coding it
  inside the find() function. This is prep-work for unifying the
  find() calls, based on various coordinate systems.
This commit is contained in:
Adriaan de Groot 2020-08-08 13:26:39 +02:00
parent fdbc253623
commit 0fda1dcf7d

View File

@ -336,6 +336,24 @@ ZonesModel::find( const QString& region, const QString& zone ) const
return nullptr;
}
STATICTEST const TimeZoneData*
find( const ZoneVector& zones, std::function< double( const TimeZoneData* ) > distance )
{
double smallestDistance = 1000000.0;
const TimeZoneData* closest = nullptr;
for ( const auto* zone : zones )
{
double thisDistance = distance( zone );
if ( thisDistance < smallestDistance )
{
closest = zone;
smallestDistance = thisDistance;
}
}
return closest;
}
const TimeZoneData*
ZonesModel::find( double latitude, double longitude ) const
{
@ -344,12 +362,7 @@ ZonesModel::find( double latitude, double longitude ) const
* either N/S or E/W equal to any other; this obviously
* falls apart at the poles.
*/
double largestDifference = 720.0;
const TimeZoneData* closest = nullptr;
for ( const auto* zone : m_private->m_zones )
{
auto distance = [&]( const TimeZoneData* zone ) -> double {
// Latitude doesn't wrap around: there is nothing north of 90
double latitudeDifference = abs( zone->latitude() - latitude );
@ -368,13 +381,10 @@ ZonesModel::find( double latitude, double longitude ) const
longitudeDifference = abs( westerly - easterly );
}
if ( latitudeDifference + longitudeDifference < largestDifference )
{
largestDifference = latitudeDifference + longitudeDifference;
closest = zone;
}
}
return closest;
return latitudeDifference + longitudeDifference;
};
return CalamaresUtils::Locale::find( m_private->m_zones, distance );
}
QObject*