10 #ifndef EIGEN_SPARSE_MAP_H 11 #define EIGEN_SPARSE_MAP_H 17 template<
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
18 struct traits<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
19 :
public traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >
21 typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
22 typedef traits<PlainObjectType> TraitsBase;
24 Flags = TraitsBase::Flags & (~NestByRefBit)
28 template<
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
29 struct traits<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
30 :
public traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >
32 typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
33 typedef traits<PlainObjectType> TraitsBase;
35 Flags = TraitsBase::Flags & (~ (NestByRefBit |
LvalueBit))
41 template<
typename Derived,
43 >
class SparseMapBase;
45 template<
typename Derived>
47 :
public SparseCompressedBase<Derived>
50 typedef SparseCompressedBase<Derived> Base;
51 typedef typename Base::Scalar Scalar;
52 typedef typename Base::StorageIndex StorageIndex;
53 enum { IsRowMajor = Base::IsRowMajor };
54 using Base::operator=;
57 typedef typename internal::conditional<
58 bool(internal::is_lvalue<Derived>::value),
59 Scalar *,
const Scalar *>::type ScalarPointer;
60 typedef typename internal::conditional<
61 bool(internal::is_lvalue<Derived>::value),
62 StorageIndex *,
const StorageIndex *>::type IndexPointer;
66 Array<StorageIndex,2,1> m_zero_nnz;
67 IndexPointer m_outerIndex;
68 IndexPointer m_innerIndices;
69 ScalarPointer m_values;
70 IndexPointer m_innerNonZeros;
74 inline Index rows()
const {
return IsRowMajor ? m_outerSize : m_innerSize; }
75 inline Index cols()
const {
return IsRowMajor ? m_innerSize : m_outerSize; }
76 inline Index innerSize()
const {
return m_innerSize; }
77 inline Index outerSize()
const {
return m_outerSize; }
78 inline Index nonZeros()
const {
return m_zero_nnz[1]; }
80 bool isCompressed()
const {
return m_innerNonZeros==0; }
84 inline const Scalar* valuePtr()
const {
return m_values; }
85 inline const StorageIndex* innerIndexPtr()
const {
return m_innerIndices; }
86 inline const StorageIndex* outerIndexPtr()
const {
return m_outerIndex; }
87 inline const StorageIndex* innerNonZeroPtr()
const {
return m_innerNonZeros; }
90 inline Scalar coeff(Index row, Index col)
const 92 const Index outer = IsRowMajor ? row : col;
93 const Index inner = IsRowMajor ? col : row;
95 Index start = m_outerIndex[outer];
96 Index end = isCompressed() ? m_outerIndex[outer+1] : start + m_innerNonZeros[outer];
99 else if (end>0 && inner==m_innerIndices[end-1])
100 return m_values[end-1];
104 const StorageIndex* r = std::lower_bound(&m_innerIndices[start],&m_innerIndices[end-1],inner);
105 const Index
id = r-&m_innerIndices[0];
106 return ((*r==inner) && (
id<end)) ? m_values[id] : Scalar(0);
109 inline SparseMapBase(Index rows, Index cols, Index nnz, IndexPointer outerIndexPtr, IndexPointer innerIndexPtr,
110 ScalarPointer valuePtr, IndexPointer innerNonZerosPtr = 0)
111 : m_outerSize(IsRowMajor?rows:cols), m_innerSize(IsRowMajor?cols:rows), m_zero_nnz(0,
internal::convert_index<StorageIndex>(nnz)), m_outerIndex(outerIndexPtr),
112 m_innerIndices(innerIndexPtr), m_values(valuePtr), m_innerNonZeros(innerNonZerosPtr)
116 inline SparseMapBase(Index size, Index nnz, IndexPointer innerIndexPtr, ScalarPointer valuePtr)
117 : m_outerSize(1), m_innerSize(size), m_zero_nnz(0,
internal::convert_index<StorageIndex>(nnz)), m_outerIndex(m_zero_nnz.data()),
118 m_innerIndices(innerIndexPtr), m_values(valuePtr), m_innerNonZeros(0)
122 inline ~SparseMapBase() {}
125 inline SparseMapBase() {}
128 template<
typename Derived>
130 :
public SparseMapBase<Derived,ReadOnlyAccessors>
132 typedef MapBase<Derived, ReadOnlyAccessors> ReadOnlyMapBase;
135 typedef SparseMapBase<Derived, ReadOnlyAccessors> Base;
136 typedef typename Base::Scalar Scalar;
137 typedef typename Base::StorageIndex StorageIndex;
138 enum { IsRowMajor = Base::IsRowMajor };
140 using Base::operator=;
146 using Base::valuePtr;
147 using Base::innerIndexPtr;
148 using Base::outerIndexPtr;
149 using Base::innerNonZeroPtr;
150 inline Scalar* valuePtr() {
return Base::m_values; }
151 inline StorageIndex* innerIndexPtr() {
return Base::m_innerIndices; }
152 inline StorageIndex* outerIndexPtr() {
return Base::m_outerIndex; }
153 inline StorageIndex* innerNonZeroPtr() {
return Base::m_innerNonZeros; }
156 inline Scalar& coeffRef(Index row, Index col)
158 const Index outer = IsRowMajor ? row : col;
159 const Index inner = IsRowMajor ? col : row;
161 Index start = Base::m_outerIndex[outer];
162 Index end = Base::isCompressed() ? Base::m_outerIndex[outer+1] : start + Base::m_innerNonZeros[outer];
163 eigen_assert(end>=start &&
"you probably called coeffRef on a non finalized matrix");
164 eigen_assert(end>start &&
"coeffRef cannot be called on a zero coefficient");
165 Index* r = std::lower_bound(&Base::m_innerIndices[start],&Base::m_innerIndices[end],inner);
166 const Index
id = r - &Base::m_innerIndices[0];
167 eigen_assert((*r==inner) && (
id<end) &&
"coeffRef cannot be called on a zero coefficient");
168 return const_cast<Scalar*
>(Base::m_values)[
id];
171 inline SparseMapBase(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr, StorageIndex* innerIndexPtr,
172 Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
173 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr)
177 inline SparseMapBase(Index size, Index nnz, StorageIndex* innerIndexPtr, Scalar* valuePtr)
178 : Base(size, nnz, innerIndexPtr, valuePtr)
182 inline ~SparseMapBase() {}
185 inline SparseMapBase() {}
188 template<
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
189 class Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType>
190 :
public SparseMapBase<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
193 typedef SparseMapBase<Map> Base;
194 EIGEN_SPARSE_PUBLIC_INTERFACE(Map)
195 enum { IsRowMajor = Base::IsRowMajor };
199 inline Map(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr,
200 StorageIndex* innerIndexPtr, Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
201 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr)
208 template<
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
209 class Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType>
210 :
public SparseMapBase<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
213 typedef SparseMapBase<Map> Base;
214 EIGEN_SPARSE_PUBLIC_INTERFACE(Map)
215 enum { IsRowMajor = Base::IsRowMajor };
219 inline Map(Index rows, Index cols, Index nnz,
const StorageIndex* outerIndexPtr,
220 const StorageIndex* innerIndexPtr,
const Scalar* valuePtr,
const StorageIndex* innerNonZerosPtr = 0)
221 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr)
230 template<
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
231 struct evaluator<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
232 : evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
234 typedef evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
235 typedef Map<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
236 evaluator() : Base() {}
237 explicit evaluator(
const XprType &mat) : Base(mat) {}
240 template<
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
241 struct evaluator<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
242 : evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
244 typedef evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
245 typedef Map<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
246 evaluator() : Base() {}
247 explicit evaluator(
const XprType &mat) : Base(mat) {}
254 #endif // EIGEN_SPARSE_MAP_H Definition: Constants.h:368
const unsigned int LvalueBit
Definition: Constants.h:138
Definition: Constants.h:366
Definition: Eigen_Colamd.h:54