Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-28 09:28:35

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 
0101    // Sorting
0102    template <typename Element, typename Index>
0103    void Sort(Index n, const Element* a, Index* index, Bool_t down=kTRUE);
0104    template <typename Iterator, typename IndexIterator>
0105    void SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down=kTRUE);
0106 }
0107 
0108 
0109 //---- Even/odd ----------------------------------------------------------------
0110 
0111 /// Returns `true` if `a` is even.
0112 inline Bool_t TMath::Even(Long_t a)
0113    { return ! (a & 1); }
0114 
0115 /// Returns `true` if `a` is odd.
0116 inline Bool_t TMath::Odd(Long_t a)
0117    { return (a & 1); }
0118 
0119 //---- Abs ---------------------------------------------------------------------
0120 
0121 /// Returns the absolute value of parameter `Short_t d`.
0122 inline Short_t TMath::Abs(Short_t d)
0123 { return (d >= 0) ? d : Short_t(-d);  }
0124 
0125 /// Returns the absolute value of parameter `Int_t d`.
0126 inline Int_t TMath::Abs(Int_t d)
0127 { return std::abs(d); }
0128 
0129 /// Returns the absolute value of parameter `Long_t d`.
0130 inline Long_t TMath::Abs(Long_t d)
0131 { return std::labs(d); }
0132 
0133 /// Returns the absolute value of parameter `Long64_t d`.
0134 inline Long64_t TMath::Abs(Long64_t d)
0135 { return std::llabs(d); }
0136 
0137 /// Returns the absolute value of parameter `Float_t d`.
0138 inline Float_t TMath::Abs(Float_t d)
0139 { return std::abs(d); }
0140 
0141 /// Returns the absolute value of parameter `Double_t d`.
0142 inline Double_t TMath::Abs(Double_t d)
0143 { return std::abs(d); }
0144 
0145 /// Returns the absolute value of parameter `LongDouble_t d`.
0146 inline LongDouble_t TMath::Abs(LongDouble_t d)
0147 { return std::abs(d); }
0148 
0149 
0150 //---- Sign Bit--------------------------------------------------------------------
0151 
0152 /// Returns whether the sign of `Integer a` is negative.
0153 template<typename Integer>
0154 inline Bool_t TMath::SignBit( Integer a)
0155    { return (a < 0); }
0156 
0157 /// Returns whether the sign of `Float_t a` is negative.
0158 inline Bool_t TMath::SignBit(Float_t a)
0159    { return std::signbit(a);  }
0160 
0161 /// Returns whether the sign of `Double_t a` is negative.
0162 inline Bool_t TMath::SignBit(Double_t a)
0163    { return std::signbit(a);  }
0164 
0165 /// Returns whether the sign of `LongDouble_t a` is negative.
0166 inline Bool_t TMath::SignBit(LongDouble_t a)
0167    { return std::signbit(a);  }
0168 
0169 
0170 //---- Sign --------------------------------------------------------------------
0171 
0172 /// Returns a value with the magnitude of `a` and the sign of `b`.
0173 template<typename T1, typename T2>
0174 inline T1 TMath::Sign( T1 a, T2 b)
0175    { return (SignBit(b)) ? - Abs(a) : Abs(a); }
0176 
0177 /// Returns a value with the magnitude of `a` and the sign of `b`.
0178 /// `a`and `b` are `Short_t`.
0179 inline Float_t TMath::Sign(Float_t a, Float_t b)
0180    { return std::copysign(a,b);  }
0181 
0182 /// Returns a value with the magnitude of `a` and the sign of `b`.
0183 /// `a`and `b` are `Double_t`.
0184 inline Double_t TMath::Sign(Double_t a, Double_t b)
0185    { return std::copysign(a,b);  }
0186 
0187 /// Returns a value with the magnitude of `a` and the sign of `b`.
0188 /// `a`and `b` are `LongDouble_t`.
0189 inline LongDouble_t TMath::Sign(LongDouble_t a, LongDouble_t b)
0190    { return std::copysign(a,b);  }
0191 
0192 
0193 //---- Min ---------------------------------------------------------------------
0194 
0195 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0196 /// `a`and `b` are `Short_t`.
0197 inline Short_t TMath::Min(Short_t a, Short_t b)
0198    { return a <= b ? a : b; }
0199 
0200 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0201 /// `a`and `b` are `UShort_t`.
0202 inline UShort_t TMath::Min(UShort_t a, UShort_t b)
0203    { return a <= b ? a : b; }
0204 
0205 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0206 /// `a`and `b` are `Int_t`.
0207 inline Int_t TMath::Min(Int_t a, Int_t b)
0208    { return a <= b ? a : b; }
0209 
0210 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0211 /// `a`and `b` are `Short_t`.
0212 inline UInt_t TMath::Min(UInt_t a, UInt_t b)
0213    { return a <= b ? a : b; }
0214 
0215 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0216 /// `a`and `b` are `Long_t`.
0217 inline Long_t TMath::Min(Long_t a, Long_t b)
0218    { return a <= b ? a : b; }
0219 
0220 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0221 /// `a`and `b` are `ULong_t`.
0222 inline ULong_t TMath::Min(ULong_t a, ULong_t b)
0223    { return a <= b ? a : b; }
0224 
0225 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0226 /// `a`and `b` are `Long64_t`.
0227 inline Long64_t TMath::Min(Long64_t a, Long64_t b)
0228    { return a <= b ? a : b; }
0229 
0230 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0231 /// `a`and `b` are `ULong64_t`.
0232 inline ULong64_t TMath::Min(ULong64_t a, ULong64_t b)
0233    { return a <= b ? a : b; }
0234 
0235 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0236 /// `a`and `b` are `Float_t`.
0237 inline Float_t TMath::Min(Float_t a, Float_t b)
0238    { return a <= b ? a : b; }
0239 
0240 /// Returns the smallest of `a` and `b`. If both are equivalent, `a` is returned.
0241 /// `a`and `b` are `Double_t`.
0242 inline Double_t TMath::Min(Double_t a, Double_t b)
0243    { return a <= b ? a : b; }
0244 
0245 //---- Max ---------------------------------------------------------------------
0246 
0247 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0248 /// `a`and `b` are `Short_t`.
0249 inline Short_t TMath::Max(Short_t a, Short_t b)
0250    { return a >= b ? a : b; }
0251 
0252 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0253 /// `a`and `b` are `UShort_t`.
0254 inline UShort_t TMath::Max(UShort_t a, UShort_t b)
0255    { return a >= b ? a : b; }
0256 
0257 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0258 /// `a`and `b` are `Int_t`.
0259 inline Int_t TMath::Max(Int_t a, Int_t b)
0260    { return a >= b ? a : b; }
0261 
0262 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0263 /// `a`and `b` are `UInt_t`.
0264 inline UInt_t TMath::Max(UInt_t a, UInt_t b)
0265    { return a >= b ? a : b; }
0266 
0267 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0268 /// `a`and `b` are `Long_t`.
0269 inline Long_t TMath::Max(Long_t a, Long_t b)
0270    { return a >= b ? a : b; }
0271 
0272 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0273 /// `a`and `b` are `ULong_t`.
0274 inline ULong_t TMath::Max(ULong_t a, ULong_t b)
0275    { return a >= b ? a : b; }
0276 
0277 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0278 /// `a`and `b` are `Long64_t`.
0279 inline Long64_t TMath::Max(Long64_t a, Long64_t b)
0280    { return a >= b ? a : b; }
0281 
0282 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0283 /// `a`and `b` are `ULong64_t`.
0284 inline ULong64_t TMath::Max(ULong64_t a, ULong64_t b)
0285    { return a >= b ? a : b; }
0286 
0287 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0288 /// `a`and `b` are `Float_t`.
0289 inline Float_t TMath::Max(Float_t a, Float_t b)
0290    { return a >= b ? a : b; }
0291 
0292 /// Returns the largest of `a` and `b`. If both are equivalent, `a` is returned.
0293 /// `a`and `b` are `Double_t`.
0294 inline Double_t TMath::Max(Double_t a, Double_t b)
0295    { return a >= b ? a : b; }
0296 
0297 //---- Range -------------------------------------------------------------------
0298 
0299 /// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
0300 /// `lb`, `ub` and `x` are `Short_t`.
0301 inline Short_t TMath::Range(Short_t lb, Short_t ub, Short_t x)
0302    { return x < lb ? lb : (x > ub ? ub : x); }
0303 
0304 /// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
0305 /// `lb`, `ub` and `x` are `Int_t`.
0306 inline Int_t TMath::Range(Int_t lb, Int_t ub, Int_t x)
0307    { return x < lb ? lb : (x > ub ? ub : x); }
0308 
0309 /// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
0310 /// `lb`, `ub` and `x` are `Long_t`.
0311 inline Long_t TMath::Range(Long_t lb, Long_t ub, Long_t x)
0312    { return x < lb ? lb : (x > ub ? ub : x); }
0313 
0314 /// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
0315 /// `lb`, `ub` and `x` are `ULong_t`.
0316 inline ULong_t TMath::Range(ULong_t lb, ULong_t ub, ULong_t x)
0317    { return x < lb ? lb : (x > ub ? ub : x); }
0318 
0319 /// Returns `x` if `lb < x < up`, `lb` if `x < lb` and `ub` if `x > ub`.
0320 /// `lb`, `ub` and `x` are `Double_t`.
0321 inline Double_t TMath::Range(Double_t lb, Double_t ub, Double_t x)
0322    { return x < lb ? lb : (x > ub ? ub : x); }
0323 
0324 /// Binary search in an array of n values to locate value.
0325 ///
0326 /// Array is supposed  to be sorted prior to this call.
0327 /// If match is found, function returns position of element.
0328 /// If no match found, function gives nearest element smaller than value.
0329 template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T  *array, T value)
0330 {
0331    const T* pind;
0332    pind = std::lower_bound(array, array + n, value);
0333    if ( (pind != array + n) && (*pind == value) )
0334       return (pind - array);
0335    else
0336       return ( pind - array - 1);
0337 }
0338 
0339 /// Binary search in an array of n values to locate value.
0340 ///
0341 /// Array is supposed  to be sorted prior to this call.
0342 /// If match is found, function returns position of element.
0343 /// If no match found, function gives nearest element smaller than value.
0344 template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T **array, T value)
0345 {
0346    const T* pind;
0347    pind = std::lower_bound(*array, *array + n, value);
0348    if ( (pind != *array + n) && (*pind == value) )
0349       return (pind - *array);
0350    else
0351       return ( pind - *array - 1);
0352 }
0353 
0354 template<typename T>
0355 struct CompareDesc {
0356 
0357    CompareDesc(T d) : fData(d) {}
0358 
0359    template<typename Index>
0360    bool operator()(Index i1, Index i2) {
0361       return *(fData + i1) > *(fData + i2);
0362    }
0363 
0364    T fData;
0365 };
0366 
0367 template<typename T>
0368 struct CompareAsc {
0369 
0370    CompareAsc(T d) : fData(d) {}
0371 
0372    template<typename Index>
0373    bool operator()(Index i1, Index i2) {
0374       return *(fData + i1) < *(fData + i2);
0375    }
0376 
0377    T fData;
0378 };
0379 
0380 /// Sort the n1 elements of the Short_t array defined by its
0381 /// iterators.  In output the array index contains the indices of
0382 /// the sorted array.  If down is false sort in increasing order
0383 /// (default is decreasing order).
0384 ///
0385 /// NOTE that the array index must be created with a length bigger
0386 /// or equal than the main array before calling this function.
0387 template <typename Iterator, typename IndexIterator>
0388 void TMath::SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down)
0389 {
0390    int i = 0;
0391 
0392    IndexIterator cindex = index;
0393    for ( Iterator cfirst = first; cfirst != last; ++cfirst )
0394    {
0395       *cindex = i++;
0396       ++cindex;
0397    }
0398 
0399    if ( down )
0400       std::sort(index, cindex, CompareDesc<Iterator>(first) );
0401    else
0402       std::sort(index, cindex, CompareAsc<Iterator>(first) );
0403 }
0404 
0405 /// Sort the n elements of the  array a of generic templated type Element.
0406 /// In output the array index of type Index contains the indices of the sorted array.
0407 /// If down is false sort in increasing order (default is decreasing order).
0408 ///
0409 /// NOTE that the array index must be created with a length >= n
0410 /// before calling this function.
0411 /// NOTE also that the size type for n must be the same type used for the index array
0412 /// (templated type Index)
0413 template <typename Element, typename Index> void TMath::Sort(Index n, const Element* a, Index* index, Bool_t down)
0414 {
0415    for(Index i = 0; i < n; i++) { index[i] = i; }
0416    if ( down )
0417       std::sort(index, index + n, CompareDesc<const Element*>(a) );
0418    else
0419       std::sort(index, index + n, CompareAsc<const Element*>(a) );
0420 }
0421 
0422 #endif