[libcalamares] Build the TZRegion list in one pass

- read the file and create the regions on-the-fly, then sort the
   resulting list (instead of building a string list and then
   building the regions afterwards)
This commit is contained in:
Adriaan de Groot 2019-12-10 17:24:33 +06:30
parent 9f06903115
commit 9a5e614172
2 changed files with 16 additions and 13 deletions

View File

@ -120,6 +120,7 @@ TZRegionModel::fromFile( const char* fileName )
return model; return model;
} }
model->m_regions.reserve( 12 ); // There's 10 in the file right now
QStringList regions; QStringList regions;
QTextStream in( &file ); QTextStream in( &file );
@ -153,20 +154,20 @@ TZRegionModel::fromFile( const char* fileName )
if ( !regions.contains( region ) ) if ( !regions.contains( region ) )
{ {
regions.append( region ); regions.append( region );
model->m_regions.append( new TZRegion( region.toUtf8().data() ) );
} }
} }
regions.sort();
model->m_regions.reserve( regions.length() );
for ( int i = 0; i < regions.length(); ++i )
{
model->m_regions.append( TZRegion( regions[ i ].toUtf8().data() ) );
}
std::sort( model->m_regions.begin(), model->m_regions.end(), []( const TZRegion* l, const TZRegion* r ) {
return *l < *r;
} );
return model; return model;
} }
TZRegionModel::~TZRegionModel() {} TZRegionModel::~TZRegionModel()
{
qDeleteAll( m_regions );
}
int int
TZRegionModel::rowCount( const QModelIndex& parent ) const TZRegionModel::rowCount( const QModelIndex& parent ) const
@ -187,11 +188,11 @@ TZRegionModel::data( const QModelIndex& index, int role ) const
return QVariant(); return QVariant();
} }
const TZRegion& region = m_regions.at( index.row() ); const TZRegion* region = m_regions.at( index.row() );
return role == LabelRole ? region.tr() : region.key(); return role == LabelRole ? region->tr() : region->key();
} }
const TZRegion& const TZRegion*
TZRegionModel::region( int index ) const TZRegionModel::region( int index ) const
{ {
if ( ( index < 0 ) || ( index >= m_regions.count() ) ) if ( ( index < 0 ) || ( index >= m_regions.count() ) )

View File

@ -69,6 +69,8 @@ class TZRegion : public CStringPair
public: public:
using CStringPair::CStringPair; using CStringPair::CStringPair;
QString tr() const override; QString tr() const override;
bool operator<( const TZRegion& other ) const { return m_key < other.m_key; }
}; };
/// @brief A pair of strings for specific timezone names (e.g. "New_York") /// @brief A pair of strings for specific timezone names (e.g. "New_York")
@ -100,10 +102,10 @@ public:
QVariant data( const QModelIndex& index, int role ) const override; QVariant data( const QModelIndex& index, int role ) const override;
const TZRegion& region( int index ) const; const TZRegion* region( int index ) const;
private: private:
QVector< TZRegion > m_regions; QList< TZRegion* > m_regions;
}; };
} // namespace Locale } // namespace Locale