LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sc / inc / subtotal.hxx
bloba2a8459b193991c12d0ef0f0e74f99ea10772c03
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 #pragma once
22 #include "global.hxx"
24 class SubTotal
26 public:
27 static bool SafePlus( double& fVal1, double fVal2);
28 static bool SafeMult( double& fVal1, double fVal2);
29 static bool SafeDiv( double& fVal1, double fVal2);
32 class ScFunctionData;
34 /** Implements the Welford Online one-pass algorithm.
35 See https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_Online_algorithm
36 and Donald E. Knuth, TAoCP vol.2, 3rd edn., p. 232
38 class WelfordRunner
40 public:
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; }
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;
53 private:
54 double mfMean;
55 double mfM2;
56 sal_uInt64 mnCount;
59 /** To calculate a single subtotal function. */
60 class ScFunctionData
62 public:
63 ScFunctionData() : meFunc(SUBTOTAL_FUNC_NONE), mbError(false) {}
64 ScFunctionData( ScSubTotalFunc eFn ) : meFunc(eFn), mbError(false) {}
66 void update( double fNewVal );
67 /// Check getError() after (!) obtaining the result.
68 double getResult();
69 bool getError() const { return mbError; }
70 ScSubTotalFunc getFunc() const { return meFunc; }
71 void setError() { mbError = true; }
73 private:
74 WelfordRunner maWelford;
75 ScSubTotalFunc meFunc;
76 bool mbError;
78 double& getValueRef() { return maWelford.mfMean; }
79 sal_uInt64& getCountRef() { return maWelford.mnCount; }
82 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */