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"
25 #include <formula/errorcodes.hxx>
26 #include "scdllapi.h"
27 #include <svl/sharedstring.hxx>
28 #include <svl/sharedstringpool.hxx>
29 
30 #include <memory>
31 #include <utility>
32 #include <vector>
33 
34 #define DEBUG_MATRIX 0
35 
36 class ScInterpreter;
37 class SvNumberFormatter;
38 class ScMatrixImpl;
39 enum class FormulaError : sal_uInt16;
40 
41 namespace sc {
42 
43 struct Compare;
44 struct CompareOptions;
45 
46 }
47 
53 {
54  double fVal;
57 
59  const svl::SharedString& GetString() const { return aStr; }
60 
62  FormulaError GetError() const { return GetDoubleErrorValue(fVal); }
63 
65  bool GetBoolean() const { return fVal != 0.0; }
66 
67  ScMatrixValue() : fVal(0.0), nType(ScMatValType::Empty) {}
68 
70  fVal(r.fVal), aStr(r.aStr), nType(r.nType) {}
71 
72  bool operator== (const ScMatrixValue& r) const
73  {
74  if (nType != r.nType)
75  return false;
76 
77  switch (nType)
78  {
81  return fVal == r.fVal;
82  break;
83  default:
84  ;
85  }
86 
87  return aStr == r.aStr;
88  }
89 
90  bool operator!= (const ScMatrixValue& r) const
91  {
92  return !operator==(r);
93  }
94 
96  {
97  if (this == &r)
98  return *this;
99 
100  nType = r.nType;
101  fVal = r.fVal;
102  aStr = r.aStr;
103  return *this;
104  }
105 };
106 
113 {
114  friend class ScMatrixImpl;
115 
116  mutable size_t nRefCnt; // reference count
117  mutable bool mbCloneIfConst; // Whether the matrix is cloned with a CloneIfConst() call.
118  std::unique_ptr<ScMatrixImpl> pImpl;
119 
120  ScMatrix( const ScMatrix& ) = delete;
121  ScMatrix& operator=( const ScMatrix&) = delete;
122 
123 public:
124  ScMatrix(SCSIZE nC, SCSIZE nR);
125  ScMatrix(SCSIZE nC, SCSIZE nR, double fInitVal);
126  ScMatrix( size_t nC, size_t nR, const std::vector<double>& rInitVals );
127  ~ScMatrix();
128 
129  typedef std::function<void(size_t, size_t, double)> DoubleOpFunction;
130  typedef std::function<void(size_t, size_t, bool)> BoolOpFunction;
131  typedef std::function<void(size_t, size_t, svl::SharedString)> StringOpFunction;
132  typedef std::function<void(size_t, size_t)> EmptyOpFunction;
133 
144  {
145  double mfFirst;
146  double mfRest;
147  size_t mnCount;
148 
149  IterateResult(double fFirst, double fRest, size_t nCount) :
150  mfFirst(fFirst), mfRest(fRest), mnCount(nCount) {}
151  };
152 
157  bool static IsSizeAllocatable( SCSIZE nC, SCSIZE nR );
158 
161  {
162  return nType <= ScMatValType::Boolean;
163  }
164 
167  {
168  return nType == ScMatValType::Boolean;
169  }
170 
173  {
174  return bool(nType & ScMatValType::NonvalueMask);
175  }
176 
181  {
183  }
184 
187  {
189  }
190 
193  {
195  }
196 
198  ScMatrix* Clone() const;
199 
202  ScMatrix* CloneIfConst();
203 
206  void SetMutable();
207 
210  void SetImmutable() const;
211 
215  void Resize(SCSIZE nC, SCSIZE nR);
216 
217  void Resize(SCSIZE nC, SCSIZE nR, double fVal);
218 
221  ScMatrix* CloneAndExtend(SCSIZE nNewCols, SCSIZE nNewRows) const;
222 
223  void IncRef() const;
224  void DecRef() const;
225 
226  void SetErrorInterpreter( ScInterpreter* p);
227  void GetDimensions( SCSIZE& rC, SCSIZE& rR) const;
228  SCSIZE GetElementCount() const;
229  bool ValidColRow( SCSIZE nC, SCSIZE nR) const;
230 
237  bool ValidColRowReplicated( SCSIZE & rC, SCSIZE & rR ) const;
238 
245  bool ValidColRowOrReplicated( SCSIZE & rC, SCSIZE & rR ) const;
246 
247  void PutDouble( double fVal, SCSIZE nC, SCSIZE nR);
248  void PutDouble( double fVal, SCSIZE nIndex);
249  void PutDouble(const double* pArray, size_t nLen, SCSIZE nC, SCSIZE nR);
250 
251  void PutString( const svl::SharedString& rStr, SCSIZE nC, SCSIZE nR) ;
252  void PutString( const svl::SharedString& rStr, SCSIZE nIndex) ;
253  void PutString( const svl::SharedString* pArray, size_t nLen, SCSIZE nC, SCSIZE nR) ;
254 
255  void PutEmpty( SCSIZE nC, SCSIZE nR);
256 
258  void PutEmptyPath( SCSIZE nC, SCSIZE nR) ;
259  void PutError( FormulaError nErrorCode, SCSIZE nC, SCSIZE nR ) ;
260  void PutBoolean( bool bVal, SCSIZE nC, SCSIZE nR) ;
261 
262  void FillDouble( double fVal,
263  SCSIZE nC1, SCSIZE nR1, SCSIZE nC2, SCSIZE nR2 ) ;
264 
266  void PutDoubleVector( const ::std::vector< double > & rVec, SCSIZE nC, SCSIZE nR ) ;
267 
269  void PutStringVector( const ::std::vector< svl::SharedString > & rVec, SCSIZE nC, SCSIZE nR ) ;
270 
272  void PutEmptyVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
273 
275  void PutEmptyResultVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
276 
278  void PutEmptyPathVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
279 
285  FormulaError GetError( SCSIZE nC, SCSIZE nR) const ;
286 
290  { return IsValue( nC, nR) ? GetError( nC, nR) : FormulaError::NONE; }
291 
293  double GetDouble( SCSIZE nC, SCSIZE nR) const ;
295  double GetDouble( SCSIZE nIndex) const ;
297  double GetDoubleWithStringConversion( SCSIZE nC, SCSIZE nR ) const ;
298 
300  svl::SharedString GetString( SCSIZE nC, SCSIZE nR) const ;
302  svl::SharedString GetString( SCSIZE nIndex) const ;
303 
308  svl::SharedString GetString( SvNumberFormatter& rFormatter, SCSIZE nC, SCSIZE nR) const ;
309 
312  ScMatrixValue Get( SCSIZE nC, SCSIZE nR) const ;
313 
316  bool IsStringOrEmpty( SCSIZE nIndex ) const ;
317 
320  bool IsStringOrEmpty( SCSIZE nC, SCSIZE nR ) const ;
321 
323  bool IsEmpty( SCSIZE nC, SCSIZE nR ) const ;
324 
326  bool IsEmptyCell( SCSIZE nC, SCSIZE nR ) const ;
327 
329  bool IsEmptyResult( SCSIZE nC, SCSIZE nR ) const ;
330 
332  bool IsEmptyPath( SCSIZE nC, SCSIZE nR ) const ;
333 
335  bool IsValue( SCSIZE nIndex ) const ;
336 
338  bool IsValue( SCSIZE nC, SCSIZE nR ) const ;
339 
341  bool IsValueOrEmpty( SCSIZE nC, SCSIZE nR ) const ;
342 
344  bool IsBoolean( SCSIZE nC, SCSIZE nR ) const ;
345 
347  bool IsNumeric() const ;
348 
349  void MatTrans( const ScMatrix& mRes) const ;
350  void MatCopy ( const ScMatrix& mRes) const ;
351 
352  // Convert ScInterpreter::CompareMat values (-1,0,1) to boolean values
353  void CompareEqual() ;
354  void CompareNotEqual() ;
355  void CompareLess() ;
356  void CompareGreater() ;
357  void CompareLessEqual() ;
358  void CompareGreaterEqual() ;
359 
360  double And() const ; // logical AND of all matrix values, or NAN
361  double Or() const ; // logical OR of all matrix values, or NAN
362  double Xor() const ; // logical XOR of all matrix values, or NAN
363 
364  IterateResult Sum( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
365  IterateResult SumSquare( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
366  IterateResult Product( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
367  size_t Count(bool bCountStrings, bool bCountErrors, bool bIgnoreEmptyStrings = false) const ;
368  size_t MatchDoubleInColumns(double fValue, size_t nCol1, size_t nCol2) const ;
369  size_t MatchStringInColumns(const svl::SharedString& rStr, size_t nCol1, size_t nCol2) const ;
370 
371  double GetMaxValue( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
372  double GetMinValue( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
373  double GetGcd() const ;
374  double GetLcm() const ;
375 
376  ScMatrixRef CompareMatrix(
377  sc::Compare& rComp, size_t nMatPos, sc::CompareOptions* pOptions ) const ;
378 
387  void GetDoubleArray( std::vector<double>& rArray, bool bEmptyAsZero = true ) const ;
388  void MergeDoubleArrayMultiply( std::vector<double>& rArray ) const ;
389 
390  void NotOp(const ScMatrix& rMat) ;
391  void NegOp(const ScMatrix& rMat) ;
392  void AddOp(double fVal, const ScMatrix& rMat) ;
393  void SubOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
394  void MulOp(double fVal, const ScMatrix& rMat) ;
395  void DivOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
396  void PowOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
397 
398  std::vector<ScMatrix::IterateResult> Collect(const std::vector<sc::op::Op>& aOp) ;
399 
400  void ExecuteOperation(const std::pair<size_t, size_t>& rStartPos, const std::pair<size_t, size_t>& rEndPos,
401  DoubleOpFunction aDoubleFunc, BoolOpFunction aBoolFunc, StringOpFunction aStringFunc,
402  EmptyOpFunction aEmptyFunc) const ;
403 
404  void MatConcat(SCSIZE nMaxCol, SCSIZE nMaxRow, const ScMatrixRef& xMat1, const ScMatrixRef& xMat2,
405  SvNumberFormatter& rFormatter, svl::SharedStringPool& rPool) ;
406 
407 #if DEBUG_MATRIX
408  void Dump() const;
409 #endif
410 };
411 
412 inline void intrusive_ptr_add_ref(const ScMatrix* p)
413 {
414  p->IncRef();
415 }
416 
417 inline void intrusive_ptr_release(const ScMatrix* p)
418 {
419  p->DecRef();
420 }
421 
422 /* 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:172
Matrix data type that can store values of mixed types.
Definition: scmatrix.hxx:112
IterateResult(double fFirst, double fRest, size_t nCount)
Definition: scmatrix.hxx:149
ScMatrixValue(const ScMatrixValue &r)
Definition: scmatrix.hxx:69
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:289
Try NOT to use this struct.
Definition: scmatrix.hxx:52
OUString GetString(int nId)
double fVal
Definition: scmatrix.hxx:54
void intrusive_ptr_add_ref(const ScMatrix *p)
Definition: scmatrix.hxx:412
bool GetBoolean() const
Only valid if ScMatrix methods indicate that this is a boolean.
Definition: scmatrix.hxx:65
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
std::unique_ptr< ScMatrixImpl > pImpl
Definition: scmatrix.hxx:118
::boost::intrusive_ptr< ScMatrix > ScMatrixRef
Definition: types.hxx:25
FormulaError GetDoubleErrorValue(double fVal)
bool operator!=(const ScMatrixValue &r) const
Definition: scmatrix.hxx:90
void DecRef() const
Definition: scmatrix.cxx:2839
std::function< void(size_t, size_t)> EmptyOpFunction
Definition: scmatrix.hxx:132
FormulaError GetError() const
Only valid if ScMatrix methods indicate that this is no string!
Definition: scmatrix.hxx:62
static bool IsEmptyType(ScMatValType nType)
Empty, but not empty path or any other type.
Definition: scmatrix.hxx:186
static bool IsEmptyPathType(ScMatValType nType)
Empty path, but not empty or any other type.
Definition: scmatrix.hxx:192
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:160
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:164
static bool IsBooleanType(ScMatValType nType)
Boolean.
Definition: scmatrix.hxx:166
ScMatValType nType
Definition: scmatrix.hxx:56
ScMatrixValue & operator=(const ScMatrixValue &r)
Definition: scmatrix.hxx:95
FormulaError
size_t nRefCnt
Definition: scmatrix.hxx:116
const svl::SharedString & GetString() const
Only valid if ScMatrix methods indicate so!
Definition: scmatrix.hxx:59
bool operator==(const ScMatrixValue &r) const
Definition: scmatrix.hxx:72
static bool IsRealStringType(ScMatValType nType)
String, but not empty or empty path or any other type.
Definition: scmatrix.hxx:180
void IncRef() const
Definition: scmatrix.cxx:2834
std::function< void(size_t, size_t, double)> DoubleOpFunction
Definition: scmatrix.hxx:129
void intrusive_ptr_release(const ScMatrix *p)
Definition: scmatrix.hxx:417
std::function< void(size_t, size_t, svl::SharedString)> StringOpFunction
Definition: scmatrix.hxx:131
int GetError()
ScMatValType
Definition: types.hxx:31
QPRO_FUNC_TYPE nType
Definition: qproform.cxx:400
When adding all numerical matrix elements for a scalar result such as summation, the interpreter want...
Definition: scmatrix.hxx:143
svl::SharedString aStr
Definition: scmatrix.hxx:55
bool ValidColRow(SCCOL nCol, SCROW nRow, SCCOL nMaxCol, SCROW nMaxRow)
Definition: address.hxx:114
SVXCORE_DLLPUBLIC MSO_SPT Get(const OUString &)
bool mbCloneIfConst
Definition: scmatrix.hxx:117
Empty
#define SC_DLLPUBLIC
Definition: scdllapi.h:27
std::function< void(size_t, size_t, bool)> BoolOpFunction
Definition: scmatrix.hxx:130