testBinarySearch.cxx

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <ctime>
00003 
00004 #include <TRandom2.h>
00005 #include <TMath.h>
00006 
00007 using namespace std;
00008 
00009 const int n = 20;
00010 const int maxint = 10;
00011 const int except = 8;
00012 
00013 template <typename T> void testBinarySearch()
00014 {
00015    T k[n];
00016 
00017    TRandom2 r( time( 0 ) );
00018    for ( Int_t i = 0; i < n; i++) {
00019       T number = (T) r.Integer( maxint );
00020       while ( number == except )
00021          number = (T) r.Integer( maxint );
00022       k[i] = number;
00023    }
00024 
00025    std::sort(k, k+n);
00026 
00027    for ( Int_t i = 0; i < n; i++) {
00028       cout << k[i] << ' ';
00029    }
00030    cout << endl;
00031 
00032 
00033    for ( T elem = -1; elem <= maxint; ++elem ) {
00034       Long_t index = TMath::BinarySearch((Long_t) 20, k, elem);
00035       
00036       T* pind;
00037       pind = std::lower_bound(k, k+20, elem);
00038       Long_t index2 = ((*pind == elem)? (pind - k): ( pind - k - 1));
00039 
00040       pind = std::upper_bound(k, k+20, elem);
00041       Long_t index3 = ((*pind == elem)? (pind - k): ( pind - k - 1));
00042 
00043       cout << " ELEM = " << elem
00044            << " [TMATH] [i:" << index  << " k[i]:" << k[index] << ']'
00045            << " [LOWER] [i:" << index2 << " k[i]:" << k[index2] << ']'
00046            << " [UPPER] [i:" << index3 << " k[i]:" << k[index3] << ']'
00047            << endl;
00048 
00049    }
00050 }
00051 
00052 void testBinarySearch()
00053 {
00054    testBinarySearch<Double_t>();
00055 
00056    cout << "Test done!" << endl;
00057 }
00058 
00059 int main()
00060 {
00061    testBinarySearch();
00062 
00063    return 0;
00064 }

Generated on Tue Jul 5 14:34:30 2011 for ROOT_528-00b_version by  doxygen 1.5.1