11#ifndef EIGEN_STLITERATORS_H
12#define EIGEN_STLITERATORS_H
15#include "./InternalHeaderCheck.h"
21template <
typename IteratorType>
22struct indexed_based_stl_iterator_traits;
24template <
typename Derived>
25class indexed_based_stl_iterator_base {
27 using traits = indexed_based_stl_iterator_traits<Derived>;
28 using XprType =
typename traits::XprType;
29 using non_const_iterator = indexed_based_stl_iterator_base<typename traits::non_const_iterator>;
30 using const_iterator = indexed_based_stl_iterator_base<typename traits::const_iterator>;
31 using other_iterator = std::conditional_t<std::is_const<XprType>::value, non_const_iterator, const_iterator>;
33 friend class indexed_based_stl_iterator_base<typename traits::const_iterator>;
34 friend class indexed_based_stl_iterator_base<typename traits::non_const_iterator>;
37 using difference_type = Index;
38 using iterator_category = std::random_access_iterator_tag;
40 indexed_based_stl_iterator_base() noexcept = default;
41 indexed_based_stl_iterator_base(XprType& xpr, Index index) noexcept : mp_xpr(&xpr), m_index(index) {}
43 indexed_based_stl_iterator_base(
const non_const_iterator& other) noexcept
44 : mp_xpr(other.mp_xpr), m_index(other.m_index) {}
46 indexed_based_stl_iterator_base& operator=(
const non_const_iterator& other) {
47 mp_xpr = other.mp_xpr;
48 m_index = other.m_index;
52 Derived& operator++() {
56 Derived& operator--() {
61 Derived operator++(
int) {
62 Derived prev(derived());
66 Derived operator--(
int) {
67 Derived prev(derived());
72 friend Derived operator+(
const indexed_based_stl_iterator_base& a, Index b) {
73 Derived ret(a.derived());
77 friend Derived operator-(
const indexed_based_stl_iterator_base& a, Index b) {
78 Derived ret(a.derived());
82 friend Derived operator+(Index a,
const indexed_based_stl_iterator_base& b) {
83 Derived ret(b.derived());
87 friend Derived operator-(Index a,
const indexed_based_stl_iterator_base& b) {
88 Derived ret(b.derived());
93 Derived& operator+=(Index b) {
97 Derived& operator-=(Index b) {
102 difference_type operator-(
const indexed_based_stl_iterator_base& other)
const {
103 eigen_assert(mp_xpr == other.mp_xpr);
104 return m_index - other.m_index;
107 difference_type operator-(
const other_iterator& other)
const {
108 eigen_assert(mp_xpr == other.mp_xpr);
109 return m_index - other.m_index;
112 bool operator==(
const indexed_based_stl_iterator_base& other)
const {
113 eigen_assert(mp_xpr == other.mp_xpr);
114 return m_index == other.m_index;
116 bool operator!=(
const indexed_based_stl_iterator_base& other)
const {
117 eigen_assert(mp_xpr == other.mp_xpr);
118 return m_index != other.m_index;
120 bool operator<(
const indexed_based_stl_iterator_base& other)
const {
121 eigen_assert(mp_xpr == other.mp_xpr);
122 return m_index < other.m_index;
124 bool operator<=(
const indexed_based_stl_iterator_base& other)
const {
125 eigen_assert(mp_xpr == other.mp_xpr);
126 return m_index <= other.m_index;
128 bool operator>(
const indexed_based_stl_iterator_base& other)
const {
129 eigen_assert(mp_xpr == other.mp_xpr);
130 return m_index > other.m_index;
132 bool operator>=(
const indexed_based_stl_iterator_base& other)
const {
133 eigen_assert(mp_xpr == other.mp_xpr);
134 return m_index >= other.m_index;
137 bool operator==(
const other_iterator& other)
const {
138 eigen_assert(mp_xpr == other.mp_xpr);
139 return m_index == other.m_index;
141 bool operator!=(
const other_iterator& other)
const {
142 eigen_assert(mp_xpr == other.mp_xpr);
143 return m_index != other.m_index;
145 bool operator<(
const other_iterator& other)
const {
146 eigen_assert(mp_xpr == other.mp_xpr);
147 return m_index < other.m_index;
149 bool operator<=(
const other_iterator& other)
const {
150 eigen_assert(mp_xpr == other.mp_xpr);
151 return m_index <= other.m_index;
153 bool operator>(
const other_iterator& other)
const {
154 eigen_assert(mp_xpr == other.mp_xpr);
155 return m_index > other.m_index;
157 bool operator>=(
const other_iterator& other)
const {
158 eigen_assert(mp_xpr == other.mp_xpr);
159 return m_index >= other.m_index;
163 Derived& derived() {
return static_cast<Derived&
>(*this); }
164 const Derived& derived()
const {
return static_cast<const Derived&
>(*this); }
166 XprType* mp_xpr =
nullptr;
170template <
typename Derived>
171class indexed_based_stl_reverse_iterator_base {
173 using traits = indexed_based_stl_iterator_traits<Derived>;
174 using XprType =
typename traits::XprType;
175 using non_const_iterator = indexed_based_stl_reverse_iterator_base<typename traits::non_const_iterator>;
176 using const_iterator = indexed_based_stl_reverse_iterator_base<typename traits::const_iterator>;
177 using other_iterator = std::conditional_t<std::is_const<XprType>::value, non_const_iterator, const_iterator>;
179 friend class indexed_based_stl_reverse_iterator_base<typename traits::const_iterator>;
180 friend class indexed_based_stl_reverse_iterator_base<typename traits::non_const_iterator>;
183 using difference_type = Index;
184 using iterator_category = std::random_access_iterator_tag;
186 indexed_based_stl_reverse_iterator_base() = default;
187 indexed_based_stl_reverse_iterator_base(XprType& xpr, Index index) : mp_xpr(&xpr), m_index(index) {}
189 indexed_based_stl_reverse_iterator_base(
const non_const_iterator& other)
190 : mp_xpr(other.mp_xpr), m_index(other.m_index) {}
192 indexed_based_stl_reverse_iterator_base& operator=(
const non_const_iterator& other) {
193 mp_xpr = other.mp_xpr;
194 m_index = other.m_index;
198 Derived& operator++() {
202 Derived& operator--() {
207 Derived operator++(
int) {
208 Derived prev(derived());
212 Derived operator--(
int) {
213 Derived prev(derived());
218 friend Derived operator+(
const indexed_based_stl_reverse_iterator_base& a, Index b) {
219 Derived ret(a.derived());
223 friend Derived operator-(
const indexed_based_stl_reverse_iterator_base& a, Index b) {
224 Derived ret(a.derived());
228 friend Derived operator+(Index a,
const indexed_based_stl_reverse_iterator_base& b) {
229 Derived ret(b.derived());
233 friend Derived operator-(Index a,
const indexed_based_stl_reverse_iterator_base& b) {
234 Derived ret(b.derived());
239 Derived& operator+=(Index b) {
243 Derived& operator-=(Index b) {
248 difference_type operator-(
const indexed_based_stl_reverse_iterator_base& other)
const {
249 eigen_assert(mp_xpr == other.mp_xpr);
250 return other.m_index - m_index;
253 difference_type operator-(
const other_iterator& other)
const {
254 eigen_assert(mp_xpr == other.mp_xpr);
255 return other.m_index - m_index;
258 bool operator==(
const indexed_based_stl_reverse_iterator_base& other)
const {
259 eigen_assert(mp_xpr == other.mp_xpr);
260 return m_index == other.m_index;
262 bool operator!=(
const indexed_based_stl_reverse_iterator_base& other)
const {
263 eigen_assert(mp_xpr == other.mp_xpr);
264 return m_index != other.m_index;
266 bool operator<(
const indexed_based_stl_reverse_iterator_base& other)
const {
267 eigen_assert(mp_xpr == other.mp_xpr);
268 return m_index > other.m_index;
270 bool operator<=(
const indexed_based_stl_reverse_iterator_base& other)
const {
271 eigen_assert(mp_xpr == other.mp_xpr);
272 return m_index >= other.m_index;
274 bool operator>(
const indexed_based_stl_reverse_iterator_base& other)
const {
275 eigen_assert(mp_xpr == other.mp_xpr);
276 return m_index < other.m_index;
278 bool operator>=(
const indexed_based_stl_reverse_iterator_base& other)
const {
279 eigen_assert(mp_xpr == other.mp_xpr);
280 return m_index <= other.m_index;
283 bool operator==(
const other_iterator& other)
const {
284 eigen_assert(mp_xpr == other.mp_xpr);
285 return m_index == other.m_index;
287 bool operator!=(
const other_iterator& other)
const {
288 eigen_assert(mp_xpr == other.mp_xpr);
289 return m_index != other.m_index;
291 bool operator<(
const other_iterator& other)
const {
292 eigen_assert(mp_xpr == other.mp_xpr);
293 return m_index > other.m_index;
295 bool operator<=(
const other_iterator& other)
const {
296 eigen_assert(mp_xpr == other.mp_xpr);
297 return m_index >= other.m_index;
299 bool operator>(
const other_iterator& other)
const {
300 eigen_assert(mp_xpr == other.mp_xpr);
301 return m_index < other.m_index;
303 bool operator>=(
const other_iterator& other)
const {
304 eigen_assert(mp_xpr == other.mp_xpr);
305 return m_index <= other.m_index;
309 Derived& derived() {
return static_cast<Derived&
>(*this); }
310 const Derived& derived()
const {
return static_cast<const Derived&
>(*this); }
312 XprType* mp_xpr =
nullptr;
316template <
typename XprType>
317class pointer_based_stl_iterator {
318 static constexpr bool is_lvalue = internal::is_lvalue<XprType>::value;
319 static constexpr bool is_contiguous = XprType::InnerStrideAtCompileTime == 1;
320 using non_const_iterator = pointer_based_stl_iterator<std::remove_const_t<XprType>>;
321 using const_iterator = pointer_based_stl_iterator<std::add_const_t<XprType>>;
322 using other_iterator = std::conditional_t<std::is_const<XprType>::value, non_const_iterator, const_iterator>;
324 friend class pointer_based_stl_iterator<std::add_const_t<XprType>>;
325 friend class pointer_based_stl_iterator<std::remove_const_t<XprType>>;
328 using difference_type = Index;
329 using value_type = typename XprType::Scalar;
330#if EIGEN_COMP_CXXVER >= 20 && defined(__cpp_lib_concepts) && __cpp_lib_concepts >= 202002L
331 using iterator_category =
332 std::conditional_t<is_contiguous, std::contiguous_iterator_tag, std::random_access_iterator_tag>;
334 using iterator_category = std::random_access_iterator_tag;
336 using pointer = std::conditional_t<bool(is_lvalue), value_type*, const value_type*>;
337 using reference = std::conditional_t<bool(is_lvalue), value_type&, const value_type&>;
339 pointer_based_stl_iterator() noexcept = default;
340 pointer_based_stl_iterator(XprType& xpr, Index index) noexcept
341 : m_ptr(is_contiguous && index != 0 ? xpr.data() + index : xpr.data()),
342 m_index(is_contiguous ? 0 : index),
343 m_incr(xpr.innerStride()) {}
345 pointer_based_stl_iterator(
const non_const_iterator& other) noexcept
346 : m_ptr(other.m_ptr), m_index(other.m_index), m_incr(other.m_incr) {}
348 pointer_based_stl_iterator& operator=(
const non_const_iterator& other)
noexcept {
350 m_index = other.m_index;
351 m_incr.setValue(other.m_incr);
355 reference operator*()
const {
return m_ptr[m_index.value() * m_incr.value()]; }
356 reference operator[](Index i)
const {
return m_ptr[(m_index.value() + i) * m_incr.value()]; }
357 pointer operator->()
const {
return m_index.value() == 0 ? m_ptr : m_ptr + m_index.value() * m_incr.value(); }
359 pointer_based_stl_iterator& operator++() {
return *
this += 1; }
360 pointer_based_stl_iterator& operator--() {
return *
this -= 1; }
362 pointer_based_stl_iterator operator++(
int) {
363 pointer_based_stl_iterator prev(*
this);
367 pointer_based_stl_iterator operator--(
int) {
368 pointer_based_stl_iterator prev(*
this);
373 friend pointer_based_stl_iterator operator+(
const pointer_based_stl_iterator& a, Index b) {
374 pointer_based_stl_iterator ret(a);
378 friend pointer_based_stl_iterator operator-(
const pointer_based_stl_iterator& a, Index b) {
379 pointer_based_stl_iterator ret(a);
383 friend pointer_based_stl_iterator operator+(Index a,
const pointer_based_stl_iterator& b) {
384 pointer_based_stl_iterator ret(b);
388 friend pointer_based_stl_iterator operator-(Index a,
const pointer_based_stl_iterator& b) {
389 pointer_based_stl_iterator ret(b);
394 pointer_based_stl_iterator& operator+=(Index b) {
395 EIGEN_IF_CONSTEXPR (is_contiguous) {
396 if (b != 0) m_ptr += b;
398 m_index.setValue(m_index.value() + b);
402 pointer_based_stl_iterator& operator-=(Index b) {
403 EIGEN_IF_CONSTEXPR (is_contiguous) {
404 if (b != 0) m_ptr -= b;
406 m_index.setValue(m_index.value() - b);
411 difference_type operator-(
const pointer_based_stl_iterator& other)
const {
412 EIGEN_IF_CONSTEXPR (is_contiguous)
return m_ptr == other.m_ptr ? 0 : m_ptr - other.m_ptr;
413 eigen_assert(m_ptr == other.m_ptr);
414 return m_index.value() - other.m_index.value();
417 difference_type operator-(
const other_iterator& other)
const {
418 EIGEN_IF_CONSTEXPR (is_contiguous)
return m_ptr == other.m_ptr ? 0 : m_ptr - other.m_ptr;
419 eigen_assert(m_ptr == other.m_ptr);
420 return m_index.value() - other.m_index.value();
423 bool operator==(
const pointer_based_stl_iterator& other)
const {
424 return m_ptr == other.m_ptr && m_index.value() == other.m_index.value();
426 bool operator!=(
const pointer_based_stl_iterator& other)
const {
return !(*
this == other); }
427 bool operator<(
const pointer_based_stl_iterator& other)
const {
428 EIGEN_IF_CONSTEXPR (is_contiguous)
return m_ptr < other.m_ptr;
429 eigen_assert(m_ptr == other.m_ptr);
430 return m_index.value() < other.m_index.value();
432 bool operator<=(
const pointer_based_stl_iterator& other)
const {
return !(other < *
this); }
433 bool operator>(
const pointer_based_stl_iterator& other)
const {
return other < *
this; }
434 bool operator>=(
const pointer_based_stl_iterator& other)
const {
return !(*
this < other); }
436 bool operator==(
const other_iterator& other)
const {
437 return m_ptr == other.m_ptr && m_index.value() == other.m_index.value();
439 bool operator!=(
const other_iterator& other)
const {
return !(*
this == other); }
440 bool operator<(
const other_iterator& other)
const {
441 EIGEN_IF_CONSTEXPR (is_contiguous)
return m_ptr < other.m_ptr;
442 eigen_assert(m_ptr == other.m_ptr);
443 return m_index.value() < other.m_index.value();
445 bool operator<=(
const other_iterator& other)
const {
return !(other < *
this); }
446 bool operator>(
const other_iterator& other)
const {
return other < *
this; }
447 bool operator>=(
const other_iterator& other)
const {
return !(*
this < other); }
451 pointer m_ptr =
nullptr;
452 internal::variable_if_dynamic<Index, is_contiguous ? 0 : Dynamic> m_index{0};
453 internal::variable_if_dynamic<Index, XprType::InnerStrideAtCompileTime> m_incr{XprType::InnerStrideAtCompileTime};
456template <
typename XprType_>
457struct indexed_based_stl_iterator_traits<generic_randaccess_stl_iterator<XprType_>> {
458 using XprType = XprType_;
459 using non_const_iterator = generic_randaccess_stl_iterator<std::remove_const_t<XprType>>;
460 using const_iterator = generic_randaccess_stl_iterator<std::add_const_t<XprType>>;
463template <
typename XprType>
464class generic_randaccess_stl_iterator
465 :
public indexed_based_stl_iterator_base<generic_randaccess_stl_iterator<XprType>> {
467 using value_type =
typename XprType::Scalar;
471 has_direct_access = (internal::traits<XprType>::Flags &
DirectAccessBit) ? 1 : 0,
472 is_lvalue = internal::is_lvalue<XprType>::value
475 using Base = indexed_based_stl_iterator_base<generic_randaccess_stl_iterator>;
482 using read_only_ref_t =
const value_type;
485 using pointer = std::conditional_t<bool(is_lvalue), value_type*,
const value_type*>;
486 using reference = std::conditional_t<bool(is_lvalue), value_type&, read_only_ref_t>;
488 generic_randaccess_stl_iterator() =
default;
489 generic_randaccess_stl_iterator(XprType& xpr, Index index) : Base(xpr, index) {}
490 generic_randaccess_stl_iterator(
const typename Base::non_const_iterator& other) : Base(other) {}
491 using Base::operator=;
493 reference operator*()
const {
return (*mp_xpr)(m_index); }
494 reference operator[](Index i)
const {
return (*mp_xpr)(m_index + i); }
495 pointer operator->()
const {
return &((*mp_xpr)(m_index)); }
498template <
typename XprType_, DirectionType Direction>
499struct indexed_based_stl_iterator_traits<subvector_stl_iterator<XprType_, Direction>> {
500 using XprType = XprType_;
501 using non_const_iterator = subvector_stl_iterator<std::remove_const_t<XprType>, Direction>;
502 using const_iterator = subvector_stl_iterator<std::add_const_t<XprType>, Direction>;
505template <
typename XprType, DirectionType Direction>
506class subvector_stl_iterator :
public indexed_based_stl_iterator_base<subvector_stl_iterator<XprType, Direction>> {
508 enum { is_lvalue = internal::is_lvalue<XprType>::value };
510 using Base = indexed_based_stl_iterator_base<subvector_stl_iterator>;
514 using SubVectorType = std::conditional_t<Direction == Vertical, typename XprType::ColXpr, typename XprType::RowXpr>;
515 using ConstSubVectorType =
516 std::conditional_t<Direction == Vertical, typename XprType::ConstColXpr, typename XprType::ConstRowXpr>;
519 using reference = std::conditional_t<bool(is_lvalue), SubVectorType, ConstSubVectorType>;
520 using value_type =
typename reference::PlainObject;
523 class subvector_stl_iterator_ptr {
525 subvector_stl_iterator_ptr(
const reference& subvector) : m_subvector(subvector) {}
526 reference* operator->() {
return &m_subvector; }
529 reference m_subvector;
533 using pointer = subvector_stl_iterator_ptr;
535 subvector_stl_iterator() =
default;
536 subvector_stl_iterator(XprType& xpr, Index index) : Base(xpr, index) {}
537 subvector_stl_iterator(
const typename Base::non_const_iterator& other) : Base(other) {}
539 reference operator*()
const {
return (*mp_xpr).template subVector<Direction>(m_index); }
540 reference operator[](Index i)
const {
return (*mp_xpr).template subVector<Direction>(m_index + i); }
541 pointer operator->()
const {
return (*mp_xpr).template subVector<Direction>(m_index); }
544template <
typename XprType_, DirectionType Direction>
545struct indexed_based_stl_iterator_traits<subvector_stl_reverse_iterator<XprType_, Direction>> {
546 using XprType = XprType_;
547 using non_const_iterator = subvector_stl_reverse_iterator<std::remove_const_t<XprType>, Direction>;
548 using const_iterator = subvector_stl_reverse_iterator<std::add_const_t<XprType>, Direction>;
551template <
typename XprType, DirectionType Direction>
552class subvector_stl_reverse_iterator
553 :
public indexed_based_stl_reverse_iterator_base<subvector_stl_reverse_iterator<XprType, Direction>> {
555 enum { is_lvalue = internal::is_lvalue<XprType>::value };
557 using Base = indexed_based_stl_reverse_iterator_base<subvector_stl_reverse_iterator>;
561 using SubVectorType = std::conditional_t<Direction == Vertical, typename XprType::ColXpr, typename XprType::RowXpr>;
562 using ConstSubVectorType =
563 std::conditional_t<Direction == Vertical, typename XprType::ConstColXpr, typename XprType::ConstRowXpr>;
566 using reference = std::conditional_t<bool(is_lvalue), SubVectorType, ConstSubVectorType>;
567 using value_type =
typename reference::PlainObject;
570 class subvector_stl_reverse_iterator_ptr {
572 subvector_stl_reverse_iterator_ptr(
const reference& subvector) : m_subvector(subvector) {}
573 reference* operator->() {
return &m_subvector; }
576 reference m_subvector;
580 using pointer = subvector_stl_reverse_iterator_ptr;
582 subvector_stl_reverse_iterator() =
default;
583 subvector_stl_reverse_iterator(XprType& xpr, Index index) : Base(xpr, index) {}
584 subvector_stl_reverse_iterator(
const typename Base::non_const_iterator& other) : Base(other) {}
597template <
typename Derived>
599 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
604template <
typename Derived>
613template <
typename Derived>
615 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
623template <
typename Derived>
625 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
630template <
typename Derived>
639template <
typename Derived>
641 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
iterator begin()
Definition StlIterators.h:598
random_access_iterator_type const_iterator
Definition DenseBase.h:567
constexpr std::conditional_t< Direction==Vertical, ColXpr, RowXpr > subVector(Index i)
Definition DenseBase.h:1364
iterator end()
Definition StlIterators.h:624
const_iterator cbegin() const
Definition StlIterators.h:614
const_iterator cend() const
Definition StlIterators.h:640
random_access_iterator_type iterator
Definition DenseBase.h:565
constexpr unsigned int DirectAccessBit
Definition Constants.h:160