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(
const SparseMapBase& other)
138 m_outerSize(other.m_outerSize),
139 m_innerSize(other.m_innerSize),
140 m_zero_nnz(other.m_zero_nnz),
141 m_outerIndex(other.m_outerIndex == other.m_zero_nnz.data() ? m_zero_nnz.data() : other.m_outerIndex),
142 m_innerIndices(other.m_innerIndices),
143 m_values(other.m_values),
144 m_innerNonZeros(other.m_innerNonZeros) {}
147 inline SparseMapBase& operator=(
const SparseMapBase& other) {
148 m_outerSize = other.m_outerSize;
149 m_innerSize = other.m_innerSize;
150 m_zero_nnz = other.m_zero_nnz;
151 m_outerIndex = other.m_outerIndex == other.m_zero_nnz.data() ? m_zero_nnz.data() : other.m_outerIndex;
152 m_innerIndices = other.m_innerIndices;
153 m_values = other.m_values;
154 m_innerNonZeros = other.m_innerNonZeros;
159 inline SparseMapBase() =
default;
166template <
typename Derived>
167class SparseMapBase<Derived,
WriteAccessors> :
public SparseMapBase<Derived, ReadOnlyAccessors> {
169 using Base = SparseMapBase<Derived, ReadOnlyAccessors>;
170 using Scalar =
typename Base::Scalar;
171 using StorageIndex =
typename Base::StorageIndex;
172 enum { IsRowMajor = Base::IsRowMajor };
174 using Base::operator=;
175 EIGEN_DEFAULT_COPY_CONSTRUCTOR(SparseMapBase)
176 SparseMapBase& operator=(
const SparseMapBase&) =
default;
181 using Base::innerIndexPtr;
182 using Base::innerNonZeroPtr;
183 using Base::outerIndexPtr;
184 using Base::valuePtr;
186 inline Scalar*
valuePtr() {
return Base::m_values; }
203 const Index outer = IsRowMajor ? row : col;
204 const Index inner = IsRowMajor ? col : row;
206 Index start = Base::m_outerIndex[outer];
207 Index end = Base::isCompressed() ? Base::m_outerIndex[outer + 1] : start + Base::m_innerNonZeros[outer];
208 eigen_assert(end >= start &&
"you probably called coeffRef on a non finalized matrix");
209 eigen_assert(end > start &&
"coeffRef cannot be called on a zero coefficient");
210 StorageIndex* r = std::lower_bound(&Base::m_innerIndices[start], &Base::m_innerIndices[end], inner);
211 const Index
id = r - &Base::m_innerIndices[0];
212 eigen_assert((*r == inner) && (
id < end) &&
"coeffRef cannot be called on a zero coefficient");
213 return const_cast<Scalar*
>(Base::m_values)[
id];
216 inline SparseMapBase(Index rows, Index cols, Index nnz, StorageIndex* outerIndexPtr, StorageIndex* innerIndexPtr,
217 Scalar* valuePtr, StorageIndex* innerNonZerosPtr = 0)
218 : Base(rows, cols, nnz, outerIndexPtr, innerIndexPtr, valuePtr, innerNonZerosPtr) {}
221 inline SparseMapBase(Index size, Index nnz, StorageIndex* innerIndexPtr, Scalar* valuePtr)
222 : Base(size, nnz, innerIndexPtr, valuePtr) {}
225 inline SparseMapBase() =
default;
237#ifndef EIGEN_PARSED_BY_DOXYGEN
238template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
239class Map<
SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>
240 :
public SparseMapBase<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
242template <typename SparseMatrixType>
243class
Map<SparseMatrixType> :
public SparseMapBase<Derived, WriteAccessors>
247 using Base = SparseMapBase<Map>;
248 EIGEN_SPARSE_PUBLIC_INTERFACE(
Map)
249 enum { IsRowMajor = Base::IsRowMajor };
262 Scalar*
valuePtr, StorageIndex* innerNonZerosPtr = 0)
264#ifndef EIGEN_PARSED_BY_DOXYGEN
267template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
268class Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>
269 :
public SparseMapBase<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > {
271 using Base = SparseMapBase<Map>;
272 EIGEN_SPARSE_PUBLIC_INTERFACE(
Map)
273 enum { IsRowMajor = Base::IsRowMajor };
283 const Scalar*
valuePtr,
const StorageIndex* innerNonZerosPtr = 0)
289template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
290struct evaluator<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
291 : evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > > {
292 using Base = evaluator<SparseCompressedBase<Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>>;
293 using XprType = Map<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>;
294 evaluator() =
default;
295 explicit evaluator(
const XprType& mat) : Base(mat) {}
298template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
299struct evaluator<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> >
300 : evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType> > > {
302 evaluator<SparseCompressedBase<Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>>;
303 using XprType = Map<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>;
304 evaluator() =
default;
305 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:282
Map(Index rows, Index cols, Index nnz, StorageIndex *outerIndexPtr, StorageIndex *innerIndexPtr, Scalar *valuePtr, StorageIndex *innerNonZerosPtr=0)
Definition SparseMap.h:261
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:202
Scalar * valuePtr()
Definition SparseMap.h:186
StorageIndex * innerNonZeroPtr()
Definition SparseMap.h:192
StorageIndex * outerIndexPtr()
Definition SparseMap.h:190
StorageIndex * innerIndexPtr()
Definition SparseMap.h:188
A versatile sparse matrix representation.
Definition SparseMatrix.h:121
@ ReadOnlyAccessors
Definition Constants.h:377
@ WriteAccessors
Definition Constants.h:379
constexpr unsigned int LvalueBit
Definition Constants.h:149