Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

toolbox/stiffArray.hh
Go to the documentation of this file.
00001 /* Stiff array which suites for applications where the 
00002    length is important to have as a template parameter,
00003    e.g. for MultiIndex
00004  */
00005 
00006 #ifndef stiffArray_hh
00007 #define stiffArray_hh
00008 
00009 #include "toolbox/array.hh"
00010 #include "basics/debug.hh"
00011 
00012 #define StiffArrayConstr_D 0
00013 #define StiffArrayDestr_D 0
00014 
00015 namespace concepts {
00016 
00017   // *********************************************************** StiffArray ** 
00018 
00026   template<uint dim, class F>
00027   class StiffArray : public OutputOperator {
00028   public:
00030     StiffArray() : data_(new F[dim]) {}
00035     StiffArray(const F& dft) : data_(new F[dim]) { *this = dft; }
00040     StiffArray(const Array<F>& dft) : data_(new F[dim]) {
00041       memorycpy(data_, (const F*)dft, dim);
00042       conceptsAssert(dft.size() == dim, Assertion());
00043     }
00049     template<class H>
00050     StiffArray(const StiffArray<dim,H>& a, F fnc(const H&)) 
00051       : data_(new F[dim]) {
00052       F* d = data_; const H* e = (const H*)a; 
00053       for(uint i = dim; i--;) *d++ = fnc(*e++);
00054     }
00060     template<class H>
00061     StiffArray(const StiffArray<dim,H>& a, const F& fnc(const H&)) 
00062       : data_(new F[dim]) {
00063       F* d = data_; const H* e = (const H*)a; 
00064       for(uint i = dim; i--;) *d++ = fnc(*e++);
00065     }
00067     StiffArray(const StiffArray<dim,F>& a) : data_(new F[dim]) {
00068       std::memcpy(data_, a.data_, dim*sizeof(F));
00069     }
00070     ~StiffArray() {
00071       DEBUGL(StiffArrayDestr_D, "data = " << data_);
00072       delete[] data_;
00073       DEBUGL(StiffArrayDestr_D, "done.");
00074     }
00075     uint length() { return dim; }
00077     void zeros() { std::memset(data_, 0, dim*sizeof(F)); }
00079     operator F*() { return data_; }
00081     operator const F*() const { return data_; }
00082 
00083     operator Array<F>() const { return Array<F>(data_,dim); }
00084 
00086     const F& operator[] (const int i) const { 
00087       conceptsAssert3(i < (int)dim, Assertion(),
00088           "i = " << i << ", dim = " << dim);
00089       return data_[i];
00090     }
00092     F& operator[] (const int i) {
00093       conceptsAssert3(i < (int)dim, Assertion(),
00094           "i = " << i << ", dim = " << dim);
00095       return data_[i];
00096     }
00097 
00099     StiffArray<dim,F>& operator*=(const F n) {
00100       F* d = data_; for(uint i = dim; i--;) *d++ *= n;
00101       return *this;
00102     }
00104     StiffArray<dim,F>& operator/=(const F n) {
00105       F* d = data_; for(uint i = dim; i--;) *d++ /= n;
00106       return *this;
00107     }
00109     StiffArray<dim,F>& operator=(const F n) {
00110       F* d = data_; for(uint i = dim; i--;) *d++ = n;
00111       return *this;
00112     }
00114     StiffArray<dim,F>& operator+=(const F n) {
00115       F* d = data_; for(uint i = dim; i--;) *d++ += n;
00116       return *this;
00117     }
00119     StiffArray<dim,F>& operator-=(const F n) {
00120       F* d = data_; for(uint i = dim; i--;) *d++ -= n;
00121       return *this;
00122     }
00124     StiffArray<dim,F>& operator*=(const StiffArray<dim,F>& a) {
00125       F* d = data_; const F* e = (const F*)a;
00126       for(uint i = dim; i--;) *d++ *= *e++;
00127       return *this;
00128     }
00131     StiffArray<dim,F>& apply(F& fnc(F&)) {
00132       F* d = data_; for(uint i = dim; i--;) *d = fnc(*d++);
00133       return *this;
00134     }
00135   protected:
00136     virtual std::ostream& info(std::ostream& os) const;
00137   private:
00139     F* data_;
00140   };
00141 
00142   template<uint dim, class F>
00143   std::ostream& StiffArray<dim, F>::info(std::ostream& os) const {
00144     os << "StiffArray<" << dim << ", F>([";
00145     F* d = data_;
00146     for (uint i = dim; i--;)
00147       os << *d++ << ((i == 0) ? "" : ", ");
00148     return os << "])";
00149   }
00150 
00151   // ****************************************************** StiffArray<1,F> ** 
00152 
00153   template<class F>
00154   class StiffArray<1,F> : public OutputOperator {
00155   public:
00157     StiffArray() {}
00162     StiffArray(const F& dft) : data_(dft) {}
00167     StiffArray(const Array<F>& dft)  {
00168       conceptsAssert(dft.size() == 1, Assertion());
00169       data_ = dft[0];
00170     }
00176     template<class H>
00177     StiffArray(const StiffArray<1,H>& a, F fnc(const H&)) {
00178       data_ = fnc(a);
00179     }      
00181     StiffArray(const StiffArray<1,F>& a) : data_(a.data_) {}
00182     ~StiffArray() {
00183       DEBUGL(StiffArrayDestr_D, "done.");
00184     }
00185     uint length() { return 1; }
00187     void zeros() { data_ = 0; }
00189     operator F() const { return data_; }
00190 
00192     const F& operator[] (const int i) const { 
00193       conceptsAssert3(i == 0, Assertion(), "i = " << i);
00194       return data_;
00195     }
00197     F& operator[] (const int i) {
00198       conceptsAssert3(i == 0, Assertion(), "i = " << i);
00199       return data_;
00200     }
00201 
00203     StiffArray<1,F>& operator*=(const F n) {
00204       data_ *= n; return *this;
00205     }
00207     StiffArray<1,F>& operator/=(const F n) {
00208       data_ /= n; return *this;
00209     }
00211     StiffArray<1,F>& operator=(const F n) {
00212       data_ = n; return *this;
00213     }
00215     StiffArray<1,F>& operator+=(const F n) {
00216       data_ += n; return *this;
00217     }
00219     StiffArray<1,F>& operator-=(const F n) {
00220       data_ -= n; return *this;
00221     }
00224     StiffArray<1,F>& apply(F& fnc(F&)) {
00225       data_ = fnc(data_); return *this;
00226     }
00227   protected:
00228     virtual std::ostream& info(std::ostream& os) const;
00229   private:
00231     F data_;
00232   };
00233 
00234   template<class F>
00235   std::ostream& StiffArray<1, F>::info(std::ostream& os) const {
00236     return os << "StiffArray<1, F>([" << data_ << "])";
00237   }
00238 
00239   // ****************************************************** StiffArray<0,F> ** 
00240 
00245   template<class F>
00246   class StiffArray<0,F> : public OutputOperator {
00247   public:
00248     StiffArray() { throw conceptsException
00249          (concepts::MissingFeature("makes no sense")); }
00250   };
00251 
00252 
00253 } // namespace concepts
00254 
00255 #endif // stiffArray_hh

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)