Logo ROOT  
Reference Guide
TMathBase.h
Go to the documentation of this file.
1// @(#)root/base:
2// Authors: Rene Brun, Fons Rademakers 29/07/95
3
4/*************************************************************************
5 * Copyright (C) 1995-2004, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#ifndef ROOT_TMathBase
13#define ROOT_TMathBase
14
15
16//////////////////////////////////////////////////////////////////////////
17// //
18// TMath Base functions //
19// //
20// Define the functions Min, Max, Abs, Sign, Range for all types. //
21// NB: These functions are unfortunately not available in a portable //
22// way in std::. //
23// //
24// More functions are defined in TMath.h. TMathBase.h is designed to be //
25// a stable file and used in place of TMath.h in the ROOT miniCore. //
26// //
27//////////////////////////////////////////////////////////////////////////
28
29#include "Rtypes.h"
30
31#include <cstdlib>
32#include <cmath>
33#include <algorithm>
34
35namespace TMath {
36
37 // Abs
38 inline Short_t Abs(Short_t d);
39 inline Int_t Abs(Int_t d);
40 inline Long_t Abs(Long_t d);
41 inline Long64_t Abs(Long64_t d);
42 inline Float_t Abs(Float_t d);
43 inline Double_t Abs(Double_t d);
45
46 // Even/Odd
47 inline Bool_t Even(Long_t a);
48 inline Bool_t Odd(Long_t a);
49
50 // SignBit
51 template<typename Integer>
52 inline Bool_t SignBit(Integer a);
53 inline Bool_t SignBit(Float_t a);
54 inline Bool_t SignBit(Double_t a);
56
57 // Sign
58 template<typename T1, typename T2>
59 inline T1 Sign( T1 a, T2 b);
60 inline Float_t Sign(Float_t a, Float_t b);
63
64 // Min, Max of two scalars
65 inline Short_t Min(Short_t a, Short_t b);
67 inline Int_t Min(Int_t a, Int_t b);
68 inline UInt_t Min(UInt_t a, UInt_t b);
69 inline Long_t Min(Long_t a, Long_t b);
70 inline ULong_t Min(ULong_t a, ULong_t b);
73 inline Float_t Min(Float_t a, Float_t b);
75
76 inline Short_t Max(Short_t a, Short_t b);
78 inline Int_t Max(Int_t a, Int_t b);
79 inline UInt_t Max(UInt_t a, UInt_t b);
80 inline Long_t Max(Long_t a, Long_t b);
81 inline ULong_t Max(ULong_t a, ULong_t b);
84 inline Float_t Max(Float_t a, Float_t b);
86
87 // Range
88 inline Short_t Range(Short_t lb, Short_t ub, Short_t x);
89 inline Int_t Range(Int_t lb, Int_t ub, Int_t x);
90 inline Long_t Range(Long_t lb, Long_t ub, Long_t x);
91 inline ULong_t Range(ULong_t lb, ULong_t ub, ULong_t x);
92 inline Double_t Range(Double_t lb, Double_t ub, Double_t x);
93
94 //NextPrime is used by the Core classes.
95 Long_t NextPrime(Long_t x); // Least prime number greater than x
96
97 // Binary search
98 template <typename T> Long64_t BinarySearch(Long64_t n, const T *array, T value);
99 template <typename T> Long64_t BinarySearch(Long64_t n, const T **array, T value);
100 template <typename Iterator, typename Element> Iterator BinarySearch(Iterator first, Iterator last, Element value);
101
102 // Sorting
103 template <typename Element, typename Index>
104 void Sort(Index n, const Element* a, Index* index, Bool_t down=kTRUE);
105 template <typename Iterator, typename IndexIterator>
106 void SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down=kTRUE);
107}
108
109
110//---- Even/odd ----------------------------------------------------------------
111
113 { return ! (a & 1); }
114
116 { return (a & 1); }
117
118//---- Abs ---------------------------------------------------------------------
119
121{ return (d >= 0) ? d : Short_t(-d); }
122
124{ return std::abs(d); }
125
127{ return std::labs(d); }
128
130#if __cplusplus >= 201103
131{ return std::llabs(d); }
132#else
133{ return (d >= 0) ? d : -d; }
134#endif
135
137{ return std::abs(d); }
138
140{ return std::abs(d); }
141
143{ return std::abs(d); }
144
145
146//---- Sign Bit--------------------------------------------------------------------
147
148template<typename Integer>
149inline Bool_t TMath::SignBit( Integer a)
150 { return (a < 0); }
151
153 { return std::signbit(a); }
154
156 { return std::signbit(a); }
157
159 { return std::signbit(a); }
160
161
162//---- Sign --------------------------------------------------------------------
163
164template<typename T1, typename T2>
166 { return (SignBit(b)) ? - Abs(a) : Abs(a); }
167
169 { return std::copysign(a,b); }
170
172 { return std::copysign(a,b); }
173
175 { return std::copysign(a,b); }
176
177
178//---- Min ---------------------------------------------------------------------
179
181 { return a <= b ? a : b; }
182
184 { return a <= b ? a : b; }
185
187 { return a <= b ? a : b; }
188
190 { return a <= b ? a : b; }
191
193 { return a <= b ? a : b; }
194
196 { return a <= b ? a : b; }
197
199 { return a <= b ? a : b; }
200
202 { return a <= b ? a : b; }
203
205 { return a <= b ? a : b; }
206
208 { return a <= b ? a : b; }
209
210//---- Max ---------------------------------------------------------------------
211
213 { return a >= b ? a : b; }
214
216 { return a >= b ? a : b; }
217
219 { return a >= b ? a : b; }
220
222 { return a >= b ? a : b; }
223
225 { return a >= b ? a : b; }
226
228 { return a >= b ? a : b; }
229
231 { return a >= b ? a : b; }
232
234 { return a >= b ? a : b; }
235
237 { return a >= b ? a : b; }
238
240 { return a >= b ? a : b; }
241
242//---- Range -------------------------------------------------------------------
243
245 { return x < lb ? lb : (x > ub ? ub : x); }
246
248 { return x < lb ? lb : (x > ub ? ub : x); }
249
251 { return x < lb ? lb : (x > ub ? ub : x); }
252
254 { return x < lb ? lb : (x > ub ? ub : x); }
255
257 { return x < lb ? lb : (x > ub ? ub : x); }
258
259template <typename Iterator, typename Element>
260Iterator TMath::BinarySearch(Iterator first, Iterator last, Element value)
261{
262 // Binary search in an array defined by its iterators.
263 //
264 // The values in the iterators range are supposed to be sorted
265 // prior to this call. If match is found, function returns
266 // position of element. If no match found, function gives nearest
267 // element smaller than value.
268
269 Iterator pind;
270 pind = std::lower_bound(first, last, value);
271 if ( (pind != last) && (*pind == value) )
272 return pind;
273 else
274 return ( pind - 1);
275}
276
277
278template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T *array, T value)
279{
280 // Binary search in an array of n values to locate value.
281 //
282 // Array is supposed to be sorted prior to this call.
283 // If match is found, function returns position of element.
284 // If no match found, function gives nearest element smaller than value.
285
286 const T* pind;
287 pind = std::lower_bound(array, array + n, value);
288 if ( (pind != array + n) && (*pind == value) )
289 return (pind - array);
290 else
291 return ( pind - array - 1);
292}
293
294template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T **array, T value)
295{
296 // Binary search in an array of n values to locate value.
297 //
298 // Array is supposed to be sorted prior to this call.
299 // If match is found, function returns position of element.
300 // If no match found, function gives nearest element smaller than value.
301
302 const T* pind;
303 pind = std::lower_bound(*array, *array + n, value);
304 if ( (pind != *array + n) && (*pind == value) )
305 return (pind - *array);
306 else
307 return ( pind - *array - 1);
308}
309
310template<typename T>
312
314
315 template<typename Index>
316 bool operator()(Index i1, Index i2) {
317 return *(fData + i1) > *(fData + i2);
318 }
319
321};
322
323template<typename T>
325
327
328 template<typename Index>
329 bool operator()(Index i1, Index i2) {
330 return *(fData + i1) < *(fData + i2);
331 }
332
334};
335
336template <typename Iterator, typename IndexIterator>
337void TMath::SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down)
338{
339 // Sort the n1 elements of the Short_t array defined by its
340 // iterators. In output the array index contains the indices of
341 // the sorted array. If down is false sort in increasing order
342 // (default is decreasing order).
343
344 // NOTE that the array index must be created with a length bigger
345 // or equal than the main array before calling this function.
346
347 int i = 0;
348
349 IndexIterator cindex = index;
350 for ( Iterator cfirst = first; cfirst != last; ++cfirst )
351 {
352 *cindex = i++;
353 ++cindex;
354 }
355
356 if ( down )
357 std::sort(index, cindex, CompareDesc<Iterator>(first) );
358 else
359 std::sort(index, cindex, CompareAsc<Iterator>(first) );
360}
361
362template <typename Element, typename Index> void TMath::Sort(Index n, const Element* a, Index* index, Bool_t down)
363{
364 // Sort the n elements of the array a of generic templated type Element.
365 // In output the array index of type Index contains the indices of the sorted array.
366 // If down is false sort in increasing order (default is decreasing order).
367
368 // NOTE that the array index must be created with a length >= n
369 // before calling this function.
370 // NOTE also that the size type for n must be the same type used for the index array
371 // (templated type Index)
372
373 for(Index i = 0; i < n; i++) { index[i] = i; }
374 if ( down )
375 std::sort(index, index + n, CompareDesc<const Element*>(a) );
376 else
377 std::sort(index, index + n, CompareAsc<const Element*>(a) );
378}
379
380#endif
#define d(i)
Definition: RSha256.hxx:102
#define b(i)
Definition: RSha256.hxx:100
unsigned short UShort_t
Definition: RtypesCore.h:36
int Int_t
Definition: RtypesCore.h:41
unsigned int UInt_t
Definition: RtypesCore.h:42
unsigned long ULong_t
Definition: RtypesCore.h:51
long Long_t
Definition: RtypesCore.h:50
bool Bool_t
Definition: RtypesCore.h:59
short Short_t
Definition: RtypesCore.h:35
double Double_t
Definition: RtypesCore.h:55
long double LongDouble_t
Definition: RtypesCore.h:57
long long Long64_t
Definition: RtypesCore.h:69
unsigned long long ULong64_t
Definition: RtypesCore.h:70
float Float_t
Definition: RtypesCore.h:53
const Bool_t kTRUE
Definition: RtypesCore.h:87
Long_t NextPrime(Long_t x)
TMath Base functions.
Definition: TMathBase.cxx:30
Double_t x[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16
#define T2
Definition: md5.inl:146
#define T1
Definition: md5.inl:145
double T(double x)
Definition: ChebyshevPol.h:34
RooCmdArg Index(RooCategory &icat)
TMath.
Definition: TMathBase.h:35
LongDouble_t Abs(LongDouble_t d)
Definition: TMathBase.h:142
Short_t Max(Short_t a, Short_t b)
Definition: TMathBase.h:212
void SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down=kTRUE)
Definition: TMathBase.h:337
T1 Sign(T1 a, T2 b)
Definition: TMathBase.h:165
Short_t Range(Short_t lb, Short_t ub, Short_t x)
Definition: TMathBase.h:244
Bool_t SignBit(LongDouble_t a)
Definition: TMathBase.h:158
Short_t Min(Short_t a, Short_t b)
Definition: TMathBase.h:180
Bool_t Odd(Long_t a)
Definition: TMathBase.h:115
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Definition: TMathBase.h:362
Bool_t SignBit(Integer a)
Definition: TMathBase.h:149
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Definition: TMathBase.h:278
Short_t Abs(Short_t d)
Definition: TMathBase.h:120
Bool_t Even(Long_t a)
Definition: TMathBase.h:112
Definition: first.py:1
bool operator()(Index i1, Index i2)
Definition: TMathBase.h:329
CompareAsc(T d)
Definition: TMathBase.h:326
bool operator()(Index i1, Index i2)
Definition: TMathBase.h:316
CompareDesc(T d)
Definition: TMathBase.h:313
auto * a
Definition: textangle.C:12