Kaydet (Commit) 63d9e2dd authored tarafından Noel Grandin's avatar Noel Grandin

loplugin:useuniqueptr in ScCompressedArray

Change-Id: I6bc46fc209e648e1262ea62e5c13c70a6342ff0d
Reviewed-on: https://gerrit.libreoffice.org/58570
Tested-by: Jenkins
Reviewed-by: 's avatarNoel Grandin <noel.grandin@collabora.co.uk>
üst 46780892
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#define INCLUDED_SC_INC_COMPRESSEDARRAY_HXX #define INCLUDED_SC_INC_COMPRESSEDARRAY_HXX
#include <cstddef> #include <cstddef>
#include <memory>
#include "scdllapi.h" #include "scdllapi.h"
...@@ -112,7 +113,7 @@ public: ...@@ -112,7 +113,7 @@ public:
protected: protected:
size_t nCount; size_t nCount;
size_t nLimit; size_t nLimit;
DataEntry* pData; std::unique_ptr<DataEntry[]> pData;
A nMaxAccess; A nMaxAccess;
}; };
...@@ -122,9 +123,8 @@ void ScCompressedArray<A,D>::Reset( const D& rValue ) ...@@ -122,9 +123,8 @@ void ScCompressedArray<A,D>::Reset( const D& rValue )
// Create a temporary copy in case we got a reference passed that points to // Create a temporary copy in case we got a reference passed that points to
// a part of the array to be reallocated. // a part of the array to be reallocated.
D aTmpVal( rValue); D aTmpVal( rValue);
delete[] pData;
nCount = nLimit = 1; nCount = nLimit = 1;
pData = new DataEntry[1]; pData.reset(new DataEntry[1]);
pData[0].aValue = aTmpVal; pData[0].aValue = aTmpVal;
pData[0].nEnd = nMaxAccess; pData[0].nEnd = nMaxAccess;
} }
......
...@@ -39,7 +39,6 @@ ScCompressedArray<A,D>::ScCompressedArray( A nMaxAccessP, const D& rValue ) ...@@ -39,7 +39,6 @@ ScCompressedArray<A,D>::ScCompressedArray( A nMaxAccessP, const D& rValue )
template< typename A, typename D > template< typename A, typename D >
ScCompressedArray<A,D>::~ScCompressedArray() ScCompressedArray<A,D>::~ScCompressedArray()
{ {
delete[] pData;
} }
template< typename A, typename D > template< typename A, typename D >
...@@ -48,10 +47,9 @@ void ScCompressedArray<A,D>::Resize( size_t nNewLimit) ...@@ -48,10 +47,9 @@ void ScCompressedArray<A,D>::Resize( size_t nNewLimit)
if ((nCount <= nNewLimit && nNewLimit < nLimit) || nLimit < nNewLimit) if ((nCount <= nNewLimit && nNewLimit < nLimit) || nLimit < nNewLimit)
{ {
nLimit = nNewLimit; nLimit = nNewLimit;
DataEntry* pNewData = new DataEntry[nLimit]; std::unique_ptr<DataEntry[]> pNewData(new DataEntry[nLimit]);
memcpy( pNewData, pData, nCount*sizeof(DataEntry)); memcpy( pNewData.get(), pData.get(), nCount*sizeof(DataEntry));
delete[] pData; pData = std::move(pNewData);
pData = pNewData;
} }
} }
...@@ -104,10 +102,9 @@ void ScCompressedArray<A,D>::SetValue( A nStart, A nEnd, const D& rValue ) ...@@ -104,10 +102,9 @@ void ScCompressedArray<A,D>::SetValue( A nStart, A nEnd, const D& rValue )
nLimit += nScCompressedArrayDelta; nLimit += nScCompressedArrayDelta;
if (nLimit < nNeeded) if (nLimit < nNeeded)
nLimit = nNeeded; nLimit = nNeeded;
DataEntry* pNewData = new DataEntry[nLimit]; std::unique_ptr<DataEntry[]> pNewData(new DataEntry[nLimit]);
memcpy( pNewData, pData, nCount*sizeof(DataEntry)); memcpy( pNewData.get(), pData.get(), nCount*sizeof(DataEntry));
delete[] pData; pData = std::move(pNewData);
pData = pNewData;
} }
size_t ni; // number of leading entries size_t ni; // number of leading entries
...@@ -180,7 +177,7 @@ void ScCompressedArray<A,D>::SetValue( A nStart, A nEnd, const D& rValue ) ...@@ -180,7 +177,7 @@ void ScCompressedArray<A,D>::SetValue( A nStart, A nEnd, const D& rValue )
} }
if (ni < nj) if (ni < nj)
{ // remove entries { // remove entries
memmove( pData + ni, pData + nj, memmove( pData.get() + ni, pData.get() + nj,
(nCount - nj) * sizeof(DataEntry)); (nCount - nj) * sizeof(DataEntry));
nCount -= nj - ni; nCount -= nj - ni;
} }
...@@ -191,11 +188,11 @@ void ScCompressedArray<A,D>::SetValue( A nStart, A nEnd, const D& rValue ) ...@@ -191,11 +188,11 @@ void ScCompressedArray<A,D>::SetValue( A nStart, A nEnd, const D& rValue )
if (nInsert <= nCount) if (nInsert <= nCount)
{ {
if (!bSplit) if (!bSplit)
memmove( pData + nInsert + 1, pData + nInsert, memmove( pData.get() + nInsert + 1, pData.get() + nInsert,
(nCount - nInsert) * sizeof(DataEntry)); (nCount - nInsert) * sizeof(DataEntry));
else else
{ {
memmove( pData + nInsert + 2, pData + nInsert, memmove( pData.get() + nInsert + 2, pData.get() + nInsert,
(nCount - nInsert) * sizeof(DataEntry)); (nCount - nInsert) * sizeof(DataEntry));
pData[nInsert+1] = pData[nInsert-1]; pData[nInsert+1] = pData[nInsert-1];
nCount++; nCount++;
...@@ -290,7 +287,7 @@ void ScCompressedArray<A,D>::Remove( A nStart, size_t nAccessCount ) ...@@ -290,7 +287,7 @@ void ScCompressedArray<A,D>::Remove( A nStart, size_t nAccessCount )
} }
else else
nRemove = 1; nRemove = 1;
memmove( pData + nIndex, pData + nIndex + nRemove, (nCount - (nIndex + memmove( pData.get() + nIndex, pData.get() + nIndex + nRemove, (nCount - (nIndex +
nRemove)) * sizeof(DataEntry)); nRemove)) * sizeof(DataEntry));
nCount -= nRemove; nCount -= nRemove;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment