11#ifndef EIGEN_SPARSE_REF_H
12#define EIGEN_SPARSE_REF_H
15#include "./InternalHeaderCheck.h"
20 StandardCompressedFormat =
26template <
typename Derived>
29template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options_,
typename Str
ideType_>
30struct traits<Ref<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options_, StrideType_>>
31 :
public traits<SparseMatrix<MatScalar, MatOptions, MatIndex>> {
32 using PlainObjectType = SparseMatrix<MatScalar, MatOptions, MatIndex>;
33 enum { Options = Options_, Flags = traits<PlainObjectType>::Flags |
CompressedAccessBit | NestByRefBit };
35 template <
typename Derived>
38 StorageOrderMatch = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime ||
40 MatchAtCompileTime = (Derived::Flags & CompressedAccessBit) && StorageOrderMatch
42 using type = bool_constant<MatchAtCompileTime>;
46template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options_,
typename Str
ideType_>
47struct traits<Ref<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options_, StrideType_>>
48 :
public traits<Ref<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options_, StrideType_>> {
55template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options_,
typename Str
ideType_>
56struct traits<Ref<SparseVector<MatScalar, MatOptions, MatIndex>, Options_, StrideType_>>
57 :
public traits<SparseVector<MatScalar, MatOptions, MatIndex>> {
58 using PlainObjectType = SparseVector<MatScalar, MatOptions, MatIndex>;
59 enum { Options = Options_, Flags = traits<PlainObjectType>::Flags |
CompressedAccessBit | NestByRefBit };
61 template <
typename Derived>
63 enum { MatchAtCompileTime = (Derived::Flags &
CompressedAccessBit) && Derived::IsVectorAtCompileTime };
64 using type = bool_constant<MatchAtCompileTime>;
68template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options_,
typename Str
ideType_>
69struct traits<Ref<const SparseVector<MatScalar, MatOptions, MatIndex>, Options_, StrideType_>>
70 :
public traits<Ref<SparseVector<MatScalar, MatOptions, MatIndex>, Options_, StrideType_>> {
77template <
typename Derived>
78struct traits<SparseRefBase<Derived>> :
public traits<Derived> {};
80template <
typename Derived>
81class SparseRefBase :
public SparseMapBase<Derived> {
83 using Base = SparseMapBase<Derived>;
84 EIGEN_SPARSE_PUBLIC_INTERFACE(SparseRefBase)
87 : Base(RowsAtCompileTime == Dynamic ? 0 : RowsAtCompileTime, ColsAtCompileTime == Dynamic ? 0 : ColsAtCompileTime,
91 template <
typename Expression>
92 void construct(Expression& expr) {
93 EIGEN_IF_CONSTEXPR (Expression::IsVectorAtCompileTime) {
94 const Index offset = expr.outerIndexPtr() ? expr.outerIndexPtr()[0] : 0;
95 auto inner_index_ptr = expr.innerIndexPtr();
96 auto value_ptr = expr.valuePtr();
97 if (inner_index_ptr) inner_index_ptr += offset;
98 if (value_ptr) value_ptr += offset;
99 internal::construct_at<Base>(
this, expr.size(), expr.nonZeros(), inner_index_ptr, value_ptr);
100 }
else if (expr.outerIndexPtr() == 0) {
101 internal::construct_at<Base>(
this, expr.size(), expr.nonZeros(), expr.innerIndexPtr(), expr.valuePtr());
103 internal::construct_at<Base>(
this, expr.rows(), expr.cols(), expr.nonZeros(), expr.outerIndexPtr(),
104 expr.innerIndexPtr(), expr.valuePtr(), expr.innerNonZeroPtr());
122#ifndef EIGEN_PARSED_BY_DOXYGEN
123template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
124class Ref<
SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>
125 :
public internal::SparseRefBase<Ref<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>
127template <typename SparseMatrixType, int Options>
128class Ref<SparseMatrixType, Options>
129 :
public SparseMapBase<Derived, WriteAccessors>
133 using Traits = internal::traits<Ref>;
134 template <
int OtherOptions>
136 template <
int OtherOptions>
140 using Base = internal::SparseRefBase<Ref>;
141 EIGEN_SPARSE_PUBLIC_INTERFACE(Ref)
143#ifndef EIGEN_PARSED_BY_DOXYGEN
144 template <
int OtherOptions>
148 STORAGE_LAYOUT_DOES_NOT_MATCH);
149 eigen_assert(((Options &
int(StandardCompressedFormat)) == 0) || (expr.
isCompressed()));
150 Base::construct(expr.
derived());
153 template <
int OtherOptions>
157 STORAGE_LAYOUT_DOES_NOT_MATCH);
158 eigen_assert(((Options &
int(StandardCompressedFormat)) == 0) || (expr.
isCompressed()));
159 Base::construct(expr.
derived());
162 template <
typename Derived>
166 template <
typename Derived>
170 EIGEN_STATIC_ASSERT(
bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
171 EIGEN_STATIC_ASSERT(
bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
172 eigen_assert(((Options &
int(StandardCompressedFormat)) == 0) || (expr.
isCompressed()));
173 Base::construct(expr.const_cast_derived());
178template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
179class Ref<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>
180 :
public internal::SparseRefBase<Ref<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>> {
181 using TPlainObjectType = SparseMatrix<MatScalar, MatOptions, MatIndex>;
182 using Traits = internal::traits<Ref>;
185 using Base = internal::SparseRefBase<Ref>;
186 EIGEN_SPARSE_PUBLIC_INTERFACE(Ref)
188 template <
typename Derived>
189 inline Ref(
const SparseMatrixBase<Derived>& expr) : m_hasCopy(false) {
190 construct(expr.derived(),
typename Traits::template match<Derived>::type());
193 inline Ref(
const Ref& other) : Base(other), m_hasCopy(false) {
197 template <
typename OtherRef>
198 inline Ref(
const RefBase<OtherRef>& other) : m_hasCopy(false) {
199 construct(other.derived(),
typename Traits::template match<OtherRef>::type());
204 internal::destroy_at(
reinterpret_cast<TPlainObjectType*
>(&m_storage));
209 template <
typename Expression>
210 void construct(
const Expression& expr, std::true_type) {
211 EIGEN_IF_CONSTEXPR (Options &
int(StandardCompressedFormat)) {
212 if (!expr.isCompressed()) {
213 TPlainObjectType* obj = internal::construct_at(
reinterpret_cast<TPlainObjectType*
>(&m_storage), expr);
215 Base::construct(*obj);
219 Base::construct(expr);
222 template <
typename Expression>
223 void construct(
const Expression& expr, std::false_type) {
224 TPlainObjectType* obj = internal::construct_at(
reinterpret_cast<TPlainObjectType*
>(&m_storage), expr);
226 Base::construct(*obj);
229 typename internal::aligned_storage<
sizeof(TPlainObjectType), EIGEN_ALIGNOF(TPlainObjectType)>::type m_storage;
243#ifndef EIGEN_PARSED_BY_DOXYGEN
244template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
245class Ref<
SparseVector<MatScalar, MatOptions, MatIndex>, Options, StrideType>
246 :
public internal::SparseRefBase<Ref<SparseVector<MatScalar, MatOptions, MatIndex>, Options, StrideType>>
248template <typename SparseVectorType>
249class Ref<SparseVectorType> :
public SparseMapBase<Derived, WriteAccessors>
253 using Traits = internal::traits<Ref>;
254 template <
int OtherOptions>
258 using Base = internal::SparseRefBase<Ref>;
259 EIGEN_SPARSE_PUBLIC_INTERFACE(Ref)
261#ifndef EIGEN_PARSED_BY_DOXYGEN
262 template <
int OtherOptions>
266 STORAGE_LAYOUT_DOES_NOT_MATCH);
267 Base::construct(expr.
derived());
270 template <
typename Derived>
274 template <
typename Derived>
278 EIGEN_STATIC_ASSERT(
bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
279 EIGEN_STATIC_ASSERT(
bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
280 EIGEN_STATIC_ASSERT((!std::is_same<Derived, PlainObjectType>::value),
281 THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
282 Base::construct(expr.const_cast_derived());
287template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
288class Ref<const SparseVector<MatScalar, MatOptions, MatIndex>, Options, StrideType>
289 :
public internal::SparseRefBase<Ref<const SparseVector<MatScalar, MatOptions, MatIndex>, Options, StrideType>> {
290 using TPlainObjectType = SparseVector<MatScalar, MatOptions, MatIndex>;
291 using Traits = internal::traits<Ref>;
294 using Base = internal::SparseRefBase<Ref>;
295 EIGEN_SPARSE_PUBLIC_INTERFACE(Ref)
297 template <
typename Derived>
298 inline Ref(
const SparseMatrixBase<Derived>& expr) : m_hasCopy(false) {
299 construct(expr.derived(),
typename Traits::template match<Derived>::type());
302 inline Ref(
const Ref& other) : Base(other), m_hasCopy(false) {
306 template <
typename OtherRef>
307 inline Ref(
const RefBase<OtherRef>& other) : m_hasCopy(false) {
308 construct(other.derived(),
typename Traits::template match<OtherRef>::type());
313 internal::destroy_at(
reinterpret_cast<TPlainObjectType*
>(&m_storage));
318 template <
typename Expression>
319 void construct(
const Expression& expr, std::true_type) {
320 Base::construct(expr);
323 template <
typename Expression>
324 void construct(
const Expression& expr, std::false_type) {
325 TPlainObjectType* obj = internal::construct_at(
reinterpret_cast<TPlainObjectType*
>(&m_storage), expr);
327 Base::construct(*obj);
330 typename internal::aligned_storage<
sizeof(TPlainObjectType), EIGEN_ALIGNOF(TPlainObjectType)>::type m_storage;
339template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
340struct evaluator<Ref<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>
341 : evaluator<SparseCompressedBase<Ref<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>> {
342 using Base = evaluator<SparseCompressedBase<Ref<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>>;
343 using XprType = Ref<SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>;
344 evaluator() =
default;
345 explicit evaluator(
const XprType& mat) : Base(mat) {}
348template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
349struct evaluator<Ref<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>
350 : evaluator<SparseCompressedBase<Ref<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>> {
352 evaluator<SparseCompressedBase<Ref<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>>>;
353 using XprType = Ref<const SparseMatrix<MatScalar, MatOptions, MatIndex>, Options, StrideType>;
354 evaluator() =
default;
355 explicit evaluator(
const XprType& mat) : Base(mat) {}
358template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
359struct evaluator<Ref<SparseVector<MatScalar, MatOptions, MatIndex>, Options, StrideType>>
360 : evaluator<SparseCompressedBase<Ref<SparseVector<MatScalar, MatOptions, MatIndex>, Options, StrideType>>> {
361 using Base = evaluator<SparseCompressedBase<Ref<SparseVector<MatScalar, MatOptions, MatIndex>, Options, StrideType>>>;
362 using XprType = Ref<SparseVector<MatScalar, MatOptions, MatIndex>, Options, StrideType>;
363 evaluator() =
default;
364 explicit evaluator(
const XprType& mat) : Base(mat) {}
367template <
typename MatScalar,
int MatOptions,
typename MatIndex,
int Options,
typename Str
ideType>
368struct evaluator<Ref<const SparseVector<MatScalar, MatOptions, MatIndex>, Options, StrideType>>
369 : evaluator<SparseCompressedBase<Ref<const SparseVector<MatScalar, MatOptions, MatIndex>, Options, StrideType>>> {
371 evaluator<SparseCompressedBase<Ref<const SparseVector<MatScalar, MatOptions, MatIndex>, Options, StrideType>>>;
372 using XprType = Ref<const SparseVector<MatScalar, MatOptions, MatIndex>, Options, StrideType>;
373 evaluator() =
default;
374 explicit evaluator(
const XprType& mat) : Base(mat) {}
A matrix or vector expression mapping an existing array of data.
Definition Map.h:97
Ref(SparseCompressedBase< Derived > &expr)
Definition SparseRef.h:167
Ref(SparseCompressedBase< Derived > &expr)
Definition SparseRef.h:275
A matrix or vector expression mapping an existing expression.
Definition Ref.h:262
Common base class for sparse [compressed]-{row|column}-storage format.
Definition SparseCompressedBase.h:44
bool isCompressed() const
Definition SparseCompressedBase.h:115
A versatile sparse matrix representation.
Definition SparseMatrix.h:122
bool isCompressed() const
Definition SparseCompressedBase.h:115
a sparse vector class
Definition SparseVector.h:63
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71
constexpr unsigned int CompressedAccessBit
Definition Constants.h:196
constexpr Derived & derived()
Definition EigenBase.h:50