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