FLOPC++
|
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 DataRef& probability(double p) { return *this; } 00047 private: 00048 MP_data* D; 00049 MP_index_exp I1,I2,I3,I4,I5; 00050 Constant C; 00051 int stochastic; 00052 MP_boolean B; 00053 }; 00054 00071 class MP_data : public RowMajor, public Functor , public Named { 00072 friend class MP_variable; 00073 friend class DisplayData; 00074 friend class DataRef; 00075 friend class MP_model; 00076 public: 00077 void operator()() const; 00079 void initialize(double d) { 00080 for (int i=0; i<size(); i++) { 00081 v[i] = d; 00082 } 00083 } 00087 MP_data(const MP_set_base &s1 = MP_set::getEmpty(), 00088 const MP_set_base &s2 = MP_set::getEmpty(), 00089 const MP_set_base &s3 = MP_set::getEmpty(), 00090 const MP_set_base &s4 = MP_set::getEmpty(), 00091 const MP_set_base &s5 = MP_set::getEmpty()) : 00092 RowMajor(s1.size(),s2.size(),s3.size(),s4.size(),s5.size()), 00093 S1(s1),S2(s2),S3(s3),S4(s4),S5(s5), 00094 v(new double[size()]), manageData(true) 00095 { 00096 initialize(0); 00097 } 00098 00102 MP_data(double* value, 00103 const MP_set_base &s1 = MP_set::getEmpty(), 00104 const MP_set_base &s2 = MP_set::getEmpty(), 00105 const MP_set_base &s3 = MP_set::getEmpty(), 00106 const MP_set_base &s4 = MP_set::getEmpty(), 00107 const MP_set_base &s5 = MP_set::getEmpty()) : 00108 RowMajor(s1.size(),s2.size(),s3.size(),s4.size(),s5.size()), 00109 S1(s1),S2(s2),S3(s3),S4(s4),S5(s5), 00110 v(value), manageData(false) 00111 { } 00112 00113 ~MP_data() { 00114 if (manageData == true) delete[] v; 00115 } 00116 00118 void value(const double* d) { 00119 for (int i=0; i<size(); i++) { 00120 v[i] = d[i]; 00121 } 00122 } 00123 00125 operator double() { 00126 return operator()(0); 00127 } 00128 00133 double& operator()(int lcli1, int lcli2=0, int lcli3=0, int lcli4=0, int lcli5=0) { 00134 lcli1 = S1.check(lcli1); 00135 lcli2 = S2.check(lcli2); 00136 lcli3 = S3.check(lcli3); 00137 lcli4 = S4.check(lcli4); 00138 lcli5 = S5.check(lcli5); 00139 int i = f(lcli1,lcli2,lcli3,lcli4,lcli5); 00140 if (i == outOfBound) { 00141 outOfBoundData = 0; 00142 return outOfBoundData; 00143 } else { 00144 return v[i]; 00145 } 00146 } 00147 00152 DataRef& operator() ( 00153 const MP_index_exp& lcli1 = MP_index_exp::getEmpty(), 00154 const MP_index_exp& lcli2 = MP_index_exp::getEmpty(), 00155 const MP_index_exp& lcli3 = MP_index_exp::getEmpty(), 00156 const MP_index_exp& lcli4 = MP_index_exp::getEmpty(), 00157 const MP_index_exp& lcli5 = MP_index_exp::getEmpty() 00158 ) { 00159 myrefs.push_back(new DataRef(this, lcli1, lcli2, lcli3, lcli4, lcli5)); 00160 return *myrefs.back(); 00161 } 00162 00163 00165 void display(string s = ""); 00166 protected: 00167 vector<DataRef*> myrefs; 00168 private: 00169 MP_data(const MP_data&); // Forbid copy constructor 00170 MP_data& operator=(const MP_data&); // Forbid assignment 00171 00172 static double outOfBoundData; 00173 00174 MP_index i1,i2,i3,i4,i5; 00175 const MP_set_base &S1,&S2,&S3,&S4,&S5; 00176 double* v; 00177 bool manageData; 00178 }; 00179 00180 class MP_stochastic_data : public MP_data { 00181 public: 00182 MP_stochastic_data(const MP_set_base &s1 = MP_set::getEmpty(), 00183 const MP_set_base &s2 = MP_set::getEmpty(), 00184 const MP_set_base &s3 = MP_set::getEmpty(), 00185 const MP_set_base &s4 = MP_set::getEmpty(), 00186 const MP_set_base &s5 = MP_set::getEmpty()) : 00187 MP_data(s1,s2,s3,s4,s5) {} 00188 00189 using flopc::MP_data::operator(); // From bugsquashing party. Some compiler needs this? 00190 00191 DataRef& operator() ( 00192 const MP_index_exp& lcli1 = MP_index_exp::getEmpty(), 00193 const MP_index_exp& lcli2 = MP_index_exp::getEmpty(), 00194 const MP_index_exp& lcli3 = MP_index_exp::getEmpty(), 00195 const MP_index_exp& lcli4 = MP_index_exp::getEmpty(), 00196 const MP_index_exp& lcli5 = MP_index_exp::getEmpty() 00197 ) { 00198 myrefs.push_back(new DataRef(this, lcli1, lcli2, lcli3, lcli4, lcli5, 1)); 00199 return *myrefs.back(); 00200 } 00201 }; 00202 00203 } // End of namespace flopc 00204 #endif