sc/source/filter/excel/tokstack.cxx |  481 +++++++++++++-----------------------
 sc/source/filter/inc/tokstack.hxx   |   11 
 2 files changed, 181 insertions(+), 311 deletions(-)

New commits:
commit aae31a7d5d6e2b29b09579d1dbae37de48375d0e
Author:     Mike Kaganski <[email protected]>
AuthorDate: Mon Dec 1 18:58:29 2025 +0500
Commit:     Mike Kaganski <[email protected]>
CommitDate: Sat Dec 6 07:47:27 2025 +0100

    Simplify TokenPool using vector
    
    Change-Id: Id266f90b3243a99fd1752869570ab547d3225eed
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/195107
    Reviewed-by: Mike Kaganski <[email protected]>
    Tested-by: Jenkins

diff --git a/sc/source/filter/excel/tokstack.cxx 
b/sc/source/filter/excel/tokstack.cxx
index a0e3974caafb..3fd7a40bce0d 100644
--- a/sc/source/filter/excel/tokstack.cxx
+++ b/sc/source/filter/excel/tokstack.cxx
@@ -20,6 +20,7 @@
 #include <tokstack.hxx>
 #include <scmatrix.hxx>
 
+#include <comphelper/scopeguard.hxx>
 #include <svl/sharedstringpool.hxx>
 #include <sal/log.hxx>
 #include <osl/diagnose.h>
@@ -50,11 +51,7 @@ TokenPool::TokenPool( svl::SharedStringPool& rSPool ) :
     nP_Id = 256;
     pP_Id.reset( new sal_uInt16[ nP_Id ] );
 
-    // pool for Ids
-    nElement = 32;
-    pElement.reset( new sal_uInt16[ nElement ] );
-    pType.reset( new E_TYPE[ nElement ] );
-    pSize.reset( new sal_uInt16[ nElement ] );
+    maElements.reserve(32);
     nP_IdLast = 0;
 
     nP_Matrix = 16;
@@ -104,47 +101,15 @@ bool TokenPool::GrowId()
     return true;
 }
 
