diff options
author | Eike Rathke <erack@redhat.com> | 2018-12-13 19:26:22 +0100 |
---|---|---|
committer | Eike Rathke <erack@redhat.com> | 2018-12-14 00:05:10 +0100 |
commit | d9aaa26a4efbe7c69254ecc5f00b997f237686e9 (patch) | |
tree | 549fa984a4006d6d083cd3a12a2fbde873b64016 /sc/inc/subtotal.hxx | |
parent | 4786a09e7c41c5dc21732b2e0c74c2984c40c9b5 (diff) |
Encapsulate ScFunctionData members and abuse WelfordRunner members
... to squeeze some memory and later use this as a mass object
during consolidation.
Change-Id: I3f0aa03ec0bbbb4c64a4854b55a451dd3cacfa90
Reviewed-on: https://gerrit.libreoffice.org/65124
Tested-by: Jenkins
Reviewed-by: Eike Rathke <erack@redhat.com>
Diffstat (limited to 'sc/inc/subtotal.hxx')
-rw-r--r-- | sc/inc/subtotal.hxx | 52 |
1 files changed, 32 insertions, 20 deletions
diff --git a/sc/inc/subtotal.hxx b/sc/inc/subtotal.hxx index 2c1db3ca2264..2e2811e54815 100644 --- a/sc/inc/subtotal.hxx +++ b/sc/inc/subtotal.hxx @@ -30,6 +30,8 @@ public: static bool SafeDiv( double& fVal1, double fVal2); }; +class ScFunctionData; + /** Implements the Welford Online one-pass algorithm. See https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_Online_algorithm and Donald E. Knuth, TAoCP vol.2, 3rd edn., p. 232 @@ -37,34 +39,44 @@ public: class WelfordRunner { public: - WelfordRunner() : fMean(0.0), fM2(0.0), nCount(0) {} + WelfordRunner() : mfMean(0.0), mfM2(0.0), mnCount(0) {} void update( double fVal ); - sal_uInt64 getCount() const { return nCount; } - double getMean() const { return fMean; } - double getVarianceSample() const { return nCount > 1 ? fM2 / (nCount-1) : 0.0; } - double getVariancePopulation() const { return nCount > 0 ? fM2 / nCount : 0.0; } + sal_uInt64 getCount() const { return mnCount; } + double getMean() const { return mfMean; } + double getVarianceSample() const { return mnCount > 1 ? mfM2 / (mnCount-1) : 0.0; } + double getVariancePopulation() const { return mnCount > 0 ? mfM2 / mnCount : 0.0; } + // The private variables can be abused by ScFunctionData as general + // sum/min/max/ave/count/... variables to reduce memory footprint for that + // ScFunctionData may be a mass object during consolidation. + // ScFunctionData::update() and getResult() take care that purposes are not + // mixed. + friend class ScFunctionData; private: - double fMean; - double fM2; - sal_uInt64 nCount; + double mfMean; + double mfM2; + sal_uInt64 mnCount; }; -struct ScFunctionData // to calculate single functions +/** To calculate a single subtotal function. */ +class ScFunctionData { -private: - WelfordRunner maWelford; - double nVal; - sal_uInt64 nCount; public: - ScSubTotalFunc const eFunc; - bool bError; + ScFunctionData( ScSubTotalFunc eFn ) : meFunc(eFn), mbError(false) {} + void update( double fNewVal ); + /// Check getError() after (!) obtaining the result. + double getResult(); + bool getError() const { return mbError; } + ScSubTotalFunc getFunc() const { return meFunc; } + void setError() { mbError = true; } + +private: + WelfordRunner maWelford; + ScSubTotalFunc const meFunc; + bool mbError; - ScFunctionData( ScSubTotalFunc eFn ) : - nVal(0.0), nCount(0), eFunc(eFn), bError(false) {} - void update( double fNewVal ); - /// Check bError after (!) obtaining the result. - double getResult(); + double& getValueRef() { return maWelford.mfMean; } + sal_uInt64& getCountRef() { return maWelford.mnCount; } }; #endif |