File indexing completed on 2024-04-06 12:19:17
0001 #ifndef NPSTAT_ABSDIFFERENCE_HH_
0002 #define NPSTAT_ABSDIFFERENCE_HH_
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <cmath>
0016 #include <complex>
0017
0018 #include "Alignment/Geners/interface/IOIsUnsigned.hh"
0019
0020 namespace npstat {
0021 namespace Private {
0022 template <typename T>
0023 struct AbsReturnType {
0024 typedef T type;
0025 };
0026
0027 template <typename T>
0028 struct AbsReturnType<std::complex<T> > {
0029 typedef T type;
0030 };
0031
0032 template <typename T>
0033 struct AbsReturnType<const std::complex<T> > {
0034 typedef T type;
0035 };
0036
0037 template <typename T>
0038 struct AbsReturnType<volatile std::complex<T> > {
0039 typedef T type;
0040 };
0041
0042 template <typename T>
0043 struct AbsReturnType<const volatile std::complex<T> > {
0044 typedef T type;
0045 };
0046
0047
0048 template <typename T, int Unsigned = 0>
0049 struct AbsHelper {
0050 typedef typename Private::AbsReturnType<T>::type return_type;
0051
0052 inline static return_type delta(const T& v1, const T& v2) { return std::abs(v1 - v2); }
0053
0054 inline static return_type value(const T& v1) { return std::abs(v1); }
0055 };
0056
0057
0058 template <typename T>
0059 struct AbsHelper<T, 1> {
0060 typedef typename Private::AbsReturnType<T>::type return_type;
0061
0062 inline static return_type delta(const T& v1, const T& v2) { return v1 > v2 ? v1 - v2 : v2 - v1; }
0063
0064 inline static return_type value(const T& v1) { return v1; }
0065 };
0066 }
0067
0068
0069
0070
0071
0072 template <typename T>
0073 inline typename Private::AbsReturnType<T>::type absDifference(const T& v1, const T& v2) {
0074 return Private::AbsHelper<T, gs::IOIsUnsigned<T>::value>::delta(v1, v2);
0075 }
0076
0077
0078
0079
0080
0081 template <typename T>
0082 inline typename Private::AbsReturnType<T>::type absValue(const T& v1) {
0083 return Private::AbsHelper<T, gs::IOIsUnsigned<T>::value>::value(v1);
0084 }
0085 }
0086
0087 #endif