Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:21:27

0001 #ifndef NPSTAT_ABSDIFFERENCE_HH_
0002 #define NPSTAT_ABSDIFFERENCE_HH_
0003 
0004 /*!
0005 // \file absDifference.h
0006 //
0007 // \brief Calculate absolute value of a difference between two numbers
0008 //        for an extended set of types
0009 //
0010 // Author: I. Volobouev
0011 //
0012 // October 2009
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     // Signed type
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     // Unsigned type
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   }  // namespace Private
0067 
0068   /**
0069     // Absolute value of the difference between two numbers.
0070     // Works for all standard numeric types, including unsigned and complex.
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     // Absolute value of a number.
0079     // Works for all standard numeric types, including unsigned and complex.
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 }  // namespace npstat
0086 
0087 #endif  // NPSTAT_ABSDIFFERENCE_HH_