Blender V2.61 - r43446

tnt_fortran_array1d.h

Go to the documentation of this file.
00001 
00004 /*
00005 *
00006 * Template Numerical Toolkit (TNT)
00007 *
00008 * Mathematical and Computational Sciences Division
00009 * National Institute of Technology,
00010 * Gaithersburg, MD USA
00011 *
00012 *
00013 * This software was developed at the National Institute of Standards and
00014 * Technology (NIST) by employees of the Federal Government in the course
00015 * of their official duties. Pursuant to title 17 Section 105 of the
00016 * United States Code, this software is not subject to copyright protection
00017 * and is in the public domain. NIST assumes no responsibility whatsoever for
00018 * its use by other parties, and makes no guarantees, expressed or implied,
00019 * about its quality, reliability, or any other characteristic.
00020 *
00021 */
00022 
00023 
00024 
00025 #ifndef TNT_FORTRAN_ARRAY1D_H
00026 #define TNT_FORTRAN_ARRAY1D_H
00027 
00028 #include <cstdlib>
00029 #include <iostream>
00030 
00031 #ifdef TNT_BOUNDS_CHECK
00032 #include <assert.h>
00033 #endif
00034 
00035 
00036 #include "tnt_i_refvec.h"
00037 
00038 namespace TNT
00039 {
00040 
00041 template <class T>
00042 class Fortran_Array1D 
00043 {
00044 
00045   private:
00046 
00047     i_refvec<T> v_;
00048     int n_;
00049     T* data_;               /* this normally points to v_.begin(), but
00050                              * could also point to a portion (subvector)
00051                              * of v_.
00052                             */
00053 
00054     void initialize_(int n);
00055     void copy_(T* p, const T*  q, int len) const;
00056     void set_(T* begin,  T* end, const T& val);
00057  
00058 
00059   public:
00060 
00061     typedef         T   value_type;
00062 
00063 
00064              Fortran_Array1D();
00065     explicit Fortran_Array1D(int n);
00066              Fortran_Array1D(int n, const T &a);
00067              Fortran_Array1D(int n,  T *a);
00068     inline   Fortran_Array1D(const Fortran_Array1D &A);
00069     inline   Fortran_Array1D & operator=(const T &a);
00070     inline   Fortran_Array1D & operator=(const Fortran_Array1D &A);
00071     inline   Fortran_Array1D & ref(const Fortran_Array1D &A);
00072              Fortran_Array1D copy() const;
00073              Fortran_Array1D & inject(const Fortran_Array1D & A);
00074     inline   T& operator()(int i);
00075     inline   const T& operator()(int i) const;
00076     inline   int dim1() const;
00077     inline   int dim() const;
00078               ~Fortran_Array1D();
00079 
00080 
00081     /* ... extended interface ... */
00082 
00083     inline int ref_count() const;
00084     inline Fortran_Array1D<T> subarray(int i0, int i1);
00085 
00086 };
00087 
00088 
00089 
00090 
00091 template <class T>
00092 Fortran_Array1D<T>::Fortran_Array1D() : v_(), n_(0), data_(0) {}
00093 
00094 template <class T>
00095 Fortran_Array1D<T>::Fortran_Array1D(const Fortran_Array1D<T> &A) : v_(A.v_),  n_(A.n_), 
00096         data_(A.data_)
00097 {
00098 #ifdef TNT_DEBUG
00099     std::cout << "Created Fortran_Array1D(const Fortran_Array1D<T> &A) \n";
00100 #endif
00101 
00102 }
00103 
00104 
00105 template <class T>
00106 Fortran_Array1D<T>::Fortran_Array1D(int n) : v_(n), n_(n), data_(v_.begin())
00107 {
00108 #ifdef TNT_DEBUG
00109     std::cout << "Created Fortran_Array1D(int n) \n";
00110 #endif
00111 }
00112 
00113 template <class T>
00114 Fortran_Array1D<T>::Fortran_Array1D(int n, const T &val) : v_(n), n_(n), data_(v_.begin()) 
00115 {
00116 #ifdef TNT_DEBUG
00117     std::cout << "Created Fortran_Array1D(int n, const T& val) \n";
00118 #endif
00119     set_(data_, data_+ n, val);
00120 
00121 }
00122 
00123 template <class T>
00124 Fortran_Array1D<T>::Fortran_Array1D(int n, T *a) : v_(a), n_(n) , data_(v_.begin())
00125 {
00126 #ifdef TNT_DEBUG
00127     std::cout << "Created Fortran_Array1D(int n, T* a) \n";
00128 #endif
00129 }
00130 
00131 template <class T>
00132 inline T& Fortran_Array1D<T>::operator()(int i) 
00133 { 
00134 #ifdef TNT_BOUNDS_CHECK
00135     assert(i>= 1);
00136     assert(i <= n_);
00137 #endif
00138     return data_[i-1]; 
00139 }
00140 
00141 template <class T>
00142 inline const T& Fortran_Array1D<T>::operator()(int i) const 
00143 { 
00144 #ifdef TNT_BOUNDS_CHECK
00145     assert(i>= 1);
00146     assert(i <= n_);
00147 #endif
00148     return data_[i-1]; 
00149 }
00150 
00151 
00152     
00153 
00154 template <class T>
00155 Fortran_Array1D<T> & Fortran_Array1D<T>::operator=(const T &a)
00156 {
00157     set_(data_, data_+n_, a);
00158     return *this;
00159 }
00160 
00161 template <class T>
00162 Fortran_Array1D<T> Fortran_Array1D<T>::copy() const
00163 {
00164     Fortran_Array1D A( n_);
00165     copy_(A.data_, data_, n_);
00166 
00167     return A;
00168 }
00169 
00170 
00171 template <class T>
00172 Fortran_Array1D<T> & Fortran_Array1D<T>::inject(const Fortran_Array1D &A)
00173 {
00174     if (A.n_ == n_)
00175         copy_(data_, A.data_, n_);
00176 
00177     return *this;
00178 }
00179 
00180 
00181 
00182 
00183 
00184 template <class T>
00185 Fortran_Array1D<T> & Fortran_Array1D<T>::ref(const Fortran_Array1D<T> &A)
00186 {
00187     if (this != &A)
00188     {
00189         v_ = A.v_;      /* operator= handles the reference counting. */
00190         n_ = A.n_;
00191         data_ = A.data_; 
00192         
00193     }
00194     return *this;
00195 }
00196 
00197 template <class T>
00198 Fortran_Array1D<T> & Fortran_Array1D<T>::operator=(const Fortran_Array1D<T> &A)
00199 {
00200     return ref(A);
00201 }
00202 
00203 template <class T>
00204 inline int Fortran_Array1D<T>::dim1() const { return n_; }
00205 
00206 template <class T>
00207 inline int Fortran_Array1D<T>::dim() const { return n_; }
00208 
00209 template <class T>
00210 Fortran_Array1D<T>::~Fortran_Array1D() {}
00211 
00212 
00213 /* ............................ exented interface ......................*/
00214 
00215 template <class T>
00216 inline int Fortran_Array1D<T>::ref_count() const
00217 {
00218     return v_.ref_count();
00219 }
00220 
00221 template <class T>
00222 inline Fortran_Array1D<T> Fortran_Array1D<T>::subarray(int i0, int i1)
00223 {
00224 #ifdef TNT_DEBUG
00225         std::cout << "entered subarray. \n";
00226 #endif
00227     if ((i0 > 0) && (i1 < n_) || (i0 <= i1))
00228     {
00229         Fortran_Array1D<T> X(*this);  /* create a new instance of this array. */
00230         X.n_ = i1-i0+1;
00231         X.data_ += i0;
00232 
00233         return X;
00234     }
00235     else
00236     {
00237 #ifdef TNT_DEBUG
00238         std::cout << "subarray:  null return.\n";
00239 #endif
00240         return Fortran_Array1D<T>();
00241     }
00242 }
00243 
00244 
00245 /* private internal functions */
00246 
00247 
00248 template <class T>
00249 void Fortran_Array1D<T>::set_(T* begin, T* end, const T& a)
00250 {
00251     for (T* p=begin; p<end; p++)
00252         *p = a;
00253 
00254 }
00255 
00256 template <class T>
00257 void Fortran_Array1D<T>::copy_(T* p, const T* q, int len) const
00258 {
00259     T *end = p + len;
00260     while (p<end )
00261         *p++ = *q++;
00262 
00263 }
00264 
00265 
00266 } /* namespace TNT */
00267 
00268 #endif
00269 /* TNT_FORTRAN_ARRAY1D_H */
00270