From 9a5e614172ccdbe213ab3a13bb9f5d9375289bbd Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Tue, 10 Dec 2019 17:24:33 +0630 Subject: [PATCH] [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) --- src/libcalamares/locale/TimeZone.cpp | 23 ++++++++++++----------- src/libcalamares/locale/TimeZone.h | 6 ++++-- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/libcalamares/locale/TimeZone.cpp b/src/libcalamares/locale/TimeZone.cpp index 86ecd4097..c03afd833 100644 --- a/src/libcalamares/locale/TimeZone.cpp +++ b/src/libcalamares/locale/TimeZone.cpp @@ -120,6 +120,7 @@ TZRegionModel::fromFile( const char* fileName ) return model; } + model->m_regions.reserve( 12 ); // There's 10 in the file right now QStringList regions; QTextStream in( &file ); @@ -153,20 +154,20 @@ TZRegionModel::fromFile( const char* fileName ) if ( !regions.contains( 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; } -TZRegionModel::~TZRegionModel() {} +TZRegionModel::~TZRegionModel() +{ + qDeleteAll( m_regions ); +} int TZRegionModel::rowCount( const QModelIndex& parent ) const @@ -187,11 +188,11 @@ TZRegionModel::data( const QModelIndex& index, int role ) const return QVariant(); } - const TZRegion& region = m_regions.at( index.row() ); - return role == LabelRole ? region.tr() : region.key(); + const TZRegion* region = m_regions.at( index.row() ); + return role == LabelRole ? region->tr() : region->key(); } -const TZRegion& +const TZRegion* TZRegionModel::region( int index ) const { if ( ( index < 0 ) || ( index >= m_regions.count() ) ) diff --git a/src/libcalamares/locale/TimeZone.h b/src/libcalamares/locale/TimeZone.h index ef3ba2477..db54292c9 100644 --- a/src/libcalamares/locale/TimeZone.h +++ b/src/libcalamares/locale/TimeZone.h @@ -69,6 +69,8 @@ class TZRegion : public CStringPair public: using CStringPair::CStringPair; 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") @@ -100,10 +102,10 @@ public: QVariant data( const QModelIndex& index, int role ) const override; - const TZRegion& region( int index ) const; + const TZRegion* region( int index ) const; private: - QVector< TZRegion > m_regions; + QList< TZRegion* > m_regions; }; } // namespace Locale