00001 #ifndef Vector_H 00002 #define Vector_H 00003 #include <cassert> 00004 #include <iostream> 00005 00006 namespace puma2 { 00007 00008 template <class T> 00009 class Matrix; 00010 00018 template <class T> 00019 class Vector { 00020 public: 00021 explicit Vector(int = 0); 00022 Vector(const Vector&); 00023 virtual ~Vector(); 00024 int length() const {return size;} 00025 inline T& operator[] (int i) ; 00026 inline const T& operator[] (int i) const; 00027 void operator=(const Vector &); 00028 void operator=(T v); 00029 operator T* () { return vec; } 00030 operator const T* () const { return vec; } 00031 static inline void copy(const T&, T&); 00032 private: 00033 int size; 00034 T *vec; 00035 bool allocated; 00036 friend class Matrix<T>; 00037 Vector(T*,int); 00038 }; 00039 00040 00041 template <class T> inline void Vector<T>::copy(const T & f, T& t) { t = f; } 00042 00043 template <class T> 00044 Vector<T>::Vector(const Vector & v) : size(v.size){ 00045 allocated = true; 00046 vec = new T[size]; 00047 for (int i = 0; i<size; ++i){ 00048 copy(v.vec[i],vec[i]); 00049 } 00050 } 00051 00052 template <class T> 00053 Vector<T>::~Vector(){ 00054 if (allocated){ 00055 delete [] vec; 00056 } 00057 } 00058 00059 template <class T> 00060 Vector<T>::Vector(int s) : size(s){ 00061 vec = new T[s]; 00062 allocated = true; 00063 } 00064 00065 template <class T> 00066 Vector<T>::Vector(T* m, int s){ 00067 allocated = false; 00068 vec = m; 00069 size = s; 00070 } 00071 00072 template <class T> 00073 void Vector<T>::operator=(const Vector & v){ 00074 assert(v.size == size); 00075 for (int i = 0; i<size; ++i){ 00076 // vec[i]=v.vec[i]; 00077 copy(v.vec[i],vec[i]); 00078 } 00079 } 00080 template <class T> 00081 inline const T& Vector<T>::operator[] (int i) const{ 00082 assert((i<size) && (i>=0)); 00083 return vec[i]; 00084 } 00085 00086 template <class T> 00087 inline T& Vector<T>::operator[] (int i){ 00088 assert(i<size); 00089 assert(i>=0); 00090 return vec[i]; 00091 } 00092 00093 } 00094 00095 #endif