File indexing completed on 2025-01-18 10:12:14
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef ROOT_TMathBase
0013 #define ROOT_TMathBase
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029 #include "RtypesCore.h"
0030
0031 #include <cstdlib>
0032 #include <cmath>
0033 #include <algorithm>
0034
0035 namespace TMath {
0036
0037
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
0047 inline Bool_t Even(Long_t a);
0048 inline Bool_t Odd(Long_t a);
0049
0050
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
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
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
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
0095 Long_t NextPrime(Long_t x);
0096
0097
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
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
0111
0112
0113 inline Bool_t TMath::Even(Long_t a)
0114 { return ! (a & 1); }
0115
0116
0117 inline Bool_t TMath::Odd(Long_t a)
0118 { return (a & 1); }
0119
0120
0121
0122
0123 inline Short_t TMath::Abs(Short_t d)
0124 { return (d >= 0) ? d : Short_t(-d); }
0125
0126
0127 inline Int_t TMath::Abs(Int_t d)
0128 { return std::abs(d); }
0129
0130
0131 inline Long_t TMath::Abs(Long_t d)
0132 { return std::labs(d); }
0133
0134
0135 inline Long64_t TMath::Abs(Long64_t d)
0136 { return std::llabs(d); }
0137
0138
0139 inline Float_t TMath::Abs(Float_t d)
0140 { return std::abs(d); }
0141
0142
0143 inline Double_t TMath::Abs(Double_t d)
0144 { return std::abs(d); }
0145
0146
0147 inline LongDouble_t TMath::Abs(LongDouble_t d)
0148 { return std::abs(d); }
0149
0150
0151
0152
0153
0154 template<typename Integer>
0155 inline Bool_t TMath::SignBit( Integer a)
0156 { return (a < 0); }
0157
0158
0159 inline Bool_t TMath::SignBit(Float_t a)
0160 { return std::signbit(a); }
0161
0162
0163 inline Bool_t TMath::SignBit(Double_t a)
0164 { return std::signbit(a); }
0165
0166
0167 inline Bool_t TMath::SignBit(LongDouble_t a)
0168 { return std::signbit(a); }
0169
0170
0171
0172
0173
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
0179
0180 inline Float_t TMath::Sign(Float_t a, Float_t b)
0181 { return std::copysign(a,b); }
0182
0183
0184
0185 inline Double_t TMath::Sign(Double_t a, Double_t b)
0186 { return std::copysign(a,b); }
0187
0188
0189
0190 inline LongDouble_t TMath::Sign(LongDouble_t a, LongDouble_t b)
0191 { return std::copysign(a,b); }
0192
0193
0194
0195
0196
0197
0198 inline Short_t TMath::Min(Short_t a, Short_t b)
0199 { return a <= b ? a : b; }
0200
0201
0202
0203 inline UShort_t TMath::Min(UShort_t a, UShort_t b)
0204 { return a <= b ? a : b; }
0205
0206
0207
0208 inline Int_t TMath::Min(Int_t a, Int_t b)
0209 { return a <= b ? a : b; }
0210
0211
0212
0213 inline UInt_t TMath::Min(UInt_t a, UInt_t b)
0214 { return a <= b ? a : b; }
0215
0216
0217
0218 inline Long_t TMath::Min(Long_t a, Long_t b)
0219 { return a <= b ? a : b; }
0220
0221
0222
0223 inline ULong_t TMath::Min(ULong_t a, ULong_t b)
0224 { return a <= b ? a : b; }
0225
0226
0227
0228 inline Long64_t TMath::Min(Long64_t a, Long64_t b)
0229 { return a <= b ? a : b; }
0230
0231
0232
0233 inline ULong64_t TMath::Min(ULong64_t a, ULong64_t b)
0234 { return a <= b ? a : b; }
0235
0236
0237
0238 inline Float_t TMath::Min(Float_t a, Float_t b)
0239 { return a <= b ? a : b; }
0240
0241
0242
0243 inline Double_t TMath::Min(Double_t a, Double_t b)
0244 { return a <= b ? a : b; }
0245
0246
0247
0248
0249
0250 inline Short_t TMath::Max(Short_t a, Short_t b)
0251 { return a >= b ? a : b; }
0252
0253
0254
0255 inline UShort_t TMath::Max(UShort_t a, UShort_t b)
0256 { return a >= b ? a : b; }
0257
0258
0259
0260 inline Int_t TMath::Max(Int_t a, Int_t b)
0261 { return a >= b ? a : b; }
0262
0263
0264
0265 inline UInt_t TMath::Max(UInt_t a, UInt_t b)
0266 { return a >= b ? a : b; }
0267
0268
0269
0270 inline Long_t TMath::Max(Long_t a, Long_t b)
0271 { return a >= b ? a : b; }
0272
0273
0274
0275 inline ULong_t TMath::Max(ULong_t a, ULong_t b)
0276 { return a >= b ? a : b; }
0277
0278
0279
0280 inline Long64_t TMath::Max(Long64_t a, Long64_t b)
0281 { return a >= b ? a : b; }
0282
0283
0284
0285 inline ULong64_t TMath::Max(ULong64_t a, ULong64_t b)
0286 { return a >= b ? a : b; }
0287
0288
0289
0290 inline Float_t TMath::Max(Float_t a, Float_t b)
0291 { return a >= b ? a : b; }
0292
0293
0294
0295 inline Double_t TMath::Max(Double_t a, Double_t b)
0296 { return a >= b ? a : b; }
0297
0298
0299
0300
0301
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
0306
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
0311
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
0316
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
0321
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
0326
0327
0328
0329
0330
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
0343
0344
0345
0346
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
0358
0359
0360
0361
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
0399
0400
0401
0402
0403
0404
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
0424
0425
0426
0427
0428
0429
0430
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