Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : : #ifndef SC_JUMPMATRIX_HXX
30 : : #define SC_JUMPMATRIX_HXX
31 : :
32 : : #include "formula/token.hxx"
33 : : #include "formula/errorcodes.hxx"
34 : : #include <tools/solar.h>
35 : : #include <vector>
36 : : #include "scmatrix.hxx"
37 : :
38 : : typedef ::std::vector< formula::FormulaToken*> ScTokenVec;
39 : :
40 : : struct ScJumpMatrixEntry
41 : : {
42 : : double fBool; // 0:= false 1:= true also if no-path
43 : : // other values may contain error conditions like NAN and INF
44 : : short nStart; // start of path (actually start-1, see formula::FormulaTokenIterator)
45 : : short nNext; // next after path
46 : : // jump path exists if nStart != nNext, else no path
47 : : short nStop; // optional stop of path (nPC < nStop)
48 : :
49 : 0 : void SetJump( double fBoolP, short nStartP, short nNextP, short nStopP )
50 : : {
51 : 0 : fBool = fBoolP;
52 : 0 : nStart = nStartP;
53 : 0 : nNext = nNextP;
54 : 0 : nStop = nStopP;
55 : 0 : }
56 : 0 : void GetJump( double& rBool, short& rStart, short& rNext, short& rStop )
57 : : {
58 : 0 : rBool = fBool;
59 : 0 : rStart = nStart;
60 : 0 : rNext = nNext;
61 : 0 : rStop = nStop;
62 : 0 : }
63 : : };
64 : :
65 : : class ScJumpMatrix
66 : : {
67 : : ScJumpMatrixEntry* pJump; // the jumps
68 : : ScMatrixRef pMat; // the results
69 : : ScTokenVec* pParams; // parameter stack
70 : : SCSIZE nCols;
71 : : SCSIZE nRows;
72 : : SCSIZE nCurCol;
73 : : SCSIZE nCurRow;
74 : : SCSIZE nResMatCols;
75 : : SCSIZE nResMatRows;
76 : : bool bStarted;
77 : :
78 : : // not implemented, prevent usage
79 : : ScJumpMatrix( const ScJumpMatrix& );
80 : : ScJumpMatrix& operator=( const ScJumpMatrix& );
81 : :
82 : : public:
83 : 0 : ScJumpMatrix( SCSIZE nColsP, SCSIZE nRowsP )
84 : 0 : : pJump( new ScJumpMatrixEntry[ nColsP * nRowsP ] )
85 [ # # ]: 0 : , pMat( new ScMatrix( nColsP, nRowsP) )
86 : : , pParams( NULL )
87 : : , nCols( nColsP )
88 : : , nRows( nRowsP )
89 : : , nCurCol( 0 )
90 : : , nCurRow( 0 )
91 : : , nResMatCols( nColsP )
92 : : , nResMatRows( nRowsP )
93 : 0 : , bStarted( false )
94 : : {
95 : : // Initialize result matrix in case of
96 : : // a premature end of the interpreter
97 : : // due to errors.
98 : : pMat->FillDouble( CreateDoubleError(
99 : : NOTAVAILABLE), 0, 0, nCols-1,
100 [ # # ]: 0 : nRows-1);
101 : : /*! pJump not initialized */
102 : 0 : }
103 : 0 : ~ScJumpMatrix()
104 : 0 : {
105 [ # # ]: 0 : if ( pParams )
106 : : {
107 [ # # # # ]: 0 : for ( ScTokenVec::iterator i =
[ # # ]
108 : 0 : pParams->begin(); i !=
109 : 0 : pParams->end(); ++i )
110 : : {
111 [ # # ][ # # ]: 0 : (*i)->DecRef();
112 : : }
113 [ # # ]: 0 : delete pParams;
114 : : }
115 [ # # ]: 0 : delete [] pJump;
116 : 0 : }
117 : 0 : void GetDimensions( SCSIZE& rCols, SCSIZE& rRows ) const
118 : : {
119 : 0 : rCols = nCols;
120 : 0 : rRows = nRows;
121 : 0 : }
122 : 0 : void SetJump( SCSIZE nCol, SCSIZE nRow, double fBool,
123 : : short nStart, short nNext,
124 : : short nStop = SHRT_MAX )
125 : : {
126 : : pJump[ (sal_uLong)nCol * nRows + nRow ].
127 : 0 : SetJump( fBool, nStart, nNext, nStop);
128 : 0 : }
129 : 0 : void GetJump( SCSIZE nCol, SCSIZE nRow, double& rBool,
130 : : short& rStart, short& rNext,
131 : : short& rStop ) const
132 : : {
133 [ # # ][ # # ]: 0 : if (nCols == 1 && nRows == 1)
134 : : {
135 : 0 : nCol = 0;
136 : 0 : nRow = 0;
137 : : }
138 [ # # ][ # # ]: 0 : else if (nCols == 1 && nRow < nRows)
139 : 0 : nCol = 0;
140 [ # # ][ # # ]: 0 : else if (nRows == 1 && nCol < nCols)
141 : 0 : nRow = 0;
142 [ # # ][ # # ]: 0 : else if (nCols <= nCol || nRows <= nRow)
143 : : {
144 : : OSL_FAIL("ScJumpMatrix::GetJump: dimension error");
145 : 0 : nCol = 0;
146 : 0 : nRow = 0;
147 : : }
148 : : pJump[ (sal_uLong)nCol * nRows + nRow ].
149 : 0 : GetJump( rBool, rStart, rNext, rStop);
150 : 0 : }
151 : 0 : void SetAllJumps( double fBool,
152 : : short nStart, short nNext,
153 : : short nStop = SHRT_MAX )
154 : : {
155 : 0 : sal_uLong n = (sal_uLong)nCols * nRows;
156 [ # # ]: 0 : for ( sal_uLong j=0; j<n; ++j )
157 : : {
158 : : pJump[ j ].SetJump( fBool, nStart,
159 : 0 : nNext, nStop);
160 : : }
161 : 0 : }
162 : 0 : void SetJumpParameters( ScTokenVec* p )
163 : 0 : { pParams = p; }
164 : 0 : const ScTokenVec* GetJumpParameters() const { return pParams; }
165 : 0 : ScMatrix* GetResultMatrix() const { return pMat.get(); }
166 : 0 : void GetPos( SCSIZE& rCol, SCSIZE& rRow ) const
167 : : {
168 : 0 : rCol = nCurCol;
169 : 0 : rRow = nCurRow;
170 : 0 : }
171 : 0 : bool Next( SCSIZE& rCol, SCSIZE& rRow )
172 : : {
173 [ # # ]: 0 : if ( !bStarted )
174 : : {
175 : 0 : bStarted = true;
176 : 0 : nCurCol = nCurRow = 0;
177 : : }
178 : : else
179 : : {
180 [ # # ]: 0 : if ( ++nCurRow >= nResMatRows )
181 : : {
182 : 0 : nCurRow = 0;
183 : 0 : ++nCurCol;
184 : : }
185 : : }
186 : 0 : GetPos( rCol, rRow );
187 : 0 : return nCurCol < nResMatCols;
188 : : }
189 : 0 : void GetResMatDimensions( SCSIZE& rCols, SCSIZE& rRows )
190 : : {
191 : 0 : rCols = nResMatCols;
192 : 0 : rRows = nResMatRows;
193 : 0 : }
194 : 0 : void SetNewResMat( SCSIZE nNewCols, SCSIZE nNewRows )
195 : : {
196 [ # # ][ # # ]: 0 : if ( nNewCols > nResMatCols || nNewRows > nResMatRows )
197 : : {
198 : 0 : pMat = pMat->CloneAndExtend(nNewCols, nNewRows);
199 [ # # ]: 0 : if ( nResMatCols < nNewCols )
200 : : {
201 : : pMat->FillDouble( CreateDoubleError(
202 : : NOTAVAILABLE), nResMatCols, 0, nNewCols-1,
203 : 0 : nResMatRows-1);
204 : : }
205 [ # # ]: 0 : if ( nResMatRows < nNewRows )
206 : : {
207 : : pMat->FillDouble( CreateDoubleError(
208 : : NOTAVAILABLE), 0, nResMatRows, nNewCols-1,
209 : 0 : nNewRows-1);
210 : : }
211 [ # # ][ # # ]: 0 : if ( nRows == 1 && nCurCol != 0 )
212 : : {
213 : 0 : nCurCol = 0;
214 : 0 : nCurRow = nResMatRows - 1;
215 : : }
216 : 0 : nResMatCols = nNewCols;
217 : 0 : nResMatRows = nNewRows;
218 : : }
219 : 0 : }
220 : : };
221 : :
222 : : #endif // SC_JUMPMATRIX_HXX
223 : :
224 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|