Stride.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 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_STRIDE_H
  10. #define EIGEN_STRIDE_H
  11. namespace Eigen {
  12. /** \class Stride
  13. * \ingroup Core_Module
  14. *
  15. * \brief Holds strides information for Map
  16. *
  17. * This class holds the strides information for mapping arrays with strides with class Map.
  18. *
  19. * It holds two values: the inner stride and the outer stride.
  20. *
  21. * The inner stride is the pointer increment between two consecutive entries within a given row of a
  22. * row-major matrix or within a given column of a column-major matrix.
  23. *
  24. * The outer stride is the pointer increment between two consecutive rows of a row-major matrix or
  25. * between two consecutive columns of a column-major matrix.
  26. *
  27. * These two values can be passed either at compile-time as template parameters, or at runtime as
  28. * arguments to the constructor.
  29. *
  30. * Indeed, this class takes two template parameters:
  31. * \tparam _OuterStrideAtCompileTime the outer stride, or Dynamic if you want to specify it at runtime.
  32. * \tparam _InnerStrideAtCompileTime the inner stride, or Dynamic if you want to specify it at runtime.
  33. *
  34. * Here is an example:
  35. * \include Map_general_stride.cpp
  36. * Output: \verbinclude Map_general_stride.out
  37. *
  38. * \sa class InnerStride, class OuterStride, \ref TopicStorageOrders
  39. */
  40. template<int _OuterStrideAtCompileTime, int _InnerStrideAtCompileTime>
  41. class Stride
  42. {
  43. public:
  44. typedef Eigen::Index Index; ///< \deprecated since Eigen 3.3
  45. enum {
  46. InnerStrideAtCompileTime = _InnerStrideAtCompileTime,
  47. OuterStrideAtCompileTime = _OuterStrideAtCompileTime
  48. };
  49. /** Default constructor, for use when strides are fixed at compile time */
  50. EIGEN_DEVICE_FUNC
  51. Stride()
  52. : m_outer(OuterStrideAtCompileTime), m_inner(InnerStrideAtCompileTime)
  53. {
  54. eigen_assert(InnerStrideAtCompileTime != Dynamic && OuterStrideAtCompileTime != Dynamic);
  55. }
  56. /** Constructor allowing to pass the strides at runtime */
  57. EIGEN_DEVICE_FUNC
  58. Stride(Index outerStride, Index innerStride)
  59. : m_outer(outerStride), m_inner(innerStride)
  60. {
  61. eigen_assert(innerStride>=0 && outerStride>=0);
  62. }
  63. /** Copy constructor */
  64. EIGEN_DEVICE_FUNC
  65. Stride(const Stride& other)
  66. : m_outer(other.outer()), m_inner(other.inner())
  67. {}
  68. /** \returns the outer stride */
  69. EIGEN_DEVICE_FUNC
  70. inline Index outer() const { return m_outer.value(); }
  71. /** \returns the inner stride */
  72. EIGEN_DEVICE_FUNC
  73. inline Index inner() const { return m_inner.value(); }
  74. protected:
  75. internal::variable_if_dynamic<Index, OuterStrideAtCompileTime> m_outer;
  76. internal::variable_if_dynamic<Index, InnerStrideAtCompileTime> m_inner;
  77. };
  78. /** \brief Convenience specialization of Stride to specify only an inner stride
  79. * See class Map for some examples */
  80. template<int Value>
  81. class InnerStride : public Stride<0, Value>
  82. {
  83. typedef Stride<0, Value> Base;
  84. public:
  85. EIGEN_DEVICE_FUNC InnerStride() : Base() {}
  86. EIGEN_DEVICE_FUNC InnerStride(Index v) : Base(0, v) {} // FIXME making this explicit could break valid code
  87. };
  88. /** \brief Convenience specialization of Stride to specify only an outer stride
  89. * See class Map for some examples */
  90. template<int Value>
  91. class OuterStride : public Stride<Value, 0>
  92. {
  93. typedef Stride<Value, 0> Base;
  94. public:
  95. EIGEN_DEVICE_FUNC OuterStride() : Base() {}
  96. EIGEN_DEVICE_FUNC OuterStride(Index v) : Base(v,0) {} // FIXME making this explicit could break valid code
  97. };
  98. } // end namespace Eigen
  99. #endif // EIGEN_STRIDE_H