Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:12:14

0001 // @(#)root/base:
0002 // Authors: Rene Brun, Fons Rademakers   29/07/95
0003 
0004 /*************************************************************************
0005  * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers.               *
0006  * All rights reserved.                                                  *
0007  *                                                                       *
0008  * For the licensing terms see $ROOTSYS/LICENSE.                         *
0009  * For the list of contributors see $ROOTSYS/README/CREDITS.             *
0010  *************************************************************************/
0011 
0012 #ifndef ROOT_TMathBase
0013 #define ROOT_TMathBase
0014 
0015 
0016 //////////////////////////////////////////////////////////////////////////
0017 //                                                                      //
0018 // TMath Base functions                                                 //
0019 //                                                                      //
0020 // Define the functions Min, Max, Abs, Sign, Range for all types.       //
0021 // NB: These functions are unfortunately not available in a portable    //
0022 // way in std::.                                                        //
0023 //                                                                      //
0024 // More functions are defined in TMath.h. TMathBase.h is designed to be //
0025 // a stable file and used in place of TMath.h in the ROOT miniCore.     //
0026 //                                                                      //
0027 //////////////////////////////////////////////////////////////////////////
0028 
0029 #include "RtypesCore.h"
0030 
0031 #include <cstdlib>
0032 #include <cmath>
0033 #include <algorithm>
0034 
0035 namespace TMath {
0036 
0037    // Abs
0038    inline Short_t  Abs(Short_t d);
0039    inline Int_t    Abs(Int_t d);
0040    inline Long_t   Abs(Long_t d);
0041    inline Long64_t Abs(Long64_t d);
0042    inline Float_t  Abs(Float_t d);
0043    inline Double_t Abs(Double_t d);
0044    inline LongDouble_t Abs(LongDouble_t d);
0045 
0046    // Even/Odd
0047    inline Bool_t   Even(Long_t a);
0048    inline Bool_t   Odd(Long_t a);
0049 
0050    // SignBit
0051    template<typename Integer>
0052    inline Bool_t SignBit(Integer a);
0053    inline Bool_t SignBit(Float_t a);
0054    inline Bool_t SignBit(Double_t a);
0055    inline Bool_t SignBit(LongDouble_t a);
0056 
0057    // Sign
0058    template<typename T1, typename T2>
0059    inline T1 Sign( T1 a, T2 b);
0060    inline Float_t  Sign(Float_t a, Float_t b);
0061    inline Double_t Sign(Double_t a, Double_t b);
0062    inline LongDouble_t Sign(LongDouble_t a, LongDouble_t b);
0063 
0064    // Min, Max of two scalars
0065    inline Short_t   Min(Short_t a, Short_t b);
0066    inline UShort_t  Min(UShort_t a, UShort_t b);
0067    inline Int_t     Min(Int_t a, Int_t b);
0068    inline UInt_t    Min(UInt_t a, UInt_t b);
0069    inline Long_t    Min(Long_t a, Long_t b);
0070    inline ULong_t   Min(ULong_t a, ULong_t b);
0071    inline Long64_t  Min(Long64_t a, Long64_t b);
0072    inline ULong64_t Min(ULong64_t a, ULong64_t b);
0073    inline Float_t   Min(Float_t a, Float_t b);
0074    inline Double_t  Min(Double_t a, Double_t b);
0075 
0076    inline Short_t   Max(Short_t a, Short_t b);
0077    inline UShort_t  Max(UShort_t a, UShort_t b);
0078    inline Int_t     Max(Int_t a, Int_t b);
0079    inline UInt_t    Max(UInt_t a, UInt_t b);
0080    inline Long_t    Max(Long_t a, Long_t b);
0081    inline ULong_t   Max(ULong_t a, ULong_t b);
0082    inline Long64_t  Max(Long64_t a, Long64_t b);
0083    inline ULong64_t Max(ULong64_t a, ULong64_t b);
0084    inline Float_t   Max(Float_t a, Float_t b);
0085    inline Double_t  Max(Double_t a, Double_t b);
0086 
0087    // Range
0088    inline Short_t   Range(Short_t lb, Short_t ub, Short_t x);
0089    inline Int_t     Range(Int_t lb, Int_t ub, Int_t x);
0090    inline Long_t    Range(Long_t lb, Long_t ub, Long_t x);
0091    inline ULong_t   Range(ULong_t lb, ULong_t ub, ULong_t x);
0092    inline Double_t  Range(Double_t lb, Double_t ub, Double_t x);
0093 
0094    //NextPrime is used by the Core classes.
0095    Long_t   NextPrime(Long_t x);   // Least prime number greater than x
0096 
0097    // Binary search
0098    template <typename T> Long64_t BinarySearch(Long64_t n, const T  *array, T value);
0099    template <typename T> Long64_t BinarySearch(Long64_t n, const T **array, T value);
0100    template <typename Iterator, typename Element> Iterator BinarySearch(Iterator first, Iterator last, Element value);
0101 
0102    // Sorting
0103    template <typename Element, typename Index>
0104    void Sort(Index n, const Element* a, Index* index, Bool_t down=kTRUE);
0105    template <typename Iterator, typename IndexIterator>
0106    void SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down=kTRUE);
0107 }
0108 
0109 
0110 //---- Even/odd ----------------------------------------------------------------
0111 
0112 /// Returns `true` if `a` is even.
0113 inline Bool_t TMath::Even(Long_t a)
0114    { return ! (a & 1); }
0115 
0116 /// Returns `true` if `a` is odd.
0117 inline Bool_t TMath::Odd(Long_t a)
0118    { return (a & 1); }
0119 
0120 //---- Abs ---------------------------------------------------------------------
0121 
0122 /// Returns the absolute value of parameter `Short_t d`.
0123 inline Short_t TMath::Abs(Short_t d)
0124 { return (d >= 0) ? d : Short_t(-d);  }
0125 
0126 /// Returns the absolute value of parameter `Int_t d`.
0127 inline Int_t TMath::Abs(Int_t d)
0128 { return std::abs(d); }
0129 
0130 /// Returns the absolute value of parameter `Long_t d`.
0131 inline Long_t TMath::Abs(Long_t d)
0132 { return std::labs(d); }
0133 
0134 /// Returns the absolute value of parameter `Long64_t d`.
0135 inline Long64_t TMath::Abs(Long64_t d)
0136 { return std::llabs(d); }
0137 
0138 /// Returns the absolute value of parameter `Float_t d`.
0139 inline Float_t TMath::Abs(Float_t d)
0140 { return std::abs(d); }
0141 
0142 /// Returns the absolute value of parameter `Double_t d`.
0143 inline Double_t TMath::Abs(Double_t d)
0144 { return std::abs(d); }
0145 
0146 /// Returns the absolute value of parameter `LongDouble_t d`.
0147 inline LongDouble_t TMath::Abs(LongDouble_t d)
0148 { return std::abs(d); }
0149 
0150 
0151 //---- Sign Bit--------------------------------------------------------------------
0152 
0153 /// Returns whether the sign of `Integer a` is negative.
0154 template<typename Integer>
0155 inline Bool_t TMath::SignBit( Integer a)
0156    { return (a < 0); }
0157 
0158 /// Returns whether the sign of `Float_t a` is negative.
0159 inline Bool_t TMath::SignBit(Float_t a)
0160    { return std::signbit(a);  }
0161 
0162 /// Returns whether the sign of `Double_t a` is negative.
0163 inline Bool_t TMath::SignBit(Double_t a)
0164    { return std::signbit(a);  }
0165 
0166 /// Returns whether the sign of `LongDouble_t a` is negative.
0167 inline Bool_t TMath::SignBit(LongDouble_t a)
0168    { return std::signbit(a);  }
0169 
0170 
0171 //---- Sign --------------------------------------------------------------------
0172 
0173 /// Returns a value with the magnitude of `a` and the sign of `b`.
0174 template<typename T1, typename T2>
0175 inline T1 TMath::Sign( T1 a, T2 b)
0176    { return (SignBit(b)) ? - Abs(a) : Abs(a); }
0177 
0178 /// Returns a value with the magnitude of `a` and the sign of `b`.
0179 /// `a`and `b` are `Short_t`.
0180 inline Float_t TMath::Sign(Float_t a, Float_t b)
0181    { return std::copysign(a,b);  }
0182 
0183 /// Returns a value with the magnitude of `a` and the sign of `b`.
0184 /// `a`and `b` are `Double_t`.
0185 inline Double_t TMath::Sign(Double_t a, Double_t b)
0186    { return std::copysign(a,b);  }
0187 
0188 /// Returns a value with the magnitude of `a` and the sign of `b`.
0189 /// `a`and `b` are `LongDouble_t`.
0190 inline LongDouble_t TMath::Sign(LongDouble_t a, LongDouble_t b)
0191    { return std::copysign(a,b);  }
0192 
0193 
0194 //---- Min ---------------------------------------------------------------------
0195 
0196 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0197 /// `a`and `b` are `Short_t`.
0198 inline Short_t TMath::Min(Short_t a, Short_t b)
0199    { return a <= b ? a : b; }
0200 
0201 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0202 /// `a`and `b` are `UShort_t`.
0203 inline UShort_t TMath::Min(UShort_t a, UShort_t b)
0204    { return a <= b ? a : b; }
0205 
0206 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0207 /// `a`and `b` are `Int_t`.
0208 inline Int_t TMath::Min(Int_t a, Int_t b)
0209    { return a <= b ? a : b; }
0210 
0211 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0212 /// `a`and `b` are `Short_t`.
0213 inline UInt_t TMath::Min(UInt_t a, UInt_t b)
0214    { return a <= b ? a : b; }
0215 
0216 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0217 /// `a`and `b` are `Long_t`.
0218 inline Long_t TMath::Min(Long_t a, Long_t b)
0219    { return a <= b ? a : b; }
0220 
0221 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0222 /// `a`and `b` are `ULong_t`.
0223 inline ULong_t TMath::Min(ULong_t a, ULong_t b)
0224    { return a <= b ? a : b; }
0225 
0226 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0227 /// `a`and `b` are `Long64_t`.
0228 inline Long64_t TMath::Min(Long64_t a, Long64_t b)
0229    { return a <= b ? a : b; }
0230 
0231 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0232 /// `a`and `b` are `ULong64_t`.
0233 inline ULong64_t TMath::Min(ULong64_t a, ULong64_t b)
0234    { return a <= b ? a : b; }
0235 
0236 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0237 /// `a`and `b` are `Float_t`.
0238 inline Float_t TMath::Min(Float_t a, Float_t b)
0239    { return a <= b ? a : b; }
0240 
0241 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0242 /// `a`and `b` are `Double_t`.
0243 inline Double_t TMath::Min(Double_t a, Double_t b)
0244    { return a <= b ? a : b; }
0245 
0246 //---- Max ---------------------------------------------------------------------
0247 
0248 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0249 /// `a`and `b` are `Short_t`.
0250 inline Short_t TMath::Max(Short_t a, Short_t b)
0251    { return a >= b ? a : b; }
0252 
0253 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0254 /// `a`and `b` are `UShort_t`.
0255 inline UShort_t TMath::Max(UShort_t a, UShort_t b)
0256    { return a >= b ? a : b; }
0257 
0258 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0259 /// `a`and `b` are `Int_t`.
0260 inline Int_t TMath::Max(Int_t a, Int_t b)
0261    { return a >= b ? a : b; }
0262 
0263 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0264 /// `a`and `b` are `UInt_t`.
0265 inline UInt_t TMath::Max(UInt_t a, UInt_t b)
0266    { return a >= b ? a : b; }
0267 
0268 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0269 /// `a`and `b` are `Long_t`.
0270 inline Long_t TMath::Max(Long_t a, Long_t b)
0271    { return a >= b ? a : b; }
0272 
0273 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0274 /// `a`and `b` are `ULong_t`.
0275 inline ULong_t TMath::Max(ULong_t a, ULong_t b)
0276    { return a >= b ? a : b; }
0277 
0278 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0279 /// `a`and `b` are `Long64_t`.
0280 inline Long64_t TMath::Max(Long64_t a, Long64_t b)
0281    { return a >= b ? a : b; }
0282 
0283 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0284 /// `a`and `b` are `ULong64_t`.
0285 inline ULong64_t TMath::Max(ULong64_t a, ULong64_t b)
0286    { return a >= b ? a : b; }
0287 
0288 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0289 /// `a`and `b` are `Float_t`.
0290 inline Float_t TMath::Max(Float_t a, Float_t b)
0291    { return a >= b ? a : b; }
0292 
0293 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0294 /// `a`and `b` are `Double_t`.
0295 inline Double_t TMath::Max(Double_t a, Double_t b)
0296    { return a >= b ? a : b; }
0297 
0298 //---- Range -------------------------------------------------------------------
0299 
0300 /// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
0301 /// `lb`, `ub` and `x` are `Short_t`.
0302 inline Short_t TMath::Range(Short_t lb, Short_t ub, Short_t x)
0303    { return x < lb ? lb : (x > ub ? ub : x); }
0304 
0305 /// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
0306 /// `lb`, `ub` and `x` are `Int_t`.
0307 inline Int_t TMath::Range(Int_t lb, Int_t ub, Int_t x)
0308    { return x < lb ? lb : (x > ub ? ub : x); }
0309 
0310 /// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
0311 /// `lb`, `ub` and `x` are `Long_t`.
0312 inline Long_t TMath::Range(Long_t lb, Long_t ub, Long_t x)
0313    { return x < lb ? lb : (x > ub ? ub : x); }
0314 
0315 /// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
0316 /// `lb`, `ub` and `x` are `ULong_t`.
0317 inline ULong_t TMath::Range(ULong_t lb, ULong_t ub, ULong_t x)
0318    { return x < lb ? lb : (x > ub ? ub : x); }
0319 
0320 /// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
0321 /// `lb`, `ub` and `x` are `Double_t`.
0322 inline Double_t TMath::Range(Double_t lb, Double_t ub, Double_t x)
0323    { return x < lb ? lb : (x > ub ? ub : x); }
0324 
0325 /// Binary search in an array defined by its iterators.
0326 ///
0327 /// The values in the iterators range are supposed to be sorted
0328 /// prior to this call.  If match is found, function returns
0329 /// position of element.  If no match found, function gives nearest
0330 /// element smaller than value.
0331 template <typename Iterator, typename Element>
0332 Iterator TMath::BinarySearch(Iterator first, Iterator last, Element value)
0333 {
0334    Iterator pind;
0335    pind = std::lower_bound(first, last, value);
0336    if ( (pind != last) && (*pind == value) )
0337       return pind;
0338    else
0339       return ( pind - 1);
0340 }
0341 
0342 /// Binary search in an array of n values to locate value.
0343 ///
0344 /// Array is supposed  to be sorted prior to this call.
0345 /// If match is found, function returns position of element.
0346 /// If no match found, function gives nearest element smaller than value.
0347 template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T  *array, T value)
0348 {
0349    const T* pind;
0350    pind = std::lower_bound(array, array + n, value);
0351    if ( (pind != array + n) && (*pind == value) )
0352       return (pind - array);
0353    else
0354       return ( pind - array - 1);
0355 }
0356 
0357 /// Binary search in an array of n values to locate value.
0358 ///
0359 /// Array is supposed  to be sorted prior to this call.
0360 /// If match is found, function returns position of element.
0361 /// If no match found, function gives nearest element smaller than value.
0362 template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T **array, T value)
0363 {
0364    const T* pind;
0365    pind = std::lower_bound(*array, *array + n, value);
0366    if ( (pind != *array + n) && (*pind == value) )
0367       return (pind - *array);
0368    else
0369       return ( pind - *array - 1);
0370 }
0371 
0372 template<typename T>
0373 struct CompareDesc {
0374 
0375    CompareDesc(T d) : fData(d) {}
0376 
0377    template<typename Index>
0378    bool operator()(Index i1, Index i2) {
0379       return *(fData + i1) > *(fData + i2);
0380    }
0381 
0382    T fData;
0383 };
0384 
0385 template<typename T>
0386 struct CompareAsc {
0387 
0388    CompareAsc(T d) : fData(d) {}
0389 
0390    template<typename Index>
0391    bool operator()(Index i1, Index i2) {
0392       return *(fData + i1) < *(fData + i2);
0393    }
0394 
0395    T fData;
0396 };
0397 
0398 /// Sort the n1 elements of the Short_t array defined by its
0399 /// iterators.  In output the array index contains the indices of
0400 /// the sorted array.  If down is false sort in increasing order
0401 /// (default is decreasing order).
0402 ///
0403 /// NOTE that the array index must be created with a length bigger
0404 /// or equal than the main array before calling this function.
0405 template <typename Iterator, typename IndexIterator>
0406 void TMath::SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down)
0407 {
0408    int i = 0;
0409 
0410    IndexIterator cindex = index;
0411    for ( Iterator cfirst = first; cfirst != last; ++cfirst )
0412    {
0413       *cindex = i++;
0414       ++cindex;
0415    }
0416 
0417    if ( down )
0418       std::sort(index, cindex, CompareDesc<Iterator>(first) );
0419    else
0420       std::sort(index, cindex, CompareAsc<Iterator>(first) );
0421 }
0422 
0423 /// Sort the n elements of the  array a of generic templated type Element.
0424 /// In output the array index of type Index contains the indices of the sorted array.
0425 /// If down is false sort in increasing order (default is decreasing order).
0426 ///
0427 /// NOTE that the array index must be created with a length >= n
0428 /// before calling this function.
0429 /// NOTE also that the size type for n must be the same type used for the index array
0430 /// (templated type Index)
0431 template <typename Element, typename Index> void TMath::Sort(Index n, const Element* a, Index* index, Bool_t down)
0432 {
0433    for(Index i = 0; i < n; i++) { index[i] = i; }
0434    if ( down )
0435       std::sort(index, index + n, CompareDesc<const Element*>(a) );
0436    else
0437       std::sort(index, index + n, CompareAsc<const Element*>(a) );
0438 }
0439 
0440 #endif