From 36644dc9f7540886801bda6131dff36241d9e879 Mon Sep 17 00:00:00 2001 From: Rolf Eike Beer Date: Wed, 3 Apr 2019 23:05:44 +0200 Subject: optimize selectedDivesGasUsed() -return the result instead of storing in a parameter, we now know that the list contains only those results that are generated in the function -allocate the result with the correct length right from the start -do not iterate over keys of a map and then do a map lookup to get the value but use an iterator that gives us both right from the start -remove one call alltogether as the results were not used there Signed-off-by: Rolf Eike Beer --- core/qthelper.cpp | 11 +++++++---- core/qthelper.h | 2 +- desktop-widgets/tab-widgets/TabDiveStatistics.cpp | 3 +-- desktop-widgets/tab-widgets/maintab.cpp | 15 --------------- 4 files changed, 9 insertions(+), 22 deletions(-) diff --git a/core/qthelper.cpp b/core/qthelper.cpp index 35965208c..9483d9ab5 100644 --- a/core/qthelper.cpp +++ b/core/qthelper.cpp @@ -375,7 +375,7 @@ static bool lessThan(const QPair &a, const QPair &b) return a.second < b.second; } -void selectedDivesGasUsed(QVector > &gasUsedOrdered) +QVector> selectedDivesGasUsed() { int i, j; struct dive *d; @@ -391,10 +391,13 @@ void selectedDivesGasUsed(QVector > &gasUsedOrdered) gasUsed[gasName] += diveGases[j].mliter; } } - Q_FOREACH(const QString& gas, gasUsed.keys()) { - gasUsedOrdered.append(qMakePair(gas, gasUsed[gas])); - } + QVector> gasUsedOrdered; + gasUsedOrdered.reserve(gasUsed.size()); + for (auto it = gasUsed.cbegin(); it != gasUsed.cend(); ++it) + gasUsedOrdered.append(qMakePair(it.key(), it.value())); std::sort(gasUsedOrdered.begin(), gasUsedOrdered.end(), lessThan); + + return gasUsedOrdered; } QString getUserAgent() diff --git a/core/qthelper.h b/core/qthelper.h index cbe2b77f0..3b7ff2c59 100644 --- a/core/qthelper.h +++ b/core/qthelper.h @@ -80,7 +80,7 @@ QString get_short_dive_date_string(timestamp_t when); QString get_trip_date_string(timestamp_t when, int nr, bool getday); QString uiLanguage(QLocale *callerLoc); QLocale getLocale(); -void selectedDivesGasUsed(QVector > &gasUsed); +QVector> selectedDivesGasUsed(); QString getUserAgent(); QString printGPSCoords(const location_t *loc); diff --git a/desktop-widgets/tab-widgets/TabDiveStatistics.cpp b/desktop-widgets/tab-widgets/TabDiveStatistics.cpp index dacb01721..0cf3befd2 100644 --- a/desktop-widgets/tab-widgets/TabDiveStatistics.cpp +++ b/desktop-widgets/tab-widgets/TabDiveStatistics.cpp @@ -97,10 +97,9 @@ void TabDiveStatistics::updateData() ui->timeLimits->setMinimum(""); } - QVector > gasUsed; + QVector > gasUsed = selectedDivesGasUsed(); QString gasUsedString; volume_t vol; - selectedDivesGasUsed(gasUsed); for (int j = 0; j < MAX_CYLINDERS; j++) { if (gasUsed.isEmpty()) break; diff --git a/desktop-widgets/tab-widgets/maintab.cpp b/desktop-widgets/tab-widgets/maintab.cpp index dbb83f356..d1f13dd13 100644 --- a/desktop-widgets/tab-widgets/maintab.cpp +++ b/desktop-widgets/tab-widgets/maintab.cpp @@ -542,21 +542,6 @@ void MainTab::updateDiveInfo(bool clear) int mean[MAX_CYLINDERS], duration[MAX_CYLINDERS]; per_cylinder_mean_depth(&displayed_dive, select_dc(&displayed_dive), mean, duration); - // now let's get some gas use statistics - QVector > gasUsed; - QString gasUsedString; - volume_t vol; - selectedDivesGasUsed(gasUsed); - for (int j = 0; j < MAX_CYLINDERS; j++) { - if (gasUsed.isEmpty()) - break; - QPair gasPair = gasUsed.last(); - gasUsed.pop_back(); - vol.mliter = gasPair.second; - gasUsedString.append(gasPair.first).append(": ").append(get_volume_string(vol, true)).append("\n"); - } - if (!gasUsed.isEmpty()) - gasUsedString.append("..."); volume_t o2_tot = {}, he_tot = {}; selected_dives_gas_parts(&o2_tot, &he_tot); -- cgit v1.2.3-70-g09d2