summaryrefslogtreecommitdiff
path: root/sc/inc/scmatrix.hxx
blob: 9026288c55cffef182da9e5ceca6738870035e0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (the "License"); you may not use this file
 *   except in compliance with the License. You may obtain a copy of
 *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
 */

#pragma once

#include "address.hxx"
#include "matrixoperators.hxx"
#include "types.hxx"
#include <formula/errorcodes.hxx>
#include "scdllapi.h"
#include <svl/sharedstring.hxx>
#include <svl/sharedstringpool.hxx>

#include <memory>
#include <utility>
#include <vector>

#define DEBUG_MATRIX 0

class ScInterpreter;
class SvNumberFormatter;
class ScMatrixImpl;
enum class FormulaError : sal_uInt16;

namespace sc {

struct Compare;
struct CompareOptions;

}

/**
 * Try NOT to use this struct.  This struct should go away in a hopefully
 * not so distant future.
 */
struct ScMatrixValue
{
    double fVal;
    svl::SharedString aStr;
    ScMatValType nType;

    /// Only valid if ScMatrix methods indicate so!
    const svl::SharedString& GetString() const { return aStr; }

    /// Only valid if ScMatrix methods indicate that this is no string!
    FormulaError GetError() const { return GetDoubleErrorValue(fVal); }

    /// Only valid if ScMatrix methods indicate that this is a boolean
    bool GetBoolean() const         { return fVal != 0.0; }

    ScMatrixValue() : fVal(0.0), nType(ScMatValType::Empty) {}

    ScMatrixValue(const ScMatrixValue& r) :
        fVal(r.fVal), aStr(r.aStr), nType(r.nType) {}

    bool operator== (const ScMatrixValue& r) const
    {
        if (nType != r.nType)
            return false;

        switch (nType)
        {
            case ScMatValType::Value:
            case ScMatValType::Boolean:
                return fVal == r.fVal;
            break;
            default:
                ;
        }

        return aStr == r.aStr;
    }

    bool operator!= (const ScMatrixValue& r) const
    {
        return !operator==(r);
    }

    ScMatrixValue& operator= (const ScMatrixValue& r)
    {
        if (this == &r)
            return *this;

        nType = r.nType;
        fVal = r.fVal;
        aStr = r.aStr;
        return *this;
    }
};

/**
 * Matrix data type that can store values of mixed types.  Each element can
 * be one of the following types: numeric, string, boolean, empty, and empty
 * path.
 */
class SC_DLLPUBLIC ScMatrix final
{
    friend class ScMatrixImpl;

    mutable size_t  nRefCnt;    // reference count
    mutable bool    mbCloneIfConst; // Whether the matrix is cloned with a CloneIfConst() call.
    std::unique_ptr<ScMatrixImpl> pImpl;

    ScMatrix( const ScMatrix& ) = delete;
    ScMatrix& operator=( const ScMatrix&) = delete;

public:
    ScMatrix(SCSIZE nC, SCSIZE nR);
    ScMatrix(SCSIZE nC, SCSIZE nR, double fInitVal);
    ScMatrix( size_t nC, size_t nR, const std::vector<double>& rInitVals );
    ~ScMatrix();

    typedef std::function<void(size_t, size_t, double)> DoubleOpFunction;
    typedef std::function<void(size_t, size_t, bool)> BoolOpFunction;
    typedef std::function<void(size_t, size_t, svl::SharedString)> StringOpFunction;
    typedef std::function<void(size_t, size_t)> EmptyOpFunction;

    /**
     * When adding all numerical matrix elements for a scalar result such as
     * summation, the interpreter wants to separate the first non-zero value
     * with the rest of the summed values. This is necessary for better
     * numerical stability, unless we sort all by absolute values before
     * summing (not really an option) or use another algorithm, e.g. Kahan's
     * summation algorithm,
     * https://en.wikipedia.org/wiki/Kahan_summation_algorithm
     */
    template<typename tRes>
    struct IterateResultMultiple
    {
        std::vector<tRes> maAccumulator;
        size_t mnCount;

        IterateResultMultiple(size_t nCount) :
            maAccumulator(0), mnCount(nCount) {}
    };
    typedef IterateResultMultiple<KahanSum> KahanIterateResultMultiple;
    typedef IterateResultMultiple<double> DoubleIterateResultMultiple;

