sc/inc/column.hxx                  |    2 +-
 sc/inc/table.hxx                   |    8 ++++----
 sc/source/core/data/column.cxx     |    7 ++++---
 sc/source/core/data/document10.cxx |    2 +-
 sc/source/core/data/table1.cxx     |    5 +++--
 sc/source/core/data/table2.cxx     |   12 ++++++------
 sc/source/core/data/table3.cxx     |   20 +++++++++++++-------
 sc/source/core/data/table7.cxx     |    2 +-
 8 files changed, 33 insertions(+), 25 deletions(-)

New commits:
commit 9b06af2adddc49414adc135f3d08dcc88c896058
Author:     Luboš Luňák <l.lu...@collabora.com>
AuthorDate: Tue May 17 19:59:09 2022 +0200
Commit:     Luboš Luňák <l.lu...@collabora.com>
CommitDate: Wed May 18 06:36:18 2022 +0200

    make CreateColumnIfNotExists() non-const
    
    Generally const functions should not modify the data, so this
    generally should not be needed. Those functions that need to allocate
    a column because they can't/don't handle default values for
    non-existent columns well should go with const_cast, being
    an exception to the rule.
    
    Change-Id: I62706da5b447019542d6775f14064fa15b71f3c1
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134488
    Tested-by: Jenkins
    Reviewed-by: Luboš Luňák <l.lu...@collabora.com>

diff --git a/sc/inc/column.hxx b/sc/inc/column.hxx
index 01dc4e8611fa..4d6f2c8e01ac 100644
--- a/sc/inc/column.hxx
+++ b/sc/inc/column.hxx
@@ -379,7 +379,7 @@ public:
     bool       TestCopyScenarioTo( const ScColumn& rDestCol ) const;
     void        MarkScenarioIn( ScMarkData& rDestMark ) const;
 
-    void        CopyUpdated( const ScColumn& rPosCol, ScColumn& rDestCol ) 
const;
+    void        CopyUpdated( const ScColumn* pPosCol, ScColumn& rDestCol ) 
const;
 
     void        SwapCol(ScColumn& rCol);
     void        MoveTo(SCROW nStartRow, SCROW nEndRow, ScColumn& rCol);
diff --git a/sc/inc/table.hxx b/sc/inc/table.hxx
index e89dbd34dbc8..6c083e064add 100644
--- a/sc/inc/table.hxx
+++ b/sc/inc/table.hxx
@@ -157,7 +157,7 @@ class ScTable
 private:
     typedef ::std::vector< ScRange > ScRangeVec;
 
-    mutable ScColContainer aCol;
+    ScColContainer aCol;
 
     OUString aName;
     OUString aCodeName;
@@ -286,14 +286,14 @@ public:
 
     ScOutlineTable* GetOutlineTable()               { return 
pOutlineTable.get(); }
 
-    ScColumn& CreateColumnIfNotExists( const SCCOL nScCol ) const
+    ScColumn& CreateColumnIfNotExists( const SCCOL nScCol )
     {
         if ( nScCol >= aCol.size() )
             CreateColumnIfNotExistsImpl(nScCol);
         return aCol[nScCol];
     }
     // out-of-line the cold part of the function
-    void CreateColumnIfNotExistsImpl( const SCCOL nScCol ) const;
+    void CreateColumnIfNotExistsImpl( const SCCOL nScCol );
     sal_uInt64      GetCellCount() const;
     sal_uInt64      GetWeightedCount() const;
     sal_uInt64      GetWeightedCount(SCROW nStartRow, SCROW nEndRow) const;
@@ -584,7 +584,7 @@ public:
                             SCCOL nDestCol, SCROW nDestRow, SCTAB nDestTab );
 
     void        CopyScenarioFrom( const ScTable* pSrcTab );
-    void        CopyScenarioTo( const ScTable* pDestTab ) const;
+    void        CopyScenarioTo( ScTable* pDestTab ) const;
     bool        TestCopyScenarioTo( const ScTable* pDestTab ) const;
     void        MarkScenarioIn(ScMarkData& rMark, ScScenarioFlags nNeededBits) 
const;
     bool        HasScenarioRange( const ScRange& rRange ) const;
diff --git a/sc/source/core/data/column.cxx b/sc/source/core/data/column.cxx
index d9133e3ad55c..2353a476fe8a 100644
--- a/sc/source/core/data/column.cxx
+++ b/sc/source/core/data/column.cxx
@@ -1680,14 +1680,15 @@ void ScColumn::UndoToColumn(
         CopyToColumn(rCxt, nRow2+1, GetDoc().MaxRow(), 
InsertDeleteFlags::FORMULA, false, rColumn);
 }
 
