Use std::swap instead of homegrown alternatives

This commit is contained in:
Ray Speth 2012-02-21 16:05:53 +00:00
parent 35429de71c
commit 28270820a5
9 changed files with 59 additions and 179 deletions

View file

@ -19,10 +19,6 @@ namespace VCSnonideal
*/
//@{
#ifndef SWAP
# define SWAP(x1, x2, temp) ((temp) = (x1), (x1) = (x2), (x2) = (temp))
#endif
#ifndef SQUARE
# define SQUARE(x) ((x) * (x))
#endif

View file

@ -43,15 +43,6 @@ static void print_stringTrunc(const char* str, int space, int alignment);
*/
static size_t amax(double* x, size_t j, size_t n);
//! Switch the position in the vector
/*!
* @param orderVector Vector to be manipulated
* @param jr first position
* @param kspec second species
*/
static void switch_pos(std::vector<size_t> &orderVector, size_t jr, size_t kspec);
//! Invert an nxn matrix and solve m rhs's
/*!
*
@ -361,7 +352,7 @@ size_t Cantera::BasisOptimize(int* usedZeroedSpecies, bool doFormRxn,
writelogf("(%9.2g) as component %3d\n", molNum[jj], jr);
}
#endif
switch_pos(orderVectorSpecies, jr, k);
std::swap(orderVectorSpecies[jr], orderVectorSpecies[k]);
}
/* - entry point from up above */
L_END_LOOP:
@ -558,14 +549,6 @@ static size_t amax(double* x, size_t j, size_t n)
return largest;
}
static void switch_pos(std::vector<size_t>& orderVector, size_t jr, size_t kspec)
{
size_t kcurr = orderVector[jr];
orderVector[jr] = orderVector[kspec];
orderVector[kspec] = kcurr;
}
/*
* vcs_mlequ:
*
@ -897,7 +880,7 @@ size_t Cantera::ElemRearrange(size_t nComponents, const vector_fp& elementAbunda
writelogf(" as element %3d\n", jr);
}
#endif
switch_pos(orderVectorElements, jr, k);
std::swap(orderVectorElements[jr], orderVectorElements[k]);
}
/*

View file

@ -205,7 +205,7 @@ int VCS_SOLVE::vcs_elem_rearrange(double* const aw, double* const sa,
}
#endif
vcs_switch_elem_pos(jr, k);
vcsUtil_dsw(aw, jr, k);
std::swap(aw[jr], aw[k]);
}
/*
@ -232,7 +232,6 @@ void VCS_SOLVE::vcs_switch_elem_pos(size_t ipos, size_t jpos)
return;
}
size_t j;
double dtmp;
vcs_VolPhase* volPhase;
#ifdef DEBUG_MODE
if (ipos > (m_numElemConstraints - 1) ||
@ -258,15 +257,15 @@ void VCS_SOLVE::vcs_switch_elem_pos(size_t ipos, size_t jpos)
}
}
}
vcsUtil_dsw(VCS_DATA_PTR(m_elemAbundancesGoal), ipos, jpos);
vcsUtil_dsw(VCS_DATA_PTR(m_elemAbundances), ipos, jpos);
vcsUtil_ssw(VCS_DATA_PTR(m_elementMapIndex), ipos, jpos);
vcsUtil_isw(VCS_DATA_PTR(m_elType), ipos, jpos);
vcsUtil_isw(VCS_DATA_PTR(m_elementActive), ipos, jpos);
std::swap(m_elemAbundancesGoal[ipos], m_elemAbundancesGoal[jpos]);
std::swap(m_elemAbundances[ipos], m_elemAbundancesGoal[jpos]);
std::swap(m_elementMapIndex[ipos], m_elementMapIndex[jpos]);
std::swap(m_elType[ipos], m_elType[jpos]);
std::swap(m_elementActive[ipos], m_elementActive[jpos]);
for (j = 0; j < m_numSpeciesTot; ++j) {
SWAP(m_formulaMatrix[ipos][j], m_formulaMatrix[jpos][j], dtmp);
std::swap(m_formulaMatrix[ipos][j], m_formulaMatrix[jpos][j]);
}
vcsUtil_stsw(m_elementName, ipos, jpos);
std::swap(m_elementName[ipos], m_elementName[jpos]);
}
}

View file

@ -193,37 +193,6 @@ int vcsUtil_mlequ(double* c, size_t idem, size_t n, double* b, size_t m);
*/
int vcsUtil_gaussj(double* c, size_t idem, size_t n, double* b, size_t m);
//! Swap values in vector of doubles
/*!
* Switches the value of x[i1] with x[i2]
*
* @param x Vector of doubles
* @param i1 first index
* @param i2 second index
*/
void vcsUtil_dsw(double x[], size_t i1, size_t i2);
//! Swap values in an integer array
/*!
* Switches the value of x[i1] with x[i2]
*
* @param x Vector of integers
* @param i1 first index
* @param i2 second index
*/
void vcsUtil_isw(int x[], size_t i1, size_t i2);
void vcsUtil_ssw(size_t x[], size_t i1, size_t i2);
//! Swap values in a std vector string
/*!
* Switches the value of vecStrings[i1] with vecStrings[i2]
*
* @param vecStrings Vector of integers
* @param i1 first index
* @param i2 second index
*/
void vcsUtil_stsw(std::vector<std::string> & vecStrings,
size_t i1, size_t i2);
//! Definition of the function pointer for the root finder
/*!

View file

@ -73,8 +73,8 @@ int VCS_SOLVE::vcs_report(int iconv)
for (l = m_numComponents; l < m_numSpeciesRdc; ++l) {
k = vcs_optMax(VCS_DATA_PTR(xy), 0, l, m_numSpeciesRdc);
if (k != l) {
vcsUtil_dsw(VCS_DATA_PTR(xy), k, l);
vcsUtil_ssw(VCS_DATA_PTR(sortindex), k, l);
std::swap(xy[k], xy[l]);
std::swap(sortindex[k], sortindex[l]);
}
}

View file

@ -1857,8 +1857,8 @@ public:
std::vector<size_t> m_phaseID;
//! Boolean indicating whether a species belongs to a single-species phase
std::vector<bool> m_SSPhase;
// vector<bool> can't be used here because it doesn't work with std::swap
std::vector<char> m_SSPhase;
//! Species string name for the kth species
/*!

View file

@ -3480,7 +3480,7 @@ int VCS_SOLVE::vcs_basopt(const bool doJustComponents, double aw[], double sa[],
}
#endif
vcs_switch_pos(false, jr, k);
vcsUtil_dsw(aw, jr, k);
std::swap(aw[jr], aw[k]);
}
#ifdef DEBUG_MODE
else {
@ -4986,15 +4986,14 @@ void VCS_SOLVE::vcs_switch2D(double* const* const Jac,
const size_t k1, const size_t k2) const
{
size_t i;
register double dtmp;
if (k1 == k2) {
return;
}
for (i = 0; i < m_numSpeciesTot; i++) {
SWAP(Jac[k1][i], Jac[k2][i], dtmp);
std::swap(Jac[k1][i], Jac[k2][i]);
}
for (i = 0; i < m_numSpeciesTot; i++) {
SWAP(Jac[i][k1], Jac[i][k2], dtmp);
std::swap(Jac[i][k1], Jac[i][k2]);
}
}
/*****************************************************************************/
@ -5558,11 +5557,8 @@ void VCS_SOLVE::vcs_deltag_Phase(const size_t iphase, const bool doDeleted,
*/
void VCS_SOLVE::vcs_switch_pos(const bool ifunc, const size_t k1, const size_t k2)
{
register size_t j;
register double t1 = 0.0;
size_t i1, i2, iph, kp1, kp2;
vcs_VolPhase* pv1, *pv2;
VCS_SPECIES_THERMO* st_tmp;
if (k1 == k2) {
return;
}
@ -5595,37 +5591,35 @@ void VCS_SOLVE::vcs_switch_pos(const bool ifunc, const size_t k1, const size_t k
pv2->setSpGlobalIndexVCS(kp2, k1);
//pv1->IndSpecies[kp1] = k2;
//pv2->IndSpecies[kp2] = k1;
int itmp;
bool btmp;
vcsUtil_stsw(m_speciesName, k1, k2);
SWAP(m_molNumSpecies_old[k1], m_molNumSpecies_old[k2], t1);
SWAP(m_speciesUnknownType[k1], m_speciesUnknownType[k2], itmp);
SWAP(m_molNumSpecies_new[k1], m_molNumSpecies_new[k2], t1);
SWAP(m_SSfeSpecies[k1], m_SSfeSpecies[k2], t1);
SWAP(m_spSize[k1], m_spSize[k2], t1);
SWAP(m_deltaMolNumSpecies[k1], m_deltaMolNumSpecies[k2], t1);
SWAP(m_feSpecies_old[k1], m_feSpecies_old[k2], t1);
SWAP(m_feSpecies_new[k1], m_feSpecies_new[k2], t1);
SWAP(m_SSPhase[k1], m_SSPhase[k2], btmp);
SWAP(m_phaseID[k1], m_phaseID[k2], j);
SWAP(m_speciesMapIndex[k1], m_speciesMapIndex[k2], j);
SWAP(m_speciesLocalPhaseIndex[k1], m_speciesLocalPhaseIndex[k2], j);
SWAP(m_actConventionSpecies[k1], m_actConventionSpecies[k2], itmp);
SWAP(m_lnMnaughtSpecies[k1], m_lnMnaughtSpecies[k2], t1);
SWAP(m_actCoeffSpecies_new[k1], m_actCoeffSpecies_new[k2], t1);
SWAP(m_actCoeffSpecies_old[k1], m_actCoeffSpecies_old[k2], t1);
SWAP(m_wtSpecies[k1], m_wtSpecies[k2], t1);
SWAP(m_chargeSpecies[k1], m_chargeSpecies[k2], t1);
SWAP(m_speciesThermoList[k1], m_speciesThermoList[k2], st_tmp);
SWAP(m_PMVolumeSpecies[k1], m_PMVolumeSpecies[k2], t1);
std::swap(m_speciesName[k1], m_speciesName[k2]);
std::swap(m_molNumSpecies_old[k1], m_molNumSpecies_old[k2]);
std::swap(m_speciesUnknownType[k1], m_speciesUnknownType[k2]);
std::swap(m_molNumSpecies_new[k1], m_molNumSpecies_new[k2]);
std::swap(m_SSfeSpecies[k1], m_SSfeSpecies[k2]);
std::swap(m_spSize[k1], m_spSize[k2]);
std::swap(m_deltaMolNumSpecies[k1], m_deltaMolNumSpecies[k2]);
std::swap(m_feSpecies_old[k1], m_feSpecies_old[k2]);
std::swap(m_feSpecies_new[k1], m_feSpecies_new[k2]);
std::swap(m_SSPhase[k1], m_SSPhase[k2]);
std::swap(m_phaseID[k1], m_phaseID[k2]);
std::swap(m_speciesMapIndex[k1], m_speciesMapIndex[k2]);
std::swap(m_speciesLocalPhaseIndex[k1], m_speciesLocalPhaseIndex[k2]);
std::swap(m_actConventionSpecies[k1], m_actConventionSpecies[k2]);
std::swap(m_lnMnaughtSpecies[k1], m_lnMnaughtSpecies[k2]);
std::swap(m_actCoeffSpecies_new[k1], m_actCoeffSpecies_new[k2]);
std::swap(m_actCoeffSpecies_old[k1], m_actCoeffSpecies_old[k2]);
std::swap(m_wtSpecies[k1], m_wtSpecies[k2]);
std::swap(m_chargeSpecies[k1], m_chargeSpecies[k2]);
std::swap(m_speciesThermoList[k1], m_speciesThermoList[k2]);
std::swap(m_PMVolumeSpecies[k1], m_PMVolumeSpecies[k2]);
for (j = 0; j < m_numElemConstraints; ++j) {
SWAP(m_formulaMatrix[j][k1], m_formulaMatrix[j][k2], t1);
for (size_t j = 0; j < m_numElemConstraints; ++j) {
std::swap(m_formulaMatrix[j][k1], m_formulaMatrix[j][k2]);
}
if (m_useActCoeffJac) {
vcs_switch2D(m_dLnActCoeffdMolNum.baseDataAddr(), k1, k2);
}
SWAP(m_speciesStatus[k1], m_speciesStatus[k2], itmp);
std::swap(m_speciesStatus[k1], m_speciesStatus[k2]);
/*
* Handle the index pointer in the phase structures
*/
@ -5644,18 +5638,18 @@ void VCS_SOLVE::vcs_switch_pos(const bool ifunc, const size_t k1, const size_t k
i1 , i2);
}
#endif
for (j = 0; j < m_numComponents; ++j) {
SWAP(m_stoichCoeffRxnMatrix[i1][j], m_stoichCoeffRxnMatrix[i2][j], t1);
for (size_t j = 0; j < m_numComponents; ++j) {
std::swap(m_stoichCoeffRxnMatrix[i1][j], m_stoichCoeffRxnMatrix[i2][j]);
}
SWAP(m_scSize[i1], m_scSize[i2], t1);
std::swap(m_scSize[i1], m_scSize[i2]);
for (iph = 0; iph < m_numPhases; iph++) {
SWAP(m_deltaMolNumPhase[i1][iph], m_deltaMolNumPhase[i2][iph], t1);
SWAP(m_phaseParticipation[i1][iph],
m_phaseParticipation[i2][iph], itmp);
std::swap(m_deltaMolNumPhase[i1][iph], m_deltaMolNumPhase[i2][iph]);
std::swap(m_phaseParticipation[i1][iph],
m_phaseParticipation[i2][iph]);
}
SWAP(m_deltaGRxn_new[i1], m_deltaGRxn_new[i2], t1);
SWAP(m_deltaGRxn_old[i1], m_deltaGRxn_old[i2], t1);
SWAP(m_deltaGRxn_tmp[i1], m_deltaGRxn_tmp[i2], t1);
std::swap(m_deltaGRxn_new[i1], m_deltaGRxn_new[i2]);
std::swap(m_deltaGRxn_old[i1], m_deltaGRxn_old[i2]);
std::swap(m_deltaGRxn_tmp[i1], m_deltaGRxn_tmp[i2]);
/*
* We don't want to swap ir[], because the values of ir should

View file

@ -248,58 +248,6 @@ int vcs_max_int(const int* vector, int length)
return retn;
}
//====================================================================================================================
// Swap values in a std vector string
/*
* Switches the value of vecStrings[i1] with vecStrings[i2]
*
* @param vecStrings Vector of integers
* @param i1 first index
* @param i2 second index
*/
void vcsUtil_stsw(std::vector<std::string> & vstr, size_t i1, size_t i2)
{
std::string tmp(vstr[i2]);
vstr[i2] = vstr[i1];
vstr[i1] = tmp;
}
//====================================================================================================================
// Swap values in vector of doubles
/*
* Switches the value of x[i1] with x[i2]
*
* @param x Vector of doubles
* @param i1 first index
* @param i2 second index
*/
void vcsUtil_dsw(double x[], size_t i1, size_t i2)
{
double t = x[i1];
x[i1] = x[i2];
x[i2] = t;
}
//====================================================================================================================
// Swap values in an integer array
/*
* Switches the value of x[i1] with x[i2]
*
* @param x Vector of integers
* @param i1 first index
* @param i2 second index
*/
void vcsUtil_isw(int x[], size_t i1, size_t i2)
{
int t = x[i1];
x[i1] = x[i2];
x[i2] = t;
}
void vcsUtil_ssw(size_t x[], size_t i1, size_t i2)
{
size_t t = x[i1];
x[i1] = x[i2];
x[i2] = t;
}
//====================================================================================================================
#ifdef DEBUG_HKM
@ -344,15 +292,14 @@ static void mlequ_matrixDump(double* c, int idem, int n)
*/
static void vcsUtil_swapRows(double* c, size_t idem, size_t n, double* b, size_t m, size_t irowa, size_t irowb)
{
double t1;
if (irowa == irowb) {
return;
}
for (size_t j = 0; j < n; j++) {
SWAP(c[irowa + j * idem], c[irowb + j * idem], t1);
std::swap(c[irowa + j * idem], c[irowb + j * idem]);
}
for (size_t j = 0; j < m; j++) {
SWAP(b[irowa + j * idem], b[irowb + j * idem], t1);
std::swap(b[irowa + j * idem], b[irowb + j * idem]);
}
}
//====================================================================================================================
@ -624,7 +571,7 @@ int vcsUtil_gaussj(double* c, size_t idem, size_t n, double* b, size_t m)
size_t irow = npos;
size_t icol = npos;
bool needInverse = false;
double pivinv, dum;
double pivinv;
#ifdef DEBUG_HKM
static int s_numCalls = 0;
s_numCalls++;
@ -683,7 +630,7 @@ int vcsUtil_gaussj(double* c, size_t idem, size_t n, double* b, size_t m)
}
for (ll = 0; ll < n; ll++) {
if (ll != icol) {
dum = c[ll + idem * icol];
double dum = c[ll + idem * icol];
c[ll + idem * icol] = 0;
for (l = 0; l < n; l++) {
c[ll + idem * l] -= c[icol + idem * l] * dum;
@ -698,7 +645,7 @@ int vcsUtil_gaussj(double* c, size_t idem, size_t n, double* b, size_t m)
for (l = n-1; l >= 0; l--) {
if (indxr[l] != indxc[l]) {
for (k = 0; k < n; k++) {
SWAP(c[k + idem * indxr[l]], c[k + idem * indxr[l]], dum);
std::swap(c[k + idem * indxr[l]], c[k + idem * indxr[l]]);
}
}
}

View file

@ -44,13 +44,6 @@ namespace Cantera
#define DSIGN(x) (( (x) == (0.0) ) ? (0.0) : ( ((x) > 0.0) ? 1.0 : -1.0 ))
#endif
#ifdef SWAP
#undef SWAP
#endif
#ifndef SWAP
#define SWAP(x1, x2, tmp) ((tmp) = (x2), (x2) = (x1), (x1) = (tmp))
#endif
/*****************************************************************************/
/*****************************************************************************/
/*****************************************************************************/
@ -360,7 +353,6 @@ int RootFind::solve(doublereal xmin, doublereal xmax, int itmax, doublereal& fun
doublereal fnorm; /* A valid norm for the making the function value dimensionless */
doublereal xDelMin;
doublereal sgn;
doublereal dtmp;
doublereal fnoise = 0.0;
rfHistory_.clear();
rfTable rfT;
@ -1162,13 +1154,13 @@ done:
rfHistory_.push_back(rfT);
rfT.clear();
rfT.its = its;
SWAP(f1, f2, dtmp);
SWAP(x1, x2, dtmp);
std::swap(f1, f2);
std::swap(x1, x2);
*xbest = x2;
if (fabs(fnew) < fabs(f1)) {
if (f1 * fnew > 0.0) {
SWAP(f1, fnew, dtmp);
SWAP(x1, xnew, dtmp);
std::swap(f1, fnew);
std::swap(x1, xnew);
}
}