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 
143  template<typename tRes>
145  {
146  std::vector<tRes> maAccumulator;
147  size_t mnCount;
148 
149  IterateResultMultiple(size_t nCount) :
150  maAccumulator(0), mnCount(nCount) {}
151  };
154 
158  template<typename tRes>
160  {
162  size_t mnCount;
163 
164  IterateResult(tRes fAccumulator, size_t nCount)
165  : maAccumulator(fAccumulator), mnCount(nCount) {}
166  };
169 
170 
175  bool static IsSizeAllocatable( SCSIZE nC, SCSIZE nR );
176 
179  {
180  return nType <= ScMatValType::Boolean;
181  }
182 
185  {
186  return nType == ScMatValType::Boolean;
187  }
188 
191  {
192  return bool(nType & ScMatValType::NonvalueMask);
193  }
194 
199  {
201  }
202 
205  {
207  }
208 
211  {
213  }
214 
216  ScMatrix* Clone() const;
217 
220  ScMatrix* CloneIfConst();
221 
224  void SetMutable();
225 
228  void SetImmutable() const;
229 
233  void Resize(SCSIZE nC, SCSIZE nR);
234 
235  void Resize(SCSIZE nC, SCSIZE nR, double fVal);
236 
239  ScMatrix* CloneAndExtend(SCSIZE nNewCols, SCSIZE nNewRows) const;
240 
241  void IncRef() const;
242  void DecRef() const;
243 
244  void SetErrorInterpreter( ScInterpreter* p);
245  void GetDimensions( SCSIZE& rC, SCSIZE& rR) const;
246  SCSIZE GetElementCount() const;
247  bool ValidColRow( SCSIZE nC, SCSIZE nR) const;
248 
255  bool ValidColRowReplicated( SCSIZE & rC, SCSIZE & rR ) const;
256 
263  bool ValidColRowOrReplicated( SCSIZE & rC, SCSIZE & rR ) const;
264 
265  void PutDouble( double fVal, SCSIZE nC, SCSIZE nR);
266  void PutDouble( double fVal, SCSIZE nIndex);
267  void PutDouble(const double* pArray, size_t nLen, SCSIZE nC, SCSIZE nR);
268 
269  void PutString( const svl::SharedString& rStr, SCSIZE nC, SCSIZE nR) ;
270  void PutString( const svl::SharedString& rStr, SCSIZE nIndex) ;
271  void PutString( const svl::SharedString* pArray, size_t nLen, SCSIZE nC, SCSIZE nR) ;
272 
273  void PutEmpty( SCSIZE nC, SCSIZE nR);
274 
276  void PutEmptyPath( SCSIZE nC, SCSIZE nR) ;
277  void PutError( FormulaError nErrorCode, SCSIZE nC, SCSIZE nR ) ;
278  void PutBoolean( bool bVal, SCSIZE nC, SCSIZE nR) ;
279 
280  void FillDouble( double fVal,
281  SCSIZE nC1, SCSIZE nR1, SCSIZE nC2, SCSIZE nR2 ) ;
282 
284  void PutDoubleVector( const ::std::vector< double > & rVec, SCSIZE nC, SCSIZE nR ) ;
285 
287  void PutStringVector( const ::std::vector< svl::SharedString > & rVec, SCSIZE nC, SCSIZE nR ) ;
288 
290  void PutEmptyVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
291 
293  void PutEmptyResultVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
294 
296  void PutEmptyPathVector( SCSIZE nCount, SCSIZE nC, SCSIZE nR ) ;
297 
303  FormulaError GetError( SCSIZE nC, SCSIZE nR) const ;
304 
308  { return IsValue( nC, nR) ? GetError( nC, nR) : FormulaError::NONE; }
309 
311  double GetDouble( SCSIZE nC, SCSIZE nR) const ;
313  double GetDouble( SCSIZE nIndex) const ;
315  double GetDoubleWithStringConversion( SCSIZE nC, SCSIZE nR ) const ;
316 
318  svl::SharedString GetString( SCSIZE nC, SCSIZE nR) const ;
320  svl::SharedString GetString( SCSIZE nIndex) const ;
321 
326  svl::SharedString GetString( SvNumberFormatter& rFormatter, SCSIZE nC, SCSIZE nR) const ;
327 
330  ScMatrixValue Get( SCSIZE nC, SCSIZE nR) const ;
331 
334  bool IsStringOrEmpty( SCSIZE nIndex ) const ;
335 
338  bool IsStringOrEmpty( SCSIZE nC, SCSIZE nR ) const ;
339 
341  bool IsEmpty( SCSIZE nC, SCSIZE nR ) const ;
342 
344  bool IsEmptyCell( SCSIZE nC, SCSIZE nR ) const ;
345 
347  bool IsEmptyResult( SCSIZE nC, SCSIZE nR ) const ;
348 
350  bool IsEmptyPath( SCSIZE nC, SCSIZE nR ) const ;
351 
353  bool IsValue( SCSIZE nIndex ) const ;
354 
356  bool IsValue( SCSIZE nC, SCSIZE nR ) const ;
357 
359  bool IsValueOrEmpty( SCSIZE nC, SCSIZE nR ) const ;
360 
362  bool IsBoolean( SCSIZE nC, SCSIZE nR ) const ;
363 
365  bool IsNumeric() const ;
366 
367  void MatTrans( const ScMatrix& mRes) const ;
368  void MatCopy ( const ScMatrix& mRes) const ;
369 
370  // Convert ScInterpreter::CompareMat values (-1,0,1) to boolean values
371  void CompareEqual() ;
372  void CompareNotEqual() ;
373  void CompareLess() ;
374  void CompareGreater() ;
375  void CompareLessEqual() ;
376  void CompareGreaterEqual() ;
377 
378  double And() const ; // logical AND of all matrix values, or NAN
379  double Or() const ; // logical OR of all matrix values, or NAN
380  double Xor() const ; // logical XOR of all matrix values, or NAN
381 
382  KahanIterateResult Sum( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
383  KahanIterateResult SumSquare( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
384  DoubleIterateResult Product( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
385  size_t Count(bool bCountStrings, bool bCountErrors, bool bIgnoreEmptyStrings = false) const ;
386  size_t MatchDoubleInColumns(double fValue, size_t nCol1, size_t nCol2) const ;
387  size_t MatchStringInColumns(const svl::SharedString& rStr, size_t nCol1, size_t nCol2) const ;
388 
389  double GetMaxValue( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
390  double GetMinValue( bool bTextAsZero, bool bIgnoreErrorValues = false ) const ;
391  double GetGcd() const ;
392  double GetLcm() const ;
393 
394  ScMatrixRef CompareMatrix(
395  sc::Compare& rComp, size_t nMatPos, sc::CompareOptions* pOptions ) const ;
396 
405  void GetDoubleArray( std::vector<double>& rArray, bool bEmptyAsZero = true ) const ;
406  void MergeDoubleArrayMultiply( std::vector<double>& rArray ) const ;
407 
408  void NotOp(const ScMatrix& rMat) ;
409  void NegOp(const ScMatrix& rMat) ;
410  void AddOp(double fVal, const ScMatrix& rMat) ;
411  void SubOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
412  void MulOp(double fVal, const ScMatrix& rMat) ;
413  void DivOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
414  void PowOp(bool bFlag, double fVal, const ScMatrix& rMat) ;
415 
416  KahanIterateResultMultiple CollectKahan(const std::vector<sc::op::kOp>& aOp) ;
417 
418  void ExecuteOperation(const std::pair<size_t, size_t>& rStartPos, const std::pair<size_t, size_t>& rEndPos,
419  DoubleOpFunction aDoubleFunc, BoolOpFunction aBoolFunc, StringOpFunction aStringFunc,
420  EmptyOpFunction aEmptyFunc) const ;
421 
422  void MatConcat(SCSIZE nMaxCol, SCSIZE nMaxRow, const ScMatrixRef& xMat1, const ScMatrixRef& xMat2,
423  SvNumberFormatter& rFormatter, svl::SharedStringPool& rPool) ;
424 
425 #if DEBUG_MATRIX
426  void Dump() const;
427 #endif
428 };
429 
430 inline void intrusive_ptr_add_ref(const ScMatrix* p)
431 {
432  p->IncRef();
433 }
434 
435 inline void intrusive_ptr_release(const ScMatrix* p)
436 {
437  p->DecRef();
438 }
439 
440 /* 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:190
Matrix data type that can store values of mixed types.
Definition: scmatrix.hxx:112
IterateResultMultiple(size_t nCount)
Definition: scmatrix.hxx:149
std::vector< tRes > maAccumulator
Definition: scmatrix.hxx:146
ScMatrixValue(const ScMatrixValue &r)
Definition: scmatrix.hxx:69
bool IsNumeric(const OUString &rText)
IterateResult< KahanSum > KahanIterateResult
Definition: scmatrix.hxx:167
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:307
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:430
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:2794
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:204
static bool IsEmptyPathType(ScMatValType nType)
Empty path, but not empty or any other type.
Definition: scmatrix.hxx:210
Reference< XAnimationNode > Clone(const Reference< XAnimationNode > &xSourceNode, const SdPage *pSource, const SdPage *pTarget)
IterateResult(tRes fAccumulator, size_t nCount)
Definition: scmatrix.hxx:164
double GetGcd(double f1, double f2)
Count
static bool IsValueType(ScMatValType nType)
Value or boolean.
Definition: scmatrix.hxx:178
IterateResult< double > DoubleIterateResult
Definition: scmatrix.hxx:168
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:166
static bool IsBooleanType(ScMatValType nType)
Boolean.
Definition: scmatrix.hxx:184
ScMatValType nType
Definition: scmatrix.hxx:56
ScMatrixValue & operator=(const ScMatrixValue &r)
Definition: scmatrix.hxx:95
FormulaError
size_t nRefCnt
Definition: scmatrix.hxx:116
IterateResultMultiple< double > DoubleIterateResultMultiple
Definition: scmatrix.hxx:153
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:198
void IncRef() const
Definition: scmatrix.cxx:2789
std::function< void(size_t, size_t, double)> DoubleOpFunction
Definition: scmatrix.hxx:129
IterateResultMultiple< KahanSum > KahanIterateResultMultiple
Definition: scmatrix.hxx:152
void intrusive_ptr_release(const ScMatrix *p)
Definition: scmatrix.hxx:435
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
Iterator for executing one operation with the matrix data.
Definition: scmatrix.hxx:159
svl::SharedString aStr
Definition: scmatrix.hxx:55
bool ValidColRow(SCCOL nCol, SCROW nRow, SCCOL nMaxCol, SCROW nMaxRow)
Definition: address.hxx:114
When adding all numerical matrix elements for a scalar result such as summation, the interpreter want...
Definition: scmatrix.hxx:144
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