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#pragma once
21
22#include "global.hxx"
23
25{
26public:
27 static bool SafePlus( double& fVal1, double fVal2);
28 static bool SafeMult( double& fVal1, double fVal2);
29 static bool SafeDiv( double& fVal1, double fVal2);
30};
31
32class ScFunctionData;
33
39{
40public:
41 WelfordRunner() : mfMean(0.0), mfM2(0.0), mnCount(0) {}
42 void update( double fVal );
43 sal_uInt64 getCount() const { return mnCount; }
44 double getVarianceSample() const { return mnCount > 1 ? mfM2 / (mnCount-1) : 0.0; }
45 double getVariancePopulation() const { return mnCount > 0 ? mfM2 / mnCount : 0.0; }
46
47 // The private variables can be abused by ScFunctionData as general
48 // sum/min/max/ave/count/... variables to reduce memory footprint for that
49 // ScFunctionData may be a mass object during consolidation.
50 // ScFunctionData::update() and getResult() take care that purposes are not
51 // mixed.
52 friend class ScFunctionData;
53private:
54 double mfMean;
55 double mfM2;
56 sal_uInt64 mnCount;
57};
58
61{
62public:
65
66 void update( double fNewVal );
68 double getResult();
69 bool getError() const { return mbError; }
70 ScSubTotalFunc getFunc() const { return meFunc; }
71 void setError() { mbError = true; }
72
73private:
76 bool mbError;
77
78 double& getValueRef() { return maWelford.mfMean; }
79 sal_uInt64& getCountRef() { return maWelford.mnCount; }
80};
81
82/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
To calculate a single subtotal function.
Definition: subtotal.hxx:61
double & getValueRef()
Definition: subtotal.hxx:78
void setError()
Definition: subtotal.hxx:71
ScFunctionData(ScSubTotalFunc eFn)
Definition: subtotal.hxx:64
bool getError() const
Definition: subtotal.hxx:69
ScSubTotalFunc getFunc() const
Definition: subtotal.hxx:70
sal_uInt64 & getCountRef()
Definition: subtotal.hxx:79
void update(double fNewVal)
Definition: subtotal.cxx:66
WelfordRunner maWelford
Definition: subtotal.hxx:74
double getResult()
Check getError() after (!) obtaining the result.
Definition: subtotal.cxx:129
ScSubTotalFunc meFunc
Definition: subtotal.hxx:75
static bool SafePlus(double &fVal1, double fVal2)
Definition: subtotal.cxx:24
static bool SafeMult(double &fVal1, double fVal2)
Definition: subtotal.cxx:40
static bool SafeDiv(double &fVal1, double fVal2)
Definition: subtotal.cxx:53
Implements the Welford Online one-pass algorithm.
Definition: subtotal.hxx:39
sal_uInt64 mnCount
Definition: subtotal.hxx:56
double mfM2
Definition: subtotal.hxx:55
double mfMean
Definition: subtotal.hxx:54
double getVariancePopulation() const
Definition: subtotal.hxx:45
sal_uInt64 getCount() const
Definition: subtotal.hxx:43
void update(double fVal)
Definition: subtotal.cxx:196
double getVarianceSample() const
Definition: subtotal.hxx:44
ScSubTotalFunc
Definition: global.hxx:860
@ SUBTOTAL_FUNC_NONE
Definition: global.hxx:861