Files Classes Functions Hierarchy
00001 #include <cassert> 00002 #include <algorithm> 00003 #include <iostream> 00004 #include <set> 00005 #include <vector> 00006 using namespace std; 00007 00008 #include <aclock.h> 00009 #include <bucket.h> 00010 #include <buckethybrid.h> 00011 #include <buckettest.h> 00012 #include <print.h> 00013 #include <random.h> 00014 #include <unitdouble.h> 00015 00016 string buckettest::doc[] = 00017 { 00018 "", 00019 "Use bucket: sorting a array of doubles in range [0.0,1.0).", 00020 "Testing the buckets sorting time of doubles in the range [0.0,1.0) tested in progressive powers of two.", 00021 "Test the hybrid bucket sort time of doubles in the range [0.0,1.0) tested in progressive powers of two.", 00022 "Test hybrid bucket sort with bad data." 00023 00024 }; 00025 00026 void buckettest::fill(double * data, uintc n) 00027 { 00028 random10<double> r; 00029 00030 for (uint i=0; i<n; ++i) 00031 data[i] = r(); 00032 } 00033 00034 void buckettest::test01() 00035 { 00036 cout << "Testing Bucket Sort" << endl << endl; 00037 00038 uintc n(10); 00039 00040 vector<double> v(n); 00041 fill(&v[0],n); 00042 00043 cout << "Vector of random values" << endl; 00044 cout << print(v,"\n"); 00045 cout << endl << endl; 00046 00047 00048 00049 cout << "Sorting" << endl; 00050 unitdouble f1(2*n); 00051 bucket<double,unitdouble> b(&v[0],n,f1); 00052 b.eval(); 00053 00054 vector<double> v2(n); 00055 b.copy(&v2[0]); 00056 00057 cout << print(v2) << endl; 00058 00059 } 00060 00061 boolc buckettest::verifyequalvectors 00062 ( 00063 vector<double> & v1, 00064 vector<double> const & v2 00065 ) 00066 { 00067 sort(v1.begin(),v1.end()); 00068 if (v1.size()!=v2.size()) 00069 return false; 00070 00071 uint sz = v1.size(); 00072 for (uint i=0; i<sz; ++i) 00073 if (v1[i]!=v2[i]) 00074 return false; 00075 00076 return true; 00077 } 00078 00079 void buckettest::createandtest(uintc n) 00080 { 00081 vector<double> v(n); 00082 fill(&v[0],n); 00083 cout << n << " "; 00084 00085 unitdouble f1(n); 00086 bucket<double,unitdouble> b(&v[0],n,f1); 00087 00088 aclock c; 00089 c.measure(); 00090 b.eval(); 00091 c.measure(); 00092 cout << c.diff_s() << endl; 00093 00094 vector<double> v2(n); 00095 b.copy(&v2[0]); 00096 00097 assert(verifyequalvectors(v,v2)); 00098 } 00099 00100 void buckettest::test02() 00101 { 00102 cout << "Testing Bucket Sort" << endl << endl; 00103 00104 cout << "Testing Sorting Time" << endl; 00105 00106 uint u=10000; 00107 for (uint k=0; k<10; ++k) 00108 { 00109 createandtest(u); 00110 u *= 2; 00111 } 00112 } 00113 00114 void buckettest::createandtest2(uintc n, uintc chainmax ) 00115 { 00116 vector<double> v(n); 00117 fill(&v[0],n); 00118 cout << n << " "; 00119 unitdouble f1(n*2); 00120 typedef buckethybrid<double,unitdouble, multiset<double> > bhd; 00121 bhd b(&v[0],n,f1,chainmax); 00122 00123 aclock c; 00124 c.measure(); 00125 b.eval(); 00126 c.measure(); 00127 cout << c.diff_s() << endl; 00128 00129 vector<double> v2(n); 00130 b.copy(&v2[0]); 00131 00132 assert(verifyequalvectors(v,v2)); 00133 } 00134 00135 // This is the worst situation: all the data is in the one place. 00136 void buckettest::fillbad(double * data, uintc n) 00137 { 00138 random10<double> r; 00139 00140 doublec x = r(); 00141 00142 for (uint i=0; i<n; ++i) 00143 data[i] = x; 00144 } 00145 00146 void buckettest::test03() 00147 { 00148 cout << "Testing Bucket Hybrid Sort" << endl << endl; 00149 cout << "Testing Sorting Time" << endl; 00150 00151 uint u=10000; 00152 uint blen = 4; 00153 for (uint k=0; k<9; ++k) 00154 { 00155 createandtest2(u,blen); 00156 u *= 2; 00157 ++blen; 00158 } 00159 } 00160 00161 void buckettest::createandtest3(uintc n, uintc chainmax ) 00162 { 00163 vector<double> v(n); 00164 fillbad(&v[0],n); 00165 cout << n << " "; 00166 unitdouble f1(n*2); 00167 typedef buckethybrid<double,unitdouble, multiset<double> > bhd; 00168 bhd b(&v[0],n,f1,chainmax); 00169 00170 aclock c; 00171 c.measure(); 00172 b.eval(); 00173 c.measure(); 00174 cout << c.diff_s() << endl; 00175 00176 vector<double> v2(n); 00177 b.copy(&v2[0]); 00178 00179 cout << SHOW(v.size()) << endl; 00180 cout << SHOW(v2.size()) << endl; 00181 00182 cout << "v=" << print(v) << endl; 00183 cout << "v2=" << print(v2) << endl; 00184 00185 assert(verifyequalvectors(v,v2)); 00186 } 00187 00188 void buckettest::test04() 00189 { 00190 cout << "Testing Bucket Hybrid Sort" << endl << endl; 00191 cout << "Testing Sorting Time" << endl; 00192 00193 uint u=1000; 00194 uint blen = 4; 00195 for (uint k=0; k<1; ++k) 00196 { 00197 createandtest3(u,blen); 00198 u *= 2; 00199 ++blen; 00200 } 00201 } 00202 00203 00204 00205
1.5.8