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