Association.h

Go to the documentation of this file.
00001 // ----------------------------------------------------------------------------
00002 //
00003 // $Id$
00004 //
00005 // Copyright 2008, 2009, 2010, 2011, 2012  Antonio Franchi and Paolo Stegagno    
00006 //
00007 // This file is part of MIP.
00008 //
00009 // MIP is free software: you can redistribute it and/or modify
00010 // it under the terms of the GNU General Public License as published by
00011 // the Free Software Foundation, either version 3 of the License, or
00012 // (at your option) any later version.
00013 //
00014 // MIP is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 // GNU General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU General Public License
00020 // along with MIP. If not, see <http://www.gnu.org/licenses/>.
00021 //
00022 // Contact info: antonio.franchi@tuebingen.mpg.de stegagno@diag.uniroma1.it
00023 //
00024 // ----------------------------------------------------------------------------
00025 
00026 
00030 
00031 
00033 /* @{ */
00034 
00035 #ifndef __STD_ROBOT_H_
00036 #define __STD_ROBOT_H_
00037 
00038 #ifdef MIP_HOST_APPLE
00039 #include </usr/include/time.h>
00040 #include <ctime>
00041 #endif
00042 
00043 #include <stdio.h>
00044 #include <math.h>
00045 #include <vector>
00046 #include <iostream>
00047 #include <string>
00048 #include <sstream>
00049 #include <sys/time.h>
00050 #include <sys/select.h>
00051 #include <assert.h>
00052 #include <pthread.h>
00053 
00054 #include "Types.h"
00055 
00056 #ifdef FOUND_BOOST_SER
00057  #include <boost/serialization/vector.hpp>
00058  #include <boost/archive/text_iarchive.hpp>
00059  #include <boost/archive/text_oarchive.hpp>
00060 #endif
00061 
00062 using namespace std;
00063 
00064 
00070 class Meaner{
00071  private:
00072   Decimal  _mean;
00073   long int _n;
00074  public:
00077   Meaner (void){
00078    _mean = 0.0;
00079    _n  = 0;
00080   }
00084   Meaner (Decimal value){
00085    _mean = value;
00086    _n  = 1;
00087   }
00090    void reset(void){
00091    _mean = 0.0;
00092    _n  = 0;
00093   }
00097   void reset(Decimal value){
00098    _mean = value;
00099    _n  = 1;
00100   }
00104   Decimal mean(){
00105    return _mean;
00106   }
00111   Decimal mean(Decimal value){
00112    if(_n!=0){
00113     _mean = (((Decimal) _n)/((Decimal) _n+1))*_mean + ( value / ((Decimal) _n+1) );
00114    }else{
00115     _mean = value;
00116    }
00117     _n++;
00118    return _mean;
00119   }
00120 };
00121 
00122 /*TODO fondere tutto in SequenceStats che ha media meadiapesata min max std var (e a questo punto anche la sequenza con i vector)*/
00129 // class MinMaxer{
00130 //  private:
00131 //   Decimal  _max;
00132 //   Decimal  _min;
00133 //   bool   _on;
00134 //  public:
00135 //   /** default constructor 
00136 //    */
00137 //   MinMaxer (void){
00138 //    _min = 0.0;
00139 //    _max = 0.0;
00140 //    _on  = false;
00141 //   }
00142 //   /** Decimal constructor 
00143 //    * \param value first value
00144 //    */
00145 //   MinMaxer (Decimal value){
00146 //    _min = value;
00147 //    _max = value;
00148 //    _on  = true;
00149 //   }
00150 //   /** reset the values at null
00151 //    */
00152 //   void reset(void){
00153 //    _min = 0.0;
00154 //    _max = 0.0;
00155 //    _on  = false;
00156 //   }
00157 //   /** reset the mean at another value
00158 //    * \param value first value
00159 //    */
00160 //   void reset(Decimal value){
00161 //    _mean = value;
00162 //    _n  = 1;
00163 //   }
00164 //   /** current mean
00165 //    * \return currrent mean
00166 //    */
00167 //   Decimal min(){
00168 //    return _min;
00169 //   }
00170 //   /** update the mean and return the updated mean
00171 //    * \return updated mean
00172 //    * \param value another value
00173 //    */
00174 //   Decimal min(Decimal value){
00175 //    if(_n!=0){
00176 //     _mean = (((Decimal) _n)/((Decimal) _n+1))*_mean + ( value / ((Decimal) _n+1) );
00177 //    }else{
00178 //     _mean = value;
00179 //    }
00180 //    _n++;
00181 //    return _mean;
00182 //   }
00183 // };
00184 
00185 
00186 
00191 template <class T, class assocType>
00192 class Association{
00193  private:
00194 #ifdef FOUND_BOOST_SER
00195   friend class boost::serialization::access;
00196   template<class Archive>
00197     void serialize(Archive & ar, const unsigned int version){
00198    ar & _obj; 
00199    ar & _assoObj;
00200    ar & _isAsso;
00201   }
00202 #endif
00203 
00204   T    _obj;   
00205   assocType  _assoObj; 
00206   bool  _isAsso; 
00207  public:
00211   Association(){
00212    _isAsso = false;
00213 
00214   }
00219   Association(const T& o){
00220    _obj = o;
00221    _isAsso = false;
00222   }
00228   Association(const T& o, const assocType& ao){
00229    _obj = o;
00230    _assoObj = ao;
00231    _isAsso  = true;
00232   }
00233 
00235   Association(const Association& a){
00236    _obj = a._obj;
00237    _assoObj = a._assoObj;
00238    _isAsso  = a._isAsso;
00239   };
00240   
00241   
00243   Association& operator=(const Association& a){
00244    if (this != &a){      // Not necessary in this case but it is useful to don't forget it
00245     _obj   = a._obj;
00246     _assoObj = a._assoObj;
00247     _isAsso  = a._isAsso;
00248    }
00249    return *this;
00250   }
00251 
00256   void associate(const assocType& ao){
00257    _assoObj = ao;
00258    _isAsso  = true;
00259   }
00264   void unassociate() {
00265    _isAsso  = false;
00266   }
00270   bool associated(void) const {
00271    return _isAsso;
00272   }
00273 
00277   T getObj(void) const {
00278    return _obj;
00279   }
00280 
00281         // open the object in edit mode
00282   // TODO: (mriedel:) I would rather return a reference here.
00283         T* setObj(void) {
00284          return &_obj;
00285         }
00286                 
00287         // open the object in edit mode
00288         void setObj(const T& pp){
00289          _obj = pp;
00290         }
00291                 
00295   assocType getAsso(void) const {
00296    return _assoObj;
00297   }
00298 };
00299 
00300 
00301 
00302 #endif
00303 
00304 /* @} */
00305 
00306 
00307 
00308 

Generated on Mon Feb 20 07:01:07 2017 for MIP by  doxygen 1.5.6