Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef EIGEN_MAPPED_SPARSEMATRIX_H
00011 #define EIGEN_MAPPED_SPARSEMATRIX_H
00012
00013 namespace Eigen {
00014
00024 namespace internal {
00025 template<typename _Scalar, int _Flags, typename _Index>
00026 struct traits<MappedSparseMatrix<_Scalar, _Flags, _Index> > : traits<SparseMatrix<_Scalar, _Flags, _Index> >
00027 {};
00028 }
00029
00030 template<typename _Scalar, int _Flags, typename _Index>
00031 class MappedSparseMatrix
00032 : public SparseMatrixBase<MappedSparseMatrix<_Scalar, _Flags, _Index> >
00033 {
00034 public:
00035 EIGEN_SPARSE_PUBLIC_INTERFACE(MappedSparseMatrix)
00036 enum { IsRowMajor = Base::IsRowMajor };
00037
00038 protected:
00039
00040 Index m_outerSize;
00041 Index m_innerSize;
00042 Index m_nnz;
00043 Index* m_outerIndex;
00044 Index* m_innerIndices;
00045 Scalar* m_values;
00046
00047 public:
00048
00049 inline Index rows() const { return IsRowMajor ? m_outerSize : m_innerSize; }
00050 inline Index cols() const { return IsRowMajor ? m_innerSize : m_outerSize; }
00051 inline Index innerSize() const { return m_innerSize; }
00052 inline Index outerSize() const { return m_outerSize; }
00053
00054
00055
00056 inline const Scalar* valuePtr() const { return m_values; }
00057 inline Scalar* valuePtr() { return m_values; }
00058
00059 inline const Index* innerIndexPtr() const { return m_innerIndices; }
00060 inline Index* innerIndexPtr() { return m_innerIndices; }
00061
00062 inline const Index* outerIndexPtr() const { return m_outerIndex; }
00063 inline Index* outerIndexPtr() { return m_outerIndex; }
00064
00065
00066 inline Scalar coeff(Index row, Index col) const
00067 {
00068 const Index outer = IsRowMajor ? row : col;
00069 const Index inner = IsRowMajor ? col : row;
00070
00071 Index start = m_outerIndex[outer];
00072 Index end = m_outerIndex[outer+1];
00073 if (start==end)
00074 return Scalar(0);
00075 else if (end>0 && inner==m_innerIndices[end-1])
00076 return m_values[end-1];
00077
00078
00079
00080 const Index* r = std::lower_bound(&m_innerIndices[start],&m_innerIndices[end-1],inner);
00081 const Index id = r-&m_innerIndices[0];
00082 return ((*r==inner) && (id<end)) ? m_values[id] : Scalar(0);
00083 }
00084
00085 inline Scalar& coeffRef(Index row, Index col)
00086 {
00087 const Index outer = IsRowMajor ? row : col;
00088 const Index inner = IsRowMajor ? col : row;
00089
00090 Index start = m_outerIndex[outer];
00091 Index end = m_outerIndex[outer+1];
00092 eigen_assert(end>=start && "you probably called coeffRef on a non finalized matrix");
00093 eigen_assert(end>start && "coeffRef cannot be called on a zero coefficient");
00094 Index* r = std::lower_bound(&m_innerIndices[start],&m_innerIndices[end],inner);
00095 const Index id = r-&m_innerIndices[0];
00096 eigen_assert((*r==inner) && (id<end) && "coeffRef cannot be called on a zero coefficient");
00097 return m_values[id];
00098 }
00099
00100 class InnerIterator;
00101 class ReverseInnerIterator;
00102
00104 inline Index nonZeros() const { return m_nnz; }
00105
00106 inline MappedSparseMatrix(Index rows, Index cols, Index nnz, Index* outerIndexPtr, Index* innerIndexPtr, Scalar* valuePtr)
00107 : m_outerSize(IsRowMajor?rows:cols), m_innerSize(IsRowMajor?cols:rows), m_nnz(nnz), m_outerIndex(outerIndexPtr),
00108 m_innerIndices(innerIndexPtr), m_values(valuePtr)
00109 {}
00110
00112 inline ~MappedSparseMatrix() {}
00113 };
00114
00115 template<typename Scalar, int _Flags, typename _Index>
00116 class MappedSparseMatrix<Scalar,_Flags,_Index>::InnerIterator
00117 {
00118 public:
00119 InnerIterator(const MappedSparseMatrix& mat, Index outer)
00120 : m_matrix(mat),
00121 m_outer(outer),
00122 m_id(mat.outerIndexPtr()[outer]),
00123 m_start(m_id),
00124 m_end(mat.outerIndexPtr()[outer+1])
00125 {}
00126
00127 inline InnerIterator& operator++() { m_id++; return *this; }
00128
00129 inline Scalar value() const { return m_matrix.valuePtr()[m_id]; }
00130 inline Scalar& valueRef() { return const_cast<Scalar&>(m_matrix.valuePtr()[m_id]); }
00131
00132 inline Index index() const { return m_matrix.innerIndexPtr()[m_id]; }
00133 inline Index row() const { return IsRowMajor ? m_outer : index(); }
00134 inline Index col() const { return IsRowMajor ? index() : m_outer; }
00135
00136 inline operator bool() const { return (m_id < m_end) && (m_id>=m_start); }
00137
00138 protected:
00139 const MappedSparseMatrix& m_matrix;
00140 const Index m_outer;
00141 Index m_id;
00142 const Index m_start;
00143 const Index m_end;
00144 };
00145
00146 template<typename Scalar, int _Flags, typename _Index>
00147 class MappedSparseMatrix<Scalar,_Flags,_Index>::ReverseInnerIterator
00148 {
00149 public:
00150 ReverseInnerIterator(const MappedSparseMatrix& mat, Index outer)
00151 : m_matrix(mat),
00152 m_outer(outer),
00153 m_id(mat.outerIndexPtr()[outer+1]),
00154 m_start(mat.outerIndexPtr()[outer]),
00155 m_end(m_id)
00156 {}
00157
00158 inline ReverseInnerIterator& operator--() { m_id--; return *this; }
00159
00160 inline Scalar value() const { return m_matrix.valuePtr()[m_id-1]; }
00161 inline Scalar& valueRef() { return const_cast<Scalar&>(m_matrix.valuePtr()[m_id-1]); }
00162
00163 inline Index index() const { return m_matrix.innerIndexPtr()[m_id-1]; }
00164 inline Index row() const { return IsRowMajor ? m_outer : index(); }
00165 inline Index col() const { return IsRowMajor ? index() : m_outer; }
00166
00167 inline operator bool() const { return (m_id <= m_end) && (m_id>m_start); }
00168
00169 protected:
00170 const MappedSparseMatrix& m_matrix;
00171 const Index m_outer;
00172 Index m_id;
00173 const Index m_start;
00174 const Index m_end;
00175 };
00176
00177 }
00178
00179 #endif // EIGEN_MAPPED_SPARSEMATRIX_H