00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef GENLIB2_VECTOR_INCLUDED
00022 #define GENLIB2_VECTOR_INCLUDED
00023
00024 #include <cassert>
00025 #include <iostream>
00026
00027 using namespace std;
00028
00029 namespace GenLib2
00030 {
00031
00036 template<class T>
00037 class Vector
00038 {
00039
00040
00041
00042
00043
00044
00045
00047
00048
00049 protected:
00050
00052 void allocate(int n);
00053
00055 void free();
00056
00058 void copy(const T* v);
00059
00061 void set(const T& val);
00062
00063 public:
00064
00066 Vector();
00067
00069 Vector(int n, const T& val=T(0));
00070
00072 Vector(int n, const T* v);
00073
00075 Vector(const Vector& A);
00076
00078 ~Vector();
00079
00080
00081
00082 Vector& operator=(const Vector& a);
00083
00085 Vector& newsize(int n);
00086
00088 Vector& clear() { set(T(0)); return *this; }
00089
00090
00091 Vector& operator=(const T& v);
00092
00093
00095 int dim() const { return _n; }
00096 int size() const { return _n; }
00097
00098
00099 bool is_data() const { return (_data != NULL); }
00100
00102 T& operator()(int i) { return _data[i]; }
00103 const T& operator()(int i) const { return _data[i]; }
00104
00105
00107 void read(istream& is);
00108
00110 void print(ostream& os) const;
00111
00112
00113
00114
00115 Vector& operator*=(T s);
00116
00118 Vector& operator/=(T s);
00119
00120 protected:
00122 int _n;
00123
00125 T* _data;
00126 };
00127
00128
00129
00130
00131
00132 template<class T>
00133 void Vector<T>::allocate(int n)
00134 {
00135 assert(_data == NULL);
00136
00137
00138 _data = (T*)new T[_n=n];
00139 assert(_data != NULL);
00140 }
00141
00142 template<class T>
00143 void Vector<T>::free()
00144 {
00145
00146 if (_data == NULL) return;
00147
00148
00149 if (_data != NULL) delete[] _data;
00150
00151 _data = NULL;
00152 }
00153
00154
00155
00156
00157 template<class T>
00158 void Vector<T>::copy(const T* v)
00159 {
00160 assert(_data != NULL);
00161 for (int i = 0; i < _n; i++)
00162 _data[i] = v[i];
00163 }
00164
00165
00166
00167
00168 template<class T>
00169 void Vector<T>::set(const T& val)
00170 {
00171
00172 if(_data == NULL) return;
00173 for (int i = 0; i < _n; i++)
00174 _data[i] = val;
00175 }
00176
00177
00178
00179
00180 template<class T>
00181 Vector<T>::Vector() : _n(0), _data(NULL) { }
00182
00183
00184
00185
00186 template<class T>
00187 Vector<T>::Vector(int n, const T& val) : _n(0), _data(NULL)
00188 {
00189 allocate(n);
00190 set(val);
00191 }
00192
00193
00194
00195
00196 template<class T>
00197 Vector<T>::Vector(int n, const T* v) : _n(0), _data(NULL)
00198 {
00199 allocate(n);
00200 copy(v);
00201 }
00202
00203
00204
00205
00206 template<class T>
00207 Vector<T>::Vector(const Vector& A) : _n(0), _data(NULL)
00208 {
00209 allocate(A._n);
00210 copy(A._data);
00211 }
00212
00213
00214
00215
00216 template<class T>
00217 Vector<T>::~Vector()
00218 {
00219 free();
00220 }
00221
00222
00223
00224
00225 template<class T>
00226 Vector<T>& Vector<T>::operator=(const Vector& a)
00227 {
00228 if (_data == a._data) return *this;
00229
00230 if (_n == a._n)
00231 copy(a._data);
00232 else
00233 {
00234 free();
00235 allocate(a._n);
00236 copy(a._data);
00237 }
00238
00239 return *this;
00240 }
00241
00242
00243
00244
00245 template<class T>
00246 Vector<T>& Vector<T>::newsize(int n)
00247 {
00248 if (_n == n) return *this;
00249 if (_data != NULL) free();
00250 allocate(n);
00251 return *this;
00252 }
00253
00254
00255
00256
00257 template<class T>
00258 Vector<T>& Vector<T>::operator=(const T& v)
00259 {
00260 set(v);
00261 return *this;
00262 }
00263
00264
00265
00266
00267 template<class T>
00268 void Vector<T>::read(istream& is)
00269 {
00270 int n = 0;
00271 is >> n;
00272 newsize(n);
00273
00274 for (int i = 0; i < _n; i++)
00275 is >> _data[i];
00276 }
00277
00278
00279
00280
00281 template<class T>
00282 void Vector<T>::print(ostream& os) const
00283 {
00284 os << _n << endl;
00285 for (int i = 0; i < _n; i++)
00286 os << _data[i] << " ";
00287 os << endl;
00288 }
00289
00290
00291
00292
00293 template<class T>
00294 Vector<T>& Vector<T>::operator*=(T s)
00295 {
00296 for (int i = 0; i < _n; i++) _data[i] *= s;
00297 return *this;
00298 }
00299
00300
00301
00302
00303 template<class T>
00304 Vector<T>& Vector<T>::operator/=(T s)
00305 {
00306 for (int i = 0; i < _n; i++) _data[i] /= s;
00307 return *this;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00363
00365
00366 template<class T>
00367 std::istream& operator>>(std::istream& is, Vector<T>& a)
00368 {
00369 a.read(is);
00370 return is;
00371 }
00372
00373 template<class T>
00374 std::ostream& operator<<(std::ostream& os, const Vector<T>& a)
00375 {
00376 a.print(os);
00377 return os;
00378 }
00379
00380
00382
00384 template<class T>
00385 Vector<T> operator+(const Vector<T>& a, const Vector<T>& b)
00386 {
00387 int n = a.size();
00388
00389 assert(n == b.size());
00390
00391 Vector<T> c(n);
00392
00393 for (register int i = 0; i < n; i++)
00394 c(i) = a(i) + b(i);
00395
00396 return c;
00397 }
00398
00399
00401 template<class T>
00402 Vector<T> operator-(const Vector<T>& a, const Vector<T>& b)
00403 {
00404 int n = a.size();
00405
00406 assert(n == b.size());
00407
00408 Vector<T> c(n);
00409
00410 for (int i = 0; i < n; i++)
00411 c(i) = a(i) - b(i);
00412
00413 return c;
00414 }
00415
00416
00418 template<class T>
00419 T dot_prod(const Vector<T>& a, const Vector<T>& b)
00420 {
00421
00422
00423
00424
00425
00426
00427
00428
00429 int n = a.size();
00430
00431 assert(n == b.size());
00432
00433 T sum = 0;
00434
00435 for (int i = 0; i < n; i++)
00436 sum += a(i) * b(i);
00437
00438 return sum;
00439 }
00440
00441 }
00442
00443 #endif