LibreOffice Module sc (master)  1
subtotal.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_SC_INC_SUBTOTAL_HXX
21 #define INCLUDED_SC_INC_SUBTOTAL_HXX
22 
23 #include "global.hxx"
24 
25 class SubTotal
26 {
27 public:
28  static bool SafePlus( double& fVal1, double fVal2);
29  static bool SafeMult( double& fVal1, double fVal2);
30  static bool SafeDiv( double& fVal1, double fVal2);
31 };
32 
33 class ScFunctionData;
34 
40 {
41 public:
42  WelfordRunner() : mfMean(0.0), mfM2(0.0), mnCount(0) {}
43  void update( double fVal );
44  sal_uInt64 getCount() const { return mnCount; }
45  double getVarianceSample() const { return mnCount > 1 ? mfM2 / (mnCount-1) : 0.0; }
46  double getVariancePopulation() const { return mnCount > 0 ? mfM2 / mnCount : 0.0; }
47 
48  // The private variables can be abused by ScFunctionData as general
49  // sum/min/max/ave/count/... variables to reduce memory footprint for that
50  // ScFunctionData may be a mass object during consolidation.
51  // ScFunctionData::update() and getResult() take care that purposes are not
52  // mixed.
53  friend class ScFunctionData;
54 private:
55  double mfMean;
56  double mfM2;
57  sal_uInt64 mnCount;
58 };
59 
62 {
63 public:
65  ScFunctionData( ScSubTotalFunc eFn ) : meFunc(eFn), mbError(false) {}
66 
67  void update( double fNewVal );
69  double getResult();
70  bool getError() const { return mbError; }
71  ScSubTotalFunc getFunc() const { return meFunc; }
72  void setError() { mbError = true; }
73 
74 private:
77  bool mbError;
78 
79  double& getValueRef() { return maWelford.mfMean; }
80  sal_uInt64& getCountRef() { return maWelford.mnCount; }
81 };
82 
83 #endif
84 
85 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ScSubTotalFunc getFunc() const
Definition: subtotal.hxx:71
ScSubTotalFunc meFunc
Definition: subtotal.hxx:76
double getVarianceSample() const
Definition: subtotal.hxx:45
double getVariancePopulation() const
Definition: subtotal.hxx:46
static bool SafeMult(double &fVal1, double fVal2)
Definition: subtotal.cxx:40
Implements the Welford Online one-pass algorithm.
Definition: subtotal.hxx:39
sal_uInt64 & getCountRef()
Definition: subtotal.hxx:80
sal_uInt64 mnCount
Definition: subtotal.hxx:57
void setError()
Definition: subtotal.hxx:72
double getResult()
Check getError() after (!) obtaining the result.
Definition: subtotal.cxx:129
static bool SafePlus(double &fVal1, double fVal2)
Definition: subtotal.cxx:24
double mfMean
Definition: subtotal.hxx:55
static bool SafeDiv(double &fVal1, double fVal2)
Definition: subtotal.cxx:53
To calculate a single subtotal function.
Definition: subtotal.hxx:61
double & getValueRef()
Definition: subtotal.hxx:79
sal_uInt64 getCount() const
Definition: subtotal.hxx:44
WelfordRunner maWelford
Definition: subtotal.hxx:75
void update(double fNewVal)
Definition: subtotal.cxx:66
void update(double fVal)
Definition: subtotal.cxx:196
ScFunctionData(ScSubTotalFunc eFn)
Definition: subtotal.hxx:65
double mfM2
Definition: subtotal.hxx:56
bool getError() const
Definition: subtotal.hxx:70
ScSubTotalFunc
Definition: global.hxx:845