-void ScColumn::CopyUpdated( const ScColumn& rPosCol, ScColumn& rDestCol ) const
+void ScColumn::CopyUpdated( const ScColumn* pPosCol, ScColumn& rDestCol ) const
 {
     // Copy cells from this column to the destination column only for those
-    // rows that are present in the position column (rPosCol).
+    // rows that are present in the position column (pPosCol).
 
     // First, mark all the non-empty cell ranges from the position column.
     sc::SingleColumnSpanSet aRangeSet(GetDoc().GetSheetLimits());
-    aRangeSet.scan(rPosCol);
+    if(pPosCol)
+        aRangeSet.scan(*pPosCol);
 
     // Now, copy cells from this column to the destination column for those
     // marked row ranges.
diff --git a/sc/source/core/data/document10.cxx 
b/sc/source/core/data/document10.cxx
index 4a9efa170285..ec61fa530952 100644
--- a/sc/source/core/data/document10.cxx
+++ b/sc/source/core/data/document10.cxx
@@ -991,7 +991,7 @@ std::optional<sc::ColumnIterator> 
ScDocument::GetColumnIterator( SCTAB nTab, SCC
 
 void ScDocument::CreateColumnIfNotExists( SCTAB nTab, SCCOL nCol )
 {
-    const ScTable* pTab = FetchTable(nTab);
+    ScTable* pTab = FetchTable(nTab);
     if (!pTab)
         return;
 
diff --git a/sc/source/core/data/table1.cxx b/sc/source/core/data/table1.cxx
index a423b2b4774c..619947b15f2a 100644
--- a/sc/source/core/data/table1.cxx
+++ b/sc/source/core/data/table1.cxx
@@ -2594,8 +2594,9 @@ void ScTable::DeleteEmptyBroadcasters()
 void ScTable::FillMatrix( ScMatrix& rMat, SCCOL nCol1, SCROW nRow1, SCCOL 
nCol2, SCROW nRow2, svl::SharedStringPool* pPool ) const
 {
     size_t nMatCol = 0;
+    nCol2 = ClampToAllocatedColumns(nCol2);
     for (SCCOL nCol = nCol1; nCol <= nCol2; ++nCol, ++nMatCol)
-        CreateColumnIfNotExists(nCol).FillMatrix(rMat, nMatCol, nRow1, nRow2, 
pPool);
+        aCol[nCol].FillMatrix(rMat, nMatCol, nRow1, nRow2, pPool);
 }
 
 void ScTable::InterpretDirtyCells( SCCOL nCol1, SCROW nRow1, SCCOL nCol2, 
SCROW nRow2 )
@@ -2718,7 +2719,7 @@ ScColumnsRange ScTable::GetColumnsRange(SCCOL nColBegin, 
SCCOL nColEnd) const
 }
 
 // out-of-line the cold part of the CreateColumnIfNotExists function
-void ScTable::CreateColumnIfNotExistsImpl( const SCCOL nScCol ) const
+void ScTable::CreateColumnIfNotExistsImpl( const SCCOL nScCol )
 {
     // When doing multi-threaded load of, e.g. XLS files, we can hit this, 
which calls
     // into SfxItemPool::Put, in parallel with other code that calls into 
SfxItemPool::Put,
diff --git a/sc/source/core/data/table2.cxx b/sc/source/core/data/table2.cxx
index 8d442dd2bd00..a290d07f4769 100644
--- a/sc/source/core/data/table2.cxx
+++ b/sc/source/core/data/table2.cxx
@@ -140,7 +140,7 @@ bool ScTable::TestInsertRow( SCCOL nStartCol, SCCOL 
nEndCol, SCROW nStartRow, SC
         bTest = pOutlineTable->TestInsertRow(nSize);
 
     for (SCCOL i=nStartCol; (i<=nEndCol) && bTest; i++)
-        bTest = CreateColumnIfNotExists(i).TestInsertRow(nStartRow, nSize);
+        bTest = 
const_cast<ScTable*>(this)->CreateColumnIfNotExists(i).TestInsertRow(nStartRow, 
nSize);
 
     return bTest;
 }
@@ -1531,10 +1531,9 @@ void ScTable::UndoToTable(
 
 void ScTable::CopyUpdated( const ScTable* pPosTab, ScTable* pDestTab ) const
 {
-    pPosTab->CreateColumnIfNotExists(aCol.size()-1);
     pDestTab->CreateColumnIfNotExists(aCol.size()-1);
     for (SCCOL i=0; i < aCol.size(); i++)
-        aCol[i].CopyUpdated( pPosTab->aCol[i], pDestTab->aCol[i] );
+        aCol[i].CopyUpdated( pPosTab->FetchColumn(i), pDestTab->aCol[i] );
 }
 
 void ScTable::InvalidateTableArea()
@@ -1548,7 +1547,7 @@ void ScTable::InvalidatePageBreaks()
     mbPageBreaksValid = false;
 }
 
-void ScTable::CopyScenarioTo( const ScTable* pDestTab ) const
+void ScTable::CopyScenarioTo( ScTable* pDestTab ) const
 {
     OSL_ENSURE( bScenario, "bScenario == FALSE" );
 
@@ -2808,9 +2807,10 @@ void ScTable::MergeSelectionPattern( 
ScMergePatternState& rState, const ScMarkDa
 
     for (const sc::ColRowSpan & rSpan : aSpans)
     {
-        for (SCCOLROW i = rSpan.mnStart; i <= rSpan.mnEnd; ++i)
+        SCCOL maxCol = ClampToAllocatedColumns(rSpan.mnEnd);
+        for (SCCOL i = rSpan.mnStart; i <= maxCol; ++i)
         {
-            CreateColumnIfNotExists(i).MergeSelectionPattern( rState, rMark, 
bDeep );
+            aCol[i].MergeSelectionPattern( rState, rMark, bDeep );
         }
     }
 }
diff --git a/sc/source/core/data/table3.cxx b/sc/source/core/data/table3.cxx
index a04f0b4c8541..c30032582c14 100644
--- a/sc/source/core/data/table3.cxx
+++ b/sc/source/core/data/table3.cxx
@@ -1738,20 +1738,26 @@ short ScTable::Compare(SCCOLROW nIndex1, SCCOLROW 
nIndex2) const
         do
         {
             SCCOL nCol = 
static_cast<SCCOL>(aSortParam.maKeyState[nSort].nField);
-            CreateColumnIfNotExists(nCol);
-            ScRefCellValue aCell1 = aCol[nCol].GetCellValue(nIndex1);
-            ScRefCellValue aCell2 = aCol[nCol].GetCellValue(nIndex2);
-            nRes = CompareCell(nSort, aCell1, nCol, nIndex1, aCell2, nCol, 
nIndex2);
+            nRes = 0;
+            if(nCol < GetAllocatedColumnsCount())
+            {
+                ScRefCellValue aCell1 = aCol[nCol].GetCellValue(nIndex1);
+                ScRefCellValue aCell2 = aCol[nCol].GetCellValue(nIndex2);
+                nRes = CompareCell(nSort, aCell1, nCol, nIndex1, aCell2, nCol, 
nIndex2);
+            }
         } while ( nRes == 0 && ++nSort < nMaxSorts && 
aSortParam.maKeyState[nSort].bDoSort );
     }
     else
     {
-        CreateColumnIfNotExists(std::max(nIndex1, nIndex2));
         do
         {
             SCROW nRow = aSortParam.maKeyState[nSort].nField;
-            ScRefCellValue aCell1 = aCol[nIndex1].GetCellValue(nRow);
-            ScRefCellValue aCell2 = aCol[nIndex2].GetCellValue(nRow);
+            ScRefCellValue aCell1;
+            ScRefCellValue aCell2;
+            if(nIndex1 < GetAllocatedColumnsCount())
+                aCell1 = aCol[nIndex1].GetCellValue(nRow);
+            if(nIndex2 < GetAllocatedColumnsCount())
+                aCell2 = aCol[nIndex2].GetCellValue(nRow);
             nRes = CompareCell( nSort, aCell1, static_cast<SCCOL>(nIndex1),
                     nRow, aCell2, static_cast<SCCOL>(nIndex2), nRow );
         } while ( nRes == 0 && ++nSort < nMaxSorts && 
aSortParam.maKeyState[nSort].bDoSort );
diff --git a/sc/source/core/data/table7.cxx b/sc/source/core/data/table7.cxx
index 30788b0daca7..f7576361c8a1 100644
--- a/sc/source/core/data/table7.cxx
+++ b/sc/source/core/data/table7.cxx
@@ -427,7 +427,7 @@ std::optional<sc::ColumnIterator> 
ScTable::GetColumnIterator( SCCOL nCol, SCROW
     if (!ValidCol(nCol))
         return {};
 
-    return CreateColumnIfNotExists(nCol).GetColumnIterator(nRow1, nRow2);
+    return 
const_cast<ScTable*>(this)->CreateColumnIfNotExists(nCol).GetColumnIterator(nRow1,
 nRow2);
 }
 
 bool ScTable::EnsureFormulaCellResults( const SCCOL nCol1, SCROW nRow1, const 
SCCOL nCol2, SCROW nRow2, bool bSkipRunning )

Reply via email to