Commit 6eafacb5 authored by Cédric Traizet's avatar Cédric Traizet

PERF: use hidden friend idiom pattern when overloading operator== (code review)

parent 3825e8cf
Pipeline #5962 failed with stages
in 128 minutes and 27 seconds
......@@ -161,12 +161,28 @@ struct OTBMetadata_EXPORT RPCParam
oss << "]";
return oss.str();
};
// Equality comparison operator (hidden friend idiom)
friend bool operator==(const RPCParam & lhs, const RPCParam & rhs)
{
return lhs.LineOffset == rhs.LineOffset
&& lhs.SampleOffset == rhs.SampleOffset
&& lhs.LatOffset == rhs.LatOffset
&& lhs.LonOffset == rhs.LonOffset
&& lhs.HeightOffset == rhs.HeightOffset
&& lhs.LineScale == rhs.LineScale
&& lhs.SampleScale == rhs.SampleScale
&& lhs.LatScale == rhs.LatScale
&& lhs.LonScale == rhs.LonScale
&& lhs.HeightScale == rhs.HeightScale
&& std::equal(std::begin(lhs.LineNum), std::end(lhs.LineNum), std::begin(rhs.LineNum))
&& std::equal(std::begin(lhs.LineDen), std::end(lhs.LineDen), std::begin(rhs.LineDen))
&& std::equal(std::begin(lhs.SampleNum), std::end(lhs.SampleNum), std::begin(rhs.SampleNum))
&& std::equal(std::begin(lhs.SampleDen), std::end(lhs.SampleDen), std::begin(rhs.SampleDen));
}
};
OTBMetadata_EXPORT bool operator==(const RPCParam & lhs, const RPCParam & rhs);
} // end namespace Projection
} // end namespace otb
......
......@@ -238,9 +238,14 @@ struct OTBMetadata_EXPORT Time : tm
friend OTBMetadata_EXPORT std::istream& operator>>(std::istream& is, Time& val);
friend OTBMetadata_EXPORT bool operator==(const Time & lhs, const Time & rhs)
{
tm tmLhs = lhs;
tm tmRhs = rhs;
return mktime(&tmLhs) + lhs.frac_sec == mktime(&tmRhs) + rhs.frac_sec;
}
};
OTBMetadata_EXPORT bool operator==(const Time & lhs, const Time & rhs);
struct LUTAxis
{
......@@ -254,10 +259,15 @@ struct LUTAxis
std::vector<double> Values;
/** Export to JSON */
std::string ToJSON(bool multiline=false) const;
};
OTBMetadata_EXPORT bool operator==(const LUTAxis & lhs, const LUTAxis & rhs);
friend bool operator==(const LUTAxis & lhs, const LUTAxis & rhs)
{
return lhs.Size == rhs.Size
&& lhs.Origin == rhs.Origin
&& lhs.Spacing == rhs.Spacing
&& lhs.Values == rhs.Values;
}
};
template <unsigned int VDim> class LUT
{
......@@ -271,14 +281,14 @@ public:
std::string OTBMetadata_EXPORT ToString() const;
void OTBMetadata_EXPORT FromString(std::string);
};
template <unsigned int VDim>
bool operator==(const LUT<VDim> & lhs, const LUT<VDim> & rhs)
{
return std::equal(std::begin(lhs.Array), std::end(lhs.Array), std::begin(rhs.Array) )
&& lhs.Array == rhs.Array;
}
friend bool operator==(const LUT<VDim> & lhs, const LUT<VDim> & rhs)
{
return std::equal(std::begin(lhs.Array), std::end(lhs.Array), std::begin(rhs.Array) )
&& lhs.Array == rhs.Array;
}
};
template <unsigned int VDim>
......
......@@ -107,23 +107,5 @@ std::string RPCParam::ToJSON(bool multiline) const
return oss.str();
}
bool operator==(const RPCParam & lhs, const RPCParam & rhs)
{
return lhs.LineOffset == rhs.LineOffset
&& lhs.SampleOffset == rhs.SampleOffset
&& lhs.LatOffset == rhs.LatOffset
&& lhs.LonOffset == rhs.LonOffset
&& lhs.HeightOffset == rhs.HeightOffset
&& lhs.LineScale == rhs.LineScale
&& lhs.SampleScale == rhs.SampleScale
&& lhs.LatScale == rhs.LatScale
&& lhs.LonScale == rhs.LonScale
&& lhs.HeightScale == rhs.HeightScale
&& std::equal(lhs.LineNum, lhs.LineNum+20, rhs.LineNum )
&& std::equal(lhs.LineDen, lhs.LineDen+20, rhs.LineDen )
&& std::equal(lhs.SampleNum, lhs.SampleNum+20, rhs.SampleNum )
&& std::equal(lhs.SampleDen, lhs.SampleDen+20, rhs.SampleDen );
}
} // end namespace Projection
} // end namespace otb
......@@ -188,15 +188,6 @@ std::istream& operator>>(std::istream& is, Time& val)
#undef _OTB_ISTREAM_EXPECT
bool operator==(const Time & lhs, const Time & rhs)
{
tm tmLhs = lhs;
tm tmRhs = rhs;
return mktime(&tmLhs) + lhs.frac_sec == mktime(&tmRhs) + rhs.frac_sec;
}
std::string LUTAxis::ToJSON(bool multiline) const
{
std::ostringstream oss;
......@@ -404,15 +395,6 @@ MDGeomBmType MDGeomNames = bimapGenerator<MDGeom>(std::map<MDGeom, std::string>
{MDGeom::Adjustment, "Adjustment"}
});
OTBMetadata_EXPORT bool operator==(const LUTAxis & lhs, const LUTAxis & rhs)
{
return lhs.Size == rhs.Size
&& lhs.Origin == rhs.Origin
&& lhs.Spacing == rhs.Spacing
&& lhs.Values == rhs.Values;
}
template<>
std::string EnumToString(MDGeom value)
{
......
......@@ -1456,7 +1456,6 @@ int CompareMetadataDict( const MapType & baselineMap,
while (first1 != last1)
{
if (std::find(untestedKeys.begin(), untestedKeys.end(), first1->first) == untestedKeys.end())
//if (first1->first != untestedKeys)
{
if (first1->first != first2->first)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment