////////////////////////////////////////////////////////////////////// // // Variable-length Vector header // // File: vectorn.h // Description : vectorn template class declaration and inlined implementation // // History: // -:Created by Anton Knyazev // ////////////////////////////////////////////////////////////////////// #ifndef vectorn_h #define vectorn_h #pragma once template class matrix_vector_product_tpl { public: matrix_vector_product_tpl(int nrows,int ncols,ftype *pmtxdata, ftype *pvecdata) { nRows=nrows; nCols=ncols; mtxdata=pmtxdata; vecdata=pvecdata; } int nRows,nCols; ftype *mtxdata,*vecdata; }; template class vectorn_tpl { public: vectorn_tpl(int _len,ftype *pdata=0) { len = _len; if (pdata) { flags=mtx_foreign_data; data=pdata; } #ifdef USE_MATRIX_POOLS else if (len<64) { if (vecn_pool_pos+len > vecn_pool_size) vecn_pool_pos = 0; data = vecn_pool+vecn_pool_pos; vecn_pool_pos += len; flags = mtx_foreign_data; } #endif else { data=new ftype[len]; flags=0; } } vectorn_tpl(vectorn_tpl &src) { flags=src.flags & mtx_foreign_data; data=src.data; len=src.len; src.flags |= mtx_foreign_data; }; ~vectorn_tpl() { if (!(flags & mtx_foreign_data)) delete[] data; } vectorn_tpl& operator=(const vectorn_tpl &src) { if (src.len!=len && !(flags & mtx_foreign_data)) { if (data) delete data; data = new ftype[src.len]; } len = src.len; for(int i=0;i vectorn_tpl& operator=(const vectorn_tpl &src) { if (src.len!=len && !(flags & mtx_foreign_data)) { if (data) delete data; data = new ftype[src.len]; } len = src.len; for(int i=0;i &src) { int i,j; for(i=0;i &src) { int i,j; for(i=0;i &src) { int i,j; for(i=0;i res(len); for(int i=0;i ftype1 operator*(const vectorn_tpl& op1, const vectorn_tpl& op2) { ftype1 res=0; for(int i=0;i vectorn_tpl operator+(const vectorn_tpl &op1,const vectorn_tpl &op2) { vectorn_tpl res(len); for(int i=0;i vectorn_tpl operator-(const vectorn_tpl &op1, const vectorn_tpl &op2) { vectorn_tpl res(op1.len); for(int i=0;i vectorn_tpl& operator+=(vectorn_tpl &op1, const vectorn_tpl &op2) { for(int i=0;i vectorn_tpl& operator-=(vectorn_tpl &op1, const vectorn_tpl &op2) { for(int i=0;i vectorn_tpl operator*(const vectorn_tpl &vec, const matrix_tpl &mtx) { int i,j; vectorn_tpl res(mtx.nCols); for(i=0;i vectorn_tpl operator*(const matrix_tpl &mtx, const vectorn_tpl &vec) { int i,j; vectorn_tpl res(mtx.nRows); for(i=0;i matrix_vector_product_tpl operator*(const matrix_tpl &mtx, const vectorn_tpl &vec) { return matrix_vector_product_tpl(mtx.nRows,mtx.nCols,mtx.data, vec.data); } typedef vectorn_tpl vectornf; typedef vectorn_tpl vectorn; #if defined(LINUX) #define DECLARE_VECTORN_POOL(ftype,sz) template<> ftype vectorn_tpl::vecn_pool[sz] = {}; template<> int vectorn_tpl::vecn_pool_pos=0; \ template<> int vectorn_tpl::vecn_pool_size=sz; #else #define DECLARE_VECTORN_POOL(ftype,sz) template<> ftype vectorn_tpl::vecn_pool[sz]; template<> int vectorn_tpl::vecn_pool_pos=0; \ template<> int vectorn_tpl::vecn_pool_size=sz; #endif //LINUX #endif