Bump version to 6.4-15
[LibreOffice.git] / sc / inc / subtotal.hxx
blobb78bf06844a3e70a33bc2793831349f9525d0790
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SC_INC_SUBTOTAL_HXX
21 #define INCLUDED_SC_INC_SUBTOTAL_HXX
23 #include "global.hxx"
25 class SubTotal
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);
33 class ScFunctionData;
35 /** Implements the Welford Online one-pass algorithm.
36 See https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_Online_algorithm
37 and Donald E. Knuth, TAoCP vol.2, 3rd edn., p. 232
39 class WelfordRunner
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; }
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;
60 /** To calculate a single subtotal function. */
61 class ScFunctionData
63 public:
64 ScFunctionData() : meFunc(SUBTOTAL_FUNC_NONE), mbError(false) {}
65 ScFunctionData( ScSubTotalFunc eFn ) : meFunc(eFn), mbError(false) {}
67 void update( double fNewVal );
68 /// Check getError() after (!) obtaining the result.
69 double getResult();
70 bool getError() const { return mbError; }
71 ScSubTotalFunc getFunc() const { return meFunc; }
72 void setError() { mbError = true; }
74 private:
75 WelfordRunner maWelford;
76 ScSubTotalFunc meFunc;
77 bool mbError;
79 double& getValueRef() { return maWelford.mfMean; }
80 sal_uInt64& getCountRef() { return maWelford.mnCount; }
83 #endif
85 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */