Blender V2.61 - r43446

tnt_fortran_array2d.h

Go to the documentation of this file.
00001 
00004 /*
00005 *
00006 * Template Numerical Toolkit (TNT): Two-dimensional Fortran numerical array
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_ARRAY2D_H
00026 #define TNT_FORTRAN_ARRAY2D_H
00027 
00028 #include <cstdlib>
00029 #include <iostream>
00030 
00031 #ifdef TNT_BOUNDS_CHECK
00032 #include <assert.h>
00033 #endif
00034 
00035 #include "tnt_i_refvec.h"
00036 
00037 namespace TNT
00038 {
00039 
00040 template <class T>
00041 class Fortran_Array2D 
00042 {
00043 
00044 
00045   private: 
00046         i_refvec<T> v_;
00047         int m_;
00048         int n_;
00049         T* data_;
00050 
00051 
00052         void initialize_(int n);
00053         void copy_(T* p, const T*  q, int len);
00054         void set_(T* begin,  T* end, const T& val);
00055  
00056   public:
00057 
00058     typedef         T   value_type;
00059 
00060            Fortran_Array2D();
00061            Fortran_Array2D(int m, int n);
00062            Fortran_Array2D(int m, int n,  T *a);
00063            Fortran_Array2D(int m, int n, const T &a);
00064     inline Fortran_Array2D(const Fortran_Array2D &A);
00065     inline Fortran_Array2D & operator=(const T &a);
00066     inline Fortran_Array2D & operator=(const Fortran_Array2D &A);
00067     inline Fortran_Array2D & ref(const Fortran_Array2D &A);
00068            Fortran_Array2D copy() const;
00069            Fortran_Array2D & inject(const Fortran_Array2D & A);
00070     inline T& operator()(int i, int j);
00071     inline const T& operator()(int i, int j) const ;
00072     inline int dim1() const;
00073     inline int dim2() const;
00074                ~Fortran_Array2D();
00075 
00076     /* extended interface */
00077 
00078     inline int ref_count() const;
00079 
00080 };
00081 
00082 template <class T>
00083 Fortran_Array2D<T>::Fortran_Array2D() : v_(), m_(0), n_(0), data_(0) {}
00084 
00085 
00086 template <class T>
00087 Fortran_Array2D<T>::Fortran_Array2D(const Fortran_Array2D<T> &A) : v_(A.v_),
00088         m_(A.m_), n_(A.n_), data_(A.data_) {}
00089 
00090 
00091 
00092 template <class T>
00093 Fortran_Array2D<T>::Fortran_Array2D(int m, int n) : v_(m*n), m_(m), n_(n),
00094     data_(v_.begin()) {}
00095 
00096 template <class T>
00097 Fortran_Array2D<T>::Fortran_Array2D(int m, int n, const T &val) : 
00098     v_(m*n), m_(m), n_(n), data_(v_.begin())
00099 {
00100     set_(data_, data_+m*n, val);
00101 }
00102 
00103 
00104 template <class T>
00105 Fortran_Array2D<T>::Fortran_Array2D(int m, int n, T *a) : v_(a),
00106     m_(m), n_(n), data_(v_.begin()) {}
00107 
00108 
00109 
00110 
00111 template <class T>
00112 inline T& Fortran_Array2D<T>::operator()(int i, int j) 
00113 { 
00114 #ifdef TNT_BOUNDS_CHECK
00115     assert(i >= 1);
00116     assert(i <= m_);
00117     assert(j >= 1);
00118     assert(j <= n_);
00119 #endif
00120 
00121     return v_[ (j-1)*m_ + (i-1) ];
00122 
00123 }
00124 
00125 template <class T>
00126 inline const T& Fortran_Array2D<T>::operator()(int i, int j) const
00127 { 
00128 #ifdef TNT_BOUNDS_CHECK
00129     assert(i >= 1);
00130     assert(i <= m_);
00131     assert(j >= 1);
00132     assert(j <= n_);
00133 #endif
00134 
00135     return v_[ (j-1)*m_ + (i-1) ];
00136 
00137 }
00138 
00139 
00140 template <class T>
00141 Fortran_Array2D<T> & Fortran_Array2D<T>::operator=(const T &a)
00142 {
00143     set_(data_, data_+m_*n_, a);
00144     return *this;
00145 }
00146 
00147 template <class T>
00148 Fortran_Array2D<T> Fortran_Array2D<T>::copy() const
00149 {
00150 
00151     Fortran_Array2D B(m_,n_);
00152     
00153     B.inject(*this);
00154     return B;
00155 }
00156 
00157 
00158 template <class T>
00159 Fortran_Array2D<T> & Fortran_Array2D<T>::inject(const Fortran_Array2D &A)
00160 {
00161     if (m_ == A.m_ && n_ == A.n_)
00162         copy_(data_, A.data_, m_*n_);
00163 
00164     return *this;
00165 }
00166 
00167 
00168 
00169 template <class T>
00170 Fortran_Array2D<T> & Fortran_Array2D<T>::ref(const Fortran_Array2D<T> &A)
00171 {
00172     if (this != &A)
00173     {
00174         v_ = A.v_;
00175         m_ = A.m_;
00176         n_ = A.n_;
00177         data_ = A.data_;
00178     }
00179     return *this;
00180 }
00181 
00182 template <class T>
00183 Fortran_Array2D<T> & Fortran_Array2D<T>::operator=(const Fortran_Array2D<T> &A)
00184 {
00185     return ref(A);
00186 }
00187 
00188 template <class T>
00189 inline int Fortran_Array2D<T>::dim1() const { return m_; }
00190 
00191 template <class T>
00192 inline int Fortran_Array2D<T>::dim2() const { return n_; }
00193 
00194 
00195 template <class T>
00196 Fortran_Array2D<T>::~Fortran_Array2D()
00197 {
00198 }
00199 
00200 template <class T>
00201 inline int Fortran_Array2D<T>::ref_count() const { return v_.ref_count(); }
00202 
00203 
00204 
00205 
00206 template <class T>
00207 void Fortran_Array2D<T>::set_(T* begin, T* end, const T& a)
00208 {
00209     for (T* p=begin; p<end; p++)
00210         *p = a;
00211 
00212 }
00213 
00214 template <class T>
00215 void Fortran_Array2D<T>::copy_(T* p, const T* q, int len) 
00216 {
00217     T *end = p + len;
00218     while (p<end )
00219         *p++ = *q++;
00220 
00221 }
00222 
00223 
00224 } /* namespace TNT */
00225 
00226 #endif
00227 /* TNT_FORTRAN_ARRAY2D_H */
00228