CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/JetMETCorrections/InterpolationTables/interface/absDifference.h

Go to the documentation of this file.
00001 #ifndef NPSTAT_ABSDIFFERENCE_HH_
00002 #define NPSTAT_ABSDIFFERENCE_HH_
00003 
00015 #include <cmath>
00016 #include <complex>
00017 
00018 #include "Alignment/Geners/interface/IOIsUnsigned.hh"
00019 
00020 namespace npstat {
00021     namespace Private {
00022         template <typename T>
00023         struct AbsReturnType
00024         {
00025             typedef T type;
00026         };
00027 
00028         template <typename T>
00029         struct AbsReturnType<std::complex<T> >
00030         {
00031             typedef T type;
00032         };
00033 
00034         template <typename T>
00035         struct AbsReturnType<const std::complex<T> >
00036         {
00037             typedef T type;
00038         };
00039 
00040         template <typename T>
00041         struct AbsReturnType<volatile std::complex<T> >
00042         {
00043             typedef T type;
00044         };
00045 
00046         template <typename T>
00047         struct AbsReturnType<const volatile std::complex<T> >
00048         {
00049             typedef T type;
00050         };
00051 
00052         // Signed type
00053         template <typename T, int Unsigned=0>
00054         struct AbsHelper
00055         {
00056             typedef typename Private::AbsReturnType<T>::type return_type;
00057 
00058             inline static return_type delta(const T& v1, const T& v2)
00059                 {return std::abs(v1 - v2);}
00060 
00061             inline static return_type value(const T& v1)
00062                 {return std::abs(v1);}
00063         };
00064 
00065         // Unsigned type
00066         template <typename T>
00067         struct AbsHelper<T, 1>
00068         {
00069             typedef typename Private::AbsReturnType<T>::type return_type;
00070 
00071             inline static return_type delta(const T& v1, const T& v2)
00072                 {return v1 > v2 ? v1 - v2 : v2 - v1;}
00073 
00074             inline static return_type value(const T& v1)
00075                 {return v1;}
00076         };
00077     }
00078 
00083     template<typename T>
00084     inline typename Private::AbsReturnType<T>::type
00085     absDifference(const T& v1, const T& v2)
00086     {
00087         return Private::AbsHelper<T,gs::IOIsUnsigned<T>::value>::delta(v1, v2);
00088     }
00089 
00094     template<typename T>
00095     inline typename Private::AbsReturnType<T>::type
00096     absValue(const T& v1)
00097     {
00098         return Private::AbsHelper<T,gs::IOIsUnsigned<T>::value>::value(v1);
00099     }
00100 }
00101 
00102 #endif // NPSTAT_ABSDIFFERENCE_HH_
00103