00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef TEMPLATEVALUEANALYSATION_H_
00026 #define TEMPLATEVALUEANALYSATION_H_
00027
00028
00029 #define ANALYSATION_CONTEXT TemplateValueAnalysation<type,zero,lower,higher,doubleDiv,doubleMul,add,sub,mul,div>
00030 #define DOUBLE_ANALYSATION_CONTEXT TemplateValueAnalysation<double,defaultZero>
00031
00032
00033 #include <vector>
00034 #include <list>
00035
00036
00037
00038
00039
00040
00041
00042 template
00043 <class type>
00044 bool defaultLower(const type& a, const type& b) {
00045 if(a<b)
00046 return true;
00047
00048 return false;
00049 }
00050
00051
00052
00053
00054
00055
00056
00057 template
00058 <class type>
00059 type defaultDoubleDiv(const type& a, const double& b) {
00060 return a/b;
00061 }
00062
00063
00064
00065
00066
00067
00068
00069 template
00070 <class type>
00071 type defaultDoubleMul(const type& a, const double& b) {
00072 return a*b;
00073 }
00074
00075
00076
00077
00078
00079
00080
00081 template
00082 <class type>
00083 bool defaultHigher(const type& a,const type& b) {
00084 if(a>b)
00085 return true;
00086
00087 return false;
00088 }
00089
00090
00091
00092
00093
00094
00095
00096 template
00097 <class type>
00098 type defaultAdd(const type& a, const type& b) {
00099 return a+b;
00100 }
00101
00102
00103
00104
00105
00106
00107
00108 template
00109 <class type>
00110 type defaultSub(const type& a, const type& b) {
00111 return a-b;
00112 }
00113
00114
00115
00116
00117
00118
00119
00120 template
00121 <class type>
00122 type defaultMul(const type& a, const type& b) {
00123 return a*b;
00124 }
00125
00126
00127
00128
00129
00130
00131
00132 template
00133 <class type>
00134 type defaultDiv(const type& a, const type& b) {
00135 return a/b;
00136 }
00137
00138
00139
00140
00141
00142 double defaultZero();
00143
00144
00145
00146
00147
00148
00149
00150
00151 template
00152 <class type,
00153 type zero(void),
00154 bool lower(const type&, const type&)=defaultLower<type>,
00155 bool higher(const type&, const type&)=defaultHigher<type>,
00156 type doubleDiv(const type&, const double&)=defaultDoubleDiv<type>,
00157 type doubleMul(const type&, const double&)=defaultDoubleMul<type>,
00158 type add(const type&, const type&)=defaultAdd<type>,
00159 type sub(const type&, const type&)=defaultSub<type>,
00160 type mul(const type&, const type&)=defaultMul<type>,
00161 type div(const type&, const type&)=defaultDiv<type> >
00162 class TemplateValueAnalysation {
00163 public:
00164
00165
00166
00167
00168
00169 TemplateValueAnalysation(std::vector<type>& values) : m_vector(values), m_list(), m_listCreated(false) {}
00170
00171
00172
00173
00174 ~TemplateValueAnalysation() {}
00175
00176
00177
00178
00179
00180
00181
00182 type getAvg() {
00183 type avg=zero();
00184 __gnu_cxx::__normal_iterator<type*,std::vector<type,std::allocator<type> > > iter;
00185
00186 for(iter = m_vector.begin(); iter != m_vector.end(); iter++) {
00187 avg = add(avg,(*iter));
00188
00189 }
00190
00191 if(m_vector.size()!=0)
00192 avg = doubleDiv(avg,m_vector.size());
00193
00194 return avg;
00195 }
00196
00197
00198
00199
00200
00201
00202
00203 type getMin() {
00204 type min = m_vector[0];
00205 __gnu_cxx::__normal_iterator<type*,std::vector<type,std::allocator<type> > > iter = m_vector.begin();
00206
00207 for(iter++; iter != m_vector.end(); iter++) {
00208 if(lower((*iter),min))
00209 min=(*iter);
00210 }
00211
00212 return min;
00213 }
00214
00215
00216
00217
00218
00219
00220
00221 type getMax() {
00222 type max = m_vector[0];
00223 __gnu_cxx::__normal_iterator<type*,std::vector<type,std::allocator<type> > > iter = m_vector.begin();
00224
00225 for(iter++; iter != m_vector.end(); iter++) {
00226 if(lower(max,(*iter)))
00227 max=(*iter);
00228 }
00229
00230 return max;
00231 }
00232
00233
00234
00235
00236
00237
00238
00239 type getRange() {
00240 type dMax = getMax();
00241 type dMin = getMin();
00242
00243 return sub(dMax,dMin);
00244 }
00245
00246
00247
00248
00249
00250
00251
00252 type getMedian() {
00253 type median;
00254 int x;
00255 int num = m_vector.size()/2;
00256 std::_List_iterator<TYPE_SAVE> iter;
00257
00258 if(!m_listCreated)
00259 sort();
00260
00261 iter = m_list.begin();
00262 for(x=0;x<num;x++) iter++;
00263
00264 if(m_vector.size() % 2 == 0) {
00265 median = (*iter->pointer);
00266 iter--;
00267 median = add(median,(*iter->pointer));
00268 median = doubleMul(median,0.5);
00269 }
00270
00271 else {
00272 median = (*iter->pointer);
00273 }
00274
00275 return median;
00276 }
00277
00278
00279
00280
00281
00282
00283
00284 type getQuartil1() {
00285 type q;
00286 int x;
00287 int num = m_vector.size()/4;
00288 std::_List_iterator<TYPE_SAVE> iter;
00289
00290 if(!m_listCreated)
00291 sort();
00292
00293 iter = m_list.begin();
00294 for(x=0;x<num;x++) iter++;
00295
00296 if(m_vector.size() % 4 == 0) {
00297 q = (*iter->pointer);
00298 iter--;
00299 q = add(q,(*iter->pointer));
00300 q = doubleMul(q,0.5);
00301 }
00302 else {
00303 q = (*iter->pointer);
00304 }
00305
00306 return q;
00307 }
00308
00309
00310
00311
00312
00313
00314
00315 type getQuartil3() {
00316 type q;
00317 int x;
00318 int num = m_vector.size()*3/4;
00319 std::_List_iterator<TYPE_SAVE> iter;
00320
00321 if(!m_listCreated)
00322 sort();
00323
00324 iter = m_list.begin();
00325 for(x=0;x<num;x++) iter++;
00326
00327 if(m_vector.size() % 4 == 0) {
00328 q = (*iter->pointer);
00329 iter--;
00330 q = add(q,(*iter->pointer));
00331 q = doubleMul(q,0.5);
00332 }
00333 else {
00334 q = (*iter->pointer);
00335 }
00336
00337 return q;
00338 }
00339
00340
00341
00342
00343
00344
00345
00346 type getIQR() {
00347 type q1 = getQuartil1();
00348 type q3 = getQuartil3();
00349
00350 return sub(q3,q1);
00351 }
00352
00353
00354
00355
00356
00357
00358
00359
00360 type getWhisker(double factor) {
00361 type dIqr = getIQR();
00362
00363 return doubleMul(dIqr,factor);
00364 }
00365
00366
00367
00368
00369
00370
00371
00372
00373 type getWhisker1(double factor) {
00374 type dW = getWhisker(factor);
00375 type dQ1 = getQuartil1();
00376
00377 type dBorder = sub(dQ1,dW);
00378 std::_List_iterator<TYPE_SAVE> iter = m_list.begin();
00379
00380 while(lower((*iter->pointer),dBorder) && iter!=m_list.end()) {
00381 iter++;
00382 if(iter==m_list.end())
00383 break;
00384 }
00385
00386 if(iter==m_list.end())
00387 return zero();
00388
00389 return (*iter->pointer);
00390 }
00391
00392
00393
00394
00395
00396
00397
00398
00399 type getWhisker3(double factor) {
00400 type dW = getWhisker(factor);
00401 type dQ3 = getQuartil3();
00402
00403 type dBorder = add(dQ3,dW);
00404 std::_List_iterator<TYPE_SAVE> iter = m_list.begin();
00405
00406 while(lower((*iter->pointer),dBorder) && iter!=m_list.end()) {
00407 iter++;
00408 if(iter==m_list.end())
00409 break;
00410 }
00411
00412 if(iter!=m_list.begin())
00413 iter--;
00414
00415 return (*iter->pointer);
00416 }
00417
00418
00419
00420
00421
00422
00423
00424
00425 unsigned int getNumExtrems(double factor) {
00426 unsigned int result = 0;
00427 type dW1 = getWhisker1(factor);
00428 type dW3 = getWhisker3(factor);
00429 std::_List_iterator<TYPE_SAVE> iter = m_list.begin();
00430
00431 while(lower((*iter->pointer),dW1) && iter!=m_list.end()) {
00432 iter++;
00433 if(iter==m_list.end())
00434 break;
00435 result++;
00436 }
00437
00438 while(!higher((*iter->pointer),dW3) && iter!=m_list.end()) {
00439 iter++;
00440 if(iter==m_list.end())
00441 break;
00442 }
00443
00444 while(iter!=m_list.end()) {
00445 iter++;
00446 result++;
00447 }
00448
00449 return result;
00450 }
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460 type getExtrem(double factor, unsigned int i) {
00461 type dW1 = getWhisker1(factor);
00462 type dW3 = getWhisker3(factor);
00463 std::_List_iterator<TYPE_SAVE> iter = m_list.begin();
00464
00465 while(lower((*iter->pointer),dW1) && iter!=m_list.end()) {
00466 i--;
00467
00468 if(i==0)
00469 return (*iter->pointer);
00470
00471 iter++;
00472 if(iter==m_list.end())
00473 break;
00474 }
00475
00476 while(!higher((*iter->pointer),dW3) && iter!=m_list.end()) {
00477 iter++;
00478 if(iter==m_list.end())
00479 break;
00480 }
00481
00482 while(iter!=m_list.end()) {
00483 i--;
00484
00485 if(i==0)
00486 return (*iter->pointer);
00487
00488 iter++;
00489 }
00490
00491 return zero();
00492 }
00493
00494
00495
00496
00497
00498
00499
00500 type getBest(void) {
00501 type z = zero();
00502 type* l;
00503 type* h;
00504
00505 if(!m_listCreated)
00506 sort();
00507
00508 std::_List_iterator<TYPE_SAVE> iter = m_list.begin();
00509
00510 while(lower((*iter->pointer),z) && iter!=m_list.end()) {
00511 iter++;
00512 if(iter==m_list.end())
00513 break;
00514 }
00515
00516 if(iter!=m_list.end())
00517
00518 h = iter->pointer;
00519
00520 else {
00521 iter--;
00522
00523 h = iter->pointer;
00524 }
00525
00526 while(!lower((*iter->pointer),z) && iter!= m_list.begin()) {
00527 iter--;
00528 if(iter==m_list.begin())
00529 break;
00530 }
00531
00532 l = iter->pointer;
00533
00534 if(lower(sub(*h,z),sub(z,*l)))
00535 return *h;
00536 else
00537 return *l;
00538 }
00539
00540 protected:
00541
00542
00543
00544
00545
00546 struct TYPE_SAVE {
00547 TYPE_SAVE(type& a) : pointer(&a) {}
00548
00549 type* pointer;
00550
00551 bool operator<(const TYPE_SAVE& other) {
00552 return lower((*pointer),(*other.pointer));
00553 }
00554 };
00555
00556
00557
00558
00559 std::vector<type>& m_vector;
00560
00561
00562
00563
00564 std::list<TYPE_SAVE> m_list;
00565
00566
00567
00568
00569 bool m_listCreated;
00570
00571
00572
00573
00574 void sort(void) {
00575 __gnu_cxx::__normal_iterator<type*,std::vector<type,std::allocator<type> > > iter;
00576
00577 for(iter = m_vector.begin(); iter != m_vector.end(); iter++) {
00578 m_list.push_back(TYPE_SAVE((*iter)));
00579 }
00580
00581 m_list.sort();
00582 m_listCreated = true;
00583 }
00584 };
00585
00586 #endif