Eigen  5.0.1
 
Loading...
Searching...
No Matches
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// SPDX-License-Identifier: MPL-2.0
10
11#ifndef EIGEN_ARITHMETIC_SEQUENCE_H
12#define EIGEN_ARITHMETIC_SEQUENCE_H
13
14// IWYU pragma: private
15#include "./InternalHeaderCheck.h"
16
17namespace Eigen {
18
19namespace internal {
20
21// Helper to cleanup the type of the increment:
22template <typename T>
23struct cleanup_seq_incr {
24 using type = typename cleanup_index_type<T, DynamicIndex>::type;
25};
26
27} // namespace internal
28
29//--------------------------------------------------------------------------------
30// seq(first,last,incr) and seqN(first,size,incr)
31//--------------------------------------------------------------------------------
32
33template <typename FirstType = Index, typename SizeType = Index, typename IncrType = internal::FixedInt<1> >
35
36template <typename FirstType, typename SizeType, typename IncrType>
38 typename internal::cleanup_index_type<SizeType>::type,
39 typename internal::cleanup_seq_incr<IncrType>::type>
40seqN(FirstType first, SizeType size, IncrType incr);
41
62template <typename FirstType, typename SizeType, typename IncrType>
63class ArithmeticSequence {
64 public:
65 constexpr ArithmeticSequence() = default;
66 constexpr ArithmeticSequence(FirstType first, SizeType size) : m_first(first), m_size(size) {}
67 constexpr ArithmeticSequence(FirstType first, SizeType size, IncrType incr)
68 : m_first(first), m_size(size), m_incr(incr) {}
69
70 enum {
71 // SizeAtCompileTime = internal::get_fixed_value<SizeType>::value,
72 IncrAtCompileTime = internal::get_fixed_value<IncrType, DynamicIndex>::value
73 };
74
76 constexpr Index size() const { return m_size; }
77
79 constexpr Index first() const { return m_first; }
80
82 constexpr Index operator[](Index i) const { return m_first + i * m_incr; }
83
84 constexpr const FirstType& firstObject() const { return m_first; }
85 constexpr const SizeType& sizeObject() const { return m_size; }
86 constexpr const IncrType& incrObject() const { return m_incr; }
87
88 protected:
89 FirstType m_first;
90 SizeType m_size;
91 IncrType m_incr;
92
93 public:
94 constexpr auto reverse() const { return seqN(m_first + (m_size + fix<-1>()) * m_incr, m_size, -m_incr); }
95};
96
100template <typename FirstType, typename SizeType, typename IncrType>
102 typename internal::cleanup_index_type<SizeType>::type,
103 typename internal::cleanup_seq_incr<IncrType>::type>
104seqN(FirstType first, SizeType size, IncrType incr) {
106 typename internal::cleanup_index_type<SizeType>::type,
107 typename internal::cleanup_seq_incr<IncrType>::type>(first, size, incr);
108}
109
113template <typename FirstType, typename SizeType>
115 typename internal::cleanup_index_type<SizeType>::type>
116seqN(FirstType first, SizeType size) {
118 typename internal::cleanup_index_type<SizeType>::type>(first, size);
119}
120
121#ifdef EIGEN_PARSED_BY_DOXYGEN
122
133template <typename FirstType, typename LastType, typename IncrType>
134auto seq(FirstType f, LastType l, IncrType incr);
135
145template <typename FirstType, typename LastType>
146auto seq(FirstType f, LastType l);
147
148#else // EIGEN_PARSED_BY_DOXYGEN
149
150template <typename FirstType, typename LastType>
151auto seq(FirstType f, LastType l) {
152 return 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) + fix<1>()));
155}
156
157template <typename FirstType, typename LastType, typename IncrType>
158auto seq(FirstType f, LastType l, IncrType incr) {
159 using CleanedIncrType = typename internal::cleanup_seq_incr<IncrType>::type;
160 return seqN(typename internal::cleanup_index_type<FirstType>::type(f),
161 (typename internal::cleanup_index_type<LastType>::type(l) -
162 typename internal::cleanup_index_type<FirstType>::type(f) + CleanedIncrType(incr)) /
163 CleanedIncrType(incr),
164 CleanedIncrType(incr));
165}
166
167#endif // EIGEN_PARSED_BY_DOXYGEN
168
169namespace placeholders {
170
176template <typename SizeType, typename IncrType>
177auto lastN(SizeType size, IncrType incr) {
178 return seqN(Eigen::placeholders::last - (size - fix<1>()) * incr, size, incr);
179}
180
186template <typename SizeType>
187auto lastN(SizeType size) {
188 return seqN(Eigen::placeholders::last + fix<1>() - size, size);
189}
190
191} // namespace placeholders
192
214namespace indexing {
215using Eigen::fix;
216using Eigen::seq;
217using Eigen::seqN;
220using Eigen::placeholders::lastN;
222} // namespace indexing
223
224} // end namespace Eigen
225
226#endif // EIGEN_ARITHMETIC_SEQUENCE_H
Definition ArithmeticSequence.h:63
constexpr Index size() const
Definition ArithmeticSequence.h:76
constexpr Index operator[](Index i) const
Definition ArithmeticSequence.h:82
constexpr Index first() const
Definition ArithmeticSequence.h:79
static constexpr const last_t last
Definition IndexedViewHelper.h:49
static constexpr Eigen::internal::all_t all
Definition IndexedViewHelper.h:86
static const auto fix()
static constexpr auto lastp1
Definition IndexedViewHelper.h:68
auto seq(FirstType f, LastType l, IncrType incr)
static const auto fix()
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:104
auto lastN(SizeType size, IncrType incr)
Definition ArithmeticSequence.h:177