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
00037 namespace internal {
00038 template<typename _Scalar, int _Flags, typename _Index>
00039 struct traits<MappedSparseMatrix<_Scalar, _Flags, _Index> > : traits<SparseMatrix<_Scalar, _Flags, _Index> >
00040 {};
00041 }
00042
00043 template<typename _Scalar, int _Flags, typename _Index>
00044 class MappedSparseMatrix
00045 : public SparseMatrixBase<MappedSparseMatrix<_Scalar, _Flags, _Index> >
00046 {
00047 public:
00048 EIGEN_SPARSE_PUBLIC_INTERFACE(MappedSparseMatrix)
00049
00050 protected:
00051 enum { IsRowMajor = Base::IsRowMajor };
00052
00053 Index m_outerSize;
00054 Index m_innerSize;
00055 Index m_nnz;
00056 Index* m_outerIndex;
00057 Index* m_innerIndices;
00058 Scalar* m_values;
00059
00060 public:
00061
00062 inline Index rows() const { return IsRowMajor ? m_outerSize : m_innerSize; }
00063 inline Index cols() const { return IsRowMajor ? m_innerSize : m_outerSize; }
00064 inline Index innerSize() const { return m_innerSize; }
00065 inline Index outerSize() const { return m_outerSize; }
00066 inline Index innerNonZeros(Index j) const { return m_outerIndex[j+1]-m_outerIndex[j]; }
00067
00068
00069
00070 inline const Scalar* _valuePtr() const { return m_values; }
00071 inline Scalar* _valuePtr() { return m_values; }
00072
00073 inline const Index* _innerIndexPtr() const { return m_innerIndices; }
00074 inline Index* _innerIndexPtr() { return m_innerIndices; }
00075
00076 inline const Index* _outerIndexPtr() const { return m_outerIndex; }
00077 inline Index* _outerIndexPtr() { return m_outerIndex; }
00078
00079
00080 inline Scalar coeff(Index row, Index col) const
00081 {
00082 const Index outer = IsRowMajor ? row : col;
00083 const Index inner = IsRowMajor ? col : row;
00084
00085 Index start = m_outerIndex[outer];
00086 Index end = m_outerIndex[outer+1];
00087 if (start==end)
00088 return Scalar(0);
00089 else if (end>0 && inner==m_innerIndices[end-1])
00090 return m_values[end-1];
00091
00092
00093
00094 const Index* r = std::lower_bound(&m_innerIndices[start],&m_innerIndices[end-1],inner);
00095 const Index id = r-&m_innerIndices[0];
00096 return ((*r==inner) && (id<end)) ? m_values[id] : Scalar(0);
00097 }
00098
00099 inline Scalar& coeffRef(Index row, Index col)
00100 {
00101 const Index outer = IsRowMajor ? row : col;
00102 const Index inner = IsRowMajor ? col : row;
00103
00104 Index start = m_outerIndex[outer];
00105 Index end = m_outerIndex[outer+1];
00106 eigen_assert(end>=start && "you probably called coeffRef on a non finalized matrix");
00107 eigen_assert(end>start && "coeffRef cannot be called on a zero coefficient");
00108 Index* r = std::lower_bound(&m_innerIndices[start],&m_innerIndices[end],inner);
00109 const Index id = r-&m_innerIndices[0];
00110 eigen_assert((*r==inner) && (id<end) && "coeffRef cannot be called on a zero coefficient");
00111 return m_values[id];
00112 }
00113
00114 class InnerIterator;
00115
00117 inline Index nonZeros() const { return m_nnz; }
00118
00119 inline MappedSparseMatrix(Index rows, Index cols, Index nnz, Index* outerIndexPtr, Index* innerIndexPtr, Scalar* valuePtr)
00120 : m_outerSize(IsRowMajor?rows:cols), m_innerSize(IsRowMajor?cols:rows), m_nnz(nnz), m_outerIndex(outerIndexPtr),
00121 m_innerIndices(innerIndexPtr), m_values(valuePtr)
00122 {}
00123
00125 inline ~MappedSparseMatrix() {}
00126 };
00127
00128 template<typename Scalar, int _Flags, typename _Index>
00129 class MappedSparseMatrix<Scalar,_Flags,_Index>::InnerIterator
00130 {
00131 public:
00132 InnerIterator(const MappedSparseMatrix& mat, Index outer)
00133 : m_matrix(mat),
00134 m_outer(outer),
00135 m_id(mat._outerIndexPtr()[outer]),
00136 m_start(m_id),
00137 m_end(mat._outerIndexPtr()[outer+1])
00138 {}
00139
00140 template<unsigned int Added, unsigned int Removed>
00141 InnerIterator(const Flagged<MappedSparseMatrix,Added,Removed>& mat, Index outer)
00142 : m_matrix(mat._expression()), m_id(m_matrix._outerIndexPtr()[outer]),
00143 m_start(m_id), m_end(m_matrix._outerIndexPtr()[outer+1])
00144 {}
00145
00146 inline InnerIterator& operator++() { m_id++; return *this; }
00147
00148 inline Scalar value() const { return m_matrix._valuePtr()[m_id]; }
00149 inline Scalar& valueRef() { return const_cast<Scalar&>(m_matrix._valuePtr()[m_id]); }
00150
00151 inline Index index() const { return m_matrix._innerIndexPtr()[m_id]; }
00152 inline Index row() const { return IsRowMajor ? m_outer : index(); }
00153 inline Index col() const { return IsRowMajor ? index() : m_outer; }
00154
00155 inline operator bool() const { return (m_id < m_end) && (m_id>=m_start); }
00156
00157 protected:
00158 const MappedSparseMatrix& m_matrix;
00159 const Index m_outer;
00160 Index m_id;
00161 const Index m_start;
00162 const Index m_end;
00163 };
00164
00165 #endif // EIGEN_MAPPED_SPARSEMATRIX_H