ArithmeticSequence.h
Go to the documentation of this file.
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 #include "./InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 // Helper to cleanup the type of the increment:
20 template<typename T> struct cleanup_seq_incr {
21  typedef typename cleanup_index_type<T,DynamicIndex>::type type;
22 };
23 
24 } // namespace internal
25 
26 //--------------------------------------------------------------------------------
27 // seq(first,last,incr) and seqN(first,size,incr)
28 //--------------------------------------------------------------------------------
29 
30 template<typename FirstType=Index,typename SizeType=Index,typename IncrType=internal::FixedInt<1> >
31 class ArithmeticSequence;
32 
33 template<typename FirstType,typename SizeType,typename IncrType>
34 ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,
35  typename internal::cleanup_index_type<SizeType>::type,
36  typename internal::cleanup_seq_incr<IncrType>::type >
37 seqN(FirstType first, SizeType size, IncrType incr);
38 
58 template<typename FirstType,typename SizeType,typename IncrType>
60 {
61 public:
62  ArithmeticSequence(FirstType first, SizeType size) : m_first(first), m_size(size) {}
63  ArithmeticSequence(FirstType first, SizeType size, IncrType incr) : m_first(first), m_size(size), m_incr(incr) {}
64 
65  enum {
66  SizeAtCompileTime = internal::get_fixed_value<SizeType>::value,
67  IncrAtCompileTime = internal::get_fixed_value<IncrType,DynamicIndex>::value
68  };
69 
71  Index size() const { return m_size; }
72 
74  Index first() const { return m_first; }
75 
77  Index operator[](Index i) const { return m_first + i * m_incr; }
78 
79  const FirstType& firstObject() const { return m_first; }
80  const SizeType& sizeObject() const { return m_size; }
81  const IncrType& incrObject() const { return m_incr; }
82 
83 protected:
84  FirstType m_first;
85  SizeType m_size;
86  IncrType m_incr;
87 
88 public:
89  auto reverse() const -> decltype(Eigen::seqN(m_first+(m_size+fix<-1>())*m_incr,m_size,-m_incr)) {
90  return seqN(m_first+(m_size+fix<-1>())*m_incr,m_size,-m_incr);
91  }
92 };
93 
97 template<typename FirstType,typename SizeType,typename IncrType>
98 ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,typename internal::cleanup_index_type<SizeType>::type,typename internal::cleanup_seq_incr<IncrType>::type >
99 seqN(FirstType first, SizeType size, IncrType incr) {
100  return ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,typename internal::cleanup_index_type<SizeType>::type,typename internal::cleanup_seq_incr<IncrType>::type>(first,size,incr);
101 }
102 
106 template<typename FirstType,typename SizeType>
107 ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,typename internal::cleanup_index_type<SizeType>::type >
108 seqN(FirstType first, SizeType size) {
109  return ArithmeticSequence<typename internal::cleanup_index_type<FirstType>::type,typename internal::cleanup_index_type<SizeType>::type>(first,size);
110 }
111 
112 #ifdef EIGEN_PARSED_BY_DOXYGEN
113 
123 template<typename FirstType,typename LastType, typename IncrType>
124 auto seq(FirstType f, LastType l, IncrType incr);
125 
135 template<typename FirstType,typename LastType>
136 auto seq(FirstType f, LastType l);
137 
138 #else // EIGEN_PARSED_BY_DOXYGEN
139 
140 template<typename FirstType,typename LastType>
141 auto seq(FirstType f, LastType l) -> decltype(seqN(typename internal::cleanup_index_type<FirstType>::type(f),
142  ( typename internal::cleanup_index_type<LastType>::type(l)
143  - typename internal::cleanup_index_type<FirstType>::type(f)+fix<1>())))
144 {
145  return seqN(typename internal::cleanup_index_type<FirstType>::type(f),
146  (typename internal::cleanup_index_type<LastType>::type(l)
147  -typename internal::cleanup_index_type<FirstType>::type(f)+fix<1>()));
148 }
149 
150 template<typename FirstType,typename LastType, typename IncrType>
151 auto seq(FirstType f, LastType l, IncrType incr)
152  -> decltype(seqN(typename internal::cleanup_index_type<FirstType>::type(f),
153  ( typename internal::cleanup_index_type<LastType>::type(l)
154  - typename internal::cleanup_index_type<FirstType>::type(f)+typename internal::cleanup_seq_incr<IncrType>::type(incr)
155  ) / typename internal::cleanup_seq_incr<IncrType>::type(incr),
156  typename internal::cleanup_seq_incr<IncrType>::type(incr)))
157 {
158  typedef typename internal::cleanup_seq_incr<IncrType>::type CleanedIncrType;
159  return seqN(typename internal::cleanup_index_type<FirstType>::type(f),
160  ( typename internal::cleanup_index_type<LastType>::type(l)
161  -typename internal::cleanup_index_type<FirstType>::type(f)+CleanedIncrType(incr)) / CleanedIncrType(incr),
162  CleanedIncrType(incr));
163 }
164 
165 
166 #endif // EIGEN_PARSED_BY_DOXYGEN
167 
168 namespace placeholders {
169 
176 template<typename SizeType,typename IncrType>
177 auto lastN(SizeType size, IncrType incr)
178 -> decltype(seqN(Eigen::placeholders::last-(size-fix<1>())*incr, size, incr))
179 {
180  return seqN(Eigen::placeholders::last-(size-fix<1>())*incr, size, incr);
181 }
182 
189 template<typename SizeType>
190 auto lastN(SizeType size)
191 -> decltype(seqN(Eigen::placeholders::last+fix<1>()-size, size))
192 {
193  return seqN(Eigen::placeholders::last+fix<1>()-size, size);
194 }
195 
196 } // namespace placeholders
197 
198 namespace internal {
199 
200 // Convert a symbolic span into a usable one (i.e., remove last/end "keywords")
201 template<typename T>
202 struct make_size_type {
203  typedef std::conditional_t<symbolic::is_symbolic<T>::value, Index, T> type;
204 };
205 
206 template<typename FirstType,typename SizeType,typename IncrType,int XprSize>
207 struct IndexedViewCompatibleType<ArithmeticSequence<FirstType,SizeType,IncrType>, XprSize> {
208  typedef ArithmeticSequence<Index,typename make_size_type<SizeType>::type,IncrType> type;
209 };
210 
211 template<typename FirstType,typename SizeType,typename IncrType>
212 ArithmeticSequence<Index,typename make_size_type<SizeType>::type,IncrType>
216 }
217 
218 template<typename FirstType,typename SizeType,typename IncrType>
219 struct get_compile_time_incr<ArithmeticSequence<FirstType,SizeType,IncrType> > {
220  enum { value = get_fixed_value<IncrType,DynamicIndex>::value };
221 };
222 
223 } // end namespace internal
224 
246 namespace indexing {
247  using Eigen::fix;
248  using Eigen::seq;
249  using Eigen::seqN;
254 }
255 
256 } // end namespace Eigen
257 
258 #endif // EIGEN_ARITHMETIC_SEQUENCE_H
const SizeType & sizeObject() const
ArithmeticSequence(FirstType first, SizeType size, IncrType incr)
Index operator[](Index i) const
ArithmeticSequence(FirstType first, SizeType size)
auto reverse() const -> decltype(Eigen::seqN(m_first+(m_size+fix<-1 >()) *m_incr, m_size,-m_incr))
const FirstType & firstObject() const
const IncrType & incrObject() const
static const Eigen::internal::all_t all
static const last_t last
static const auto lastp1
static const auto fix()
Index eval_expr_given_size(Index x, Index)
EIGEN_CONSTEXPR Index first(const T &x) EIGEN_NOEXCEPT
ArithmeticSequence< Index, typename make_size_type< SizeType >::type, IncrType > makeIndexedViewCompatible(const ArithmeticSequence< FirstType, SizeType, IncrType > &ids, Index size, SpecializedType)
auto lastN(SizeType size, IncrType incr) -> decltype(seqN(Eigen::placeholders::last-(size-fix< 1 >()) *incr, size, incr))
: InteropHeaders
Definition: Core:139
auto seq(FirstType f, LastType l, IncrType incr)
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)
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
SpecializedType
Definition: Constants.h:311