Commit 153a1888 authored by snuverink_j's avatar snuverink_j
Browse files

avoid postfix increment operator for non-primitive types, use range loop where...

avoid postfix increment operator for non-primitive types, use range loop where possible, for issue #62
parent 020c1302
......@@ -3,8 +3,6 @@
#include "boost/algorithm/string.hpp"
#include "boost/variant.hpp"
#include "boost/smart_ptr.hpp"
#include "boost/foreach.hpp"
#define foreach BOOST_FOREACH
FunctionEvaluator::FunctionEvaluator(Expressions::Named_t objectives,
Expressions::Named_t constraints, Param_t params,
......@@ -25,12 +23,11 @@ FunctionEvaluator::~FunctionEvaluator() {
void FunctionEvaluator::collectResults() {
requestedVars_.clear();
Expressions::Named_t::iterator it;
// evaluate all objectives
for(it = objectives_.begin(); it != objectives_.end(); it++) {
for(auto namedObjective : objectives_) {
Expressions::Expr_t *objective = it->second;
Expressions::Expr_t *objective = namedObjective.second;
Expressions::Result_t result = objective->evaluate(params_);
std::vector<double> values;
......@@ -39,14 +36,14 @@ void FunctionEvaluator::collectResults() {
reqVarInfo_t tmps = {EVALUATE, values, is_valid};
requestedVars_.insert(
std::pair<std::string, reqVarInfo_t>(it->first, tmps));
std::pair<std::string, reqVarInfo_t>(namedObjective.first, tmps));
}
// .. and constraints
for(it = constraints_.begin(); it != constraints_.end(); it++) {
for(auto namedConstraint : constraints_) {
Expressions::Expr_t *constraint = it->second;
Expressions::Expr_t *constraint = namedConstraint.second;
Expressions::Result_t result = constraint->evaluate(params_);
std::vector<double> values;
......@@ -76,8 +73,7 @@ void FunctionEvaluator::collectResults() {
reqVarInfo_t tmps = {EVALUATE, values, is_valid};
requestedVars_.insert(
std::pair<std::string, reqVarInfo_t>(it->first, tmps));
std::pair<std::string, reqVarInfo_t>(namedConstraint.first, tmps));
}
}
......@@ -32,7 +32,6 @@
// access to OPAL lib
//#include "/gpfs/home/adelmann/opal/master-src/src/opal.h"
#include "opal.h"
#include "Utilities/OpalException.h"
#include "Utilities/Options.h"
......@@ -58,18 +57,17 @@ OpalSimulation::OpalSimulation(Expressions::Named_t objectives,
// prepare design variables given by the optimizer for generating the
// input file
std::vector<std::string> dict;
Param_t::iterator it;
for(it = params.begin(); it != params.end(); it++) {
for(auto parameter : params) {
std::ostringstream tmp;
tmp.precision(15);
tmp << it->first << "=" << it->second;
tmp << parameter.first << "=" << parameter.second;
dict.push_back(tmp.str());
std::ostringstream value;
value.precision(15);
value << it->second;
value << parameter.second;
userVariables_.insert(
std::pair<std::string, std::string>(it->first, value.str()));
std::pair<std::string, std::string>(parameter.first, value.str()));
}
......@@ -306,10 +304,9 @@ void OpalSimulation::collectResults() {
invalidBunch();
} else {
Expressions::Named_t::iterator it;
for(it = objectives_.begin(); it != objectives_.end(); it++) {
for(auto namedObjective : objectives_) {
Expressions::Expr_t *objective = it->second;
Expressions::Expr_t *objective = namedObjective.second;
// find out which variables we need in order to evaluate the
// objective
......@@ -362,14 +359,14 @@ void OpalSimulation::collectResults() {
reqVarInfo_t tmps = {EVALUATE, values, is_valid};
requestedVars_.insert(
std::pair<std::string, reqVarInfo_t>(it->first, tmps));
std::pair<std::string, reqVarInfo_t>(namedObjective.first, tmps));
}
// .. and constraints
for(it = constraints_.begin(); it != constraints_.end(); it++) {
for(auto namedConstraint : constraints_) {
Expressions::Expr_t *constraint = it->second;
Expressions::Expr_t *constraint = namedConstraint.second;
// find out which variables we need in order to evaluate the
// objective
......@@ -441,7 +438,7 @@ void OpalSimulation::collectResults() {
reqVarInfo_t tmps = {EVALUATE, values, is_valid};
requestedVars_.insert(
std::pair<std::string, reqVarInfo_t>(it->first, tmps));
std::pair<std::string, reqVarInfo_t>(namedConstraint.first, tmps));
}
......@@ -459,13 +456,12 @@ void OpalSimulation::collectResults() {
void OpalSimulation::invalidBunch() {
Expressions::Named_t::iterator it;
for(it = objectives_.begin(); it != objectives_.end(); it++) {
for(auto namedObjective : objectives_) {
std::vector<double> tmp_values;
tmp_values.push_back(0.0);
reqVarInfo_t tmps = {EVALUATE, tmp_values, false};
requestedVars_.insert(
std::pair<std::string, reqVarInfo_t>(it->first, tmps));
std::pair<std::string, reqVarInfo_t>(namedObjective.first, tmps));
}
}
......
......@@ -88,9 +88,6 @@
class Beamline;
class PartData;
using Physics::m_p; // GeV
using Physics::PMASS;
using Physics::PCHARGE;
using Physics::pi;
using Physics::q_e;
......@@ -194,16 +191,14 @@ ParallelCyclotronTracker::ParallelCyclotronTracker(const Beamline &beamline,
*
*/
ParallelCyclotronTracker::~ParallelCyclotronTracker() {
for(std::list<Component *>::iterator compindex = myElements.begin(); compindex != myElements.end(); compindex++) {
delete(*compindex);
for(Component* component : myElements) {
delete(component);
}
for(beamline_list::iterator fdindex = FieldDimensions.begin(); fdindex != FieldDimensions.end(); fdindex++) {
delete(*fdindex);
for(auto fd : FieldDimensions) {
delete(fd);
}
delete itsBeamline;
if (opalRing_m != NULL) {
// delete opalRing_m;
}
// delete opalRing_m;
}
/**
......@@ -212,8 +207,8 @@ ParallelCyclotronTracker::~ParallelCyclotronTracker() {
* @param none
*/
void ParallelCyclotronTracker::initializeBoundaryGeometry() {
for(std::list<Component *>::iterator compindex = myElements.begin(); compindex != myElements.end(); compindex++) {
bgf_m = dynamic_cast<ElementBase *>(*compindex)->getBoundaryGeometry();
for(Component * component : myElements) {
bgf_m = dynamic_cast<ElementBase *>(component)->getBoundaryGeometry();
if(!bgf_m)
continue;
else
......@@ -1136,8 +1131,8 @@ void ParallelCyclotronTracker::execute() {
*gmsg << "* -------------------------------------" << endl;
*gmsg << "* The selected Beam line elements are :" << endl;
for(beamline_list::iterator sindex = FieldDimensions.begin(); sindex != FieldDimensions.end(); sindex++)
*gmsg << "* -> " << ElementBase::getTypeString((*sindex)->first) << endl;
for(auto fd : FieldDimensions)
*gmsg << "* -> " << ElementBase::getTypeString(fd->first) << endl;
*gmsg << "* -------------------------------------" << endl;
......@@ -1190,8 +1185,8 @@ void ParallelCyclotronTracker::execute() {
*gmsg << "* ----------------------------------------------- *" << endl;
*gmsg << "* Finalizing i.e. write data and close files :" << endl;
for(beamline_list::iterator sindex = FieldDimensions.begin(); sindex != FieldDimensions.end(); sindex++) {
(((*sindex)->second).second)->finalise();
for(auto fd : FieldDimensions) {
((fd->second).second)->finalise();
}
*gmsg << "* ----------------------------------------------- *" << endl;
}
......@@ -2161,7 +2156,7 @@ void ParallelCyclotronTracker::applyPluginElements(const double dt) {
itsBunch->R *= Vector_t(1000.0);
for(beamline_list::iterator sindex = ++(FieldDimensions.begin()); sindex != FieldDimensions.end(); sindex++) {
for(beamline_list::iterator sindex = ++(FieldDimensions.begin()); sindex != FieldDimensions.end(); ++sindex) {
if(((*sindex)->first) == ElementBase::SEPTUM) {
(static_cast<Septum *>(((*sindex)->second).second))->checkSeptum(*itsBunch);
}
......@@ -2401,7 +2396,7 @@ void ParallelCyclotronTracker::initDistInGlobalFrame() {
checkNumPart(std::string("* Before repartition: "));
repartition();
checkNumPart(std::string("* After repartition: "));
checkNumPart(std::string("* After repartition: "));
itsBunch->calcBeamParameters();
......@@ -2512,16 +2507,15 @@ void ParallelCyclotronTracker::singleParticleDump() {
}
dvector_t::iterator itParameter = tmpr.begin();
ivector_t::iterator itId = tmpi.begin();
for(itId = tmpi.begin(); itId != tmpi.end(); itId++) {
for(auto id : tmpi) {
outfTrackOrbit_m << "ID" << *itId;
outfTrackOrbit_m << "ID" << id;
for(int ii = 0; ii < 6; ii++) {
outfTrackOrbit_m << " " << *itParameter;
itParameter++;
++itParameter;
}
outfTrackOrbit_m << std::endl;
......@@ -3244,7 +3238,7 @@ void ParallelCyclotronTracker::gapCrossKick_m(size_t i, double t,
const Vector_t& Pold)
{
for (beamline_list::iterator sindex = ++(FieldDimensions.begin());
sindex != FieldDimensions.end(); sindex++)
sindex != FieldDimensions.end(); ++sindex)
{
bool tag_crossing = false;
double DistOld = 0.0; //mm
......
......@@ -761,7 +761,7 @@ vector<Aperture::coord> Aperture::getShape(vector<double> vec) {
}
} else if((r > -10.5) && (r < -9.5)) {
vector<double>::iterator iter = vec.begin();
iter++;
++iter;
while(iter != vec.end()) {
pt.x = *iter;
pt.y = *(iter + 1);
......
......@@ -193,7 +193,7 @@ unsigned int Statement::position() const {
std::ostringstream os;
bool white = false;
for(TokenList::const_iterator c = tokens.begin(); c != curr; c++) {
for(TokenList::const_iterator c = tokens.begin(); c != curr; ++c) {
if(white && !c->isDel()) os << ' ';
white = !c->isDel();
os << *c;
......@@ -206,7 +206,7 @@ unsigned int Statement::position() const {
void Statement::print(std::ostream &msg) const {
bool white = false;
for(TokenList::const_iterator c = tokens.begin(); c != tokens.end(); c++) {
for(TokenList::const_iterator c = tokens.begin(); c != tokens.end(); ++c) {
if(white && !c->isDel()) msg << ' ';
white = !c->isDel();
msg << *c;
......
......@@ -970,7 +970,7 @@ void CollimatorPhysics::deleteParticleFromLocalVector() {
// find start of particles to delete
std::vector<PART>::iterator inv = locParts_m.begin();
for (; inv != locParts_m.end(); inv++) {
for (; inv != locParts_m.end(); ++inv) {
if ((*inv).label == -1)
break;
}
......
......@@ -297,7 +297,7 @@ size_t Distribution::getNumOfLocalParticlesToCreate(size_t n) {
/**
* At the moment only write the header into the file dist.dat
* PartBunch will then append (very uggly)
* PartBunch will then append (very ugly)
* @param
* @param
* @param
......@@ -821,46 +821,33 @@ void Distribution::addDistributions() {
particlesPerDist_m[idx] = (*addedDistIt)->tOrZDist_m.size();
std::vector<double>::iterator distIt;
for (distIt = (*addedDistIt)->getXDist().begin();
distIt != (*addedDistIt)->getXDist().end();
distIt++) {
xDist_m.push_back(*distIt);
for (double dist : (*addedDistIt)->getXDist()) {
xDist_m.push_back(dist);
}
(*addedDistIt)->eraseXDist();
for (distIt = (*addedDistIt)->getBGxDist().begin();
distIt != (*addedDistIt)->getBGxDist().end();
distIt++) {
pxDist_m.push_back(*distIt);
for (double dist : (*addedDistIt)->getBGxDist()) {
pxDist_m.push_back(dist);
}
(*addedDistIt)->eraseBGxDist();
for (distIt = (*addedDistIt)->getYDist().begin();
distIt != (*addedDistIt)->getYDist().end();
distIt++) {
yDist_m.push_back(*distIt);
for (double dist : (*addedDistIt)->getYDist()) {
yDist_m.push_back(dist);
}
(*addedDistIt)->eraseYDist();
for (distIt = (*addedDistIt)->getBGyDist().begin();
distIt != (*addedDistIt)->getBGyDist().end();
distIt++) {
pyDist_m.push_back(*distIt);
for (double dist : (*addedDistIt)->getBGyDist()) {
pyDist_m.push_back(dist);
}
(*addedDistIt)->eraseBGyDist();
for (distIt = (*addedDistIt)->getTOrZDist().begin();
distIt != (*addedDistIt)->getTOrZDist().end();
distIt++) {
tOrZDist_m.push_back(*distIt);
for (double dist : (*addedDistIt)->getTOrZDist()) {
tOrZDist_m.push_back(dist);
}
(*addedDistIt)->eraseTOrZDist();
for (distIt = (*addedDistIt)->getBGzDist().begin();
distIt != (*addedDistIt)->getBGzDist().end();
distIt++) {
pzDist_m.push_back(*distIt);
for (double dist : (*addedDistIt)->getBGzDist()) {
pzDist_m.push_back(dist);
}
(*addedDistIt)->eraseBGzDist();
}
......@@ -957,8 +944,6 @@ void Distribution::applyEmissModelNonEquil(double lowEnergyLimit,
void Distribution::calcPartPerDist(size_t numberOfParticles) {
typedef std::vector<Distribution *>::iterator iterator;
if (numberOfDistributions_m == 1) {
particlesPerDist_m.push_back(numberOfParticles);
return;
......@@ -1728,10 +1713,8 @@ void Distribution::createOpalT(PartBunch &beam,
// Set distribution type(s).
setDistType();
std::vector<Distribution *>::iterator addedDistIt;
for (addedDistIt = addedDistributions_m.begin();
addedDistIt != addedDistributions_m.end(); addedDistIt++)
(*addedDistIt)->setDistType();
for (Distribution* addedDist : addedDistributions_m)
addedDist->setDistType();
/*
* Determine the number of particles for each distribution. Note
......@@ -1748,21 +1731,18 @@ void Distribution::createOpalT(PartBunch &beam,
* Force added distributions to the same emission condition as the main
* distribution.
*/
for (addedDistIt = addedDistributions_m.begin();
addedDistIt != addedDistributions_m.end(); addedDistIt++)
(*addedDistIt)->setDistToEmitted(emitting_m);
for (Distribution* addedDist : addedDistributions_m)
addedDist->setDistToEmitted(emitting_m);
if (emitting_m) {
if (emitting_m)
setupEmissionModel(beam);
}
// Create distributions.
create(particlesPerDist_m.at(0), beam.getM());
size_t distCount = 1;
for (addedDistIt = addedDistributions_m.begin();
addedDistIt != addedDistributions_m.end(); addedDistIt++) {
(*addedDistIt)->create(particlesPerDist_m.at(distCount), beam.getM());
for (Distribution* addedDist : addedDistributions_m) {
addedDist->create(particlesPerDist_m.at(distCount), beam.getM());
distCount++;
}
......@@ -1978,7 +1958,7 @@ size_t Distribution::emitParticles(PartBunch &beam, double eZ) {
std::vector<size_t>::reverse_iterator ptbErasedIt;
for (ptbErasedIt = particlesToBeErased.rbegin();
ptbErasedIt < particlesToBeErased.rend();
ptbErasedIt++) {
++ptbErasedIt) {
/*
* We don't use the vector class function erase because it
......@@ -3061,7 +3041,7 @@ double Distribution::getMaxTOrZ() {
std::vector<double>::iterator longIt = tOrZDist_m.begin();
double maxTOrZ = *longIt;
for (++longIt; longIt != tOrZDist_m.end(); longIt++) {
for (++longIt; longIt != tOrZDist_m.end(); ++longIt) {
if (maxTOrZ < *longIt)
maxTOrZ = *longIt;
}
......@@ -3075,7 +3055,7 @@ double Distribution::getMinTOrZ() {
std::vector<double>::iterator longIt = tOrZDist_m.begin();
double minTOrZ = *longIt;
for (++longIt; longIt != tOrZDist_m.end(); longIt++) {
for (++longIt; longIt != tOrZDist_m.end(); ++longIt) {
if (minTOrZ > *longIt)
minTOrZ = *longIt;
}
......@@ -3979,7 +3959,7 @@ void Distribution::setDistType() {
distrTypeT_m = DistrTypeT::MATCHEDGAUSS;
else {
throw OpalException("Distribution::setDistType()",
"The distribution \"" + distT_m + "\" isnt'know.\n" +
"The distribution \"" + distT_m + "\" isn't known.\n" +
"Known distributions are:\n"
"FROMFILE\n"
"GAUSS\n"
......@@ -4461,36 +4441,35 @@ void Distribution::setupParticleBins(double massIneV, PartBunch &beam) {
void Distribution::shiftBeam(double &maxTOrZ, double &minTOrZ) {
std::vector<double>::iterator tOrZIt;
if (emitting_m) {
if (addedDistributions_m.size() == 0) {
if (distrTypeT_m == DistrTypeT::ASTRAFLATTOPTH) {
for (tOrZIt = tOrZDist_m.begin(); tOrZIt != tOrZDist_m.end(); tOrZIt++)
*tOrZIt -= tEmission_m / 2.0;
for (double& tOrZ : tOrZDist_m)
tOrZ -= tEmission_m / 2.0;
minTOrZ -= tEmission_m / 2.0;
maxTOrZ -= tEmission_m / 2.0;
} else if (distrTypeT_m == DistrTypeT::GAUSS
|| distrTypeT_m == DistrTypeT::FLATTOP
|| distrTypeT_m == DistrTypeT::GUNGAUSSFLATTOPTH) {
for (tOrZIt = tOrZDist_m.begin(); tOrZIt != tOrZDist_m.end(); tOrZIt++)
*tOrZIt -= tEmission_m;
for (double& tOrZ : tOrZDist_m)
tOrZ -= tEmission_m;
minTOrZ -= tEmission_m;
maxTOrZ -= tEmission_m;
} else {
for (tOrZIt = tOrZDist_m.begin(); tOrZIt != tOrZDist_m.end(); tOrZIt++)
*tOrZIt -= maxTOrZ;
for (double& tOrZ : tOrZDist_m)
tOrZ -= maxTOrZ;
minTOrZ -= maxTOrZ;
maxTOrZ -= maxTOrZ;
}
} else {
for (tOrZIt = tOrZDist_m.begin(); tOrZIt != tOrZDist_m.end(); tOrZIt++)
*tOrZIt -= maxTOrZ;
for (double& tOrZ : tOrZDist_m)
tOrZ -= maxTOrZ;
minTOrZ -= maxTOrZ;
maxTOrZ -= maxTOrZ;
......@@ -4498,16 +4477,15 @@ void Distribution::shiftBeam(double &maxTOrZ, double &minTOrZ) {
} else {
double avgZ[] = {0.0, 1.0 * tOrZDist_m.size()};
for (tOrZIt = tOrZDist_m.begin(); tOrZIt != tOrZDist_m.end(); tOrZIt++)
avgZ[0] += *tOrZIt;
for (double tOrZ : tOrZDist_m)
avgZ[0] += tOrZ;
reduce(avgZ, avgZ + 2, avgZ, OpAddAssign());
avgZ[0] /= avgZ[1];
for (tOrZIt = tOrZDist_m.begin(); tOrZIt != tOrZDist_m.end(); tOrZIt++)
*tOrZIt -= avgZ[0];
for (double& tOrZ : tOrZDist_m)
tOrZ -= avgZ[0];
}
}
double Distribution::getEmissionTimeShift() const {
......
......@@ -213,8 +213,8 @@ bool Edit::cycle(const PlaceRep &init) {
}
pos.leave(*start);
start++;
start++;
++start;
++start;
}
return false;
......
......@@ -75,9 +75,8 @@ Object *LineTemplate::makeInstance
i < formals.size(); i++) {
if(word == formals[i]) {
std::vector<Token> act = actuals[i];
for(std::vector<Token>::iterator t = act.begin();
t != act.end(); t++) {
expansion.append(*t);
for(Token t : act) {
expansion.append(t);
}
found = true;
break;
......
......@@ -233,11 +233,10 @@ void Sequence::print(std::ostream &os) const {
if(isShared()) os << "SHARED ";
os << getOpalName() << ":SEQUENCE";
for(std::vector<Attribute>::const_iterator i = itsAttr.begin();
i != itsAttr.end(); i++) {
if(*i) {
os << ',' << i->getName()
<< (i->isExpression() ? ":=" : "=") << *i;
for(const Attribute& i : itsAttr) {
if(i) {
os << ',' << i.getName()
<< (i.isExpression() ? ":=" : "=") << i;
}
}
......
......@@ -78,9 +78,8 @@ Object *SequenceTemplate::makeInstance
i < formals.size(); i++) {
if(word == formals[i]) {
std::vector<Token> act = actuals[i];
for(std::vector<Token>::iterator t = act.begin();
t != act.end(); t++) {
expansion->append(*t);
for(Token t : act) {
expansion->append(t);
}
found = true;
break;
......
......@@ -76,9 +76,8 @@ Object *MacroCmd::makeInstance
i < formals.size(); i++) {
if(word == formals[i]) {
std::vector<Token> act = actuals[i];
for(std::vector<Token>::iterator t = act.begin();
t != act.end(); t++) {
macro->body->append(*t);
for(Token t : act) {
macro->body->append(t);
}
found = true;
break;
......
......@@ -361,13 +361,13 @@ void EllipticDomain::linearInterpolation(int x, int y, int z, double &W, double
double dx = 0.0;
std::multimap<int, double>::iterator it = IntersectXDir.find(y);
if(cx < 0)
it++;
++it;
dx = it->second;
double dy = 0.0;
it = IntersectYDir.find(x);
if(cy < 0)
it++;
++it;
dy = it->second;
......@@ -453,13 +453,13 @@ void EllipticDomain::quadraticInterpolation(int x, int y, int z, double &W, doub
double dx = 0.0;
std::multimap<int, double>::iterator it = IntersectXDir.find(y);
if(cx < 0)
it++;
++it;
dx = it->second;