    /**
      * Iterator for executing one operation with the matrix data.
      */
    template<typename tRes>
    struct IterateResult
    {
        tRes maAccumulator;
        size_t mnCount;

        IterateResult(tRes fAccumulator, size_t nCount)
            : maAccumulator(fAccumulator), mnCount(nCount) {}
    };
    typedef IterateResult<KahanSum> KahanIterateResult;
    typedef IterateResult<double> DoubleIterateResult;


    /** Checks nC or nR for zero and uses GetElementsMax() whether a matrix of
        the size of nC*nR could be allocated. A zero size (both nC and nR zero)
        matrix is allowed for later resize.
     */
    bool static IsSizeAllocatable( SCSIZE nC, SCSIZE nR );

    /// Value or boolean.
    static bool IsValueType( ScMatValType nType )
    {
        return nType <= ScMatValType::Boolean;
    }

    /// Boolean.
    static bool IsBooleanType( ScMatValType nType )
    {
        return nType == ScMatValType::Boolean;
    }

    /// String, empty or empty path, but not value nor boolean.
    static bool IsNonValueType( ScMatValType nType )
    {
        return bool(nType & ScMatValType::NonvalueMask);
    }

    /** String, but not empty or empty path or any other type.
        Not named IsStringType to prevent confusion because previously
        IsNonValueType was named IsStringType. */
    static bool IsRealStringType( ScMatValType nType )
    {
        return (nType & ScMatValType::NonvalueMask) == ScMatValType::String;
    }

    /// Empty, but not empty path or any other type.
    static bool IsEmptyType( ScMatValType nType )
    {
        return (nType & ScMatValType::NonvalueMask) == ScMatValType::Empty;
    }

    /// Empty path, but not empty or any other type.
    static bool IsEmptyPathType( ScMatValType nType )
    {
        return (nType & ScMatValType::NonvalueMask) == ScMatValType::EmptyPath;
    }

    /** Clone the matrix. */
    ScMatrix* Clone() const;

    /** Clone the matrix if mbCloneIfConst (immutable) is set, otherwise
        return _this_ matrix, to be assigned to a ScMatrixRef. */
    ScMatrix* CloneIfConst();

    /** Set the matrix to mutable for CloneIfConst(), only the interpreter
        should do this and know the consequences. */
    void SetMutable();

    /** Set the matrix to immutable for CloneIfConst(), only the interpreter
        should do this and know the consequences. */
    void SetImmutable() const;

    /**
     * Resize the matrix to specified new dimension.
     */
    void Resize(SCSIZE nC, SCSIZE nR);

    void Resize(SCSIZE nC, SCSIZE nR, double fVal);

    /** Clone the matrix and extend it to the new size. nNewCols and nNewRows
        MUST be at least of the size of the original matrix. */
    ScMatrix* CloneAndExtend(SCSIZE nNewCols, SCSIZE nNewRows) const;

    void IncRef() const;
    void DecRef() const;

    void SetErrorInterpreter( ScInterpreter* p);
    void GetDimensions( SCSIZE& rC, SCSIZE& rR) const;
    SCSIZE GetElementCount() const;
    bool ValidColRow( SCSIZE nC, SCSIZE nR) const;

    /** For a row vector or column vector, if the position does not point into
        the vector but is a valid column or row offset it is adapted such that
        it points to an element to be replicated, same column row 0 for a row
        vector, same row column 0 for a column vector. Else, for a 2D matrix,
        returns false.
     */
    bool ValidColRowReplicated( SCSIZE & rC, SCSIZE & rR ) const;

    /** Checks if the matrix position is within the matrix. If it is not, for a
        row vector or column vector the position is adapted such that it points
        to an element to be replicated, same column row 0 for a row vector,
        same row column 0 for a column vector. Else, for a 2D matrix and
        position not within matrix, returns false.
     */
    bool ValidColRowOrReplicated( SCSIZE & rC, SCSIZE & rR ) const;

    void PutDouble( double fVal, SCSIZE nC, SCSIZE nR);
    void PutDouble( double fVal, SCSIZE nIndex);
    void PutDouble(const double* pArray, size_t nLen, SCSIZE nC, SCSIZE nR);

