$darkmode
Eigen  5.0.1-dev
ArithmeticSequence.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2017 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_ARITHMETIC_SEQUENCE_H
11 #define EIGEN_ARITHMETIC_SEQUENCE_H
12 
13 // IWYU pragma: private
14 #include "./InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 // Helper to cleanup the type of the increment:
21 template <typename T>
22 struct cleanup_seq_incr {
23  typedef typename cleanup_index_type<T, DynamicIndex>::type type;
24 };
25 
26 } // namespace internal
27 
28 //--------------------------------------------------------------------------------
29 // seq(first,last,incr) and seqN(first,size,incr)
30 //--------------------------------------------------------------------------------
31 
32 template <typename FirstType = Index, typename SizeType = Index, typename IncrType = internal::FixedInt<1> >
34 
35 template <typename FirstType, typename SizeType, typename IncrType>
37  typename internal::cleanup_index_type<SizeType>::type,
38  typename internal::cleanup_seq_incr<IncrType>::type>
39 seqN(FirstType first, SizeType size, IncrType incr);
40 
61 template <typename FirstType, typename SizeType, typename IncrType>
62 class ArithmeticSequence {
63  public:
64  constexpr ArithmeticSequence() = default;
65  constexpr ArithmeticSequence(FirstType first, SizeType size) : m_first(first), m_size(size) {}
66  constexpr ArithmeticSequence(FirstType first, SizeType size, IncrType incr)
67  : m_first(first), m_size(size), m_incr(incr) {}
68 
69  enum {
70  // SizeAtCompileTime = internal::get_fixed_value<SizeType>::value,
71  IncrAtCompileTime = internal::get_fixed_value<IncrType, DynamicIndex>::value
72  };
73 
75  constexpr Index size() const { return m_size; }
76 
78  constexpr Index first() const { return m_first; }
79 
81  constexpr Index operator[](Index i) const { return m_first + i * m_incr; }
82 
83  constexpr const FirstType& firstObject() const { return m_first; }
84  constexpr const SizeType& sizeObject() const { return m_size; }
85  constexpr const IncrType& incrObject() const { return m_incr; }
86 
87  protected:
88  FirstType m_first;
89  SizeType m_size;
90  IncrType m_incr;
91 
92  public:
93  constexpr auto reverse() const -> decltype(Eigen::seqN(m_first + (m_size + fix<-1>()) * m_incr, m_size, -m_incr)) {
94  return seqN(m_first + (m_size + fix<-1>()) * m_incr, m_size, -m_incr);
95  }
96 };
97 
101 template <typename FirstType, typename SizeType, typename IncrType>
102 ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,
103  typename internal::cleanup_index_type<SizeType>::type,
104  typename internal::cleanup_seq_incr<IncrType>::type>
105 seqN(FirstType first, SizeType size, IncrType incr) {
107  typename internal::cleanup_index_type<SizeType>::type,
108  typename internal::cleanup_seq_incr<IncrType>::type>(first, size, incr);
109 }
110 
114 template <typename FirstType, typename SizeType>
115 ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,
116  typename internal::cleanup_index_type<SizeType>::type>
117 seqN(FirstType first, SizeType size) {
119  typename internal::cleanup_index_type<SizeType>::type>(first, size);
120 }
121 
122 #ifdef EIGEN_PARSED_BY_DOXYGEN
123 
134 template <typename FirstType, typename LastType, typename IncrType>
135 auto seq(FirstType f, LastType l, IncrType incr);
136 
146 template <typename FirstType, typename LastType>
147 auto seq(FirstType f, LastType l);
148 
149 #else // EIGEN_PARSED_BY_DOXYGEN
150 
151 template <typename FirstType, typename LastType>
152 auto seq(FirstType f, LastType l)
153  -> decltype(seqN(typename internal::cleanup_index_type<FirstType>::type(f),
154  (typename internal::cleanup_index_type<LastType>::type(l) -
155  typename internal::cleanup_index_type<FirstType>::type(f) + fix<1>()))) {
156  return seqN(typename internal::cleanup_index_type<FirstType>::type(f),
157  (typename internal::cleanup_index_type<LastType>::type(l) -
158  typename internal::cleanup_index_type<FirstType>::type(f) + fix<1>()));
159 }
160 
161 template <typename FirstType, typename LastType, typename IncrType>
162 auto seq(FirstType f, LastType l, IncrType incr)
163  -> decltype(seqN(typename internal::cleanup_index_type<FirstType>::type(f),
164  (typename internal::cleanup_index_type<LastType>::type(l) -
165  typename internal::cleanup_index_type<FirstType>::type(f) +
166  typename internal::cleanup_seq_incr<IncrType>::type(incr)) /
167  typename internal::cleanup_seq_incr<IncrType>::type(incr),
168  typename internal::cleanup_seq_incr<IncrType>::type(incr))) {
169  typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType;
170  return seqN(typename internal::cleanup_index_type<FirstType>::type(f),
171  (typename internal::cleanup_index_type<LastType>::type(l) -
172  typename internal::cleanup_index_type<FirstType>::type(f) + CleanedIncrType(incr)) /
173  CleanedIncrType(incr),
174  CleanedIncrType(incr));
175 }
176 
177 #endif // EIGEN_PARSED_BY_DOXYGEN
178 
179 namespace placeholders {
180 
187 template <typename SizeType, typename IncrType>
188 auto lastN(SizeType size, IncrType incr)
189  -> decltype(seqN(Eigen::placeholders::last - (size - fix<1>()) * incr, size, incr)) {
190  return seqN(Eigen::placeholders::last - (size - fix<1>()) * incr, size, incr);
191 }
192 
199 template <typename SizeType>
200 auto lastN(SizeType size) -> decltype(seqN(Eigen::placeholders::last + fix<1>() - size, size)) {
201  return seqN(Eigen::placeholders::last + fix<1>() - size, size);
202 }
203 
204 } // namespace placeholders
205 
227 namespace indexing {
228 using Eigen::fix;
229 using Eigen::seq;
230 using Eigen::seqN;
233 using Eigen::placeholders::lastN;
235 } // namespace indexing
236 
237 } // end namespace Eigen
238 
239 #endif // EIGEN_ARITHMETIC_SEQUENCE_H
static constexpr Eigen::internal::all_t all
Definition: IndexedViewHelper.h:86
constexpr Index operator[](Index i) const
Definition: ArithmeticSequence.h:81
constexpr Index size() const
Definition: ArithmeticSequence.h:75
Namespace containing all symbols from the Eigen library.
Definition: B01_Experimental.dox:1
static constexpr auto lastp1
Definition: IndexedViewHelper.h:68
auto seq(FirstType f, LastType l, IncrType incr)
static const auto fix()
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
constexpr Index first() const
Definition: ArithmeticSequence.h:78
Definition: ArithmeticSequence.h:33
static constexpr const last_t last
Definition: IndexedViewHelper.h:48
ArithmeticSequence< typename internal::cleanup_index_type< FirstType >::type, typename internal::cleanup_index_type< SizeType >::type, typename internal::cleanup_seq_incr< IncrType >::type > seqN(FirstType first, SizeType size, IncrType incr)
Definition: ArithmeticSequence.h:105