From 09210f271c008d81343f9c321e4019d552f6777d Mon Sep 17 00:00:00 2001 From: Giovanni Bussi <giovanni.bussi@gmail.com> Date: Sun, 5 Mar 2017 19:40:58 +0100 Subject: [PATCH] Massive replacement of c++11 loops in core --- src/core/Action.cpp | 16 ++++----- src/core/Action.h | 1 - src/core/ActionAtomistic.cpp | 6 ++-- src/core/ActionRegister.cpp | 7 ++-- src/core/ActionRegister.h | 8 ----- src/core/ActionSet.cpp | 8 ++--- src/core/ActionSet.h | 14 ++++---- src/core/ActionSetup.cpp | 4 +-- src/core/ActionWithVirtualAtom.cpp | 4 +-- src/core/Atoms.cpp | 18 +++++----- src/core/CLToolRegister.cpp | 7 ++-- src/core/CLToolRegister.h | 4 --- src/core/GREX.cpp | 2 +- src/core/PlumedMain.cpp | 55 +++++++++++++++--------------- src/core/PlumedMain.h | 1 - src/core/Value.cpp | 18 +++++----- 16 files changed, 79 insertions(+), 94 deletions(-) diff --git a/src/core/Action.cpp b/src/core/Action.cpp index ff1efdb71..dfccecb17 100644 --- a/src/core/Action.cpp +++ b/src/core/Action.cpp @@ -115,8 +115,8 @@ int Action::fclose(FILE*fp){ } void Action::fflush(){ - for(files_iterator p=files.begin();p!=files.end();++p){ - std::fflush((*p)); + for(const auto & p : files){ + std::fflush(p); } } @@ -171,14 +171,14 @@ void Action::activate(){ prepare(); this->lockRequests(); } else return; - for(Dependencies::iterator p=after.begin();p!=after.end();++p) (*p)->activate(); + for(const auto & p : after) p->activate(); active=true; } void Action::setOption(const std::string &s){ // This overloads the action and activate some options options.insert(s); - for(Dependencies::iterator p=after.begin();p!=after.end();++p) (*p)->setOption(s); + for(const auto & p : after) p->setOption(s); } void Action::clearOptions(){ @@ -240,11 +240,11 @@ void Action::warning( const std::string & msg ){ void Action::calculateFromPDB( const PDB& pdb ){ activate(); - for(Dependencies::iterator p=after.begin();p!=after.end();++p){ - ActionWithValue*av=dynamic_cast<ActionWithValue*>(*p); + for(const auto & p : after){ + ActionWithValue*av=dynamic_cast<ActionWithValue*>(p); if(av){ av->clearInputForces(); av->clearDerivatives(); } - (*p)->readAtomsFromPDB( pdb ); - (*p)->calculate(); + p->readAtomsFromPDB( pdb ); + p->calculate(); } readAtomsFromPDB( pdb ); calculate(); diff --git a/src/core/Action.h b/src/core/Action.h index 360813e90..21e0ea971 100644 --- a/src/core/Action.h +++ b/src/core/Action.h @@ -161,7 +161,6 @@ public: /// std::set<FILE*> files; - typedef std::set<FILE*>::iterator files_iterator; public: /// Standard constructor from ActionOptions diff --git a/src/core/ActionAtomistic.cpp b/src/core/ActionAtomistic.cpp index 9f8e900a8..662e4497b 100644 --- a/src/core/ActionAtomistic.cpp +++ b/src/core/ActionAtomistic.cpp @@ -188,7 +188,7 @@ void ActionAtomistic::interpretAtomList( std::vector<std::string>& strings, std: // here we check if the atom name is the name of a group if(!ok){ if(atoms.groups.count(strings[i])){ - map<string,vector<AtomNumber> >::const_iterator m=atoms.groups.find(strings[i]); + const auto m=atoms.groups.find(strings[i]); t.insert(t.end(),m->second.begin(),m->second.end()); ok=true; } @@ -196,8 +196,8 @@ void ActionAtomistic::interpretAtomList( std::vector<std::string>& strings, std: // here we check if the atom name is the name of an added virtual atom if(!ok){ const ActionSet&actionSet(plumed.getActionSet()); - for(ActionSet::const_iterator a=actionSet.begin();a!=actionSet.end();++a){ - ActionWithVirtualAtom* c=dynamic_cast<ActionWithVirtualAtom*>(*a); + for(const auto & a : actionSet){ + ActionWithVirtualAtom* c=dynamic_cast<ActionWithVirtualAtom*>(a); if(c) if(c->getLabel()==strings[i]){ ok=true; t.push_back(c->getIndex()); diff --git a/src/core/ActionRegister.cpp b/src/core/ActionRegister.cpp index cac31b0b6..96fb73fb6 100644 --- a/src/core/ActionRegister.cpp +++ b/src/core/ActionRegister.cpp @@ -32,7 +32,7 @@ namespace PLMD{ ActionRegister::~ActionRegister(){ if(m.size()>0){ string names=""; - for(mIterator p=m.begin();p!=m.end();++p)names+=p->first+" "; + for(const auto & p : m) names+=p.first+" "; std::cerr<<"WARNING: Directive "+ names +" has not been properly unregistered. This might lead to memory leak!!\n"; } } @@ -43,7 +43,7 @@ ActionRegister& actionRegister(){ } void ActionRegister::remove(creator_pointer f){ - for(mIterator p=m.begin();p!=m.end();++p){ + for(auto p=m.begin();p!=m.end();++p){ if((*p).second==f){ m.erase(p); break; } @@ -113,8 +113,7 @@ bool ActionRegister::printTemplate( const std::string& action, bool include_opti std::ostream & operator<<(std::ostream &log,const ActionRegister&ar){ vector<string> s; - for(ActionRegister::const_mIterator it=ar.m.begin();it!=ar.m.end();++it) - s.push_back((*it).first); + for(const auto & it : ar.m) s.push_back(it.first); sort(s.begin(),s.end()); for(unsigned i=0;i<s.size();i++) log<<" "<<s[i]<<"\n"; if(!ar.disabled.empty()){ diff --git a/src/core/ActionRegister.h b/src/core/ActionRegister.h index cf2cd35fe..6bf48530a 100644 --- a/src/core/ActionRegister.h +++ b/src/core/ActionRegister.h @@ -51,16 +51,8 @@ class ActionRegister{ typedef void(*keywords_pointer)(Keywords&); /// Map action to a function which creates the related object std::map<std::string,creator_pointer> m; -/// Iterator over the map - typedef std::map<std::string,creator_pointer>::iterator mIterator; -/// Iterator over the map - typedef std::map<std::string,creator_pointer>::const_iterator const_mIterator; /// Map action to a function which documents the related object std::map<std::string,keywords_pointer> mk; -/// Iterator over the map - typedef std::map<std::string,Keywords>::iterator mIteratork; -/// Iterator over the map - typedef std::map<std::string,Keywords>::const_iterator const_mIteratork; /// Set of disabled actions (which were registered more than once) std::set<std::string> disabled; public: diff --git a/src/core/ActionSet.cpp b/src/core/ActionSet.cpp index 1c01ece37..cfbc63cc6 100644 --- a/src/core/ActionSet.cpp +++ b/src/core/ActionSet.cpp @@ -42,16 +42,16 @@ void ActionSet::clearDelete(){ std::string ActionSet::getLabelList() const{ std::string outlist; - for(const_iterator p=begin();p!=end();++p){ - outlist+=dynamic_cast<Action*>(*p)->getLabel()+" "; + for(const auto & p : (*this)){ + outlist+=dynamic_cast<Action*>(p)->getLabel()+" "; }; return outlist; } std::vector<std::string> ActionSet::getLabelVector() const{ std::vector<std::string> outlist; - for(const_iterator p=begin();p!=end();++p){ - outlist.push_back(dynamic_cast<Action*>(*p)->getLabel()); + for(const auto & p : (*this)){ + outlist.push_back(dynamic_cast<Action*>(p)->getLabel()); }; return outlist; } diff --git a/src/core/ActionSet.h b/src/core/ActionSet.h index d0babf45a..2277f009b 100644 --- a/src/core/ActionSet.h +++ b/src/core/ActionSet.h @@ -72,8 +72,8 @@ public: template <class T> std::vector<T> ActionSet::select()const{ std::vector<T> ret; - for(const_iterator p=begin();p!=end();++p){ - T t=dynamic_cast<T>(*p); + for(const auto & p : (*this)){ + T t=dynamic_cast<T>(p); if(t) ret.push_back(t); }; return ret; @@ -81,8 +81,8 @@ std::vector<T> ActionSet::select()const{ template <class T> T ActionSet::selectWithLabel(const std::string&s)const{ - for(const_iterator p=begin();p!=end();++p){ - T t=dynamic_cast<T>(*p); + for(const auto & p : (*this)){ + T t=dynamic_cast<T>(p); if(t && dynamic_cast<Action*>(t)->getLabel()==s) return t; }; return NULL; @@ -91,9 +91,9 @@ T ActionSet::selectWithLabel(const std::string&s)const{ template <class T> std::vector<Action*> ActionSet::selectNot()const{ std::vector<Action*> ret; - for(const_iterator p=begin();p!=end();++p){ - T t=dynamic_cast<T>(*p); - if(!t) ret.push_back(*p); + for(const auto & p : (*this)){ + T t=dynamic_cast<T>(p); + if(!t) ret.push_back(p); }; return ret; } diff --git a/src/core/ActionSetup.cpp b/src/core/ActionSetup.cpp index d0b7fd0ff..0a310d565 100644 --- a/src/core/ActionSetup.cpp +++ b/src/core/ActionSetup.cpp @@ -30,9 +30,9 @@ ActionSetup::ActionSetup(const ActionOptions&ao): Action(ao) { const ActionSet& actionset(plumed.getActionSet()); - for(ActionSet::const_iterator p=actionset.begin();p!=actionset.end();++p){ + for(const auto & p : actionset){ // check that all the preceeding actions are ActionSetup - if( !dynamic_cast<ActionSetup*>(*p) ) error("Action " + getLabel() + " is a setup action, and should be only preceeded by other setup actions"); + if( !dynamic_cast<ActionSetup*>(p) ) error("Action " + getLabel() + " is a setup action, and should be only preceeded by other setup actions"); } } diff --git a/src/core/ActionWithVirtualAtom.cpp b/src/core/ActionWithVirtualAtom.cpp index 04febbf1e..de6d0c6cb 100644 --- a/src/core/ActionWithVirtualAtom.cpp +++ b/src/core/ActionWithVirtualAtom.cpp @@ -67,8 +67,8 @@ void ActionWithVirtualAtom::setGradients(){ // this case if the atom is a virtual one if(atoms.isVirtualAtom(an)){ const ActionWithVirtualAtom* a=atoms.getVirtualAtomsAction(an); - for(std::map<AtomNumber,Tensor>::const_iterator p=a->gradients.begin();p!=a->gradients.end();++p){ - gradients[(*p).first]+=matmul(derivatives[i],(*p).second); + for(const auto & p : a->gradients){ + gradients[p.first]+=matmul(derivatives[i],p.second); } // this case if the atom is a normal one } else { diff --git a/src/core/Atoms.cpp b/src/core/Atoms.cpp index 2c3a5fbdc..834f2e75a 100644 --- a/src/core/Atoms.cpp +++ b/src/core/Atoms.cpp @@ -202,15 +202,15 @@ void Atoms::share(const std::set<AtomNumber>& unique){ for(unsigned i=0;i<dd.mpi_request_index.size();i++) dd.mpi_request_index[i].wait(); } int count=0; - for(std::set<AtomNumber>::const_iterator p=unique.begin();p!=unique.end();++p){ - if(dd.g2l[p->index()]>=0){ - dd.indexToBeSent[count]=p->index(); - dd.positionsToBeSent[ndata*count+0]=positions[p->index()][0]; - dd.positionsToBeSent[ndata*count+1]=positions[p->index()][1]; - dd.positionsToBeSent[ndata*count+2]=positions[p->index()][2]; + for(const auto & p : unique){ + if(dd.g2l[p.index()]>=0){ + dd.indexToBeSent[count]=p.index(); + dd.positionsToBeSent[ndata*count+0]=positions[p.index()][0]; + dd.positionsToBeSent[ndata*count+1]=positions[p.index()][1]; + dd.positionsToBeSent[ndata*count+2]=positions[p.index()][2]; if(!massAndChargeOK){ - dd.positionsToBeSent[ndata*count+3]=masses[p->index()]; - dd.positionsToBeSent[ndata*count+4]=charges[p->index()]; + dd.positionsToBeSent[ndata*count+3]=masses[p.index()]; + dd.positionsToBeSent[ndata*count+4]=charges[p.index()]; } count++; } @@ -321,7 +321,7 @@ void Atoms::add(const ActionAtomistic*a){ } void Atoms::remove(const ActionAtomistic*a){ - vector<const ActionAtomistic*>::iterator f=find(actions.begin(),actions.end(),a); + auto f=find(actions.begin(),actions.end(),a); plumed_massert(f!=actions.end(),"cannot remove an action registered to atoms"); actions.erase(f); } diff --git a/src/core/CLToolRegister.cpp b/src/core/CLToolRegister.cpp index 5c62170e9..c300d3058 100644 --- a/src/core/CLToolRegister.cpp +++ b/src/core/CLToolRegister.cpp @@ -32,7 +32,7 @@ namespace PLMD{ CLToolRegister::~CLToolRegister(){ if(m.size()>0){ string names=""; - for(mIterator p=m.begin();p!=m.end();++p)names+=p->first+" "; + for(const auto & p : m) names+=p.first+" "; std::cerr<<"WARNING: CLTools "+ names +" has not been properly unregistered. This might lead to memory leak!!\n"; } } @@ -43,7 +43,7 @@ CLToolRegister& cltoolRegister(){ } void CLToolRegister::remove(creator_pointer f){ - for(mIterator p=m.begin();p!=m.end();++p){ + for(auto p=m.begin();p!=m.end();++p){ if((*p).second==f){ m.erase(p); break; } @@ -103,8 +103,7 @@ bool CLToolRegister::printManual( const std::string& cltool ){ vector<string> CLToolRegister::list()const{ vector<string> s; - for(const_mIterator it=m.begin();it!=m.end();++it) - s.push_back((*it).first); + for(const auto & it : m) s.push_back(it.first); sort(s.begin(),s.end()); return s; } diff --git a/src/core/CLToolRegister.h b/src/core/CLToolRegister.h index b4044ffae..c99be9011 100644 --- a/src/core/CLToolRegister.h +++ b/src/core/CLToolRegister.h @@ -46,10 +46,6 @@ class CLToolRegister{ std::map<std::string,creator_pointer> m; /// Map cltool name to the keywords for this function std::map<std::string,Keywords> mk; -/// Iterator over the map - typedef std::map<std::string,creator_pointer>::iterator mIterator; -/// Iterator over the map - typedef std::map<std::string,creator_pointer>::const_iterator const_mIterator; /// Set of disabled cltools (which were registered more than once) std::set<std::string> disabled; public: diff --git a/src/core/GREX.cpp b/src/core/GREX.cpp index 001b0108f..b86ac57b8 100644 --- a/src/core/GREX.cpp +++ b/src/core/GREX.cpp @@ -74,7 +74,7 @@ void GREX::cmd(const string&key,void*val){ // do nothing } else { int iword=-1; - auto it=GREXWordMap().find(words[0]); + const auto it=GREXWordMap().find(words[0]); if(it!=GREXWordMap().end()) iword=it->second; switch(iword){ case cmd_initialized: diff --git a/src/core/PlumedMain.cpp b/src/core/PlumedMain.cpp index dadd5f31d..ff4e9b79f 100644 --- a/src/core/PlumedMain.cpp +++ b/src/core/PlumedMain.cpp @@ -128,7 +128,7 @@ void PlumedMain::cmd(const std::string & word,void*val){ } else { int iword=-1; double d; - auto it=plumedMainWordMap().find(words[0]); + const auto it=plumedMainWordMap().find(words[0]); if(it!=plumedMainWordMap().end()) iword=it->second; switch(iword) { case cmd_setBox: @@ -593,8 +593,8 @@ void PlumedMain::prepareDependencies(){ // which can be dynamically changed). // First switch off all actions - for(ActionSet::iterator p=actionSet.begin();p!=actionSet.end();++p){ - (*p)->deactivate(); + for(const auto & p : actionSet){ + p->deactivate(); //I think this is already done inside deactivate //(*p)->clearOptions(); } @@ -609,9 +609,9 @@ void PlumedMain::prepareDependencies(){ }; // also, if one of them is the total energy, tell to atoms that energy should be collected - for(ActionSet::iterator p=actionSet.begin();p!=actionSet.end();++p){ - if((*p)->isActive()){ - if((*p)->checkNeedsGradients()) (*p)->setOption("GRADIENTS"); + for(const auto & p : actionSet){ + if(p->isActive()){ + if(p->checkNeedsGradients()) p->setOption("GRADIENTS"); } } @@ -654,16 +654,16 @@ void PlumedMain::justCalculate(){ int iaction=0; // calculate the active actions in order (assuming *backward* dependence) - for(ActionSet::iterator p=actionSet.begin();p!=actionSet.end();++p){ - if((*p)->isActive()){ + for(const auto & p : actionSet){ + if(p->isActive()){ std::string actionNumberLabel; if(detailedTimers){ Tools::convert(iaction,actionNumberLabel); - actionNumberLabel="4A "+actionNumberLabel+" "+(*p)->getLabel(); + actionNumberLabel="4A "+actionNumberLabel+" "+p->getLabel(); stopwatch.start(actionNumberLabel); } - ActionWithValue*av=dynamic_cast<ActionWithValue*>(*p); - ActionAtomistic*aa=dynamic_cast<ActionAtomistic*>(*p); + ActionWithValue*av=dynamic_cast<ActionWithValue*>(p); + ActionAtomistic*aa=dynamic_cast<ActionAtomistic*>(p); { if(av) av->clearInputForces(); if(av) av->clearDerivatives(); @@ -672,13 +672,13 @@ void PlumedMain::justCalculate(){ if(aa) aa->clearOutputForces(); if(aa) if(aa->isActive()) aa->retrieveAtoms(); } - if((*p)->checkNumericalDerivatives()) (*p)->calculateNumericalDerivatives(); - else (*p)->calculate(); + if(p->checkNumericalDerivatives()) p->calculateNumericalDerivatives(); + else p->calculate(); // This retrieves components called bias if(av) bias+=av->getOutputQuantity("bias"); if(av) work+=av->getOutputQuantity("work"); if(av)av->setGradientsIfNeeded(); - ActionWithVirtualAtom*avv=dynamic_cast<ActionWithVirtualAtom*>(*p); + ActionWithVirtualAtom*avv=dynamic_cast<ActionWithVirtualAtom*>(p); if(avv)avv->setGradientsIfNeeded(); if(detailedTimers) stopwatch.stop(actionNumberLabel); } @@ -697,18 +697,19 @@ void PlumedMain::backwardPropagate(){ int iaction=0; stopwatch.start("5 Applying (backward loop)"); // apply them in reverse order - for(ActionSet::reverse_iterator p=actionSet.rbegin();p!=actionSet.rend();++p){ - if((*p)->isActive()){ + for(auto pp=actionSet.rbegin();pp!=actionSet.rend();++pp){ + const auto & p(*pp); + if(p->isActive()){ std::string actionNumberLabel; if(detailedTimers){ Tools::convert(iaction,actionNumberLabel); - actionNumberLabel="5A "+actionNumberLabel+" "+(*p)->getLabel(); + actionNumberLabel="5A "+actionNumberLabel+" "+p->getLabel(); stopwatch.start(actionNumberLabel); } - (*p)->apply(); - ActionAtomistic*a=dynamic_cast<ActionAtomistic*>(*p); + p->apply(); + ActionAtomistic*a=dynamic_cast<ActionAtomistic*>(p); // still ActionAtomistic has a special treatment, since they may need to add forces on atoms if(a) a->applyForces(); @@ -730,9 +731,9 @@ void PlumedMain::update(){ stopwatch.start("6 Update"); // update step (for statistics, etc) updateFlags.push(true); - for(ActionSet::iterator p=actionSet.begin();p!=actionSet.end();++p){ - (*p)->beforeUpdate(); - if((*p)->isActive() && (*p)->checkUpdate() && updateFlagsTop()) (*p)->update(); + for(const auto & p : actionSet){ + p->beforeUpdate(); + if(p->isActive() && p->checkUpdate() && updateFlagsTop()) p->update(); } while(!updateFlags.empty()) updateFlags.pop(); if(!updateFlags.empty()) plumed_merror("non matching changes in the update flags"); @@ -748,7 +749,7 @@ void PlumedMain::update(){ if(step%10000==0||doCheckPoint){ fflush(); log.flush(); - for(ActionSet::const_iterator p=actionSet.begin();p!=actionSet.end();++p) (*p)->fflush(); + for(const auto & p : actionSet) p->fflush(); } stopwatch.stop("6 Update"); } @@ -815,8 +816,8 @@ std::string PlumedMain::cite(const std::string&item){ } void PlumedMain::fflush(){ - for(files_iterator p=files.begin();p!=files.end();++p){ - (*p)->flush(); + for(const auto & p : files){ + p->flush(); } } @@ -833,8 +834,8 @@ void PlumedMain::stop(){ } void PlumedMain::runJobsAtEndOfCalculation(){ - for(ActionSet::iterator p=actionSet.begin();p!=actionSet.end();++p){ - (*p)->runFinalJobs(); + for(const auto & p : actionSet){ + p->runFinalJobs(); } } diff --git a/src/core/PlumedMain.h b/src/core/PlumedMain.h index 234296f84..ced8d15c9 100644 --- a/src/core/PlumedMain.h +++ b/src/core/PlumedMain.h @@ -137,7 +137,6 @@ private: bool doCheckPoint; std::set<FileBase*> files; - typedef std::set<FileBase*>::iterator files_iterator; /// Stuff to make plumed stop the MD code cleanly int* stopFlag; diff --git a/src/core/Value.cpp b/src/core/Value.cpp index db58db328..e344efb1c 100644 --- a/src/core/Value.cpp +++ b/src/core/Value.cpp @@ -119,9 +119,9 @@ void Value::setGradients(){ AtomNumber an=aa->getAbsoluteIndex(j); if(atoms.isVirtualAtom(an)){ const ActionWithVirtualAtom* a=atoms.getVirtualAtomsAction(an); - for(std::map<AtomNumber,Tensor>::const_iterator p=a->getGradients().begin();p!=a->getGradients().end();++p){ + for(const auto & p : a->getGradients()){ // controllare l'ordine del matmul: - gradients[(*p).first]+=matmul(Vector(derivatives[3*j],derivatives[3*j+1],derivatives[3*j+2]),(*p).second); + gradients[p.first]+=matmul(Vector(derivatives[3*j],derivatives[3*j+1],derivatives[3*j+2]),p.second); } } else { for(unsigned i=0;i<3;i++) gradients[an][i]+=derivatives[3*j+i]; @@ -130,9 +130,9 @@ void Value::setGradients(){ } else if(aw){ std::vector<Value*> values=aw->getArguments(); for(unsigned j=0;j<derivatives.size();j++){ - for(std::map<AtomNumber,Vector>::const_iterator p=values[j]->gradients.begin();p!=values[j]->gradients.end();++p){ - AtomNumber iatom=(*p).first; - gradients[iatom]+=(*p).second*derivatives[j]; + for(const auto & p : values[j]->gradients){ + AtomNumber iatom=p.first; + gradients[iatom]+=p.second*derivatives[j]; } } } else plumed_error(); @@ -142,11 +142,11 @@ double Value::projection(const Value& v1,const Value&v2){ double proj=0.0; const std::map<AtomNumber,Vector> & grad1(v1.gradients); const std::map<AtomNumber,Vector> & grad2(v2.gradients); - for(std::map<AtomNumber,Vector>::const_iterator p1=grad1.begin();p1!=grad1.end();++p1){ - AtomNumber a=(*p1).first; - std::map<AtomNumber,Vector>::const_iterator p2=grad2.find(a); + for(const auto & p1 : grad1){ + AtomNumber a=p1.first; + const auto p2=grad2.find(a); if(p2!=grad2.end()){ - proj+=dotProduct((*p1).second,(*p2).second); + proj+=dotProduct(p1.second,(*p2).second); } } return proj; -- GitLab