Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 "RtypesCore.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{ return std::llabs(d); }
131
133{ return std::abs(d); }
134
136{ return std::abs(d); }
137
139{ return std::abs(d); }
140
141
142//---- Sign Bit--------------------------------------------------------------------
143
144template<typename Integer>
145inline Bool_t TMath::SignBit( Integer a)
146 { return (a < 0); }
147
149 { return std::signbit(a); }
150
152 { return std::signbit(a); }
153
155 { return std::signbit(a); }
156
157
158//---- Sign --------------------------------------------------------------------
159
160template<typename T1, typename T2>
162 { return (SignBit(b)) ? - Abs(a) : Abs(a); }
163
165 { return std::copysign(a,b); }
166
168 { return std::copysign(a,b); }
169
171 { return std::copysign(a,b); }
172
173
174//---- Min ---------------------------------------------------------------------
175
177 { return a <= b ? a : b; }
178
180 { return a <= b ? a : b; }
181
183 { return a <= b ? a : b; }
184
186 { return a <= b ? a : b; }
187
189 { return a <= b ? a : b; }
190
192 { return a <= b ? a : b; }
193
195 { return a <= b ? a : b; }
196
198 { return a <= b ? a : b; }
199
201 { return a <= b ? a : b; }
202
204 { return a <= b ? a : b; }
205
206//---- Max ---------------------------------------------------------------------
207
209 { return a >= b ? a : b; }
210
212 { return a >= b ? a : b; }
213
215 { return a >= b ? a : b; }
216
218 { return a >= b ? a : b; }
219
221 { return a >= b ? a : b; }
222
224 { return a >= b ? a : b; }
225
227 { return a >= b ? a : b; }
228
230 { return a >= b ? a : b; }
231
233 { return a >= b ? a : b; }
234
236 { return a >= b ? a : b; }
237
238//---- Range -------------------------------------------------------------------
239
241 { return x < lb ? lb : (x > ub ? ub : x); }
242
244 { return x < lb ? lb : (x > ub ? ub : x); }
245
247 { return x < lb ? lb : (x > ub ? ub : x); }
248
250 { return x < lb ? lb : (x > ub ? ub : x); }
251
253 { return x < lb ? lb : (x > ub ? ub : x); }
254
255template <typename Iterator, typename Element>
256Iterator TMath::BinarySearch(Iterator first, Iterator last, Element value)
257{
258 // Binary search in an array defined by its iterators.
259 //
260 // The values in the iterators range are supposed to be sorted
261 // prior to this call. If match is found, function returns
262 // position of element. If no match found, function gives nearest
263 // element smaller than value.
264
265 Iterator pind;
266 pind = std::lower_bound(first, last, value);
267 if ( (pind != last) && (*pind == value) )
268 return pind;
269 else
270 return ( pind - 1);
271}
272
273
274template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T *array, T value)
275{
276 // Binary search in an array of n values to locate value.
277 //
278 // Array is supposed to be sorted prior to this call.
279 // If match is found, function returns position of element.
280 // If no match found, function gives nearest element smaller than value.
281
282 const T* pind;
283 pind = std::lower_bound(array, array + n, value);
284 if ( (pind != array + n) && (*pind == value) )
285 return (pind - array);
286 else
287 return ( pind - array - 1);
288}
289
290template <typename T> Long64_t TMath::BinarySearch(Long64_t n, const T **array, T value)
291{
292 // Binary search in an array of n values to locate value.
293 //
294 // Array is supposed to be sorted prior to this call.
295 // If match is found, function returns position of element.
296 // If no match found, function gives nearest element smaller than value.
297
298 const T* pind;
299 pind = std::lower_bound(*array, *array + n, value);
300 if ( (pind != *array + n) && (*pind == value) )
301 return (pind - *array);
302 else
303 return ( pind - *array - 1);
304}
305
306template<typename T>
308
310
311 template<typename Index>
312 bool operator()(Index i1, Index i2) {
313 return *(fData + i1) > *(fData + i2);
314 }
315
317};
318
319template<typename T>
321
323
324 template<typename Index>
325 bool operator()(Index i1, Index i2) {
326 return *(fData + i1) < *(fData + i2);
327 }
328
330};
331
332template <typename Iterator, typename IndexIterator>
333void TMath::SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down)
334{
335 // Sort the n1 elements of the Short_t array defined by its
336 // iterators. In output the array index contains the indices of
337 // the sorted array. If down is false sort in increasing order
338 // (default is decreasing order).
339
340 // NOTE that the array index must be created with a length bigger
341 // or equal than the main array before calling this function.
342
343 int i = 0;
344
345 IndexIterator cindex = index;
346 for ( Iterator cfirst = first; cfirst != last; ++cfirst )
347 {
348 *cindex = i++;
349 ++cindex;
350 }
351
352 if ( down )
353 std::sort(index, cindex, CompareDesc<Iterator>(first) );
354 else
355 std::sort(index, cindex, CompareAsc<Iterator>(first) );
356}
357
358template <typename Element, typename Index> void TMath::Sort(Index n, const Element* a, Index* index, Bool_t down)
359{
360 // Sort the n elements of the array a of generic templated type Element.
361 // In output the array index of type Index contains the indices of the sorted array.
362 // If down is false sort in increasing order (default is decreasing order).
363
364 // NOTE that the array index must be created with a length >= n
365 // before calling this function.
366 // NOTE also that the size type for n must be the same type used for the index array
367 // (templated type Index)
368
369 for(Index i = 0; i < n; i++) { index[i] = i; }
370 if ( down )
371 std::sort(index, index + n, CompareDesc<const Element*>(a) );
372 else
373 std::sort(index, index + n, CompareAsc<const Element*>(a) );
374}
375
376#endif
#define d(i)
Definition RSha256.hxx:102
#define b(i)
Definition RSha256.hxx:100
#define a(i)
Definition RSha256.hxx:99
unsigned short UShort_t
Definition RtypesCore.h:40
int Int_t
Definition RtypesCore.h:45
unsigned int UInt_t
Definition RtypesCore.h:46
unsigned long ULong_t
Definition RtypesCore.h:55
long Long_t
Definition RtypesCore.h:54
short Short_t
Definition RtypesCore.h:39
double Double_t
Definition RtypesCore.h:59
long double LongDouble_t
Definition RtypesCore.h:61
long long Long64_t
Definition RtypesCore.h:80
unsigned long long ULong64_t
Definition RtypesCore.h:81
float Float_t
Definition RtypesCore.h:57
const Bool_t kTRUE
Definition RtypesCore.h:100
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
TMath.
Definition TMathBase.h:35
Short_t Max(Short_t a, Short_t b)
Definition TMathBase.h:208
void SortItr(Iterator first, Iterator last, IndexIterator index, Bool_t down=kTRUE)
Definition TMathBase.h:333
Long_t NextPrime(Long_t x)
T1 Sign(T1 a, T2 b)
Definition TMathBase.h:161
Short_t Range(Short_t lb, Short_t ub, Short_t x)
Definition TMathBase.h:240
Short_t Min(Short_t a, Short_t b)
Definition TMathBase.h:176
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:358
Bool_t SignBit(Integer a)
Definition TMathBase.h:145
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Definition TMathBase.h:274
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:325
CompareAsc(T d)
Definition TMathBase.h:322
bool operator()(Index i1, Index i2)
Definition TMathBase.h:312
CompareDesc(T d)
Definition TMathBase.h:309