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 : #include "jumpmatrix.hxx"
21 : #include "scmatrix.hxx"
22 :
23 : namespace {
24 : // Don't bother with buffer overhead for less than y rows.
25 : const SCSIZE kBufferThreshhold = 128;
26 : }
27 :
28 0 : ScJumpMatrix::ScJumpMatrix(SCSIZE nColsP, SCSIZE nRowsP)
29 0 : : pJump(new ScJumpMatrixEntry[nColsP * nRowsP])
30 0 : , pMat(new ScMatrix(nColsP, nRowsP))
31 : , pParams(NULL)
32 : , nCols(nColsP)
33 : , nRows(nRowsP)
34 : , nCurCol(0)
35 : , nCurRow(0)
36 : , nResMatCols(nColsP)
37 : , nResMatRows(nRowsP)
38 : , bStarted(false)
39 : , mnBufferCol(0)
40 : , mnBufferRowStart(0)
41 : , mnBufferEmptyCount(0)
42 0 : , mnBufferEmptyPathCount(0)
43 : {
44 : // Initialize result matrix in case of
45 : // a premature end of the interpreter
46 : // due to errors.
47 0 : pMat->FillDouble(CreateDoubleError(NOTAVAILABLE), 0, 0, nCols - 1, nRows - 1);
48 : /*! pJump not initialized */
49 0 : }
50 :
51 0 : ScJumpMatrix::~ScJumpMatrix()
52 : {
53 0 : if (pParams)
54 : {
55 0 : for (ScTokenVec::iterator i =
56 0 : pParams->begin(); i !=
57 0 : pParams->end(); ++i)
58 : {
59 0 : (*i)->DecRef();
60 : }
61 0 : delete pParams;
62 : }
63 0 : delete[] pJump;
64 0 : }
65 :
66 0 : void ScJumpMatrix::GetDimensions(SCSIZE& rCols, SCSIZE& rRows) const
67 : {
68 0 : rCols = nCols;
69 0 : rRows = nRows;
70 0 : }
71 :
72 0 : void ScJumpMatrix::SetJump(SCSIZE nCol, SCSIZE nRow, double fBool,
73 : short nStart, short nNext, short nStop)
74 : {
75 0 : pJump[(sal_uLong)nCol * nRows + nRow].SetJump(fBool, nStart, nNext, nStop);
76 0 : }
77 :
78 0 : void ScJumpMatrix::GetJump(
79 : SCSIZE nCol, SCSIZE nRow, double& rBool, short& rStart, short& rNext, short& rStop) const
80 : {
81 0 : if (nCols == 1 && nRows == 1)
82 : {
83 0 : nCol = 0;
84 0 : nRow = 0;
85 : }
86 0 : else if (nCols == 1 && nRow < nRows) nCol = 0;
87 0 : else if (nRows == 1 && nCol < nCols) nRow = 0;
88 0 : else if (nCols <= nCol || nRows <= nRow)
89 : {
90 : OSL_FAIL("ScJumpMatrix::GetJump: dimension error");
91 0 : nCol = 0;
92 0 : nRow = 0;
93 : }
94 0 : pJump[(sal_uLong)nCol * nRows + nRow].
95 0 : GetJump(rBool, rStart, rNext, rStop);
96 0 : }
97 :
98 0 : void ScJumpMatrix::SetAllJumps(double fBool, short nStart, short nNext, short nStop)
99 : {
100 0 : sal_uLong n = (sal_uLong)nCols * nRows;
101 0 : for (sal_uLong j = 0; j < n; ++j)
102 : {
103 : pJump[j].SetJump(fBool, nStart,
104 0 : nNext, nStop);
105 : }
106 0 : }
107 :
108 0 : void ScJumpMatrix::SetJumpParameters(ScTokenVec* p)
109 : {
110 0 : pParams = p;
111 0 : }
112 :
113 0 : const ScTokenVec* ScJumpMatrix::GetJumpParameters() const
114 : {
115 0 : return pParams;
116 : }
117 :
118 0 : void ScJumpMatrix::GetPos(SCSIZE& rCol, SCSIZE& rRow) const
119 : {
120 0 : rCol = nCurCol;
121 0 : rRow = nCurRow;
122 0 : }
123 :
124 0 : bool ScJumpMatrix::Next(SCSIZE& rCol, SCSIZE& rRow)
125 : {
126 0 : if (!bStarted)
127 : {
128 0 : bStarted = true;
129 0 : nCurCol = nCurRow = 0;
130 : }
131 : else
132 : {
133 0 : if (++nCurRow >= nResMatRows)
134 : {
135 0 : nCurRow = 0;
136 0 : ++nCurCol;
137 : }
138 : }
139 0 : GetPos(rCol, rRow);
140 0 : return nCurCol < nResMatCols;
141 : }
142 :
143 0 : void ScJumpMatrix::GetResMatDimensions(SCSIZE& rCols, SCSIZE& rRows)
144 : {
145 0 : rCols = nResMatCols;
146 0 : rRows = nResMatRows;
147 0 : }
148 :
149 0 : void ScJumpMatrix::SetNewResMat(SCSIZE nNewCols, SCSIZE nNewRows)
150 : {
151 0 : if (nNewCols > nResMatCols || nNewRows > nResMatRows)
152 : {
153 0 : FlushBufferOtherThan( BUFFER_NONE, 0, 0);
154 0 : pMat = pMat->CloneAndExtend(nNewCols, nNewRows);
155 0 : if (nResMatCols < nNewCols)
156 : {
157 : pMat->FillDouble(CreateDoubleError(
158 : NOTAVAILABLE), nResMatCols, 0, nNewCols - 1,
159 0 : nResMatRows - 1);
160 : }
161 0 : if (nResMatRows < nNewRows)
162 : {
163 : pMat->FillDouble(CreateDoubleError(
164 : NOTAVAILABLE), 0, nResMatRows, nNewCols - 1,
165 0 : nNewRows - 1);
166 : }
167 0 : if (nRows == 1 && nCurCol != 0)
168 : {
169 0 : nCurCol = 0;
170 0 : nCurRow = nResMatRows - 1;
171 : }
172 0 : nResMatCols = nNewCols;
173 0 : nResMatRows = nNewRows;
174 : }
175 0 : }
176 :
177 0 : bool ScJumpMatrix::HasResultMatrix() const
178 : {
179 : // We now always have a matrix but caller logic may still want to check it.
180 0 : return pMat.get() != NULL;
181 : }
182 :
183 0 : void ScJumpMatrix::FlushBufferOtherThan( ScJumpMatrix::BufferType eType, SCSIZE nC, SCSIZE nR )
184 : {
185 0 : if (!mvBufferDoubles.empty() &&
186 0 : (eType != BUFFER_DOUBLE || nC != mnBufferCol || nR != mnBufferRowStart + mvBufferDoubles.size()))
187 : {
188 0 : pMat->PutDoubleVector( mvBufferDoubles, mnBufferCol, mnBufferRowStart);
189 0 : mvBufferDoubles.clear();
190 : }
191 0 : if (!mvBufferStrings.empty() &&
192 0 : (eType != BUFFER_STRING || nC != mnBufferCol || nR != mnBufferRowStart + mvBufferStrings.size()))
193 : {
194 0 : pMat->PutStringVector( mvBufferStrings, mnBufferCol, mnBufferRowStart);
195 0 : mvBufferStrings.clear();
196 : }
197 0 : if (mnBufferEmptyCount &&
198 0 : (eType != BUFFER_EMPTY || nC != mnBufferCol || nR != mnBufferRowStart + mnBufferEmptyCount))
199 : {
200 0 : pMat->PutEmptyVector( mnBufferEmptyCount, mnBufferCol, mnBufferRowStart);
201 0 : mnBufferEmptyCount = 0;
202 : }
203 0 : if (mnBufferEmptyPathCount &&
204 0 : (eType != BUFFER_EMPTYPATH || nC != mnBufferCol || nR != mnBufferRowStart + mnBufferEmptyPathCount))
205 : {
206 0 : pMat->PutEmptyPathVector( mnBufferEmptyPathCount, mnBufferCol, mnBufferRowStart);
207 0 : mnBufferEmptyPathCount = 0;
208 : }
209 0 : }
210 :
211 0 : ScMatrix* ScJumpMatrix::GetResultMatrix()
212 : {
213 0 : if (nResMatRows >= kBufferThreshhold)
214 0 : FlushBufferOtherThan( BUFFER_NONE, 0, 0);
215 0 : return pMat.get();
216 : }
217 :
218 0 : void ScJumpMatrix::PutResultDouble( double fVal, SCSIZE nC, SCSIZE nR )
219 : {
220 0 : if (nResMatRows < kBufferThreshhold)
221 0 : pMat->PutDouble( fVal, nC, nR);
222 : else
223 : {
224 0 : FlushBufferOtherThan( BUFFER_DOUBLE, nC, nR);
225 0 : if (mvBufferDoubles.empty())
226 : {
227 0 : mnBufferCol = nC;
228 0 : mnBufferRowStart = nR;
229 : }
230 0 : mvBufferDoubles.push_back( fVal);
231 : }
232 0 : }
233 :
234 0 : void ScJumpMatrix::PutResultString( const svl::SharedString& rStr, SCSIZE nC, SCSIZE nR )
235 : {
236 0 : if (nResMatRows < kBufferThreshhold)
237 0 : pMat->PutString( rStr, nC, nR);
238 : else
239 : {
240 0 : FlushBufferOtherThan( BUFFER_STRING, nC, nR);
241 0 : if (mvBufferStrings.empty())
242 : {
243 0 : mnBufferCol = nC;
244 0 : mnBufferRowStart = nR;
245 : }
246 0 : mvBufferStrings.push_back( rStr);
247 : }
248 0 : }
249 :
250 0 : void ScJumpMatrix::PutResultEmpty( SCSIZE nC, SCSIZE nR )
251 : {
252 0 : if (nResMatRows < kBufferThreshhold)
253 0 : pMat->PutEmpty( nC, nR);
254 : else
255 : {
256 0 : FlushBufferOtherThan( BUFFER_EMPTY, nC, nR);
257 0 : if (!mnBufferEmptyCount)
258 : {
259 0 : mnBufferCol = nC;
260 0 : mnBufferRowStart = nR;
261 : }
262 0 : ++mnBufferEmptyCount;
263 : }
264 0 : }
265 :
266 0 : void ScJumpMatrix::PutResultEmptyPath( SCSIZE nC, SCSIZE nR )
267 : {
268 0 : if (nResMatRows < kBufferThreshhold)
269 0 : pMat->PutEmptyPath( nC, nR);
270 : else
271 : {
272 0 : FlushBufferOtherThan( BUFFER_EMPTYPATH, nC, nR);
273 0 : if (!mnBufferEmptyPathCount)
274 : {
275 0 : mnBufferCol = nC;
276 0 : mnBufferRowStart = nR;
277 : }
278 0 : ++mnBufferEmptyPathCount;
279 : }
280 0 : }
281 :
282 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|