Skip to content

Commit

Permalink
Use .emplace instead of .insert to avoid extraneous copies
Browse files Browse the repository at this point in the history
  • Loading branch information
ZedThree committed Dec 11, 2023
1 parent d79c05e commit 61a4568
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 14 deletions.
30 changes: 17 additions & 13 deletions cxx4/ncGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "ncString.h"
#include <cstddef>
#include <ncException.h>
#include <tuple>
#include <utility>
#include "ncCheck.h"
using namespace std;
using namespace netCDF::exceptions;
Expand Down Expand Up @@ -188,7 +190,7 @@ multimap<std::string,NcGroup> NcGroup::getGroups(NcGroup::GroupLocation location

// record this group
if(location == ParentsAndCurrentGrps || location == AllGrps) {
ncGroups.insert(pair<const string,NcGroup>(getName(),*this));
ncGroups.emplace(getName(), *this);
}

// the child groups of the current group
Expand All @@ -202,7 +204,7 @@ multimap<std::string,NcGroup> NcGroup::getGroups(NcGroup::GroupLocation location
ncCheck(nc_inq_grps(myId, numgrps,&ncids[0]),__FILE__,__LINE__);
for (auto ncid : ncids) {
NcGroup tmpGroup(ncid);
ncGroups.insert(pair<const string,NcGroup>(tmpGroup.getName(),tmpGroup));
ncGroups.emplace(tmpGroup.getName(), tmpGroup);
}
}
}
Expand Down Expand Up @@ -321,8 +323,8 @@ multimap<std::string,NcVar> NcGroup::getVars(NcGroup::Location location) const {
vector<int> varids(varCount);
ncCheck(nc_inq_varids(myId, nvars,&varids[0]),__FILE__,__LINE__);
for (auto varid : varids) {
NcVar tmpVar(*this,varid);
ncVars.insert(pair<const string,NcVar>(tmpVar.getName(),tmpVar));
NcVar tmpVar(*this, varid);
ncVars.emplace(tmpVar.getName(), tmpVar);
}
}
}
Expand All @@ -341,7 +343,7 @@ multimap<std::string,NcVar> NcGroup::getVars(NcGroup::Location location) const {
ncCheck(nc_inq_varids(tmpGroup.getId(), nvars,&varids[0]),__FILE__,__LINE__);
for (auto varid : varids) {
NcVar tmpVar(tmpGroup, varid);
ncVars.insert(pair<const string,NcVar>(tmpVar.getName(),tmpVar));
ncVars.emplace(tmpVar.getName(), tmpVar);
}
}
// continue loop with the parent.
Expand Down Expand Up @@ -539,8 +541,9 @@ multimap<std::string,NcGroupAtt> NcGroup::getAtts(NcGroup::Location location) co
for(int i=0; i<attCount;i++){
char charName[NC_MAX_NAME+1];
ncCheck(nc_inq_attname(tmpGroup.getId(),NC_GLOBAL,i,charName),__FILE__,__LINE__);
NcGroupAtt tmpAtt(tmpGroup.getId(),i);
ncAtts.insert(pair<const string,NcGroupAtt>(string(charName),tmpAtt));
ncAtts.emplace(std::piecewise_construct,
std::forward_as_tuple(charName),
std::forward_as_tuple(tmpGroup, i));
}
}

Expand All @@ -554,8 +557,9 @@ multimap<std::string,NcGroupAtt> NcGroup::getAtts(NcGroup::Location location) co
for(int i=0; i<attCount;i++){
char charName[NC_MAX_NAME+1];
ncCheck(nc_inq_attname(tmpGroup.getId(),NC_GLOBAL,i,charName),__FILE__,__LINE__);
NcGroupAtt tmpAtt(tmpGroup.getId(),i);
ncAtts.insert(pair<const string,NcGroupAtt>(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();
Expand Down Expand Up @@ -927,7 +931,7 @@ multimap<string,NcDim> NcGroup::getDims(NcGroup::Location location) const {
// now get the name of each NcDim and populate the nDims container.
for (auto dimid : dimids) {
NcDim tmpDim(*this,dimid);
ncDims.insert(pair<const string,NcDim>(tmpDim.getName(),tmpDim));
ncDims.emplace(tmpDim.getName(), tmpDim);
}
}
}
Expand Down Expand Up @@ -1094,8 +1098,8 @@ multimap<string,NcType> NcGroup::getTypes(NcGroup::Location location) const {
ncCheck(nc_inq_typeids(getId(), nullptr, &typeids[0]),__FILE__,__LINE__);
// now get the name of each NcType and populate the nTypes container.
for (auto typeid_ : typeids) {
NcType tmpType(*this,typeid_);
ncTypes.insert(pair<const string,NcType>(tmpType.getName(),tmpType));
NcType tmpType(*this, typeid_);
ncTypes.emplace(tmpType.getName(), tmpType);
}
}
}
Expand Down Expand Up @@ -1257,7 +1261,7 @@ map<string,NcGroup> NcGroup::getCoordVars(NcGroup::Location location) const {
string coordName(dim.first);
const auto var = varTmp.find(coordName);
if(var != varTmp.end()) {
coordVars.insert(pair<const string,NcGroup>(coordName, tmpGroup));
coordVars.emplace(coordName, tmpGroup);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion cxx4/ncVar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ map<string,NcVarAtt> NcVar::getAtts() const
map<string,NcVarAtt> ncAtts;
for (int i=0; i<attCount; i++){
NcVarAtt tmpAtt(getParentGroup(),*this,i);
ncAtts.insert(pair<const string,NcVarAtt>(tmpAtt.getName(),tmpAtt));
ncAtts.emplace(tmpAtt.getName(), tmpAtt);
}
return ncAtts;
}
Expand Down

0 comments on commit 61a4568

Please sign in to comment.