Blender V2.61 - r43446

tnt_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_ARRAY1D_H
00026 #define TNT_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 Array1D 
00043 {
00044 
00045   private:
00046 
00047       /* ... */
00048     i_refvec<T> v_;
00049     int n_;
00050     T* data_;               /* this normally points to v_.begin(), but
00051                              * could also point to a portion (subvector)
00052                              * of v_.
00053                             */
00054 
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              Array1D();
00065     explicit Array1D(int n);
00066              Array1D(int n, const T &a);
00067              Array1D(int n,  T *a);
00068     inline   Array1D(const Array1D &A);
00069     inline   operator T*();
00070     inline   operator const T*();
00071     inline   Array1D & operator=(const T &a);
00072     inline   Array1D & operator=(const Array1D &A);
00073     inline   Array1D & ref(const Array1D &A);
00074              Array1D copy() const;
00075              Array1D & inject(const Array1D & A);
00076     inline   T& operator[](int i);
00077     inline   const T& operator[](int i) const;
00078     inline   int dim1() const;
00079     inline   int dim() const;
00080               ~Array1D();
00081 
00082 
00083     /* ... extended interface ... */
00084 
00085     inline int ref_count() const;
00086     inline Array1D<T> subarray(int i0, int i1);
00087 
00088 };
00089 
00090 
00091 
00092 
00093 template <class T>
00094 Array1D<T>::Array1D() : v_(), n_(0), data_(0) {}
00095 
00096 template <class T>
00097 Array1D<T>::Array1D(const Array1D<T> &A) : v_(A.v_),  n_(A.n_), 
00098         data_(A.data_)
00099 {
00100 #ifdef TNT_DEBUG
00101     std::cout << "Created Array1D(const Array1D<T> &A) \n";
00102 #endif
00103 
00104 }
00105 
00106 
00107 template <class T>
00108 Array1D<T>::Array1D(int n) : v_(n), n_(n), data_(v_.begin())
00109 {
00110 #ifdef TNT_DEBUG
00111     std::cout << "Created Array1D(int n) \n";
00112 #endif
00113 }
00114 
00115 template <class T>
00116 Array1D<T>::Array1D(int n, const T &val) : v_(n), n_(n), data_(v_.begin()) 
00117 {
00118 #ifdef TNT_DEBUG
00119     std::cout << "Created Array1D(int n, const T& val) \n";
00120 #endif
00121     set_(data_, data_+ n, val);
00122 
00123 }
00124 
00125 template <class T>
00126 Array1D<T>::Array1D(int n, T *a) : v_(a), n_(n) , data_(v_.begin())
00127 {
00128 #ifdef TNT_DEBUG
00129     std::cout << "Created Array1D(int n, T* a) \n";
00130 #endif
00131 }
00132 
00133 template <class T>
00134 inline Array1D<T>::operator T*()
00135 {
00136     return &(v_[0]);
00137 }
00138 
00139 
00140 template <class T>
00141 inline Array1D<T>::operator const T*()
00142 {
00143     return &(v_[0]);
00144 }
00145 
00146 
00147 
00148 template <class T>
00149 inline T& Array1D<T>::operator[](int i) 
00150 { 
00151 #ifdef TNT_BOUNDS_CHECK
00152     assert(i>= 0);
00153     assert(i < n_);
00154 #endif
00155     return data_[i]; 
00156 }
00157 
00158 template <class T>
00159 inline const T& Array1D<T>::operator[](int i) const 
00160 { 
00161 #ifdef TNT_BOUNDS_CHECK
00162     assert(i>= 0);
00163     assert(i < n_);
00164 #endif
00165     return data_[i]; 
00166 }
00167 
00168 
00169     
00170 
00171 template <class T>
00172 Array1D<T> & Array1D<T>::operator=(const T &a)
00173 {
00174     set_(data_, data_+n_, a);
00175     return *this;
00176 }
00177 
00178 template <class T>
00179 Array1D<T> Array1D<T>::copy() const
00180 {
00181     Array1D A( n_);
00182     copy_(A.data_, data_, n_);
00183 
00184     return A;
00185 }
00186 
00187 
00188 template <class T>
00189 Array1D<T> & Array1D<T>::inject(const Array1D &A)
00190 {
00191     if (A.n_ == n_)
00192         copy_(data_, A.data_, n_);
00193 
00194     return *this;
00195 }
00196 
00197 
00198 
00199 
00200 
00201 template <class T>
00202 Array1D<T> & Array1D<T>::ref(const Array1D<T> &A)
00203 {
00204     if (this != &A)
00205     {
00206         v_ = A.v_;      /* operator= handles the reference counting. */
00207         n_ = A.n_;
00208         data_ = A.data_; 
00209         
00210     }
00211     return *this;
00212 }
00213 
00214 template <class T>
00215 Array1D<T> & Array1D<T>::operator=(const Array1D<T> &A)
00216 {
00217     return ref(A);
00218 }
00219 
00220 template <class T>
00221 inline int Array1D<T>::dim1() const { return n_; }
00222 
00223 template <class T>
00224 inline int Array1D<T>::dim() const { return n_; }
00225 
00226 template <class T>
00227 Array1D<T>::~Array1D() {}
00228 
00229 
00230 /* ............................ exented interface ......................*/
00231 
00232 template <class T>
00233 inline int Array1D<T>::ref_count() const
00234 {
00235     return v_.ref_count();
00236 }
00237 
00238 template <class T>
00239 inline Array1D<T> Array1D<T>::subarray(int i0, int i1)
00240 {
00241     if ((i0 > 0) && (i1 < n_) || (i0 <= i1))
00242     {
00243         Array1D<T> X(*this);  /* create a new instance of this array. */
00244         X.n_ = i1-i0+1;
00245         X.data_ += i0;
00246 
00247         return X;
00248     }
00249     else
00250     {
00251         return Array1D<T>();
00252     }
00253 }
00254 
00255 
00256 /* private internal functions */
00257 
00258 
00259 template <class T>
00260 void Array1D<T>::set_(T* begin, T* end, const T& a)
00261 {
00262     for (T* p=begin; p<end; p++)
00263         *p = a;
00264 
00265 }
00266 
00267 template <class T>
00268 void Array1D<T>::copy_(T* p, const T* q, int len) const
00269 {
00270     T *end = p + len;
00271     while (p<end )
00272         *p++ = *q++;
00273 
00274 }
00275 
00276 
00277 } /* namespace TNT */
00278 
00279 #endif
00280 /* TNT_ARRAY1D_H */
00281