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 typedef std::random_access_iterator_tag iterator_category;
329 typedef std::conditional_t<bool(is_lvalue), value_type*, const value_type*> pointer;
330 typedef std::conditional_t<bool(is_lvalue), value_type&, const value_type&> reference;
332 pointer_based_stl_iterator() EIGEN_NO_THROW : m_ptr(0) {}
333 pointer_based_stl_iterator(XprType& xpr, Index index) EIGEN_NO_THROW : m_incr(xpr.innerStride()) {
334 m_ptr = xpr.data() + index * m_incr.value();
337 pointer_based_stl_iterator(
const non_const_iterator& other) EIGEN_NO_THROW : m_ptr(other.m_ptr),
338 m_incr(other.m_incr) {}
340 pointer_based_stl_iterator& operator=(
const non_const_iterator& other) EIGEN_NO_THROW {
342 m_incr.setValue(other.m_incr);
346 reference operator*()
const {
return *m_ptr; }
347 reference operator[](Index i)
const {
return *(m_ptr + i * m_incr.value()); }
348 pointer operator->()
const {
return m_ptr; }
350 pointer_based_stl_iterator& operator++() {
351 m_ptr += m_incr.value();
354 pointer_based_stl_iterator& operator--() {
355 m_ptr -= m_incr.value();
359 pointer_based_stl_iterator operator++(
int) {
360 pointer_based_stl_iterator prev(*
this);
364 pointer_based_stl_iterator operator--(
int) {
365 pointer_based_stl_iterator prev(*
this);
370 friend pointer_based_stl_iterator operator+(
const pointer_based_stl_iterator& a, Index b) {
371 pointer_based_stl_iterator ret(a);
375 friend pointer_based_stl_iterator operator-(
const pointer_based_stl_iterator& a, Index b) {
376 pointer_based_stl_iterator ret(a);
380 friend pointer_based_stl_iterator operator+(Index a,
const pointer_based_stl_iterator& b) {
381 pointer_based_stl_iterator ret(b);
385 friend pointer_based_stl_iterator operator-(Index a,
const pointer_based_stl_iterator& b) {
386 pointer_based_stl_iterator ret(b);
391 pointer_based_stl_iterator& operator+=(Index b) {
392 m_ptr += b * m_incr.value();
395 pointer_based_stl_iterator& operator-=(Index b) {
396 m_ptr -= b * m_incr.value();
400 difference_type operator-(
const pointer_based_stl_iterator& other)
const {
401 return (m_ptr - other.m_ptr) / m_incr.value();
404 difference_type operator-(
const other_iterator& other)
const {
return (m_ptr - other.m_ptr) / m_incr.value(); }
406 bool operator==(
const pointer_based_stl_iterator& other)
const {
return m_ptr == other.m_ptr; }
407 bool operator!=(
const pointer_based_stl_iterator& other)
const {
return m_ptr != other.m_ptr; }
408 bool operator<(
const pointer_based_stl_iterator& other)
const {
return m_ptr < other.m_ptr; }
409 bool operator<=(
const pointer_based_stl_iterator& other)
const {
return m_ptr <= other.m_ptr; }
410 bool operator>(
const pointer_based_stl_iterator& other)
const {
return m_ptr > other.m_ptr; }
411 bool operator>=(
const pointer_based_stl_iterator& other)
const {
return m_ptr >= other.m_ptr; }
413 bool operator==(
const other_iterator& other)
const {
return m_ptr == other.m_ptr; }
414 bool operator!=(
const other_iterator& other)
const {
return m_ptr != other.m_ptr; }
415 bool operator<(
const other_iterator& other)
const {
return m_ptr < other.m_ptr; }
416 bool operator<=(
const other_iterator& other)
const {
return m_ptr <= other.m_ptr; }
417 bool operator>(
const other_iterator& other)
const {
return m_ptr > other.m_ptr; }
418 bool operator>=(
const other_iterator& other)
const {
return m_ptr >= other.m_ptr; }
422 internal::variable_if_dynamic<Index, XprType::InnerStrideAtCompileTime> m_incr;
425template <
typename XprType_>
426struct indexed_based_stl_iterator_traits<generic_randaccess_stl_iterator<XprType_>> {
427 typedef XprType_ XprType;
428 typedef generic_randaccess_stl_iterator<std::remove_const_t<XprType>> non_const_iterator;
429 typedef generic_randaccess_stl_iterator<std::add_const_t<XprType>> const_iterator;
432template <
typename XprType>
433class generic_randaccess_stl_iterator
434 :
public indexed_based_stl_iterator_base<generic_randaccess_stl_iterator<XprType>> {
436 typedef typename XprType::Scalar value_type;
440 has_direct_access = (internal::traits<XprType>::Flags &
DirectAccessBit) ? 1 : 0,
441 is_lvalue = internal::is_lvalue<XprType>::value
444 typedef indexed_based_stl_iterator_base<generic_randaccess_stl_iterator> Base;
451 typedef const value_type read_only_ref_t;
454 typedef std::conditional_t<bool(is_lvalue), value_type*,
const value_type*> pointer;
455 typedef std::conditional_t<bool(is_lvalue), value_type&, read_only_ref_t> reference;
457 generic_randaccess_stl_iterator() : Base() {}
458 generic_randaccess_stl_iterator(XprType& xpr, Index index) : Base(xpr, index) {}
459 generic_randaccess_stl_iterator(
const typename Base::non_const_iterator& other) : Base(other) {}
460 using Base::operator=;
462 reference operator*()
const {
return (*mp_xpr)(m_index); }
463 reference operator[](Index i)
const {
return (*mp_xpr)(m_index + i); }
464 pointer operator->()
const {
return &((*mp_xpr)(m_index)); }
467template <
typename XprType_, DirectionType Direction>
468struct indexed_based_stl_iterator_traits<subvector_stl_iterator<XprType_, Direction>> {
469 typedef XprType_ XprType;
470 typedef subvector_stl_iterator<std::remove_const_t<XprType>, Direction> non_const_iterator;
471 typedef subvector_stl_iterator<std::add_const_t<XprType>, Direction> const_iterator;
474template <
typename XprType, DirectionType Direction>
475class subvector_stl_iterator :
public indexed_based_stl_iterator_base<subvector_stl_iterator<XprType, Direction>> {
477 enum { is_lvalue = internal::is_lvalue<XprType>::value };
479 typedef indexed_based_stl_iterator_base<subvector_stl_iterator> Base;
483 typedef std::conditional_t<Direction == Vertical, typename XprType::ColXpr, typename XprType::RowXpr> SubVectorType;
484 typedef std::conditional_t<Direction == Vertical, typename XprType::ConstColXpr, typename XprType::ConstRowXpr>
488 typedef std::conditional_t<bool(is_lvalue), SubVectorType, ConstSubVectorType> reference;
489 typedef typename reference::PlainObject value_type;
492 class subvector_stl_iterator_ptr {
494 subvector_stl_iterator_ptr(
const reference& subvector) : m_subvector(subvector) {}
495 reference* operator->() {
return &m_subvector; }
498 reference m_subvector;
502 typedef subvector_stl_iterator_ptr pointer;
504 subvector_stl_iterator() : Base() {}
505 subvector_stl_iterator(XprType& xpr, Index index) : Base(xpr, index) {}
507 reference operator*()
const {
return (*mp_xpr).template subVector<Direction>(m_index); }
508 reference operator[](Index i)
const {
return (*mp_xpr).template subVector<Direction>(m_index + i); }
509 pointer operator->()
const {
return (*mp_xpr).template subVector<Direction>(m_index); }
512template <
typename XprType_, DirectionType Direction>
513struct indexed_based_stl_iterator_traits<subvector_stl_reverse_iterator<XprType_, Direction>> {
514 typedef XprType_ XprType;
515 typedef subvector_stl_reverse_iterator<std::remove_const_t<XprType>, Direction> non_const_iterator;
516 typedef subvector_stl_reverse_iterator<std::add_const_t<XprType>, Direction> const_iterator;
519template <
typename XprType, DirectionType Direction>
520class subvector_stl_reverse_iterator
521 :
public indexed_based_stl_reverse_iterator_base<subvector_stl_reverse_iterator<XprType, Direction>> {
523 enum { is_lvalue = internal::is_lvalue<XprType>::value };
525 typedef indexed_based_stl_reverse_iterator_base<subvector_stl_reverse_iterator> Base;
529 typedef std::conditional_t<Direction == Vertical, typename XprType::ColXpr, typename XprType::RowXpr> SubVectorType;
530 typedef std::conditional_t<Direction == Vertical, typename XprType::ConstColXpr, typename XprType::ConstRowXpr>
534 typedef std::conditional_t<bool(is_lvalue), SubVectorType, ConstSubVectorType> reference;
535 typedef typename reference::PlainObject value_type;
538 class subvector_stl_reverse_iterator_ptr {
540 subvector_stl_reverse_iterator_ptr(
const reference& subvector) : m_subvector(subvector) {}
541 reference* operator->() {
return &m_subvector; }
544 reference m_subvector;
548 typedef subvector_stl_reverse_iterator_ptr pointer;
550 subvector_stl_reverse_iterator() : Base() {}
551 subvector_stl_reverse_iterator(XprType& xpr, Index index) : Base(xpr, index) {}
553 reference operator*()
const {
return (*mp_xpr).template subVector<Direction>(m_index); }
554 reference operator[](Index i)
const {
return (*mp_xpr).template subVector<Direction>(m_index + i); }
555 pointer operator->()
const {
return (*mp_xpr).template subVector<Direction>(m_index); }
564template <
typename Derived>
566 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
571template <
typename Derived>
580template <
typename Derived>
582 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
590template <
typename Derived>
592 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
597template <
typename Derived>
606template <
typename Derived>
608 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
random_access_iterator_type const_iterator
Definition DenseBase.h:572
iterator begin()
Definition StlIterators.h:565
iterator end()
Definition StlIterators.h:591
const_iterator cbegin() const
Definition StlIterators.h:581
const_iterator cend() const
Definition StlIterators.h:607
random_access_iterator_type iterator
Definition DenseBase.h:570
const unsigned int DirectAccessBit
Definition Constants.h:159
Namespace containing all symbols from the Eigen library.
Definition Core:137