From 778326c9518427395d11f2d3c78a376111258f9b Mon Sep 17 00:00:00 2001 From: Giovanni Bussi <giovanni.bussi@gmail.com> Date: Sun, 5 Mar 2017 19:41:01 +0100 Subject: [PATCH] Massive replacement of c++11 loops in tools --- src/tools/BiasRepresentation.cpp | 4 ++-- src/tools/Grid.cpp | 10 +++++----- src/tools/Grid.h | 2 -- src/tools/NeighborList.cpp | 3 +-- src/tools/PDB.cpp | 12 ++++-------- src/tools/Tools.cpp | 26 +++++++++++++------------- src/tools/Tools.h | 2 +- 7 files changed, 26 insertions(+), 33 deletions(-) diff --git a/src/tools/BiasRepresentation.cpp b/src/tools/BiasRepresentation.cpp index 3131bfdc4..9aefeb3ab 100644 --- a/src/tools/BiasRepresentation.cpp +++ b/src/tools/BiasRepresentation.cpp @@ -261,9 +261,9 @@ void BiasRepresentation::getMinMaxBin(vector<double> &vmin, vector<double> &vmax } void BiasRepresentation::clear(){ // clear the hills - for(vector<KernelFunctions*>::const_iterator it = hills.begin(); it != hills.end(); ++it) + for(const auto & it : hills) { - delete *it; + delete it; } hills.clear(); // clear the grid diff --git a/src/tools/Grid.cpp b/src/tools/Grid.cpp index 4cf1ed520..d66fee5ee 100644 --- a/src/tools/Grid.cpp +++ b/src/tools/Grid.cpp @@ -731,7 +731,7 @@ void Grid::findSetOfPointsOnContour(const double& target, const std::vector<bool double SparseGrid::getValue(index_t index)const{ plumed_assert(index<maxsize_); double value=0.0; - iterator it=map_.find(index); + const auto it=map_.find(index); if(it!=map_.end()) value=it->second; return value; } @@ -741,9 +741,9 @@ double SparseGrid::getValueAndDerivatives plumed_assert(index<maxsize_ && usederiv_ && der.size()==dimension_); double value=0.0; for(unsigned int i=0;i<dimension_;++i) der[i]=0.0; - iterator it=map_.find(index); + const auto it=map_.find(index); if(it!=map_.end()) value=it->second; - iterator_der itder=der_.find(index); + const auto itder=der_.find(index); if(itder!=der_.end()) der=itder->second; return value; } @@ -779,8 +779,8 @@ void SparseGrid::writeToFile(OFile& ofile){ double f; writeHeader(ofile); ofile.fmtField(" "+fmt_); - for(iterator it=map_.begin();it!=map_.end();++it){ - index_t i=(*it).first; + for(const auto & it : map_){ + index_t i=it.first; xx=getPoint(i); if(usederiv_){f=getValueAndDerivatives(i,der);} else{f=getValue(i);} diff --git a/src/tools/Grid.h b/src/tools/Grid.h index e423e332f..060aa7f8d 100644 --- a/src/tools/Grid.h +++ b/src/tools/Grid.h @@ -221,9 +221,7 @@ class SparseGrid : public Grid { std::map<index_t,double> map_; - typedef std::map<index_t,double>::const_iterator iterator; std::map< index_t,std::vector<double> > der_; - typedef std::map<index_t,std::vector<double> >::const_iterator iterator_der; protected: void clear(); diff --git a/src/tools/NeighborList.cpp b/src/tools/NeighborList.cpp index a07d8604d..bb2135321 100644 --- a/src/tools/NeighborList.cpp +++ b/src/tools/NeighborList.cpp @@ -128,8 +128,7 @@ vector<AtomNumber>& NeighborList::getReducedAtomList() { AtomNumber index0=fullatomlist_[neighbors_[i].first]; AtomNumber index1=fullatomlist_[neighbors_[i].second]; // I exploit the fact that requestlist_ is an ordered vector - vector<AtomNumber>::iterator p; - p = std::find(requestlist_.begin(), requestlist_.end(), index0); plumed_assert(p!=requestlist_.end()); newindex0=p-requestlist_.begin(); + auto p = std::find(requestlist_.begin(), requestlist_.end(), index0); plumed_assert(p!=requestlist_.end()); newindex0=p-requestlist_.begin(); p = std::find(requestlist_.begin(), requestlist_.end(), index1); plumed_assert(p!=requestlist_.end()); newindex1=p-requestlist_.begin(); neighbors_[i]=pair<unsigned,unsigned>(newindex0,newindex1); } diff --git a/src/tools/PDB.cpp b/src/tools/PDB.cpp index 3cd3de176..746e18212 100644 --- a/src/tools/PDB.cpp +++ b/src/tools/PDB.cpp @@ -66,22 +66,19 @@ const std::vector<AtomNumber> & PDB::getAtomNumbers()const{ } std::string PDB::getAtomName(AtomNumber a)const{ - std::map<AtomNumber,unsigned>::const_iterator p; - p=number2index.find(a); + const auto p=number2index.find(a); if(p==number2index.end()) return ""; else return atomsymb[p->second]; } unsigned PDB::getResidueNumber(AtomNumber a)const{ - std::map<AtomNumber,unsigned>::const_iterator p; - p=number2index.find(a); + const auto p=number2index.find(a); if(p==number2index.end()) return 0; else return residue[p->second]; } std::string PDB::getResidueName(AtomNumber a) const{ - std::map<AtomNumber,unsigned>::const_iterator p; - p=number2index.find(a); + const auto p=number2index.find(a); if(p==number2index.end()) return ""; else return residuenames[p->second]; } @@ -297,8 +294,7 @@ Log& operator<<(Log& ostr, const PDB& pdb){ } Vector PDB::getPosition(AtomNumber a)const{ - std::map<AtomNumber,unsigned>::const_iterator p; - p=number2index.find(a); + const auto p=number2index.find(a); if(p==number2index.end()) plumed_merror("atom not available"); else return positions[p->second]; } diff --git a/src/tools/Tools.cpp b/src/tools/Tools.cpp index 8a3d1cf4f..2dac5e033 100644 --- a/src/tools/Tools.cpp +++ b/src/tools/Tools.cpp @@ -212,7 +212,7 @@ void Tools::trimComments(string & s){ bool Tools::getKey(vector<string>& line,const string & key,string & s){ s.clear(); - for(vector<string>::iterator p=line.begin();p!=line.end();++p){ + for(auto p=line.begin();p!=line.end();++p){ if((*p).length()==0) continue; string x=(*p).substr(0,key.length()); if(x==key){ @@ -228,31 +228,31 @@ bool Tools::getKey(vector<string>& line,const string & key,string & s){ void Tools::interpretRanges(std::vector<std::string>&s){ vector<string> news; - for(vector<string>::iterator p=s.begin();p!=s.end();++p){ - news.push_back(*p); - size_t dash=p->find("-"); + for(const auto & p :s){ + news.push_back(p); + size_t dash=p.find("-"); if(dash==string::npos) continue; int first; - if(!Tools::convert(p->substr(0,dash),first)) continue; + if(!Tools::convert(p.substr(0,dash),first)) continue; int stride=1; int second; - size_t colon=p->substr(dash+1).find(":"); + size_t colon=p.substr(dash+1).find(":"); if(colon!=string::npos){ - if(!Tools::convert(p->substr(dash+1).substr(0,colon),second) || - !Tools::convert(p->substr(dash+1).substr(colon+1),stride)) continue; + if(!Tools::convert(p.substr(dash+1).substr(0,colon),second) || + !Tools::convert(p.substr(dash+1).substr(colon+1),stride)) continue; } else { - if(!Tools::convert(p->substr(dash+1),second)) continue; + if(!Tools::convert(p.substr(dash+1),second)) continue; } news.resize(news.size()-1); if(first<=second){ - plumed_massert(stride>0,"interpreting ranges "+ *p + ", stride should be positive"); + plumed_massert(stride>0,"interpreting ranges "+ p + ", stride should be positive"); for(int i=first;i<=second;i+=stride){ string ss; convert(i,ss); news.push_back(ss); } } else { - plumed_massert(stride<0,"interpreting ranges "+ *p + ", stride should be positive"); + plumed_massert(stride<0,"interpreting ranges "+ p + ", stride should be positive"); for(int i=first;i>=second;i+=stride){ string ss; convert(i,ss); @@ -326,8 +326,8 @@ bool Tools::startWith(const std::string & full,const std::string &start){ bool Tools::findKeyword(const std::vector<std::string>&line,const std::string&key){ const std::string search(key+"="); - for(vector<string>::const_iterator p=line.begin();p!=line.end();++p){ - if(startWith(*p,search)) return true; + for(const auto & p : line){ + if(startWith(p,search)) return true; } return false; } diff --git a/src/tools/Tools.h b/src/tools/Tools.h index bb9153adb..b0e7c3ab2 100644 --- a/src/tools/Tools.h +++ b/src/tools/Tools.h @@ -169,7 +169,7 @@ void Tools::removeDuplicates(std::vector<T>& vec) inline bool Tools::parseFlag(std::vector<std::string>&line,const std::string&key,bool&val){ - for(std::vector<std::string>::iterator p=line.begin();p!=line.end();++p){ + for(auto p=line.begin();p!=line.end();++p){ if(key==*p){ val=true; line.erase(p); -- GitLab