Coverage Report

Created: 2025-12-08 09:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/sc/inc/subtotal.hxx
Line
Count
Source
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
24
class SubTotal
25
{
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);
30
};
31
32
/** Implements the Welford Online one-pass algorithm.
33
    See https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Welford's_Online_algorithm
34
    and Donald E. Knuth, TAoCP vol.2, 3rd edn., p. 232
35
 */
36
class WelfordRunner
37
{
38
public:
39
12.7k
    WelfordRunner() : mfMean(0.0), mfM2(0.0), mnCount(0) {}
40
    void        update( double fVal );
41
0
    sal_uInt64  getCount() const                { return mnCount; }
42
0
    double      getVarianceSample() const       { return mnCount > 1 ? mfM2 / (mnCount-1) : 0.0; }
43
0
    double      getVariancePopulation() const   { return mnCount > 0 ? mfM2 / mnCount : 0.0; }
44
45
    // The private variables can be abused by ScFunctionData as general
46
    // sum/min/max/ave/count/... variables to reduce memory footprint for that
47
    // ScFunctionData may be a mass object during consolidation.
48
    // ScFunctionData::update() and getResult() take care that purposes are not
49
    // mixed.
50
    friend class ScFunctionData;
51
private:
52
    double      mfMean;
53
    double      mfM2;
54
    sal_uInt64  mnCount;
55
};
56
57
/** To calculate a single subtotal function. */
58
class ScFunctionData
59
{
60
public:
61
0
    ScFunctionData() : meFunc(SUBTOTAL_FUNC_NONE), mbError(false) {}
62
0
    ScFunctionData( ScSubTotalFunc eFn ) : meFunc(eFn), mbError(false) {}
63
64
    void            update( double fNewVal );
65
    /// Check getError() after (!) obtaining the result.
66
    double          getResult();
67
0
    bool            getError() const    { return mbError; }
68
0
    ScSubTotalFunc  getFunc() const     { return meFunc; }
69
0
    void            setError()          { mbError = true; }
70
71
private:
72
    WelfordRunner   maWelford;
73
    ScSubTotalFunc  meFunc;
74
    bool            mbError;
75
76
0
    double&         getValueRef()   { return maWelford.mfMean; }
77
0
    sal_uInt64&     getCountRef()   { return maWelford.mnCount; }
78
};
79
80
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */