📄 matrixiterator.hpp
字号:
#ifndef _SDL_MATHS_MATRIX_ITERATOR_HPP_
#define _SDL_MATHS_MATRIX_ITERATOR_HPP_
SDL_MATHS_MATRIX_BEGIN
class MatrixIterator{};
template<
typename _DataType,
typename _NumberType,
typename _DerivedType
>
struct MatrixStoredElementIterator
: public MatrixIterator
{
typedef MatrixStoredElementIterator SelfType;
typedef _NumberType NumberType;
typedef _DataType DataType;
typedef _DerivedType DerivedType;
MatrixStoredElementIterator(DataType * _pData, Int32 _Row, Int32 _Col)
: pData(_pData),
Row(_Row), Col(_Col),
CurrRow(0), CurrCol(0),
ok(true)
{
}
MatrixStoredElementIterator(const SelfType& other)
: pData(other.pData),
Row(other.Row), Col(other.Col),
CurrRow(other.CurrRow), CurrCol(other.CurrCol),
ok(other.ok)
{
}
Bool good() const
{
return ok;
}
Int32 RowIdx() const
{
return CurrRow + INDEX_OFFSET;
}
Int32 ColIdx() const
{
return CurrCol + INDEX_OFFSET;
}
Int32 row() const
{
return Row;
}
Int32 col() const
{
return Col;
}
void Reset()
{
CurrRow = CurrCol = 0;
ok = true;
}
operator Bool () const
{
return ok;
}
protected:
DataType * pData;
Int32 Row, Col;
Int32 CurrRow, CurrCol;
Bool ok;
};
template<
typename _DataType,
typename _NumberType
>
struct FullMatrixStoredElementIterator
: public MatrixStoredElementIterator<_DataType, _NumberType,
FullMatrixStoredElementIterator<_DataType, _NumberType> >
{
typedef FullMatrixStoredElementIterator SelfType;
typedef _NumberType NumberType;
typedef _DataType DataType;
typedef MatrixStoredElementIterator<_DataType, _NumberType, SelfType >
BaseType;
using BaseType::pData;
using BaseType::Row;
using BaseType::Col;
using BaseType::CurrRow;
using BaseType::CurrCol;
using BaseType::ok;
FullMatrixStoredElementIterator(DataType * _pData, Int32 _Row, Int32 _Col)
: BaseType(_pData, _Row, _Col)
{
}
SelfType& operator ++()
{
MoveNext();
return *this;
}
SelfType operator ++(int)
{
SelfType t(*this);
++*this;
return t;
}
NumberType& operator * ()
{
return pData[CurrRow * Col + CurrCol];
}
Bool MoveNext()
{
if (++CurrCol == Col)
{
CurrCol = 0;
if (++CurrRow == Row)
{
ok = false;
}
}
return ok;
}
};
template<
typename _DataType,
typename _NumberType
>
struct ConstFullMatrixStoredElementIterator
: public FullMatrixStoredElementIterator<_DataType, _NumberType>
{
typedef ConstFullMatrixStoredElementIterator SelfType;
typedef _NumberType NumberType;
typedef _DataType DataType;
typedef FullMatrixStoredElementIterator<_DataType, _NumberType>
BaseType;
using BaseType::pData;
using BaseType::Row;
using BaseType::Col;
using BaseType::CurrRow;
using BaseType::CurrCol;
using BaseType::ok;
using BaseType::MoveNext;
ConstFullMatrixStoredElementIterator(DataType * _pData, Int32 _Row, Int32 _Col)
: BaseType(_pData, _Row, _Col)
{
}
SelfType& operator ++()
{
MoveNext();
return *this;
}
SelfType operator ++(int)
{
SelfType t(*this);
++*this;
return t;
}
NumberType operator * ()
{
return pData[CurrRow * Col + CurrCol];
}
};
template<
typename _DataType,
typename _NumberType
>
struct DiagMatrixStoredElementIterator
: public MatrixStoredElementIterator<_DataType, _NumberType,
DiagMatrixStoredElementIterator<_DataType, _NumberType> >
{
typedef DiagMatrixStoredElementIterator SelfType;
typedef _NumberType NumberType;
typedef _DataType DataType;
typedef MatrixStoredElementIterator<_DataType, _NumberType, SelfType >
BaseType;
using BaseType::pData;
using BaseType::Row;
using BaseType::Col;
using BaseType::CurrRow;
using BaseType::CurrCol;
using BaseType::ok;
DiagMatrixStoredElementIterator(DataType * _pData, Int32 _Row, Int32 _Col)
: BaseType(_pData, _Row, _Col)
{
}
SelfType& operator ++()
{
MoveNext();
return *this;
}
SelfType operator ++(int)
{
SelfType t(*this);
++*this;
return t;
}
NumberType& operator * ()
{
return pData[CurrRow];
}
Bool MoveNext()
{
if (++CurrRow == Row)
{
ok = false;
}
return ok;
}
};
template<
typename _DataType,
typename _NumberType
>
struct ConstDiagMatrixStoredElementIterator
: public DiagMatrixStoredElementIterator<_DataType, _NumberType>
{
typedef ConstDiagMatrixStoredElementIterator SelfType;
typedef _NumberType NumberType;
typedef _DataType DataType;
typedef DiagMatrixStoredElementIterator<_DataType, _NumberType>
BaseType;
using BaseType::pData;
using BaseType::Row;
using BaseType::Col;
using BaseType::CurrRow;
using BaseType::CurrCol;
using BaseType::ok;
using BaseType::MoveNext;
ConstDiagMatrixStoredElementIterator(DataType * _pData, Int32 _Row, Int32 _Col)
: BaseType(_pData, _Row, _Col)
{
}
SelfType& operator ++()
{
MoveNext();
return *this;
}
SelfType operator ++(int)
{
SelfType t(*this);
++*this;
return t;
}
NumberType operator * ()
{
return pData[CurrRow];
}
};
template<
typename _DataType,
typename _NumberType
>
struct KDiagMatrixStoredElementIterator
: public MatrixStoredElementIterator<_DataType, _NumberType,
KDiagMatrixStoredElementIterator<_DataType, _NumberType> >
{
typedef KDiagMatrixStoredElementIterator SelfType;
typedef _NumberType NumberType;
typedef _DataType DataType;
typedef MatrixStoredElementIterator<_DataType, _NumberType, SelfType >
BaseType;
using BaseType::pData;
using BaseType::Row;
using BaseType::Col;
using BaseType::CurrRow;
using BaseType::CurrCol;
using BaseType::ok;
KDiagMatrixStoredElementIterator(DataType * _pData, Int32 _Row, Int32 _Col)
: BaseType(_pData, _Row, _Col), K(_Col), HalfWidth((K + 1) >> 1)
{
}
/*
*
* Hide BaseType::row, BaseType::col
*
*/
Int32 row() const
{
return Row;
}
Int32 col() const
{
return Row;
}
SelfType& operator ++()
{
MoveNext();
return *this;
}
SelfType operator ++(int)
{
SelfType t(*this);
++*this;
return t;
}
NumberType& operator * ()
{
return pData[index(CurrRow, CurrCol)];
}
Bool MoveNext()
{
if (++CurrCol == CurrRow + HalfWidth || CurrCol == Row)
{
if (++CurrRow == Row)
{
ok = false;
}
else
{
CurrCol = 0;
if (CurrRow-HalfWidth+1 > CurrCol)
{
CurrCol = CurrRow-HalfWidth+1;
}
}
}
return ok;
}
protected:
Int32 index(Int32 _row, Int32 _col) const
{
return _row < HalfWidth ? _row * K + _col : _row * K + _col - (_row - HalfWidth);
}
protected:
Int32 K;
Int32 HalfWidth;
};
template<
typename _DataType,
typename _NumberType
>
struct ConstKDiagMatrixStoredElementIterator
: public KDiagMatrixStoredElementIterator<_DataType, _NumberType>
{
typedef ConstKDiagMatrixStoredElementIterator SelfType;
typedef _NumberType NumberType;
typedef _DataType DataType;
typedef KDiagMatrixStoredElementIterator<_DataType, _NumberType>
BaseType;
using BaseType::pData;
using BaseType::Row;
using BaseType::Col;
using BaseType::CurrRow;
using BaseType::CurrCol;
using BaseType::ok;
using BaseType::MoveNext;
ConstKDiagMatrixStoredElementIterator(DataType * _pData, Int32 _Row, Int32 _Col)
: BaseType(_pData, _Row, _Col)
{
}
SelfType& operator ++()
{
MoveNext();
return *this;
}
SelfType operator ++(int)
{
SelfType t(*this);
++*this;
return t;
}
NumberType operator * ()
{
return pData[index(CurrRow, CurrCol)];
}
};
template<
typename _DataType,
typename _NumberType
>
struct UpperMatrixStoredElementIterator
: public MatrixStoredElementIterator<_DataType, _NumberType,
UpperMatrixStoredElementIterator<_DataType, _NumberType> >
{
typedef UpperMatrixStoredElementIterator SelfType;
typedef _NumberType NumberType;
typedef _DataType DataType;
typedef MatrixStoredElementIterator<_DataType, _NumberType, SelfType >
BaseType;
using BaseType::pData;
using BaseType::Row;
using BaseType::Col;
using BaseType::CurrRow;
using BaseType::CurrCol;
using BaseType::ok;
UpperMatrixStoredElementIterator(DataType * _pData, Int32 _Row, Int32 _Col)
: BaseType(_pData, _Row, _Col)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -