00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef _MV_VECTOR_TPL_H_
00030 #define _MV_VECTOR_TPL_H_
00031
00032 #include <stdlib.h>
00033 #ifdef MV_VECTOR_BOUNDS_CHECK
00034 # include <assert.h>
00035 #endif
00036
00037 #include "mvvind.h"
00038 #include "mvvrf.h"
00039
00040 template <class TYPE>
00041 class MV_Vector
00042 {
00043 protected:
00044 TYPE *p_;
00045 int dim_;
00046 int ref_;
00047 public:
00048
00049
00050
00051
00052
00053
00054 MV_Vector();
00055 MV_Vector( int);
00056 MV_Vector( int, const TYPE&);
00057
00058 MV_Vector(TYPE*, int);
00059 MV_Vector(const TYPE*, int);
00060
00061
00062
00063 MV_Vector(TYPE*, int, MV_Vector_::ref_type i);
00064 MV_Vector(const MV_Vector<TYPE>&);
00065 ~MV_Vector();
00066
00067
00068
00069
00070
00071
00072 inline TYPE& operator()( int i)
00073 {
00074 # ifdef MV_VECTOR_BOUNDS_CHECK
00075 assert(i < dim_);
00076 # endif
00077 return p_[i];
00078 }
00079 inline const TYPE& operator()( int i) const
00080 {
00081 # ifdef MV_VECTOR_BOUNDS_CHECK
00082 assert(i < dim_);
00083 # endif
00084 return p_[i];
00085 }
00086
00087 inline TYPE& operator[]( int i)
00088 {
00089 # ifdef MV_VECTOR_BOUNDS_CHECK
00090 assert(i < dim_);
00091 # endif
00092 return p_[i];
00093 }
00094 inline const TYPE& operator[]( int i) const
00095 {
00096 # ifdef MV_VECTOR_BOUNDS_CHECK
00097 assert(i < dim_);
00098 # endif
00099 return p_[i];
00100 }
00101
00102
00103
00104 inline MV_Vector<TYPE> operator()(const MV_VecIndex &I) ;
00105 inline MV_Vector<TYPE> operator()(void);
00106 inline const MV_Vector<TYPE> operator()(void) const;
00107 inline const MV_Vector<TYPE> operator()(const MV_VecIndex &I) const;
00108
00109 inline int size() const { return dim_;}
00110 inline int ref() const { return ref_;}
00111 inline int null() const {return dim_== 0;}
00112
00113
00114 MV_Vector<TYPE> & newsize( int );
00115
00116
00117
00118
00119
00120 MV_Vector<TYPE> & operator=(const MV_Vector<TYPE>&);
00121 MV_Vector<TYPE> & operator=(const TYPE&);
00122
00123
00124
00125 };
00126
00127
00128 template <class TYPE>
00129 MV_Vector<TYPE>::MV_Vector() : p_(0), dim_(0) , ref_(0){};
00130
00131 template <class TYPE>
00132 MV_Vector<TYPE>::MV_Vector( int n) : p_(new TYPE[n]), dim_(n),
00133 ref_(0)
00134 {
00135 if (p_ == NULL)
00136 {
00137 cerr << "Error: NULL pointer in MV_Vector(int) constructor " << endl;
00138 cerr << " Most likely out of memory... " << endl;
00139 exit(1);
00140 }
00141 }
00142
00143 template <class TYPE>
00144 MV_Vector<TYPE>::MV_Vector( int n, const TYPE& v) :
00145 p_(new TYPE[n]), dim_(n), ref_(0)
00146 {
00147 if (p_ == NULL)
00148 {
00149 cerr << "Error: NULL pointer in MV_Vector(int) constructor " << endl;
00150 cerr << " Most likely out of memory... " << endl;
00151 exit(1);
00152 }
00153 for (int i=0; i<n; i++)
00154 p_[i] = v;
00155 }
00156
00157
00158
00159
00160
00161
00162
00163 template <class TYPE>
00164 MV_Vector<TYPE>& MV_Vector<TYPE>::operator=(const TYPE & m)
00165 {
00166 #ifdef TRACE_VEC
00167 cout << "> MV_Vector<TYPE>::operator=(const TYPE & m) " << endl;
00168 #endif
00169
00170
00171
00172 int N = size();
00173
00174 int Nminus4 = N-4;
00175 int i;
00176
00177 for (i=0; i<Nminus4; )
00178 {
00179 p_[i++] = m;
00180 p_[i++] = m;
00181 p_[i++] = m;
00182 p_[i++] = m;
00183 }
00184
00185 for (; i<N; p_[i++] = m);
00186
00187 #ifdef TRACE_VEC
00188 cout << "< MV_Vector<TYPE>::operator=(const TYPE & m) " << endl;
00189 #endif
00190 return *this;
00191 }
00192
00193 template <class TYPE>
00194 MV_Vector<TYPE>& MV_Vector<TYPE>::newsize( int n)
00195 {
00196 #ifdef TRACE_VEC
00197 cout << "> MV_Vector<TYPE>::newsize( int n) " << endl;
00198 #endif
00199 if (ref_ )
00200 {
00201 {
00202 cerr << "MV_Vector::newsize can't operator on references.\n";
00203 exit(1);
00204 }
00205 }
00206 else
00207 if (dim_ != n )
00208 {
00209 if (p_) delete [] p_;
00210 p_ = new TYPE[n];
00211 if (p_ == NULL)
00212 {
00213 cerr << "Error : NULL pointer in operator= " << endl;
00214 exit(1);
00215 }
00216 dim_ = n;
00217 }
00218
00219 #ifdef TRACE_VEC
00220 cout << "< MV_Vector<TYPE>::newsize( int n) " << endl;
00221 #endif
00222
00223 return *this;
00224 }
00225
00226
00227
00228
00229 template <class TYPE>
00230 MV_Vector<TYPE>& MV_Vector<TYPE>::operator=(const MV_Vector<TYPE> & m)
00231 {
00232
00233 int N = m.dim_;
00234 int i;
00235
00236 if (ref_ )
00237 {
00238 if (dim_ != m.dim_)
00239 {
00240 cerr << "MV_VectorRef::operator= non-conformant assignment.\n";
00241 exit(1);
00242 }
00243
00244
00245 if ((m.p_ + m.dim_) >= p_)
00246 {
00247
00248 for (i= N-1; i>=0; i--)
00249 p_[i] = m.p_[i];
00250 }
00251 else
00252 {
00253 for (i=0; i<N; i++)
00254 p_[i] = m.p_[i];
00255 }
00256
00257 }
00258 else
00259 {
00260 newsize(N);
00261
00262
00263 for (i =0; i< N; i++)
00264 p_[i] = m.p_[i];
00265 }
00266 return *this;
00267 }
00268
00269 template <class TYPE>
00270 MV_Vector<TYPE>::MV_Vector(const MV_Vector<TYPE> & m) : p_(new TYPE[m.dim_]),
00271 dim_(m.dim_) , ref_(0)
00272 {
00273 if (p_ == NULL)
00274 {
00275 cerr << "Error: Null pointer in MV_Vector(const MV_Vector&); " << endl;
00276 exit(1);
00277 }
00278
00279 int N = m.dim_;
00280
00281 for (int i=0; i<N; i++)
00282 p_[i] = m.p_[i];
00283 }
00284
00285
00286
00287
00288
00289
00290 template <class TYPE>
00291 MV_Vector<TYPE>::MV_Vector(TYPE* d, int n, MV_Vector_::ref_type i) :
00292 p_(d), dim_(n) , ref_(i) {}
00293
00294 template <class TYPE>
00295 MV_Vector<TYPE>::MV_Vector(TYPE* d, int n) : p_(new TYPE[n]),
00296 dim_(n) , ref_(0)
00297 {
00298 if (p_ == NULL)
00299 {
00300 cerr << "Error: Null pointer in MV_Vector(TYPE*, int) " << endl;
00301 exit(1);
00302 }
00303 for (int i=0; i<n; i++)
00304 p_[i] = d[i];
00305
00306 }
00307
00308
00309 template <class TYPE>
00310 MV_Vector<TYPE>::MV_Vector(const TYPE* d, int n) : p_(new TYPE[n]),
00311 dim_(n) , ref_(0)
00312 {
00313 if (p_ == NULL)
00314 {
00315 cerr << "Error: Null pointer in MV_Vector(TYPE*, int) " << endl;
00316 exit(1);
00317 }
00318 for (int i=0; i<n; i++)
00319 p_[i] = d[i];
00320
00321 }
00322
00323 template <class TYPE>
00324 MV_Vector<TYPE> MV_Vector<TYPE>::operator()(void)
00325 {
00326 return MV_Vector<TYPE>(p_, dim_, MV_Vector_::ref);
00327 }
00328
00329 template <class TYPE>
00330 const MV_Vector<TYPE> MV_Vector<TYPE>::operator()(void) const
00331 {
00332 return MV_Vector<TYPE>(p_, dim_, MV_Vector_::ref);
00333 }
00334
00335 template <class TYPE>
00336 MV_Vector<TYPE> MV_Vector<TYPE>::operator()(const MV_VecIndex &I)
00337 {
00338
00339 if (I.all())
00340 return MV_Vector<TYPE>(p_, dim_, MV_Vector_::ref);
00341 else
00342 {
00343
00344
00345 if ( I.end() >= dim_)
00346 {
00347 cerr << "MV_VecIndex: (" << I.start() << ":" << I.end() <<
00348 ") too big for matrix (0:" << dim_ - 1 << ") " << endl;
00349 exit(1);
00350 }
00351 return MV_Vector<TYPE>(p_+ I.start(), I.end() - I.start() + 1,
00352 MV_Vector_::ref);
00353 }
00354 }
00355
00356 template <class TYPE>
00357 const MV_Vector<TYPE> MV_Vector<TYPE>::operator()(const MV_VecIndex &I) const
00358 {
00359
00360
00361 if ( I.end() >= dim_)
00362 {
00363 cerr << "MV_VecIndex: (" << I.start() << ":" << I.end() <<
00364 ") too big for matrix (0:" << dim_ - 1 << ") " << endl;
00365 exit(1);
00366 }
00367 return MV_Vector<TYPE>(p_+ I.start(), I.end() - I.start() + 1,
00368 MV_Vector_::ref);
00369 }
00370
00371 template <class TYPE>
00372 MV_Vector<TYPE>::~MV_Vector()
00373 {
00374 if (p_ && !ref_ ) delete [] p_;
00375 }
00376
00377
00378 template <class TYPE>
00379 class FMV_Vector : public MV_Vector<TYPE>
00380 {
00381 public:
00382 FMV_Vector( int n) : MV_Vector<TYPE>(n) {}
00383 FMV_Vector<TYPE>& operator=(const FMV_Vector<TYPE>& m);
00384 FMV_Vector<TYPE>& operator=(const TYPE& m);
00385 };
00386
00387 template <class TYPE>
00388 FMV_Vector<TYPE>& FMV_Vector<TYPE>::operator=( const FMV_Vector<TYPE>& m)
00389 {
00390
00391 #ifdef TRACE_VEC
00392 cout << "> FMV_Vector<TYPE>::operator=( const FMV_Vector<TYPE>& m)" << endl;
00393 #endif
00394
00395 int N = m.dim_;
00396
00397
00398
00399 if (ref_ )
00400 {
00401 if (dim_ != m.dim_)
00402 {
00403 cerr << "MV_VectorRef::operator= non-conformant assignment.\n";
00404 exit(1);
00405 }
00406 }
00407 else if ( dim_ != m.dim_ )
00408 newsize(N);
00409
00410 memmove(p_, m.p_, N * sizeof(TYPE));
00411
00412 #ifdef TRACE_VEC
00413 cout << "< FMV_Vector<TYPE>::operator=( const FMV_Vector<TYPE>& m)" << endl;
00414 #endif
00415
00416 return *this;
00417 }
00418
00419 template <class TYPE>
00420 FMV_Vector<TYPE>& FMV_Vector<TYPE>::operator=(const TYPE & m)
00421 {
00422 #ifdef TRACE_VEC
00423 cout << "> FMV_Vector<TYPE>::operator=(const TYPE & m) " << endl;
00424 #endif
00425
00426
00427
00428 int N = size();
00429
00430 int Nminus4 = N-4;
00431 int i;
00432
00433 for (i=0; i<Nminus4; )
00434 {
00435 p_[i++] = m;
00436 p_[i++] = m;
00437 p_[i++] = m;
00438 p_[i++] = m;
00439 }
00440
00441 for (; i<N; p_[i++] = m);
00442
00443 #ifdef TRACE_VEC
00444 cout << "< FMV_Vector<TYPE>::operator=(const TYPE & m) " << endl;
00445 #endif
00446 return *this;
00447 }
00448
00449 #include "mvblas.h"
00450
00451 #endif
00452
00453