00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef TNT_VEC_H
00023 #define TNT_VEC_H
00024
00025 #include "tnt_subscript.h"
00026 #include <cstdlib>
00027 #include <cassert>
00028 #include <iostream>
00029 #include <sstream>
00030
00031 namespace TNT
00032 {
00033
00041 template <class T>
00042 class Vector
00043 {
00044
00045
00046 public:
00047
00048 typedef Subscript size_type;
00049 typedef T value_type;
00050 typedef T element_type;
00051 typedef T* pointer;
00052 typedef T* iterator;
00053 typedef T& reference;
00054 typedef const T* const_iterator;
00055 typedef const T& const_reference;
00056
00057 Subscript lbound() const { return 1;}
00058
00059 protected:
00060 T* v_;
00061 T* vm1_;
00062 Subscript n_;
00063
00064
00065
00066
00067 void initialize(Subscript N)
00068 {
00069
00070
00071
00072 assert(v_ == NULL);
00073 v_ = new T[N];
00074 assert(v_ != NULL);
00075 vm1_ = v_-1;
00076 n_ = N;
00077 }
00078
00079 void copy(const T* v)
00080 {
00081 Subscript N = n_;
00082 Subscript i;
00083
00084 #ifdef TNT_UNROLL_LOOPS
00085 Subscript Nmod4 = N & 3;
00086 Subscript N4 = N - Nmod4;
00087
00088 for (i=0; i<N4; i+=4)
00089 {
00090 v_[i] = v[i];
00091 v_[i+1] = v[i+1];
00092 v_[i+2] = v[i+2];
00093 v_[i+3] = v[i+3];
00094 }
00095
00096 for (i=N4; i< N; i++)
00097 v_[i] = v[i];
00098 #else
00099
00100 for (i=0; i< N; i++)
00101 v_[i] = v[i];
00102 #endif
00103 }
00104
00105 void set(const T& val)
00106 {
00107 Subscript N = n_;
00108 Subscript i;
00109
00110 #ifdef TNT_UNROLL_LOOPS
00111 Subscript Nmod4 = N & 3;
00112 Subscript N4 = N - Nmod4;
00113
00114 for (i=0; i<N4; i+=4)
00115 {
00116 v_[i] = val;
00117 v_[i+1] = val;
00118 v_[i+2] = val;
00119 v_[i+3] = val;
00120 }
00121
00122 for (i=N4; i< N; i++)
00123 v_[i] = val;
00124 #else
00125
00126 for (i=0; i< N; i++)
00127 v_[i] = val;
00128
00129 #endif
00130 }
00131
00132
00133
00134 void destroy()
00135 {
00136
00137 if (v_ == NULL) return ;
00138
00139
00140 delete [] (v_);
00141
00142 v_ = NULL;
00143 vm1_ = NULL;
00144 }
00145
00146
00147 public:
00148
00149
00150
00151 iterator begin() { return v_;}
00152 iterator end() { return v_ + n_; }
00153 const iterator begin() const { return v_;}
00154 const iterator end() const { return v_ + n_; }
00155
00156
00157
00158 ~Vector()
00159 {
00160 destroy();
00161 }
00162
00163
00164
00165 Vector() : v_(0), vm1_(0), n_(0) {};
00166
00167 Vector(const Vector<T> &A) : v_(0), vm1_(0), n_(0)
00168 {
00169 initialize(A.n_);
00170 copy(A.v_);
00171 }
00172
00173 Vector(Subscript N, const T& value = T()) : v_(0), vm1_(0), n_(0)
00174 {
00175 initialize(N);
00176 set(value);
00177 }
00178
00179 Vector(Subscript N, const T* v) : v_(0), vm1_(0), n_(0)
00180 {
00181 initialize(N);
00182 copy(v);
00183 }
00184
00185 Vector(Subscript N, char *s) : v_(0), vm1_(0), n_(0)
00186 {
00187 initialize(N);
00188 std::istringstream ins(s);
00189
00190 Subscript i;
00191
00192 for (i=0; i<N; i++)
00193 ins >> v_[i];
00194 }
00195
00196
00197
00198
00199 Vector<T>& newsize(Subscript N)
00200 {
00201 if (n_ == N) return *this;
00202
00203 destroy();
00204 initialize(N);
00205
00206 return *this;
00207 }
00208
00209
00210
00211
00212 Vector<T>& operator=(const Vector<T> &A)
00213 {
00214 if (v_ == A.v_)
00215 return *this;
00216
00217 if (n_ == A.n_)
00218 copy(A.v_);
00219
00220 else
00221 {
00222 destroy();
00223 initialize(A.n_);
00224 copy(A.v_);
00225 }
00226
00227 return *this;
00228 }
00229
00230 Vector<T>& operator=(const T& scalar)
00231 {
00232 set(scalar);
00233 return *this;
00234 }
00235
00236 inline Subscript dim() const
00237 {
00238 return n_;
00239 }
00240
00241 inline Subscript size() const
00242 {
00243 return n_;
00244 }
00245
00246
00247 inline reference operator()(Subscript i)
00248 {
00249 #ifdef TNT_BOUNDS_CHECK
00250 assert(1<=i);
00251 assert(i <= n_) ;
00252 #endif
00253 return vm1_[i];
00254 }
00255
00256 inline const_reference operator() (Subscript i) const
00257 {
00258 #ifdef TNT_BOUNDS_CHECK
00259 assert(1<=i);
00260 assert(i <= n_) ;
00261 #endif
00262 return vm1_[i];
00263 }
00264
00265 inline reference operator[](Subscript i)
00266 {
00267 #ifdef TNT_BOUNDS_CHECK
00268 assert(0<=i);
00269 assert(i < n_) ;
00270 #endif
00271 return v_[i];
00272 }
00273
00274 inline const_reference operator[](Subscript i) const
00275 {
00276 #ifdef TNT_BOUNDS_CHECK
00277 assert(0<=i);
00278
00279
00280
00281
00282
00283
00284 assert(i < n_) ;
00285 #endif
00286 return v_[i];
00287 }
00288
00289
00290
00291 };
00292
00293
00294
00295
00296 template <class T>
00297 std::ostream& operator<<(std::ostream &s, const Vector<T> &A)
00298 {
00299 Subscript N=A.dim();
00300
00301 s << N << "\n";
00302
00303 for (Subscript i=0; i<N; i++)
00304 s << A[i] << " " << "\n";
00305 s << "\n";
00306
00307 return s;
00308 }
00309
00310 template <class T>
00311 std::istream & operator>>(std::istream &s, Vector<T> &A)
00312 {
00313
00314 Subscript N;
00315
00316 s >> N;
00317
00318 if ( !(N == A.size() ))
00319 {
00320 A.newsize(N);
00321 }
00322
00323
00324 for (Subscript i=0; i<N; i++)
00325 s >> A[i];
00326
00327
00328 return s;
00329 }
00330
00331
00332
00333
00334 template <class T>
00335 Vector<T> operator+(const Vector<T> &A,
00336 const Vector<T> &B)
00337 {
00338 Subscript N = A.dim();
00339
00340 assert(N==B.dim());
00341
00342 Vector<T> tmp(N);
00343 Subscript i;
00344
00345 for (i=0; i<N; i++)
00346 tmp[i] = A[i] + B[i];
00347
00348 return tmp;
00349 }
00350
00351 template <class T>
00352 Vector<T> operator-(const Vector<T> &A,
00353 const Vector<T> &B)
00354 {
00355 Subscript N = A.dim();
00356
00357 assert(N==B.dim());
00358
00359 Vector<T> tmp(N);
00360 Subscript i;
00361
00362 for (i=0; i<N; i++)
00363 tmp[i] = A[i] - B[i];
00364
00365 return tmp;
00366 }
00367
00368 template <class T>
00369 Vector<T> operator*(const Vector<T> &A,
00370 const Vector<T> &B)
00371 {
00372 Subscript N = A.dim();
00373
00374 assert(N==B.dim());
00375
00376 Vector<T> tmp(N);
00377 Subscript i;
00378
00379 for (i=0; i<N; i++)
00380 tmp[i] = A[i] * B[i];
00381
00382 return tmp;
00383 }
00384
00385
00386 template <class T>
00387 T dot_prod(const Vector<T> &A, const Vector<T> &B)
00388 {
00389 Subscript N = A.dim();
00390 assert(N == B.dim());
00391
00392 Subscript i;
00393 T sum = 0;
00394
00395 for (i=0; i<N; i++)
00396 sum += A[i] * B[i];
00397
00398 return sum;
00399 }
00400
00401 }
00402
00403 #endif
00404