Blender V2.61 - r43446

tnt_fortran_array3d.h

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