MP_data.hpp

Go to the documentation of this file.
00001 // ******************** FlopCpp **********************************************
00002 // File: MP_data.hpp
00003 // $Id$
00004 // Author: Tim Helge Hultberg (thh@mat.ua.pt)
00005 // Copyright (C) 2003 Tim Helge Hultberg
00006 // All Rights Reserved.
00007 // ****************************************************************************
00008 
00009 #ifndef _MP_data_hpp_
00010 #define _MP_data_hpp_
00011 
00012 #include <vector>
00013 #include "MP_index.hpp"    
00014 #include "MP_set.hpp"      
00015 #include "MP_constant.hpp" 
00016 #include "MP_boolean.hpp" 
00017 
00018 namespace flopc {
00019 
00020     class MP_data;
00021 
00027     class DataRef : public Constant_base, public Functor {
00028     public:
00029         DataRef(MP_data* d, 
00030                 const MP_index_exp& i1,
00031                 const MP_index_exp& i2,
00032                 const MP_index_exp& i3,
00033                 const MP_index_exp& i4,
00034                 const MP_index_exp& i5,
00035                 int s = 0) : 
00036             D(d),I1(i1),I2(i2),I3(i3),I4(i4),I5(i5),C(0),stochastic(s) {}
00037 
00038         ~DataRef() {} 
00039         DataRef& such_that(const MP_boolean& b);
00040         double evaluate() const;
00041         int getStage() const;
00042         const DataRef& operator=(const DataRef& r); 
00043         const DataRef& operator=(const Constant& c);
00044         void evaluate_lhs(double v) const;
00045         void operator()() const;
00046     private:
00047         MP_data* D;
00048         MP_index_exp I1,I2,I3,I4,I5;
00049         Constant C;
00050         int stochastic;
00051         MP_boolean B;
00052     };
00053 
00069     class MP_data : public RowMajor, public Functor , public Named {
00070         friend class MP_variable;
00071         friend class DisplayData;
00072         friend class DataRef;
00073         friend class MP_model;
00074     public:
00075         void operator()() const;
00077         void initialize(double d) {
00078             for (int i=0; i<size(); i++) {
00079                 v[i] = d;
00080             }
00081         }
00085         MP_data(const MP_set_base &s1 = MP_set::getEmpty(), 
00086                 const MP_set_base &s2 = MP_set::getEmpty(), 
00087                 const MP_set_base &s3 = MP_set::getEmpty(),
00088                 const MP_set_base &s4 = MP_set::getEmpty(), 
00089                 const MP_set_base &s5 = MP_set::getEmpty()) :
00090             RowMajor(s1.size(),s2.size(),s3.size(),s4.size(),s5.size()),
00091             S1(s1),S2(s2),S3(s3),S4(s4),S5(s5),
00092             v(new double[size()]), manageData(true) 
00093             {
00094                 initialize(0); 
00095             }
00096 
00100         MP_data(double* value,
00101                 const MP_set_base &s1 = MP_set::getEmpty(), 
00102                 const MP_set_base &s2 = MP_set::getEmpty(), 
00103                 const MP_set_base &s3 = MP_set::getEmpty(),
00104                 const MP_set_base &s4 = MP_set::getEmpty(), 
00105                 const MP_set_base &s5 = MP_set::getEmpty()) :
00106             RowMajor(s1.size(),s2.size(),s3.size(),s4.size(),s5.size()),
00107             S1(s1),S2(s2),S3(s3),S4(s4),S5(s5),
00108             v(value), manageData(false) 
00109             {
00110             }
00111 
00112         ~MP_data() {
00113             if (manageData == true) delete[] v;
00115 //          for (unsigned int i=0; i<myrefs.size(); i++) {
00116 //              cout<<"# "<<i<<"   "<<myrefs[i]<<endl;
00117 //              delete myrefs[i]; //Gives segmentation fault. I dont know why!
00118 //          }
00119         }
00120     
00122         void value(const double* d) {
00123             for (int i=0; i<size(); i++) {
00124                 v[i] = d[i];
00125             }
00126         }
00127 
00129         operator double() {
00130             return operator()(0);
00131         }
00132     
00137         double& operator()(int lcli1, int lcli2=0, int lcli3=0, int lcli4=0, int lcli5=0) {
00138             lcli1 = S1.check(lcli1);
00139             lcli2 = S2.check(lcli2);
00140             lcli3 = S3.check(lcli3);
00141             lcli4 = S4.check(lcli4);
00142             lcli5 = S5.check(lcli5);
00143             int i = f(lcli1,lcli2,lcli3,lcli4,lcli5);
00144             if (i == outOfBound) {
00145                 outOfBoundData = 0;
00146                 return outOfBoundData;
00147             } else {
00148                 return v[i];
00149             }
00150         }
00151     
00156         DataRef& operator() (
00157             const MP_index_exp& lcli1 = MP_index_exp::getEmpty(),
00158             const MP_index_exp& lcli2 = MP_index_exp::getEmpty(),
00159             const MP_index_exp& lcli3 = MP_index_exp::getEmpty(),
00160             const MP_index_exp& lcli4 = MP_index_exp::getEmpty(),
00161             const MP_index_exp& lcli5 = MP_index_exp::getEmpty()
00162             ) {
00163             myrefs.push_back(new DataRef(this, lcli1, lcli2, lcli3, lcli4, lcli5));
00164             return *myrefs.back();
00165         }
00166     
00167 
00169         void display(string s = "");
00170     protected:
00171         vector<DataRef*> myrefs;
00172     private:
00173         MP_data(const MP_data&); // Forbid copy constructor
00174         MP_data& operator=(const MP_data&); // Forbid assignment
00175 
00176         static double outOfBoundData;
00177 
00178         MP_index i1,i2,i3,i4,i5;
00179         const MP_set_base &S1,&S2,&S3,&S4,&S5;
00180         double* v;
00181         bool manageData;
00182     };
00183 
00184     class MP_stochastic_data : public MP_data {
00185     public:
00186         MP_stochastic_data(const MP_set_base &s1 = MP_set::getEmpty(), 
00187                            const MP_set_base &s2 = MP_set::getEmpty(), 
00188                            const MP_set_base &s3 = MP_set::getEmpty(),
00189                            const MP_set_base &s4 = MP_set::getEmpty(), 
00190                            const MP_set_base &s5 = MP_set::getEmpty()) :
00191             MP_data(s1,s2,s3,s4,s5) {}
00192 
00193         using flopc::MP_data::operator();
00194         DataRef& operator() (
00195             const MP_index_exp& lcli1 = MP_index_exp::getEmpty(),
00196             const MP_index_exp& lcli2 = MP_index_exp::getEmpty(),
00197             const MP_index_exp& lcli3 = MP_index_exp::getEmpty(),
00198             const MP_index_exp& lcli4 = MP_index_exp::getEmpty(),
00199             const MP_index_exp& lcli5 = MP_index_exp::getEmpty()
00200             ) {
00201             myrefs.push_back(new DataRef(this, lcli1, lcli2, lcli3, lcli4, lcli5, 1));
00202             return *myrefs.back();
00203         }
00204     };
00205 
00206 } // End of namespace flopc
00207 #endif

Generated on Fri Aug 26 03:02:58 2011 for FLOPC++ by  doxygen 1.4.7