00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _plDataValues_h_
00019 #define _plDataValues_h_
00020
00021
00022 #include <plData.h>
00023
00024 #ifdef macintosh
00025 #include <sys/malloc.h>
00026 #else
00027 #include <malloc.h>
00028 #endif
00029
00030
00031 #ifdef PL_USE_ATHAPASCAN
00032 #include <athapascan-1>
00033 #endif
00034
00035
00036 #define THIERRY_DV
00037 #define PL_USE_ALLOCA
00038
00039
00040
00041
00042
00043
00044
00045 class kplVariable;
00046
00047
00048
00049
00050
00051
00052
00053 #ifndef RAVI_INTERFACE
00054 #ifdef THIERRY_DV
00055
00056 #ifndef PL_USE_ALLOCA
00057 #define PL_DEF_LOCAL_PL_DATA_VALUE(name, theSize) \
00058 plData pt_values##name[theSize]; \
00059 plDataValues name((theSize), pt_values##name, true);
00060 #define PL_DEF_LOCAL_PL_DATA_VALUE_WITH_TYPE(name, theSize, theType) \
00061 plData pt_values##name[(theSize)]; \
00062 plDataValues name((theSize), pt_values##name, theType, true);
00063 #define PL_DEF_LOCAL_PL_DATA_VALUE_WITH_VAR_LIST(name, theVarList) \
00064 plData pt_values##name[(theVarList).size()]; \
00065 plDataValues name((theVarList), pt_values##name, true);
00066
00067 #else
00068
00069 #define PL_DEF_LOCAL_PL_DATA_VALUE(name, theSize) \
00070 plData* pt_values##name = (plData*)alloca( sizeof(plData) * (theSize)); \
00071 plDataValues name((theSize), pt_values##name, true);
00072 #define PL_DEF_LOCAL_PL_DATA_VALUE_WITH_TYPE(name, theSize, theType) \
00073 plData* pt_values##name = (plData*)alloca( sizeof(plData) * (theSize)); \
00074 plDataValues name((theSize), pt_values##name, (theType), true);
00075 #define PL_DEF_LOCAL_PL_DATA_VALUE_WITH_VAR_LIST(name, theVarList) \
00076 plData* pt_values##name = (plData*)alloca( sizeof(plData) * (theVarList).size()); \
00077 plDataValues name((theVarList), pt_values##name, true);
00078
00079 #endif //PL_USE_ALLOCA
00080
00081 #else
00082 #define PL_DEF_LOCAL_PL_DATA_VALUE(name, theSize) \
00083 plDataValues name;\
00084 name.resize(theSize);
00085 #define PL_DEF_LOCAL_PL_DATA_VALUE_WITH_TYPE(name, theSize, theType) \
00086 plDataValues name((theSize), (theType));
00087 #define PL_DEF_LOCAL_PL_DATA_VALUE_WITH_VAR_LIST(name, theVarList) \
00088 plDataValues name((theVarList), NULL, false);
00089 #endif
00090
00091
00092
00093 class plDataValues
00094 {
00095 protected:
00096
00098 void Output(ostream &out) const;
00099
00100
00103 void set(const list <kplVariable*> &variables_list);
00104
00105 public:
00106
00107
00108 inline plDataValues()
00109 #ifdef THIERRY_DV
00110 : v(0), onstack(false), sz(0), psz(0)
00111 #else
00112 : v()
00113 #endif
00114 {
00115 #ifdef TRACE_DV
00116 cout << "plDataValues()" << endl;
00117 #endif
00118 }
00119
00120
00125 inline plDataValues( unsigned int sizev, plData* ptr, bool onstackv )
00126 #ifdef THIERRY_DV
00127 : v(ptr), onstack(onstackv), sz(sizev), psz(sizev)
00128 #else
00129 : v( sizev )
00130 #endif
00131 {
00132 #ifdef TRACE_DV
00133 cout << "plDataValues(unsigned int size, plData* ptr, bool stack): size = " << sizev << endl;
00134 #endif
00135
00136 #ifdef THIERRY_DV
00137
00138 unsigned int i;
00139
00140 if ((ptr ==0) && (sizev >0)) {
00141 v = (plData*)malloc( sizeof(plData)*sizev);
00142 onstack = false;
00143 for (i=0; i<sizev; ++i)
00144 new (&v[i]) plData();
00145 }
00146 #ifdef PL_USE_ALLOCA
00147 if (ptr !=0) {
00148 for (i=0; i<sizev; ++i)
00149 new (&v[i]) plData();
00150 }
00151 #endif
00152
00153 #else
00154 #endif
00155 }
00156
00157
00158 inline plDataValues( unsigned int sizev)
00159 #ifdef THIERRY_DV
00160 :sz(sizev), psz(sizev)
00161 #else
00162 : v(sizev)
00163 #endif
00164 {
00165 #ifdef TRACE_DV
00166 cout << ":plDataValues(unsigned int sizev)" << endl;
00167 #endif
00168
00169 #ifdef THIERRY_DV
00170 v = (plData*)malloc( sizeof(plData)*sizev);
00171 onstack = false;
00172 for (unsigned int i=0; i<sizev; ++i)
00173 new (&v[i]) plData();
00174 #endif
00175 }
00176
00177
00180 inline plDataValues(unsigned int sizev, plData::plData_type the_type)
00181 #ifdef THIERRY_DV
00182 :sz(sizev), psz(sizev)
00183 {
00184 v = (plData*)malloc( sizeof(plData)*sizev);
00185 onstack = false;
00186 for (unsigned int i=0; i<sizev; ++i){
00187 new (&v[i]) plData();
00188 v[i].data_type = the_type;
00189 }
00190
00191 }
00192 #else
00193 {
00194 unsigned int i;
00195 resize(sizev);
00196
00197 for(i=0;i<sizev;i++){
00198 v[i].data_type = the_type;
00199 }
00200
00201 }
00202 #endif
00203
00204
00205 inline plDataValues(unsigned int sizev, plData* ptr, plData::plData_type the_type,
00206 bool stack)
00207 #ifdef THIERRY_DV
00208 : v(ptr), onstack(stack), sz(sizev), psz(sizev)
00209 #else
00210 : v( sizev )
00211 #endif
00212 {
00213 unsigned int i;
00214
00215 #ifdef TRACE_DV
00216 cout << "plDataValues(unsigned int sizev, plData* ptr, plData_type the_type, bool stack)" << endl;
00217 #endif
00218
00219 #ifdef THIERRY_DV
00220 if ((ptr ==0) && (sizev >0))
00221 {
00222 v = (plData*)malloc( sizeof(plData)*sizev);
00223 onstack = false;
00224 for (i=0; i<sizev; ++i){
00225 new (&v[i]) plData();
00226 v[i].data_type = the_type;
00227 }
00228 }
00229 if (ptr !=0){
00230 for (i=0; i<sizev; ++i){
00231 #ifdef PL_USE_ALLOCA
00232 new (&v[i]) plData();
00233 #endif
00234 v[i].data_type = the_type;
00235 }
00236 }
00237 #else
00238 #endif
00239 }
00240
00241
00244 plDataValues(const list <kplVariable*> &variables_list);
00245
00246
00247
00248 plDataValues(const list <kplVariable*> &variables_list,
00249 plData* ptr, bool stack);
00250
00251
00252
00254 inline ~plDataValues()
00255 {
00256 #ifdef THIERRY_DV
00257 if ((v !=0) && (!onstack)) free(v);
00258 #endif
00259 }
00260
00261
00262 #if 1
00263 inline plDataValues(const plDataValues& pd )
00264 #ifdef THIERRY_DV
00265 : v(0), onstack(false), sz(pd.sz), psz(pd.sz)
00266 {
00267 #ifdef TRACE_DV
00268 cout << "plDataValues(const plDataValues& pd )" << endl;
00269 #endif
00270
00271 if (sz > 0) {
00272 v = (plData*)malloc( sizeof(plData)*sz);
00273 unsigned int i=0;
00274 for (; i<sz; ++i)
00275 new (&v[i]) plData(pd.v[i]);
00276 }
00277 }
00278 #else
00279 : v(pd.v)
00280 {
00281 }
00282 #endif
00283
00284
00285 inline plDataValues& operator=(const plDataValues& pd )
00286 {
00287 #ifdef TRACE_DV
00288 cout << "operator= size() = " << size() << " pd.size() = " << pd.size() << endl;
00289 #endif
00290
00291 #ifdef THIERRY_DV
00292 if (psz < pd.sz) {
00293 if ((v !=0) && (!onstack))
00294 free(v);
00295 v = (plData*)malloc( sizeof(plData)*pd.sz);
00296 onstack = false;
00297 psz = sz = pd.sz;
00298 for (unsigned int i=0; i<sz; ++i)
00299 new (&v[i]) plData(pd.v[i]);
00300
00301 }
00302 else {
00303
00304
00305 sz = pd.sz;
00306 memcpy(v,
00307 pd.v,
00308 sz*sizeof(plData));
00309 }
00310 #else
00311 v = pd.v;
00312 #endif
00313 return *this;
00314 }
00315 #endif
00316
00317
00320 inline void deliver_values(const vector <pair <int,int> > &key_box,
00321 plDataValues &addressee_value) const
00322 {
00323 unsigned int i;
00324 for(i=0;i<key_box.size();i++){
00325
00326
00327 addressee_value.v[ key_box[i].first] = v[key_box[i].second];
00328 }
00329
00330 }
00331
00332
00334 #ifdef THIERRY_DV
00335 plData* v;
00336 public:
00337 bool onstack;
00338 size_t sz;
00339 size_t psz;
00340 public:
00341 #else
00342 vector <plData> v;
00343 #endif
00344
00345
00347 inline void resize(const int &new_size)
00348 {
00349 #ifdef TRACE_DV
00350 cout << "plDataValues::resize(const int &new_size)" << endl;
00351 #endif
00352
00353 #ifdef THIERRY_DV
00354 size_t ns = (size_t)new_size;
00355 if (psz >= ns) {
00356 sz = ns; return;
00357 }
00358 plData* newv = (plData*)malloc( sizeof(plData)*ns);
00359 unsigned int i;
00360 if (sz >0) {
00361 for (i=0; i<sz; ++i)
00362 new (&newv[i]) plData(v[i]);
00363 for (; i<ns; ++i)
00364 new (&newv[i]) plData();
00365 }
00366 else {
00367 for (i=0; i<ns; ++i)
00368 new (&newv[i]) plData();
00369 }
00370 if ((v !=0) && (!onstack))
00371 free(v);
00372 v = newv;
00373 onstack = false;
00374 psz = sz = ns;
00375 #else
00376 v.resize(new_size);
00377 #endif
00378 }
00379
00380
00382 inline unsigned int size() const
00383 {
00384 #ifdef THIERRY_DV
00385 return sz;
00386 #else
00387 return(v.size());
00388 #endif
00389 }
00390
00391
00393 inline bool operator==(const plDataValues &value) const
00394 {
00397 if (size() != value.size())
00398 return(false);
00399
00400 unsigned int i;
00401 unsigned int values_size = size();
00402
00403 for(i=0;i<values_size;i++)
00404 {
00405 if (! (v[i] == value.v[i])){
00406 return false;
00407 }
00408 }
00409 return(true);
00410 }
00411
00412
00413 inline bool operator!=(const plDataValues &value) const
00414 {
00415 return !(*this == value);
00416 }
00417
00418
00420 inline plData& operator[](unsigned int i){return v[i];}
00421 inline const plData& operator[](unsigned int i) const {return v[i];}
00422
00423 inline bool operator < (const plDataValues &b ) const
00424 {
00425 unsigned int i = 0;
00426 while(i < size()){
00427 if (v[i] < b.v[i]) {return true;}
00428 if (v[i] > b.v[i]) {return false;}
00429 i++ ;
00430 }
00431 return false;
00432 }
00433
00434 inline bool operator >= (const plDataValues &b ) const {return !(*this < b);}
00435
00436 inline bool operator > (const plDataValues &b ) const
00437 {
00438 unsigned int i = 0;
00439 while(i < size()){
00440 if (v[i] > b.v[i]) {return true;}
00441 if (v[i] < b.v[i]) {return false;}
00442 i++ ;
00443 }
00444 return false;
00445 }
00446
00447 inline bool operator <= (const plDataValues &b ) const {return !(*this > b);}
00448
00449
00450
00452 friend inline void concatenate (plDataValues &result,
00453 const plDataValues &Values1,
00454 const plDataValues &Values2)
00455 {
00456 #ifdef THIERRY_DV
00457 unsigned int values1_size = Values1.sz*sizeof(plData);
00458 unsigned int values2_size = Values2.sz*sizeof(plData);
00459
00460 memcpy(result.v,
00461 Values1.v,
00462 values1_size);
00463 memcpy(result.v + Values1.sz,
00464 Values2.v,
00465 values2_size);
00466
00467 #else
00468 result.v = Values1.v;
00469 result.v.insert(result.v.end(),
00470 Values2.v.begin(),
00471 Values2.v.end());
00472 #endif
00473 }
00474
00475
00476
00477 inline void extract(plDataValues &result, unsigned int start, unsigned int n)const
00478 {
00479 #ifdef THIERRY_DV
00480 memcpy(result.v,
00481 v+start,
00482 n*sizeof(plData));
00483 #else
00484 for(unsigned int i = 0; i < n; i++)
00485 result.v[i] = v[start+i];
00486 #endif
00487 }
00488
00490 friend class plComputableObject;
00491 friend class kplGibbsSampler;
00492 friend class kplMCGenerator;
00493
00494 friend ostream &operator<<(ostream &out, const plDataValues &data){
00495 data.Output(out);
00496 return out;
00497 }
00498
00499 #ifdef PL_USE_ATHAPASCAN
00500 friend a1::OStream& operator<< ( a1::OStream& o, const plDataValues& pv );
00501 friend a1::IStream& operator>> ( a1::IStream& o, plDataValues& pv );
00502
00503 #endif
00504
00505 };
00506
00507 #endif
00508
00509 #endif