LibreOffice Module sc (master) 1
scmatrix.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 "address.hxx"
23#include "matrixoperators.hxx"
24#include "types.hxx"
26#include "scdllapi.h"
27#include <svl/sharedstring.hxx>
29
30#include <memory>
31#include <utility>
32#include <vector>
33
34#define DEBUG_MATRIX 0
35
36class ScInterpreter;
38class ScMatrixImpl;
39enum class FormulaError : sal_uInt16;
40
41namespace sc {
42
43struct Compare;
44struct CompareOptions;
45
46}
47
53{
54 double fVal;
57
59 const svl::SharedString& GetString() const { return aStr; }
60
63
65 bool GetBoolean() const { return fVal != 0.0; }
66
68 ScMatrixValue(const ScMatrixValue& r) = default;
70
71 bool operator== (const ScMatrixValue& r) const
72 {
73 if (nType != r.nType)
74 return false;
75
76 switch (nType)
77 {
80 return fVal == r.fVal;
81 break;
82 default:
83 ;
84 }
85
86 return aStr == r.aStr;
87 }
88
89 bool operator!= (const ScMatrixValue& r) const
90 {
91 return !operator==(r);
92 }
93};
94
101{
102 friend class ScMatrixImpl;
103
104 mutable size_t nRefCnt; // reference count
105 mutable bool mbCloneIfConst; // Whether the matrix is cloned with a CloneIfConst() call.
106 std::unique_ptr<ScMatrixImpl> pImpl;
107
108 ScMatrix( const ScMatrix& ) = delete;
109 ScMatrix& operator=( const ScMatrix&) = delete;
110
111public:
112 ScMatrix(SCSIZE nC, SCSIZE nR);
113 ScMatrix(SCSIZE nC, SCSIZE nR, double fInitVal);
114 ScMatrix( size_t nC, size_t nR, const std::vector<double>& rInitVals );
115 ~ScMatrix();
116
117 typedef std::function<void(size_t, size_t, double)> DoubleOpFunction;
118 typedef std::function<void(size_t, size_t, bool)> BoolOpFunction;
119 typedef std::function<void(size_t, size_t, svl::SharedString)> StringOpFunction;
120 typedef std::function<void(size_t, size_t)> EmptyOpFunction;
121 typedef std::function<double(double, double)> CalculateOpFunction;
122
132 template<typename tRes>
134 {
135 std::vector<tRes> maAccumulator;
136 size_t mnCount;
137
138 IterateResultMultiple(size_t nCount) :
139 maAccumulator(0), mnCount(nCount) {}
140 };
143
147 template<typename tRes>
149 {
151 size_t mnCount;
152
153 IterateResult(tRes fAccumulator, size_t nCount)
154 : maAccumulator(fAccumulator), mnCount(nCount) {}
155 };
158
159
164 bool static IsSizeAllocatable( SCSIZE nC, SCSIZE nR );
165
168 {
170 }
171
174 {
176 }
177
180 {
181 return bool(nType & ScMatValType::NonvalueMask);
182 }
183
188 {
190 }
191
194 {
196 }
197
200 {
202 }
203
205 ScMatrix* Clone() const;
206
209 ScMatrix* CloneIfConst();
210
213 void SetMutable();
214
217 void SetImmutable() const;
218
222 void Resize(SCSIZE nC, SCSIZE nR);
223
224 void Resize(SCSIZE nC, SCSIZE nR, double fVal);
225
228 ScMatrix* CloneAndExtend(SCSIZE nNewCols, SCSIZE nNewRows) const;
229
230 void IncRef() const;
231 void DecRef() const;
232
233 void SetErrorInterpreter( ScInterpreter* p);
234 void GetDimensions( SCSIZE& rC, SCSIZE& rR) const;
235 SCSIZE GetElementCount() const;
236 bool ValidColRow( SCSIZE nC, SCSIZE nR) const;
237
244 bool ValidColRowReplicated( SCSIZE & rC, SCSIZE & rR ) const;
245
252 bool ValidColRowOrReplicated( SCSIZE & rC, SCSIZE & rR ) const;
253
254 void PutDouble( double fVal, SCSIZE nC, SCSIZE nR);
255 void PutDouble( double fVal, SCSIZE nIndex);
256 void PutDouble(const double* pArray, size_t nLen, SCSIZE nC, SCSIZE nR);
257
258 void PutString( const svl::SharedString& rStr, SCSIZE nC, SCSIZE nR) ;
259 void PutString( const svl::SharedString& rStr, SCSIZE nIndex) ;
260 void PutString( const svl::SharedString* pArray, size_t nLen, SCSIZE nC, SCSIZE nR) ;
261
262 void PutEmpty( SCSIZE nC, SCSIZE nR);
263
265 void PutEmptyPath( SCSIZE nC, SCSIZE nR) ;
266 void PutError( FormulaError nErrorCode, SCSIZE nC, SCSIZE nR ) ;
267 void PutBoolean( bool bVal, SCSIZE nC, SCSIZE nR) ;
268
269 void FillDouble( double fVal,
270 SCSIZE nC1, SCSIZE nR1, SCSIZE nC2, SCSIZE nR2 ) ;
271
273 void PutDoubleVector( const ::std::vector< double > & rVec, SCSIZE nC, SCSIZE nR ) ;
274
276 void PutStringVector( const ::std::vector< svl::SharedString > & rVec, SCSIZE nC, SCSIZE nR ) ;
277
279 void PutEmptyVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
280
282 void PutEmptyResultVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
283
285 void PutEmptyPathVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
286
292 FormulaError GetError( SCSIZE nC, SCSIZE nR) const ;
293
297 { return IsValue( nC, nR) ? GetError( nC, nR) : FormulaError::NONE; }
298
300 double GetDouble( SCSIZE nC, SCSIZE nR) const ;
302 double GetDouble( SCSIZE nIndex) const ;
304 double GetDoubleWithStringConversion( SCSIZE nC, SCSIZE nR ) const ;
305
309 svl::SharedString GetString( SCSIZE nIndex) const ;
310
315 svl::SharedString GetString( SvNumberFormatter& rFormatter, SCSIZE nC, SCSIZE nR) const ;
316
319 ScMatrixValue Get( SCSIZE nC, SCSIZE nR) const ;
320
323 bool IsStringOrEmpty( SCSIZE nIndex ) const ;
324
327 bool IsStringOrEmpty( SCSIZE nC, SCSIZE nR ) const ;
328
330 bool IsEmpty( SCSIZE nC, SCSIZE nR ) const ;
331
333 bool IsEmptyCell( SCSIZE nC, SCSIZE nR ) const ;
334
336 bool IsEmptyResult( SCSIZE nC, SCSIZE nR ) const ;
337
339 bool IsEmptyPath( SCSIZE nC, SCSIZE nR ) const ;
340
342 bool IsValue( SCSIZE nIndex ) const ;
343
345 bool IsValue( SCSIZE nC, SCSIZE nR ) const ;
346
348 bool IsValueOrEmpty( SCSIZE nC, SCSIZE nR ) const ;
349
351 bool IsBoolean( SCSIZE nC, SCSIZE nR ) const ;
352
354 bool IsNumeric() const ;
355
356 void MatTrans( const ScMatrix& mRes) const ;
357 void MatCopy ( const ScMatrix& mRes) const ;
358
359 // Convert ScInterpreter::CompareMat values (-1,0,1) to boolean values
360 void CompareEqual() ;
361 void CompareNotEqual() ;
362 void CompareLess() ;
363 void CompareGreater() ;
364 void CompareLessEqual() ;
365 void CompareGreaterEqual() ;
366
367 double And() const ; // logical AND of all matrix values, or NAN
368 double Or() const ; // logical OR of all matrix values, or NAN
369 double Xor() const ; // logical XOR of all matrix values, or NAN
370
371 KahanIterateResult Sum( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
372 KahanIterateResult SumSquare( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
373 DoubleIterateResult Product( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
374 size_t Count(bool bCountStrings, bool bCountErrors, bool bIgnoreEmptyStrings = false) const ;
375 size_t MatchDoubleInColumns(double fValue, size_t nCol1, size_t nCol2) const ;
376 size_t MatchStringInColumns(const svl::SharedString& rStr, size_t nCol1, size_t nCol2) const ;
377
378 double GetMaxValue( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
379 double GetMinValue( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
380 double GetGcd() const ;
381 double GetLcm() const ;
382
383 ScMatrixRef CompareMatrix(
384 sc::Compare& rComp, size_t nMatPos, sc::CompareOptions* pOptions ) const ;
385
394 void GetDoubleArray( std::vector<double>& rArray, bool bEmptyAsZero = true ) const ;
395 void MergeDoubleArrayMultiply( std::vector<double>& rArray ) const ;
396
397 void NotOp(const ScMatrix& rMat) ;
398 void NegOp(const ScMatrix& rMat) ;
399 void AddOp(double fVal, const ScMatrix& rMat) ;
400 void SubOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
401 void MulOp(double fVal, const ScMatrix& rMat) ;
402 void DivOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
403 void PowOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
404
405 KahanIterateResultMultiple CollectKahan(const std::vector<sc::op::kOp>& aOp) ;
406
407 void ExecuteOperation(const std::pair<size_t, size_t>& rStartPos, const std::pair<size_t, size_t>& rEndPos,
408 DoubleOpFunction aDoubleFunc, BoolOpFunction aBoolFunc, StringOpFunction aStringFunc,
409 EmptyOpFunction aEmptyFunc) const ;
410
411 void MatConcat(SCSIZE nMaxCol, SCSIZE nMaxRow, const ScMatrixRef& xMat1, const ScMatrixRef& xMat2,
412 SvNumberFormatter& rFormatter, svl::SharedStringPool& rPool) ;
413
415 void ExecuteBinaryOp(SCSIZE nMaxCol, SCSIZE nMaxRow, const ScMatrix& rInputMat1, const ScMatrix& rInputMat2,
416 ScInterpreter* pInterpreter, CalculateOpFunction op);
417
418#if DEBUG_MATRIX
419 void Dump() const;
420#endif
421};
422
423inline void intrusive_ptr_add_ref(const ScMatrix* p)
424{
425 p->IncRef();
426}
427
428inline void intrusive_ptr_release(const ScMatrix* p)
429{
430 p->DecRef();
431}
432
433/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool ValidColRow(SCCOL nCol, SCROW nRow, SCCOL nMaxCol, SCROW nMaxRow)
Definition: address.hxx:121
size_t SCSIZE
size_t typedef to be able to find places where code was changed from USHORT to size_t and is used to ...
Definition: address.hxx:44
Matrix data type that can store values of mixed types.
Definition: scmatrix.hxx:101
IterateResult< double > DoubleIterateResult
Definition: scmatrix.hxx:157
static bool IsEmptyPathType(ScMatValType nType)
Empty path, but not empty or any other type.
Definition: scmatrix.hxx:199
FormulaError GetErrorIfNotString(SCSIZE nC, SCSIZE nR) const
Use in ScInterpreter to obtain the error code, if any.
Definition: scmatrix.hxx:296
std::function< double(double, double)> CalculateOpFunction
Definition: scmatrix.hxx:121
IterateResult< KahanSum > KahanIterateResult
Definition: scmatrix.hxx:156
std::function< void(size_t, size_t)> EmptyOpFunction
Definition: scmatrix.hxx:120
static bool IsBooleanType(ScMatValType nType)
Boolean.
Definition: scmatrix.hxx:173
IterateResultMultiple< KahanSum > KahanIterateResultMultiple
Definition: scmatrix.hxx:141
std::function< void(size_t, size_t, svl::SharedString)> StringOpFunction
Definition: scmatrix.hxx:119
static bool IsEmptyType(ScMatValType nType)
Empty, but not empty path or any other type.
Definition: scmatrix.hxx:193
static bool IsValueType(ScMatValType nType)
Value or boolean.
Definition: scmatrix.hxx:167
size_t nRefCnt
Definition: scmatrix.hxx:104
std::function< void(size_t, size_t, double)> DoubleOpFunction
Definition: scmatrix.hxx:117
IterateResultMultiple< double > DoubleIterateResultMultiple
Definition: scmatrix.hxx:142
ScMatrix & operator=(const ScMatrix &)=delete
ScMatrix(const ScMatrix &)=delete
bool mbCloneIfConst
Definition: scmatrix.hxx:105
std::function< void(size_t, size_t, bool)> BoolOpFunction
Definition: scmatrix.hxx:118
std::unique_ptr< ScMatrixImpl > pImpl
Definition: scmatrix.hxx:106
static bool IsNonValueType(ScMatValType nType)
String, empty or empty path, but not value nor boolean.
Definition: scmatrix.hxx:179
static bool IsRealStringType(ScMatValType nType)
String, but not empty or empty path or any other type.
Definition: scmatrix.hxx:187
int nCount
std::size_t mnCount
FormulaError
FormulaError GetDoubleErrorValue(double fVal)
void * p
SVXCORE_DLLPUBLIC MSO_SPT Get(const OUString &)
OUString GetString(int nId)
bool IsNumeric(std::u16string_view rText)
static short Compare(const OUString &sInput1, const OUString &sInput2, const bool bCaseSens, const ScUserListData *pData, const CollatorWrapper *pCW)
Naturally compares two given strings.
Definition: table3.cxx:162
CAUTION! The following defines must be in the same namespace as the respective type.
Definition: broadcast.cxx:15
double GetGcd(double f1, double f2)
css::uno::Reference< css::animations::XAnimationNode > Clone(const css::uno::Reference< css::animations::XAnimationNode > &xSourceNode, const SdPage *pSource=nullptr, const SdPage *pTarget=nullptr)
Empty
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:398
#define SC_DLLPUBLIC
Definition: scdllapi.h:27
void intrusive_ptr_add_ref(const ScMatrix *p)
Definition: scmatrix.hxx:423
void intrusive_ptr_release(const ScMatrix *p)
Definition: scmatrix.hxx:428
Try NOT to use this struct.
Definition: scmatrix.hxx:53
ScMatValType nType
Definition: scmatrix.hxx:56
bool operator==(const ScMatrixValue &r) const
Definition: scmatrix.hxx:71
FormulaError GetError() const
Only valid if ScMatrix methods indicate that this is no string!
Definition: scmatrix.hxx:62
ScMatrixValue & operator=(const ScMatrixValue &r)=default
double fVal
Definition: scmatrix.hxx:54
svl::SharedString aStr
Definition: scmatrix.hxx:55
bool operator!=(const ScMatrixValue &r) const
Definition: scmatrix.hxx:89
const svl::SharedString & GetString() const
Only valid if ScMatrix methods indicate so!
Definition: scmatrix.hxx:59
ScMatrixValue(const ScMatrixValue &r)=default
bool GetBoolean() const
Only valid if ScMatrix methods indicate that this is a boolean.
Definition: scmatrix.hxx:65
When adding all numerical matrix elements for a scalar result such as summation, the interpreter want...
Definition: scmatrix.hxx:134
IterateResultMultiple(size_t nCount)
Definition: scmatrix.hxx:138
std::vector< tRes > maAccumulator
Definition: scmatrix.hxx:135
Iterator for executing one operation with the matrix data.
Definition: scmatrix.hxx:149
IterateResult(tRes fAccumulator, size_t nCount)
Definition: scmatrix.hxx:153
@ Sum
@ Product
ScMatValType
Definition: types.hxx:31
::boost::intrusive_ptr< ScMatrix > ScMatrixRef
Definition: types.hxx:25
Count
int GetError()