From 2d864c3e9da1ad38a86ca80ca0b1811c983fbe7c Mon Sep 17 00:00:00 2001 From: Berthold Stoeger Date: Sun, 10 Jan 2021 20:22:18 +0100 Subject: [PATCH] statistics: sort dive sites The dive sites where sorted by location in RAM, which is just silly. Add a DiveSiteWrapper that sorts by name, though that should probably be improved. Suggested-by: Peter Zaal Signed-off-by: Berthold Stoeger --- stats/statsvariables.cpp | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/stats/statsvariables.cpp b/stats/statsvariables.cpp index dc05067c4..ea8f33211 100644 --- a/stats/statsvariables.cpp +++ b/stats/statsvariables.cpp @@ -45,6 +45,30 @@ static QString join_strings(const std::vector &v) return res; } +// A wrapper around dive site, that caches the name of the dive site +struct DiveSiteWrapper { + const dive_site *ds; + QString name; + DiveSiteWrapper(const dive_site *ds) : ds(ds), + name(ds ? ds->name : "") + { + } + bool operator<(const DiveSiteWrapper &d2) const { + if (int cmp = QString::compare(name, d2.name, Qt::CaseInsensitive)) + return cmp < 0; + return ds < d2.ds; // This is just random, try something better. + } + bool operator==(const DiveSiteWrapper &d2) const { + return ds == d2.ds; + } + bool operator!=(const DiveSiteWrapper &d2) const { + return ds != d2.ds; + } + QString format() const { + return ds ? name : StatsTranslations::tr("no divesite"); + } +}; + // Note: usually I dislike functions defined inside class/struct // declarations ("Java style"). However, for brevity this is done // in this rather template-heavy source file more or less consistently. @@ -91,9 +115,9 @@ static bool is_invalid_value(const year_quarter &) return false; } -static bool is_invalid_value(const dive_site *d) +static bool is_invalid_value(const DiveSiteWrapper &d) { - return !d; + return !d.ds; } static bool is_invalid_value(const StatsOperationResults &res) @@ -1730,15 +1754,14 @@ struct CylinderTypeVariable : public StatsVariableTemplate; +using LocationBin = SimpleBin; struct LocationBinner : public SimpleBinner { QString format(const StatsBin &bin) const override { - const dive_site *ds = derived_bin(bin).value; - return QString(ds ? ds->name : "-"); + return derived_bin(bin).value.format(); } - const dive_site *to_bin_value(const dive *d) const { - return d->dive_site; + const DiveSiteWrapper to_bin_value(const dive *d) const { + return DiveSiteWrapper(d->dive_site); } }; @@ -1748,7 +1771,7 @@ struct LocationVariable : public StatsVariableTemplatedive_site ? d->dive_site->name : "-"; + return DiveSiteWrapper(d->dive_site).format(); } std::vector binners() const override { return { &location_binner };