diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2021-01-10 19:07:29 +0100 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2021-01-10 13:42:48 -0800 |
commit | 0efcbb0eb6e7711c14779140c6198a43a3a09806 (patch) | |
tree | 2c3ea582e5e3e2059077df62e255a2c30db855f2 /stats | |
parent | bd26d8407f28186c9db86e96260bd51f25e07f01 (diff) | |
download | subsurface-0efcbb0eb6e7711c14779140c6198a43a3a09806.tar.gz |
statistics: add min and max operations for numerical types
This makes sense and is easy to implement.
Suggested-by: Peter Zaal <peter.zaal@gmail.com>
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'stats')
-rw-r--r-- | stats/statsvariables.cpp | 31 | ||||
-rw-r--r-- | stats/statsvariables.h | 4 |
2 files changed, 23 insertions, 12 deletions
diff --git a/stats/statsvariables.cpp b/stats/statsvariables.cpp index c163fd0df..dc05067c4 100644 --- a/stats/statsvariables.cpp +++ b/stats/statsvariables.cpp @@ -297,7 +297,9 @@ static const char *operation_names[] = { QT_TRANSLATE_NOOP("StatsTranslations", "Median"), QT_TRANSLATE_NOOP("StatsTranslations", "Mean"), QT_TRANSLATE_NOOP("StatsTranslations", "Time-weighted mean"), - QT_TRANSLATE_NOOP("StatsTranslations", "Sum") + QT_TRANSLATE_NOOP("StatsTranslations", "Sum"), + QT_TRANSLATE_NOOP("StatsTranslations", "Minimum"), + QT_TRANSLATE_NOOP("StatsTranslations", "Maximum") }; QStringList StatsVariable::supportedOperationNames() const @@ -413,11 +415,17 @@ StatsOperationResults StatsVariable::applyOperations(const std::vector<dive *> & if (res.count <= 0) return res; + res.min = std::numeric_limits<double>::max(); + res.max = std::numeric_limits<double>::lowest(); for (auto [v, d]: val) { res.sum += v; res.mean += v; sumTime += d->duration.seconds; res.timeWeightedMean += v * d->duration.seconds; + if (v < res.min) + res.min = v; + if (v > res.max) + res.max = v; } res.mean /= res.count; @@ -426,7 +434,7 @@ StatsOperationResults StatsVariable::applyOperations(const std::vector<dive *> & } StatsOperationResults::StatsOperationResults() : - count(0), median(0.0), mean(0.0), timeWeightedMean(0.0), sum(0.0) + count(0), median(0.0), mean(0.0), timeWeightedMean(0.0), sum(0.0), min(0.0), max(0.0) { } @@ -442,6 +450,8 @@ double StatsOperationResults::get(StatsOperation op) const case StatsOperation::Mean: return mean; case StatsOperation::TimeWeightedMean: return timeWeightedMean; case StatsOperation::Sum: return sum; + case StatsOperation::Min: return min; + case StatsOperation::Max: return max; case StatsOperation::Invalid: default: return invalid_value<double>(); } @@ -986,9 +996,6 @@ struct DepthVariableBase : public StatsVariableTemplate<StatsVariable::Type::Num return prefs.units.length == units::METERS ? depth / 1000.0 : mm_to_feet(depth); } - std::vector<StatsOperation> supportedOperations() const override { - return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::Sum }; - } }; struct MaxDepthVariable : public DepthVariableBase { @@ -998,7 +1005,7 @@ struct MaxDepthVariable : public DepthVariableBase { return StatsTranslations::tr("Max. Depth"); } std::vector<StatsOperation> supportedOperations() const override { - return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::Sum }; + return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::Sum, StatsOperation::Min, StatsOperation::Max }; } }; @@ -1010,7 +1017,7 @@ struct MeanDepthVariable : public DepthVariableBase { return StatsTranslations::tr("Mean Depth"); } std::vector<StatsOperation> supportedOperations() const override { - return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::TimeWeightedMean }; + return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::TimeWeightedMean, StatsOperation::Min, StatsOperation::Max }; } }; @@ -1068,7 +1075,7 @@ struct DurationVariable : public StatsVariableTemplate<StatsVariable::Type::Nume return d->duration.seconds / 60.0; } std::vector<StatsOperation> supportedOperations() const override { - return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::Sum }; + return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::Sum, StatsOperation::Min, StatsOperation::Max }; } }; @@ -1159,7 +1166,7 @@ struct SACVariable : public StatsVariableTemplate<StatsVariable::Type::Numeric> ml_to_cuft(d->sac); } std::vector<StatsOperation> supportedOperations() const override { - return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::TimeWeightedMean }; + return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::TimeWeightedMean, StatsOperation::Min, StatsOperation::Max }; } }; @@ -1219,7 +1226,7 @@ struct TemperatureVariable : public StatsVariableTemplate<StatsVariable::Type::N return { &bin2F, &bin5F, &bin10F, &bin20F }; } std::vector<StatsOperation> supportedOperations() const override { - return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::TimeWeightedMean }; + return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::TimeWeightedMean, StatsOperation::Min, StatsOperation::Max }; } }; @@ -1298,7 +1305,7 @@ struct WeightVariable : public StatsVariableTemplate<StatsVariable::Type::Numeri : grams_to_lbs(total_weight(d)); } std::vector<StatsOperation> supportedOperations() const override { - return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::Sum }; + return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::Sum, StatsOperation::Min, StatsOperation::Max }; } }; @@ -1601,7 +1608,7 @@ struct GasContentVariable : public StatsVariableTemplate<StatsVariable::Type::Nu return 1; } std::vector<StatsOperation> supportedOperations() const override { - return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::TimeWeightedMean }; + return { StatsOperation::Median, StatsOperation::Mean, StatsOperation::TimeWeightedMean, StatsOperation::Min, StatsOperation::Max }; } double toFloat(const dive *d) const override { int res = get_gas_content(d, he, max_he); diff --git a/stats/statsvariables.h b/stats/statsvariables.h index 06fb90f2a..964878550 100644 --- a/stats/statsvariables.h +++ b/stats/statsvariables.h @@ -26,6 +26,8 @@ enum class StatsOperation : int { Mean, TimeWeightedMean, Sum, + Min, + Max, Invalid }; @@ -36,6 +38,8 @@ struct StatsOperationResults { double mean; double timeWeightedMean; double sum; + double min; + double max; StatsOperationResults(); // Initialize to invalid (e.g. no dives) bool isValid() const; double get(StatsOperation op) const; |