    void PutString( const svl::SharedString& rStr, SCSIZE nC, SCSIZE nR) ;
    void PutString( const svl::SharedString& rStr, SCSIZE nIndex) ;
    void PutString( const svl::SharedString* pArray, size_t nLen, SCSIZE nC, SCSIZE nR) ;

    void PutEmpty( SCSIZE nC, SCSIZE nR);

    /// Jump sal_False without path
    void PutEmptyPath( SCSIZE nC, SCSIZE nR) ;
    void PutError( FormulaError nErrorCode, SCSIZE nC, SCSIZE nR ) ;
    void PutBoolean( bool bVal, SCSIZE nC, SCSIZE nR) ;

    void FillDouble( double fVal,
            SCSIZE nC1, SCSIZE nR1, SCSIZE nC2, SCSIZE nR2 ) ;

    /** Put a column vector of doubles, starting at row nR, must fit into dimensions. */
    void PutDoubleVector( const ::std::vector< double > & rVec, SCSIZE nC, SCSIZE nR ) ;

    /** Put a column vector of strings, starting at row nR, must fit into dimensions. */
    void PutStringVector( const ::std::vector< svl::SharedString > & rVec, SCSIZE nC, SCSIZE nR ) ;

    /** Put a column vector of empties, starting at row nR, must fit into dimensions. */
    void PutEmptyVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;

    /** Put a column vector of empty results, starting at row nR, must fit into dimensions. */
    void PutEmptyResultVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;

    /** Put a column vector of empty paths, starting at row nR, must fit into dimensions. */
    void PutEmptyPathVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;

    /** May be used before obtaining the double value of an element to avoid
        passing its NAN around.
        @ATTENTION: MUST NOT be used if the element is a string!
                    Use GetErrorIfNotString() instead if not sure.
        @returns 0 if no error, else one of err... constants */
    FormulaError GetError( SCSIZE nC, SCSIZE nR) const ;

    /** Use in ScInterpreter to obtain the error code, if any.
        @returns 0 if no error or string element, else one of err... constants */
    FormulaError GetErrorIfNotString( SCSIZE nC, SCSIZE nR) const
        { return IsValue( nC, nR) ? GetError( nC, nR) : FormulaError::NONE; }

    /// @return 0.0 if empty or empty path, else value or DoubleError.
    double GetDouble( SCSIZE nC, SCSIZE nR) const ;
    /// @return 0.0 if empty or empty path, else value or DoubleError.
    double GetDouble( SCSIZE nIndex) const ;
    /// @return value or DoubleError or string converted to value.
    double GetDoubleWithStringConversion( SCSIZE nC, SCSIZE nR ) const ;

    /// @return empty string if empty or empty path, else string content.
    svl::SharedString GetString( SCSIZE nC, SCSIZE nR) const ;
    /// @return empty string if empty or empty path, else string content.
    svl::SharedString GetString( SCSIZE nIndex) const ;

    /** @returns the matrix element's string if one is present, otherwise the
        numerical value formatted as string, or in case of an error the error
        string is returned; an empty string for empty, a "FALSE" string for
        empty path. */
    svl::SharedString GetString( SvNumberFormatter& rFormatter, SCSIZE nC, SCSIZE nR) const ;

    /// @ATTENTION: If bString the ScMatrixValue->pS may still be NULL to indicate
    /// an empty string!
    ScMatrixValue Get( SCSIZE nC, SCSIZE nR) const ;

    /** @return <TRUE/> if string or any empty, empty cell, empty result, empty
        path, in fact non-value. */
    bool IsStringOrEmpty( SCSIZE nIndex ) const ;

    /** @return <TRUE/> if string or any empty, empty cell, empty result, empty
        path, in fact non-value. */
    bool IsStringOrEmpty( SCSIZE nC, SCSIZE nR ) const ;

    /// @return <TRUE/> if empty or empty cell or empty result, not empty path.
    bool IsEmpty( SCSIZE nC, SCSIZE nR ) const ;

    /// @return <TRUE/> if empty cell, not empty or empty result or empty path.
    bool IsEmptyCell( SCSIZE nC, SCSIZE nR ) const ;

