Eigen  5.0.1-dev+master
 
Loading...
Searching...
No Matches
Reverse.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5// Copyright (C) 2009 Ricard Marxer <email@ricardmarxer.com>
6// Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
7//
8// This Source Code Form is subject to the terms of the Mozilla
9// Public License v. 2.0. If a copy of the MPL was not distributed
10// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11// SPDX-License-Identifier: MPL-2.0
12
13#ifndef EIGEN_REVERSE_H
14#define EIGEN_REVERSE_H
15
16// IWYU pragma: private
17#include "./InternalHeaderCheck.h"
18
19namespace Eigen {
20
21namespace internal {
22
23template <typename MatrixType, int Direction>
24struct traits<Reverse<MatrixType, Direction> > : traits<MatrixType> {
25 using Scalar = typename MatrixType::Scalar;
26 using StorageKind = typename traits<MatrixType>::StorageKind;
27 using XprKind = typename traits<MatrixType>::XprKind;
28 using MatrixTypeNested = typename ref_selector<MatrixType>::type;
29 using MatrixTypeNested_ = std::remove_reference_t<MatrixTypeNested>;
30 enum {
31 RowsAtCompileTime = MatrixType::RowsAtCompileTime,
32 ColsAtCompileTime = MatrixType::ColsAtCompileTime,
33 MaxRowsAtCompileTime = MatrixType::MaxRowsAtCompileTime,
34 MaxColsAtCompileTime = MatrixType::MaxColsAtCompileTime,
35 Flags = MatrixTypeNested_::Flags & (RowMajorBit | LvalueBit)
36 };
37};
38
39template <typename PacketType, bool ReversePacket>
40struct reverse_packet_cond {
41 static inline PacketType run(const PacketType& x) { return preverse(x); }
42};
43
44template <typename PacketType>
45struct reverse_packet_cond<PacketType, false> {
46 static inline PacketType run(const PacketType& x) { return x; }
47};
48
49} // end namespace internal
50
65template <typename MatrixType, int Direction>
66class Reverse : public internal::dense_xpr_base<Reverse<MatrixType, Direction> >::type {
67 public:
68 using Base = typename internal::dense_xpr_base<Reverse>::type;
69 EIGEN_DENSE_PUBLIC_INTERFACE(Reverse)
70 using NestedExpression = internal::remove_all_t<MatrixType>;
71 using Base::IsRowMajor;
72
73 protected:
74 enum {
75 PacketSize = internal::packet_traits<Scalar>::size,
76 IsColMajor = !IsRowMajor,
77 ReverseRow = (Direction == Vertical) || (Direction == BothDirections),
78 ReverseCol = (Direction == Horizontal) || (Direction == BothDirections),
79 OffsetRow = ReverseRow && IsColMajor ? PacketSize : 1,
80 OffsetCol = ReverseCol && IsRowMajor ? PacketSize : 1,
81 ReversePacket = (Direction == BothDirections) || ((Direction == Vertical) && IsColMajor) ||
82 ((Direction == Horizontal) && IsRowMajor)
83 };
84 using reverse_packet = internal::reverse_packet_cond<PacketScalar, ReversePacket>;
85
86 public:
87 EIGEN_DEVICE_FUNC constexpr explicit inline Reverse(const MatrixType& matrix) : m_matrix(matrix) {}
88
89 EIGEN_INHERIT_ASSIGNMENT_OPERATORS(Reverse)
90
91 EIGEN_DEVICE_FUNC constexpr Index rows() const noexcept { return m_matrix.rows(); }
92 EIGEN_DEVICE_FUNC constexpr Index cols() const noexcept { return m_matrix.cols(); }
93
94 EIGEN_DEVICE_FUNC inline Index innerStride() const { return -m_matrix.innerStride(); }
95
96 EIGEN_DEVICE_FUNC constexpr const internal::remove_all_t<typename MatrixType::Nested>& nestedExpression() const {
97 return m_matrix;
98 }
99
100 protected:
101 typename MatrixType::Nested m_matrix;
102};
103
110template <typename Derived>
111EIGEN_DEVICE_FUNC inline typename DenseBase<Derived>::ReverseReturnType DenseBase<Derived>::reverse() {
112 return ReverseReturnType(derived());
113}
114
115// reverse const overload moved to DenseBase.h due to a CUDA compiler bug
116
129template <typename Derived>
130EIGEN_DEVICE_FUNC inline void DenseBase<Derived>::reverseInPlace() {
131 constexpr int HalfRowsAtCompileTime = RowsAtCompileTime == Dynamic ? Dynamic : RowsAtCompileTime / 2;
132 constexpr int HalfColsAtCompileTime = ColsAtCompileTime == Dynamic ? Dynamic : ColsAtCompileTime / 2;
133 // The middle column or row is a block rather than col()/row(): both branches compile for every shape, and with a
134 // zero-sized fixed dimension col()/row() is not a vector, so head()/tail() would not compile.
135 if (cols() > rows()) {
136 Index half = cols() / 2;
137 this->template leftCols<HalfColsAtCompileTime>(half).swap(
138 this->template rightCols<HalfColsAtCompileTime>(half).reverse());
139 if ((cols() % 2) == 1) {
140 Index half2 = rows() / 2;
141 this->template block<HalfRowsAtCompileTime, 1>(0, half, half2, 1)
142 .swap(this->template block<HalfRowsAtCompileTime, 1>(rows() - half2, half, half2, 1).reverse());
143 }
144 } else {
145 Index half = rows() / 2;
146 this->template topRows<HalfRowsAtCompileTime>(half).swap(
147 this->template bottomRows<HalfRowsAtCompileTime>(half).reverse());
148 if ((rows() % 2) == 1) {
149 Index half2 = cols() / 2;
150 this->template block<1, HalfColsAtCompileTime>(half, 0, 1, half2)
151 .swap(this->template block<1, HalfColsAtCompileTime>(half, cols() - half2, 1, half2).reverse());
152 }
153 }
154}
155
156namespace internal {
157
158template <int Direction>
159struct vectorwise_reverse_inplace_impl;
160
161template <>
162struct vectorwise_reverse_inplace_impl<Vertical> {
163 template <typename ExpressionType>
164 static void run(ExpressionType& xpr) {
165 constexpr Index HalfAtCompileTime =
166 ExpressionType::RowsAtCompileTime == Dynamic ? Dynamic : ExpressionType::RowsAtCompileTime / 2;
167 Index half = xpr.rows() / 2;
168 xpr.template topRows<HalfAtCompileTime>(half).swap(
169 xpr.template bottomRows<HalfAtCompileTime>(half).colwise().reverse());
170 }
171};
172
173template <>
174struct vectorwise_reverse_inplace_impl<Horizontal> {
175 template <typename ExpressionType>
176 static void run(ExpressionType& xpr) {
177 constexpr Index HalfAtCompileTime =
178 ExpressionType::ColsAtCompileTime == Dynamic ? Dynamic : ExpressionType::ColsAtCompileTime / 2;
179 Index half = xpr.cols() / 2;
180 xpr.template leftCols<HalfAtCompileTime>(half).swap(
181 xpr.template rightCols<HalfAtCompileTime>(half).rowwise().reverse());
182 }
183};
184
185} // end namespace internal
186
198template <typename ExpressionType, int Direction>
200 internal::vectorwise_reverse_inplace_impl<Direction>::run(m_matrix);
201}
202
203} // end namespace Eigen
204
205#endif // EIGEN_REVERSE_H
ReverseReturnType reverse()
Definition Reverse.h:111
constexpr NRowsBlockXpr<... >::Type bottomRows(NRowsType n)
Definition DenseBase.h:671
constexpr NColsBlockXpr<... >::Type rightCols(NColsType n)
Definition DenseBase.h:876
constexpr NColsBlockXpr<... >::Type leftCols(NColsType n)
Definition DenseBase.h:809
constexpr FixedBlockXpr<...,... >::Type block(Index startRow, Index startCol, NRowsType blockRows, NColsType blockCols)
Definition DenseBase.h:123
@ ColsAtCompileTime
Definition DenseBase.h:103
@ RowsAtCompileTime
Definition DenseBase.h:97
void reverseInPlace()
Definition Reverse.h:130
constexpr NRowsBlockXpr<... >::Type topRows(NRowsType n)
Definition DenseBase.h:604
void reverseInPlace()
Definition Reverse.h:199
@ BothDirections
Definition Constants.h:273
@ Horizontal
Definition Constants.h:270
@ Vertical
Definition Constants.h:267
constexpr unsigned int LvalueBit
Definition Constants.h:149
constexpr unsigned int RowMajorBit
Definition Constants.h:71