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