ROOT  6.06/09
Reference Guide
testSortOrder.cxx
Go to the documentation of this file.
1 #include <iostream>
2 #include <algorithm>
3 #include <ctime>
4 #include <vector>
5 
6 #include "TMath.h"
7 #include "TRandom2.h"
8 
9 using namespace std;
10 
11 const int maxint = 20;
12 
13 #ifndef ROOT_TMath
14 
15 template<typename T>
16 struct CompareDesc {
17 
18  CompareDesc(T d) : fData(d) {}
19 
20  bool operator()(int i1, int i2) {
21  return *(fData + i1) > *(fData + i2);
22  }
23 
24  T fData;
25 };
26 
27 template<typename T>
28 struct CompareAsc {
29 
30  CompareAsc(T d) : fData(d) {}
31 
32  bool operator()(int i1, int i2) {
33  return *(fData + i1) < *(fData + i2);
34  }
35 
36  T fData;
37 };
38 
39 #endif
40 
41 template <typename T> bool testSort(const int n)
42 {
43  vector<T> k(n);
44  vector<T> indexM(n);
45  vector<T> indexS(n);
46 
47  bool equals = true;
48 
49  TRandom2 r( time( 0 ) );
50 
51  cout << "k: ";
52  for ( Int_t i = 0; i < n; i++) {
53  k[i] = (T) r.Integer( maxint );
54  cout << k[i] << ' ';
55  }
56  cout << endl;
57 
58  for(Int_t i = 0; i < n; i++) { indexM[i] = i; }
59  TMath::Sort(n,&k[0],&indexM[0],kTRUE);
60 
61  cout << "TMath[kTRUE]\n\tindex = ";
62  for ( Int_t i = 0; i < n; ++i )
63  cout << k[indexM[i]] << ' ';
64  cout << endl;
65 
66  for(Int_t i = 0; i < n; i++) { indexS[i] = i; }
67  std::sort(&indexS[0],&indexS[0]+n, CompareDesc<const T*>(&k[0]) );
68 
69  cout << "std::sort[CompareDesc]\n\tindex = ";
70  for ( Int_t i = 0; i < n; ++i )
71  cout << k[indexS[i]] << ' ';
72  cout << endl;
73 
74 
75  equals &= std::equal(indexM.begin(), indexM.end(), indexS.begin());
76  cout << "Equals? " << (char*) (equals?"OK":"FAILED") << endl;
77 
78 
79  for(Int_t i = 0; i < n; i++) { indexM[i] = i; }
80  TMath::Sort(n,&k[0],&indexM[0],kFALSE);
81 
82  cout << "TMath[kFALSE]\n\tindex = ";
83  for ( Int_t i = 0; i < n; ++i )
84  cout << k[indexM[i]] << ' ';
85  cout << endl;
86 
87  for(Int_t i = 0; i < n; i++) { indexS[i] = i; }
88  std::sort(&indexS[0],&indexS[0]+n, CompareAsc<const T*>(&k[0]) );
89 
90  cout << "std::sort[CompareAsc]\n\tindex = ";
91  for ( Int_t i = 0; i < n; ++i )
92  cout << k[indexS[i]] << ' ';
93  cout << endl;
94 
95 
96  equals &= std::equal(indexM.begin(), indexM.end(), indexS.begin());
97  cout << "Equals? " << (char*) (equals?"OK":"FAILED") << endl;
98 
99  return equals;
100 }
101 
102 bool stdsort()
103 {
104  return testSort<Int_t>(20);
105 }
106 
107 int main(int /* argc */ , char ** /* argv */ )
108 {
109  bool equals = stdsort();
110 
111  if ( !equals )
112  return 1;
113  else
114  return 0;
115 }
double T(double x)
Definition: ChebyshevPol.h:34
bool equal(double d1, double d2, double stol=10000)
Random number generator class based on the maximally quidistributed combined Tausworthe generator by ...
Definition: TRandom2.h:29
bool operator()(int i1, int i2)
int Int_t
Definition: RtypesCore.h:41
const Bool_t kFALSE
Definition: Rtypes.h:92
int equals(Double_t n1, Double_t n2, double ERRORLIMIT=1.E-10)
Definition: UnitTesting.cxx:24
STL namespace.
bool operator()(int i1, int i2)
virtual UInt_t Integer(UInt_t imax)
Returns a random integer on [ 0, imax-1 ].
Definition: TRandom.cxx:320
void Sort(Index n, const Element *a, Index *index, Bool_t down=kTRUE)
Definition: TMath.h:1002
ROOT::R::TRInterface & r
Definition: Object.C:4
bool testSort(const int n)
int main(int, char **)
const int maxint
const Bool_t kTRUE
Definition: Rtypes.h:91
const Int_t n
Definition: legend1.C:16
bool stdsort()