#ifndef DENSEVECTOR_H #define DENSEVECTOR_H #include "Vector.h" namespace ATC_matrix { template /** * @class DenseVector * @brief Class for storing data in a "dense" vector form */ class DenseVector : public Vector { public: explicit DenseVector(INDEX n=0, bool z=1) { _create(n,z); } DenseVector(const DenseVector &c) : Vector(), _data(NULL) { _copy(c); } DenseVector(const Vector &c) : Vector(), _data(NULL) { _copy(c); } DenseVector(const T * ptr, INDEX nrows) : Vector(), _data(NULL) { copy(ptr,nrows); } virtual ~DenseVector() { _delete(); } //* resizes the Vector, ignores nCols, optionally copys what fits void resize(INDEX rows, INDEX cols=1, bool copy=false); //* resizes the Vector, ignores nCols, optionally zeros it out void reset (INDEX rows, INDEX cols=1, bool zero=true); //* resizes the Vector and copies data, ignores nCols void copy(const T * ptr, INDEX rows, INDEX cols=1); // overloaded inline virtual functions T operator[](INDEX i) const { VICK(i) return _data[i]; } T& operator[](INDEX i) { VICK(i) return _data[i]; } T operator()(INDEX i, INDEX j=0) const { VICK(i) return _data[i]; } T& operator()(INDEX i, INDEX j=0) { VICK(i) return _data[i]; } void set_all_elements_to(const T &v) { int sz = this->size(); for (INDEX i = 0; i < sz; i++) _data[i] = v; } INDEX nRows() const { return _size; } T* ptr() const { return _data; } DenseVector& operator=(const T &v); DenseVector& operator=(const Vector &c); DenseVector& operator=(const DenseVector &c); void write_restart(FILE *f) const; private: void _delete(); void _create(INDEX n, bool zero=0); void _copy(const Vector &c); T *_data; INDEX _size; }; /////////////////////////////////////////////////////////////////////////////// // Template definitions /////////////////////////////////////////////////////// //----------------------------------------------------------------------------- // resizes the matrix and optionally copies over what still fits, ignores cols //----------------------------------------------------------------------------- template void DenseVector::resize(INDEX rows, INDEX cols, bool copy) { if (_size==rows) return; // if is correct size, done if (!copy) { _delete(); _create(rows); return; } DenseVector temp(*this); _delete(); _create(rows); int sz = this->size(); for (INDEX i = 0; i < sz; i++) _data[i] = i void DenseVector::reset(INDEX rows, INDEX /* cols */, bool zero) { if (_size!=rows) { _delete(); _create(rows); } if (zero) this->zero(); } /////////////////////////////////////////////////////////////////////////////// //* resizes the matrix and optionally zeros it out template void DenseVector::copy(const T * ptr, INDEX rows, INDEX /* cols */) { resize(rows, 1, false); memcpy(_data, ptr, this->size()*sizeof(T)); } /////////////////////////////////////////////////////////////////////////////// //* writes the matrix data to a file template void DenseVector::write_restart(FILE *f) const { fwrite(&_size, sizeof(INDEX),1,f); if(_size) fwrite(_data, sizeof(T), _size, f); } /////////////////////////////////////////////////////////////////////////////// //* clears allocated memory template inline void DenseVector::_delete() { if (_data) delete [] _data; _size = 0; } /////////////////////////////////////////////////////////////////////////////// //* allocates memory for an rows by cols DenseMatrix template inline void DenseVector::_create(INDEX n, bool zero) { _size=n; _data = _size ? new T [_size] : NULL ; if (zero) this->zero(); } /////////////////////////////////////////////////////////////////////////////// //* creates a deep memory copy from a general matrix template inline void DenseVector::_copy(const Vector &c) { if (!_data || _size!=c.size()) { _delete(); _create(c.size(), false); } else _size = c.size(); memcpy(_data, c.ptr(), _size*sizeof(T)); } /////////////////////////////////////////////////////////////////////////////// //* assigns v to all values in the vector template DenseVector& DenseVector::operator=(const T &v) { int sz = this->size(); for (INDEX i = 0; i < sz; i++) (*this)[i] = v; return *this; } /////////////////////////////////////////////////////////////////////////////// //* copys c with a deep copy template DenseVector& DenseVector::operator=(const Vector &c) { _copy(c); return *this; } /////////////////////////////////////////////////////////////////////////////// //* copys c with a deep copy template DenseVector& DenseVector::operator=(const DenseVector &c) { _copy(c); return *this; } } // end namespace #endif