1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include <jumpmatrix.hxx>
#include <scmatrix.hxx>
#include <osl/diagnose.h>
namespace {
// Don't bother with buffer overhead for less than y rows.
const SCSIZE kBufferThreshold = 128;
}
ScJumpMatrix::ScJumpMatrix( OpCode eOp, SCSIZE nColsP, SCSIZE nRowsP )
: mvJump(nColsP * nRowsP)
// Initialize result matrix in case of
// a premature end of the interpreter
// due to errors.
, pMat(new ScMatrix(nColsP, nRowsP, CreateDoubleError(FormulaError::NotAvailable)))
, nCols(nColsP)
, nRows(nRowsP)
, nCurCol(0)
, nCurRow(0)
, nResMatCols(nColsP)
, nResMatRows(nRowsP)
, meOp(eOp)
, bStarted(false)
, mnBufferCol(0)
, mnBufferRowStart(0)
, mnBufferEmptyCount(0)
, mnBufferEmptyPathCount(0)
{
/*! pJump not initialized */
}
ScJumpMatrix::~ScJumpMatrix()
{
for (const auto & i : mvParams)
i->DecRef();
}
void ScJumpMatrix::GetDimensions(SCSIZE& rCols, SCSIZE& rRows) const
{
rCols = nCols;
rRows = nRows;
}
void ScJumpMatrix::SetJump(SCSIZE nCol, SCSIZE nRow, double fBool,
short nStart, short nNext)
{
mvJump[static_cast<sal_uInt64>(nCol) * nRows + nRow].SetJump(fBool, nStart, nNext, SHRT_MAX);
}
void ScJumpMatrix::GetJump(
SCSIZE nCol, SCSIZE nRow, double& rBool, short& rStart, short& rNext, short& rStop) const
{
if (nCols == 1 && nRows == 1)
{
nCol = 0;
nRow = 0;
}
else if (nCols == 1 && nRow < nRows) nCol = 0;
else if (nRows == 1 && nCol < nCols) nRow = 0;
else if (nCols <= nCol || nRows <= nRow)
{
OSL_FAIL("ScJumpMatrix::GetJump: dimension error");
nCol = 0;
nRow = 0;
}
mvJump[static_cast<sal_uInt64>(nCol) * nRows + nRow].
GetJump(rBool, rStart, rNext, rStop);
}
void ScJumpMatrix::SetAllJumps(double fBool, short nStart, short nNext, short nStop)
{
sal_uInt64 n = static_cast<sal_uInt64>(nCols) * nRows;
for (sal_uInt64 j = 0; j < n; ++j)
{
mvJump[j].SetJump(fBool, nStart,
nNext, nStop);
}
}
void ScJumpMatrix::SetJumpParameters(ScTokenVec&& p)
{
mvParams = std::move(p);
}
void ScJumpMatrix::GetPos(SCSIZE& rCol, SCSIZE& rRow) const
{
rCol = nCurCol;
rRow = nCurRow;
}
bool ScJumpMatrix::Next(SCSIZE& rCol, SCSIZE& rRow)
{
if (!bStarted)
{
bStarted = true;
nCurCol = nCurRow = 0;
}
else
{
if (++nCurRow >= nResMatRows)
{
nCurRow = 0;
++nCurCol;
}
}
GetPos(rCol, rRow);
return nCurCol < nResMatCols;
}
void ScJumpMatrix::GetResMatDimensions(SCSIZE& rCols, SCSIZE& rRows)
{
rCols = nResMatCols;
rRows = nResMatRows;
}
void ScJumpMatrix::SetNewResMat(SCSIZE nNewCols, SCSIZE nNewRows)
{
if (nNewCols <= nResMatCols && nNewRows <= nResMatRows)
return;
FlushBufferOtherThan( BUFFER_NONE, 0, 0);
pMat = pMat->CloneAndExtend(nNewCols, nNewRows);
if (nResMatCols < nNewCols)
{
pMat->FillDouble(
CreateDoubleError(FormulaError::NotAvailable),
nResMatCols, 0, nNewCols - 1, nResMatRows - 1);
}
if (nResMatRows < nNewRows)
{
pMat->FillDouble(
CreateDoubleError(FormulaError::NotAvailable),
0, nResMatRows, nNewCols - 1, nNewRows - 1);
}
if (nRows == 1 && nCurCol != 0)
{
nCurCol = 0;
nCurRow = nResMatRows - 1;
}
nResMatCols = nNewCols;
nResMatRows = nNewRows;
}
bool ScJumpMatrix::HasResultMatrix() const
{
// We now always have a matrix but caller logic may still want to check it.
return bool(pMat);
}
ScRefList& ScJumpMatrix::GetRefList()
{
return mvRefList;
}
void ScJumpMatrix::FlushBufferOtherThan( ScJumpMatrix::BufferType eType, SCSIZE nC, SCSIZE nR )
{
if (!mvBufferDoubles.empty() &&
(eType != BUFFER_DOUBLE || nC != mnBufferCol || nR != mnBufferRowStart + mvBufferDoubles.size()))
{
pMat->PutDoubleVector( mvBufferDoubles, mnBufferCol, mnBufferRowStart);
mvBufferDoubles.clear();
}
if (!mvBufferStrings.empty() &&
(eType != BUFFER_STRING || nC != mnBufferCol || nR != mnBufferRowStart + mvBufferStrings.size()))
{
pMat->PutStringVector( mvBufferStrings, mnBufferCol, mnBufferRowStart);
mvBufferStrings.clear();
}
if (mnBufferEmptyCount &&
(eType != BUFFER_EMPTY || nC != mnBufferCol || nR != mnBufferRowStart + mnBufferEmptyCount))
{
pMat->PutEmptyVector( mnBufferEmptyCount, mnBufferCol, mnBufferRowStart);
mnBufferEmptyCount = 0;
}
if (mnBufferEmptyPathCount &&
(eType != BUFFER_EMPTYPATH || nC != mnBufferCol || nR != mnBufferRowStart + mnBufferEmptyPathCount))
{
pMat->PutEmptyPathVector( mnBufferEmptyPathCount, mnBufferCol, mnBufferRowStart);
mnBufferEmptyPathCount = 0;
}
}
ScMatrix* ScJumpMatrix::GetResultMatrix()
{
if (nResMatRows >= kBufferThreshold)
FlushBufferOtherThan( BUFFER_NONE, 0, 0);
return pMat.get();
}
void ScJumpMatrix::PutResultDouble( double fVal, SCSIZE nC, SCSIZE nR )
{
if (nResMatRows < kBufferThreshold)
pMat->PutDouble( fVal, nC, nR);
else
{
FlushBufferOtherThan( BUFFER_DOUBLE, nC, nR);
if (mvBufferDoubles.empty())
{
mnBufferCol = nC;
mnBufferRowStart = nR;
}
mvBufferDoubles.push_back( fVal);
}
}
void ScJumpMatrix::PutResultString( const svl::SharedString& rStr, SCSIZE nC, SCSIZE nR )
{
if (nResMatRows < kBufferThreshold)
pMat->PutString( rStr, nC, nR);
else
{
FlushBufferOtherThan( BUFFER_STRING, nC, nR);
if (mvBufferStrings.empty())
{
mnBufferCol = nC;
mnBufferRowStart = nR;
}
mvBufferStrings.push_back( rStr);
}
}
void ScJumpMatrix::PutResultEmpty( SCSIZE nC, SCSIZE nR )
{
if (nResMatRows < kBufferThreshold)
pMat->PutEmpty( nC, nR);
else
{
FlushBufferOtherThan( BUFFER_EMPTY, nC, nR);
if (!mnBufferEmptyCount)
{
mnBufferCol = nC;
mnBufferRowStart = nR;
}
++mnBufferEmptyCount;
}
}
void ScJumpMatrix::PutResultEmptyPath( SCSIZE nC, SCSIZE nR )
{
if (nResMatRows < kBufferThreshold)
pMat->PutEmptyPath( nC, nR);
else
{
FlushBufferOtherThan( BUFFER_EMPTYPATH, nC, nR);
if (!mnBufferEmptyPathCount)
{
mnBufferCol = nC;
mnBufferRowStart = nR;
}
++mnBufferEmptyPathCount;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|