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 ColumnNonEmptyRangesScanner
29 {
31  bool mbVal;
32 public:
33  ColumnNonEmptyRangesScanner(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  if( nRow1 > pTab->aCol[nCol].GetLastDataPos())
141  continue;
142 
143  ColumnNonEmptyRangesScanner aScanner(rCol.maSpans, bVal);
144  ParseBlock(rSrcCells.begin(), rSrcCells, aScanner, nRow1, nRow2);
145  }
146 }
147 
149 {
150  for (size_t nTab = 0; nTab < maTables.size(); ++nTab)
151  {
152  if (maTables[nTab].empty())
153  continue;
154 
155  ScTable* pTab = rDoc.FetchTable(nTab);
156  if (!pTab)
157  continue;
158 
159  const TableType& rTab = maTables[nTab];
160  for (SCCOL nCol = 0; nCol < static_cast<SCCOL>(rTab.size()); ++nCol)
161  {
162  if (!rTab[nCol])
163  continue;
164  if (nCol >= pTab->GetAllocatedColumnsCount())
165  break;
166 
167  ac.startColumn(nTab, nCol);
168  const ColumnType& rCol = *rTab[nCol];
169  ColumnSpansType::const_iterator it = rCol.maSpans.begin(), itEnd = rCol.maSpans.end();
170  SCROW nRow1, nRow2;
171  nRow1 = it->first;
172  bool bVal = it->second;
173  for (++it; it != itEnd; ++it)
174  {
175  nRow2 = it->first-1;
176  ac.execute(ScAddress(nCol, nRow1, nTab), nRow2-nRow1+1, bVal);
177 
178  nRow1 = nRow2+1; // for the next iteration.
179  bVal = it->second;
180  }
181  }
182  }
183 }
184 
186 {
187  for (size_t nTab = 0; nTab < maTables.size(); ++nTab)
188  {
189  if (maTables[nTab].empty())
190  continue;
191 
192  ScTable* pTab = rDoc.FetchTable(nTab);
193  if (!pTab)
194  continue;
195 
196  const TableType& rTab = maTables[nTab];
197  for (SCCOL nCol = 0; nCol < static_cast<SCCOL>(rTab.size()); ++nCol)
198  {
199  if (!rTab[nCol])
200  continue;
201  if (nCol >= pTab->GetAllocatedColumnsCount())
202  break;
203 
204  ScColumn& rColumn = pTab->aCol[nCol];
205  ac.startColumn(&rColumn);
206  const ColumnType& rCol = *rTab[nCol];
207  ColumnSpansType::const_iterator it = rCol.maSpans.begin(), itEnd = rCol.maSpans.end();
208  SCROW nRow1, nRow2;
209  nRow1 = it->first;
210  bool bVal = it->second;
211  for (++it; it != itEnd; ++it)
212  {
213  nRow2 = it->first-1;
214  ac.execute(nRow1, nRow2, bVal);
215 
216  nRow1 = nRow2+1; // for the next iteration.
217  bVal = it->second;
218  }
219  }
220  }
221 }
222 
223 namespace {
224 
225 class NonEmptyRangesScanner
226 {
228 public:
229  explicit NonEmptyRangesScanner(SingleColumnSpanSet::ColumnSpansType& rRanges) : mrRanges(rRanges) {}
230 
231  void operator() (const sc::CellStoreType::value_type& node, size_t nOffset, size_t nDataSize)
232  {
233  if (node.type == sc::element_type_empty)
234  return;
235 
236  size_t nRow = node.position + nOffset;
237  size_t nEndRow = nRow + nDataSize; // Last row of current block plus 1
238  mrRanges.insert_back(nRow, nEndRow, true);
239  }
240 };
241 
242 }
243 
245  : mrSheetLimits(rSheetLimits),
246  maSpans(0, rSheetLimits.GetMaxRowCount(), false) {}
247 
249 {
250  const CellStoreType& rCells = rColumn.maCells;
251  SCROW nCurRow = 0;
252  for (const auto& rCell : rCells)
253  {
254  SCROW nEndRow = nCurRow + rCell.size; // Last row of current block plus 1.
255  if (rCell.type != sc::element_type_empty)
256  maSpans.insert_back(nCurRow, nEndRow, true);
257 
258  nCurRow = nEndRow;
259  }
260 }
261 
262 void SingleColumnSpanSet::scan(const ScColumn& rColumn, SCROW nStart, SCROW nEnd)
263 {
264  if( nStart > rColumn.GetLastDataPos())
265  return;
266  const CellStoreType& rCells = rColumn.maCells;
267  NonEmptyRangesScanner aScanner(maSpans);
268  sc::ParseBlock(rCells.begin(), rCells, aScanner, nStart, nEnd);
269 }
270 
272  ColumnBlockConstPosition& rBlockPos, const ScColumn& rColumn, SCROW nStart, SCROW nEnd)
273 {
274  if( nStart > rColumn.GetLastDataPos())
275  return;
276  const CellStoreType& rCells = rColumn.maCells;
277  NonEmptyRangesScanner aScanner(maSpans);
278  rBlockPos.miCellPos = sc::ParseBlock(rBlockPos.miCellPos, rCells, aScanner, nStart, nEnd);
279 }
280 
281 void SingleColumnSpanSet::scan(const ScMarkData& rMark, SCTAB nTab, SCCOL nCol)
282 {
283  if (!rMark.GetTableSelect(nTab))
284  // This table is not selected. Nothing to scan.
285  return;
286 
287  ScRangeList aRanges = rMark.GetMarkedRangesForTab(nTab);
288  scan(aRanges, nTab, nCol);
289 }
290 
291 void SingleColumnSpanSet::scan(const ScRangeList& rRanges, SCTAB nTab, SCCOL nCol)
292 {
293  for (size_t i = 0, n = rRanges.size(); i < n; ++i)
294  {
295  const ScRange & rRange = rRanges[i];
296  if (nTab < rRange.aStart.Tab() || rRange.aEnd.Tab() < nTab)
297  continue;
298 
299  if (nCol < rRange.aStart.Col() || rRange.aEnd.Col() < nCol)
300  // This column is not in this range. Skip it.
301  continue;
302 
303  maSpans.insert_back(rRange.aStart.Row(), rRange.aEnd.Row()+1, true);
304  }
305 }
306 
307 void SingleColumnSpanSet::set(SCROW nRow1, SCROW nRow2, bool bVal)
308 {
309  maSpans.insert_back(nRow1, nRow2+1, bVal);
310 }
311 
312 void SingleColumnSpanSet::getRows(std::vector<SCROW> &rRows) const
313 {
314  std::vector<SCROW> aRows;
315 
316  SpansType aRanges;
317  getSpans(aRanges);
318  for (const auto& rRange : aRanges)
319  {
320  for (SCROW nRow = rRange.mnRow1; nRow <= rRange.mnRow2; ++nRow)
321  aRows.push_back(nRow);
322  }
323 
324  rRows.swap(aRows);
325 }
326 
328 {
329  SpansType aSpans = toSpanArray<SCROW,RowSpan>(maSpans);
330  rSpans.swap(aSpans);
331 }
332 
334 {
335  maSpans.swap(r.maSpans);
336 }
337 
339 {
340  // Empty if there's only the 0..rDoc.MaxRow() span with false.
341  ColumnSpansType::const_iterator it = maSpans.begin();
342  return (it->first == 0) && !(it->second) && (++it != maSpans.end()) && (it->first == mrSheetLimits.GetMaxRowCount());
343 }
344 
345 
347 {
348  for (SCTAB nTab = range.aStart.Tab(); nTab <= range.aEnd.Tab(); ++nTab)
349  {
350  ScTable* pTab = rDoc.FetchTable(nTab);
351  if (!pTab)
352  continue;
353 
354  SCCOL nEndCol = pTab->ClampToAllocatedColumns(range.aEnd.Col());
355  for (SCCOL nCol = range.aStart.Col(); nCol <= nEndCol; ++nCol)
356  {
357  if (!rDoc.ValidCol(nCol))
358  break;
359 
360  ScColumn& rColumn = pTab->aCol[nCol];
361  ac.startColumn(&rColumn);
362  ac.execute( range.aStart.Row(), range.aEnd.Row(), true );
363  }
364  }
365 }
366 
367 } // namespace sc
368 
369 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
CellStoreType::const_iterator miCellPos
ScAddress aStart
Definition: address.hxx:497
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:274
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:2536
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:498
SCROW GetMaxRowCount() const
Definition: sheetlimits.hxx:66
bool empty() const
Whether there isn't any row tagged.
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:891
sal_Int32 SCCOLROW
a type capable of holding either SCCOL or SCROW
Definition: types.hxx:23
sc::CellStoreType maCells
Definition: column.hxx:196
void set(const ScDocument &rDoc, SCTAB nTab, SCCOL nCol, SCROW nRow, bool bVal)
SCTAB Tab() const
Definition: address.hxx:283
SCROW GetLastDataPos() const
Definition: column2.cxx:1384
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:897
CAUTION! The following defines must be in the same namespace as the respective type.
void getRows(std::vector< SCROW > &rRows) const
void executeAction(ScDocument &rDoc, Action &ac) 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:57
size_t size() const
Definition: rangelst.hxx:89
void set(SCROW nRow1, SCROW nRow2, bool bVal)
ScColContainer aCol
Definition: table.hxx:160
ColumnSpansType::const_iterator miPos
SCCOL Col() const
Definition: address.hxx:279
size_t mnStart
Definition: cellvalues.cxx:23
bool ValidColRow(SCCOL nCol, SCROW nRow) const
Definition: document.hxx:899
std::vector< TableType > maTables
sal_Int32 SCROW
Definition: types.hxx:17
mdds::mtv::soa::multi_type_vector< CellFunc, CellStoreTrait > CellStoreType
RowSpan(SCROW nRow1, SCROW nRow2)
bool ValidRow(SCROW nRow) const
Definition: document.hxx:898
ScRangeList GetMarkedRangesForTab(SCTAB nTab) const
Get marked ranges with sheet-tab set to nTab.
Definition: markdata.cxx:457
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:1126
bool GetTableSelect(SCTAB nTab) const
Definition: markdata.cxx:169
SingleColumnSpanSet(ScSheetLimits const &)
void swap(SingleColumnSpanSet &r)
constexpr OUStringLiteral first
SCCOL GetAllocatedColumnsCount() const
Definition: table.hxx:1127
virtual void execute(SCROW nRow1, SCROW nRow2, bool bVal)=0
bool ValidTab(SCTAB nTab)
Definition: address.hxx:111
virtual void startColumn(ScColumn *pCol)=0
sal_Int16 SCTAB
Definition: types.hxx:22
bool m_bDetectedRangeSegmentation false