    /// @return <TRUE/> if empty result, not empty or empty cell or empty path.
    bool IsEmptyResult( SCSIZE nC, SCSIZE nR ) const ;

    /// @return <TRUE/> if empty path, not empty or empty cell or empty result.
    bool IsEmptyPath( SCSIZE nC, SCSIZE nR ) const ;

    /// @return <TRUE/> if value or boolean.
    bool IsValue( SCSIZE nIndex ) const ;

    /// @return <TRUE/> if value or boolean.
    bool IsValue( SCSIZE nC, SCSIZE nR ) const ;

    /// @return <TRUE/> if value or boolean or empty or empty path.
    bool IsValueOrEmpty( SCSIZE nC, SCSIZE nR ) const ;

    /// @return <TRUE/> if boolean.
    bool IsBoolean( SCSIZE nC, SCSIZE nR ) const ;

    /// @return <TRUE/> if entire matrix is numeric, including booleans, with no strings or empties
    bool IsNumeric() const ;

    void MatTrans( const ScMatrix& mRes) const ;
    void MatCopy ( const ScMatrix& mRes) const ;

    // Convert ScInterpreter::CompareMat values (-1,0,1) to boolean values
    void CompareEqual() ;
    void CompareNotEqual() ;
    void CompareLess() ;
    void CompareGreater() ;
    void CompareLessEqual() ;
    void CompareGreaterEqual() ;

    double And() const ;       // logical AND of all matrix values, or NAN
    double Or() const ;        // logical OR of all matrix values, or NAN
    double Xor() const ;       // logical XOR of all matrix values, or NAN

    KahanIterateResult Sum( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
    KahanIterateResult SumSquare( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
    DoubleIterateResult Product( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
    size_t Count(bool bCountStrings, bool bCountErrors, bool bIgnoreEmptyStrings = false) const ;
    size_t MatchDoubleInColumns(double fValue, size_t nCol1, size_t nCol2) const ;
    size_t MatchStringInColumns(const svl::SharedString& rStr, size_t nCol1, size_t nCol2) const ;

    double GetMaxValue( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
    double GetMinValue( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
    double GetGcd() const ;
    double GetLcm() const ;

    ScMatrixRef CompareMatrix(
        sc::Compare& rComp, size_t nMatPos, sc::CompareOptions* pOptions ) const ;

    /**
     * Convert the content of matrix into a linear array of numeric values.
     * String elements are mapped to NaN's and empty elements are mapped to
     * either NaN or zero values.
     *
     * @param bEmptyAsZero if true empty elements are mapped to zero values,
     *                     otherwise they become NaN values.
     */
    void GetDoubleArray( std::vector<double>& rArray, bool bEmptyAsZero = true ) const ;
    void MergeDoubleArrayMultiply( std::vector<double>& rArray ) const ;

    void NotOp(const ScMatrix& rMat) ;
    void NegOp(const ScMatrix& rMat) ;
    void AddOp(double fVal, const ScMatrix& rMat) ;
    void SubOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
    void MulOp(double fVal, const ScMatrix& rMat) ;
    void DivOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
    void PowOp(bool bFlag, double fVal, const ScMatrix& rMat) ;

    KahanIterateResultMultiple CollectKahan(const std::vector<sc::op::kOp>& aOp) ;

    void ExecuteOperation(const std::pair<size_t, size_t>& rStartPos, const std::pair<size_t, size_t>& rEndPos,
            DoubleOpFunction aDoubleFunc, BoolOpFunction aBoolFunc, StringOpFunction aStringFunc,
            EmptyOpFunction aEmptyFunc) const ;

    void MatConcat(SCSIZE nMaxCol, SCSIZE nMaxRow, const ScMatrixRef& xMat1, const ScMatrixRef& xMat2,
            SvNumberFormatter& rFormatter, svl::SharedStringPool& rPool) ;

#if DEBUG_MATRIX
    void Dump() const;
#endif
};

inline void intrusive_ptr_add_ref(const ScMatrix* p)
{
    p->IncRef();
}

inline void intrusive_ptr_release(const ScMatrix* p)
{
    p->DecRef();
}

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */