MTK++ Latest version: 0.2.0

idObject.h
Go to the documentation of this file.
00001 
00033 #ifndef IDOBJECT_H
00034 #define IDOBJECT_H
00035 
00036 #include <iostream>
00037 #include <fstream>
00038 #include <sstream>
00039 #include <vector>
00040 #include <algorithm>
00041 #include <iomanip>
00042 
00043 namespace MTKpp
00044 {
00045 
00046 // ============================================================
00047 // Class : idObject()
00048 // ------------------------------------------------------------
00056 // ============================================================
00057 class idObject
00058 {
00059 public:
00060 
00064     idObject() {
00065       this->i = 0;
00066       this->d = 0.0;
00067     }
00068 
00072     idObject(int i, double d) {
00073       this->i = i;
00074       this->d = d;
00075     }
00076 
00080     idObject(int i, double d, std::string n) {
00081       this->i = i;
00082       this->d = d;
00083       this->n = n;
00084     }
00085 
00089     virtual ~idObject() {};
00090 
00099     static bool less(const idObject *lhs, const idObject *rhs) {
00100         return lhs->d < rhs->d;
00101     }
00102 
00111     static bool greater(const idObject *lhs, const idObject *rhs) {
00112         return lhs->d > rhs->d;
00113     }
00114 
00115     void setI(int i) {
00116       this->i = i;
00117     }
00118 
00119     void setD(double d) {
00120       this->d = d;
00121     }
00122 
00123     void setN(std::string n) {
00124       this->n = n;
00125     }
00126 
00127     int getI() {
00128       return this->i;
00129     }
00130 
00131     double getD() {
00132       return this->d;
00133     }
00134 
00135     std::string getN() {
00136       return this->n;
00137     }
00138 
00139 protected:
00140 
00142    int i;
00143 
00145    double d;
00146 
00148    std::string n;
00149 };
00150 
00151 // ============================================================
00152 // Class : idObjectList()
00153 // ------------------------------------------------------------
00161 // ============================================================
00162 class idObjectList
00163 {
00164 public:
00165 
00167     int index;
00168 
00170     std::vector<idObject*> objList;
00171 
00172 public:
00173 
00177     idObjectList() {
00178       this->index = 0;
00179     }
00180 
00184     virtual ~idObjectList() {};
00185 
00186     void addObject(std::string name, double value) {
00187       idObject* idO = new idObject(index, value, name);
00188       objList.push_back(idO);
00189       index++;
00190     }
00191 
00192     void sortList(int d) {
00193       if (d > 0) { // ascending
00194         sort(objList.begin(), objList.end(), idObject::less);
00195       }
00196       else { // descending
00197         sort(objList.begin(), objList.end(), idObject::greater);
00198       }
00199     }
00200 
00201     int getRank(std::string n) {
00202       for (unsigned int i = 0; i < objList.size(); i++) {
00203         if (objList[i]->getN() == n) {
00204           return i;
00205         }
00206       }
00207       return -1;
00208     }
00209 
00210     idObject* getObject(std::string n) {
00211       if (index == 0) return 0;
00212       for (unsigned int i = 0; i < objList.size(); i++) {
00213         if (objList[i]->getN() == n) {
00214           return objList[i];
00215         }
00216       }
00217       return 0;
00218     }
00219 
00220     idObject* getFirst() {
00221       return objList[0];
00222     }
00223 
00224     void print() {
00225       std::cout << " Number of objects = " << objList.size() << std::endl;
00226       for (unsigned int i = 0; i < objList.size(); i++) {
00227         std::cout << " Name = " << objList[i]->getN() << " Value = " << objList[i]->getD() << std::endl;
00228       }
00229     }
00230 
00231     double lMin() {
00232       double m = objList[0]->getD();
00233       for (unsigned int i = 1; i < objList.size(); i++) {
00234         if (objList[i]->getD() < m) {
00235           m = objList[i]->getD();
00236         }
00237       }
00238       return m;
00239     }
00240 
00241     double lMax() {
00242       double m = objList[0]->getD();
00243       for (unsigned int i = 1; i < objList.size(); i++) {
00244         if (objList[i]->getD() > m) {
00245           m = objList[i]->getD();
00246         }
00247       }
00248       return m;
00249     }
00250 
00251     double mean() {
00252       double m = 0.0;
00253       for (unsigned int i = 0; i < objList.size(); i++) {
00254         m += objList[i]->getD();
00255       }
00256       m /= double(objList.size());
00257       return m;
00258     }
00259 
00260     double variance() {
00261       if (index == 0) return 0.0;
00262       double variance = 0.0;
00263       double m = this->mean();
00264 
00265       for (unsigned int i = 0; i < objList.size(); i++) {
00266         double x = objList[i]->getD() - m;
00267         variance += (x * x);
00268       }
00269       variance /= double(objList.size() - 1);
00270       return variance;
00271     }
00272 
00273     double standardDeviation() {
00274       if (index == 0) return 0.0;
00275       return sqrt(this->variance());
00276     }
00277 
00278     void normalize() {
00279       double mn = lMin();
00280       double mx = lMax();
00281       double mxmn = mx - mn;
00282 
00283       for (unsigned int i = 0; i < objList.size(); i++) {
00284         objList[i]->setD(  (objList[i]->getD() - mn) / mxmn  );
00285       }
00286     }
00287 
00288     void autoScale() {
00289       double m = this->mean();
00290       double s = this->standardDeviation();
00291 
00292       for (unsigned int i = 0; i < objList.size(); i++) {
00293         objList[i]->setD(  (objList[i]->getD() - m) / s  );
00294       }
00295     }
00296 
00297 };
00298 
00299 } // MTKpp namespace
00300 
00301 #endif // IDOBJECT_H
00302 

Generated on Fri Dec 23 2011 09:28:50 for MTK++ by Doxygen 1.7.5.1