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 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
|
/* -*- 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/.
*/
#include <columnspanset.hxx>
#include <column.hxx>
#include <table.hxx>
#include <document.hxx>
#include <mtvfunctions.hxx>
#include <markdata.hxx>
#include <rangelst.hxx>
#include <fstalgorithm.hxx>
#include <algorithm>
#include <memory>
#include <o3tl/safeint.hxx>
namespace sc {
namespace {
class ColumnScanner
{
ColumnSpanSet::ColumnSpansType& mrRanges;
bool mbVal;
public:
ColumnScanner(ColumnSpanSet::ColumnSpansType& rRanges, bool bVal) :
mrRanges(rRanges), mbVal(bVal) {}
void operator() (const sc::CellStoreType::value_type& node, size_t nOffset, size_t nDataSize)
{
if (node.type == sc::element_type_empty)
return;
size_t nRow = node.position + nOffset;
size_t nEndRow = nRow + nDataSize; // Last row of current block plus 1
mrRanges.insert_back(nRow, nEndRow, mbVal);
}
};
}
RowSpan::RowSpan(SCROW nRow1, SCROW nRow2) : mnRow1(nRow1), mnRow2(nRow2) {}
ColRowSpan::ColRowSpan(SCCOLROW nStart, SCCOLROW nEnd) : mnStart(nStart), mnEnd(nEnd) {}
ColumnSpanSet::ColumnType::ColumnType(SCROW nStart, SCROW nEnd, bool bInit) :
maSpans(nStart, nEnd+1, bInit), miPos(maSpans.begin()) {}
ColumnSpanSet::Action::~Action() {}
void ColumnSpanSet::Action::startColumn(SCTAB /*nTab*/, SCCOL /*nCol*/) {}
ColumnSpanSet::ColumnAction::~ColumnAction() {}
ColumnSpanSet::ColumnSpanSet() {}
ColumnSpanSet::~ColumnSpanSet()
{
}
ColumnSpanSet::ColumnType& ColumnSpanSet::getColumn(const ScDocument& rDoc, SCTAB nTab, SCCOL nCol)
{
if (o3tl::make_unsigned(nTab) >= maTables.size())
maTables.resize(nTab+1);
if (!maTables[nTab])
maTables[nTab].reset(new TableType);
TableType& rTab = *maTables[nTab];
if (o3tl::make_unsigned(nCol) >= rTab.size())
rTab.resize(nCol+1);
if (!rTab[nCol])
rTab[nCol].reset(new ColumnType(0, rDoc.MaxRow(), /*bInit*/false));
return *rTab[nCol];
}
void ColumnSpanSet::set(const ScDocument& rDoc, SCTAB nTab, SCCOL nCol, SCROW nRow, bool bVal)
{
if (!ValidTab(nTab) || !rDoc.ValidCol(nCol) || !rDoc.ValidRow(nRow))
return;
ColumnType& rCol = getColumn(rDoc, nTab, nCol);
rCol.miPos = rCol.maSpans.insert(rCol.miPos, nRow, nRow+1, bVal).first;
}
void ColumnSpanSet::set(const ScDocument& rDoc, SCTAB nTab, SCCOL nCol, SCROW nRow1, SCROW nRow2, bool bVal)
{
if (!ValidTab(nTab) || !rDoc.ValidCol(nCol) || !rDoc.ValidRow(nRow1) || !rDoc.ValidRow(nRow2))
return;
ColumnType& rCol = getColumn(rDoc, nTab, nCol);
rCol.miPos = rCol.maSpans.insert(rCol.miPos, nRow1, nRow2+1, bVal).first;
}
void ColumnSpanSet::set(const ScDocument& rDoc, const ScRange& rRange, bool bVal)
{
for (SCTAB nTab = rRange.aStart.Tab(); nTab <= rRange.aEnd.Tab(); ++nTab)
{
for (SCCOL nCol = rRange.aStart.Col(); nCol <= rRange.aEnd.Col(); ++nCol)
{
ColumnType& rCol = getColumn(rDoc, nTab, nCol);
rCol.miPos = rCol.maSpans.insert(rCol.miPos, rRange.aStart.Row(), rRange.aEnd.Row()+1, bVal).first;
}
}
}
void ColumnSpanSet::set( const ScDocument& rDoc, SCTAB nTab, SCCOL nCol, const SingleColumnSpanSet& rSingleSet, bool bVal )
{
SingleColumnSpanSet::SpansType aSpans;
rSingleSet.getSpans(aSpans);
for (const auto& rSpan : aSpans)
set(rDoc, nTab, nCol, rSpan.mnRow1, rSpan.mnRow2, bVal);
}
void ColumnSpanSet::scan(
const ScDocument& rDoc, SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2, bool bVal)
{
if (!rDoc.ValidColRow(nCol1, nRow1) || !rDoc.ValidColRow(nCol2, nRow2))
return;
if (nCol1 > nCol2 || nRow1 > nRow2)
return;
const ScTable* pTab = rDoc.FetchTable(nTab);
if (!pTab)
return;
nCol2 = pTab->ClampToAllocatedColumns(nCol2);
for (SCCOL nCol = nCol1; nCol <= nCol2; ++nCol)
{
ColumnType& rCol = getColumn(rDoc, nTab, nCol);
const CellStoreType& rSrcCells = pTab->aCol[nCol].maCells;
ColumnScanner aScanner(rCol.maSpans, bVal);
ParseBlock(rSrcCells.begin(), rSrcCells, aScanner, nRow1, nRow2);
}
}
void ColumnSpanSet::executeAction(Action& ac) const
{
for (size_t nTab = 0; nTab < maTables.size(); ++nTab)
{
if (!maTables[nTab])
continue;
const TableType& rTab = *maTables[nTab];
for (size_t nCol = 0; nCol < rTab.size(); ++nCol)
{
if (!rTab[nCol])
continue;
ac.startColumn(nTab, nCol);
ColumnType& rCol = *rTab[nCol];
ColumnSpansType::const_iterator it = rCol.maSpans.begin(), itEnd = rCol.maSpans.end();
SCROW nRow1, nRow2;
nRow1 = it->first;
bool bVal = it->second;
for (++it; it != itEnd; ++it)
{
nRow2 = it->first-1;
ac.execute(ScAddress(nCol, nRow1, nTab), nRow2-nRow1+1, bVal);
nRow1 = nRow2+1; // for the next iteration.
bVal = it->second;
}
}
}
}
void ColumnSpanSet::executeColumnAction(ScDocument& rDoc, ColumnAction& ac) const
{
for (size_t nTab = 0; nTab < maTables.size(); ++nTab)
{
if (!maTables[nTab])
continue;
const TableType& rTab = *maTables[nTab];
for (SCCOL nCol = 0; nCol < static_cast<SCCOL>(rTab.size()); ++nCol)
{
if (!rTab[nCol])
continue;
ScTable* pTab = rDoc.FetchTable(nTab);
if (!pTab)
continue;
if (!rDoc.ValidCol(nCol) || nCol >= pTab->GetAllocatedColumnsCount())
{
// End the loop.
nCol = rTab.size();
continue;
}
ScColumn& rColumn = pTab->aCol[nCol];
ac.startColumn(&rColumn);
ColumnType& rCol = *rTab[nCol];
ColumnSpansType::const_iterator it = rCol.maSpans.begin(), itEnd = rCol.maSpans.end();
SCROW nRow1, nRow2;
nRow1 = it->first;
bool bVal = it->second;
for (++it; it != itEnd; ++it)
{
nRow2 = it->first-1;
ac.execute(nRow1, nRow2, bVal);
nRow1 = nRow2+1; // for the next iteration.
bVal = it->second;
}
}
}
}
namespace {
class Scanner
{
SingleColumnSpanSet::ColumnSpansType& mrRanges;
public:
explicit Scanner(SingleColumnSpanSet::ColumnSpansType& rRanges) : mrRanges(rRanges) {}
void operator() (const sc::CellStoreType::value_type& node, size_t nOffset, size_t nDataSize)
{
if (node.type == sc::element_type_empty)
return;
size_t nRow = node.position + nOffset;
size_t nEndRow = nRow + nDataSize; // Last row of current block plus 1
mrRanges.insert_back(nRow, nEndRow, true);
}
};
}
SingleColumnSpanSet::SingleColumnSpanSet() : maSpans(0, MAXROWCOUNT, false) {}
void SingleColumnSpanSet::scan(const ScColumn& rColumn)
{
const CellStoreType& rCells = rColumn.maCells;
SCROW nCurRow = 0;
for (const auto& rCell : rCells)
{
SCROW nEndRow = nCurRow + rCell.size; // Last row of current block plus 1.
if (rCell.type != sc::element_type_empty)
maSpans.insert_back(nCurRow, nEndRow, true);
nCurRow = nEndRow;
}
}
void SingleColumnSpanSet::scan(const ScColumn& rColumn, SCROW nStart, SCROW nEnd)
{
const CellStoreType& rCells = rColumn.maCells;
Scanner aScanner(maSpans);
sc::ParseBlock(rCells.begin(), rCells, aScanner, nStart, nEnd);
}
void SingleColumnSpanSet::scan(
ColumnBlockConstPosition& rBlockPos, const ScColumn& rColumn, SCROW nStart, SCROW nEnd)
{
const CellStoreType& rCells = rColumn.maCells;
Scanner aScanner(maSpans);
rBlockPos.miCellPos = sc::ParseBlock(rBlockPos.miCellPos, rCells, aScanner, nStart, nEnd);
}
void SingleColumnSpanSet::scan(const ScMarkData& rMark, SCTAB nTab, SCCOL nCol)
{
if (!rMark.GetTableSelect(nTab))
// This table is not selected. Nothing to scan.
return;
ScRangeList aRanges = rMark.GetMarkedRangesForTab(nTab);
scan(aRanges, nTab, nCol);
}
void SingleColumnSpanSet::scan(const ScRangeList& rRanges, SCTAB nTab, SCCOL nCol)
{
for (size_t i = 0, n = rRanges.size(); i < n; ++i)
{
const ScRange & rRange = rRanges[i];
if (nTab < rRange.aStart.Tab() || rRange.aEnd.Tab() < nTab)
continue;
if (nCol < rRange.aStart.Col() || rRange.aEnd.Col() < nCol)
// This column is not in this range. Skip it.
continue;
maSpans.insert_back(rRange.aStart.Row(), rRange.aEnd.Row()+1, true);
}
}
void SingleColumnSpanSet::set(SCROW nRow1, SCROW nRow2, bool bVal)
{
maSpans.insert_back(nRow1, nRow2+1, bVal);
}
void SingleColumnSpanSet::getRows(std::vector<SCROW> &rRows) const
{
std::vector<SCROW> aRows;
SpansType aRanges;
getSpans(aRanges);
for (const auto& rRange : aRanges)
{
for (SCROW nRow = rRange.mnRow1; nRow <= rRange.mnRow2; ++nRow)
aRows.push_back(nRow);
}
rRows.swap(aRows);
}
void SingleColumnSpanSet::getSpans(SpansType& rSpans) const
{
SpansType aSpans = toSpanArray<SCROW,RowSpan>(maSpans);
rSpans.swap(aSpans);
}
void SingleColumnSpanSet::swap( SingleColumnSpanSet& r )
{
maSpans.swap(r.maSpans);
}
bool SingleColumnSpanSet::empty() const
{
// Empty if there's only the 0..rDoc.MaxRow() span with false.
ColumnSpansType::const_iterator it = maSpans.begin();
return (it->first == 0) && !(it->second) && (++it != maSpans.end()) && (it->first == MAXROWCOUNT);
}
void RangeColumnSpanSet::executeColumnAction(ScDocument& rDoc, sc::ColumnSpanSet::ColumnAction& ac) const
{
for (SCTAB nTab = range.aStart.Tab(); nTab <= range.aEnd.Tab(); ++nTab)
{
ScTable* pTab = rDoc.FetchTable(nTab);
if (!pTab)
continue;
SCCOL nEndCol = pTab->ClampToAllocatedColumns(range.aEnd.Col());
for (SCCOL nCol = range.aStart.Col(); nCol <= nEndCol; ++nCol)
{
if (!rDoc.ValidCol(nCol))
break;
ScColumn& rColumn = pTab->aCol[nCol];
ac.startColumn(&rColumn);
ac.execute( range.aStart.Row(), range.aEnd.Row(), true );
}
}
}
void RangeColumnSpanSet::executeColumnAction(ScDocument& rDoc, sc::ColumnSpanSet::ColumnAction& ac, double& fMem) const
{
for (SCTAB nTab = range.aStart.Tab(); nTab <= range.aEnd.Tab(); ++nTab)
{
ScTable* pTab = rDoc.FetchTable(nTab);
if (!pTab)
continue;
SCCOL nEndCol = pTab->ClampToAllocatedColumns(range.aEnd.Col());
for (SCCOL nCol = range.aStart.Col(); nCol <= nEndCol; ++nCol)
{
if (!rDoc.ValidCol(nCol))
break;
ScColumn& rColumn = pTab->aCol[nCol];
ac.startColumn(&rColumn);
ac.executeSum( range.aStart.Row(), range.aEnd.Row(), true, fMem );
}
}
}
} // namespace sc
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|