11#ifndef EIGEN_SPARSE_MAP_H
12#define EIGEN_SPARSE_MAP_H
15#include "./InternalHeaderCheck.h"
21template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
22struct traits<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
23 :
public traits<SparseMatrix<MatScalar, MatOptions, MatIndex> > {
24 using PlainObjectType = SparseMatrix<MatScalar, MatOptions, MatIndex>;
25 using TraitsBase = traits<PlainObjectType>;
26 enum { Flags = TraitsBase::Flags & (~NestByRefBit) };
29template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
30struct traits<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
31 :
public traits<SparseMatrix<MatScalar, MatOptions, MatIndex> > {
32 using PlainObjectType = SparseMatrix<MatScalar, MatOptions, MatIndex>;
33 using TraitsBase = traits<PlainObjectType>;
34 enum { Flags = TraitsBase::Flags & (~(NestByRefBit |
LvalueBit)) };
39template <
typename Derived,
47template <
typename Derived>
50 using Base = SparseCompressedBase<Derived>;
51 using Scalar =
typename Base::Scalar;
52 using StorageIndex =
typename Base::StorageIndex;
53 enum { IsRowMajor = Base::IsRowMajor };
54 using Base::operator=;
57 using ScalarPointer = std::conditional_t<bool(internal::is_lvalue<Derived>::value), Scalar*,
const Scalar*>;
59 std::conditional_t<bool(internal::is_lvalue<Derived>::value), StorageIndex*,
const StorageIndex*>;
63 Array<StorageIndex, 2, 1> m_zero_nnz;
64 IndexPointer m_outerIndex;
65 IndexPointer m_innerIndices;
66 ScalarPointer m_values;
67 IndexPointer m_innerNonZeros;
71 inline Index rows()
const {
return IsRowMajor ? m_outerSize : m_innerSize; }
73 inline Index cols()
const {
return IsRowMajor ? m_innerSize : m_outerSize; }
75 inline Index innerSize()
const {
return m_innerSize; }
77 inline Index outerSize()
const {
return m_outerSize; }
79 inline Index nonZeros()
const {
return m_zero_nnz[1]; }
82 bool isCompressed()
const {
return m_innerNonZeros == 0; }
87 inline const Scalar* valuePtr()
const {
return m_values; }
89 inline const StorageIndex* innerIndexPtr()
const {
return m_innerIndices; }
91 inline const StorageIndex* outerIndexPtr()
const {
return m_outerIndex; }
93 inline const StorageIndex* innerNonZeroPtr()
const {
return m_innerNonZeros; }
97 inline Scalar coeff(Index row, Index col)
const {
98 const Index outer = IsRowMajor ? row : col;
99 const Index inner = IsRowMajor ? col : row;
101 Index start = m_outerIndex[outer];
102 Index end = isCompressed() ? m_outerIndex[outer + 1] : start + m_innerNonZeros[outer];
105 else if (end > 0 && inner == m_innerIndices[end - 1])
106 return m_values[end - 1];
110 const StorageIndex* r = std::lower_bound(&m_innerIndices[start], &m_innerIndices[end - 1], inner);
111 const Index
id = r - &m_innerIndices[0];
112 return ((*r == inner) && (
id < end)) ? m_values[id] : Scalar(0);
115 inline SparseMapBase(Index rows, Index cols, Index nnz, IndexPointer outerIndexPtr, IndexPointer innerIndexPtr,
116 ScalarPointer valuePtr, IndexPointer innerNonZerosPtr = 0)
117 : m_outerSize(IsRowMajor ? rows : cols),
118 m_innerSize(IsRowMajor ? cols : rows),
119 m_zero_nnz(0, internal::convert_index<StorageIndex>(nnz)),
120 m_outerIndex(outerIndexPtr),
121 m_innerIndices(innerIndexPtr),
123 m_innerNonZeros(innerNonZerosPtr) {}
126 inline SparseMapBase(Index size, Index nnz, IndexPointer innerIndexPtr, ScalarPointer valuePtr)
129 m_zero_nnz(0, internal::convert_index<StorageIndex>(nnz)),
130 m_outerIndex(m_zero_nnz.data()),
131 m_innerIndices(innerIndexPtr),
133 m_innerNonZeros(0) {}
136 inline SparseMapBase() =
default;
143template <
typename Derived>
144class SparseMapBase<Derived,
WriteAccessors> :
public SparseMapBase<Derived, ReadOnlyAccessors> {
146 using Base = SparseMapBase<Derived, ReadOnlyAccessors>;
147 using Scalar =
typename Base::Scalar;
148 using StorageIndex =
typename Base::StorageIndex;
149 enum { IsRowMajor = Base::IsRowMajor };
151 using Base::operator=;
156 using Base::innerIndexPtr;
157 using Base::innerNonZeroPtr;
158 using Base::outerIndexPtr;
159 using Base::valuePtr;
161 inline Scalar*
valuePtr() {
return Base::m_values; }
178 const Index outer = IsRowMajor ? row : col;
179 const Index inner = IsRowMajor ? col : row;
181 Index start = Base::m_outerIndex[outer];
182 Index end = Base::isCompressed() ? Base::m_outerIndex[outer + 1] : start + Base::m_innerNonZeros[outer];
183 eigen_assert(end >= start &&
"you probably called coeffRef on a non finalized matrix");
184 eigen_assert(end > start &&
"coeffRef cannot be called on a zero coefficient");
185 StorageIndex* r = std::lower_bound(&Base::m_innerIndices[start], &Base::m_innerIndices[end], inner);
186 const Index
id = r - &Base::m_innerIndices[0];
187 eigen_assert((*r == inner) && (
id < end) &&
"coeffRef cannot be called on a zero coefficient");
188 return const_cast<Scalar*
>(Base::m_values)[
id];
191 inline SparseMapBase(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr, StorageIndex* innerIndexPtr,
192 Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
193 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr) {}
196 inline SparseMapBase(Index size, Index nnz, StorageIndex* innerIndexPtr, Scalar* valuePtr)
197 : Base(size, nnz, innerIndexPtr, valuePtr) {}
200 inline SparseMapBase() =
default;
212#ifndef EIGEN_PARSED_BY_DOXYGEN
213template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
214class Map<
SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>
215 :
public SparseMapBase<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
217template <typename SparseMatrixType>
218class
Map<SparseMatrixType> :
public SparseMapBase<Derived, WriteAccessors>
222 using Base = SparseMapBase<Map>;
223 EIGEN_SPARSE_PUBLIC_INTERFACE(
Map)
224 enum { IsRowMajor = Base::IsRowMajor };
237 Scalar*
valuePtr, StorageIndex* innerNonZerosPtr = 0)
239#ifndef EIGEN_PARSED_BY_DOXYGEN
242template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
243class Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>
244 :
public SparseMapBase<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > {
246 using Base = SparseMapBase<Map>;
247 EIGEN_SPARSE_PUBLIC_INTERFACE(
Map)
248 enum { IsRowMajor = Base::IsRowMajor };
258 const Scalar*
valuePtr,
const StorageIndex* innerNonZerosPtr = 0)
264template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
265struct evaluator<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
266 : evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > > {
267 using Base = evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>>;
268 using XprType = Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>;
269 evaluator() =
default;
270 explicit evaluator(
const XprType& mat) : Base(mat) {}
273template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
274struct evaluator<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
275 : evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > > {
277 evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>>;
278 using XprType = Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>;
279 evaluator() =
default;
280 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:257
Map(Index rows, Index cols, Index nnz, StorageIndex *outerIndexPtr, StorageIndex *innerIndexPtr, Scalar *valuePtr, StorageIndex *innerNonZerosPtr=0)
Definition SparseMap.h:236
A matrix or vector expression mapping an existing array of data.
Definition Map.h:97
constexpr Map(PointerArgType dataPtr, const StrideType &stride=StrideType())
Definition Map.h:124
Common base class for sparse [compressed]-{row|column}-storage format.
Definition SparseCompressedBase.h:44
Scalar & coeffRef(Index row, Index col)
Definition SparseMap.h:177
Scalar * valuePtr()
Definition SparseMap.h:161
StorageIndex * innerNonZeroPtr()
Definition SparseMap.h:167
StorageIndex * outerIndexPtr()
Definition SparseMap.h:165
StorageIndex * innerIndexPtr()
Definition SparseMap.h:163
A versatile sparse matrix representation.
Definition SparseMatrix.h:122
@ ReadOnlyAccessors
Definition Constants.h:377
@ WriteAccessors
Definition Constants.h:379
constexpr unsigned int LvalueBit
Definition Constants.h:149