10#ifndef EIGEN_SPARSE_MAP_H
11#define EIGEN_SPARSE_MAP_H
14#include "./InternalHeaderCheck.h"
20template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
21struct traits<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
22 :
public traits<SparseMatrix<MatScalar, MatOptions, MatIndex> > {
23 typedef SparseMatrix<MatScalar, MatOptions, MatIndex> PlainObjectType;
24 typedef traits<PlainObjectType> TraitsBase;
25 enum { Flags = TraitsBase::Flags & (~NestByRefBit) };
28template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
29struct traits<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
30 :
public traits<SparseMatrix<MatScalar, MatOptions, MatIndex> > {
31 typedef SparseMatrix<MatScalar, MatOptions, MatIndex> PlainObjectType;
32 typedef traits<PlainObjectType> TraitsBase;
33 enum { Flags = TraitsBase::Flags & (~(NestByRefBit |
LvalueBit)) };
38template <
typename Derived,
46template <
typename Derived>
49 typedef SparseCompressedBase<Derived> Base;
50 typedef typename Base::Scalar Scalar;
51 typedef typename Base::StorageIndex StorageIndex;
52 enum { IsRowMajor = Base::IsRowMajor };
53 using Base::operator=;
56 typedef std::conditional_t<bool(internal::is_lvalue<Derived>::value), Scalar*,
const Scalar*> ScalarPointer;
57 typedef std::conditional_t<bool(internal::is_lvalue<Derived>::value), StorageIndex*,
const StorageIndex*>
62 Array<StorageIndex, 2, 1> m_zero_nnz;
63 IndexPointer m_outerIndex;
64 IndexPointer m_innerIndices;
65 ScalarPointer m_values;
66 IndexPointer m_innerNonZeros;
70 inline Index rows()
const {
return IsRowMajor ? m_outerSize : m_innerSize; }
72 inline Index cols()
const {
return IsRowMajor ? m_innerSize : m_outerSize; }
74 inline Index innerSize()
const {
return m_innerSize; }
76 inline Index outerSize()
const {
return m_outerSize; }
78 inline Index nonZeros()
const {
return m_zero_nnz[1]; }
81 bool isCompressed()
const {
return m_innerNonZeros == 0; }
86 inline const Scalar* valuePtr()
const {
return m_values; }
88 inline const StorageIndex* innerIndexPtr()
const {
return m_innerIndices; }
90 inline const StorageIndex* outerIndexPtr()
const {
return m_outerIndex; }
92 inline const StorageIndex* innerNonZeroPtr()
const {
return m_innerNonZeros; }
96 inline Scalar coeff(
Index row,
Index col)
const {
97 const Index outer = IsRowMajor ? row : col;
98 const Index inner = IsRowMajor ? col : row;
100 Index start = m_outerIndex[outer];
101 Index end = isCompressed() ? m_outerIndex[outer + 1] : start + m_innerNonZeros[outer];
104 else if (end > 0 && inner == m_innerIndices[end - 1])
105 return m_values[end - 1];
109 const StorageIndex* r = std::lower_bound(&m_innerIndices[start], &m_innerIndices[end - 1], inner);
110 const Index id = r - &m_innerIndices[0];
111 return ((*r == inner) && (
id < end)) ? m_values[id] : Scalar(0);
114 inline SparseMapBase(
Index rows,
Index cols,
Index nnz, IndexPointer outerIndexPtr, IndexPointer innerIndexPtr,
115 ScalarPointer valuePtr, IndexPointer innerNonZerosPtr = 0)
116 : m_outerSize(IsRowMajor ? rows : cols),
117 m_innerSize(IsRowMajor ? cols : rows),
118 m_zero_nnz(0, internal::convert_index<StorageIndex>(nnz)),
119 m_outerIndex(outerIndexPtr),
120 m_innerIndices(innerIndexPtr),
122 m_innerNonZeros(innerNonZerosPtr) {}
125 inline SparseMapBase(
Index size,
Index nnz, IndexPointer innerIndexPtr, ScalarPointer valuePtr)
128 m_zero_nnz(0, internal::convert_index<StorageIndex>(nnz)),
129 m_outerIndex(m_zero_nnz.data()),
130 m_innerIndices(innerIndexPtr),
132 m_innerNonZeros(0) {}
135 inline ~SparseMapBase() {}
138 inline SparseMapBase() {}
145template <
typename Derived>
146class SparseMapBase<Derived,
WriteAccessors> :
public SparseMapBase<Derived, ReadOnlyAccessors> {
147 typedef MapBase<Derived, ReadOnlyAccessors> ReadOnlyMapBase;
150 typedef SparseMapBase<Derived, ReadOnlyAccessors> Base;
151 typedef typename Base::Scalar Scalar;
152 typedef typename Base::StorageIndex StorageIndex;
153 enum { IsRowMajor = Base::IsRowMajor };
155 using Base::operator=;
160 using Base::innerIndexPtr;
161 using Base::innerNonZeroPtr;
162 using Base::outerIndexPtr;
163 using Base::valuePtr;
165 inline Scalar*
valuePtr() {
return Base::m_values; }
176 const Index outer = IsRowMajor ? row : col;
177 const Index inner = IsRowMajor ? col : row;
179 Index start = Base::m_outerIndex[outer];
180 Index end = Base::isCompressed() ? Base::m_outerIndex[outer + 1] : start + Base::m_innerNonZeros[outer];
181 eigen_assert(end >= start &&
"you probably called coeffRef on a non finalized matrix");
182 eigen_assert(end > start &&
"coeffRef cannot be called on a zero coefficient");
183 StorageIndex* r = std::lower_bound(&Base::m_innerIndices[start], &Base::m_innerIndices[end], inner);
184 const Index id = r - &Base::m_innerIndices[0];
185 eigen_assert((*r == inner) && (
id < end) &&
"coeffRef cannot be called on a zero coefficient");
186 return const_cast<Scalar*
>(Base::m_values)[
id];
189 inline SparseMapBase(
Index rows,
Index cols,
Index nnz, StorageIndex* outerIndexPtr, StorageIndex* innerIndexPtr,
190 Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
191 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr) {}
194 inline SparseMapBase(Index size, Index nnz, StorageIndex* innerIndexPtr, Scalar* valuePtr)
195 : Base(size, nnz, innerIndexPtr, valuePtr) {}
201 inline SparseMapBase() {}
146class SparseMapBase<Derived,
WriteAccessors> :
public SparseMapBase<Derived, ReadOnlyAccessors> {
…};
213#ifndef EIGEN_PARSED_BY_DOXYGEN
214template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
215class Map<
SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>
216 :
public SparseMapBase<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
218template <typename SparseMatrixType>
219class
Map<SparseMatrixType> :
public SparseMapBase<Derived, WriteAccessors>
223 typedef SparseMapBase<Map> Base;
224 EIGEN_SPARSE_PUBLIC_INTERFACE(
Map)
225 enum { IsRowMajor = Base::IsRowMajor };
238 Scalar*
valuePtr, StorageIndex* innerNonZerosPtr = 0)
240#ifndef EIGEN_PARSED_BY_DOXYGEN
245template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
246class Map<const
SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>
247 :
public SparseMapBase<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > {
249 typedef SparseMapBase<Map> Base;
250 EIGEN_SPARSE_PUBLIC_INTERFACE(
Map)
251 enum { IsRowMajor = Base::IsRowMajor };
261 const Scalar*
valuePtr,
const StorageIndex* innerNonZerosPtr = 0)
219class
Map<SparseMatrixType> :
public SparseMapBase<Derived, WriteAccessors> {
…};
270template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
271struct evaluator<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
272 : evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > > {
273 typedef evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > >
275 typedef Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> XprType;
276 evaluator() : Base() {}
277 explicit evaluator(
const XprType& mat) : Base(mat) {}
280template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
281struct evaluator<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
282 : evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > > {
284 SparseCompressedBase<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > >
286 typedef Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> XprType;
287 evaluator() : Base() {}
288 explicit evaluator(
const XprType& mat) : Base(mat) {}
Map(Index rows, Index cols, Index nnz, const StorageIndex *outerIndexPtr, const StorageIndex *innerIndexPtr, const Scalar *valuePtr, const StorageIndex *innerNonZerosPtr=0)
Definition SparseMap.h:260
Map(Index rows, Index cols, Index nnz, StorageIndex *outerIndexPtr, StorageIndex *innerIndexPtr, Scalar *valuePtr, StorageIndex *innerNonZerosPtr=0)
Definition SparseMap.h:237
~Map()
Definition SparseMap.h:265
A matrix or vector expression mapping an existing array of data.
Definition ForwardDeclarations.h:158
Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition Map.h:123
Common base class for sparse [compressed]-{row|column}-storage format.
Definition SparseCompressedBase.h:43
Scalar & coeffRef(Index row, Index col)
Definition SparseMap.h:175
Scalar * valuePtr()
Definition SparseMap.h:165
StorageIndex * innerNonZeroPtr()
Definition SparseMap.h:171
~SparseMapBase()
Definition SparseMap.h:198
StorageIndex * outerIndexPtr()
Definition SparseMap.h:169
StorageIndex * innerIndexPtr()
Definition SparseMap.h:167
A versatible sparse matrix representation.
Definition SparseUtil.h:47
@ ReadOnlyAccessors
Definition Constants.h:372
@ WriteAccessors
Definition Constants.h:374
const unsigned int LvalueBit
Definition Constants.h:148
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82