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