-bool TokenPool::CheckElementOrGrow()
+bool TokenPool::CanAddElement()
 {
-    // Last possible ID to be assigned somewhere is nElementCurrent+1
-    if (nElementCurrent + 1 == nScTokenOff - 1)
+    // Last possible ID to be assigned somewhere is maElements.size()+1
+    if (maElements.size() + 1 == nScTokenOff - 1)
     {
-        SAL_WARN("sc.filter","TokenPool::CheckElementOrGrow - last possible ID 
" << nElementCurrent+1);
+        SAL_WARN("sc.filter","TokenPool::CheckElementOrGrow - last possible ID 
" << maElements.size()+1);
         return false;
     }
 
-    if (nElementCurrent >= nElement)
-        return GrowElement();
-
-    return true;
-}
-
-bool TokenPool::GrowElement()
-{
-    sal_uInt16 nElementNew = lcl_canGrow( nElement);
-    if (!nElementNew)
-        return false;
-
-    std::unique_ptr<sal_uInt16[]> pElementNew(new (::std::nothrow) sal_uInt16[ 
nElementNew ]);
-    std::unique_ptr<E_TYPE[]> pTypeNew(new (::std::nothrow) E_TYPE[ 
nElementNew ]);
-    std::unique_ptr<sal_uInt16[]> pSizeNew(new (::std::nothrow) sal_uInt16[ 
nElementNew ]);
-    if (!pElementNew || !pTypeNew || !pSizeNew)
-    {
-        return false;
-    }
-
-    for( sal_uInt16 nL = 0 ; nL < nElement ; nL++ )
-    {
-        pElementNew[ nL ] = pElement[ nL ];
-        pTypeNew[ nL ] = pType[ nL ];
-        pSizeNew[ nL ] = pSize[ nL ];
-    }
-
-    nElement = nElementNew;
-
-    pElement = std::move( pElementNew );
-    pType = std::move( pTypeNew );
-    pSize = std::move( pSizeNew );
     return true;
 }
 
@@ -169,164 +134,119 @@ bool TokenPool::GrowMatrix()
 
 bool TokenPool::GetElement( const sal_uInt16 nId, ScTokenArray* pScToken )
 {
-    if (nId >= nElementCurrent)
+    if (nId >= maElements.size())
     {
-        SAL_WARN("sc.filter","TokenPool::GetElement - Id too large, " << nId 
<< " >= " << nElementCurrent);
+        SAL_WARN("sc.filter","TokenPool::GetElement - Id too large, " << nId 
<< " >= " << maElements.size());
         return false;
     }
 
-    bool bRet = true;
-    if( pType[ nId ] == T_Id )
-        bRet = GetElementRek( nId, pScToken );
-    else
+    switch ([[maybe_unused]] const auto [n, eType, _] = maElements[nId]; eType)
     {
-        switch( pType[ nId ] )
-        {
-            case T_Str:
-                {
-                    sal_uInt16 n = pElement[ nId ];
-                    auto* p = ppP_Str.getIfInRange( n );
-                    if (p)
-                        pScToken->AddString(mrStringPool.intern(**p));
-                    else
-                        bRet = false;
-                }
-                break;
-            case T_D:
-                {
-                    sal_uInt16 n = pElement[ nId ];
-                    if (n < pP_Dbl.m_writemark)
-                        pScToken->AddDouble( pP_Dbl[ n ] );
-                    else
-                        bRet = false;
-                }
-                break;
-            case T_Err:
-                break;
+        case T_Id:
+            return GetElementRek(nId, pScToken);
+        case T_Str:
+            if (auto* p = ppP_Str.getIfInRange(n))
+            {
+                pScToken->AddString(mrStringPool.intern(**p));
+                return true;
+            }
+            break;
+        case T_D:
+            if (n < pP_Dbl.m_writemark)
+            {
+                pScToken->AddDouble(pP_Dbl[n]);
+                return true;
+            }
+            break;
+        case T_Err:
+            return true;
 /* TODO: in case we had FormulaTokenArray::AddError() */
 #if 0
-                {
-                    sal_uInt16 n = pElement[ nId ];
-                    if (n < nP_Err)
-                        pScToken->AddError( pP_Err[ n ] );
-                    else
-                        bRet = false;
-                }
+            if (n < nP_Err)
+            {
+                pScToken->AddError(pP_Err[n]);
+                return true;
+            }
+            break;
 #endif
-            case T_RefC:
-                {
-                    sal_uInt16 n = pElement[ nId ];
-                    auto p = ppP_RefTr.getIfInRange(n);
-                    if (p)
-                        pScToken->AddSingleReference( **p );
-                    else
-                        bRet = false;
-                }
-                break;
-            case T_RefA:
-                {
-                    sal_uInt16 n = pElement[ nId ];
-                    if (n < ppP_RefTr.m_writemark && ppP_RefTr[ n ] && n+1 < 
ppP_RefTr.m_writemark && ppP_RefTr[ n + 1 ])
-                    {
-                        ScComplexRefData aScComplexRefData;
-                        aScComplexRefData.Ref1 = *ppP_RefTr[ n ];
-                        aScComplexRefData.Ref2 = *ppP_RefTr[ n + 1 ];
-                        pScToken->AddDoubleReference( aScComplexRefData );
-                    }
-                    else
-                        bRet = false;
-                }
-                break;
-            case T_RN:
+        case T_RefC:
+            if (auto p = ppP_RefTr.getIfInRange(n))
             {
-                sal_uInt16 n = pElement[nId];
-                if (n < maRangeNames.size())
-                {
-                    const RangeName& r = maRangeNames[n];
-                    pScToken->AddRangeName(r.mnIndex, r.mnSheet);
-                }
+                pScToken->AddSingleReference(**p);
+                return true;
             }
             break;
-            case T_Ext:
-                {
-                    sal_uInt16 n = pElement[ nId ];
-                    auto       p = ppP_Ext.getIfInRange(n);
-
-                    if( p )
-                    {
-                        if( (*p)->eId == ocEuroConvert )
-                            pScToken->AddOpCode( (*p)->eId );
-                        else
-                            pScToken->AddExternal( (*p)->aText, (*p)->eId );
-                    }
-                    else
-                        bRet = false;
-                }
-                break;
-            case T_Nlf:
-                {
-                    sal_uInt16 n = pElement[ nId ];
-                    auto       p = ppP_Nlf.getIfInRange(n);
-
-                    if( p )
-                        pScToken->AddColRowName( **p );
-                    else
-                        bRet = false;
-                }
-                break;
-            case T_Matrix:
-                {
-                    sal_uInt16      n = pElement[ nId ];
-                    ScMatrix*       p = ( n < nP_Matrix )? ppP_Matrix[ n ] : 
nullptr;
-
-                    if( p )
-                        pScToken->AddMatrix( p );
-                    else
-                        bRet = false;
-                }
-                break;
-            case T_ExtName:
+        case T_RefA:
+            if (n + sal_uInt32(1) < ppP_RefTr.m_writemark && ppP_RefTr[n] && 
ppP_RefTr[n + 1])
             {
-                sal_uInt16 n = pElement[nId];
-                if (n < maExtNames.size())
-                {
-                    const ExtName& r = maExtNames[n];
-                    pScToken->AddExternalName(r.mnFileId, mrStringPool.intern( 
r.maName));
-                }
-                else
-                    bRet = false;
+                ScComplexRefData aScComplexRefData;
+                aScComplexRefData.Ref1 = *ppP_RefTr[n];
+                aScComplexRefData.Ref2 = *ppP_RefTr[n + 1];
+                pScToken->AddDoubleReference(aScComplexRefData);
+                return true;
             }
             break;
-            case T_ExtRefC:
+        case T_RN:
+            if (n < maRangeNames.size())
             {
-                sal_uInt16 n = pElement[nId];
-                if (n < maExtCellRefs.size())
-                {
-                    const ExtCellRef& r = maExtCellRefs[n];
-                    pScToken->AddExternalSingleReference(r.mnFileId, 
mrStringPool.intern( r.maTabName), r.maRef);
-                }
-                else
-                    bRet = false;
+                const RangeName& r = maRangeNames[n];
+                pScToken->AddRangeName(r.mnIndex, r.mnSheet);
+                return true;
             }
             break;
-            case T_ExtRefA:
+        case T_Ext:
+            if (auto p = ppP_Ext.getIfInRange(n))
             {
-                sal_uInt16 n = pElement[nId];
-                if (n < maExtAreaRefs.size())
-                {
-                    const ExtAreaRef& r = maExtAreaRefs[n];
-                    pScToken->AddExternalDoubleReference(r.mnFileId, 
mrStringPool.intern( r.maTabName), r.maRef);
-                }
+                if ((*p)->eId == ocEuroConvert)
+                    pScToken->AddOpCode((*p)->eId);
                 else
-                    bRet = false;
+                    pScToken->AddExternal((*p)->aText, (*p)->eId);
+                return true;
             }
             break;
-            default:
-                OSL_FAIL("-TokenPool::GetElement(): undefined state!?");
-                bRet = false;
-        }
+        case T_Nlf:
+            if (auto p = ppP_Nlf.getIfInRange(n))
+            {
+                pScToken->AddColRowName(**p);
+                return true;
+            }
+            break;
+        case T_Matrix:
+            if (ScMatrix* p = (n < nP_Matrix) ? ppP_Matrix[n] : nullptr)
+            {
+                pScToken->AddMatrix(p);
+                return true;
+            }
+            break;
+        case T_ExtName:
+            if (n < maExtNames.size())
+            {
+                const ExtName& r = maExtNames[n];
+                pScToken->AddExternalName(r.mnFileId, 
mrStringPool.intern(r.maName));
+                return true;
+            }
+            break;
+        case T_ExtRefC:
+            if (n < maExtCellRefs.size())
+            {
+                const ExtCellRef& r = maExtCellRefs[n];
+                pScToken->AddExternalSingleReference(r.mnFileId, 
mrStringPool.intern(r.maTabName), r.maRef);
+                return true;
+            }
+            break;
+        case T_ExtRefA:
+            if (n < maExtAreaRefs.size())
+            {
+                const ExtAreaRef& r = maExtAreaRefs[n];
+                pScToken->AddExternalDoubleReference(r.mnFileId, 
mrStringPool.intern(r.maTabName), r.maRef);
+                return true;
+            }
+            break;
+        default:
+            OSL_FAIL("-TokenPool::GetElement(): undefined state!?");
+            break;
     }
-    return bRet;
+    return false;
 }
 
 bool TokenPool::GetElementRek( const sal_uInt16 nId, ScTokenArray* pScToken )
@@ -334,108 +254,85 @@ bool TokenPool::GetElementRek( const sal_uInt16 nId, 
ScTokenArray* pScToken )
 #ifdef DBG_UTIL
     m_nRek++;
     OSL_ENSURE(m_nRek <= nP_Id, "*TokenPool::GetElement(): recursion loops!?");
+    comphelper::ScopeGuard decreaseRek([&]() { m_nRek--; });
 #endif
 
-    OSL_ENSURE( nId < nElementCurrent, "*TokenPool::GetElementRek(): nId >= 
nElementCurrent" );
+    OSL_ENSURE(nId < maElements.size(), "*TokenPool::GetElementRek(): nId >= 
maElements.size()");
 
-    if (nId >= nElementCurrent)
+    if (nId >= maElements.size())
     {
-        SAL_WARN("sc.filter", "*TokenPool::GetElementRek(): nId >= 
nElementCurrent");
-#ifdef DBG_UTIL
-        m_nRek--;
-#endif
+        SAL_WARN("sc.filter", "*TokenPool::GetElementRek(): nId >= 
maElements.size()");
         return false;
     }
 
-    if (pType[ nId ] != T_Id)
+    auto [nFirstId, eType, nCnt] = maElements[nId];
+    if (eType != T_Id)
     {
         SAL_WARN("sc.filter", "-TokenPool::GetElementRek(): pType[ nId ] != 
T_Id");
-#ifdef DBG_UTIL
-        m_nRek--;
-#endif
         return false;
     }
 
-    bool bRet = true;
-    sal_uInt16      nCnt = pSize[ nId ];
-    sal_uInt16 nFirstId = pElement[ nId ];
     if (nFirstId >= nP_Id)
     {
         SAL_WARN("sc.filter", "TokenPool::GetElementRek: nFirstId >= nP_Id");
-        nCnt = 0;
-        bRet = false;
+        return false;
     }
-    sal_uInt16* pCurrent = nCnt ? &pP_Id[ nFirstId ] : nullptr;
+    bool bRet = true;
     if (nCnt > nP_Id - nFirstId)
     {
         SAL_WARN("sc.filter", "TokenPool::GetElementRek: nCnt > nP_Id - 
nFirstId");
         nCnt = nP_Id - nFirstId;
         bRet = false;
     }
-    for( ; nCnt > 0 ; nCnt--, pCurrent++ )
+    for (sal_uInt16* pCurrent = &pP_Id[nFirstId]; nCnt > 0; nCnt--, pCurrent++)
     {
-        assert(pCurrent);
         if( *pCurrent < nScTokenOff )
         {// recursion or not?
-            if (*pCurrent >= nElementCurrent)
+            if (*pCurrent >= maElements.size())
             {
-                SAL_WARN("sc.filter", "TokenPool::GetElementRek: *pCurrent >= 
nElementCurrent");
+                SAL_WARN("sc.filter", "TokenPool::GetElementRek: *pCurrent >= 
maElements.size()");
                 bRet = false;
             }
             else
             {
-                if (pType[ *pCurrent ] == T_Id)
-                    bRet = GetElementRek( *pCurrent, pScToken );
-                else
-                    bRet = GetElement( *pCurrent, pScToken );
+                bRet = GetElement( *pCurrent, pScToken );
             }
         }
         else    // elementary SC_Token
             pScToken->AddOpCode( static_cast<DefTokenId>( *pCurrent - 
nScTokenOff ) );
     }
 
-#ifdef DBG_UTIL
-    m_nRek--;
-#endif
     return bRet;
 }
 
 void TokenPool::operator >>( TokenId& rId )
 {
-    rId = static_cast<TokenId>( nElementCurrent + 1 );
+    rId = static_cast<TokenId>(maElements.size() + 1);
 
-    if (!CheckElementOrGrow())
+    if (!CanAddElement())
         return;
+    // Start of Token-sequence, Typeinfo, from nP_IdLast to nP_IdCurrent-1 -> 
length of the sequence
+    maElements.emplace_back(nP_IdLast, T_Id, nP_IdCurrent - nP_IdLast);
 
-    pElement[ nElementCurrent ] = nP_IdLast;    // Start of Token-sequence
-    pType[ nElementCurrent ] = T_Id;            // set Typeinfo
-    pSize[ nElementCurrent ] = nP_IdCurrent - nP_IdLast;
-        // write from nP_IdLast to nP_IdCurrent-1 -> length of the sequence
-
-    nElementCurrent++;          // start value for next sequence
     nP_IdLast = nP_IdCurrent;
 }
 
 TokenId TokenPool::Store( const double& rDouble )
 {
-    if (!CheckElementOrGrow())
-        return static_cast<const TokenId>(nElementCurrent+1);
+    if (!CanAddElement())
+        return static_cast<const TokenId>(maElements.size() + 1);
 
     if( pP_Dbl.m_writemark >= pP_Dbl.m_capacity )
         if (!pP_Dbl.Grow())
-            return static_cast<const TokenId>(nElementCurrent+1);
+            return static_cast<const TokenId>(maElements.size() + 1);
 
-    pElement[ nElementCurrent ] = pP_Dbl.m_writemark;    // Index in 
Double-Array
-    pType[ nElementCurrent ] = T_D;             // set Typeinfo Double
+    // Index in Double-Array, Typeinfo Double, size does not matter
+    maElements.emplace_back(pP_Dbl.m_writemark, T_D, 1);
 
     pP_Dbl[ pP_Dbl.m_writemark ] = rDouble;
-
-    pSize[ nElementCurrent ] = 1;               // does not matter
-
-    nElementCurrent++;
     pP_Dbl.m_writemark++;
 
-    return static_cast<const TokenId>(nElementCurrent); // return old value + 
1!
+    return static_cast<const TokenId>(maElements.size()); // return old value 
+ 1!
 }
 
 TokenId TokenPool::Store( const sal_uInt16 nIndex )
@@ -446,15 +343,12 @@ TokenId TokenPool::Store( const sal_uInt16 nIndex )
 TokenId TokenPool::Store( const OUString& rString )
 {
     // mostly copied to Store( const char* ), to avoid a temporary string
-    if (!CheckElementOrGrow())
-        return static_cast<const TokenId>(nElementCurrent+1);
+    if (!CanAddElement())
+        return static_cast<const TokenId>(maElements.size() + 1);
 
     if( ppP_Str.m_writemark >= ppP_Str.m_capacity )
         if (!ppP_Str.Grow())
-            return static_cast<const TokenId>(nElementCurrent+1);
-
-    pElement[ nElementCurrent ] = ppP_Str.m_writemark;    // Index in 
String-Array
-    pType[ nElementCurrent ] = T_Str;           // set Typeinfo String
+            return static_cast<const TokenId>(maElements.size() + 1);
 
     // create String
     if( !ppP_Str[ ppP_Str.m_writemark ] )
@@ -464,49 +358,48 @@ TokenId TokenPool::Store( const OUString& rString )
         //...copy otherwise
         *ppP_Str[ ppP_Str.m_writemark ] = rString;
 
-    /* attention truncate to 16 bits */
-    pSize[ nElementCurrent ] = static_cast<sal_uInt16>(ppP_Str[ 
ppP_Str.m_writemark ]->getLength());
+    // Index in String-Array, Typeinfo String, size (attention truncate to 16 
bits)
+    maElements.emplace_back(ppP_Str.m_writemark, T_Str,
+                           
static_cast<sal_uInt16>(ppP_Str[ppP_Str.m_writemark]->getLength()));
 
-    nElementCurrent++;
     ppP_Str.m_writemark++;
 
-    return static_cast<const TokenId>(nElementCurrent); // return old value + 
1!
+    return static_cast<const TokenId>(maElements.size()); // return old value 
+ 1!
 }
 
 TokenId TokenPool::Store( const ScSingleRefData& rTr )
 {
-    if (!CheckElementOrGrow())
-        return static_cast<const TokenId>(nElementCurrent+1);
+    if (!CanAddElement())
+        return static_cast<const TokenId>(maElements.size() + 1);
 
     if( ppP_RefTr.m_writemark >= ppP_RefTr.m_capacity )
         if (!ppP_RefTr.Grow())
-            return static_cast<const TokenId>(nElementCurrent+1);
+            return static_cast<const TokenId>(maElements.size() + 1);
 
-    pElement[ nElementCurrent ] = ppP_RefTr.m_writemark;
-    pType[ nElementCurrent ] = T_RefC;          // set Typeinfo Cell-Ref
+    // Typeinfo Cell-Ref
+    maElements.emplace_back(ppP_RefTr.m_writemark, T_RefC, 0);
 
     if( !ppP_RefTr[ ppP_RefTr.m_writemark ] )
         ppP_RefTr[ ppP_RefTr.m_writemark ].reset( new ScSingleRefData( rTr ) );
     else
         *ppP_RefTr[ ppP_RefTr.m_writemark ] = rTr;
 
-    nElementCurrent++;
     ppP_RefTr.m_writemark++;
 
-    return static_cast<const TokenId>(nElementCurrent); // return old value + 
1!
+    return static_cast<const TokenId>(maElements.size()); // return old value 
+ 1!
 }
 
 TokenId TokenPool::Store( const ScComplexRefData& rTr )
 {
-    if (!CheckElementOrGrow())
-        return static_cast<const TokenId>(nElementCurrent+1);
+    if (!CanAddElement())
+        return static_cast<const TokenId>(maElements.size() + 1);
 
     if( ppP_RefTr.m_writemark + 1 >= ppP_RefTr.m_capacity )
         if (!ppP_RefTr.Grow(2))
-            return static_cast<const TokenId>(nElementCurrent+1);
+            return static_cast<const TokenId>(maElements.size() + 1);
 
-    pElement[ nElementCurrent ] = ppP_RefTr.m_writemark;
-    pType[ nElementCurrent ] = T_RefA;          // setTypeinfo Area-Ref
+    // setTypeinfo Area-Ref
+    maElements.emplace_back(ppP_RefTr.m_writemark, T_RefA, 0);
 
     if( !ppP_RefTr[ ppP_RefTr.m_writemark ] )
         ppP_RefTr[ ppP_RefTr.m_writemark ].reset( new ScSingleRefData( 
rTr.Ref1 ) );
@@ -520,22 +413,20 @@ TokenId TokenPool::Store( const ScComplexRefData& rTr )
         *ppP_RefTr[ ppP_RefTr.m_writemark ] = rTr.Ref2;
     ppP_RefTr.m_writemark++;
 
-    nElementCurrent++;
-
-    return static_cast<const TokenId>(nElementCurrent); // return old value + 
1!
+    return static_cast<const TokenId>(maElements.size()); // return old value 
+ 2!
 }
 
 TokenId TokenPool::Store( const DefTokenId e, const OUString& r )
 {
-    if (!CheckElementOrGrow())
-        return static_cast<const TokenId>(nElementCurrent+1);
+    if (!CanAddElement())
+        return static_cast<const TokenId>(maElements.size() + 1);
 
     if( ppP_Ext.m_writemark >= ppP_Ext.m_capacity )
         if (!ppP_Ext.Grow())
-            return static_cast<const TokenId>(nElementCurrent+1);
+            return static_cast<const TokenId>(maElements.size() + 1);
 
-    pElement[ nElementCurrent ] = ppP_Ext.m_writemark;
-    pType[ nElementCurrent ] = T_Ext;           // set Typeinfo String
+    // Typeinfo "something unknown with function name"
+    maElements.emplace_back(ppP_Ext.m_writemark, T_Ext, 0);
 
     if( ppP_Ext[ ppP_Ext.m_writemark ] )
     {
@@ -545,23 +436,21 @@ TokenId TokenPool::Store( const DefTokenId e, const 
OUString& r )
     else
         ppP_Ext[ ppP_Ext.m_writemark ].reset( new EXTCONT( e, r ) );
 
-    nElementCurrent++;
     ppP_Ext.m_writemark++;
 
-    return static_cast<const TokenId>(nElementCurrent); // return old value + 
1!
+    return static_cast<const TokenId>(maElements.size()); // return old value 
+ 1!
 }
 
 TokenId TokenPool::StoreNlf( const ScSingleRefData& rTr )
 {
-    if (!CheckElementOrGrow())
-        return static_cast<const TokenId>(nElementCurrent+1);
+    if (!CanAddElement())
+        return static_cast<const TokenId>(maElements.size() + 1);
 
     if( ppP_Nlf.m_writemark >= ppP_Nlf.m_capacity )
         if (!ppP_Nlf.Grow())
-            return static_cast<const TokenId>(nElementCurrent+1);
+            return static_cast<const TokenId>(maElements.size() + 1);
 
-    pElement[ nElementCurrent ] = ppP_Nlf.m_writemark;
-    pType[ nElementCurrent ] = T_Nlf;
+    maElements.emplace_back(ppP_Nlf.m_writemark, T_Nlf, 0);
 
     if( ppP_Nlf[ ppP_Nlf.m_writemark ] )
     {
@@ -570,77 +459,67 @@ TokenId TokenPool::StoreNlf( const ScSingleRefData& rTr )
     else
         ppP_Nlf[ ppP_Nlf.m_writemark ].reset( new ScSingleRefData( rTr ) );
 
-    nElementCurrent++;
     ppP_Nlf.m_writemark++;
 
-    return static_cast<const TokenId>(nElementCurrent);
+    return static_cast<const TokenId>(maElements.size());
 }
 
 TokenId TokenPool::StoreMatrix()
 {
-    if (!CheckElementOrGrow())
-        return static_cast<const TokenId>(nElementCurrent+1);
+    if (!CanAddElement())
+        return static_cast<const TokenId>(maElements.size() + 1);
 
     if( nP_MatrixCurrent >= nP_Matrix )
         if (!GrowMatrix())
-            return static_cast<const TokenId>(nElementCurrent+1);
+            return static_cast<const TokenId>(maElements.size() + 1);
 
-    pElement[ nElementCurrent ] = nP_MatrixCurrent;
-    pType[ nElementCurrent ] = T_Matrix;
+    maElements.emplace_back(nP_MatrixCurrent, T_Matrix, 0);
 
     ScMatrix* pM = new ScMatrix( 0, 0 );
     pM->IncRef( );
     ppP_Matrix[ nP_MatrixCurrent ] = pM;
 
-    nElementCurrent++;
     nP_MatrixCurrent++;
 
-    return static_cast<const TokenId>(nElementCurrent);
+    return static_cast<const TokenId>(maElements.size());
 }
 
 TokenId TokenPool::StoreName( sal_uInt16 nIndex, sal_Int16 nSheet )
 {
-    if (!CheckElementOrGrow())
-        return static_cast<const TokenId>(nElementCurrent+1);
+    if (!CanAddElement())
+        return static_cast<const TokenId>(maElements.size() + 1);
 
-    pElement[nElementCurrent] = static_cast<sal_uInt16>(maRangeNames.size());
-    pType[nElementCurrent] = T_RN;
+    maElements.emplace_back(static_cast<sal_uInt16>(maRangeNames.size()), 
T_RN, 0);
 
     maRangeNames.emplace_back();
     RangeName& r = maRangeNames.back();
     r.mnIndex = nIndex;
     r.mnSheet = nSheet;
 
-    ++nElementCurrent;
-
-    return static_cast<const TokenId>(nElementCurrent);
+    return static_cast<const TokenId>(maElements.size());
 }
 
 TokenId TokenPool::StoreExtName( sal_uInt16 nFileId, const OUString& rName )
 {
-    if (!CheckElementOrGrow())
-        return static_cast<const TokenId>(nElementCurrent+1);
+    if (!CanAddElement())
+        return static_cast<const TokenId>(maElements.size() + 1);
 
-    pElement[nElementCurrent] = static_cast<sal_uInt16>(maExtNames.size());
-    pType[nElementCurrent] = T_ExtName;
+    maElements.emplace_back(static_cast<sal_uInt16>(maExtNames.size()), 
T_ExtName, 0);
 
     maExtNames.emplace_back();
     ExtName& r = maExtNames.back();
     r.mnFileId = nFileId;
     r.maName = rName;
 
-    ++nElementCurrent;
-
-    return static_cast<const TokenId>(nElementCurrent);
+    return static_cast<const TokenId>(maElements.size());
 }
 
 TokenId TokenPool::StoreExtRef( sal_uInt16 nFileId, const OUString& rTabName, 
const ScSingleRefData& rRef )
 {
-    if (!CheckElementOrGrow())
-        return static_cast<const TokenId>(nElementCurrent+1);
+    if (!CanAddElement())
+        return static_cast<const TokenId>(maElements.size() + 1);
 
-    pElement[nElementCurrent] = static_cast<sal_uInt16>(maExtCellRefs.size());
-    pType[nElementCurrent] = T_ExtRefC;
+    maElements.emplace_back(static_cast<sal_uInt16>(maExtCellRefs.size()), 
T_ExtRefC, 0);
 
     maExtCellRefs.emplace_back();
     ExtCellRef& r = maExtCellRefs.back();
@@ -648,18 +527,15 @@ TokenId TokenPool::StoreExtRef( sal_uInt16 nFileId, const 
OUString& rTabName, co
     r.maTabName = rTabName;
     r.maRef = rRef;
 
-    ++nElementCurrent;
-
-    return static_cast<const TokenId>(nElementCurrent);
+    return static_cast<const TokenId>(maElements.size());
 }
 
 TokenId TokenPool::StoreExtRef( sal_uInt16 nFileId, const OUString& rTabName, 
const ScComplexRefData& rRef )
 {
-    if (!CheckElementOrGrow())
-        return static_cast<const TokenId>(nElementCurrent+1);
+    if (!CanAddElement())
+        return static_cast<const TokenId>(maElements.size() + 1);
 
-    pElement[nElementCurrent] = static_cast<sal_uInt16>(maExtAreaRefs.size());
-    pType[nElementCurrent] = T_ExtRefA;
+    maElements.emplace_back(static_cast<sal_uInt16>(maExtAreaRefs.size()), 
T_ExtRefA, 0);
 
     maExtAreaRefs.emplace_back();
     ExtAreaRef& r = maExtAreaRefs.back();
@@ -667,16 +543,15 @@ TokenId TokenPool::StoreExtRef( sal_uInt16 nFileId, const 
OUString& rTabName, co
     r.maTabName = rTabName;
     r.maRef = rRef;
 
-    ++nElementCurrent;
-
-    return static_cast<const TokenId>(nElementCurrent);
+    return static_cast<const TokenId>(maElements.size());
 }
 
 void TokenPool::Reset()
 {
-    nP_IdCurrent = nP_IdLast = nElementCurrent
+    nP_IdCurrent = nP_IdLast
         = ppP_Str.m_writemark = pP_Dbl.m_writemark = pP_Err.m_writemark
         = ppP_RefTr.m_writemark = ppP_Ext.m_writemark = ppP_Nlf.m_writemark = 
nP_MatrixCurrent = 0;
+    maElements.clear();
     maRangeNames.clear();
     maExtNames.clear();
     maExtCellRefs.clear();
@@ -687,14 +562,13 @@ void TokenPool::Reset()
 bool TokenPool::IsSingleOp( const TokenId& rId, const DefTokenId eId ) const
 {
     sal_uInt16 nId = static_cast<sal_uInt16>(rId);
-    if( nId && nId <= nElementCurrent )
+    if (nId && nId <= maElements.size())
     {// existent?
         nId--;
-        if( T_Id == pType[ nId ] )
+        if (const auto [nPid, eType, nSize] = maElements[nId]; T_Id == eType)
         {// Token-Sequence?
-            if( pSize[ nId ] == 1 )
+            if (nSize == 1)
             {// EXACTLY 1 Token
-                sal_uInt16 nPid = pElement[ nId ];
                 if (nPid < nP_Id)
                 {
                     sal_uInt16 nSecId = pP_Id[ nPid ];
@@ -714,12 +588,11 @@ const OUString* TokenPool::GetExternal( const TokenId& 
rId ) const
 {
     const OUString*   p = nullptr;
     sal_uInt16 n = static_cast<sal_uInt16>(rId);
-    if( n && n <= nElementCurrent )
+    if (n && n <= maElements.size())
     {
         n--;
-        if( pType[ n ] == T_Ext )
+        if ([[maybe_unused]] const auto [nExt, eType, _] = maElements[n]; 
eType == T_Ext)
         {
-            sal_uInt16 nExt = pElement[ n ];
             if ( nExt < ppP_Ext.m_writemark && ppP_Ext[ nExt ] )
                 p = &ppP_Ext[ nExt ]->aText;
         }
diff --git a/sc/source/filter/inc/tokstack.hxx 
b/sc/source/filter/inc/tokstack.hxx
index 66c1918eabc1..2e84fc068c31 100644
--- a/sc/source/filter/inc/tokstack.hxx
+++ b/sc/source/filter/inc/tokstack.hxx
@@ -24,6 +24,7 @@
 #include <sal/log.hxx>
 
 #include <memory>
+#include <tuple>
 #include <utility>
 #include <vector>
 
@@ -199,11 +200,8 @@ private:
         };
         ::std::vector<ExtAreaRef>   maExtAreaRefs;
 
-        std::unique_ptr<sal_uInt16[]>   pElement;   // Array with Indices for 
elements
-        std::unique_ptr<E_TYPE[]>       pType;      // ...with Type-Info
-        std::unique_ptr<sal_uInt16[]>   pSize;      // ...with size
-        sal_uInt16                      nElement;
-        sal_uInt16                      nElementCurrent;
+        // Array with Indices for elements, with Type-Info and with size
+        std::vector<std::tuple<sal_uInt16, E_TYPE, sal_uInt16>> maElements;
 
         static const sal_uInt16         nScTokenOff;// Offset for SC-Token
 #ifdef DBG_UTIL
@@ -212,7 +210,6 @@ private:
 
         bool                        GrowTripel( sal_uInt16 nByMin );
         bool                        GrowId();
-        bool                        GrowElement();
         bool                        GrowMatrix();
                                     /** @return false means nElementCurrent 
range
                                         below nScTokenOff would overflow or
@@ -220,7 +217,7 @@ private:
                                         new ID available other than
                                         nElementCurrent+1.
                                      */
-        bool                        CheckElementOrGrow();
+        bool                        CanAddElement();
         bool                        GetElement( const sal_uInt16 nId, 
ScTokenArray* pScToken );
         bool                        GetElementRek( const sal_uInt16 nId, 
ScTokenArray* pScToken );
         void                        ClearMatrix();

Reply via email to