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
131 template<typename tRes>
133 {
134 std::vector<tRes> maAccumulator;
135 size_t mnCount;
136
137 IterateResultMultiple(size_t nCount) :
138 maAccumulator(0), mnCount(nCount) {}
139 };
142
146 template<typename tRes>
148 {
150 size_t mnCount;
151
152 IterateResult(tRes fAccumulator, size_t nCount)
153 : maAccumulator(fAccumulator), mnCount(nCount) {}
154 };
157
158
163 bool static IsSizeAllocatable( SCSIZE nC, SCSIZE nR );
164
167 {
169 }
170
173 {
175 }
176
179 {
180 return bool(nType & ScMatValType::NonvalueMask);
181 }
182
187 {
189 }
190
193 {
195 }
196
199 {
201 }
202
204 ScMatrix* Clone() const;
205
208 ScMatrix* CloneIfConst();
209
212 void SetMutable();
213
216 void SetImmutable() const;
217
221 void Resize(SCSIZE nC, SCSIZE nR);
222
223 void Resize(SCSIZE nC, SCSIZE nR, double fVal);
224
227 ScMatrix* CloneAndExtend(SCSIZE nNewCols, SCSIZE nNewRows) const;
228
229 void IncRef() const;
230 void DecRef() const;
231
232 void SetErrorInterpreter( ScInterpreter* p);
233 void GetDimensions( SCSIZE& rC, SCSIZE& rR) const;
234 SCSIZE GetElementCount() const;
235 bool ValidColRow( SCSIZE nC, SCSIZE nR) const;
236
243 bool ValidColRowReplicated( SCSIZE & rC, SCSIZE & rR ) const;
244
251 bool ValidColRowOrReplicated( SCSIZE & rC, SCSIZE & rR ) const;
252
253 void PutDouble( double fVal, SCSIZE nC, SCSIZE nR);
254 void PutDouble( double fVal, SCSIZE nIndex);
255 void PutDouble(const double* pArray, size_t nLen, SCSIZE nC, SCSIZE nR);
256
257 void PutString( const svl::SharedString& rStr, SCSIZE nC, SCSIZE nR) ;
258 void PutString( const svl::SharedString& rStr, SCSIZE nIndex) ;
259 void PutString( const svl::SharedString* pArray, size_t nLen, SCSIZE nC, SCSIZE nR) ;
260
261 void PutEmpty( SCSIZE nC, SCSIZE nR);
262
264 void PutEmptyPath( SCSIZE nC, SCSIZE nR) ;
265 void PutError( FormulaError nErrorCode, SCSIZE nC, SCSIZE nR ) ;
266 void PutBoolean( bool bVal, SCSIZE nC, SCSIZE nR) ;
267
268 void FillDouble( double fVal,
269 SCSIZE nC1, SCSIZE nR1, SCSIZE nC2, SCSIZE nR2 ) ;
270
272 void PutDoubleVector( const ::std::vector< double > & rVec, SCSIZE nC, SCSIZE nR ) ;
273
275 void PutStringVector( const ::std::vector< svl::SharedString > & rVec, SCSIZE nC, SCSIZE nR ) ;
276
278 void PutEmptyVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
279
281 void PutEmptyResultVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
282
284 void PutEmptyPathVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
285
291 FormulaError GetError( SCSIZE nC, SCSIZE nR) const ;
292
296 { return IsValue( nC, nR) ? GetError( nC, nR) : FormulaError::NONE; }
297
299 double GetDouble( SCSIZE nC, SCSIZE nR) const ;
301 double GetDouble( SCSIZE nIndex) const ;
303 double GetDoubleWithStringConversion( SCSIZE nC, SCSIZE nR ) const ;
304
308 svl::SharedString GetString( SCSIZE nIndex) const ;
309
314 svl::SharedString GetString( SvNumberFormatter& rFormatter, SCSIZE nC, SCSIZE nR) const ;
315
318 ScMatrixValue Get( SCSIZE nC, SCSIZE nR) const ;
319
322 bool IsStringOrEmpty( SCSIZE nIndex ) const ;
323
326 bool IsStringOrEmpty( SCSIZE nC, SCSIZE nR ) const ;
327
329 bool IsEmpty( SCSIZE nC, SCSIZE nR ) const ;
330
332 bool IsEmptyCell( SCSIZE nC, SCSIZE nR ) const ;
333
335 bool IsEmptyResult( SCSIZE nC, SCSIZE nR ) const ;
336
338 bool IsEmptyPath( SCSIZE nC, SCSIZE nR ) const ;
339
341 bool IsValue( SCSIZE nIndex ) const ;
342
344 bool IsValue( SCSIZE nC, SCSIZE nR ) const ;
345
347 bool IsValueOrEmpty( SCSIZE nC, SCSIZE nR ) const ;
348
350 bool IsBoolean( SCSIZE nC, SCSIZE nR ) const ;
351
353 bool IsNumeric() const ;
354
355 void MatTrans( const ScMatrix& mRes) const ;
356 void MatCopy ( const ScMatrix& mRes) const ;
357
358 // Convert ScInterpreter::CompareMat values (-1,0,1) to boolean values
359 void CompareEqual() ;
360 void CompareNotEqual() ;
361 void CompareLess() ;
362 void CompareGreater() ;
363 void CompareLessEqual() ;
364 void CompareGreaterEqual() ;
365
366 double And() const ; // logical AND of all matrix values, or NAN
367 double Or() const ; // logical OR of all matrix values, or NAN
368 double Xor() const ; // logical XOR of all matrix values, or NAN
369
370 KahanIterateResult Sum( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
371 KahanIterateResult SumSquare( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
372 DoubleIterateResult Product( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
373 size_t Count(bool bCountStrings, bool bCountErrors, bool bIgnoreEmptyStrings = false) const ;
374 size_t MatchDoubleInColumns(double fValue, size_t nCol1, size_t nCol2) const ;
375 size_t MatchStringInColumns(const svl::SharedString& rStr, size_t nCol1, size_t nCol2) const ;
376
377 double GetMaxValue( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
378 double GetMinValue( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
379 double GetGcd() const ;
380 double GetLcm() const ;
381
382 ScMatrixRef CompareMatrix(
383 sc::Compare& rComp, size_t nMatPos, sc::CompareOptions* pOptions ) const ;
384
393 void GetDoubleArray( std::vector<double>& rArray, bool bEmptyAsZero = true ) const ;
394 void MergeDoubleArrayMultiply( std::vector<double>& rArray ) const ;
395
396 void NotOp(const ScMatrix& rMat) ;
397 void NegOp(const ScMatrix& rMat) ;
398 void AddOp(double fVal, const ScMatrix& rMat) ;
399 void SubOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
400 void MulOp(double fVal, const ScMatrix& rMat) ;
401 void DivOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
402 void PowOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
403
404 KahanIterateResultMultiple CollectKahan(const std::vector<sc::op::kOp>& aOp) ;
405
406 void ExecuteOperation(const std::pair<size_t, size_t>& rStartPos, const std::pair<size_t, size_t>& rEndPos,
407 DoubleOpFunction aDoubleFunc, BoolOpFunction aBoolFunc, StringOpFunction aStringFunc,
408 EmptyOpFunction aEmptyFunc) const ;
409
410 void MatConcat(SCSIZE nMaxCol, SCSIZE nMaxRow, const ScMatrixRef& xMat1, const ScMatrixRef& xMat2,
411 SvNumberFormatter& rFormatter, svl::SharedStringPool& rPool) ;
412
413#if DEBUG_MATRIX
414 void Dump() const;
415#endif
416};
417
418inline void intrusive_ptr_add_ref(const ScMatrix* p)
419{
420 p->IncRef();
421}
422
423inline void intrusive_ptr_release(const ScMatrix* p)
424{
425 p->DecRef();
426}
427
428/* 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:156
static bool IsEmptyPathType(ScMatValType nType)
Empty path, but not empty or any other type.
Definition: scmatrix.hxx:198
FormulaError GetErrorIfNotString(SCSIZE nC, SCSIZE nR) const
Use in ScInterpreter to obtain the error code, if any.
Definition: scmatrix.hxx:295
IterateResult< KahanSum > KahanIterateResult
Definition: scmatrix.hxx:155
std::function< void(size_t, size_t)> EmptyOpFunction
Definition: scmatrix.hxx:120
static bool IsBooleanType(ScMatValType nType)
Boolean.
Definition: scmatrix.hxx:172
IterateResultMultiple< KahanSum > KahanIterateResultMultiple
Definition: scmatrix.hxx:140
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:192
static bool IsValueType(ScMatValType nType)
Value or boolean.
Definition: scmatrix.hxx:166
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:141
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:178
static bool IsRealStringType(ScMatValType nType)
String, but not empty or empty path or any other type.
Definition: scmatrix.hxx:186
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
Count
CAUTION! The following defines must be in the same namespace as the respective type.
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:418
void intrusive_ptr_release(const ScMatrix *p)
Definition: scmatrix.hxx:423
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:133
IterateResultMultiple(size_t nCount)
Definition: scmatrix.hxx:137
std::vector< tRes > maAccumulator
Definition: scmatrix.hxx:134
Iterator for executing one operation with the matrix data.
Definition: scmatrix.hxx:148
IterateResult(tRes fAccumulator, size_t nCount)
Definition: scmatrix.hxx:152
@ Sum
@ Product
ScMatValType
Definition: types.hxx:31
::boost::intrusive_ptr< ScMatrix > ScMatrixRef
Definition: types.hxx:25
int GetError()