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 #ifndef INCLUDED_SC_INC_SCMATRIX_HXX
21 #define INCLUDED_SC_INC_SCMATRIX_HXX
22 
23 #include "address.hxx"
24 #include "matrixoperators.hxx"
25 #include "types.hxx"
26 #include <formula/errorcodes.hxx>
27 #include "scdllapi.h"
28 #include <svl/sharedstring.hxx>
29 #include <svl/sharedstringpool.hxx>
30 
31 #include <memory>
32 #include <utility>
33 #include <vector>
34 
35 #define DEBUG_MATRIX 0
36 
37 class ScInterpreter;
38 class SvNumberFormatter;
39 class ScMatrixImpl;
40 enum class FormulaError : sal_uInt16;
41 
42 namespace sc {
43 
44 struct Compare;
45 struct CompareOptions;
46 
47 }
48 
54 {
55  double fVal;
58 
60  const svl::SharedString& GetString() const { return aStr; }
61 
63  FormulaError GetError() const { return GetDoubleErrorValue(fVal); }
64 
66  bool GetBoolean() const { return fVal != 0.0; }
67 
68  ScMatrixValue() : fVal(0.0), nType(ScMatValType::Empty) {}
69 
71  fVal(r.fVal), aStr(r.aStr), nType(r.nType) {}
72 
73  bool operator== (const ScMatrixValue& r) const
74  {
75  if (nType != r.nType)
76  return false;
77 
78  switch (nType)
79  {
82  return fVal == r.fVal;
83  break;
84  default:
85  ;
86  }
87 
88  return aStr == r.aStr;
89  }
90 
91  bool operator!= (const ScMatrixValue& r) const
92  {
93  return !operator==(r);
94  }
95 
97  {
98  if (this == &r)
99  return *this;
100 
101  nType = r.nType;
102  fVal = r.fVal;
103  aStr = r.aStr;
104  return *this;
105  }
106 };
107 
114 {
115  friend class ScMatrixImpl;
116 
117  mutable size_t nRefCnt; // reference count
118  mutable bool mbCloneIfConst; // Whether the matrix is cloned with a CloneIfConst() call.
119  std::unique_ptr<ScMatrixImpl> pImpl;
120 
121  ScMatrix( const ScMatrix& ) = delete;
122  ScMatrix& operator=( const ScMatrix&) = delete;
123 
124 public:
125  ScMatrix(SCSIZE nC, SCSIZE nR);
126  ScMatrix(SCSIZE nC, SCSIZE nR, double fInitVal);
127  ScMatrix( size_t nC, size_t nR, const std::vector<double>& rInitVals );
128  ~ScMatrix();
129 
130  typedef std::function<void(size_t, size_t, double)> DoubleOpFunction;
131  typedef std::function<void(size_t, size_t, bool)> BoolOpFunction;
132  typedef std::function<void(size_t, size_t, svl::SharedString)> StringOpFunction;
133  typedef std::function<void(size_t, size_t)> EmptyOpFunction;
134 
145  {
146  double mfFirst;
147  double mfRest;
148  size_t mnCount;
149 
150  IterateResult(double fFirst, double fRest, size_t nCount) :
151  mfFirst(fFirst), mfRest(fRest), mnCount(nCount) {}
152  };
153 
158  bool static IsSizeAllocatable( SCSIZE nC, SCSIZE nR );
159 
162  {
163  return nType <= ScMatValType::Boolean;
164  }
165 
168  {
169  return nType == ScMatValType::Boolean;
170  }
171 
174  {
175  return bool(nType & ScMatValType::NonvalueMask);
176  }
177 
182  {
184  }
185 
188  {
190  }
191 
194  {
196  }
197 
199  ScMatrix* Clone() const;
200 
203  ScMatrix* CloneIfConst();
204 
207  void SetMutable();
208 
211  void SetImmutable() const;
212 
216  void Resize(SCSIZE nC, SCSIZE nR);
217 
218  void Resize(SCSIZE nC, SCSIZE nR, double fVal);
219 
222  ScMatrix* CloneAndExtend(SCSIZE nNewCols, SCSIZE nNewRows) const;
223 
224  void IncRef() const;
225  void DecRef() const;
226 
227  void SetErrorInterpreter( ScInterpreter* p);
228  void GetDimensions( SCSIZE& rC, SCSIZE& rR) const;
229  SCSIZE GetElementCount() const;
230  bool ValidColRow( SCSIZE nC, SCSIZE nR) const;
231 
238  bool ValidColRowReplicated( SCSIZE & rC, SCSIZE & rR ) const;
239 
246  bool ValidColRowOrReplicated( SCSIZE & rC, SCSIZE & rR ) const;
247 
248  void PutDouble( double fVal, SCSIZE nC, SCSIZE nR);
249  void PutDouble( double fVal, SCSIZE nIndex);
250  void PutDouble(const double* pArray, size_t nLen, SCSIZE nC, SCSIZE nR);
251 
252  void PutString( const svl::SharedString& rStr, SCSIZE nC, SCSIZE nR) ;
253  void PutString( const svl::SharedString& rStr, SCSIZE nIndex) ;
254  void PutString( const svl::SharedString* pArray, size_t nLen, SCSIZE nC, SCSIZE nR) ;
255 
256  void PutEmpty( SCSIZE nC, SCSIZE nR);
257 
259  void PutEmptyPath( SCSIZE nC, SCSIZE nR) ;
260  void PutError( FormulaError nErrorCode, SCSIZE nC, SCSIZE nR ) ;
261  void PutBoolean( bool bVal, SCSIZE nC, SCSIZE nR) ;
262 
263  void FillDouble( double fVal,
264  SCSIZE nC1, SCSIZE nR1, SCSIZE nC2, SCSIZE nR2 ) ;
265 
267  void PutDoubleVector( const ::std::vector< double > & rVec, SCSIZE nC, SCSIZE nR ) ;
268 
270  void PutStringVector( const ::std::vector< svl::SharedString > & rVec, SCSIZE nC, SCSIZE nR ) ;
271 
273  void PutEmptyVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
274 
276  void PutEmptyResultVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
277 
279  void PutEmptyPathVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
280 
286  FormulaError GetError( SCSIZE nC, SCSIZE nR) const ;
287 
291  { return IsValue( nC, nR) ? GetError( nC, nR) : FormulaError::NONE; }
292 
294  double GetDouble( SCSIZE nC, SCSIZE nR) const ;
296  double GetDouble( SCSIZE nIndex) const ;
298  double GetDoubleWithStringConversion( SCSIZE nC, SCSIZE nR ) const ;
299 
301  svl::SharedString GetString( SCSIZE nC, SCSIZE nR) const ;
303  svl::SharedString GetString( SCSIZE nIndex) const ;
304 
309  svl::SharedString GetString( SvNumberFormatter& rFormatter, SCSIZE nC, SCSIZE nR) const ;
310 
313  ScMatrixValue Get( SCSIZE nC, SCSIZE nR) const ;
314 
317  bool IsStringOrEmpty( SCSIZE nIndex ) const ;
318 
321  bool IsStringOrEmpty( SCSIZE nC, SCSIZE nR ) const ;
322 
324  bool IsEmpty( SCSIZE nC, SCSIZE nR ) const ;
325 
327  bool IsEmptyCell( SCSIZE nC, SCSIZE nR ) const ;
328 
330  bool IsEmptyResult( SCSIZE nC, SCSIZE nR ) const ;
331 
333  bool IsEmptyPath( SCSIZE nC, SCSIZE nR ) const ;
334 
336  bool IsValue( SCSIZE nIndex ) const ;
337 
339  bool IsValue( SCSIZE nC, SCSIZE nR ) const ;
340 
342  bool IsValueOrEmpty( SCSIZE nC, SCSIZE nR ) const ;
343 
345  bool IsBoolean( SCSIZE nC, SCSIZE nR ) const ;
346 
348  bool IsNumeric() const ;
349 
350  void MatTrans( const ScMatrix& mRes) const ;
351  void MatCopy ( const ScMatrix& mRes) const ;
352 
353  // Convert ScInterpreter::CompareMat values (-1,0,1) to boolean values
354  void CompareEqual() ;
355  void CompareNotEqual() ;
356  void CompareLess() ;
357  void CompareGreater() ;
358  void CompareLessEqual() ;
359  void CompareGreaterEqual() ;
360 
361  double And() const ; // logical AND of all matrix values, or NAN
362  double Or() const ; // logical OR of all matrix values, or NAN
363  double Xor() const ; // logical XOR of all matrix values, or NAN
364 
365  IterateResult Sum( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
366  IterateResult SumSquare( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
367  IterateResult Product( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
368  size_t Count(bool bCountStrings, bool bCountErrors) const ;
369  size_t MatchDoubleInColumns(double fValue, size_t nCol1, size_t nCol2) const ;
370  size_t MatchStringInColumns(const svl::SharedString& rStr, size_t nCol1, size_t nCol2) const ;
371 
372  double GetMaxValue( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
373  double GetMinValue( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
374  double GetGcd() const ;
375  double GetLcm() const ;
376 
377  ScMatrixRef CompareMatrix(
378  sc::Compare& rComp, size_t nMatPos, sc::CompareOptions* pOptions ) const ;
379 
388  void GetDoubleArray( std::vector<double>& rArray, bool bEmptyAsZero = true ) const ;
389  void MergeDoubleArrayMultiply( std::vector<double>& rArray ) const ;
390 
391  void NotOp(const ScMatrix& rMat) ;
392  void NegOp(const ScMatrix& rMat) ;
393  void AddOp(double fVal, const ScMatrix& rMat) ;
394  void SubOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
395  void MulOp(double fVal, const ScMatrix& rMat) ;
396  void DivOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
397  void PowOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
398 
399  std::vector<ScMatrix::IterateResult> Collect(const std::vector<sc::op::Op>& aOp) ;
400 
401  void ExecuteOperation(const std::pair<size_t, size_t>& rStartPos, const std::pair<size_t, size_t>& rEndPos,
402  DoubleOpFunction aDoubleFunc, BoolOpFunction aBoolFunc, StringOpFunction aStringFunc,
403  EmptyOpFunction aEmptyFunc) const ;
404 
405  void MatConcat(SCSIZE nMaxCol, SCSIZE nMaxRow, const ScMatrixRef& xMat1, const ScMatrixRef& xMat2,
406  SvNumberFormatter& rFormatter, svl::SharedStringPool& rPool) ;
407 
408 #if DEBUG_MATRIX
409  void Dump() const;
410 #endif
411 };
412 
413 inline void intrusive_ptr_add_ref(const ScMatrix* p)
414 {
415  p->IncRef();
416 }
417 
418 inline void intrusive_ptr_release(const ScMatrix* p)
419 {
420  p->DecRef();
421 }
422 
423 #endif // INCLUDED_SC_INC_SCMATRIX_HXX
424 
425 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static bool IsNonValueType(ScMatValType nType)
String, empty or empty path, but not value nor boolean.
Definition: scmatrix.hxx:173
Matrix data type that can store values of mixed types.
Definition: scmatrix.hxx:113
IterateResult(double fFirst, double fRest, size_t nCount)
Definition: scmatrix.hxx:150
ScMatrixValue(const ScMatrixValue &r)
Definition: scmatrix.hxx:70
bool IsNumeric(const OUString &rText)
const ScMatrixImpl & operator=(const ScMatrixImpl &)=delete
FormulaError GetErrorIfNotString(SCSIZE nC, SCSIZE nR) const
Use in ScInterpreter to obtain the error code, if any.
Definition: scmatrix.hxx:290
Try NOT to use this struct.
Definition: scmatrix.hxx:53
double fVal
Definition: scmatrix.hxx:55
void intrusive_ptr_add_ref(const ScMatrix *p)
Definition: scmatrix.hxx:413
bool GetBoolean() const
Only valid if ScMatrix methods indicate that this is a boolean.
Definition: scmatrix.hxx:66
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:45
std::unique_ptr< ScMatrixImpl > pImpl
Definition: scmatrix.hxx:119
::boost::intrusive_ptr< ScMatrix > ScMatrixRef
Definition: types.hxx:26
FormulaError GetDoubleErrorValue(double fVal)
bool operator!=(const ScMatrixValue &r) const
Definition: scmatrix.hxx:91
void DecRef() const
Definition: scmatrix.cxx:2833
std::function< void(size_t, size_t)> EmptyOpFunction
Definition: scmatrix.hxx:133
FormulaError GetError() const
Only valid if ScMatrix methods indicate that this is no string!
Definition: scmatrix.hxx:63
static bool IsEmptyType(ScMatValType nType)
Empty, but not empty path or any other type.
Definition: scmatrix.hxx:187
static bool IsEmptyPathType(ScMatValType nType)
Empty path, but not empty or any other type.
Definition: scmatrix.hxx:193
Reference< XAnimationNode > Clone(const Reference< XAnimationNode > &xSourceNode, const SdPage *pSource, const SdPage *pTarget)
double GetGcd(double f1, double f2)
Count
static bool IsValueType(ScMatValType nType)
Value or boolean.
Definition: scmatrix.hxx:161
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:163
static bool IsBooleanType(ScMatValType nType)
Boolean.
Definition: scmatrix.hxx:167
ScMatValType nType
Definition: scmatrix.hxx:57
ScMatrixValue & operator=(const ScMatrixValue &r)
Definition: scmatrix.hxx:96
FormulaError
size_t nRefCnt
Definition: scmatrix.hxx:117
const svl::SharedString & GetString() const
Only valid if ScMatrix methods indicate so!
Definition: scmatrix.hxx:60
bool operator==(const ScMatrixValue &r) const
Definition: scmatrix.hxx:73
static bool IsRealStringType(ScMatValType nType)
String, but not empty or empty path or any other type.
Definition: scmatrix.hxx:181
void IncRef() const
Definition: scmatrix.cxx:2828
std::function< void(size_t, size_t, double)> DoubleOpFunction
Definition: scmatrix.hxx:130
void intrusive_ptr_release(const ScMatrix *p)
Definition: scmatrix.hxx:418
std::function< void(size_t, size_t, svl::SharedString)> StringOpFunction
Definition: scmatrix.hxx:132
int GetError()
ScMatValType
Definition: types.hxx:32
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:401
When adding all numerical matrix elements for a scalar result such as summation, the interpreter want...
Definition: scmatrix.hxx:144
svl::SharedString aStr
Definition: scmatrix.hxx:56
bool ValidColRow(SCCOL nCol, SCROW nRow, SCCOL nMaxCol, SCROW nMaxRow)
Definition: address.hxx:115
SVXCORE_DLLPUBLIC MSO_SPT Get(const OUString &)
bool mbCloneIfConst
Definition: scmatrix.hxx:118
Empty
#define SC_DLLPUBLIC
Definition: scdllapi.h:28
std::function< void(size_t, size_t, bool)> BoolOpFunction
Definition: scmatrix.hxx:131
OUString GetString(double f, bool bLeadingSign, sal_uInt16 nMaxDig)