Update ooo320-m1
[ooovba.git] / sc / source / core / inc / jumpmatrix.hxx
blobe550245ee01f0d341dc9795b95e33fafa0d0e8ec
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: jumpmatrix.hxx,v $
10 * $Revision: 1.6.148.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef SC_JUMPMATRIX_HXX
32 #define SC_JUMPMATRIX_HXX
34 #include "formula/token.hxx"
35 #include "formula/errorcodes.hxx"
36 #include <tools/solar.h>
37 #include <vector>
38 #include "scmatrix.hxx"
40 typedef ::std::vector< formula::FormulaToken*> ScTokenVec;
42 struct ScJumpMatrixEntry
44 double fBool; // 0:= false 1:= true also if no-path
45 // other values may contain error conditions like NAN and INF
46 short nStart; // start of path (actually start-1, see formula::FormulaTokenIterator)
47 short nNext; // next after path
48 // jump path exists if nStart != nNext, else no path
49 short nStop; // optional stop of path (nPC < nStop)
51 void SetJump( double fBoolP, short nStartP, short nNextP, short nStopP )
53 fBool = fBoolP;
54 nStart = nStartP;
55 nNext = nNextP;
56 nStop = nStopP;
58 void GetJump( double& rBool, short& rStart, short& rNext, short& rStop )
60 rBool = fBool;
61 rStart = nStart;
62 rNext = nNext;
63 rStop = nStop;
67 class ScJumpMatrix
69 ScJumpMatrixEntry* pJump; // the jumps
70 ScMatrixRef pMat; // the results
71 ScTokenVec* pParams; // parameter stack
72 SCSIZE nCols;
73 SCSIZE nRows;
74 SCSIZE nCurCol;
75 SCSIZE nCurRow;
76 SCSIZE nResMatCols;
77 SCSIZE nResMatRows;
78 bool bStarted;
80 // not implemented, prevent usage
81 ScJumpMatrix( const ScJumpMatrix& );
82 ScJumpMatrix& operator=( const ScJumpMatrix& );
84 public:
85 ScJumpMatrix( SCSIZE nColsP, SCSIZE nRowsP )
86 : pJump( new ScJumpMatrixEntry[ nColsP * nRowsP ] )
87 , pMat( new ScMatrix( nColsP, nRowsP) )
88 , pParams( NULL )
89 , nCols( nColsP )
90 , nRows( nRowsP )
91 , nCurCol( 0 )
92 , nCurRow( 0 )
93 , nResMatCols( nColsP )
94 , nResMatRows( nRowsP )
95 , bStarted( false )
97 // Initialize result matrix in case of
98 // a premature end of the interpreter
99 // due to errors.
100 pMat->FillDouble( CreateDoubleError(
101 NOTAVAILABLE), 0, 0, nCols-1,
102 nRows-1);
103 /*! pJump not initialized */
105 ~ScJumpMatrix()
107 if ( pParams )
109 for ( ScTokenVec::iterator i =
110 pParams->begin(); i !=
111 pParams->end(); ++i )
113 (*i)->DecRef();
115 delete pParams;
117 delete [] pJump;
119 void GetDimensions( SCSIZE& rCols, SCSIZE& rRows ) const
121 rCols = nCols;
122 rRows = nRows;
124 void SetJump( SCSIZE nCol, SCSIZE nRow, double fBool,
125 short nStart, short nNext,
126 short nStop = SHRT_MAX )
128 pJump[ (ULONG)nCol * nRows + nRow ].
129 SetJump( fBool, nStart, nNext, nStop);
131 void GetJump( SCSIZE nCol, SCSIZE nRow, double& rBool,
132 short& rStart, short& rNext,
133 short& rStop ) const
135 if (nCols == 1 && nRows == 1)
137 nCol = 0;
138 nRow = 0;
140 else if (nCols == 1 && nRow < nRows)
141 nCol = 0;
142 else if (nRows == 1 && nCol < nCols)
143 nRow = 0;
144 else if (nCols <= nCol || nRows <= nRow)
146 DBG_ERROR("ScJumpMatrix::GetJump: dimension error");
147 nCol = 0;
148 nRow = 0;
150 pJump[ (ULONG)nCol * nRows + nRow ].
151 GetJump( rBool, rStart, rNext, rStop);
153 void SetAllJumps( double fBool,
154 short nStart, short nNext,
155 short nStop = SHRT_MAX )
157 ULONG n = (ULONG)nCols * nRows;
158 for ( ULONG j=0; j<n; ++j )
160 pJump[ j ].SetJump( fBool, nStart,
161 nNext, nStop);
164 void SetJumpParameters( ScTokenVec* p )
165 { pParams = p; }
166 const ScTokenVec* GetJumpParameters() const { return pParams; }
167 ScMatrix* GetResultMatrix() const { return pMat; }
168 void GetPos( SCSIZE& rCol, SCSIZE& rRow ) const
170 rCol = nCurCol;
171 rRow = nCurRow;
173 bool Next( SCSIZE& rCol, SCSIZE& rRow )
175 if ( !bStarted )
177 bStarted = true;
178 nCurCol = nCurRow = 0;
180 else
182 if ( ++nCurRow >= nResMatRows )
184 nCurRow = 0;
185 ++nCurCol;
188 GetPos( rCol, rRow );
189 return nCurCol < nResMatCols;
191 void GetResMatDimensions( SCSIZE& rCols, SCSIZE& rRows )
193 rCols = nResMatCols;
194 rRows = nResMatRows;
196 void SetNewResMat( SCSIZE nNewCols, SCSIZE nNewRows )
198 if ( nNewCols > nResMatCols || nNewRows > nResMatRows )
200 pMat = pMat->CloneAndExtend( nNewCols, nNewRows );
201 if ( nResMatCols < nNewCols )
203 pMat->FillDouble( CreateDoubleError(
204 NOTAVAILABLE), nResMatCols, 0, nNewCols-1,
205 nResMatRows-1);
207 if ( nResMatRows < nNewRows )
209 pMat->FillDouble( CreateDoubleError(
210 NOTAVAILABLE), 0, nResMatRows, nNewCols-1,
211 nNewRows-1);
213 if ( nRows == 1 && nCurCol != 0 )
215 nCurCol = 0;
216 nCurRow = nResMatRows - 1;
218 nResMatCols = nNewCols;
219 nResMatRows = nNewRows;
224 #endif // SC_JUMPMATRIX_HXX