10#ifndef EIGEN_STLITERATORS_H
11#define EIGEN_STLITERATORS_H
14#include "./InternalHeaderCheck.h"
20template <
typename IteratorType>
21struct indexed_based_stl_iterator_traits;
23template <
typename Derived>
24class indexed_based_stl_iterator_base {
26 typedef indexed_based_stl_iterator_traits<Derived> traits;
27 typedef typename traits::XprType XprType;
28 typedef indexed_based_stl_iterator_base<typename traits::non_const_iterator> non_const_iterator;
29 typedef indexed_based_stl_iterator_base<typename traits::const_iterator> const_iterator;
30 typedef std::conditional_t<internal::is_const<XprType>::value, non_const_iterator, const_iterator> other_iterator;
32 friend class indexed_based_stl_iterator_base<typename traits::const_iterator>;
33 friend class indexed_based_stl_iterator_base<typename traits::non_const_iterator>;
36 typedef
Index difference_type;
37 typedef std::random_access_iterator_tag iterator_category;
39 indexed_based_stl_iterator_base() EIGEN_NO_THROW : mp_xpr(0), m_index(0) {}
40 indexed_based_stl_iterator_base(XprType& xpr,
Index index) EIGEN_NO_THROW : mp_xpr(&xpr), m_index(index) {}
42 indexed_based_stl_iterator_base(
const non_const_iterator& other) EIGEN_NO_THROW : mp_xpr(other.mp_xpr),
43 m_index(other.m_index) {}
45 indexed_based_stl_iterator_base& operator=(
const non_const_iterator& other) {
46 mp_xpr = other.mp_xpr;
47 m_index = other.m_index;
51 Derived& operator++() {
55 Derived& operator--() {
60 Derived operator++(
int) {
61 Derived prev(derived());
65 Derived operator--(
int) {
66 Derived prev(derived());
71 friend Derived operator+(
const indexed_based_stl_iterator_base& a,
Index b) {
72 Derived ret(a.derived());
76 friend Derived operator-(
const indexed_based_stl_iterator_base& a,
Index b) {
77 Derived ret(a.derived());
81 friend Derived operator+(
Index a,
const indexed_based_stl_iterator_base& b) {
82 Derived ret(b.derived());
86 friend Derived operator-(
Index a,
const indexed_based_stl_iterator_base& b) {
87 Derived ret(b.derived());
92 Derived& operator+=(
Index b) {
96 Derived& operator-=(
Index b) {
101 difference_type operator-(
const indexed_based_stl_iterator_base& other)
const {
102 eigen_assert(mp_xpr == other.mp_xpr);
103 return m_index - other.m_index;
106 difference_type operator-(
const other_iterator& other)
const {
107 eigen_assert(mp_xpr == other.mp_xpr);
108 return m_index - other.m_index;
111 bool operator==(
const indexed_based_stl_iterator_base& other)
const {
112 eigen_assert(mp_xpr == other.mp_xpr);
113 return m_index == other.m_index;
115 bool operator!=(
const indexed_based_stl_iterator_base& other)
const {
116 eigen_assert(mp_xpr == other.mp_xpr);
117 return m_index != other.m_index;
119 bool operator<(
const indexed_based_stl_iterator_base& other)
const {
120 eigen_assert(mp_xpr == other.mp_xpr);
121 return m_index < other.m_index;
123 bool operator<=(
const indexed_based_stl_iterator_base& other)
const {
124 eigen_assert(mp_xpr == other.mp_xpr);
125 return m_index <= other.m_index;
127 bool operator>(
const indexed_based_stl_iterator_base& other)
const {
128 eigen_assert(mp_xpr == other.mp_xpr);
129 return m_index > other.m_index;
131 bool operator>=(
const indexed_based_stl_iterator_base& other)
const {
132 eigen_assert(mp_xpr == other.mp_xpr);
133 return m_index >= other.m_index;
136 bool operator==(
const other_iterator& other)
const {
137 eigen_assert(mp_xpr == other.mp_xpr);
138 return m_index == other.m_index;
140 bool operator!=(
const other_iterator& other)
const {
141 eigen_assert(mp_xpr == other.mp_xpr);
142 return m_index != other.m_index;
144 bool operator<(
const other_iterator& other)
const {
145 eigen_assert(mp_xpr == other.mp_xpr);
146 return m_index < other.m_index;
148 bool operator<=(
const other_iterator& other)
const {
149 eigen_assert(mp_xpr == other.mp_xpr);
150 return m_index <= other.m_index;
152 bool operator>(
const other_iterator& other)
const {
153 eigen_assert(mp_xpr == other.mp_xpr);
154 return m_index > other.m_index;
156 bool operator>=(
const other_iterator& other)
const {
157 eigen_assert(mp_xpr == other.mp_xpr);
158 return m_index >= other.m_index;
162 Derived& derived() {
return static_cast<Derived&
>(*this); }
163 const Derived& derived()
const {
return static_cast<const Derived&
>(*this); }
169template <
typename Derived>
170class indexed_based_stl_reverse_iterator_base {
172 typedef indexed_based_stl_iterator_traits<Derived> traits;
173 typedef typename traits::XprType XprType;
174 typedef indexed_based_stl_reverse_iterator_base<typename traits::non_const_iterator> non_const_iterator;
175 typedef indexed_based_stl_reverse_iterator_base<typename traits::const_iterator> const_iterator;
176 typedef std::conditional_t<internal::is_const<XprType>::value, non_const_iterator, const_iterator> other_iterator;
178 friend class indexed_based_stl_reverse_iterator_base<typename traits::const_iterator>;
179 friend class indexed_based_stl_reverse_iterator_base<typename traits::non_const_iterator>;
182 typedef
Index difference_type;
183 typedef std::random_access_iterator_tag iterator_category;
185 indexed_based_stl_reverse_iterator_base() : mp_xpr(0), m_index(0) {}
186 indexed_based_stl_reverse_iterator_base(XprType& xpr,
Index index) : mp_xpr(&xpr), m_index(index) {}
188 indexed_based_stl_reverse_iterator_base(
const non_const_iterator& other)
189 : mp_xpr(other.mp_xpr), m_index(other.m_index) {}
191 indexed_based_stl_reverse_iterator_base& operator=(
const non_const_iterator& other) {
192 mp_xpr = other.mp_xpr;
193 m_index = other.m_index;
197 Derived& operator++() {
201 Derived& operator--() {
206 Derived operator++(
int) {
207 Derived prev(derived());
211 Derived operator--(
int) {
212 Derived prev(derived());
217 friend Derived operator+(
const indexed_based_stl_reverse_iterator_base& a,
Index b) {
218 Derived ret(a.derived());
222 friend Derived operator-(
const indexed_based_stl_reverse_iterator_base& a,
Index b) {
223 Derived ret(a.derived());
227 friend Derived operator+(
Index a,
const indexed_based_stl_reverse_iterator_base& b) {
228 Derived ret(b.derived());
232 friend Derived operator-(
Index a,
const indexed_based_stl_reverse_iterator_base& b) {
233 Derived ret(b.derived());
238 Derived& operator+=(
Index b) {
242 Derived& operator-=(
Index b) {
247 difference_type operator-(
const indexed_based_stl_reverse_iterator_base& other)
const {
248 eigen_assert(mp_xpr == other.mp_xpr);
249 return other.m_index - m_index;
252 difference_type operator-(
const other_iterator& other)
const {
253 eigen_assert(mp_xpr == other.mp_xpr);
254 return other.m_index - m_index;
257 bool operator==(
const indexed_based_stl_reverse_iterator_base& other)
const {
258 eigen_assert(mp_xpr == other.mp_xpr);
259 return m_index == other.m_index;
261 bool operator!=(
const indexed_based_stl_reverse_iterator_base& other)
const {
262 eigen_assert(mp_xpr == other.mp_xpr);
263 return m_index != other.m_index;
265 bool operator<(
const indexed_based_stl_reverse_iterator_base& other)
const {
266 eigen_assert(mp_xpr == other.mp_xpr);
267 return m_index > other.m_index;
269 bool operator<=(
const indexed_based_stl_reverse_iterator_base& other)
const {
270 eigen_assert(mp_xpr == other.mp_xpr);
271 return m_index >= other.m_index;
273 bool operator>(
const indexed_based_stl_reverse_iterator_base& other)
const {
274 eigen_assert(mp_xpr == other.mp_xpr);
275 return m_index < other.m_index;
277 bool operator>=(
const indexed_based_stl_reverse_iterator_base& other)
const {
278 eigen_assert(mp_xpr == other.mp_xpr);
279 return m_index <= other.m_index;
282 bool operator==(
const other_iterator& other)
const {
283 eigen_assert(mp_xpr == other.mp_xpr);
284 return m_index == other.m_index;
286 bool operator!=(
const other_iterator& other)
const {
287 eigen_assert(mp_xpr == other.mp_xpr);
288 return m_index != other.m_index;
290 bool operator<(
const other_iterator& other)
const {
291 eigen_assert(mp_xpr == other.mp_xpr);
292 return m_index > other.m_index;
294 bool operator<=(
const other_iterator& other)
const {
295 eigen_assert(mp_xpr == other.mp_xpr);
296 return m_index >= other.m_index;
298 bool operator>(
const other_iterator& other)
const {
299 eigen_assert(mp_xpr == other.mp_xpr);
300 return m_index < other.m_index;
302 bool operator>=(
const other_iterator& other)
const {
303 eigen_assert(mp_xpr == other.mp_xpr);
304 return m_index <= other.m_index;
308 Derived& derived() {
return static_cast<Derived&
>(*this); }
309 const Derived& derived()
const {
return static_cast<const Derived&
>(*this); }
315template <
typename XprType>
316class pointer_based_stl_iterator {
317 enum { is_lvalue = internal::is_lvalue<XprType>::value };
318 typedef pointer_based_stl_iterator<std::remove_const_t<XprType>> non_const_iterator;
319 typedef pointer_based_stl_iterator<std::add_const_t<XprType>> const_iterator;
320 typedef std::conditional_t<internal::is_const<XprType>::value, non_const_iterator, const_iterator> other_iterator;
322 friend class pointer_based_stl_iterator<std::add_const_t<XprType>>;
323 friend class pointer_based_stl_iterator<std::remove_const_t<XprType>>;
326 typedef
Index difference_type;
327 typedef typename XprType::Scalar value_type;
328#if EIGEN_CPLUSPLUS >= 202002L
329 typedef std::conditional_t<XprType::InnerStrideAtCompileTime == 1, std::contiguous_iterator_tag,
330 std::random_access_iterator_tag>
333 typedef std::random_access_iterator_tag iterator_category;
335 typedef std::conditional_t<bool(is_lvalue), value_type*, const value_type*> pointer;
336 typedef std::conditional_t<bool(is_lvalue), value_type&, const value_type&> reference;
338 pointer_based_stl_iterator() EIGEN_NO_THROW : m_ptr(0) {}
339 pointer_based_stl_iterator(XprType& xpr,
Index index) EIGEN_NO_THROW : m_incr(xpr.innerStride()) {
340 m_ptr = xpr.data() + index * m_incr.value();
343 pointer_based_stl_iterator(
const non_const_iterator& other) EIGEN_NO_THROW : m_ptr(other.m_ptr),
344 m_incr(other.m_incr) {}
346 pointer_based_stl_iterator& operator=(
const non_const_iterator& other) EIGEN_NO_THROW {
348 m_incr.setValue(other.m_incr);
352 reference operator*()
const {
return *m_ptr; }
353 reference operator[](
Index i)
const {
return *(m_ptr + i * m_incr.value()); }
354 pointer operator->()
const {
return m_ptr; }
356 pointer_based_stl_iterator& operator++() {
357 m_ptr += m_incr.value();
360 pointer_based_stl_iterator& operator--() {
361 m_ptr -= m_incr.value();
365 pointer_based_stl_iterator operator++(
int) {
366 pointer_based_stl_iterator prev(*
this);
370 pointer_based_stl_iterator operator--(
int) {
371 pointer_based_stl_iterator prev(*
this);
376 friend pointer_based_stl_iterator operator+(
const pointer_based_stl_iterator& a,
Index b) {
377 pointer_based_stl_iterator ret(a);
381 friend pointer_based_stl_iterator operator-(
const pointer_based_stl_iterator& a,
Index b) {
382 pointer_based_stl_iterator ret(a);
386 friend pointer_based_stl_iterator operator+(
Index a,
const pointer_based_stl_iterator& b) {
387 pointer_based_stl_iterator ret(b);
391 friend pointer_based_stl_iterator operator-(
Index a,
const pointer_based_stl_iterator& b) {
392 pointer_based_stl_iterator ret(b);
397 pointer_based_stl_iterator& operator+=(
Index b) {
398 m_ptr += b * m_incr.value();
401 pointer_based_stl_iterator& operator-=(
Index b) {
402 m_ptr -= b * m_incr.value();
406 difference_type operator-(
const pointer_based_stl_iterator& other)
const {
407 return (m_ptr - other.m_ptr) / m_incr.value();
410 difference_type operator-(
const other_iterator& other)
const {
return (m_ptr - other.m_ptr) / m_incr.value(); }
412 bool operator==(
const pointer_based_stl_iterator& other)
const {
return m_ptr == other.m_ptr; }
413 bool operator!=(
const pointer_based_stl_iterator& other)
const {
return m_ptr != other.m_ptr; }
414 bool operator<(
const pointer_based_stl_iterator& other)
const {
return m_ptr < other.m_ptr; }
415 bool operator<=(
const pointer_based_stl_iterator& other)
const {
return m_ptr <= other.m_ptr; }
416 bool operator>(
const pointer_based_stl_iterator& other)
const {
return m_ptr > other.m_ptr; }
417 bool operator>=(
const pointer_based_stl_iterator& other)
const {
return m_ptr >= other.m_ptr; }
419 bool operator==(
const other_iterator& other)
const {
return m_ptr == other.m_ptr; }
420 bool operator!=(
const other_iterator& other)
const {
return m_ptr != other.m_ptr; }
421 bool operator<(
const other_iterator& other)
const {
return m_ptr < other.m_ptr; }
422 bool operator<=(
const other_iterator& other)
const {
return m_ptr <= other.m_ptr; }
423 bool operator>(
const other_iterator& other)
const {
return m_ptr > other.m_ptr; }
424 bool operator>=(
const other_iterator& other)
const {
return m_ptr >= other.m_ptr; }
428 internal::variable_if_dynamic<Index, XprType::InnerStrideAtCompileTime> m_incr;
431template <
typename XprType_>
432struct indexed_based_stl_iterator_traits<generic_randaccess_stl_iterator<XprType_>> {
433 typedef XprType_ XprType;
434 typedef generic_randaccess_stl_iterator<std::remove_const_t<XprType>> non_const_iterator;
435 typedef generic_randaccess_stl_iterator<std::add_const_t<XprType>> const_iterator;
438template <
typename XprType>
439class generic_randaccess_stl_iterator
440 :
public indexed_based_stl_iterator_base<generic_randaccess_stl_iterator<XprType>> {
442 typedef typename XprType::Scalar value_type;
446 has_direct_access = (internal::traits<XprType>::Flags &
DirectAccessBit) ? 1 : 0,
447 is_lvalue = internal::is_lvalue<XprType>::value
450 typedef indexed_based_stl_iterator_base<generic_randaccess_stl_iterator> Base;
457 typedef const value_type read_only_ref_t;
460 typedef std::conditional_t<bool(is_lvalue), value_type*,
const value_type*> pointer;
461 typedef std::conditional_t<bool(is_lvalue), value_type&, read_only_ref_t> reference;
463 generic_randaccess_stl_iterator() : Base() {}
464 generic_randaccess_stl_iterator(XprType& xpr,
Index index) : Base(xpr, index) {}
465 generic_randaccess_stl_iterator(
const typename Base::non_const_iterator& other) : Base(other) {}
466 using Base::operator=;
468 reference operator*()
const {
return (*mp_xpr)(m_index); }
469 reference operator[](
Index i)
const {
return (*mp_xpr)(m_index + i); }
470 pointer operator->()
const {
return &((*mp_xpr)(m_index)); }
473template <
typename XprType_, DirectionType Direction>
474struct indexed_based_stl_iterator_traits<subvector_stl_iterator<XprType_, Direction>> {
475 typedef XprType_ XprType;
476 typedef subvector_stl_iterator<std::remove_const_t<XprType>, Direction> non_const_iterator;
477 typedef subvector_stl_iterator<std::add_const_t<XprType>, Direction> const_iterator;
480template <
typename XprType, DirectionType Direction>
481class subvector_stl_iterator :
public indexed_based_stl_iterator_base<subvector_stl_iterator<XprType, Direction>> {
483 enum { is_lvalue = internal::is_lvalue<XprType>::value };
485 typedef indexed_based_stl_iterator_base<subvector_stl_iterator> Base;
489 typedef std::conditional_t<Direction == Vertical, typename XprType::ColXpr, typename XprType::RowXpr> SubVectorType;
490 typedef std::conditional_t<Direction == Vertical, typename XprType::ConstColXpr, typename XprType::ConstRowXpr>
494 typedef std::conditional_t<bool(is_lvalue), SubVectorType, ConstSubVectorType> reference;
495 typedef typename reference::PlainObject value_type;
498 class subvector_stl_iterator_ptr {
500 subvector_stl_iterator_ptr(
const reference& subvector) : m_subvector(subvector) {}
501 reference* operator->() {
return &m_subvector; }
504 reference m_subvector;
508 typedef subvector_stl_iterator_ptr pointer;
510 subvector_stl_iterator() : Base() {}
511 subvector_stl_iterator(XprType& xpr, Index index) : Base(xpr, index) {}
513 reference operator*()
const {
return (*mp_xpr).template subVector<Direction>(m_index); }
514 reference operator[](Index i)
const {
return (*mp_xpr).template subVector<Direction>(m_index + i); }
515 pointer operator->()
const {
return (*mp_xpr).template subVector<Direction>(m_index); }
518template <
typename XprType_, DirectionType Direction>
519struct indexed_based_stl_iterator_traits<subvector_stl_reverse_iterator<XprType_, Direction>> {
520 typedef XprType_ XprType;
521 typedef subvector_stl_reverse_iterator<std::remove_const_t<XprType>, Direction> non_const_iterator;
522 typedef subvector_stl_reverse_iterator<std::add_const_t<XprType>, Direction> const_iterator;
525template <
typename XprType, DirectionType Direction>
526class subvector_stl_reverse_iterator
527 :
public indexed_based_stl_reverse_iterator_base<subvector_stl_reverse_iterator<XprType, Direction>> {
529 enum { is_lvalue = internal::is_lvalue<XprType>::value };
531 typedef indexed_based_stl_reverse_iterator_base<subvector_stl_reverse_iterator> Base;
535 typedef std::conditional_t<Direction == Vertical, typename XprType::ColXpr, typename XprType::RowXpr> SubVectorType;
536 typedef std::conditional_t<Direction == Vertical, typename XprType::ConstColXpr, typename XprType::ConstRowXpr>
540 typedef std::conditional_t<bool(is_lvalue), SubVectorType, ConstSubVectorType> reference;
541 typedef typename reference::PlainObject value_type;
544 class subvector_stl_reverse_iterator_ptr {
546 subvector_stl_reverse_iterator_ptr(
const reference& subvector) : m_subvector(subvector) {}
547 reference* operator->() {
return &m_subvector; }
550 reference m_subvector;
554 typedef subvector_stl_reverse_iterator_ptr pointer;
556 subvector_stl_reverse_iterator() : Base() {}
557 subvector_stl_reverse_iterator(XprType& xpr, Index index) : Base(xpr, index) {}
559 reference operator*()
const {
return (*mp_xpr).template subVector<Direction>(m_index); }
560 reference operator[](Index i)
const {
return (*mp_xpr).template subVector<Direction>(m_index + i); }
561 pointer operator->()
const {
return (*mp_xpr).template subVector<Direction>(m_index); }
570template <
typename Derived>
572 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
577template <
typename Derived>
586template <
typename Derived>
588 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
596template <
typename Derived>
598 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
603template <
typename Derived>
612template <
typename Derived>
614 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
random_access_iterator_type const_iterator
Definition DenseBase.h:574
iterator begin()
Definition StlIterators.h:571
iterator end()
Definition StlIterators.h:597
const_iterator cbegin() const
Definition StlIterators.h:587
const_iterator cend() const
Definition StlIterators.h:613
random_access_iterator_type iterator
Definition DenseBase.h:572
const unsigned int DirectAccessBit
Definition Constants.h:159
Namespace containing all symbols from the Eigen library.
Definition B01_Experimental.dox:1
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition Meta.h:82