QGpCoreTools/MatrixIterator.h
Go to the documentation of this file.
00001 /***************************************************************************
00002 **
00003 **  This file is part of QGpCoreTools.
00004 **
00005 **  This library is free software; you can redistribute it and/or
00006 **  modify it under the terms of the GNU Lesser General Public
00007 **  License as published by the Free Software Foundation; either
00008 **  version 2.1 of the License, or (at your option) any later version.
00009 **
00010 **  This file is distributed in the hope that it will be useful, but WITHOUT
00011 **  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 **  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00013 **  License for more details.
00014 **
00015 **  You should have received a copy of the GNU Lesser General Public
00016 **  License along with this library; if not, write to the Free Software
00017 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00018 **
00019 **  See http://www.geopsy.org for more information.
00020 **
00021 **  Created : 2008-07-15
00022 **  Authors :
00023 **    Marc Wathelet
00024 **    Marc Wathelet (LGIT, Grenoble, France)
00025 **
00026 ***************************************************************************/
00027 
00028 #ifndef MATRIXITERATOR_H
00029 #define MATRIXITERATOR_H
00030 
00031 #include "QGpCoreToolsDLLExport.h"
00032 #include "Matrix.h"
00033 
00034 namespace QGpCoreTools {
00035 
00036 template <typename T> class QGPCORETOOLS_EXPORT MatrixIterator
00037 {
00038 public:
00039   inline MatrixIterator(const Matrix<T>& m);
00040 
00041   inline const T * next() {_cur += _offset; return _cur;}
00042   inline const T * previous() {_cur -= _offset; return _cur;}
00043   inline const T& operator*() {ASSERT(_cur >= _start && _cur <= _end); return *_cur;}
00044   bool hasNext() const {return _cur < _end;}
00045   bool hasPrevious() const {return _cur > _start;}
00046   void toBack() {return _cur=_end;}
00047   void toFront() {return _cur=_start;}
00048 protected:
00049   MatrixIterator() {}
00050 
00051   const T * _cur, * _start, * _end;
00052   int _offset;
00053 };
00054 
00055 template <typename T> class QGPCORETOOLS_EXPORT MutableMatrixIterator
00056 {
00057 public:
00058   inline MutableMatrixIterator(Matrix<T>& m);
00059 
00060   inline T * next() {_cur+=_offset; return _cur;}
00061   inline T * previous() {_cur-=_offset; return _cur;}
00062   inline T& operator*() {ASSERT(_cur>=_start && _cur<=_end); return *_cur;}
00063   bool hasNext() const {return _cur<_end;}
00064   bool hasPrevious() const {return _cur>_start;}
00065   void toBack() {return _cur=_end+_offset;}
00066   void toFront() {return _cur=_start-_offset;}
00067 protected:
00068   MutableMatrixIterator() {}
00069 
00070   T * _cur, * _start, * _end;
00071   int _offset;
00072 };
00073 
00074 template <typename T> class QGPCORETOOLS_EXPORT MatrixRowIterator : public MatrixIterator<T>
00075 {
00076 public:
00077   inline MatrixRowIterator(const Matrix<T>& m, int iRow);
00078 };
00079 
00080 template <typename T> class QGPCORETOOLS_EXPORT MatrixColumnIterator : public MatrixIterator<T>
00081 {
00082 public:
00083   inline MatrixColumnIterator(const Matrix<T>& m, int iCol);
00084 };
00085 
00086 template <typename T>
00087 inline MatrixIterator<T>::MatrixIterator(const Matrix<T>& m)
00088 {
00089   TRACE;
00090   _offset=1;
00091   _start=m._d->values();
00092   _end=_start+m.rowCount()*m.columnCount()-1;
00093   _cur=_start-_offset;
00094 }
00095 
00096 template <typename T>
00097 inline MutableMatrixIterator<T>::MutableMatrixIterator(Matrix<T>& m)
00098 {
00099   TRACE;
00100   _offset=1;
00101   _start=m._d->values();
00102   _end=_start+m.rowCount()*m.columnCount()-1;
00103   _cur=_start-_offset;
00104 }
00105 
00106 template <typename T>
00107 inline MatrixRowIterator<T>::MatrixRowIterator(const Matrix<T>& m, int iRow)
00108    : MatrixIterator<T>()
00109 {
00110   TRACE;
00111   ASSERT(iRow>=0 && iRow<m.rowCount());
00112   MatrixIterator<T>::_offset=m.rowCount();
00113   MatrixIterator<T>::_start=m._d->values() + iRow;
00114   MatrixIterator<T>::_end=MatrixIterator<T>::_start+m.rowCount()*(m.columnCount()-1);
00115   MatrixIterator<T>::_cur=MatrixIterator<T>::_start-MatrixIterator<T>::_offset;
00116 }
00117 
00118 template <typename T>
00119 inline MatrixColumnIterator<T>::MatrixColumnIterator(const Matrix<T>& m, int iCol)
00120    : MatrixIterator<T>()
00121 {
00122   TRACE;
00123   ASSERT(iCol>=0 && iCol<m.columnCount());
00124   MatrixIterator<T>::_offset=1;
00125   MatrixIterator<T>::_start=m._d->values()+m.rowCount()*iCol;
00126   MatrixIterator<T>::_end=MatrixIterator<T>::_start+m.rowCount()-1;
00127   MatrixIterator<T>::_cur=MatrixIterator<T>::_start-MatrixIterator<T>::_offset;
00128 }
00129 
00130 template <typename T> class QGPCORETOOLS_EXPORT MutableMatrixRowIterator : public MatrixIterator<T>
00131 {
00132 public:
00133   inline MutableMatrixRowIterator(const Matrix<T>& m, int iRow);
00134 };
00135 
00136 template <typename T>
00137 inline MutableMatrixRowIterator<T>::MutableMatrixRowIterator(const Matrix<T>& m, int iRow)
00138    : MutableMatrixIterator<T>()
00139 {
00140   TRACE;
00141   ASSERT(iRow >= 0 && iRow < m.rowCount());
00142   MutableMatrixIterator<T>::_offset=m.rowCount();
00143   MutableMatrixIterator<T>::_start=m._data.data()+iRow;
00144   MutableMatrixIterator<T>::_end=MutableMatrixIterator<T>::_start+m.rowCount()*(m.columnCount()-1);
00145   MutableMatrixIterator<T>::_cur=MutableMatrixIterator<T>::_start-m.rowCount();
00146 }
00147 
00148 template <typename T> class QGPCORETOOLS_EXPORT MutableMatrixColumnIterator : public MatrixIterator<T>
00149 {
00150 public:
00151   inline MutableMatrixColumnIterator(const Matrix<T>& m, int iCol);
00152 };
00153 
00154 template <typename T>
00155 inline MutableMatrixColumnIterator<T>::MutableMatrixColumnIterator(const Matrix<T>& m, int iCol)
00156    : MutableMatrixIterator<T>()
00157 {
00158   TRACE;
00159   ASSERT(iCol >= 0 && iCol < m.columnCount());
00160   MutableMatrixIterator<T>::_offset=1;
00161   MutableMatrixIterator<T>::_start=m._data.data() + m.rowCount() * iCol;
00162   MutableMatrixIterator<T>::_end=MutableMatrixIterator<T>::_start + m.rowCount() - 1;
00163   MutableMatrixIterator<T>::_cur=MutableMatrixIterator<T>::_start - 1;
00164 }
00165 
00166 } // namespace QGpCoreTools
00167 
00168 #endif // MATRIXITERATOR_H
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Defines