www.pudn.com > Lapackpp1_1a.zip > template_v.h
// LAPACK++ (V. 1.1) // (C) 1992-1996 All Rights Reserved. // Lapack++ templated vector class #ifndef _VECTOR_TEMPLATE_H_ #define _VECTOR_TEMPLATE_H_ #include// for formatted printing of matrices #include #ifndef __ASSERT_H #include // cheap "error" protection used in checking #endif // checking array bounds. template typedef struct vrep{ int sz; type* data; int ref_count; } vref ; template class Vector { private: vref *p; type *data; // performance hack, avoid type // indirection to data. public: /*::::::::::::::::::::::::::*/ /* Constructors/Destructors */ /*::::::::::::::::::::::::::*/ //inline Vector (); // this should behave as Vector (0) inline Vector(int); Vector(int, const type); // can't be inlined because of 'for' // statement. inline Vector(type*, int); Vector(const Vector &); inline ~Vector() ; /*::::::::::::::::::::::::::::::::*/ /* Indices and access operations */ /*::::::::::::::::::::::::::::::::*/ inline type& operator[](int); inline type& operator()(int); inline type& operator()(int) const; // read only inline operator type*(); inline int size() const; inline int null(); int resize(int d); inline int ref_count() const; // return the number of ref counts inline type* addr() const; /*::::::::::::::*/ /* Assignment */ /*::::::::::::::*/ inline Vector & operator=(const Vector &); inline Vector & operator=(type); inline Vector & ref(const Vector &); Vector & inject(Vector &); Vector & copy(const Vector &); /* I/O */ friend ostream& operator<<(ostream&, const Vector &); }; // constructors template inline Vector ::Vector(int n=0) { assert(n>=0); p = new vref ; p->sz = n; p->ref_count = 1; p->data = data = new type[n]; } template inline Vector ::Vector(type *d, int n) { p = new vref ; p->sz = n; p->ref_count = 2; p-> data = data = d; } template inline Vector ::~Vector() { if (--(p->ref_count) == 0) // perform garbage col. { delete [] ( (type*) p->data); delete p; } } // operators and member functions template inline int Vector ::null() { return (size() == 0) ; } template inline int Vector ::size() const { return p-> sz; } template inline int Vector ::ref_count() const { return p->ref_count; } template inline type* Vector ::addr() const { return data; } template inline Vector ::operator type*() { return &((*this)(0)); } template inline type& Vector ::operator()(int i) { #if 0 // already done by LA_BOUNDS_CHECK assert(0<=i && i inline type& Vector ::operator()(int i) const { #if 0 // already done by LA_BOUNDS_CHECK assert(0<=i && i inline type& Vector ::operator[](int i) { assert(0<=i && i inline Vector & Vector ::ref(const Vector & m) { // always check that the p field has been initialized. // Either the lhs or rhs could be a NULL Vector ... m.p->ref_count++; if (--(p->ref_count) == 0) // perform garbage col. { delete [] ( (type*) p->data); delete p; } p = m.p; data = m.data; return *this; } template inline Vector & Vector ::operator=(const Vector & m) { return copy(m); } template inline Vector & Vector ::operator=(type scalar) { for (int i=0; i