00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #ifndef _CJMATRIX_H_
00021 #define _CJMATRIX_H_ 1
00022
00023 #include "CjMatrixException.hh"
00024 #include "CjMatrixImpl.hh"
00025 #include "CjMatrixImpl_1D.hh"
00026
00027 #include <iterator>
00028
00029 #include <ostream>
00030 #include <iomanip>
00031
00032 #include <cassert>
00033 #include <typeinfo>
00034
00035
00036
00037 template <typename, bool> class CjMatrixSubVector_const_iterator;
00038 template <typename, bool> class CjMatrixSubVector_iterator;
00039 template <typename, bool> class CjMatrixSubVector;
00040 template <typename, bool> class CjMatrixSubVectorC;
00041
00042
00043
00052 template <typename T> class CjMatrix {
00053
00054 private:
00055 CjMatrixImpl<T> * m_impl;
00056
00057 protected:
00058 CjMatrixImpl<T> * get_impl() { return m_impl; }
00059
00060 public:
00061
00063
00070 CjMatrix (const unsigned int width, const unsigned int height, const T * const defaultValue = 0):m_impl( new CjMatrixImpl_1D<T>( width, height, defaultValue ) ) { }
00071
00073
00076 CjMatrix (CjMatrixImpl<T> * impl) throw ():m_impl( impl ) { }
00077
00079
00087 explicit CjMatrix (const CjMatrix & cjm, const unsigned int width, const unsigned int height, const T * const defaultValue = 0 ):m_impl( new CjMatrixImpl_1D<T>( cjm, width, height, defaultValue ) ) { };
00088
00090
00094 explicit CjMatrix (const CjMatrix & cjm):m_impl(cjm.m_impl->clone()) { }
00095
00097 ~CjMatrix() { delete m_impl; }
00098
00100
00104 CjMatrix<T> & operator=(const CjMatrix<T> & cjm) {
00105
00106
00107
00108
00109
00110 if (this != &cjm) {
00111 m_impl->resize_with_copyImpl(cjm.getWidth(), cjm.getHeight(), &cjm);
00112 }
00113 return *this;
00114 }
00115
00117
00125 void resize_with_copy(unsigned int width, unsigned int height, const CjMatrix<T> * cjm = (CjMatrix<T> *) 0) { m_impl->resize_with_copyImpl(width, height, cjm); };
00126
00127
00129
00134 CjMatrix<T> & setColumnValues(unsigned int col, const T & val) { m_impl->setColumnValuesImpl(col, val); return *this; }
00136
00141 CjMatrix<T> & setRowValues(unsigned int row, const T & val) { m_impl->setRowValuesImpl(row, val); return *this; }
00143
00146 CjMatrix<T> & setAllValues(const T val) { m_impl->setAllValuesImpl(val); return *this; }
00148
00151 CjMatrix<T> & setDiagonal(const T & val) { m_impl->setDiagonalImpl(val); return *this; }
00152
00154
00157 CjMatrix<T> & setDefaultValue(const T & val) { m_impl->setDefaultValueImpl( val ); return *this; }
00159 T getDefaultValue() const { return m_impl->getDefaultValueImpl(); }
00160
00162 bool getDefaultValueSet() const { return m_impl->getDefaultValueSetImpl(); }
00163
00165
00168 bool operator==(const CjMatrix<T> & cjm) const { return m_impl->equalElements(cjm); }
00170
00173 bool operator!=(const CjMatrix<T> & cjm) const { return !operator==(cjm); }
00175
00178 CjMatrix<T> & transpose(void) { m_impl->transposeImpl(); return *this; }
00179
00181
00183 bool isSymmetric() const { return m_impl->isSymmetricImpl(); }
00185 unsigned int getWidth() const { return m_impl->getWidthImpl(); }
00187 unsigned int getHeight() const { return m_impl->getHeightImpl(); }
00188
00190
00193 inline T & at (const unsigned int row, const unsigned int col) { return m_impl->atImpl(row,col); }
00195
00198 inline const T & at (const unsigned int row, const unsigned int col) const { return m_impl->atImpl(row,col); }
00200
00202 inline T & operator() (const unsigned int row, const unsigned int col) { return m_impl->getImpl(row,col); }
00204
00206 inline const T & operator() (const unsigned int row, const unsigned int col) const { return m_impl->getImpl(row,col); }
00207
00208
00210 typedef CjMatrixSubVector<T, false> row_t;
00212 typedef CjMatrixSubVector<T, true> column_t;
00214 typedef CjMatrixSubVectorC<T, false> const_row_t;
00216 typedef CjMatrixSubVectorC<T, true> const_column_t;
00217
00219 inline row_t row(const int r) { return row_t( r, this); }
00221 inline column_t column(const int c) { return column_t( c, this); }
00223 inline const_row_t row(const int r) const { return const_row_t( r, this); }
00225 inline const_column_t column(const int c) const { return const_column_t( c, this); }
00226
00228 typename column_t::iterator rows_begin() { return column(0).begin(); }
00230 typename column_t::iterator rows_end() { return column(0).end(); }
00232 typename const_column_t::const_iterator rows_cbegin() { return column(0).cbegin(); }
00234 typename const_column_t::const_iterator rows_cend() { return column(0).cend(); }
00236 typename row_t::iterator columns_begin() { return row(0).begin(); }
00238 typename row_t::iterator columns_end() { return row(0).end(); }
00240 typename const_row_t::const_iterator columns_cbegin() { return row(0).cbegin(); }
00242 typename const_row_t::const_iterator columns_cend() { return row(0).cend(); }
00243
00244
00245
00246
00247
00248 void swap (CjMatrix<T> & other) { std::swap(m_impl, other.m_impl); }
00249 };
00250
00251 template<typename T, bool is_col> class CjMatrixSubVector {
00252 protected:
00253 CjMatrix <T> * const m_mat;
00254 unsigned int m_row_col;
00255 public:
00256 typedef CjMatrixSubVector_iterator<T, is_col> iterator;
00257 typedef CjMatrixSubVector_const_iterator<T, is_col> const_iterator;
00258
00259 CjMatrixSubVector( const unsigned int row_col, CjMatrix <T> * const mat):m_mat(mat),m_row_col(row_col) { }
00260 CjMatrixSubVector( const CjMatrixSubVector<T, is_col> & orig):m_mat(orig.m_mat),m_row_col(orig.m_row_col) { }
00261
00262
00263 iterator begin() { return iterator(m_mat, m_row_col, false); }
00264 iterator end() { return iterator(m_mat, m_row_col, true); }
00265 const_iterator cbegin() { return const_iterator(m_mat, m_row_col, false); }
00266 const_iterator cend() { return const_iterator(m_mat, m_row_col, true); }
00267
00268 template<typename TP, bool is_colP> friend class CjMatrixSubVectorC;
00269 };
00270
00271 template<typename T, bool is_col> class CjMatrixSubVectorC {
00272 protected:
00273 const CjMatrix <T> * const m_mat;
00274 unsigned int m_row_col;
00275 public:
00276 typedef CjMatrixSubVector_const_iterator<T, is_col> const_iterator;
00277
00278 CjMatrixSubVectorC (const unsigned int row_col, const CjMatrix <T> * const mat):m_mat(mat),m_row_col(row_col) { }
00279 CjMatrixSubVectorC( const CjMatrixSubVectorC<T, is_col> & orig):m_mat(orig.m_mat),m_row_col(orig.m_row_col) { }
00280 CjMatrixSubVectorC( const CjMatrixSubVector<T, is_col> & orig):m_mat(orig.m_mat),m_row_col(orig.m_row_col) { }
00281
00282 const_iterator cbegin() { return const_iterator(m_mat, m_row_col, false); }
00283 const_iterator cend() { return const_iterator(m_mat, m_row_col, true); }
00284 };
00285
00286 template <typename U, bool is_col> class CjMatrixSubVector_iterator : public std::iterator < std::random_access_iterator_tag, U, int > {
00287 protected:
00288 CjMatrix<U> * m_mat;
00289 int m_n;
00290 int m_row_col;
00291
00292 public:
00293 CjMatrixSubVector_iterator( CjMatrix <U> * const mat, const unsigned int row_col, const bool is_end ):
00294 m_mat(mat),
00295 m_n( is_end ? (is_col?mat->getHeight():mat->getWidth()) : 0 ),
00296 m_row_col(row_col) { }
00297
00298 template <typename V, bool is_col_prime> friend class CjMatrixSubVector_iterator;
00299 template <typename V, bool is_col_prime> friend class CjMatrixSubVector_const_iterator;
00300
00301 CjMatrixSubVector_iterator( const CjMatrixSubVector_iterator<U, is_col> & i ): m_mat(i.m_mat), m_n( i.m_n ), m_row_col(i.m_row_col) { }
00302 explicit CjMatrixSubVector_iterator( const CjMatrixSubVector_iterator<U, !is_col> & i ): m_mat(i.m_mat), m_n( i.m_row_col ), m_row_col(i.m_n) { }
00303 CjMatrixSubVector_iterator<U,is_col> & operator=(const CjMatrixSubVector_iterator<U,is_col> & it) { m_mat = it.m_mat; m_n=it.m_n; m_row_col=it.m_row_col; return *this; }
00304
00305 CjMatrixSubVector_iterator<U,!is_col> convertType() const { return CjMatrixSubVector_iterator<U,!is_col>(*this); }
00306 bool atEnd() const { return m_n == (is_col?(int)m_mat->getHeight():(int)m_mat->getWidth()); }
00307 CjMatrixSubVector_iterator<U,is_col> end_iterator() { return CjMatrixSubVector_iterator<U,is_col>(m_mat, m_row_col, true); }
00308 CjMatrixSubVector_iterator<U,is_col> transpose( CjMatrix <U> * const mat) { if( (getRow() > (int)mat->getHeight()) || (getColumn() > (int)mat->getWidth()) ) throw E_CjMatrix_BoundsError("CjMatrixSubVector_iterator::transpose(CjMatrix): Invalid element location being transposed to new matrix"); CjMatrixSubVector_iterator tmp(*this); tmp.m_mat = mat; return tmp; }
00309
00310 int getRowCol() const { return m_row_col; }
00311 int getOffset() const { return m_n; }
00312 int getRow() const { return (is_col?m_n:m_row_col); }
00313 int getColumn() const { return (is_col?m_row_col:m_n); }
00314 bool isSameMatrix(const CjMatrix <U> * const mat) const { return mat==m_mat; }
00315 bool atDiagonal() const { return m_n==m_row_col; }
00316
00317 CjMatrixSubVector_iterator & operator++ () { ++m_n; return *this; }
00318 CjMatrixSubVector_iterator operator++ (int) { CjMatrixSubVector_iterator<U, is_col> old_val(*this); ++(*this); return old_val; }
00319 CjMatrixSubVector_iterator & operator-- () { --m_n; return *this; }
00320 CjMatrixSubVector_iterator operator-- (int) { CjMatrixSubVector_iterator<U, is_col> old_val(*this); --(*this); return old_val; }
00321 typename CjMatrixSubVector_iterator<U, is_col>::reference operator[] (const int n) { return m_mat->operator()( is_col?(m_n+n):m_row_col, is_col?m_row_col:(m_n+n) ); }
00322
00323 bool operator==(const CjMatrixSubVector_iterator<U, true> & i) const { return ( (m_mat==i.m_mat) && ((!is_col)?(m_n==i.m_row_col):(m_n==i.m_n)) && ((!is_col)?(m_row_col==i.m_n):(m_row_col==i.m_row_col) )); }
00324 bool operator==(const CjMatrixSubVector_iterator<U, false> & i) const { return ( (m_mat==i.m_mat) && (is_col?(m_n==i.m_row_col):(m_n==i.m_n)) && (is_col?(m_row_col==i.m_n):(m_row_col==i.m_row_col) )); }
00325 bool operator==(const CjMatrixSubVector_const_iterator<U, true> & i) const { return ( (m_mat==i.m_mat) && ((!is_col)?(m_n==i.m_row_col):(m_n==i.m_n)) && ((!is_col)?(m_row_col==i.m_n):(m_row_col==i.m_row_col) )); }
00326 bool operator==(const CjMatrixSubVector_const_iterator<U, false> & i) const { return ( (m_mat==i.m_mat) && (is_col?(m_n==i.m_row_col):(m_n==i.m_n)) && (is_col?(m_row_col==i.m_n):(m_row_col==i.m_row_col) )); }
00327
00328 bool operator!=(const CjMatrixSubVector_iterator<U, is_col> & i) const { return !(*this == i ); }
00329 bool operator!=(const CjMatrixSubVector_const_iterator<U, is_col> & i) const { return !(*this == i ); }
00330
00331 typename CjMatrixSubVector_iterator<U, is_col>::reference operator* () { return m_mat->operator()( is_col?m_n:m_row_col, is_col?m_row_col:m_n ); }
00332 typename CjMatrixSubVector_iterator<U, is_col>::pointer operator->() const { return &(m_mat->operator()( is_col?m_n:m_row_col, is_col?m_row_col:m_n )); }
00333 CjMatrixSubVector_iterator<U, is_col> & operator+= (const int n) { m_n += n; return *this; }
00334 CjMatrixSubVector_iterator<U, is_col> & operator-= (const int n) { m_n -= n; return *this; }
00335
00336 CjMatrixSubVector_iterator<U, is_col> operator+ (const int n) const { CjMatrixSubVector_iterator<U, is_col> ret(*this); ret.m_n = m_n+n; return ret; }
00337 CjMatrixSubVector_iterator<U, is_col> friend operator+ ( const int n, const CjMatrixSubVector_iterator<U, is_col> & rhs ) { CjMatrixSubVector_iterator<U, is_col> ret(rhs); ret.m_n += n; return ret; }
00338 CjMatrixSubVector_iterator<U, is_col> operator- (const int n) const { CjMatrixSubVector_iterator<U, is_col> ret(*this); ret.m_n = m_n-n; return ret; }
00339 typename CjMatrixSubVector_iterator<U, is_col>::difference_type operator- (const CjMatrixSubVector_iterator<U, is_col> & iter) const { return (m_n-iter.m_n); }
00340 typename CjMatrixSubVector_iterator<U, is_col>::difference_type operator- (const CjMatrixSubVector_const_iterator<U, is_col> & iter) const { return (m_n-iter.m_n); }
00341
00342 bool operator< (const CjMatrixSubVector_iterator<U, is_col> & iter) const { return (m_n < iter.m_n); }
00343 bool operator> (const CjMatrixSubVector_iterator<U, is_col> & iter) const { return (m_n > iter.m_n); }
00344 bool operator<= (const CjMatrixSubVector_iterator<U, is_col> & iter) const { return (m_n <= iter.m_n); }
00345 bool operator>= (const CjMatrixSubVector_iterator<U, is_col> & iter) const { return (m_n >= iter.m_n); }
00346 bool operator< (const CjMatrixSubVector_const_iterator<U, is_col> & iter) const { return (m_n < iter.m_n); }
00347 bool operator> (const CjMatrixSubVector_const_iterator<U, is_col> & iter) const { return (m_n > iter.m_n); }
00348 bool operator<= (const CjMatrixSubVector_const_iterator<U, is_col> & iter) const { return (m_n <= iter.m_n); }
00349 bool operator>= (const CjMatrixSubVector_const_iterator<U, is_col> & iter) const { return (m_n >= iter.m_n); }
00350
00351 friend std::ostream & operator<<(std::ostream & out, const CjMatrixSubVector_iterator<U, is_col> & a ) {
00352 out << "CjMatrixSubVector_iterator<" << typeid(U).name() << "," << (is_col?"true":"false") << ">{m_mat=" << a.m_mat << ",m_n=" << a.m_n << ",m_row_col=" << a.m_row_col << "}";
00353 return out;
00354 }
00355 };
00356
00357 template <typename V, bool is_col> class CjMatrixSubVector_const_iterator : public std::iterator < std::random_access_iterator_tag, const V, int > {
00358 protected:
00359 const CjMatrix<V> * m_mat;
00360 int m_n;
00361 int m_row_col;
00362 public:
00363 CjMatrixSubVector_const_iterator( const CjMatrix <V> * const mat, const unsigned int row_col, const bool is_end ):
00364 m_mat(mat),
00365 m_n( is_end ? (is_col?mat->getHeight():mat->getWidth()) : 0 ),
00366 m_row_col((int)row_col) { }
00367
00368 template <typename U, bool is_col_prime> friend class CjMatrixSubVector_const_iterator;
00369 template <typename U, bool is_col_prime> friend class CjMatrixSubVector_iterator;
00370
00371 CjMatrixSubVector_const_iterator( const CjMatrixSubVector_const_iterator<V, is_col> & i ): m_mat(i.m_mat), m_n( i.m_n ), m_row_col(i.m_row_col) { }
00372
00373 CjMatrixSubVector_const_iterator<V, is_col>( const CjMatrixSubVector_iterator<V, is_col> & i ): m_mat(i.m_mat), m_n( i.m_n ), m_row_col(i.m_row_col) { }
00374 explicit CjMatrixSubVector_const_iterator<V, is_col>( const CjMatrixSubVector_const_iterator<V, !is_col> & i ): m_mat(i.m_mat), m_n( i.m_row_col ), m_row_col(i.m_n) { }
00375 CjMatrixSubVector_const_iterator<V,is_col> & operator=(const CjMatrixSubVector_const_iterator<V,is_col> & it) { m_mat = it.m_mat; m_n=it.m_n; m_row_col=it.m_row_col; return *this; }
00376
00377 int getRowCol() const { return m_row_col; }
00378 int getOffset() const { return m_n; }
00379 int getRow() const { return (is_col?m_n:m_row_col); }
00380 int getColumn() const { return (is_col?m_row_col:m_n); }
00381 bool isSameMatrix(const CjMatrix <V> * const mat) const { return mat==m_mat; }
00382 bool atDiagonal() const { return m_n==m_row_col; }
00383
00384
00385 CjMatrixSubVector_const_iterator<V,!is_col> convertType() const { return CjMatrixSubVector_const_iterator<V,!is_col>(*this); }
00386 bool atEnd() const { return m_n == (is_col?(int)m_mat->getHeight():(int)m_mat->getWidth()); }
00387 CjMatrixSubVector_const_iterator<V,is_col> end_iterator() { return CjMatrixSubVector_const_iterator<V,is_col>(m_mat, m_row_col, true); }
00388 CjMatrixSubVector_const_iterator<V,is_col> transpose(const CjMatrix <V> * const mat) { if( (getRow() > (int)mat->getHeight()) || (getColumn() > (int)mat->getWidth()) ) throw E_CjMatrix_BoundsError("CjMatrixSubVector_const_iterator::transpose(CjMatrix): Invalid element location being transposed to new matrix"); CjMatrixSubVector_const_iterator tmp(*this); tmp.m_mat = mat; return tmp; }
00389
00390 CjMatrixSubVector_const_iterator & operator++ () { ++m_n; return *this; }
00391 CjMatrixSubVector_const_iterator operator++ (int) { CjMatrixSubVector_const_iterator<V, is_col> old_val(*this); ++(*this); return old_val; }
00392 CjMatrixSubVector_const_iterator & operator-- () { --m_n; return *this; }
00393 CjMatrixSubVector_const_iterator operator-- (int) { CjMatrixSubVector_const_iterator<V, is_col> old_val(*this); --(*this); return old_val; }
00394 const typename CjMatrixSubVector_const_iterator<V, is_col>::reference operator[] (const int n) { return m_mat->operator()( is_col?(m_n+n):m_row_col, is_col?m_row_col:(m_n+n) ); }
00395
00396 bool operator==(const CjMatrixSubVector_const_iterator<V, true> & i) const { return ( (m_mat==i.m_mat) && ((!is_col)?(m_n==i.m_row_col):(m_n==i.m_n)) && ((!is_col)?(m_row_col==i.m_n):(m_row_col==i.m_row_col) )); }
00397 bool operator==(const CjMatrixSubVector_const_iterator<V, false> & i) const { return ( (m_mat==i.m_mat) && (is_col?(m_n==i.m_row_col):(m_n==i.m_n)) && (is_col?(m_row_col==i.m_n):(m_row_col==i.m_row_col) )); }
00398 bool operator==(const CjMatrixSubVector_iterator<V, true> & i) const { return ( (m_mat==i.m_mat) && ((!is_col)?(m_n==i.m_row_col):(m_n==i.m_n)) && ((!is_col)?(m_row_col==i.m_n):(m_row_col==i.m_row_col) )); }
00399 bool operator==(const CjMatrixSubVector_iterator<V, false> & i) const { return ( (m_mat==i.m_mat) && (is_col?(m_n==i.m_row_col):(m_n==i.m_n)) && (is_col?(m_row_col==i.m_n):(m_row_col==i.m_row_col) )); }
00400
00401 bool operator!=(const CjMatrixSubVector_const_iterator<V, is_col> & i) const { return !(*this == i ); }
00402 bool operator!=(const CjMatrixSubVector_iterator<V, is_col> & i) const { return !(*this == i ); }
00403
00404 const typename CjMatrixSubVector_const_iterator<V, is_col>::reference operator* () { return m_mat->operator()( is_col?m_n:m_row_col, is_col?m_row_col:m_n ); }
00405 const typename CjMatrixSubVector_const_iterator<V, is_col>::pointer operator->() const { return &(m_mat->operator()( is_col?m_n:m_row_col, is_col?m_row_col:m_n )); }
00406 CjMatrixSubVector_const_iterator<V, is_col> & operator+= (const int n) { m_n += n; return *this; }
00407 CjMatrixSubVector_const_iterator<V, is_col> & operator-= (const int n) { m_n -= n; return *this; }
00408
00409 CjMatrixSubVector_const_iterator<V, is_col> operator+ (const int n) const { CjMatrixSubVector_const_iterator<V, is_col> ret(*this); ret.m_n = m_n+n; return ret; }
00410 CjMatrixSubVector_const_iterator<V, is_col> friend operator+ ( const int n, const CjMatrixSubVector_const_iterator<V, is_col> & rhs ) { CjMatrixSubVector_const_iterator<V, is_col> ret(rhs); ret.m_n += n; return ret; }
00411 CjMatrixSubVector_const_iterator<V, is_col> operator- (const int n) const { CjMatrixSubVector_const_iterator<V, is_col> ret(*this); ret.m_n = m_n-n; return ret; }
00412 typename CjMatrixSubVector_const_iterator<V, is_col>::difference_type operator- (const CjMatrixSubVector_const_iterator<V, is_col> & iter) const { return (m_n-iter.m_n); }
00413 typename CjMatrixSubVector_const_iterator<V, is_col>::difference_type operator- (const CjMatrixSubVector_iterator<V, is_col> & iter) const { return (m_n-iter.m_n); }
00414
00415 bool operator< (const CjMatrixSubVector_const_iterator<V, is_col> & iter) const { return (m_n < iter.m_n); }
00416 bool operator> (const CjMatrixSubVector_const_iterator<V, is_col> & iter) const { return (m_n > iter.m_n); }
00417 bool operator<= (const CjMatrixSubVector_const_iterator<V, is_col> & iter) const { return (m_n <= iter.m_n); }
00418 bool operator>= (const CjMatrixSubVector_const_iterator<V, is_col> & iter) const { return (m_n >= iter.m_n); }
00419 bool operator< (const CjMatrixSubVector_iterator<V, is_col> & iter) const { return (m_n < iter.m_n); }
00420 bool operator> (const CjMatrixSubVector_iterator<V, is_col> & iter) const { return (m_n > iter.m_n); }
00421 bool operator<= (const CjMatrixSubVector_iterator<V, is_col> & iter) const { return (m_n <= iter.m_n); }
00422 bool operator>= (const CjMatrixSubVector_iterator<V, is_col> & iter) const { return (m_n >= iter.m_n); }
00423 friend std::ostream & operator<<(std::ostream & out, const CjMatrixSubVector_const_iterator<V, is_col> & a ) {
00424 out << "CjMatrixSubVector_iterator<" << typeid(V).name() << "," << (is_col?"true":"false") << ">{m_mat=" << a.m_mat << ",m_n=" << a.m_n << ",m_row_col=" << a.m_row_col << "}";
00425 return out;
00426 }
00427 };
00428
00429
00430
00431 namespace std {
00432 template <typename T> void swap (CjMatrix<T> & a, CjMatrix<T> & b) { a.swap(b); }
00433 }
00434
00435 template <typename T> std::string dumpMatrix( const CjMatrix<T> & a, const unsigned int cellWidth, const std::string & rowPrefix, const bool blankDiagonal = false, const unsigned int max_col = 0, const unsigned int max_row = 0 ) {
00436 std::stringstream ss(std::ios_base::out);
00437 dumpMatrixTmpl(ss, a, cellWidth, rowPrefix, blankDiagonal, max_col, max_row);
00438 return ss.str();
00439 }
00440
00441 template <typename T> std::ostream & operator<<(std::ostream & out, const CjMatrix<T> & a ){
00442 dumpMatrixTmpl(out, a, 0, "", false, 0, 0);
00443 return out;
00444 }
00445
00446 template <typename T> void dumpMatrixTmpl(std::ostream &os, const CjMatrix<T> & a, const unsigned int cellWidth, const std::string & rowPrefix, const bool blankDiagonal, const unsigned int max_col , const unsigned int max_row ) {
00447
00448 unsigned int h = (max_row==0)?a.getHeight():std::min(max_row,a.getHeight());
00449 unsigned int w = (max_col==0)?a.getWidth():std::min(max_col,a.getWidth());
00450 std::cout << "dumpMatrixTmpl: height is " << h << " and width is " << w << std::endl;
00451 std::cout << "dumpMatrixTmpl: rowPrefix is " << rowPrefix << std::endl;
00452 for( unsigned int i = 0; i < h; ++i) {
00453 os << std::setw(0);
00454 os << rowPrefix.c_str();
00455 os << std::right;
00456 typename CjMatrix<T>::const_row_t row = a.row(i);
00457 unsigned int j = 0;
00458 for( typename CjMatrix<T>::const_row_t::const_iterator it = row.cbegin(); (it != row.cend()) && (j!=w) ; ++it, ++j) {
00459 os << ((cellWidth==0)?"\t":"") << std::setw(cellWidth) << ((blankDiagonal&&(i==j))?0:(*it));
00460 }
00461 if(i!=(h-1)) os << std::endl;
00462 }
00463 }
00464
00465 #endif // _CJMATRIX_H_