diff --git a/CMakeLists.txt b/CMakeLists.txt index 6202498..b4a28a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,6 +7,17 @@ #Minimum required CMake Version cmake_minimum_required(VERSION 2.8.12) +# Only set the cxx_standard if it is not set by someone else +if (NOT DEFINED CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 14) +endif() + +# strongly encouraged to enable this globally to avoid conflicts between +# -Wpedantic being enabled and -std=c++20 and -std=gnu++20 for example +# when compiling with PCH enabled +set(CMAKE_CXX_EXTENSIONS OFF) + + #Project Name PROJECT(NCXX C CXX) set(PACKAGE "netcdf-cxx4" CACHE STRING "") diff --git a/cxx4/ncAtt.cpp b/cxx4/ncAtt.cpp index c373e73..9b639e4 100644 --- a/cxx4/ncAtt.cpp +++ b/cxx4/ncAtt.cpp @@ -65,22 +65,19 @@ NcType NcAtt::getType() const{ // get the identifier for the netCDF type of this attribute. nc_type xtypep; ncCheck(nc_inq_atttype(groupId,varId,myName.c_str(),&xtypep),__FILE__,__LINE__); - if(xtypep <= 12) + if (xtypep <= 12) { // This is an atomic type return NcType(xtypep); - else - // this is a user-defined type - { - // now get the set of NcType objects in this file. - multimap typeMap(getParentGroup().getTypes(NcGroup::ParentsAndCurrent)); - multimap::iterator iter; - // identify the Nctype object with the same id as this attribute. - for (iter=typeMap.begin(); iter!= typeMap.end();iter++) { - if(iter->second.getId() == xtypep) return iter->second; - } - // return a null object, as no type was identified. - return NcType(); - } + } + // this is a user-defined type + // now get the set of NcType objects in this file. + multimap typeMap(getParentGroup().getTypes(NcGroup::ParentsAndCurrent)); + // identify the Nctype object with the same id as this attribute. + for (const auto& type: typeMap) { + if(type.second.getId() == xtypep) return type.second; + } + // return a null object, as no type was identified. + return NcType(); } // Gets attribute length. diff --git a/cxx4/ncDim.cpp b/cxx4/ncDim.cpp index c207b37..5b7ff9b 100644 --- a/cxx4/ncDim.cpp +++ b/cxx4/ncDim.cpp @@ -93,9 +93,8 @@ bool NcDim::isUnlimited() const // get all the unlimited dimension ids in this group vector unlimdimid(static_cast(numlimdims)); ncCheck(nc_inq_unlimdims(groupId,&numlimdims,&unlimdimid[0]),__FILE__,__LINE__); - vector::iterator it; // now look to see if this dimension is unlimited - it = find(unlimdimid.begin(),unlimdimid.end(),myId); + const auto it = find(unlimdimid.begin(),unlimdimid.end(),myId); return it != unlimdimid.end(); } return false; diff --git a/cxx4/ncGroup.cpp b/cxx4/ncGroup.cpp index aa543f1..60a7c9a 100644 --- a/cxx4/ncGroup.cpp +++ b/cxx4/ncGroup.cpp @@ -19,6 +19,8 @@ #include "ncString.h" #include #include +#include +#include #include "ncCheck.h" using namespace std; using namespace netCDF::exceptions; @@ -188,7 +190,7 @@ multimap NcGroup::getGroups(NcGroup::GroupLocation location // record this group if(location == ParentsAndCurrentGrps || location == AllGrps) { - ncGroups.insert(pair(getName(),*this)); + ncGroups.emplace(getName(), *this); } // the child groups of the current group @@ -200,9 +202,9 @@ multimap NcGroup::getGroups(NcGroup::GroupLocation location int* numgrps=NULL; // now get the id of each NcGroup and populate the ncGroups container. ncCheck(nc_inq_grps(myId, numgrps,&ncids[0]),__FILE__,__LINE__); - for(std::size_t i=0; i(tmpGroup.getName(),tmpGroup)); + for (auto ncid : ncids) { + NcGroup tmpGroup(ncid); + ncGroups.emplace(tmpGroup.getName(), tmpGroup); } } } @@ -214,7 +216,7 @@ multimap NcGroup::getGroups(NcGroup::GroupLocation location while(1) { const NcGroup parentGroup(tmpGroup.getParentGroup()); if(parentGroup.isNull()) break; - ncGroups.insert(pair(parentGroup.getName(),parentGroup)); + ncGroups.emplace(parentGroup.getName(), parentGroup); tmpGroup=parentGroup; } } @@ -222,11 +224,10 @@ multimap NcGroup::getGroups(NcGroup::GroupLocation location // search in child groups of the children if(location == ChildrenOfChildrenGrps || location == AllChildrenGrps || location == AllGrps ) { - multimap::iterator it; multimap groups(getGroups(ChildrenGrps)); - for (it=groups.begin();it!=groups.end();it++) { - multimap childGroups(it->second.getGroups(AllChildrenGrps)); - ncGroups.insert(childGroups.begin(),childGroups.end()); + for (const auto& group : groups) { + multimap childGroups(group.second.getGroups(AllChildrenGrps)); + ncGroups.insert(childGroups.begin(), childGroups.end()); } } @@ -236,13 +237,12 @@ multimap NcGroup::getGroups(NcGroup::GroupLocation location // Get the named child NcGroup object. NcGroup NcGroup::getGroup(const string& name,NcGroup::GroupLocation location) const{ if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getGroup on a Null group",__FILE__,__LINE__); - multimap ncGroups(getGroups(location)); - pair::iterator,multimap::iterator> ret; - ret = ncGroups.equal_range(name); - if(ret.first == ret.second) + + const auto ret = getGroups(location).equal_range(name); + if (ret.first == ret.second) { return NcGroup(); // null group is returned - else - return ret.first->second; + } + return ret.first->second; } @@ -251,12 +251,10 @@ NcGroup NcGroup::getGroup(const string& name,NcGroup::GroupLocation location) co set NcGroup::getGroups(const std::string& name,NcGroup::GroupLocation location) const { if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getGroups on a Null group",__FILE__,__LINE__); // get the set of ncGroups in this group and above. - multimap ncGroups(getGroups(location)); - pair::iterator,multimap::iterator> ret; - multimap::iterator it; - ret = ncGroups.equal_range(name); + const auto ncGroups(getGroups(location)); + const auto ret = ncGroups.equal_range(name); set tmpGroup; - for (it=ret.first; it!=ret.second; ++it) { + for (auto it=ret.first; it!=ret.second; ++it) { tmpGroup.insert(it->second); } return tmpGroup; @@ -301,10 +299,8 @@ int NcGroup::getVarCount(NcGroup::Location location) const { // search recursively in all child groups if(location == ChildrenAndCurrent || location == Children || location == All) { - multimap::iterator it; - multimap groups(getGroups()); - for (it=groups.begin();it!=groups.end();it++) { - nvars += it->second.getVarCount(ChildrenAndCurrent); + for (auto group : getGroups()) { + nvars += group.second.getVarCount(ChildrenAndCurrent); } } return nvars; @@ -326,9 +322,9 @@ multimap NcGroup::getVars(NcGroup::Location location) const { int* nvars=NULL; vector varids(varCount); ncCheck(nc_inq_varids(myId, nvars,&varids[0]),__FILE__,__LINE__); - for(std::size_t i=0; i(tmpVar.getName(),tmpVar)); + for (auto varid : varids) { + NcVar tmpVar(*this, varid); + ncVars.emplace(tmpVar.getName(), tmpVar); } } } @@ -345,9 +341,9 @@ multimap NcGroup::getVars(NcGroup::Location location) const { int* nvars=NULL; vector varids(varCount); ncCheck(nc_inq_varids(tmpGroup.getId(), nvars,&varids[0]),__FILE__,__LINE__); - for(std::size_t i=0; i(tmpVar.getName(),tmpVar)); + for (auto varid : varids) { + NcVar tmpVar(tmpGroup, varid); + ncVars.emplace(tmpVar.getName(), tmpVar); } } // continue loop with the parent. @@ -357,10 +353,8 @@ multimap NcGroup::getVars(NcGroup::Location location) const { // search recusively in all child groups. if(location == ChildrenAndCurrent || location == Children || location == All ) { - multimap::iterator it; - multimap groups(getGroups()); - for (it=groups.begin();it!=groups.end();it++) { - multimap vars=it->second.getVars(ChildrenAndCurrent); + for (auto group : getGroups()) { + multimap vars = group.second.getVars(ChildrenAndCurrent); ncVars.insert(vars.begin(),vars.end()); } } @@ -372,12 +366,10 @@ multimap NcGroup::getVars(NcGroup::Location location) const { // Get all NcVar objects with a given name. set NcGroup::getVars(const string& name,NcGroup::Location location) const { // get the set of ncVars in this group and above. - multimap ncVars(getVars(location)); - pair::iterator,multimap::iterator> ret; - multimap::iterator it; - ret = ncVars.equal_range(name); + const auto ncVars(getVars(location)); + const auto ret = ncVars.equal_range(name); set tmpVar; - for (it=ret.first; it!=ret.second; ++it) { + for (auto it=ret.first; it!=ret.second; ++it) { tmpVar.insert(it->second); } return tmpVar; @@ -387,14 +379,12 @@ set NcGroup::getVars(const string& name,NcGroup::Location location) const // Get the named NcVar object. NcVar NcGroup::getVar(const string& name,NcGroup::Location location) const { - multimap ncVars(getVars(location)); - pair::iterator,multimap::iterator> ret; - ret = ncVars.equal_range(name); - if(ret.first == ret.second) + const auto ret = getVars(location).equal_range(name); + if (ret.first == ret.second) { // no matching netCDF variable found so return null object. return NcVar(); - else - return ret.first->second; + } + return ret.first->second; } // Adds a new netCDF scalar variable. @@ -457,8 +447,8 @@ NcVar NcGroup::addVar(const string& name, const string& typeName, const vector dimIds; dimIds.reserve(dimNames.size()); - for (size_t i=0; i::const_iterator iter; vector dimIds; dimIds.reserve(ncDimVector.size()); - for (iter=ncDimVector.begin();iter < ncDimVector.end(); iter++) { - if(iter->isNull()) throw NcNullDim("Attempt to invoke NcGroup::addVar with a Null NcDim object",__FILE__,__LINE__); - NcDim tmpDim(getDim(iter->getName(),NcGroup::ParentsAndCurrent)); + for (const auto& dim : ncDimVector) { + if(dim.isNull()) throw NcNullDim("Attempt to invoke NcGroup::addVar with a Null NcDim object",__FILE__,__LINE__); + NcDim tmpDim(getDim(dim.getName(),NcGroup::ParentsAndCurrent)); if(tmpDim.isNull()) throw NcNullDim("Attempt to invoke NcGroup::addVar failed: NcDim must be defined in either the current group or a parent group",__FILE__,__LINE__); dimIds.push_back(tmpDim.getId()); } @@ -529,10 +518,8 @@ int NcGroup::getAttCount(NcGroup::Location location) const { // search recursively in all child groups if(location == ChildrenAndCurrent || location == Children || location == All) { - multimap::iterator it; - multimap groups(getGroups()); - for (it=groups.begin();it!=groups.end();it++) { - ngatts += it->second.getAttCount(ChildrenAndCurrent); + for (const auto& group : getGroups()) { + ngatts += group.second.getAttCount(ChildrenAndCurrent); } } @@ -554,8 +541,9 @@ multimap NcGroup::getAtts(NcGroup::Location location) co for(int i=0; i(string(charName),tmpAtt)); + ncAtts.emplace(std::piecewise_construct, + std::forward_as_tuple(charName), + std::forward_as_tuple(tmpGroup, i)); } } @@ -569,8 +557,9 @@ multimap NcGroup::getAtts(NcGroup::Location location) co for(int i=0; i(string(charName),tmpAtt)); + ncAtts.emplace(std::piecewise_construct, + std::forward_as_tuple(charName), + std::forward_as_tuple(tmpGroup, i)); } // continue loop with the parent. tmpGroup=tmpGroup.getParentGroup(); @@ -579,10 +568,8 @@ multimap NcGroup::getAtts(NcGroup::Location location) co // search recusively in all child groups. if(location == ChildrenAndCurrent || location == Children || location == All ) { - multimap::iterator it; - multimap groups(getGroups()); - for (it=groups.begin();it!=groups.end();it++) { - multimap atts=it->second.getAtts(ChildrenAndCurrent); + for (const auto& group : getGroups()) { + const auto atts = group.second.getAtts(ChildrenAndCurrent); ncAtts.insert(atts.begin(),atts.end()); } } @@ -593,24 +580,21 @@ multimap NcGroup::getAtts(NcGroup::Location location) co // Get the named NcGroupAtt object. NcGroupAtt NcGroup::getAtt(const std::string& name,NcGroup::Location location) const { multimap ncAtts(getAtts(location)); - pair::iterator,multimap::iterator> ret; - ret = ncAtts.equal_range(name); - if(ret.first == ret.second) + const auto ret = ncAtts.equal_range(name); + if (ret.first == ret.second) { // no matching groupAttribute so return null object. return NcGroupAtt(); - else - return ret.first->second; + } + return ret.first->second; } // Get all NcGroupAtt objects with a given name. set NcGroup::getAtts(const string& name,NcGroup::Location location) const { // get the set of ncGroupAtts in this group and above. - multimap ncAtts(getAtts(location)); - pair::iterator,multimap::iterator> ret; - multimap::iterator it; - ret = ncAtts.equal_range(name); + const auto ncAtts(getAtts(location)); + const auto ret = ncAtts.equal_range(name); set tmpAtt; - for (it=ret.first; it!=ret.second; ++it) { + for (auto it=ret.first; it!=ret.second; ++it) { tmpAtt.insert(it->second); } return tmpAtt; @@ -917,19 +901,15 @@ int NcGroup::getDimCount(NcGroup::Location location) const { // search in parent groups. if(location == Parents || location == ParentsAndCurrent || location == All ) { - multimap::iterator it; - multimap groups(getGroups(ParentsGrps)); - for (it=groups.begin();it!=groups.end();it++) { - ndims += it->second.getDimCount(); + for (const auto& group : getGroups(ParentsGrps)) { + ndims += group.second.getDimCount(); } } // search in child groups. if(location == Children || location == ChildrenAndCurrent || location == All ) { - multimap::iterator it; - multimap groups(getGroups(AllChildrenGrps)); - for (it=groups.begin();it!=groups.end();it++) { - ndims += it->second.getDimCount(); + for (const auto& group : getGroups(AllChildrenGrps)) { + ndims += group.second.getDimCount(); } } return ndims; @@ -949,29 +929,25 @@ multimap NcGroup::getDims(NcGroup::Location location) const { vector dimids(dimCount); ncCheck(nc_inq_dimids(getId(), nullptr, &dimids[0], 0),__FILE__,__LINE__); // now get the name of each NcDim and populate the nDims container. - for(std::size_t i=0; i(tmpDim.getName(),tmpDim)); + for (auto dimid : dimids) { + NcDim tmpDim(*this,dimid); + ncDims.emplace(tmpDim.getName(), tmpDim); } } } // search in parent groups. if(location == Parents || location == ParentsAndCurrent || location == All ) { - multimap::iterator it; - multimap groups(getGroups(ParentsGrps)); - for (it=groups.begin();it!=groups.end();it++) { - multimap dimTmp(it->second.getDims()); + for (const auto& group: getGroups(ParentsGrps)) { + const auto dimTmp(group.second.getDims()); ncDims.insert(dimTmp.begin(),dimTmp.end()); } } // search in child groups (makes recursive calls). if(location == Children || location == ChildrenAndCurrent || location == All ) { - multimap::iterator it; - multimap groups(getGroups(AllChildrenGrps)); - for (it=groups.begin();it!=groups.end();it++) { - multimap dimTmp(it->second.getDims()); + for (const auto& group : getGroups(AllChildrenGrps)) { + const auto dimTmp(group.second.getDims()); ncDims.insert(dimTmp.begin(),dimTmp.end()); } } @@ -985,12 +961,11 @@ multimap NcGroup::getDims(NcGroup::Location location) const { NcDim NcGroup::getDim(const string& name,NcGroup::Location location) const { if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getDim on a Null group",__FILE__,__LINE__); multimap ncDims(getDims(location)); - pair::iterator,multimap::iterator> ret; - ret = ncDims.equal_range(name); - if(ret.first == ret.second) + const auto ret = ncDims.equal_range(name); + if (ret.first == ret.second) { return NcDim(); // null group is returned - else - return ret.first->second; + } + return ret.first->second; } @@ -998,12 +973,10 @@ NcDim NcGroup::getDim(const string& name,NcGroup::Location location) const { set NcGroup::getDims(const string& name,NcGroup::Location location) const { if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getDims on a Null group",__FILE__,__LINE__); // get the set of ncDims in this group and above. - multimap ncDims(getDims(location)); - pair::iterator,multimap::iterator> ret; - multimap::iterator it; - ret = ncDims.equal_range(name); + const auto ncDims(getDims(location)); + const auto ret = ncDims.equal_range(name); set tmpDim; - for (it=ret.first; it!=ret.second; ++it) { + for (auto it=ret.first; it!=ret.second; ++it) { tmpDim.insert(it->second); } return tmpDim; @@ -1055,19 +1028,15 @@ int NcGroup::getTypeCount(NcGroup::Location location) const { // search in parent groups. if(location == Parents || location == ParentsAndCurrent || location == All ) { - multimap::iterator it; - multimap groups(getGroups(ParentsGrps)); - for (it=groups.begin();it!=groups.end();it++) { - ntypes += it->second.getTypeCount(); + for (const auto& group : getGroups(ParentsGrps)) { + ntypes += group.second.getTypeCount(); } } // search in child groups. if(location == Children || location == ChildrenAndCurrent || location == All ) { - multimap::iterator it; - multimap groups(getGroups(AllChildrenGrps)); - for (it=groups.begin();it!=groups.end();it++) { - ntypes += it->second.getTypeCount(); + for (const auto& group : getGroups(AllChildrenGrps)) { + ntypes += group.second.getTypeCount(); } } return ntypes; @@ -1091,8 +1060,8 @@ int NcGroup::getTypeCount(NcType::ncType enumType, NcGroup::Location location) c if (ntypesp){ vector typeids(static_cast(ntypesp)); ncCheck(nc_inq_typeids(getId(), &ntypesp,&typeids[0]),__FILE__,__LINE__); - for (std::size_t i=0; i(ntypesp); i++){ - NcType tmpType(*this,typeids[i]); + for (auto typeid_ : typeids) { + NcType tmpType(*this, typeid_); if(tmpType.getTypeClass() == enumType) ntypes++; } } @@ -1100,19 +1069,15 @@ int NcGroup::getTypeCount(NcType::ncType enumType, NcGroup::Location location) c // search in parent groups. if(location == Parents || location == ParentsAndCurrent || location == All ) { - multimap::iterator it; - multimap groups(getGroups(ParentsGrps)); - for (it=groups.begin();it!=groups.end();it++) { - ntypes += it->second.getTypeCount(enumType); + for (const auto& group : getGroups(ParentsGrps)) { + ntypes += group.second.getTypeCount(enumType); } } // search in child groups. if(location == Children || location == ChildrenAndCurrent || location == All ) { - multimap::iterator it; - multimap groups(getGroups(AllChildrenGrps)); - for (it=groups.begin();it!=groups.end();it++) { - ntypes += it->second.getTypeCount(enumType); + for (const auto& group : getGroups(AllChildrenGrps)) { + ntypes += group.second.getTypeCount(enumType); } } return ntypes; @@ -1132,29 +1097,25 @@ multimap NcGroup::getTypes(NcGroup::Location location) const { vector typeids(typeCount); ncCheck(nc_inq_typeids(getId(), nullptr, &typeids[0]),__FILE__,__LINE__); // now get the name of each NcType and populate the nTypes container. - for(std::size_t i=0; i(tmpType.getName(),tmpType)); + for (auto typeid_ : typeids) { + NcType tmpType(*this, typeid_); + ncTypes.emplace(tmpType.getName(), tmpType); } } } // search in parent groups. if(location == Parents || location == ParentsAndCurrent || location == All ) { - multimap::iterator it; - multimap groups(getGroups(ParentsGrps)); - for (it=groups.begin();it!=groups.end();it++) { - multimap typeTmp(it->second.getTypes()); + for (const auto& group : getGroups(ParentsGrps)) { + multimap typeTmp(group.second.getTypes()); ncTypes.insert(typeTmp.begin(),typeTmp.end()); } } // search in child groups (makes recursive calls). if(location == Children || location == ChildrenAndCurrent || location == All ) { - multimap::iterator it; - multimap groups(getGroups(AllChildrenGrps)); - for (it=groups.begin();it!=groups.end();it++) { - multimap typeTmp(it->second.getTypes()); + for (const auto& group : getGroups(AllChildrenGrps)) { + multimap typeTmp(group.second.getTypes()); ncTypes.insert(typeTmp.begin(),typeTmp.end()); } } @@ -1166,17 +1127,12 @@ multimap NcGroup::getTypes(NcGroup::Location location) const { // Gets the collection of NcType objects with a given name. set NcGroup::getTypes(const string& name, NcGroup::Location location) const { if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getTypes on a Null group",__FILE__,__LINE__); - // iterator for the multimap container. - multimap::iterator it; - // return argument of equal_range: iterators to lower and upper bounds of the range. - pair::iterator,multimap::iterator> ret; // get the entire collection of types. - multimap types(getTypes(location)); - // define STL set object to hold the result - set tmpType; + const auto types(getTypes(location)); // get the set of NcType objects with a given name - ret=types.equal_range(name); - for (it=ret.first;it!=ret.second;it++) { + const auto ret = types.equal_range(name); + set tmpType; + for (auto it=ret.first;it!=ret.second;it++) { tmpType.insert(it->second); } return tmpType; @@ -1186,16 +1142,13 @@ set NcGroup::getTypes(const string& name, NcGroup::Location location) co // Gets the collection of NcType objects with a given data type. set NcGroup::getTypes(NcType::ncType enumType, NcGroup::Location location) const { if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getTypes on a Null group",__FILE__,__LINE__); - // iterator for the multimap container. - multimap::iterator it; - // get the entire collection of types. - multimap types(getTypes(location)); + // define STL set object to hold the result set tmpType; // get the set of NcType objects with a given data type - for (it=types.begin();it!=types.end();it++) { - if(it->second.getTypeClass() == enumType) { - tmpType.insert(it->second); + for (const auto& type : getTypes(location)) { + if (type.second.getTypeClass() == enumType) { + tmpType.insert(type.second); } } return(tmpType); @@ -1205,18 +1158,15 @@ set NcGroup::getTypes(NcType::ncType enumType, NcGroup::Location locatio // Gets the collection of NcType objects with a given name and data type. set NcGroup::getTypes(const string& name, NcType::ncType enumType, NcGroup::Location location) const { if(isNull()) throw NcNullGrp("Attempt to invoke NcGroup::getTypes on a Null group",__FILE__,__LINE__); - // iterator for the multimap container. - multimap::iterator it; - // return argument of equal_range: iterators to lower and upper bounds of the range. - pair::iterator,multimap::iterator> ret; + // get the entire collection of types. multimap types(getTypes(location)); // define STL set object to hold the result set tmpType; // get the set of NcType objects with a given name - ret=types.equal_range(name); - for (it=ret.first;it!=ret.second;it++) { - if((*it).second.getTypeClass() == enumType) { + const auto ret=types.equal_range(name); + for (auto it=ret.first;it!=ret.second;it++) { + if(it->second.getTypeClass() == enumType) { tmpType.insert(it->second); } } @@ -1241,20 +1191,16 @@ NcType NcGroup::getType(const string& name, NcGroup::Location location) const { if(name == "string" ) return ncString; // this is a user defined type - // iterator for the multimap container. - multimap::iterator it; - // return argument of equal_range: iterators to lower and upper bounds of the range. - pair::iterator,multimap::iterator> ret; // get the entire collection of types. multimap types(getTypes(location)); // define STL set object to hold the result set tmpType; // get the set of NcType objects with a given name - ret=types.equal_range(name); - if(ret.first == ret.second) + const auto ret=types.equal_range(name); + if(ret.first == ret.second) { return NcType(); - else - return ret.first->second; + } + return ret.first->second; } @@ -1303,8 +1249,6 @@ map NcGroup::getCoordVars(NcGroup::Location location) const { // search in current group and parent groups. NcGroup tmpGroup(*this); - multimap::iterator itD; - multimap::iterator itV; bool check_current_group = !(location == Parents || location == Children); const bool check_parent_groups = (location == Parents || location == ParentsAndCurrent || location == All); const bool check_child_groups = (location == Children || location == ChildrenAndCurrent || location == All); @@ -1312,13 +1256,12 @@ map NcGroup::getCoordVars(NcGroup::Location location) const { while(1) { // get the collection of NcDim objects defined in this group. if (check_current_group) { - multimap dimTmp(tmpGroup.getDims()); multimap varTmp(tmpGroup.getVars()); - for (itD=dimTmp.begin();itD!=dimTmp.end();itD++) { - string coordName(itD->first); - itV = varTmp.find(coordName); - if(itV != varTmp.end()) { - coordVars.insert(pair(string(coordName),tmpGroup)); + for (const auto& dim : tmpGroup.getDims()) { + string coordName(dim.first); + const auto var = varTmp.find(coordName); + if(var != varTmp.end()) { + coordVars.emplace(coordName, tmpGroup); } } } @@ -1334,10 +1277,8 @@ map NcGroup::getCoordVars(NcGroup::Location location) const { // search in child groups (makes recursive calls). if (check_child_groups) { - multimap::iterator it; - multimap groups(getGroups()); - for (it=groups.begin();it!=groups.end();it++) { - map coordVarsTmp = it->second.getCoordVars(ChildrenAndCurrent); + for (const auto& group : getGroups()) { + map coordVarsTmp = group.second.getCoordVars(ChildrenAndCurrent); coordVars.insert(coordVarsTmp.begin(),coordVarsTmp.end()); } } @@ -1352,9 +1293,7 @@ void NcGroup::getCoordVar(const string& coordVarName, NcDim& ncDim, NcVar& ncVar ncVar = NcVar{}; // search in current group and parent groups. - multimap::iterator itD; NcGroup tmpGroup(*this); - multimap::iterator itV; bool check_current_group = !(location == Parents || location == Children); const bool check_parent_groups = (location == Parents || location == ParentsAndCurrent || location == All); const bool check_child_groups = (location == Children || location == ChildrenAndCurrent || location == All); @@ -1364,11 +1303,11 @@ void NcGroup::getCoordVar(const string& coordVarName, NcDim& ncDim, NcVar& ncVar if (check_current_group) { multimap dimTmp(tmpGroup.getDims()); multimap varTmp(tmpGroup.getVars()); - itD=dimTmp.find(coordVarName); - itV=varTmp.find(coordVarName); - if(itD != dimTmp.end() && itV != varTmp.end()) { - ncDim=itD->second; - ncVar=itV->second; + const auto dim = dimTmp.find(coordVarName); + const auto var = varTmp.find(coordVarName); + if(dim != dimTmp.end() && var != varTmp.end()) { + ncDim=dim->second; + ncVar=var->second; return; } } @@ -1382,10 +1321,8 @@ void NcGroup::getCoordVar(const string& coordVarName, NcDim& ncDim, NcVar& ncVar // search in child groups (makes recursive calls). if (check_child_groups) { - multimap::iterator it; - multimap groups(getGroups()); - for (it=groups.begin();it!=groups.end();it++) { - it->second.getCoordVar(coordVarName,ncDim,ncVar,ChildrenAndCurrent); + for (const auto& group : getGroups()) { + group.second.getCoordVar(coordVarName,ncDim,ncVar,ChildrenAndCurrent); if(!ncDim.isNull()) break; } } diff --git a/cxx4/ncVar.cpp b/cxx4/ncVar.cpp index 76d4feb..70f7f63 100644 --- a/cxx4/ncVar.cpp +++ b/cxx4/ncVar.cpp @@ -116,10 +116,9 @@ NcType NcVar::getType() const { if(xtypep == ncDouble.getId() ) return ncDouble; if(xtypep == ncString.getId() ) return ncString; - multimap::const_iterator it; multimap types(NcGroup(groupId).getTypes(NcGroup::ParentsAndCurrent)); - for(it=types.begin(); it!=types.end(); it++) { - if(it->second.getId() == xtypep) return it->second; + for (const auto& type : types) { + if(type.second.getId() == xtypep) return type.second; } // we will never reach here return true; @@ -159,8 +158,8 @@ vector NcVar::getDims() const vector dimids(dimCount); ncCheck(nc_inq_vardimid(groupId,myId, &dimids[0]),__FILE__,__LINE__); ncDims.reserve(dimCount); - for (std::size_t i = 0; i < dimCount; i++) { - NcDim tmpDim(getParentGroup(),dimids[i]); + for (auto dimid : dimids) { + NcDim tmpDim(getParentGroup(), dimid); ncDims.push_back(tmpDim); } } @@ -202,7 +201,7 @@ map NcVar::getAtts() const map ncAtts; for (int i=0; i(tmpAtt.getName(),tmpAtt)); + ncAtts.emplace(tmpAtt.getName(), tmpAtt); } return ncAtts; } @@ -212,8 +211,7 @@ map NcVar::getAtts() const NcVarAtt NcVar::getAtt(const string& name) const { map attributeList = getAtts(); - map::iterator myIter; - myIter = attributeList.find(name); + const auto myIter = attributeList.find(name); if(myIter == attributeList.end()){ string msg("Attribute '"+name+"' not found"); throw NcException(msg.c_str(),__FILE__,__LINE__);