00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef VECTOR_H
00019 #define VECTOR_H
00020
00021 #include <iostream>
00022 #include "blas_types.h"
00023
00024 namespace colarray {
00025
00026 using blas::int_t;
00027
00028 template <typename T> class Matrix;
00029
00030
00031 template <typename T> class Vector;
00032 template <typename T>
00033 std::ostream& operator << (std::ostream&, const Vector<T> &);
00034
00038 template <typename T>
00039 class Vector {
00040 public:
00042 int_t _n;
00044 int_t _inc;
00046 T* _v;
00047 protected:
00049 T* _data;
00052 int* _refCount;
00053
00054
00055 public:
00058 Vector() : _n(0), _inc(0), _v(0), _data(0), _refCount(0) { }
00059
00062 Vector(int_t n);
00063
00068 Vector(const Vector& x, int_t start, int_t end) :
00069 _n(end - start), _inc(x._inc), _v(x._v + start*x._inc),
00070 _data(x._data), _refCount(x._refCount)
00071 { if (_refCount) *_refCount += 1; }
00072
00076 Vector(const Vector& a) : _n(a._n), _inc(a._inc), _v(a._v),
00077 _data(a._data), _refCount(a._refCount)
00078 { if (_refCount) *_refCount += 1; }
00079
00080 protected:
00083 Vector(T* v, int_t n, int_t inc, T* data, int* refCount) :
00084 _n(n), _inc(inc), _v(v), _data(data), _refCount(refCount)
00085 { if (_refCount) *_refCount += 1; }
00086
00087 public:
00088
00089 ~Vector();
00090
00091
00092 const T& operator () (int_t i) const
00093 { return _v[i*_inc]; }
00094 T& operator () (int_t i)
00095 { return _v[i*_inc]; }
00096 const T& operator[] (int i) const
00097 { return _v[i*_inc]; }
00098 T& operator[] (int i)
00099 { return _v[i*_inc]; }
00100
00101
00102
00103 inline Vector& operator= (const Vector&);
00104 T operator= (const T v);
00105
00106
00109 void inject(const Vector<T> &x);
00110
00111 friend std::ostream& operator << <T> (std::ostream&, const Vector<T> &);
00112 };
00113
00119 template <typename T>
00120 class ColumnVector : public Vector<T> {
00121 public:
00124 ColumnVector() : Vector<T>() {}
00125
00128 ColumnVector(int_t n) : Vector<T>(n) {}
00129
00134 ColumnVector(const ColumnVector& x, int_t start, int_t end) :
00135 Vector<T>(x._v + start, end - start, 1, x._data, x._refcount) {}
00136
00141 ColumnVector(const Matrix<T>& A, int_t i) :
00142 Vector<T>(A._v + A._ld*i, A._m, 1, A._data, A._refCount) {}
00143
00150 ColumnVector(T* v, int n) :
00151 Vector<T>(v, n, 1, 0, 0) { }
00152
00153
00154 const T& operator () (int_t i) const
00155 { return (this->_v)[i]; }
00156 T& operator () (int_t i)
00157 { return (this->_v)[i]; }
00158 const T& operator[] (int i) const
00159 { return (this->_v)[i]; }
00160 T& operator[] (int i)
00161 { return (this->_v)[i]; }
00162
00163
00164 inline T operator = (const T v) { return this->Vector<T>::operator=(v); }
00165 };
00166
00167
00171 template <typename T>
00172 class RowVector : public Vector<T> {
00173 public:
00174
00178 RowVector(const Matrix<T>& a, int_t i) :
00179 Vector<T>(a._v + i, a._n, a._ld, a._data, a._refCount) {}
00180
00181
00182 inline T operator = (const T v) { return this->Vector<T>::operator=(v); }
00183 };
00184
00185 }
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206 #include <cassert>
00207 #include "vector.h"
00208
00209 namespace colarray {
00210
00211 template <typename T>
00212 Vector<T>::Vector(int_t n) {
00213 _n = n;
00214 _inc = 1;
00215 _refCount = new int(1);
00216 _v = _data = new T[n];
00217 #if 0
00218 std::cout << std::showbase << std::hex << this->v << std::endl << std::dec;
00219 #endif
00220 }
00221
00222 template <typename T>
00223 inline Vector<T>::~Vector() {
00224 if (_refCount) {
00225 *_refCount -= 1;
00226 if (*_refCount < 1) {
00227 delete [] _data;
00228 delete _refCount;
00229 }
00230 }
00231 }
00232
00233 template <typename T>
00234 T Vector<T>::operator = (const T v) {
00235 for (int_t i = 0; i < _n; i ++)
00236 this->operator()(i) = v;
00237 return v;
00238 }
00239
00240 template <typename T>
00241 inline Vector<T>& Vector<T>::operator= (const Vector<T>& x) {
00242 if (&x != this) {
00243
00244 if (_refCount) {
00245 *_refCount -= 1;
00246 if (*_refCount < 1) {
00247 delete [] _data;
00248 delete _refCount;
00249 }
00250 }
00251
00252 _n = x._n;
00253 _inc = x._inc;
00254 _v = x._v;
00255 _data = x._data;
00256 _refCount = x._refCount;
00257 if (_refCount) *_refCount += 1;
00258 }
00259 return *this;
00260 }
00261
00262 template <typename T>
00263 void Vector<T>::inject(const Vector<T> &x) {
00264 assert(_n == x._n);
00265 for (int_t i = 0; i < _n; i ++)
00266 this->operator()(i) = x(i);
00267 }
00268
00269 template <typename T>
00270 std::ostream& operator << (std::ostream& ostr, const Vector<T>& x)
00271 {
00272 bool first = true;
00273
00274 ostr << "Vector(" << x._n << ")" ;
00275 ostr << "{";
00276 for (int_t i = 0; i < x._n; i ++) {
00277 if (!first) {
00278 ostr << ", ";
00279 } else
00280 first = false;
00281 ostr << x(i);
00282 }
00283 ostr << "}";
00284 return ostr;
00285 }
00286
00287 }
00288
00289
00290 #endif