From c85dc28858e1dffddb25bb890082c972e3725d15 Mon Sep 17 00:00:00 2001 From: Michael Uleysky Date: Thu, 3 Aug 2023 15:39:50 +1000 Subject: [PATCH] Removed copy constructor from BaseData to prevent unnecessary copying of data --- include/actiondep.h | 6 ++--- include/basedata.h | 10 ++++++--- sources/AVISOLOCAL.cpp | 11 +++++----- sources/BINFILE.cpp | 11 +++++----- sources/MODISBINLOCAL.cpp | 4 ++-- src/layereddata.cpp | 46 ++++++++++++++++++--------------------- 6 files changed, 43 insertions(+), 45 deletions(-) diff --git a/include/actiondep.h b/include/actiondep.h index eb3978b..14dd54d 100644 --- a/include/actiondep.h +++ b/include/actiondep.h @@ -133,11 +133,11 @@ template ReadType Read(const D& data, const MString& vname, const Ba else if constexpr(ReadSupported) dat = data.Read(vname, ind); if(dat) - out.Add(dat); + out.Add(std::move(dat)); else ok = false; } - if(ok) return out.Div(); + if(ok) return std::move(out.Div()); } return RT(); } @@ -191,7 +191,7 @@ template UVData> ReadUV(const D& data, const BaseParameters } UV dat(u, v); if(dat) - out.Add(dat); + out.Add(std::move(dat)); else ok = false; } diff --git a/include/basedata.h b/include/basedata.h index 4df7b67..a9a03e1 100644 --- a/include/basedata.h +++ b/include/basedata.h @@ -25,7 +25,11 @@ class BaseData BaseData(size_t n, MString&& unit_ = ""): data(n), unit(std::move(unit_)) {} public: - BaseData() = default; + BaseData() = default; + BaseData(const BaseData&) = delete; + BaseData(BaseData&&) = default; + + BaseData& operator=(BaseData&&) = default; const real& V(size_t i) const { return data[i]; } real& V(size_t i) { return data[i]; } @@ -86,12 +90,12 @@ template class DefaultAverager: public Data } public: - void Add(const Data& d) + void Add(Data&& d) { if(!d) return; if(!*this) { - *static_cast(this) = d; + *static_cast(this) = std::move(d); count.resize(Data::N()); for(size_t i = 0; i < Data::N(); i++) count[i] = DataOk(this, i) ? 1 : 0; return; diff --git a/sources/AVISOLOCAL.cpp b/sources/AVISOLOCAL.cpp index cc27f5d..093ab3f 100644 --- a/sources/AVISOLOCAL.cpp +++ b/sources/AVISOLOCAL.cpp @@ -122,16 +122,15 @@ AVISOLOCALData::Data AVISOLOCALData::Read(const MString& vname, const BaseParame auto u = Read("u", ip, i); auto v = Read("v", ip, i); if(!(u && v)) return Data(); - auto out = u; - out.SetUnit(square ? ("(" + u.Unit() + ")2") : u.Unit()); - for(size_t ind = 0; ind < out.N(); ind++) + if(square && u.Unit().Exist()) u.SetUnit("(" + u.Unit() + ")2"); + for(size_t ind = 0; ind < u.N(); ind++) { if(u.IsFill(ind) || v.IsFill(ind)) - out.V(ind) = out.Fillval(); + u.V(ind) = u.Fillval(); else - out.V(ind) = square ? (u(ind) * u(ind) + v(ind) * v(ind)) : michlib::Hypot(u(ind), v(ind)); + u.V(ind) = square ? (u(ind) * u(ind) + v(ind) * v(ind)) : michlib::Hypot(u(ind), v(ind)); } - return out; + return u; } return Data(); } diff --git a/sources/BINFILE.cpp b/sources/BINFILE.cpp index 9b628a9..596d6e2 100644 --- a/sources/BINFILE.cpp +++ b/sources/BINFILE.cpp @@ -69,16 +69,15 @@ BINFILEData::Data BINFILEData::Read(const MString& vname, size_t i) const auto u = Read("u", i); auto v = Read("v", i); if(!(u && v)) return Data(); - auto out = u; - out.SetUnit(square ? ("(" + u.Unit() + ")2") : u.Unit()); - for(size_t ind = 0; ind < out.N(); ind++) + if(square && u.Unit().Exist()) u.SetUnit("(" + u.Unit() + ")2"); + for(size_t ind = 0; ind < u.N(); ind++) { if(u.IsFill(ind) || v.IsFill(ind)) - out.V(ind) = out.Fillval(); + u.V(ind) = u.Fillval(); else - out.V(ind) = square ? (u(ind) * u(ind) + v(ind) * v(ind)) : michlib::Hypot(u(ind), v(ind)); + u.V(ind) = square ? (u(ind) * u(ind) + v(ind) * v(ind)) : michlib::Hypot(u(ind), v(ind)); } - return out; + return u; } if(vname == "u" || vname == "v") diff --git a/sources/MODISBINLOCAL.cpp b/sources/MODISBINLOCAL.cpp index dd09451..36e5151 100644 --- a/sources/MODISBINLOCAL.cpp +++ b/sources/MODISBINLOCAL.cpp @@ -92,9 +92,9 @@ MODISBINLOCALData::Data MODISBINLOCALData::Read(const MString& var, const BasePa for(const auto& suf: dataset) { Data dat = ReadFile(suf, p, tind); - if(dat) out.Add(dat); + if(dat) out.Add(std::move(dat)); } - return out.Div(); + return std::move(out.Div()); } MODISBINLOCALData::Data MODISBINLOCALData::ReadFile(const MString& suf, const struct MODISBINLOCALData::Parameters* p, size_t tind) const diff --git a/src/layereddata.cpp b/src/layereddata.cpp index 8ea5fa1..98fbc63 100644 --- a/src/layereddata.cpp +++ b/src/layereddata.cpp @@ -193,16 +193,15 @@ LayeredData::Data LayeredData::Read(const MString& vname, const BaseParameters* auto temp = Read("temp", ip, i); auto sal = Read("sal", ip, i); if(!(temp && sal)) return Data(); - auto out = temp; - out.SetUnit("degrees_C"); - for(size_t ind = 0; ind < out.N(); ind++) + temp.SetUnit("degrees_C"); + for(size_t ind = 0; ind < temp.N(); ind++) { if(temp.IsFill(ind) || sal.IsFill(ind)) - out.V(ind) = out.Fillval(); + temp.V(ind) = temp.Fillval(); else - out.V(ind) = Temp2PTemp(temp.V(ind), sal.V(ind), Depth(p->layer), out.Lon(ind), out.Lat(ind)); + temp.V(ind) = Temp2PTemp(temp.V(ind), sal.V(ind), Depth(p->layer), temp.Lon(ind), temp.Lat(ind)); } - return out; + return temp; } // temp from ptemp and sal if(vname == "temp") @@ -210,16 +209,15 @@ LayeredData::Data LayeredData::Read(const MString& vname, const BaseParameters* auto temp = Read("ptemp", ip, i); auto sal = Read("sal", ip, i); if(!(temp && sal)) return Data(); - auto out = temp; - out.SetUnit("degrees_C"); - for(size_t ind = 0; ind < out.N(); ind++) + temp.SetUnit("degrees_C"); + for(size_t ind = 0; ind < temp.N(); ind++) { if(temp.IsFill(ind) || sal.IsFill(ind)) - out.V(ind) = out.Fillval(); + temp.V(ind) = temp.Fillval(); else - out.V(ind) = PTemp2Temp(temp.V(ind), sal.V(ind), Depth(p->layer), out.Lon(ind), out.Lat(ind)); + temp.V(ind) = PTemp2Temp(temp.V(ind), sal.V(ind), Depth(p->layer), temp.Lon(ind), temp.Lat(ind)); } - return out; + return temp; } // pdens from temp and sal if(vname == "pdens") @@ -228,17 +226,16 @@ LayeredData::Data LayeredData::Read(const MString& vname, const BaseParameters* auto temp = Read(tempispot ? "ptemp" : "temp", ip, i); auto sal = Read("sal", ip, i); if(!(temp && sal)) return Data(); - auto out = temp; - out.SetUnit("kg m-3"); - for(size_t ind = 0; ind < out.N(); ind++) + temp.SetUnit("kg m-3"); + for(size_t ind = 0; ind < temp.N(); ind++) { if(temp.IsFill(ind) || sal.IsFill(ind)) - out.V(ind) = out.Fillval(); + temp.V(ind) = temp.Fillval(); else - out.V(ind) = tempispot ? PTemp2PDens(temp.V(ind), sal.V(ind), Depth(p->layer), out.Lon(ind), out.Lat(ind)) - : Temp2PDens(temp.V(ind), sal.V(ind), Depth(p->layer), out.Lon(ind), out.Lat(ind)); + temp.V(ind) = tempispot ? PTemp2PDens(temp.V(ind), sal.V(ind), Depth(p->layer), temp.Lon(ind), temp.Lat(ind)) + : Temp2PDens(temp.V(ind), sal.V(ind), Depth(p->layer), temp.Lon(ind), temp.Lat(ind)); } - return out; + return temp; } // U and U2 from u and v if(vname == "U" || vname == "U2") @@ -247,16 +244,15 @@ LayeredData::Data LayeredData::Read(const MString& vname, const BaseParameters* auto u = Read("u", ip, i); auto v = Read("v", ip, i); if(!(u && v)) return Data(); - auto out = u; - out.SetUnit(square ? ("(" + u.Unit() + ")2") : u.Unit()); - for(size_t ind = 0; ind < out.N(); ind++) + if(square && u.Unit().Exist()) u.SetUnit("(" + u.Unit() + ")2"); + for(size_t ind = 0; ind < u.N(); ind++) { if(u.IsFill(ind) || v.IsFill(ind)) - out.V(ind) = out.Fillval(); + u.V(ind) = u.Fillval(); else - out.V(ind) = square ? (u(ind) * u(ind) + v(ind) * v(ind)) : michlib::Hypot(u(ind), v(ind)); + u.V(ind) = square ? (u(ind) * u(ind) + v(ind) * v(ind)) : michlib::Hypot(u(ind), v(ind)); } - return out; + return u; } return Data(); }