Eigen  3.2.91
SparseRef.h
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 namespace Eigen {
14 
15 enum {
16  StandardCompressedFormat = 2
17 };
18 
19 namespace internal {
20 
21 template<typename Derived> class SparseRefBase;
22 
23 template<typename MatScalar, int MatOptions, typename MatIndex, int _Options, typename _StrideType>
24 struct traits<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, _Options, _StrideType> >
25  : public traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >
26 {
27  typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
28  enum {
29  Options = _Options,
30  Flags = traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >::Flags | CompressedAccessBit | NestByRefBit
31  };
32 
33  template<typename Derived> struct match {
34  enum {
35  StorageOrderMatch = PlainObjectType::IsVectorAtCompileTime || Derived::IsVectorAtCompileTime || ((PlainObjectType::Flags&RowMajorBit)==(Derived::Flags&RowMajorBit)),
36  MatchAtCompileTime = (Derived::Flags&CompressedAccessBit) && StorageOrderMatch
37  };
38  typedef typename internal::conditional<MatchAtCompileTime,internal::true_type,internal::false_type>::type type;
39  };
40 
41 };
42 
43 template<typename MatScalar, int MatOptions, typename MatIndex, int _Options, typename _StrideType>
44 struct traits<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, _Options, _StrideType> >
45  : public traits<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, _Options, _StrideType> >
46 {
47  enum {
48  Flags = (traits<SparseMatrix<MatScalar,MatOptions,MatIndex> >::Flags | CompressedAccessBit | NestByRefBit) & ~LvalueBit
49  };
50 };
51 
52 template<typename Derived>
53 struct traits<SparseRefBase<Derived> > : public traits<Derived> {};
54 
55 template<typename Derived> class SparseRefBase
56  : public SparseMapBase<Derived>
57 {
58 public:
59 
60  typedef SparseMapBase<Derived> Base;
61  _EIGEN_SPARSE_PUBLIC_INTERFACE(SparseRefBase)
62 
63  SparseRefBase()
64  : Base(RowsAtCompileTime==Dynamic?0:RowsAtCompileTime,ColsAtCompileTime==Dynamic?0:ColsAtCompileTime, 0, 0, 0, 0, 0)
65  {}
66 
67 protected:
68 
69 
70  template<typename Expression>
71  void construct(Expression& expr)
72  {
73  ::new (static_cast<Base*>(this)) Base(expr.rows(), expr.cols(), expr.nonZeros(), expr.outerIndexPtr(), expr.innerIndexPtr(), expr.valuePtr(), expr.innerNonZeroPtr());
74  }
75 };
76 
77 } // namespace internal
78 
79 
92 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
93 class Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType >
94  : public internal::SparseRefBase<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType > >
95 {
96  typedef SparseMatrix<MatScalar,MatOptions,MatIndex> PlainObjectType;
97  typedef internal::traits<Ref> Traits;
98  template<int OtherOptions>
100  template<int OtherOptions>
102  public:
103 
104  typedef internal::SparseRefBase<Ref> Base;
105  _EIGEN_SPARSE_PUBLIC_INTERFACE(Ref)
106 
107 
108  #ifndef EIGEN_PARSED_BY_DOXYGEN
109  template<int OtherOptions>
111  {
112  EIGEN_STATIC_ASSERT(bool(Traits::template match<SparseMatrix<MatScalar,OtherOptions,MatIndex> >::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
113  eigen_assert( ((Options & int(StandardCompressedFormat))==0) || (expr.isCompressed()) );
114  Base::construct(expr.derived());
115  }
116 
117  template<int OtherOptions>
119  {
120  EIGEN_STATIC_ASSERT(bool(Traits::template match<SparseMatrix<MatScalar,OtherOptions,MatIndex> >::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
121  eigen_assert( ((Options & int(StandardCompressedFormat))==0) || (expr.isCompressed()) );
122  Base::construct(expr.derived());
123  }
124 
125  template<typename Derived>
126  inline Ref(const SparseCompressedBase<Derived>& expr)
127  #else
128  template<typename Derived>
129  inline Ref(SparseCompressedBase<Derived>& expr)
130  #endif
131  {
132  EIGEN_STATIC_ASSERT(bool(internal::is_lvalue<Derived>::value), THIS_EXPRESSION_IS_NOT_A_LVALUE__IT_IS_READ_ONLY);
133  EIGEN_STATIC_ASSERT(bool(Traits::template match<Derived>::MatchAtCompileTime), STORAGE_LAYOUT_DOES_NOT_MATCH);
134  eigen_assert( ((Options & int(StandardCompressedFormat))==0) || (expr.isCompressed()) );
135  Base::construct(expr.const_cast_derived());
136  }
137 };
138 
139 // this is the const ref version
140 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
141 class Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType>
142  : public internal::SparseRefBase<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
143 {
144  typedef SparseMatrix<MatScalar,MatOptions,MatIndex> TPlainObjectType;
145  typedef internal::traits<Ref> Traits;
146  public:
147 
148  typedef internal::SparseRefBase<Ref> Base;
149  _EIGEN_SPARSE_PUBLIC_INTERFACE(Ref)
150 
151  template<typename Derived>
152  inline Ref(const SparseMatrixBase<Derived>& expr)
153  {
154  construct(expr.derived(), typename Traits::template match<Derived>::type());
155  }
156 
157  inline Ref(const Ref& other) : Base(other) {
158  // copy constructor shall not copy the m_object, to avoid unnecessary malloc and copy
159  }
160 
161  template<typename OtherRef>
162  inline Ref(const RefBase<OtherRef>& other) {
163  construct(other.derived(), typename Traits::template match<OtherRef>::type());
164  }
165 
166  protected:
167 
168  template<typename Expression>
169  void construct(const Expression& expr,internal::true_type)
170  {
171  if((Options & int(StandardCompressedFormat)) && (!expr.isCompressed()))
172  {
173  m_object = expr;
174  Base::construct(m_object);
175  }
176  else
177  {
178  Base::construct(expr);
179  }
180  }
181 
182  template<typename Expression>
183  void construct(const Expression& expr, internal::false_type)
184  {
185  m_object = expr;
186  Base::construct(m_object);
187  }
188 
189  protected:
190  TPlainObjectType m_object;
191 };
192 
193 
194 namespace internal {
195 
196 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
197 struct evaluator<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
198  : evaluator<SparseCompressedBase<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
199 {
200  typedef evaluator<SparseCompressedBase<Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
201  typedef Ref<SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
202  evaluator() : Base() {}
203  explicit evaluator(const XprType &mat) : Base(mat) {}
204 };
205 
206 template<typename MatScalar, int MatOptions, typename MatIndex, int Options, typename StrideType>
207 struct evaluator<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> >
208  : evaluator<SparseCompressedBase<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > >
209 {
210  typedef evaluator<SparseCompressedBase<Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> > > Base;
211  typedef Ref<const SparseMatrix<MatScalar,MatOptions,MatIndex>, Options, StrideType> XprType;
212  evaluator() : Base() {}
213  explicit evaluator(const XprType &mat) : Base(mat) {}
214 };
215 
216 }
217 
218 } // end namespace Eigen
219 
220 #endif // EIGEN_SPARSE_REF_H
const unsigned int CompressedAccessBit
Definition: Constants.h:177
A versatible sparse matrix representation.
Definition: SparseMatrix.h:92
const unsigned int LvalueBit
Definition: Constants.h:130
Definition: LDLT.h:16
Derived & derived()
Definition: EigenBase.h:44
const unsigned int RowMajorBit
Definition: Constants.h:53
Base class of any sparse matrices or sparse expressions.
Definition: SparseMatrixBase.h:26
A matrix or vector expression mapping an existing expression.
Definition: Ref.h:186
Definition: Eigen_Colamd.h:54
Sparse matrix.
Definition: MappedSparseMatrix.h:32