DenseCoeffsBase.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
  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. #ifndef EIGEN_DENSECOEFFSBASE_H
  10. #define EIGEN_DENSECOEFFSBASE_H
  11. namespace Eigen {
  12. namespace internal {
  13. template<typename T> struct add_const_on_value_type_if_arithmetic
  14. {
  15. typedef typename conditional<is_arithmetic<T>::value, T, typename add_const_on_value_type<T>::type>::type type;
  16. };
  17. }
  18. /** \brief Base class providing read-only coefficient access to matrices and arrays.
  19. * \ingroup Core_Module
  20. * \tparam Derived Type of the derived class
  21. * \tparam #ReadOnlyAccessors Constant indicating read-only access
  22. *
  23. * This class defines the \c operator() \c const function and friends, which can be used to read specific
  24. * entries of a matrix or array.
  25. *
  26. * \sa DenseCoeffsBase<Derived, WriteAccessors>, DenseCoeffsBase<Derived, DirectAccessors>,
  27. * \ref TopicClassHierarchy
  28. */
  29. template<typename Derived>
  30. class DenseCoeffsBase<Derived,ReadOnlyAccessors> : public EigenBase<Derived>
  31. {
  32. public:
  33. typedef typename internal::traits<Derived>::StorageKind StorageKind;
  34. typedef typename internal::traits<Derived>::Scalar Scalar;
  35. typedef typename internal::packet_traits<Scalar>::type PacketScalar;
  36. // Explanation for this CoeffReturnType typedef.
  37. // - This is the return type of the coeff() method.
  38. // - The LvalueBit means exactly that we can offer a coeffRef() method, which means exactly that we can get references
  39. // to coeffs, which means exactly that we can have coeff() return a const reference (as opposed to returning a value).
  40. // - The is_artihmetic check is required since "const int", "const double", etc. will cause warnings on some systems
  41. // while the declaration of "const T", where T is a non arithmetic type does not. Always returning "const Scalar&" is
  42. // not possible, since the underlying expressions might not offer a valid address the reference could be referring to.
  43. typedef typename internal::conditional<bool(internal::traits<Derived>::Flags&LvalueBit),
  44. const Scalar&,
  45. typename internal::conditional<internal::is_arithmetic<Scalar>::value, Scalar, const Scalar>::type
  46. >::type CoeffReturnType;
  47. typedef typename internal::add_const_on_value_type_if_arithmetic<
  48. typename internal::packet_traits<Scalar>::type
  49. >::type PacketReturnType;
  50. typedef EigenBase<Derived> Base;
  51. using Base::rows;
  52. using Base::cols;
  53. using Base::size;
  54. using Base::derived;
  55. EIGEN_DEVICE_FUNC
  56. EIGEN_STRONG_INLINE Index rowIndexByOuterInner(Index outer, Index inner) const
  57. {
  58. return int(Derived::RowsAtCompileTime) == 1 ? 0
  59. : int(Derived::ColsAtCompileTime) == 1 ? inner
  60. : int(Derived::Flags)&RowMajorBit ? outer
  61. : inner;
  62. }
  63. EIGEN_DEVICE_FUNC
  64. EIGEN_STRONG_INLINE Index colIndexByOuterInner(Index outer, Index inner) const
  65. {
  66. return int(Derived::ColsAtCompileTime) == 1 ? 0
  67. : int(Derived::RowsAtCompileTime) == 1 ? inner
  68. : int(Derived::Flags)&RowMajorBit ? inner
  69. : outer;
  70. }
  71. /** Short version: don't use this function, use
  72. * \link operator()(Index,Index) const \endlink instead.
  73. *
  74. * Long version: this function is similar to
  75. * \link operator()(Index,Index) const \endlink, but without the assertion.
  76. * Use this for limiting the performance cost of debugging code when doing
  77. * repeated coefficient access. Only use this when it is guaranteed that the
  78. * parameters \a row and \a col are in range.
  79. *
  80. * If EIGEN_INTERNAL_DEBUGGING is defined, an assertion will be made, making this
  81. * function equivalent to \link operator()(Index,Index) const \endlink.
  82. *
  83. * \sa operator()(Index,Index) const, coeffRef(Index,Index), coeff(Index) const
  84. */
  85. EIGEN_DEVICE_FUNC
  86. EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col) const
  87. {
  88. eigen_internal_assert(row >= 0 && row < rows()
  89. && col >= 0 && col < cols());
  90. return internal::evaluator<Derived>(derived()).coeff(row,col);
  91. }
  92. EIGEN_DEVICE_FUNC
  93. EIGEN_STRONG_INLINE CoeffReturnType coeffByOuterInner(Index outer, Index inner) const
  94. {
  95. return coeff(rowIndexByOuterInner(outer, inner),
  96. colIndexByOuterInner(outer, inner));
  97. }
  98. /** \returns the coefficient at given the given row and column.
  99. *
  100. * \sa operator()(Index,Index), operator[](Index)
  101. */
  102. EIGEN_DEVICE_FUNC
  103. EIGEN_STRONG_INLINE CoeffReturnType operator()(Index row, Index col) const
  104. {
  105. eigen_assert(row >= 0 && row < rows()
  106. && col >= 0 && col < cols());
  107. return coeff(row, col);
  108. }
  109. /** Short version: don't use this function, use
  110. * \link operator[](Index) const \endlink instead.
  111. *
  112. * Long version: this function is similar to
  113. * \link operator[](Index) const \endlink, but without the assertion.
  114. * Use this for limiting the performance cost of debugging code when doing
  115. * repeated coefficient access. Only use this when it is guaranteed that the
  116. * parameter \a index is in range.
  117. *
  118. * If EIGEN_INTERNAL_DEBUGGING is defined, an assertion will be made, making this
  119. * function equivalent to \link operator[](Index) const \endlink.
  120. *
  121. * \sa operator[](Index) const, coeffRef(Index), coeff(Index,Index) const
  122. */
  123. EIGEN_DEVICE_FUNC
  124. EIGEN_STRONG_INLINE CoeffReturnType
  125. coeff(Index index) const
  126. {
  127. EIGEN_STATIC_ASSERT(internal::evaluator<Derived>::Flags & LinearAccessBit,
  128. THIS_COEFFICIENT_ACCESSOR_TAKING_ONE_ACCESS_IS_ONLY_FOR_EXPRESSIONS_ALLOWING_LINEAR_ACCESS)
  129. eigen_internal_assert(index >= 0 && index < size());
  130. return internal::evaluator<Derived>(derived()).coeff(index);
  131. }
  132. /** \returns the coefficient at given index.
  133. *
  134. * This method is allowed only for vector expressions, and for matrix expressions having the LinearAccessBit.
  135. *
  136. * \sa operator[](Index), operator()(Index,Index) const, x() const, y() const,
  137. * z() const, w() const
  138. */
  139. EIGEN_DEVICE_FUNC
  140. EIGEN_STRONG_INLINE CoeffReturnType
  141. operator[](Index index) const
  142. {
  143. EIGEN_STATIC_ASSERT(Derived::IsVectorAtCompileTime,
  144. THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD)
  145. eigen_assert(index >= 0 && index < size());
  146. return coeff(index);
  147. }
  148. /** \returns the coefficient at given index.
  149. *
  150. * This is synonymous to operator[](Index) const.
  151. *
  152. * This method is allowed only for vector expressions, and for matrix expressions having the LinearAccessBit.
  153. *
  154. * \sa operator[](Index), operator()(Index,Index) const, x() const, y() const,
  155. * z() const, w() const
  156. */
  157. EIGEN_DEVICE_FUNC
  158. EIGEN_STRONG_INLINE CoeffReturnType
  159. operator()(Index index) const
  160. {
  161. eigen_assert(index >= 0 && index < size());
  162. return coeff(index);
  163. }
  164. /** equivalent to operator[](0). */
  165. EIGEN_DEVICE_FUNC
  166. EIGEN_STRONG_INLINE CoeffReturnType
  167. x() const { return (*this)[0]; }
  168. /** equivalent to operator[](1). */
  169. EIGEN_DEVICE_FUNC
  170. EIGEN_STRONG_INLINE CoeffReturnType
  171. y() const
  172. {
  173. EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime==-1 || Derived::SizeAtCompileTime>=2, OUT_OF_RANGE_ACCESS);
  174. return (*this)[1];
  175. }
  176. /** equivalent to operator[](2). */
  177. EIGEN_DEVICE_FUNC
  178. EIGEN_STRONG_INLINE CoeffReturnType
  179. z() const
  180. {
  181. EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime==-1 || Derived::SizeAtCompileTime>=3, OUT_OF_RANGE_ACCESS);
  182. return (*this)[2];
  183. }
  184. /** equivalent to operator[](3). */
  185. EIGEN_DEVICE_FUNC
  186. EIGEN_STRONG_INLINE CoeffReturnType
  187. w() const
  188. {
  189. EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime==-1 || Derived::SizeAtCompileTime>=4, OUT_OF_RANGE_ACCESS);
  190. return (*this)[3];
  191. }
  192. /** \internal
  193. * \returns the packet of coefficients starting at the given row and column. It is your responsibility
  194. * to ensure that a packet really starts there. This method is only available on expressions having the
  195. * PacketAccessBit.
  196. *
  197. * The \a LoadMode parameter may have the value \a #Aligned or \a #Unaligned. Its effect is to select
  198. * the appropriate vectorization instruction. Aligned access is faster, but is only possible for packets
  199. * starting at an address which is a multiple of the packet size.
  200. */
  201. template<int LoadMode>
  202. EIGEN_STRONG_INLINE PacketReturnType packet(Index row, Index col) const
  203. {
  204. typedef typename internal::packet_traits<Scalar>::type DefaultPacketType;
  205. eigen_internal_assert(row >= 0 && row < rows() && col >= 0 && col < cols());
  206. return internal::evaluator<Derived>(derived()).template packet<LoadMode,DefaultPacketType>(row,col);
  207. }
  208. /** \internal */
  209. template<int LoadMode>
  210. EIGEN_STRONG_INLINE PacketReturnType packetByOuterInner(Index outer, Index inner) const
  211. {
  212. return packet<LoadMode>(rowIndexByOuterInner(outer, inner),
  213. colIndexByOuterInner(outer, inner));
  214. }
  215. /** \internal
  216. * \returns the packet of coefficients starting at the given index. It is your responsibility
  217. * to ensure that a packet really starts there. This method is only available on expressions having the
  218. * PacketAccessBit and the LinearAccessBit.
  219. *
  220. * The \a LoadMode parameter may have the value \a #Aligned or \a #Unaligned. Its effect is to select
  221. * the appropriate vectorization instruction. Aligned access is faster, but is only possible for packets
  222. * starting at an address which is a multiple of the packet size.
  223. */
  224. template<int LoadMode>
  225. EIGEN_STRONG_INLINE PacketReturnType packet(Index index) const
  226. {
  227. EIGEN_STATIC_ASSERT(internal::evaluator<Derived>::Flags & LinearAccessBit,
  228. THIS_COEFFICIENT_ACCESSOR_TAKING_ONE_ACCESS_IS_ONLY_FOR_EXPRESSIONS_ALLOWING_LINEAR_ACCESS)
  229. typedef typename internal::packet_traits<Scalar>::type DefaultPacketType;
  230. eigen_internal_assert(index >= 0 && index < size());
  231. return internal::evaluator<Derived>(derived()).template packet<LoadMode,DefaultPacketType>(index);
  232. }
  233. protected:
  234. // explanation: DenseBase is doing "using ..." on the methods from DenseCoeffsBase.
  235. // But some methods are only available in the DirectAccess case.
  236. // So we add dummy methods here with these names, so that "using... " doesn't fail.
  237. // It's not private so that the child class DenseBase can access them, and it's not public
  238. // either since it's an implementation detail, so has to be protected.
  239. void coeffRef();
  240. void coeffRefByOuterInner();
  241. void writePacket();
  242. void writePacketByOuterInner();
  243. void copyCoeff();
  244. void copyCoeffByOuterInner();
  245. void copyPacket();
  246. void copyPacketByOuterInner();
  247. void stride();
  248. void innerStride();
  249. void outerStride();
  250. void rowStride();
  251. void colStride();
  252. };
  253. /** \brief Base class providing read/write coefficient access to matrices and arrays.
  254. * \ingroup Core_Module
  255. * \tparam Derived Type of the derived class
  256. * \tparam #WriteAccessors Constant indicating read/write access
  257. *
  258. * This class defines the non-const \c operator() function and friends, which can be used to write specific
  259. * entries of a matrix or array. This class inherits DenseCoeffsBase<Derived, ReadOnlyAccessors> which
  260. * defines the const variant for reading specific entries.
  261. *
  262. * \sa DenseCoeffsBase<Derived, DirectAccessors>, \ref TopicClassHierarchy
  263. */
  264. template<typename Derived>
  265. class DenseCoeffsBase<Derived, WriteAccessors> : public DenseCoeffsBase<Derived, ReadOnlyAccessors>
  266. {
  267. public:
  268. typedef DenseCoeffsBase<Derived, ReadOnlyAccessors> Base;
  269. typedef typename internal::traits<Derived>::StorageKind StorageKind;
  270. typedef typename internal::traits<Derived>::Scalar Scalar;
  271. typedef typename internal::packet_traits<Scalar>::type PacketScalar;
  272. typedef typename NumTraits<Scalar>::Real RealScalar;
  273. using Base::coeff;
  274. using Base::rows;
  275. using Base::cols;
  276. using Base::size;
  277. using Base::derived;
  278. using Base::rowIndexByOuterInner;
  279. using Base::colIndexByOuterInner;
  280. using Base::operator[];
  281. using Base::operator();
  282. using Base::x;
  283. using Base::y;
  284. using Base::z;
  285. using Base::w;
  286. /** Short version: don't use this function, use
  287. * \link operator()(Index,Index) \endlink instead.
  288. *
  289. * Long version: this function is similar to
  290. * \link operator()(Index,Index) \endlink, but without the assertion.
  291. * Use this for limiting the performance cost of debugging code when doing
  292. * repeated coefficient access. Only use this when it is guaranteed that the
  293. * parameters \a row and \a col are in range.
  294. *
  295. * If EIGEN_INTERNAL_DEBUGGING is defined, an assertion will be made, making this
  296. * function equivalent to \link operator()(Index,Index) \endlink.
  297. *
  298. * \sa operator()(Index,Index), coeff(Index, Index) const, coeffRef(Index)
  299. */
  300. EIGEN_DEVICE_FUNC
  301. EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col)
  302. {
  303. eigen_internal_assert(row >= 0 && row < rows()
  304. && col >= 0 && col < cols());
  305. return internal::evaluator<Derived>(derived()).coeffRef(row,col);
  306. }
  307. EIGEN_DEVICE_FUNC
  308. EIGEN_STRONG_INLINE Scalar&
  309. coeffRefByOuterInner(Index outer, Index inner)
  310. {
  311. return coeffRef(rowIndexByOuterInner(outer, inner),
  312. colIndexByOuterInner(outer, inner));
  313. }
  314. /** \returns a reference to the coefficient at given the given row and column.
  315. *
  316. * \sa operator[](Index)
  317. */
  318. EIGEN_DEVICE_FUNC
  319. EIGEN_STRONG_INLINE Scalar&
  320. operator()(Index row, Index col)
  321. {
  322. eigen_assert(row >= 0 && row < rows()
  323. && col >= 0 && col < cols());
  324. return coeffRef(row, col);
  325. }
  326. /** Short version: don't use this function, use
  327. * \link operator[](Index) \endlink instead.
  328. *
  329. * Long version: this function is similar to
  330. * \link operator[](Index) \endlink, but without the assertion.
  331. * Use this for limiting the performance cost of debugging code when doing
  332. * repeated coefficient access. Only use this when it is guaranteed that the
  333. * parameters \a row and \a col are in range.
  334. *
  335. * If EIGEN_INTERNAL_DEBUGGING is defined, an assertion will be made, making this
  336. * function equivalent to \link operator[](Index) \endlink.
  337. *
  338. * \sa operator[](Index), coeff(Index) const, coeffRef(Index,Index)
  339. */
  340. EIGEN_DEVICE_FUNC
  341. EIGEN_STRONG_INLINE Scalar&
  342. coeffRef(Index index)
  343. {
  344. EIGEN_STATIC_ASSERT(internal::evaluator<Derived>::Flags & LinearAccessBit,
  345. THIS_COEFFICIENT_ACCESSOR_TAKING_ONE_ACCESS_IS_ONLY_FOR_EXPRESSIONS_ALLOWING_LINEAR_ACCESS)
  346. eigen_internal_assert(index >= 0 && index < size());
  347. return internal::evaluator<Derived>(derived()).coeffRef(index);
  348. }
  349. /** \returns a reference to the coefficient at given index.
  350. *
  351. * This method is allowed only for vector expressions, and for matrix expressions having the LinearAccessBit.
  352. *
  353. * \sa operator[](Index) const, operator()(Index,Index), x(), y(), z(), w()
  354. */
  355. EIGEN_DEVICE_FUNC
  356. EIGEN_STRONG_INLINE Scalar&
  357. operator[](Index index)
  358. {
  359. EIGEN_STATIC_ASSERT(Derived::IsVectorAtCompileTime,
  360. THE_BRACKET_OPERATOR_IS_ONLY_FOR_VECTORS__USE_THE_PARENTHESIS_OPERATOR_INSTEAD)
  361. eigen_assert(index >= 0 && index < size());
  362. return coeffRef(index);
  363. }
  364. /** \returns a reference to the coefficient at given index.
  365. *
  366. * This is synonymous to operator[](Index).
  367. *
  368. * This method is allowed only for vector expressions, and for matrix expressions having the LinearAccessBit.
  369. *
  370. * \sa operator[](Index) const, operator()(Index,Index), x(), y(), z(), w()
  371. */
  372. EIGEN_DEVICE_FUNC
  373. EIGEN_STRONG_INLINE Scalar&
  374. operator()(Index index)
  375. {
  376. eigen_assert(index >= 0 && index < size());
  377. return coeffRef(index);
  378. }
  379. /** equivalent to operator[](0). */
  380. EIGEN_DEVICE_FUNC
  381. EIGEN_STRONG_INLINE Scalar&
  382. x() { return (*this)[0]; }
  383. /** equivalent to operator[](1). */
  384. EIGEN_DEVICE_FUNC
  385. EIGEN_STRONG_INLINE Scalar&
  386. y()
  387. {
  388. EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime==-1 || Derived::SizeAtCompileTime>=2, OUT_OF_RANGE_ACCESS);
  389. return (*this)[1];
  390. }
  391. /** equivalent to operator[](2). */
  392. EIGEN_DEVICE_FUNC
  393. EIGEN_STRONG_INLINE Scalar&
  394. z()
  395. {
  396. EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime==-1 || Derived::SizeAtCompileTime>=3, OUT_OF_RANGE_ACCESS);
  397. return (*this)[2];
  398. }
  399. /** equivalent to operator[](3). */
  400. EIGEN_DEVICE_FUNC
  401. EIGEN_STRONG_INLINE Scalar&
  402. w()
  403. {
  404. EIGEN_STATIC_ASSERT(Derived::SizeAtCompileTime==-1 || Derived::SizeAtCompileTime>=4, OUT_OF_RANGE_ACCESS);
  405. return (*this)[3];
  406. }
  407. };
  408. /** \brief Base class providing direct read-only coefficient access to matrices and arrays.
  409. * \ingroup Core_Module
  410. * \tparam Derived Type of the derived class
  411. * \tparam #DirectAccessors Constant indicating direct access
  412. *
  413. * This class defines functions to work with strides which can be used to access entries directly. This class
  414. * inherits DenseCoeffsBase<Derived, ReadOnlyAccessors> which defines functions to access entries read-only using
  415. * \c operator() .
  416. *
  417. * \sa \blank \ref TopicClassHierarchy
  418. */
  419. template<typename Derived>
  420. class DenseCoeffsBase<Derived, DirectAccessors> : public DenseCoeffsBase<Derived, ReadOnlyAccessors>
  421. {
  422. public:
  423. typedef DenseCoeffsBase<Derived, ReadOnlyAccessors> Base;
  424. typedef typename internal::traits<Derived>::Scalar Scalar;
  425. typedef typename NumTraits<Scalar>::Real RealScalar;
  426. using Base::rows;
  427. using Base::cols;
  428. using Base::size;
  429. using Base::derived;
  430. /** \returns the pointer increment between two consecutive elements within a slice in the inner direction.
  431. *
  432. * \sa outerStride(), rowStride(), colStride()
  433. */
  434. EIGEN_DEVICE_FUNC
  435. inline Index innerStride() const
  436. {
  437. return derived().innerStride();
  438. }
  439. /** \returns the pointer increment between two consecutive inner slices (for example, between two consecutive columns
  440. * in a column-major matrix).
  441. *
  442. * \sa innerStride(), rowStride(), colStride()
  443. */
  444. EIGEN_DEVICE_FUNC
  445. inline Index outerStride() const
  446. {
  447. return derived().outerStride();
  448. }
  449. // FIXME shall we remove it ?
  450. inline Index stride() const
  451. {
  452. return Derived::IsVectorAtCompileTime ? innerStride() : outerStride();
  453. }
  454. /** \returns the pointer increment between two consecutive rows.
  455. *
  456. * \sa innerStride(), outerStride(), colStride()
  457. */
  458. EIGEN_DEVICE_FUNC
  459. inline Index rowStride() const
  460. {
  461. return Derived::IsRowMajor ? outerStride() : innerStride();
  462. }
  463. /** \returns the pointer increment between two consecutive columns.
  464. *
  465. * \sa innerStride(), outerStride(), rowStride()
  466. */
  467. EIGEN_DEVICE_FUNC
  468. inline Index colStride() const
  469. {
  470. return Derived::IsRowMajor ? innerStride() : outerStride();
  471. }
  472. };
  473. /** \brief Base class providing direct read/write coefficient access to matrices and arrays.
  474. * \ingroup Core_Module
  475. * \tparam Derived Type of the derived class
  476. * \tparam #DirectWriteAccessors Constant indicating direct access
  477. *
  478. * This class defines functions to work with strides which can be used to access entries directly. This class
  479. * inherits DenseCoeffsBase<Derived, WriteAccessors> which defines functions to access entries read/write using
  480. * \c operator().
  481. *
  482. * \sa \blank \ref TopicClassHierarchy
  483. */
  484. template<typename Derived>
  485. class DenseCoeffsBase<Derived, DirectWriteAccessors>
  486. : public DenseCoeffsBase<Derived, WriteAccessors>
  487. {
  488. public:
  489. typedef DenseCoeffsBase<Derived, WriteAccessors> Base;
  490. typedef typename internal::traits<Derived>::Scalar Scalar;
  491. typedef typename NumTraits<Scalar>::Real RealScalar;
  492. using Base::rows;
  493. using Base::cols;
  494. using Base::size;
  495. using Base::derived;
  496. /** \returns the pointer increment between two consecutive elements within a slice in the inner direction.
  497. *
  498. * \sa outerStride(), rowStride(), colStride()
  499. */
  500. EIGEN_DEVICE_FUNC
  501. inline Index innerStride() const
  502. {
  503. return derived().innerStride();
  504. }
  505. /** \returns the pointer increment between two consecutive inner slices (for example, between two consecutive columns
  506. * in a column-major matrix).
  507. *
  508. * \sa innerStride(), rowStride(), colStride()
  509. */
  510. EIGEN_DEVICE_FUNC
  511. inline Index outerStride() const
  512. {
  513. return derived().outerStride();
  514. }
  515. // FIXME shall we remove it ?
  516. inline Index stride() const
  517. {
  518. return Derived::IsVectorAtCompileTime ? innerStride() : outerStride();
  519. }
  520. /** \returns the pointer increment between two consecutive rows.
  521. *
  522. * \sa innerStride(), outerStride(), colStride()
  523. */
  524. EIGEN_DEVICE_FUNC
  525. inline Index rowStride() const
  526. {
  527. return Derived::IsRowMajor ? outerStride() : innerStride();
  528. }
  529. /** \returns the pointer increment between two consecutive columns.
  530. *
  531. * \sa innerStride(), outerStride(), rowStride()
  532. */
  533. EIGEN_DEVICE_FUNC
  534. inline Index colStride() const
  535. {
  536. return Derived::IsRowMajor ? innerStride() : outerStride();
  537. }
  538. };
  539. namespace internal {
  540. template<int Alignment, typename Derived, bool JustReturnZero>
  541. struct first_aligned_impl
  542. {
  543. static inline Index run(const Derived&)
  544. { return 0; }
  545. };
  546. template<int Alignment, typename Derived>
  547. struct first_aligned_impl<Alignment, Derived, false>
  548. {
  549. static inline Index run(const Derived& m)
  550. {
  551. return internal::first_aligned<Alignment>(m.data(), m.size());
  552. }
  553. };
  554. /** \internal \returns the index of the first element of the array stored by \a m that is properly aligned with respect to \a Alignment for vectorization.
  555. *
  556. * \tparam Alignment requested alignment in Bytes.
  557. *
  558. * There is also the variant first_aligned(const Scalar*, Integer) defined in Memory.h. See it for more
  559. * documentation.
  560. */
  561. template<int Alignment, typename Derived>
  562. static inline Index first_aligned(const DenseBase<Derived>& m)
  563. {
  564. enum { ReturnZero = (int(evaluator<Derived>::Alignment) >= Alignment) || !(Derived::Flags & DirectAccessBit) };
  565. return first_aligned_impl<Alignment, Derived, ReturnZero>::run(m.derived());
  566. }
  567. template<typename Derived>
  568. static inline Index first_default_aligned(const DenseBase<Derived>& m)
  569. {
  570. typedef typename Derived::Scalar Scalar;
  571. typedef typename packet_traits<Scalar>::type DefaultPacketType;
  572. return internal::first_aligned<int(unpacket_traits<DefaultPacketType>::alignment),Derived>(m);
  573. }
  574. template<typename Derived, bool HasDirectAccess = has_direct_access<Derived>::ret>
  575. struct inner_stride_at_compile_time
  576. {
  577. enum { ret = traits<Derived>::InnerStrideAtCompileTime };
  578. };
  579. template<typename Derived>
  580. struct inner_stride_at_compile_time<Derived, false>
  581. {
  582. enum { ret = 0 };
  583. };
  584. template<typename Derived, bool HasDirectAccess = has_direct_access<Derived>::ret>
  585. struct outer_stride_at_compile_time
  586. {
  587. enum { ret = traits<Derived>::OuterStrideAtCompileTime };
  588. };
  589. template<typename Derived>
  590. struct outer_stride_at_compile_time<Derived, false>
  591. {
  592. enum { ret = 0 };
  593. };
  594. } // end namespace internal
  595. } // end namespace Eigen
  596. #endif // EIGEN_DENSECOEFFSBASE_H