loplugin:useuniqueptr in CoinMPSolver

Change-Id: Ibe0dfdfabf6f56498564406441a9c505e93dd9a6
Reviewed-on: https://gerrit.libreoffice.org/60112
Tested-by: Jenkins
Reviewed-by: Noel Grandin <noel.grandin@collabora.co.uk>
This commit is contained in:
Noel Grandin
2018-09-06 11:25:47 +02:00
parent 7c7e2fe9ad
commit 2bfc7d4809
2 changed files with 29 additions and 30 deletions

View File

@@ -143,7 +143,7 @@ void SAL_CALL CoinMPSolver::solve()
// set objective function // set objective function
const std::vector<double>& rObjCoeff = aCellsHash[maObjective]; const std::vector<double>& rObjCoeff = aCellsHash[maObjective];
double* pObjectCoeffs = new double[nVariables]; std::unique_ptr<double[]> pObjectCoeffs(new double[nVariables]);
for (nVar=0; nVar<nVariables; nVar++) for (nVar=0; nVar<nVariables; nVar++)
pObjectCoeffs[nVar] = rObjCoeff[nVar+1]; pObjectCoeffs[nVar] = rObjCoeff[nVar+1];
double nObjectConst = rObjCoeff[0]; // constant term of objective double nObjectConst = rObjCoeff[0]; // constant term of objective
@@ -152,12 +152,12 @@ void SAL_CALL CoinMPSolver::solve()
size_t nRows = maConstraints.getLength(); size_t nRows = maConstraints.getLength();
size_t nCompSize = nVariables * nRows; size_t nCompSize = nVariables * nRows;
double* pCompMatrix = new double[nCompSize]; // first collect all coefficients, row-wise std::unique_ptr<double[]> pCompMatrix(new double[nCompSize]); // first collect all coefficients, row-wise
for (size_t i=0; i<nCompSize; i++) for (size_t i=0; i<nCompSize; i++)
pCompMatrix[i] = 0.0; pCompMatrix[i] = 0.0;
double* pRHS = new double[nRows]; std::unique_ptr<double[]> pRHS(new double[nRows]);
char* pRowType = new char[nRows]; std::unique_ptr<char[]> pRowType(new char[nRows]);
for (size_t i=0; i<nRows; i++) for (size_t i=0; i<nRows; i++)
{ {
pRHS[i] = 0.0; pRHS[i] = 0.0;
@@ -217,10 +217,10 @@ void SAL_CALL CoinMPSolver::solve()
// Find non-zero coefficients, column-wise // Find non-zero coefficients, column-wise
int* pMatrixBegin = new int[nVariables+1]; std::unique_ptr<int[]> pMatrixBegin(new int[nVariables+1]);
int* pMatrixCount = new int[nVariables]; std::unique_ptr<int[]> pMatrixCount(new int[nVariables]);
double* pMatrix = new double[nCompSize]; // not always completely used std::unique_ptr<double[]> pMatrix(new double[nCompSize]); // not always completely used
int* pMatrixIndex = new int[nCompSize]; std::unique_ptr<int[]> pMatrixIndex(new int[nCompSize]);
int nMatrixPos = 0; int nMatrixPos = 0;
for (nVar=0; nVar<nVariables; nVar++) for (nVar=0; nVar<nVariables; nVar++)
{ {
@@ -239,13 +239,12 @@ void SAL_CALL CoinMPSolver::solve()
pMatrixCount[nVar] = nMatrixPos - nBegin; pMatrixCount[nVar] = nMatrixPos - nBegin;
} }
pMatrixBegin[nVariables] = nMatrixPos; pMatrixBegin[nVariables] = nMatrixPos;
delete[] pCompMatrix; pCompMatrix.reset();
pCompMatrix = nullptr;
// apply settings to all variables // apply settings to all variables
double* pLowerBounds = new double[nVariables]; std::unique_ptr<double[]> pLowerBounds(new double[nVariables]);
double* pUpperBounds = new double[nVariables]; std::unique_ptr<double[]> pUpperBounds(new double[nVariables]);
for (nVar=0; nVar<nVariables; nVar++) for (nVar=0; nVar<nVariables; nVar++)
{ {
pLowerBounds[nVar] = mbNonNegative ? 0.0 : -DBL_MAX; pLowerBounds[nVar] = mbNonNegative ? 0.0 : -DBL_MAX;
@@ -254,7 +253,7 @@ void SAL_CALL CoinMPSolver::solve()
// bounds could possibly be further restricted from single-cell constraints // bounds could possibly be further restricted from single-cell constraints
} }
char* pColType = new char[nVariables]; std::unique_ptr<char[]> pColType(new char[nVariables]);
for (nVar=0; nVar<nVariables; nVar++) for (nVar=0; nVar<nVariables; nVar++)
pColType[nVar] = mbInteger ? 'I' : 'C'; pColType[nVar] = mbInteger ? 'I' : 'C';
@@ -287,25 +286,25 @@ void SAL_CALL CoinMPSolver::solve()
HPROB hProb = CoinCreateProblem(""); HPROB hProb = CoinCreateProblem("");
int nResult = CoinLoadProblem( hProb, nVariables, nRows, nMatrixPos, 0, int nResult = CoinLoadProblem( hProb, nVariables, nRows, nMatrixPos, 0,
nObjectSense, nObjectConst, pObjectCoeffs, nObjectSense, nObjectConst, pObjectCoeffs.get(),
pLowerBounds, pUpperBounds, pRowType, pRHS, nullptr, pLowerBounds.get(), pUpperBounds.get(), pRowType.get(), pRHS.get(), nullptr,
pMatrixBegin, pMatrixCount, pMatrixIndex, pMatrix, pMatrixBegin.get(), pMatrixCount.get(), pMatrixIndex.get(), pMatrix.get(),
nullptr, nullptr, nullptr ); nullptr, nullptr, nullptr );
if (nResult == SOLV_CALL_SUCCESS) if (nResult == SOLV_CALL_SUCCESS)
{ {
nResult = CoinLoadInteger( hProb, pColType ); nResult = CoinLoadInteger( hProb, pColType.get() );
} }
delete[] pColType; pColType.reset();
delete[] pMatrixIndex; pMatrixIndex.reset();
delete[] pMatrix; pMatrix.reset();
delete[] pMatrixCount; pMatrixCount.reset();
delete[] pMatrixBegin; pMatrixBegin.reset();
delete[] pUpperBounds; pUpperBounds.reset();
delete[] pLowerBounds; pLowerBounds.reset();
delete[] pRowType; pRowType.reset();
delete[] pRHS; pRHS.reset();
delete[] pObjectCoeffs; pObjectCoeffs.reset();
CoinSetRealOption( hProb, COIN_REAL_MAXSECONDS, mnTimeout ); CoinSetRealOption( hProb, COIN_REAL_MAXSECONDS, mnTimeout );
CoinSetRealOption( hProb, COIN_REAL_MIPMAXSEC, mnTimeout ); CoinSetRealOption( hProb, COIN_REAL_MIPMAXSEC, mnTimeout );

View File

@@ -186,12 +186,12 @@ void SAL_CALL LpsolveSolver::solve()
// set objective function // set objective function
const std::vector<double>& rObjCoeff = aCellsHash[maObjective]; const std::vector<double>& rObjCoeff = aCellsHash[maObjective];
REAL* pObjVal = new REAL[nVariables+1]; std::unique_ptr<REAL[]> pObjVal(new REAL[nVariables+1]);
pObjVal[0] = 0.0; // ignored pObjVal[0] = 0.0; // ignored
for (nVar=0; nVar<nVariables; nVar++) for (nVar=0; nVar<nVariables; nVar++)
pObjVal[nVar+1] = rObjCoeff[nVar+1]; pObjVal[nVar+1] = rObjCoeff[nVar+1];
set_obj_fn( lp, pObjVal ); set_obj_fn( lp, pObjVal.get() );
delete[] pObjVal; pObjVal.reset();
set_rh( lp, 0, rObjCoeff[0] ); // constant term of objective set_rh( lp, 0, rObjCoeff[0] ); // constant term of objective
// add rows // add rows