Logo ROOT   6.10/09
Reference Guide
testBinarySearch.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <ctime>
3 
4 #include <TRandom2.h>
5 #include <TMath.h>
6 
7 using namespace std;
8 
9 const int nn = 20;
10 const int maxint = 10;
11 const int except = 8;
12 
13 template <typename T> void testBinarySearch()
14 {
15  T k[nn];
16 
17  TRandom2 r( time( 0 ) );
18  for ( Int_t i = 0; i < nn; i++) {
19  T number = (T) r.Integer( maxint );
20  while ( number == except )
21  number = (T) r.Integer( maxint );
22  k[i] = number;
23  }
24 
25  std::sort(k, k+nn);
26 
27  for ( Int_t i = 0; i < nn; i++) {
28  cout << k[i] << ' ';
29  }
30  cout << endl;
31 
32 
33  for ( T elem = -1; elem <= maxint; ++elem ) {
34  Long_t index = TMath::BinarySearch((Long_t) 20, k, elem);
35 
36  T* pind;
37  pind = std::lower_bound(k, k+20, elem);
38  Long_t index2 = ((*pind == elem)? (pind - k): ( pind - k - 1));
39 
40  pind = std::upper_bound(k, k+20, elem);
41  Long_t index3 = ((*pind == elem)? (pind - k): ( pind - k - 1));
42 
43  cout << " ELEM = " << elem
44  << " [TMATH] [i:" << index << " k[i]:" << k[index] << ']'
45  << " [LOWER] [i:" << index2 << " k[i]:" << k[index2] << ']'
46  << " [UPPER] [i:" << index3 << " k[i]:" << k[index3] << ']'
47  << endl;
48 
49  }
50 }
51 
52 void testBinarySearch()
53 {
54  testBinarySearch<Double_t>();
55 
56  cout << "Test done!" << endl;
57 }
58 
59 int main()
60 {
62 
63  return 0;
64 }
void testBinarySearch()
double T(double x)
Definition: ChebyshevPol.h:34
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition: TRandom2.h:27
int Int_t
Definition: RtypesCore.h:41
STL namespace.
virtual UInt_t Integer(UInt_t imax)
Returns a random integer on [ 0, imax-1 ].
Definition: TRandom.cxx:320
TRandom2 r(17)
long Long_t
Definition: RtypesCore.h:50
const int maxint
int main()
const int nn
const int except
Long64_t BinarySearch(Long64_t n, const T *array, T value)
Definition: TMath.h:1093