SparseRef.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2015 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_SPARSE_REF_H
11 #define EIGEN_SPARSE_REF_H
12 
13 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 enum {
19 };
20 
21 namespace internal {
22 
23 template<typename Derived> class SparseRefBase;
24 
25 template<typename MatScalar, int MatOptions, typename MatIndex, int Options_, typename StrideType_>
26 struct traits<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options_, StrideType_> >
27  : public traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >
28 {
29  typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
30  enum {
31  Options = Options_,
32  Flags = traits<PlainObjectType>::Flags | CompressedAccessBit | NestByRefBit
33  };
34 
35  template<typename Derived> struct match {
36  enum {
37  StorageOrderMatch = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime || ((PlainObjectType::Flags&RowMajorBit)==(Derived::Flags&RowMajorBit)),
38  MatchAtCompileTime = (Derived::Flags&CompressedAccessBit) && StorageOrderMatch
39  };
40  typedef std::conditional_t<MatchAtCompileTime,internal::true_type,internal::false_type> type;
41  };
42 
43 };
44 
45 template<typename MatScalar, int MatOptions, typename MatIndex, int Options_, typename StrideType_>
46 struct traits<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options_, StrideType_> >
47  : public traits<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options_, StrideType_> >
48 {
49  enum {
50  Flags = (traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >::Flags | CompressedAccessBit | NestByRefBit) & ~LvalueBit
51  };
52 };
53 
54 template<typename MatScalar, int MatOptions, typename MatIndex, int Options_, typename StrideType_>
55 struct traits<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options_, StrideType_> >
56  : public traits<SparseVector<MatScalar,MatOptions,MatIndex> >
57 {
58  typedef SparseVector<MatScalar,MatOptions,MatIndex> PlainObjectType;
59  enum {
60  Options = Options_,
61  Flags = traits<PlainObjectType>::Flags | CompressedAccessBit | NestByRefBit
62  };
63 
64  template<typename Derived> struct match {
65  enum {
66  MatchAtCompileTime = (Derived::Flags&CompressedAccessBit) && Derived::IsVectorAtCompileTime
67  };
68  typedef std::conditional_t<MatchAtCompileTime,internal::true_type,internal::false_type> type;
69  };
70 
71 };
72 
73 template<typename MatScalar, int MatOptions, typename MatIndex, int Options_, typename StrideType_>
74 struct traits<Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options_, StrideType_> >
75  : public traits<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options_, StrideType_> >
76 {
77  enum {
78  Flags = (traits<SparseVector<MatScalar,MatOptions,MatIndex> >::Flags | CompressedAccessBit | NestByRefBit) & ~LvalueBit
79  };
80 };
81 
82 template<typename Derived>
83 struct traits<SparseRefBase<Derived> > : public traits<Derived> {};
84 
85 template<typename Derived> class SparseRefBase
86  : public SparseMapBase<Derived>
87 {
88 public:
89 
90  typedef SparseMapBase<Derived> Base;
91  EIGEN_SPARSE_PUBLIC_INTERFACE(SparseRefBase)
92 
93  SparseRefBase()
94  : Base(RowsAtCompileTime==Dynamic?0:RowsAtCompileTime,ColsAtCompileTime==Dynamic?0:ColsAtCompileTime, 0, 0, 0, 0, 0)
95  {}
96 
97 protected:
98 
99  template<typename Expression>
100  void construct(Expression& expr)
101  {
102  if(expr.outerIndexPtr()==0)
103  internal::construct_at<Base>(this, expr.size(), expr.nonZeros(), expr.innerIndexPtr(), expr.valuePtr());
104  else
105  internal::construct_at<Base>(this, expr.rows(), expr.cols(), expr.nonZeros(), expr.outerIndexPtr(), expr.innerIndexPtr(), expr.valuePtr(), expr.innerNonZeroPtr());
106  }
107 };
108 
109 } // namespace internal
110 
111 
123 #ifndef EIGEN_PARSED_BY_DOXYGEN
124 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
125 class Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType >
126  : public internal::SparseRefBase<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType > >
127 #else
128 template<typename SparseMatrixType, int Options>
129 class Ref<SparseMatrixType, Options>
130  : public SparseMapBase<Derived,WriteAccessors> // yes, that's weird to use Derived here, but that works!
131 #endif
132 {
134  typedef internal::traits<Ref> Traits;
135  template<int OtherOptions>
137  template<int OtherOptions>
139  public:
140 
141  typedef internal::SparseRefBase<Ref> Base;
143 
144 
145  #ifndef EIGEN_PARSED_BY_DOXYGEN
146  template<int OtherOptions>
148  {
149  EIGEN_STATIC_ASSERT(bool(Traits::template match<SparseMatrix<MatScalar,OtherOptions,MatIndex> >::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
150  eigen_assert( ((Options & int(StandardCompressedFormat))==0) || (expr.isCompressed()) );
151  Base::construct(expr.derived());
152  }
153 
154  template<int OtherOptions>
156  {
157  EIGEN_STATIC_ASSERT(bool(Traits::template match<SparseMatrix<MatScalar,OtherOptions,MatIndex> >::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
158  eigen_assert( ((Options & int(StandardCompressedFormat))==0) || (expr.isCompressed()) );
159  Base::construct(expr.derived());
160  }
161 
162  template<typename Derived>
163  inline Ref(const SparseCompressedBase<Derived>& expr)
164  #else
166  template<typename Derived>
168  #endif
169  {
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()) );
174  }
175 };
176 
177 // this is the const ref version
178 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
179 class Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType>
180  : public internal::SparseRefBase<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
181 {
183  typedef internal::traits<Ref> Traits;
184  public:
185 
186  typedef internal::SparseRefBase<Ref> Base;
188 
189  template<typename Derived>
190  inline Ref(const SparseMatrixBase<Derived>& expr) : m_hasCopy(false)
191  {
192  construct(expr.derived(), typename Traits::template match<Derived>::type());
193  }
194 
195  inline Ref(const Ref& other) : Base(other), m_hasCopy(false) {
196  // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
197  }
198 
199  template<typename OtherRef>
200  inline Ref(const RefBase<OtherRef>& other) : m_hasCopy(false) {
201  construct(other.derived(), typename Traits::template match<OtherRef>::type());
202  }
203 
204  ~Ref() {
205  if(m_hasCopy) {
206  internal::destroy_at(reinterpret_cast<TPlainObjectType*>(&m_storage));
207  }
208  }
209 
210  protected:
211 
212  template<typename Expression>
213  void construct(const Expression& expr,internal::true_type)
214  {
215  if((Options & int(StandardCompressedFormat)) && (!expr.isCompressed()))
216  {
217  TPlainObjectType* obj = internal::construct_at(reinterpret_cast<TPlainObjectType*>(&m_storage), expr);
218  m_hasCopy = true;
219  Base::construct(*obj);
220  }
221  else
222  {
223  Base::construct(expr);
224  }
225  }
226 
227  template<typename Expression>
228  void construct(const Expression& expr, internal::false_type)
229  {
230  TPlainObjectType* obj = internal::construct_at(reinterpret_cast<TPlainObjectType*>(&m_storage), expr);
231  m_hasCopy = true;
232  Base::construct(*obj);
233  }
234 
235  protected:
236  typename internal::aligned_storage<sizeof(TPlainObjectType), EIGEN_ALIGNOF(TPlainObjectType)>::type m_storage;
237  bool m_hasCopy;
238 };
239 
240 
241 
251 #ifndef EIGEN_PARSED_BY_DOXYGEN
252 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
253 class Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType >
254  : public internal::SparseRefBase<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType > >
255 #else
256 template<typename SparseVectorType>
257 class Ref<SparseVectorType>
258  : public SparseMapBase<Derived,WriteAccessors>
259 #endif
260 {
262  typedef internal::traits<Ref> Traits;
263  template<int OtherOptions>
265  public:
266 
267  typedef internal::SparseRefBase<Ref> Base;
269 
270  #ifndef EIGEN_PARSED_BY_DOXYGEN
271  template<int OtherOptions>
273  {
274  EIGEN_STATIC_ASSERT(bool(Traits::template match<SparseVector<MatScalar,OtherOptions,MatIndex> >::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
275  Base::construct(expr.derived());
276  }
277 
278  template<typename Derived>
279  inline Ref(const SparseCompressedBase<Derived>& expr)
280  #else
282  template<typename Derived>
284  #endif
285  {
286  EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
287  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
289  }
290 };
291 
292 // this is the const ref version
293 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
294 class Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType>
295  : public internal::SparseRefBase<Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
296 {
298  typedef internal::traits<Ref> Traits;
299  public:
300 
301  typedef internal::SparseRefBase<Ref> Base;
303 
304  template<typename Derived>
305  inline Ref(const SparseMatrixBase<Derived>& expr) : m_hasCopy(false)
306  {
307  construct(expr.derived(), typename Traits::template match<Derived>::type());
308  }
309 
310  inline Ref(const Ref& other) : Base(other), m_hasCopy(false) {
311  // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
312  }
313 
314  template<typename OtherRef>
315  inline Ref(const RefBase<OtherRef>& other) : m_hasCopy(false) {
316  construct(other.derived(), typename Traits::template match<OtherRef>::type());
317  }
318 
319  ~Ref() {
320  if(m_hasCopy) {
321  internal::destroy_at(reinterpret_cast<TPlainObjectType*>(&m_storage));
322  }
323  }
324 
325  protected:
326 
327  template<typename Expression>
328  void construct(const Expression& expr,internal::true_type)
329  {
330  Base::construct(expr);
331  }
332 
333  template<typename Expression>
334  void construct(const Expression& expr, internal::false_type)
335  {
336  TPlainObjectType* obj = internal::construct_at(reinterpret_cast<TPlainObjectType*>(&m_storage), expr);
337  m_hasCopy = true;
338  Base::construct(*obj);
339  }
340 
341  protected:
342  typename internal::aligned_storage<sizeof(TPlainObjectType), EIGEN_ALIGNOF(TPlainObjectType)>::type m_storage;
343  bool m_hasCopy;
344 };
345 
346 namespace internal {
347 
348 // FIXME shall we introduce a general evaluatior_ref that we can specialize for any sparse object once, and thus remove this copy-pasta thing...
349 
350 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
351 struct evaluator<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
352  : evaluator<SparseCompressedBase<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
353 {
354  typedef evaluator<SparseCompressedBase<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
355  typedef Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
356  evaluator() : Base() {}
357  explicit evaluator(const XprType &mat) : Base(mat) {}
358 };
359 
360 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
361 struct evaluator<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
362  : evaluator<SparseCompressedBase<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
363 {
364  typedef evaluator<SparseCompressedBase<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
365  typedef Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
366  evaluator() : Base() {}
367  explicit evaluator(const XprType &mat) : Base(mat) {}
368 };
369 
370 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
371 struct evaluator<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
372  : evaluator<SparseCompressedBase<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
373 {
374  typedef evaluator<SparseCompressedBase<Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
375  typedef Ref<SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
376  evaluator() : Base() {}
377  explicit evaluator(const XprType &mat) : Base(mat) {}
378 };
379 
380 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
381 struct evaluator<Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
382  : evaluator<SparseCompressedBase<Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
383 {
384  typedef evaluator<SparseCompressedBase<Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
385  typedef Ref<const SparseVector<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
386  evaluator() : Base() {}
387  explicit evaluator(const XprType &mat) : Base(mat) {}
388 };
389 
390 }
391 
392 } // end namespace Eigen
393 
394 #endif // EIGEN_SPARSE_REF_H
#define EIGEN_ALIGNOF(x)
#define eigen_assert(x)
Definition: Macros.h:902
#define EIGEN_SPARSE_PUBLIC_INTERFACE(Derived)
Definition: SparseUtil.h:45
#define EIGEN_STATIC_ASSERT(X, MSG)
Definition: StaticAssert.h:26
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:98
bool construct(Expression &expr)
Definition: Ref.h:111
Ref(const Map< SparseMatrix< MatScalar, OtherOptions, MatIndex >> &expr)
Ref(SparseCompressedBase< Derived > &expr)
Definition: SparseRef.h:167
Ref(const SparseMatrix< MatScalar, OtherOptions, MatIndex > &expr)
internal::SparseRefBase< Ref > Base
Definition: SparseRef.h:141
internal::traits< Ref > Traits
Definition: SparseRef.h:134
SparseMatrix< MatScalar, MatOptions, MatIndex > PlainObjectType
Definition: SparseRef.h:133
Ref(SparseCompressedBase< Derived > &expr)
Definition: SparseRef.h:283
SparseVector< MatScalar, MatOptions, MatIndex > PlainObjectType
Definition: SparseRef.h:261
internal::SparseRefBase< Ref > Base
Definition: SparseRef.h:267
Ref(const SparseVector< MatScalar, OtherOptions, MatIndex > &expr)
internal::traits< Ref > Traits
Definition: SparseRef.h:262
internal::aligned_storage< sizeof(TPlainObjectType), EIGEN_ALIGNOF(TPlainObjectType)>::type m_storage
Definition: SparseRef.h:236
internal::aligned_storage< sizeof(TPlainObjectType), EIGEN_ALIGNOF(TPlainObjectType)>::type m_storage
Definition: SparseRef.h:342
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:285
Ref(const PlainObjectBase< Derived > &expr, std::enable_if_t< bool(Traits::template match< Derived >::MatchAtCompileTime), Derived > *=0)
Common base class for sparse [compressed]-{row|column}-storage format.
Base class of any sparse matrices or sparse expressions.
A versatible sparse matrix representation.
Definition: SparseMatrix.h:125
a sparse vector class
Definition: SparseVector.h:68
const unsigned int LvalueBit
Definition: Constants.h:146
const unsigned int RowMajorBit
Definition: Constants.h:68
const unsigned int CompressedAccessBit
Definition: Constants.h:193
void destroy_at(T *p)
Definition: Memory.h:1264
T * construct_at(T *p, Args &&... args)
Definition: Memory.h:1248
: InteropHeaders
Definition: Core:139
const unsigned int NestByRefBit
Definition: Constants.h:171
@ StandardCompressedFormat
Definition: SparseRef.h:18
const int Dynamic
Definition: Constants.h:24
Derived & derived()
Definition: EigenBase.h:48
Derived & const_cast_derived() const
Definition: EigenBase.h:54