LibreOffice Module sc (master)  1
columnspanset.cxx
Go to the documentation of this file.
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 
10 #include <columnspanset.hxx>
11 #include <column.hxx>
12 #include <table.hxx>
13 #include <document.hxx>
14 #include <mtvfunctions.hxx>
15 #include <markdata.hxx>
16 #include <rangelst.hxx>
17 #include <fstalgorithm.hxx>
18 
19 #include <algorithm>
20 #include <memory>
21 
22 #include <o3tl/safeint.hxx>
23 
24 namespace sc {
25 
26 namespace {
27 
28 class ColumnScanner
29 {
31  bool mbVal;
32 public:
33  ColumnScanner(ColumnSpanSet::ColumnSpansType& rRanges, bool bVal) :
34  mrRanges(rRanges), mbVal(bVal) {}
35 
36  void operator() (const sc::CellStoreType::value_type& node, size_t nOffset, size_t nDataSize)
37  {
38  if (node.type == sc::element_type_empty)
39  return;
40 
41  size_t nRow = node.position + nOffset;
42  size_t nEndRow = nRow + nDataSize; // Last row of current block plus 1
43  mrRanges.insert_back(nRow, nEndRow, mbVal);
44  }
45 };
46 
47 }
48 
49 RowSpan::RowSpan(SCROW nRow1, SCROW nRow2) : mnRow1(nRow1), mnRow2(nRow2) {}
50 
51 ColRowSpan::ColRowSpan(SCCOLROW nStart, SCCOLROW nEnd) : mnStart(nStart), mnEnd(nEnd) {}
52 
54  maSpans(nStart, nEnd+1, bInit), miPos(maSpans.begin()) {}
55 
57 void ColumnSpanSet::Action::startColumn(SCTAB /*nTab*/, SCCOL /*nCol*/) {}
58 
60 
62 
64 {
65 }
66 
68 {
69  if (o3tl::make_unsigned(nTab) >= maTables.size())
70  maTables.resize(nTab+1);
71 
72  TableType& rTab = maTables[nTab];
73  if (o3tl::make_unsigned(nCol) >= rTab.size())
74  rTab.resize(nCol+1);
75 
76  if (!rTab[nCol])
77  rTab[nCol].emplace(0, rDoc.MaxRow(), /*bInit*/false);
78 
79  return *rTab[nCol];
80 }
81 
82 void ColumnSpanSet::set(const ScDocument& rDoc, SCTAB nTab, SCCOL nCol, SCROW nRow, bool bVal)
83 {
84  if (!ValidTab(nTab) || !rDoc.ValidCol(nCol) || !rDoc.ValidRow(nRow))
85  return;
86 
87  ColumnType& rCol = getColumn(rDoc, nTab, nCol);
88  rCol.miPos = rCol.maSpans.insert(rCol.miPos, nRow, nRow+1, bVal).first;
89 }
90 
91 void ColumnSpanSet::set(const ScDocument& rDoc, SCTAB nTab, SCCOL nCol, SCROW nRow1, SCROW nRow2, bool bVal)
92 {
93  if (!ValidTab(nTab) || !rDoc.ValidCol(nCol) || !rDoc.ValidRow(nRow1) || !rDoc.ValidRow(nRow2))
94  return;
95 
96  ColumnType& rCol = getColumn(rDoc, nTab, nCol);
97  rCol.miPos = rCol.maSpans.insert(rCol.miPos, nRow1, nRow2+1, bVal).first;
98 }
99 
100 void ColumnSpanSet::set(const ScDocument& rDoc, const ScRange& rRange, bool bVal)
101 {
102  for (SCTAB nTab = rRange.aStart.Tab(); nTab <= rRange.aEnd.Tab(); ++nTab)
103  {
104  for (SCCOL nCol = rRange.aStart.Col(); nCol <= rRange.aEnd.Col(); ++nCol)
105  {
106  ColumnType& rCol = getColumn(rDoc, nTab, nCol);
107  rCol.miPos = rCol.maSpans.insert(rCol.miPos, rRange.aStart.Row(), rRange.aEnd.Row()+1, bVal).first;
108  }
109  }
110 }
111 
112 void ColumnSpanSet::set( const ScDocument& rDoc, SCTAB nTab, SCCOL nCol, const SingleColumnSpanSet& rSingleSet, bool bVal )
113 {
115  rSingleSet.getSpans(aSpans);
116  for (const auto& rSpan : aSpans)
117  set(rDoc, nTab, nCol, rSpan.mnRow1, rSpan.mnRow2, bVal);
118 }
119 
121  const ScDocument& rDoc, SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2, bool bVal)
122 {
123  if (!rDoc.ValidColRow(nCol1, nRow1) || !rDoc.ValidColRow(nCol2, nRow2))
124  return;
125 
126  if (nCol1 > nCol2 || nRow1 > nRow2)
127  return;
128 
129  const ScTable* pTab = rDoc.FetchTable(nTab);
130  if (!pTab)
131  return;
132 
133  nCol2 = pTab->ClampToAllocatedColumns(nCol2);
134  for (SCCOL nCol = nCol1; nCol <= nCol2; ++nCol)
135  {
136  ColumnType& rCol = getColumn(rDoc, nTab, nCol);
137 
138  const CellStoreType& rSrcCells = pTab->aCol[nCol].maCells;
139 
140  ColumnScanner aScanner(rCol.maSpans, bVal);
141  ParseBlock(rSrcCells.begin(), rSrcCells, aScanner, nRow1, nRow2);
142  }
143 }
144 
146 {
147  for (size_t nTab = 0; nTab < maTables.size(); ++nTab)
148  {
149  if (maTables[nTab].empty())
150  continue;
151 
152  const TableType& rTab = maTables[nTab];
153  for (size_t nCol = 0; nCol < rTab.size(); ++nCol)
154  {
155  if (!rTab[nCol])
156  continue;
157 
158  ac.startColumn(nTab, nCol);
159  const ColumnType& rCol = *rTab[nCol];
160  ColumnSpansType::const_iterator it = rCol.maSpans.begin(), itEnd = rCol.maSpans.end();
161  SCROW nRow1, nRow2;
162  nRow1 = it->first;
163  bool bVal = it->second;
164  for (++it; it != itEnd; ++it)
165  {
166  nRow2 = it->first-1;
167  ac.execute(ScAddress(nCol, nRow1, nTab), nRow2-nRow1+1, bVal);
168 
169  nRow1 = nRow2+1; // for the next iteration.
170  bVal = it->second;
171  }
172  }
173  }
174 }
175 
177 {
178  for (size_t nTab = 0; nTab < maTables.size(); ++nTab)
179  {
180  if (maTables[nTab].empty())
181  continue;
182 
183  const TableType& rTab = maTables[nTab];
184  for (SCCOL nCol = 0; nCol < static_cast<SCCOL>(rTab.size()); ++nCol)
185  {
186  if (!rTab[nCol])
187  continue;
188 
189  ScTable* pTab = rDoc.FetchTable(nTab);
190  if (!pTab)
191  continue;
192 
193  if (!rDoc.ValidCol(nCol) || nCol >= pTab->GetAllocatedColumnsCount())
194  {
195  // End the loop.
196  nCol = rTab.size();
197  continue;
198  }
199 
200  ScColumn& rColumn = pTab->aCol[nCol];
201  ac.startColumn(&rColumn);
202  const ColumnType& rCol = *rTab[nCol];
203  ColumnSpansType::const_iterator it = rCol.maSpans.begin(), itEnd = rCol.maSpans.end();
204  SCROW nRow1, nRow2;
205  nRow1 = it->first;
206  bool bVal = it->second;
207  for (++it; it != itEnd; ++it)
208  {
209  nRow2 = it->first-1;
210  ac.execute(nRow1, nRow2, bVal);
211 
212  nRow1 = nRow2+1; // for the next iteration.
213  bVal = it->second;
214  }
215  }
216  }
217 }
218 
219 namespace {
220 
221 class Scanner
222 {
224 public:
225  explicit Scanner(SingleColumnSpanSet::ColumnSpansType& rRanges) : mrRanges(rRanges) {}
226 
227  void operator() (const sc::CellStoreType::value_type& node, size_t nOffset, size_t nDataSize)
228  {
229  if (node.type == sc::element_type_empty)
230  return;
231 
232  size_t nRow = node.position + nOffset;
233  size_t nEndRow = nRow + nDataSize; // Last row of current block plus 1
234  mrRanges.insert_back(nRow, nEndRow, true);
235  }
236 };
237 
238 }
239 
241  : mrSheetLimits(rSheetLimits),
242  maSpans(0, rSheetLimits.GetMaxRowCount(), false) {}
243 
245 {
246  const CellStoreType& rCells = rColumn.maCells;
247  SCROW nCurRow = 0;
248  for (const auto& rCell : rCells)
249  {
250  SCROW nEndRow = nCurRow + rCell.size; // Last row of current block plus 1.
251  if (rCell.type != sc::element_type_empty)
252  maSpans.insert_back(nCurRow, nEndRow, true);
253 
254  nCurRow = nEndRow;
255  }
256 }
257 
258 void SingleColumnSpanSet::scan(const ScColumn& rColumn, SCROW nStart, SCROW nEnd)
259 {
260  const CellStoreType& rCells = rColumn.maCells;
261  Scanner aScanner(maSpans);
262  sc::ParseBlock(rCells.begin(), rCells, aScanner, nStart, nEnd);
263 }
264 
266  ColumnBlockConstPosition& rBlockPos, const ScColumn& rColumn, SCROW nStart, SCROW nEnd)
267 {
268  const CellStoreType& rCells = rColumn.maCells;
269  Scanner aScanner(maSpans);
270  rBlockPos.miCellPos = sc::ParseBlock(rBlockPos.miCellPos, rCells, aScanner, nStart, nEnd);
271 }
272 
273 void SingleColumnSpanSet::scan(const ScMarkData& rMark, SCTAB nTab, SCCOL nCol)
274 {
275  if (!rMark.GetTableSelect(nTab))
276  // This table is not selected. Nothing to scan.
277  return;
278 
279  ScRangeList aRanges = rMark.GetMarkedRangesForTab(nTab);
280  scan(aRanges, nTab, nCol);
281 }
282 
283 void SingleColumnSpanSet::scan(const ScRangeList& rRanges, SCTAB nTab, SCCOL nCol)
284 {
285  for (size_t i = 0, n = rRanges.size(); i < n; ++i)
286  {
287  const ScRange & rRange = rRanges[i];
288  if (nTab < rRange.aStart.Tab() || rRange.aEnd.Tab() < nTab)
289  continue;
290 
291  if (nCol < rRange.aStart.Col() || rRange.aEnd.Col() < nCol)
292  // This column is not in this range. Skip it.
293  continue;
294 
295  maSpans.insert_back(rRange.aStart.Row(), rRange.aEnd.Row()+1, true);
296  }
297 }
298 
299 void SingleColumnSpanSet::set(SCROW nRow1, SCROW nRow2, bool bVal)
300 {
301  maSpans.insert_back(nRow1, nRow2+1, bVal);
302 }
303 
304 void SingleColumnSpanSet::getRows(std::vector<SCROW> &rRows) const
305 {
306  std::vector<SCROW> aRows;
307 
308  SpansType aRanges;
309  getSpans(aRanges);
310  for (const auto& rRange : aRanges)
311  {
312  for (SCROW nRow = rRange.mnRow1; nRow <= rRange.mnRow2; ++nRow)
313  aRows.push_back(nRow);
314  }
315 
316  rRows.swap(aRows);
317 }
318 
320 {
321  SpansType aSpans = toSpanArray<SCROW,RowSpan>(maSpans);
322  rSpans.swap(aSpans);
323 }
324 
326 {
327  maSpans.swap(r.maSpans);
328 }
329 
331 {
332  // Empty if there's only the 0..rDoc.MaxRow() span with false.
333  ColumnSpansType::const_iterator it = maSpans.begin();
334  return (it->first == 0) && !(it->second) && (++it != maSpans.end()) && (it->first == mrSheetLimits.GetMaxRowCount());
335 }
336 
337 
339 {
340  for (SCTAB nTab = range.aStart.Tab(); nTab <= range.aEnd.Tab(); ++nTab)
341  {
342  ScTable* pTab = rDoc.FetchTable(nTab);
343  if (!pTab)
344  continue;
345 
346  SCCOL nEndCol = pTab->ClampToAllocatedColumns(range.aEnd.Col());
347  for (SCCOL nCol = range.aStart.Col(); nCol <= nEndCol; ++nCol)
348  {
349  if (!rDoc.ValidCol(nCol))
350  break;
351 
352  ScColumn& rColumn = pTab->aCol[nCol];
353  ac.startColumn(&rColumn);
354  ac.execute( range.aStart.Row(), range.aEnd.Row(), true );
355  }
356  }
357 }
358 
359 } // namespace sc
360 
361 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
CellStoreType::const_iterator miCellPos
ScAddress aStart
Definition: address.hxx:499
todo: It should be possible to have MarkArrays for each table, in order to enable "search all" across...
Definition: markdata.hxx:42
ColumnType & getColumn(const ScDocument &rDoc, SCTAB nTab, SCCOL nCol)
SCROW Row() const
Definition: address.hxx:261
size_t mnEnd
Definition: cellvalues.cxx:24
virtual void execute(const ScAddress &rPos, SCROW nLength, bool bVal)=0
virtual void startColumn(SCTAB nTab, SCCOL nCol)
ScTable * FetchTable(SCTAB nTab)
Definition: document.cxx:2521
void scan(const ScDocument &rDoc, SCTAB nTab, SCCOL nCol1, SCROW nRow1, SCCOL nCol2, SCROW nRow2, bool bVal)
Scan specified range in a specified sheet and mark all non-empty cells with specified boolean value...
mdds::flat_segment_tree< SCROW, bool > ColumnSpansType
StoreT::const_iterator ParseBlock(const typename StoreT::const_iterator &itPos, const StoreT &rStore, Func &rFunc, typename StoreT::size_type nStart, typename StoreT::size_type nEnd)
Generic algorithm to parse blocks of multi_type_vector either partially or fully. ...
sal_Int64 n
std::vector< std::optional< ColumnType > > TableType
mdds::flat_segment_tree< SCROW, bool > ColumnSpansType
ScAddress aEnd
Definition: address.hxx:500
SCROW GetMaxRowCount() const
Definition: sheetlimits.hxx:61
bool empty() const
Whether there isn't any row tagged.
mdds::multi_type_vector< CellFunc, CellStoreEvent > CellStoreType
ScSheetLimits const & mrSheetLimits
Keep track of spans in a single column only.
enumrange< T >::Iterator begin(enumrange< T >)
SC_DLLPUBLIC SCROW MaxRow() const
Definition: document.hxx:872
sal_Int32 SCCOLROW
a type capable of holding either SCCOL or SCROW
Definition: types.hxx:23
sc::CellStoreType maCells
Definition: column.hxx:129
void set(const ScDocument &rDoc, SCTAB nTab, SCCOL nCol, SCROW nRow, bool bVal)
SCTAB Tab() const
Definition: address.hxx:270
ColRowSpan(SCCOLROW nStart, SCCOLROW nEnd)
void executeColumnAction(ScDocument &rDoc, ColumnAction &ac) const
void getSpans(SpansType &rSpans) const
int i
sal_Int16 SCCOL
Definition: types.hxx:21
bool ValidCol(SCCOL nCol) const
Definition: document.hxx:874
void getRows(std::vector< SCROW > &rRows) const
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
const mdds::mtv::element_t element_type_empty
Definition: mtvelements.hxx:55
size_t size() const
Definition: rangelst.hxx:89
void set(SCROW nRow1, SCROW nRow2, bool bVal)
ScColContainer aCol
Definition: table.hxx:158
ColumnSpansType::const_iterator miPos
SCCOL Col() const
Definition: address.hxx:266
size_t mnStart
Definition: cellvalues.cxx:23
bool ValidColRow(SCCOL nCol, SCROW nRow) const
Definition: document.hxx:876
std::vector< TableType > maTables
sal_Int32 SCROW
Definition: types.hxx:17
RowSpan(SCROW nRow1, SCROW nRow2)
bool ValidRow(SCROW nRow) const
Definition: document.hxx:875
ScRangeList GetMarkedRangesForTab(SCTAB nTab) const
Get marked ranges with sheet-tab set to nTab.
Definition: markdata.cxx:471
std::vector< RowSpan > SpansType
ColumnType(SCROW nStart, SCROW nEnd, bool bInit)
void scan(const ScColumn &rColumn)
Scan an entire column and tag all non-empty cell positions.
void executeColumnAction(ScDocument &rDoc, sc::ColumnSpanSet::ColumnAction &ac) const
SCCOL ClampToAllocatedColumns(SCCOL nCol) const
Definition: table.hxx:1097
bool GetTableSelect(SCTAB nTab) const
Definition: markdata.cxx:183
SingleColumnSpanSet(ScSheetLimits const &)
void swap(SingleColumnSpanSet &r)
constexpr OUStringLiteral first
SCCOL GetAllocatedColumnsCount() const
Definition: table.hxx:1098
virtual void execute(SCROW nRow1, SCROW nRow2, bool bVal)=0
bool ValidTab(SCTAB nTab)
Definition: address.hxx:104
virtual void startColumn(ScColumn *pCol)=0
void executeAction(Action &ac) const
sal_Int16 SCTAB
Definition: types.hxx:22