Line data Source code
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 SC_JUMPMATRIX_HXX
21 : #define SC_JUMPMATRIX_HXX
22 :
23 : #include "formula/token.hxx"
24 : #include "formula/errorcodes.hxx"
25 : #include <tools/solar.h>
26 : #include <vector>
27 : #include "scmatrix.hxx"
28 :
29 : typedef ::std::vector< formula::FormulaToken*> ScTokenVec;
30 :
31 : struct ScJumpMatrixEntry
32 : {
33 : double fBool; // 0:= false 1:= true also if no-path
34 : // other values may contain error conditions like NAN and INF
35 : short nStart; // start of path (actually start-1, see formula::FormulaTokenIterator)
36 : short nNext; // next after path
37 : // jump path exists if nStart != nNext, else no path
38 : short nStop; // optional stop of path (nPC < nStop)
39 :
40 0 : void SetJump( double fBoolP, short nStartP, short nNextP, short nStopP )
41 : {
42 0 : fBool = fBoolP;
43 0 : nStart = nStartP;
44 0 : nNext = nNextP;
45 0 : nStop = nStopP;
46 0 : }
47 0 : void GetJump( double& rBool, short& rStart, short& rNext, short& rStop )
48 : {
49 0 : rBool = fBool;
50 0 : rStart = nStart;
51 0 : rNext = nNext;
52 0 : rStop = nStop;
53 0 : }
54 : };
55 :
56 : class ScJumpMatrix
57 : {
58 : ScJumpMatrixEntry* pJump; // the jumps
59 : ScMatrixRef pMat; // the results
60 : ScTokenVec* pParams; // parameter stack
61 : SCSIZE nCols;
62 : SCSIZE nRows;
63 : SCSIZE nCurCol;
64 : SCSIZE nCurRow;
65 : SCSIZE nResMatCols;
66 : SCSIZE nResMatRows;
67 : bool bStarted;
68 :
69 : // not implemented, prevent usage
70 : ScJumpMatrix( const ScJumpMatrix& );
71 : ScJumpMatrix& operator=( const ScJumpMatrix& );
72 :
73 : public:
74 0 : ScJumpMatrix( SCSIZE nColsP, SCSIZE nRowsP )
75 0 : : pJump( new ScJumpMatrixEntry[ nColsP * nRowsP ] )
76 0 : , pMat( new ScMatrix( nColsP, nRowsP) )
77 : , pParams( NULL )
78 : , nCols( nColsP )
79 : , nRows( nRowsP )
80 : , nCurCol( 0 )
81 : , nCurRow( 0 )
82 : , nResMatCols( nColsP )
83 : , nResMatRows( nRowsP )
84 0 : , bStarted( false )
85 : {
86 : // Initialize result matrix in case of
87 : // a premature end of the interpreter
88 : // due to errors.
89 : pMat->FillDouble( CreateDoubleError(
90 : NOTAVAILABLE), 0, 0, nCols-1,
91 0 : nRows-1);
92 : /*! pJump not initialized */
93 0 : }
94 0 : ~ScJumpMatrix()
95 0 : {
96 0 : if ( pParams )
97 : {
98 0 : for ( ScTokenVec::iterator i =
99 0 : pParams->begin(); i !=
100 0 : pParams->end(); ++i )
101 : {
102 0 : (*i)->DecRef();
103 : }
104 0 : delete pParams;
105 : }
106 0 : delete [] pJump;
107 0 : }
108 0 : void GetDimensions( SCSIZE& rCols, SCSIZE& rRows ) const
109 : {
110 0 : rCols = nCols;
111 0 : rRows = nRows;
112 0 : }
113 0 : void SetJump( SCSIZE nCol, SCSIZE nRow, double fBool,
114 : short nStart, short nNext,
115 : short nStop = SHRT_MAX )
116 : {
117 : pJump[ (sal_uLong)nCol * nRows + nRow ].
118 0 : SetJump( fBool, nStart, nNext, nStop);
119 0 : }
120 0 : void GetJump( SCSIZE nCol, SCSIZE nRow, double& rBool,
121 : short& rStart, short& rNext,
122 : short& rStop ) const
123 : {
124 0 : if (nCols == 1 && nRows == 1)
125 : {
126 0 : nCol = 0;
127 0 : nRow = 0;
128 : }
129 0 : else if (nCols == 1 && nRow < nRows)
130 0 : nCol = 0;
131 0 : else if (nRows == 1 && nCol < nCols)
132 0 : nRow = 0;
133 0 : else if (nCols <= nCol || nRows <= nRow)
134 : {
135 : OSL_FAIL("ScJumpMatrix::GetJump: dimension error");
136 0 : nCol = 0;
137 0 : nRow = 0;
138 : }
139 : pJump[ (sal_uLong)nCol * nRows + nRow ].
140 0 : GetJump( rBool, rStart, rNext, rStop);
141 0 : }
142 0 : void SetAllJumps( double fBool,
143 : short nStart, short nNext,
144 : short nStop = SHRT_MAX )
145 : {
146 0 : sal_uLong n = (sal_uLong)nCols * nRows;
147 0 : for ( sal_uLong j=0; j<n; ++j )
148 : {
149 : pJump[ j ].SetJump( fBool, nStart,
150 0 : nNext, nStop);
151 : }
152 0 : }
153 0 : void SetJumpParameters( ScTokenVec* p )
154 0 : { pParams = p; }
155 0 : const ScTokenVec* GetJumpParameters() const { return pParams; }
156 0 : ScMatrix* GetResultMatrix() const { return pMat.get(); }
157 0 : void GetPos( SCSIZE& rCol, SCSIZE& rRow ) const
158 : {
159 0 : rCol = nCurCol;
160 0 : rRow = nCurRow;
161 0 : }
162 0 : bool Next( SCSIZE& rCol, SCSIZE& rRow )
163 : {
164 0 : if ( !bStarted )
165 : {
166 0 : bStarted = true;
167 0 : nCurCol = nCurRow = 0;
168 : }
169 : else
170 : {
171 0 : if ( ++nCurRow >= nResMatRows )
172 : {
173 0 : nCurRow = 0;
174 0 : ++nCurCol;
175 : }
176 : }
177 0 : GetPos( rCol, rRow );
178 0 : return nCurCol < nResMatCols;
179 : }
180 0 : void GetResMatDimensions( SCSIZE& rCols, SCSIZE& rRows )
181 : {
182 0 : rCols = nResMatCols;
183 0 : rRows = nResMatRows;
184 0 : }
185 0 : void SetNewResMat( SCSIZE nNewCols, SCSIZE nNewRows )
186 : {
187 0 : if ( nNewCols > nResMatCols || nNewRows > nResMatRows )
188 : {
189 0 : pMat = pMat->CloneAndExtend(nNewCols, nNewRows);
190 0 : if ( nResMatCols < nNewCols )
191 : {
192 : pMat->FillDouble( CreateDoubleError(
193 : NOTAVAILABLE), nResMatCols, 0, nNewCols-1,
194 0 : nResMatRows-1);
195 : }
196 0 : if ( nResMatRows < nNewRows )
197 : {
198 : pMat->FillDouble( CreateDoubleError(
199 : NOTAVAILABLE), 0, nResMatRows, nNewCols-1,
200 0 : nNewRows-1);
201 : }
202 0 : if ( nRows == 1 && nCurCol != 0 )
203 : {
204 0 : nCurCol = 0;
205 0 : nCurRow = nResMatRows - 1;
206 : }
207 0 : nResMatCols = nNewCols;
208 0 : nResMatRows = nNewRows;
209 : }
210 0 : }
211 : };
212 :
213 : #endif // SC_JUMPMATRIX_HXX
214 :
215 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|