00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef __RVL_LDC
00034 #define __RVL_LDC
00035
00036 #include "data.hh"
00037
00038 namespace RVL {
00039
00072 template<class DataType>
00073 class LocalEvaluation;
00074
00075 template<class DataType>
00076 class LocalConstEval;
00077
00078 template<class DataType>
00079 class LocalDataContainer: public DataContainer {
00080
00081 public:
00082
00083 LocalDataContainer() {}
00084 LocalDataContainer(const LocalDataContainer<DataType> & D) {}
00085 virtual ~LocalDataContainer() {}
00086
00088 virtual size_t getSize() const = 0;
00089
00091 virtual DataType * getData() = 0;
00092
00094 virtual DataType const * getData() const = 0;
00095
00100 void eval(FunctionObject & f,
00101 vector<DataContainer const *> & x) {
00102 try {
00103 vector<LocalDataContainer<DataType> const *> lx(x.size());
00104 for (size_t i=0;i<x.size();i++) {
00105 if (!(lx[i] = dynamic_cast<LocalDataContainer<DataType> const *>(x[i]))) {
00106 RVLException e;
00107 e<<"Error: LocalDataContainer::eval(FO,...)\n";
00108 e<<"at least one of the input DCs is not an LDC\n";
00109 throw e;
00110 }
00111 }
00112 LocalEvaluation<DataType> & lf
00113 = dynamic_cast<LocalEvaluation<DataType> &>(f);
00114 lf(*this,lx);
00115 }
00116 catch (bad_cast) {
00117 RVLException e;
00118 e<<"Error: LocalDataContainer::eval(FO,...)\n";
00119 e<<"FO is not an LFO\n";
00120 throw e;
00121 }
00122 catch (RVLException & e) {
00123 e<<"\ncalled from LocalDataContainer::eval\n";
00124 throw e;
00125 }
00126 }
00127
00129 void eval(FunctionObjectConstEval & f,
00130 vector<DataContainer const *> & x) const {
00131 try {
00132 vector<LocalDataContainer<DataType> const *> ex(x.size()+1);
00133 ex[0]=this;
00134 for (size_t i=0;i<x.size();i++) {
00135 if (!(ex[i+1] = dynamic_cast<LocalDataContainer<DataType> const *>(x[i]))) {
00136 RVLException e;
00137 e<<"Error: LocalDataContainer::eval(FO,...)\n";
00138 e<<"at least one of the input DCs is not an LDC\n";
00139 throw e;
00140 }
00141 }
00142 LocalConstEval<DataType> & lf
00143 = dynamic_cast<LocalConstEval<DataType> &>(f);
00144 lf(ex);
00145 }
00146 catch (RVLException & e) {
00147 e<<"\ncalled from LocalDataContainer::eval(...) const\n";
00148 throw e;
00149 }
00150 }
00151 };
00152
00156 template<class DataType>
00157 class LocalDataContainerFactory: public DataContainerFactory {
00158
00159 public:
00160
00161 LocalDataContainerFactory() {}
00162 LocalDataContainerFactory( LocalDataContainerFactory<DataType> &) {}
00163 virtual ~LocalDataContainerFactory() {}
00164
00166 virtual LocalDataContainer<DataType> * buildLocal() const = 0;
00167
00168 DataContainer * build() const { return buildLocal(); }
00169
00171 virtual size_t getSize() const = 0;
00172
00173 };
00174
00175
00177 template<class DataType>
00178 class LocalDataContainerSection: public LocalDataContainer<DataType> {
00179
00180 private:
00181 LocalDataContainer<DataType> & src;
00182 size_t begin;
00183 size_t length;
00184
00185 LocalDataContainerSection() {}
00186
00187 public:
00189 LocalDataContainerSection(const LocalDataContainerSection<DataType> & D)
00190 :src(D.src), begin(D.begin), length(D.length) {}
00193 LocalDataContainerSection(LocalDataContainer<DataType> & _src,
00194 size_t _begin,
00195 size_t _length)
00196 : src(_src), begin(_begin), length(_length) {
00197 if (begin+length>src.getSize()) {
00198 RVLException e;
00199 e<<"Error: LocalDataContainerSection constructor\n";
00200 e<<"section overruns end of source LDC\n";
00201 e<<"length of source LDC = "<<src.getSize()<<"\n";
00202 e<<"begin index = "<<begin<<" length of section = "<<length<<"\n";
00203 throw e;
00204 }
00205 }
00206 virtual ~LocalDataContainerSection() {}
00207
00208 size_t getSize() const { return length; }
00209 DataType * getData() { return &((src.getData())[begin]); }
00210 DataType const * getData() const { return &((src.getData())[begin]); }
00211
00212 ostream & write(ostream & str) const {
00213 str<<"LocalDataContainerSection object\n";
00214 str<<"LDC view of section beginning at "<<begin<<" of length "<<length<<" of\n";
00215 str<<"elements:\n";
00216 for (size_t i=0;i<length;i++) {
00217 str<<"index = "<<i<<" value = "<<getData()[i]<<"\n";
00218 }
00219 return str;
00220 }
00221 };
00222
00223 }
00224
00225 #endif