10#ifndef EIGEN_INDEXED_VIEW_HELPER_H
11#define EIGEN_INDEXED_VIEW_HELPER_H
14#include "../InternalHeaderCheck.h"
19struct symbolic_last_tag {};
25namespace placeholders {
27typedef symbolic::SymbolExpr<internal::symbolic_last_tag> last_t;
50typedef symbolic::AddExpr<symbolic::SymbolExpr<internal::symbolic_last_tag>,
51 symbolic::ValueExpr<Eigen::internal::FixedInt<1>>>
53typedef Eigen::internal::all_t all_t;
67#ifdef EIGEN_PARSED_BY_DOXYGEN
68static constexpr auto lastp1 = last + fix<1>;
72static constexpr lastp1_t
lastp1(last + fix<1>());
86static constexpr Eigen::internal::all_t
all;
94template <
typename Expr,
int SizeAtCompileTime,
typename EnableIf =
void>
95struct SymbolicExpressionEvaluator {
97 static Index eval(
const Expr& expr,
Index ) {
return static_cast<Index>(expr); }
101template <
typename Expr,
int SizeAtCompileTime>
102struct SymbolicExpressionEvaluator<Expr, SizeAtCompileTime, std::enable_if_t<symbolic::is_symbolic<Expr>::value>> {
103 static constexpr Index ValueAtCompileTime =
105 static Index eval(
const Expr& expr, Index ) {
111template <
typename Expr>
112struct SymbolicExpressionEvaluator<Expr,
Dynamic, std::enable_if_t<symbolic::is_symbolic<Expr>::value>> {
113 static constexpr Index ValueAtCompileTime =
Undefined;
118template <
int N,
int SizeAtCompileTime>
119struct SymbolicExpressionEvaluator<FixedInt<N>, SizeAtCompileTime, void> {
120 static constexpr Index ValueAtCompileTime =
static_cast<Index
>(N);
121 static Index eval(
const FixedInt<N>& , Index ) {
return ValueAtCompileTime; }
129template <
typename Indices,
int NestedSizeAtCompileTime,
typename EnableIf =
void>
130struct IndexedViewHelperIndicesWrapper {
131 using type = Indices;
132 static const type& CreateIndexSequence(
const Indices& indices, Index ) {
return indices; }
136template <
typename Indices,
typename EnableIf =
void>
137struct IndexedViewHelper {
138 static constexpr Index FirstAtCompileTime =
Undefined;
139 static constexpr Index SizeAtCompileTime = array_size<Indices>::value;
140 static constexpr Index IncrAtCompileTime =
Undefined;
142 static constexpr Index first(
const Indices& indices) {
return static_cast<Index
>(indices[0]); }
143 static constexpr Index size(
const Indices& indices) {
return index_list_size(indices); }
144 static constexpr Index incr(
const Indices& ) {
return Undefined; }
151template <Index FirstAtCompileTime_, Index SizeAtCompileTime_, Index IncrAtCompileTime_>
152class ArithmeticSequenceRange {
154 static constexpr Index FirstAtCompileTime = FirstAtCompileTime_;
155 static constexpr Index SizeAtCompileTime = SizeAtCompileTime_;
156 static constexpr Index IncrAtCompileTime = IncrAtCompileTime_;
158 constexpr ArithmeticSequenceRange(Index first, Index size, Index incr) : first_{first}, size_{size}, incr_{incr} {}
159 constexpr Index operator[](Index i)
const {
return first() + i * incr(); }
160 constexpr Index first() const noexcept {
return first_.value(); }
161 constexpr Index size() const noexcept {
return size_.value(); }
162 constexpr Index incr() const noexcept {
return incr_.value(); }
165 variable_if_dynamicindex<
Index, int(FirstAtCompileTime)> first_;
166 variable_if_dynamic<
Index, int(SizeAtCompileTime)> size_;
167 variable_if_dynamicindex<
Index, int(IncrAtCompileTime)> incr_;
170template <
typename FirstType,
typename SizeType,
typename IncrType,
int NestedSizeAtCompileTime>
171struct IndexedViewHelperIndicesWrapper<ArithmeticSequence<FirstType, SizeType, IncrType>, NestedSizeAtCompileTime,
173 static constexpr Index EvalFirstAtCompileTime =
174 SymbolicExpressionEvaluator<FirstType, NestedSizeAtCompileTime>::ValueAtCompileTime;
175 static constexpr Index EvalSizeAtCompileTime =
176 SymbolicExpressionEvaluator<SizeType, NestedSizeAtCompileTime>::ValueAtCompileTime;
177 static constexpr Index EvalIncrAtCompileTime =
178 SymbolicExpressionEvaluator<IncrType, NestedSizeAtCompileTime>::ValueAtCompileTime;
180 static constexpr Index FirstAtCompileTime =
182 static constexpr Index SizeAtCompileTime =
184 static constexpr Index IncrAtCompileTime =
187 using Indices = ArithmeticSequence<FirstType, SizeType, IncrType>;
188 using type = ArithmeticSequenceRange<FirstAtCompileTime, SizeAtCompileTime, IncrAtCompileTime>;
190 static type CreateIndexSequence(
const Indices& indices, Index nested_size) {
192 SymbolicExpressionEvaluator<FirstType, NestedSizeAtCompileTime>::eval(indices.firstObject(), nested_size);
194 SymbolicExpressionEvaluator<SizeType, NestedSizeAtCompileTime>::eval(indices.sizeObject(), nested_size);
196 SymbolicExpressionEvaluator<IncrType, NestedSizeAtCompileTime>::eval(indices.incrObject(), nested_size);
197 return type(first, size, incr);
201template <Index FirstAtCompileTime_, Index SizeAtCompileTime_, Index IncrAtCompileTime_>
202struct IndexedViewHelper<ArithmeticSequenceRange<FirstAtCompileTime_, SizeAtCompileTime_, IncrAtCompileTime_>, void> {
204 using Indices = ArithmeticSequenceRange<FirstAtCompileTime_, SizeAtCompileTime_, IncrAtCompileTime_>;
205 static constexpr Index FirstAtCompileTime = Indices::FirstAtCompileTime;
206 static constexpr Index SizeAtCompileTime = Indices::SizeAtCompileTime;
207 static constexpr Index IncrAtCompileTime = Indices::IncrAtCompileTime;
208 static Index first(
const Indices& indices) {
return indices.first(); }
209 static Index size(
const Indices& indices) {
return indices.size(); }
210 static Index incr(
const Indices& indices) {
return indices.incr(); }
217template <Index ValueAtCompileTime>
220 static constexpr Index FirstAtCompileTime = ValueAtCompileTime;
221 static constexpr Index SizeAtCompileTime =
Index(1);
222 static constexpr Index IncrAtCompileTime =
Index(1);
224 constexpr SingleRange(Index v) noexcept : value_(v) {}
225 constexpr Index operator[](Index)
const noexcept {
return first(); }
226 constexpr Index first() const noexcept {
return value_.value(); }
227 constexpr Index size() const noexcept {
return SizeAtCompileTime; }
228 constexpr Index incr() const noexcept {
return IncrAtCompileTime; }
231 variable_if_dynamicindex<
Index, int(ValueAtCompileTime)> value_;
235struct is_single_range :
public std::false_type {};
237template <Index ValueAtCompileTime>
238struct is_single_range<SingleRange<ValueAtCompileTime>> :
public std::true_type {};
240template <
typename SingleIndex,
int NestedSizeAtCompileTime>
241struct IndexedViewHelperIndicesWrapper<
242 SingleIndex, NestedSizeAtCompileTime,
243 std::enable_if_t<std::is_integral<SingleIndex>::value || symbolic::is_symbolic<SingleIndex>::value>> {
244 static constexpr Index EvalValueAtCompileTime =
245 SymbolicExpressionEvaluator<SingleIndex, NestedSizeAtCompileTime>::ValueAtCompileTime;
246 static constexpr Index ValueAtCompileTime =
248 using type = SingleRange<ValueAtCompileTime>;
249 static type CreateIndexSequence(
const SingleIndex& index, Index nested_size) {
250 return type(SymbolicExpressionEvaluator<SingleIndex, NestedSizeAtCompileTime>::eval(index, nested_size));
254template <
int N,
int NestedSizeAtCompileTime>
255struct IndexedViewHelperIndicesWrapper<FixedInt<N>, NestedSizeAtCompileTime, void> {
256 using type = SingleRange<
Index(N)>;
257 static type CreateIndexSequence(
const FixedInt<N>& ) {
return type(
Index(N)); }
260template <Index ValueAtCompileTime>
261struct IndexedViewHelper<SingleRange<ValueAtCompileTime>, void> {
262 using Indices = SingleRange<ValueAtCompileTime>;
263 static constexpr Index FirstAtCompileTime = Indices::FirstAtCompileTime;
264 static constexpr Index SizeAtCompileTime = Indices::SizeAtCompileTime;
265 static constexpr Index IncrAtCompileTime = Indices::IncrAtCompileTime;
267 static constexpr Index first(
const Indices& indices) {
return indices.first(); }
268 static constexpr Index size(
const Indices& ) {
return SizeAtCompileTime; }
269 static constexpr Index incr(
const Indices& ) {
return IncrAtCompileTime; }
277template <Index SizeAtCompileTime_>
280 static constexpr Index FirstAtCompileTime =
Index(0);
281 static constexpr Index SizeAtCompileTime = SizeAtCompileTime_;
282 static constexpr Index IncrAtCompileTime =
Index(1);
283 constexpr AllRange(Index size) : size_(size) {}
284 constexpr Index operator[](Index i)
const noexcept {
return i; }
285 constexpr Index first() const noexcept {
return FirstAtCompileTime; }
286 constexpr Index size() const noexcept {
return size_.value(); }
287 constexpr Index incr() const noexcept {
return IncrAtCompileTime; }
290 variable_if_dynamic<
Index, int(SizeAtCompileTime)> size_;
293template <
int NestedSizeAtCompileTime>
294struct IndexedViewHelperIndicesWrapper<all_t, NestedSizeAtCompileTime, void> {
295 using type = AllRange<
Index(NestedSizeAtCompileTime)>;
296 static type CreateIndexSequence(
const all_t& , Index nested_size) {
return type(nested_size); }
299template <Index SizeAtCompileTime_>
300struct IndexedViewHelper<AllRange<SizeAtCompileTime_>, void> {
301 using Indices = AllRange<SizeAtCompileTime_>;
302 static constexpr Index FirstAtCompileTime = Indices::FirstAtCompileTime;
303 static constexpr Index SizeAtCompileTime = Indices::SizeAtCompileTime;
304 static constexpr Index IncrAtCompileTime = Indices::IncrAtCompileTime;
306 static Index first(
const Indices& indices) {
return indices.first(); }
307 static Index size(
const Indices& indices) {
return indices.size(); }
308 static Index incr(
const Indices& indices) {
return indices.incr(); }
312template <
typename Derived,
typename RowIndices,
typename ColIndices,
typename EnableIf =
void>
313struct IndexedViewSelector;
315template <
typename Indices,
int SizeAtCompileTime>
316using IvcType =
typename internal::IndexedViewHelperIndicesWrapper<Indices, SizeAtCompileTime>::type;
318template <
int SizeAtCompileTime,
typename Indices>
319inline IvcType<Indices, SizeAtCompileTime> CreateIndexSequence(
size_t size,
const Indices& indices) {
320 return internal::IndexedViewHelperIndicesWrapper<Indices, SizeAtCompileTime>::CreateIndexSequence(indices, size);
324template <
typename Derived,
typename RowIndices,
typename ColIndices>
325struct IndexedViewSelector<Derived, RowIndices, ColIndices,
326 std::enable_if_t<internal::traits<
327 IndexedView<Derived, IvcType<RowIndices, Derived::RowsAtCompileTime>,
328 IvcType<ColIndices, Derived::ColsAtCompileTime>>>::ReturnAsIndexedView>> {
329 using ReturnType = IndexedView<Derived, IvcType<RowIndices, Derived::RowsAtCompileTime>,
330 IvcType<ColIndices, Derived::ColsAtCompileTime>>;
331 using ConstReturnType = IndexedView<const Derived, IvcType<RowIndices, Derived::RowsAtCompileTime>,
332 IvcType<ColIndices, Derived::ColsAtCompileTime>>;
334 static inline ReturnType run(Derived& derived,
const RowIndices& rowIndices,
const ColIndices& colIndices) {
335 return ReturnType(derived, CreateIndexSequence<Derived::RowsAtCompileTime>(derived.rows(), rowIndices),
336 CreateIndexSequence<Derived::ColsAtCompileTime>(derived.cols(), colIndices));
338 static inline ConstReturnType run(
const Derived& derived,
const RowIndices& rowIndices,
339 const ColIndices& colIndices) {
340 return ConstReturnType(derived, CreateIndexSequence<Derived::RowsAtCompileTime>(derived.rows(), rowIndices),
341 CreateIndexSequence<Derived::ColsAtCompileTime>(derived.cols(), colIndices));
346template <
typename Derived,
typename RowIndices,
typename ColIndices>
347struct IndexedViewSelector<
348 Derived, RowIndices, ColIndices,
349 std::enable_if_t<internal::traits<IndexedView<Derived, IvcType<RowIndices, Derived::RowsAtCompileTime>,
350 IvcType<ColIndices, Derived::ColsAtCompileTime>>>::ReturnAsBlock>> {
351 using ActualRowIndices = IvcType<RowIndices, Derived::RowsAtCompileTime>;
352 using ActualColIndices = IvcType<ColIndices, Derived::ColsAtCompileTime>;
353 using IndexedViewType = IndexedView<Derived, ActualRowIndices, ActualColIndices>;
354 using ConstIndexedViewType = IndexedView<const Derived, ActualRowIndices, ActualColIndices>;
355 using ReturnType =
typename internal::traits<IndexedViewType>::BlockType;
356 using ConstReturnType =
typename internal::traits<ConstIndexedViewType>::BlockType;
357 using RowHelper = internal::IndexedViewHelper<ActualRowIndices>;
358 using ColHelper = internal::IndexedViewHelper<ActualColIndices>;
360 static inline ReturnType run(Derived& derived,
const RowIndices& rowIndices,
const ColIndices& colIndices) {
361 auto actualRowIndices = CreateIndexSequence<Derived::RowsAtCompileTime>(derived.rows(), rowIndices);
362 auto actualColIndices = CreateIndexSequence<Derived::ColsAtCompileTime>(derived.cols(), colIndices);
363 return ReturnType(derived, RowHelper::first(actualRowIndices), ColHelper::first(actualColIndices),
364 RowHelper::size(actualRowIndices), ColHelper::size(actualColIndices));
366 static inline ConstReturnType run(
const Derived& derived,
const RowIndices& rowIndices,
367 const ColIndices& colIndices) {
368 auto actualRowIndices = CreateIndexSequence<Derived::RowsAtCompileTime>(derived.rows(), rowIndices);
369 auto actualColIndices = CreateIndexSequence<Derived::ColsAtCompileTime>(derived.cols(), colIndices);
370 return ConstReturnType(derived, RowHelper::first(actualRowIndices), ColHelper::first(actualColIndices),
371 RowHelper::size(actualRowIndices), ColHelper::size(actualColIndices));
376template <
typename Derived,
typename RowIndices,
typename ColIndices>
377struct IndexedViewSelector<
378 Derived, RowIndices, ColIndices,
379 std::enable_if_t<internal::traits<IndexedView<Derived, IvcType<RowIndices, Derived::RowsAtCompileTime>,
380 IvcType<ColIndices, Derived::ColsAtCompileTime>>>::ReturnAsScalar>> {
382 using ConstReturnType =
typename DenseBase<Derived>::CoeffReturnType;
383 using ActualRowIndices = IvcType<RowIndices, Derived::RowsAtCompileTime>;
384 using ActualColIndices = IvcType<ColIndices, Derived::ColsAtCompileTime>;
385 using RowHelper = internal::IndexedViewHelper<ActualRowIndices>;
386 using ColHelper = internal::IndexedViewHelper<ActualColIndices>;
387 static inline ReturnType run(Derived& derived,
const RowIndices& rowIndices,
const ColIndices& colIndices) {
388 auto actualRowIndices = CreateIndexSequence<Derived::RowsAtCompileTime>(derived.rows(), rowIndices);
389 auto actualColIndices = CreateIndexSequence<Derived::ColsAtCompileTime>(derived.cols(), colIndices);
390 return derived(RowHelper::first(actualRowIndices), ColHelper::first(actualColIndices));
392 static inline ConstReturnType run(
const Derived& derived,
const RowIndices& rowIndices,
393 const ColIndices& colIndices) {
394 auto actualRowIndices = CreateIndexSequence<Derived::RowsAtCompileTime>(derived.rows(), rowIndices);
395 auto actualColIndices = CreateIndexSequence<Derived::ColsAtCompileTime>(derived.cols(), colIndices);
396 return derived(RowHelper::first(actualRowIndices), ColHelper::first(actualColIndices));
401template <
typename Derived,
typename Indices,
typename EnableIf =
void>
402struct VectorIndexedViewSelector;
405template <
typename Derived,
typename Indices>
406struct VectorIndexedViewSelector<
408 std::enable_if_t<!internal::is_single_range<IvcType<Indices, Derived::SizeAtCompileTime>>::value &&
409 internal::IndexedViewHelper<IvcType<Indices, Derived::SizeAtCompileTime>>::IncrAtCompileTime !=
412 using ZeroIndex = internal::SingleRange<
Index(0)>;
413 using RowMajorReturnType = IndexedView<Derived, ZeroIndex, IvcType<Indices, Derived::SizeAtCompileTime>>;
414 using ConstRowMajorReturnType = IndexedView<const Derived, ZeroIndex, IvcType<Indices, Derived::SizeAtCompileTime>>;
416 using ColMajorReturnType = IndexedView<Derived, IvcType<Indices, Derived::SizeAtCompileTime>, ZeroIndex>;
417 using ConstColMajorReturnType = IndexedView<const Derived, IvcType<Indices, Derived::SizeAtCompileTime>, ZeroIndex>;
419 using ReturnType =
typename internal::conditional<IsRowMajor, RowMajorReturnType, ColMajorReturnType>::type;
420 using ConstReturnType =
421 typename internal::conditional<IsRowMajor, ConstRowMajorReturnType, ConstColMajorReturnType>::type;
423 template <
bool UseRowMajor = IsRowMajor, std::enable_if_t<UseRowMajor,
bool> = true>
424 static inline RowMajorReturnType run(Derived& derived,
const Indices& indices) {
425 return RowMajorReturnType(derived, ZeroIndex(0),
426 CreateIndexSequence<Derived::ColsAtCompileTime>(derived.cols(), indices));
428 template <
bool UseRowMajor = IsRowMajor, std::enable_if_t<UseRowMajor,
bool> = true>
429 static inline ConstRowMajorReturnType run(
const Derived& derived,
const Indices& indices) {
430 return ConstRowMajorReturnType(derived, ZeroIndex(0),
431 CreateIndexSequence<Derived::ColsAtCompileTime>(derived.cols(), indices));
433 template <
bool UseRowMajor = IsRowMajor, std::enable_if_t<!UseRowMajor,
bool> = true>
434 static inline ColMajorReturnType run(Derived& derived,
const Indices& indices) {
435 return ColMajorReturnType(derived, CreateIndexSequence<Derived::RowsAtCompileTime>(derived.rows(), indices),
438 template <
bool UseRowMajor = IsRowMajor, std::enable_if_t<!UseRowMajor,
bool> = true>
439 static inline ConstColMajorReturnType run(
const Derived& derived,
const Indices& indices) {
440 return ConstColMajorReturnType(derived, CreateIndexSequence<Derived::RowsAtCompileTime>(derived.rows(), indices),
446template <
typename Derived,
typename Indices>
447struct VectorIndexedViewSelector<
449 std::enable_if_t<!internal::is_single_range<IvcType<Indices, Derived::SizeAtCompileTime>>::value &&
450 internal::IndexedViewHelper<IvcType<Indices, Derived::SizeAtCompileTime>>::IncrAtCompileTime ==
452 using Helper = internal::IndexedViewHelper<IvcType<Indices, Derived::SizeAtCompileTime>>;
453 using ReturnType = VectorBlock<Derived, Helper::SizeAtCompileTime>;
454 using ConstReturnType = VectorBlock<const Derived, Helper::SizeAtCompileTime>;
455 static inline ReturnType run(Derived& derived,
const Indices& indices) {
456 auto actualIndices = CreateIndexSequence<Derived::SizeAtCompileTime>(derived.size(), indices);
457 return ReturnType(derived, Helper::first(actualIndices), Helper::size(actualIndices));
459 static inline ConstReturnType run(
const Derived& derived,
const Indices& indices) {
460 auto actualIndices = CreateIndexSequence<Derived::SizeAtCompileTime>(derived.size(), indices);
461 return ConstReturnType(derived, Helper::first(actualIndices), Helper::size(actualIndices));
466template <
typename Derived,
typename Indices>
467struct VectorIndexedViewSelector<
469 std::enable_if_t<internal::is_single_range<IvcType<Indices, Derived::SizeAtCompileTime>>::value>> {
471 using ConstReturnType =
typename DenseBase<Derived>::CoeffReturnType;
472 using Helper = internal::IndexedViewHelper<IvcType<Indices, Derived::SizeAtCompileTime>>;
473 static inline ReturnType run(Derived& derived,
const Indices& indices) {
474 auto actualIndices = CreateIndexSequence<Derived::SizeAtCompileTime>(derived.size(), indices);
475 return derived(Helper::first(actualIndices));
477 static inline ConstReturnType run(
const Derived& derived,
const Indices& indices) {
478 auto actualIndices = CreateIndexSequence<Derived::SizeAtCompileTime>(derived.size(), indices);
479 return derived(Helper::first(actualIndices));
internal::traits< Derived >::Scalar Scalar
Definition DenseBase.h:62
@ IsRowMajor
Definition DenseBase.h:166
Definition SymbolicIndex.h:320
static constexpr const last_t last
Definition IndexedViewHelper.h:48
static constexpr Eigen::internal::all_t all
Definition IndexedViewHelper.h:86
static constexpr auto lastp1
Definition IndexedViewHelper.h:68
static constexpr lastp1_t end
Definition IndexedViewHelper.h:79
Namespace containing all symbols from the Eigen library.
Definition Core:137
const int Undefined
Definition Constants.h:34
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:83
const int DynamicIndex
Definition Constants.h:30
const int Dynamic
Definition Constants.h:25