xref: /trunk/main/sc/source/core/inc/jumpmatrix.hxx (revision 1354dfcdad4129e7cc561fdcfbb4f6c8958f0428)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef SC_JUMPMATRIX_HXX
25 #define SC_JUMPMATRIX_HXX
26 
27 #include "formula/token.hxx"
28 #include "formula/errorcodes.hxx"
29 #include <tools/solar.h>
30 #include <vector>
31 #include "scmatrix.hxx"
32 
33 typedef ::std::vector< formula::FormulaToken*> ScTokenVec;
34 
35 struct ScJumpMatrixEntry
36 {
37     double  fBool;      // 0:= false  1:= true   also if no-path
38                         // other values may contain error conditions like NAN and INF
39     short   nStart;     // start of path (actually start-1, see formula::FormulaTokenIterator)
40     short   nNext;      // next after path
41                         // jump path exists if nStart != nNext, else no path
42     short   nStop;      // optional stop of path (nPC < nStop)
43 
SetJumpScJumpMatrixEntry44     void    SetJump( double fBoolP, short nStartP, short nNextP, short nStopP )
45                 {
46                     fBool = fBoolP;
47                     nStart = nStartP;
48                     nNext = nNextP;
49                     nStop = nStopP;
50                 }
GetJumpScJumpMatrixEntry51     void    GetJump( double& rBool, short& rStart, short& rNext, short& rStop )
52                 {
53                     rBool = fBool;
54                     rStart = nStart;
55                     rNext = nNext;
56                     rStop = nStop;
57                 }
58 };
59 
60 class ScJumpMatrix
61 {
62             ScJumpMatrixEntry*  pJump;      // the jumps
63             ScMatrixRef         pMat;       // the results
64             ScTokenVec*         pParams;    // parameter stack
65             SCSIZE              nCols;
66             SCSIZE              nRows;
67             SCSIZE              nCurCol;
68             SCSIZE              nCurRow;
69             SCSIZE              nResMatCols;
70             SCSIZE              nResMatRows;
71             bool                bStarted;
72             bool                bKeepNoPath;
73 
74                                 // not implemented, prevent usage
75                                 ScJumpMatrix( const ScJumpMatrix& );
76             ScJumpMatrix&       operator=( const ScJumpMatrix& );
77 
78 public:
ScJumpMatrix(SCSIZE nColsP,SCSIZE nRowsP)79                                 ScJumpMatrix( SCSIZE nColsP, SCSIZE nRowsP )
80                                         : pJump( new ScJumpMatrixEntry[ nColsP * nRowsP ] )
81                                         , pMat( new ScMatrix( nColsP, nRowsP) )
82                                         , pParams( NULL )
83                                         , nCols( nColsP )
84                                         , nRows( nRowsP )
85                                         , nCurCol( 0 )
86                                         , nCurRow( 0 )
87                                         , nResMatCols( nColsP )
88                                         , nResMatRows( nRowsP )
89                                         , bStarted( false )
90                                         , bKeepNoPath( false )
91                                     {
92                                         // Initialize result matrix in case of
93                                         // a premature end of the interpreter
94                                         // due to errors.
95                                         pMat->FillDouble( CreateDoubleError(
96                                                 NOTAVAILABLE), 0, 0, nCols-1,
97                                                 nRows-1);
98                                         /*! pJump not initialized */
99                                     }
~ScJumpMatrix()100                                 ~ScJumpMatrix()
101                                     {
102                                         if ( pParams )
103                                         {
104                                             for ( ScTokenVec::iterator i =
105                                                     pParams->begin(); i !=
106                                                     pParams->end(); ++i )
107                                             {
108                                                 (*i)->DecRef();
109                                             }
110                                             delete pParams;
111                                         }
112                                         delete [] pJump;
113                                     }
GetDimensions(SCSIZE & rCols,SCSIZE & rRows) const114             void                GetDimensions( SCSIZE& rCols, SCSIZE& rRows ) const
115                                     {
116                                         rCols = nCols;
117                                         rRows = nRows;
118                                     }
SetJump(SCSIZE nCol,SCSIZE nRow,double fBool,short nStart,short nNext,short nStop=SHRT_MAX)119             void                SetJump( SCSIZE nCol, SCSIZE nRow, double fBool,
120                                             short nStart, short nNext,
121                                             short nStop = SHRT_MAX )
122                                     {
123                                         pJump[ (sal_uLong)nCol * nRows + nRow ].
124                                             SetJump( fBool, nStart, nNext, nStop);
125                                     }
GetJump(SCSIZE nCol,SCSIZE nRow,double & rBool,short & rStart,short & rNext,short & rStop) const126             void                GetJump( SCSIZE nCol, SCSIZE nRow, double& rBool,
127                                             short& rStart, short& rNext,
128                                             short& rStop ) const
129                                     {
130                                         if (nCols == 1 && nRows == 1)
131                                         {
132                                             nCol = 0;
133                                             nRow = 0;
134                                         }
135                                         else if (nCols == 1 && nRow < nRows)
136                                             nCol = 0;
137                                         else if (nRows == 1 && nCol < nCols)
138                                             nRow = 0;
139                                         else if (nCols <= nCol || nRows <= nRow)
140                                         {
141                                             DBG_ERROR("ScJumpMatrix::GetJump: dimension error");
142                                             nCol = 0;
143                                             nRow = 0;
144                                         }
145                                         pJump[ (sal_uLong)nCol * nRows + nRow ].
146                                             GetJump( rBool, rStart, rNext, rStop);
147                                     }
SetAllJumps(double fBool,short nStart,short nNext,short nStop=SHRT_MAX)148             void                SetAllJumps( double fBool,
149                                             short nStart, short nNext,
150                                             short nStop = SHRT_MAX )
151                                     {
152                                         sal_uLong n = (sal_uLong)nCols * nRows;
153                                         for ( sal_uLong j=0; j<n; ++j )
154                                         {
155                                             pJump[ j ].SetJump( fBool, nStart,
156                                                     nNext, nStop);
157                                         }
158                                     }
159             /// Elements without a jump path keep the value already in the result matrix.
SetKeepNoPath()160             void                SetKeepNoPath() { bKeepNoPath = true; }
IsKeepNoPath() const161             bool                IsKeepNoPath() const { return bKeepNoPath; }
SetJumpParameters(ScTokenVec * p)162             void                SetJumpParameters( ScTokenVec* p )
163                                     { pParams = p; }
GetJumpParameters() const164             const ScTokenVec*   GetJumpParameters() const { return pParams; }
GetResultMatrix() const165             ScMatrix*           GetResultMatrix() const { return pMat; }
GetPos(SCSIZE & rCol,SCSIZE & rRow) const166             void                GetPos( SCSIZE& rCol, SCSIZE& rRow ) const
167                                     {
168                                         rCol = nCurCol;
169                                         rRow = nCurRow;
170                                     }
Next(SCSIZE & rCol,SCSIZE & rRow)171             bool                Next( SCSIZE& rCol, SCSIZE& rRow )
172                                     {
173                                         if ( !bStarted )
174                                         {
175                                             bStarted = true;
176                                             nCurCol = nCurRow = 0;
177                                         }
178                                         else
179                                         {
180                                             if ( ++nCurRow >= nResMatRows )
181                                             {
182                                                 nCurRow = 0;
183                                                 ++nCurCol;
184                                             }
185                                         }
186                                         GetPos( rCol, rRow );
187                                         return nCurCol < nResMatCols;
188                                     }
GetResMatDimensions(SCSIZE & rCols,SCSIZE & rRows)189             void                GetResMatDimensions( SCSIZE& rCols, SCSIZE& rRows )
190                                     {
191                                         rCols = nResMatCols;
192                                         rRows = nResMatRows;
193                                     }
SetNewResMat(SCSIZE nNewCols,SCSIZE nNewRows)194             void                SetNewResMat( SCSIZE nNewCols, SCSIZE nNewRows )
195                                     {
196                                         if ( nNewCols > nResMatCols || nNewRows > nResMatRows )
197                                         {
198                                             pMat = pMat->CloneAndExtend( nNewCols, nNewRows );
199                                             if ( nResMatCols < nNewCols )
200                                             {
201                                                 pMat->FillDouble( CreateDoubleError(
202                                                     NOTAVAILABLE), nResMatCols, 0, nNewCols-1,
203                                                     nResMatRows-1);
204                                             }
205                                             if ( nResMatRows < nNewRows )
206                                             {
207                                                 pMat->FillDouble( CreateDoubleError(
208                                                     NOTAVAILABLE), 0, nResMatRows, nNewCols-1,
209                                                     nNewRows-1);
210                                             }
211                                             if ( nRows == 1 && nCurCol != 0 )
212                                             {
213                                                 nCurCol = 0;
214                                                 nCurRow = nResMatRows - 1;
215                                             }
216                                             nResMatCols = nNewCols;
217                                             nResMatRows = nNewRows;
218                                         }
219                                     }
220 };
221 
222 #endif // SC_JUMPMATRIX_HXX
223