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