Please, help us to better know about our user community by answering the following short survey: https://forms.gle/wpyrxWi18ox9Z5ae9
Eigen  3.4.99 (git rev 199c5f2b47eb1f8e5a2d20e60f07e97cd95a6ba6)
StlIterators.h
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2018 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 namespace Eigen {
11 
12 namespace internal {
13 
14 template<typename IteratorType>
15 struct indexed_based_stl_iterator_traits;
16 
17 template<typename Derived>
18 class indexed_based_stl_iterator_base
19 {
20 protected:
21  typedef indexed_based_stl_iterator_traits<Derived> traits;
22  typedef typename traits::XprType XprType;
23  typedef indexed_based_stl_iterator_base<typename traits::non_const_iterator> non_const_iterator;
24  typedef indexed_based_stl_iterator_base<typename traits::const_iterator> const_iterator;
25  typedef typename internal::conditional<internal::is_const<XprType>::value,non_const_iterator,const_iterator>::type other_iterator;
26  // NOTE: in C++03 we cannot declare friend classes through typedefs because we need to write friend class:
27  friend class indexed_based_stl_iterator_base<typename traits::const_iterator>;
28  friend class indexed_based_stl_iterator_base<typename traits::non_const_iterator>;
29 public:
30  typedef Index difference_type;
31  typedef std::random_access_iterator_tag iterator_category;
32 
33  indexed_based_stl_iterator_base() : mp_xpr(0), m_index(0) {}
34  indexed_based_stl_iterator_base(XprType& xpr, Index index) : mp_xpr(&xpr), m_index(index) {}
35 
36  indexed_based_stl_iterator_base(const non_const_iterator& other)
37  : mp_xpr(other.mp_xpr), m_index(other.m_index)
38  {}
39 
40  indexed_based_stl_iterator_base& operator=(const non_const_iterator& other)
41  {
42  mp_xpr = other.mp_xpr;
43  m_index = other.m_index;
44  return *this;
45  }
46 
47  Derived& operator++() { ++m_index; return derived(); }
48  Derived& operator--() { --m_index; return derived(); }
49 
50  Derived operator++(int) { Derived prev(derived()); operator++(); return prev;}
51  Derived operator--(int) { Derived prev(derived()); operator--(); return prev;}
52 
53  friend Derived operator+(const indexed_based_stl_iterator_base& a, Index b) { Derived ret(a.derived()); ret += b; return ret; }
54  friend Derived operator-(const indexed_based_stl_iterator_base& a, Index b) { Derived ret(a.derived()); ret -= b; return ret; }
55  friend Derived operator+(Index a, const indexed_based_stl_iterator_base& b) { Derived ret(b.derived()); ret += a; return ret; }
56  friend Derived operator-(Index a, const indexed_based_stl_iterator_base& b) { Derived ret(b.derived()); ret -= a; return ret; }
57 
58  Derived& operator+=(Index b) { m_index += b; return derived(); }
59  Derived& operator-=(Index b) { m_index -= b; return derived(); }
60 
61  difference_type operator-(const indexed_based_stl_iterator_base& other) const
62  {
63  eigen_assert(mp_xpr == other.mp_xpr);
64  return m_index - other.m_index;
65  }
66 
67  difference_type operator-(const other_iterator& other) const
68  {
69  eigen_assert(mp_xpr == other.mp_xpr);
70  return m_index - other.m_index;
71  }
72 
73  bool operator==(const indexed_based_stl_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index == other.m_index; }
74  bool operator!=(const indexed_based_stl_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index != other.m_index; }
75  bool operator< (const indexed_based_stl_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index < other.m_index; }
76  bool operator<=(const indexed_based_stl_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index <= other.m_index; }
77  bool operator> (const indexed_based_stl_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index > other.m_index; }
78  bool operator>=(const indexed_based_stl_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index >= other.m_index; }
79 
80  bool operator==(const other_iterator& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index == other.m_index; }
81  bool operator!=(const other_iterator& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index != other.m_index; }
82  bool operator< (const other_iterator& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index < other.m_index; }
83  bool operator<=(const other_iterator& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index <= other.m_index; }
84  bool operator> (const other_iterator& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index > other.m_index; }
85  bool operator>=(const other_iterator& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index >= other.m_index; }
86 
87 protected:
88 
89  Derived& derived() { return static_cast<Derived&>(*this); }
90  const Derived& derived() const { return static_cast<const Derived&>(*this); }
91 
92  XprType *mp_xpr;
93  Index m_index;
94 };
95 
96 template<typename Derived>
97 class indexed_based_stl_reverse_iterator_base
98 {
99 protected:
100  typedef indexed_based_stl_iterator_traits<Derived> traits;
101  typedef typename traits::XprType XprType;
102  typedef indexed_based_stl_reverse_iterator_base<typename traits::non_const_iterator> non_const_iterator;
103  typedef indexed_based_stl_reverse_iterator_base<typename traits::const_iterator> const_iterator;
104  typedef typename internal::conditional<internal::is_const<XprType>::value,non_const_iterator,const_iterator>::type other_iterator;
105  // NOTE: in C++03 we cannot declare friend classes through typedefs because we need to write friend class:
106  friend class indexed_based_stl_reverse_iterator_base<typename traits::const_iterator>;
107  friend class indexed_based_stl_reverse_iterator_base<typename traits::non_const_iterator>;
108 public:
109  typedef Index difference_type;
110  typedef std::random_access_iterator_tag iterator_category;
111 
112  indexed_based_stl_reverse_iterator_base() : mp_xpr(0), m_index(0) {}
113  indexed_based_stl_reverse_iterator_base(XprType& xpr, Index index) : mp_xpr(&xpr), m_index(index) {}
114 
115  indexed_based_stl_reverse_iterator_base(const non_const_iterator& other)
116  : mp_xpr(other.mp_xpr), m_index(other.m_index)
117  {}
118 
119  indexed_based_stl_reverse_iterator_base& operator=(const non_const_iterator& other)
120  {
121  mp_xpr = other.mp_xpr;
122  m_index = other.m_index;
123  return *this;
124  }
125 
126  Derived& operator++() { --m_index; return derived(); }
127  Derived& operator--() { ++m_index; return derived(); }
128 
129  Derived operator++(int) { Derived prev(derived()); operator++(); return prev;}
130  Derived operator--(int) { Derived prev(derived()); operator--(); return prev;}
131 
132  friend Derived operator+(const indexed_based_stl_reverse_iterator_base& a, Index b) { Derived ret(a.derived()); ret += b; return ret; }
133  friend Derived operator-(const indexed_based_stl_reverse_iterator_base& a, Index b) { Derived ret(a.derived()); ret -= b; return ret; }
134  friend Derived operator+(Index a, const indexed_based_stl_reverse_iterator_base& b) { Derived ret(b.derived()); ret += a; return ret; }
135  friend Derived operator-(Index a, const indexed_based_stl_reverse_iterator_base& b) { Derived ret(b.derived()); ret -= a; return ret; }
136 
137  Derived& operator+=(Index b) { m_index -= b; return derived(); }
138  Derived& operator-=(Index b) { m_index += b; return derived(); }
139 
140  difference_type operator-(const indexed_based_stl_reverse_iterator_base& other) const
141  {
142  eigen_assert(mp_xpr == other.mp_xpr);
143  return other.m_index - m_index;
144  }
145 
146  difference_type operator-(const other_iterator& other) const
147  {
148  eigen_assert(mp_xpr == other.mp_xpr);
149  return other.m_index - m_index;
150  }
151 
152  bool operator==(const indexed_based_stl_reverse_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index == other.m_index; }
153  bool operator!=(const indexed_based_stl_reverse_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index != other.m_index; }
154  bool operator< (const indexed_based_stl_reverse_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index > other.m_index; }
155  bool operator<=(const indexed_based_stl_reverse_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index >= other.m_index; }
156  bool operator> (const indexed_based_stl_reverse_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index < other.m_index; }
157  bool operator>=(const indexed_based_stl_reverse_iterator_base& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index <= other.m_index; }
158 
159  bool operator==(const other_iterator& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index == other.m_index; }
160  bool operator!=(const other_iterator& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index != other.m_index; }
161  bool operator< (const other_iterator& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index > other.m_index; }
162  bool operator<=(const other_iterator& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index >= other.m_index; }
163  bool operator> (const other_iterator& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index < other.m_index; }
164  bool operator>=(const other_iterator& other) const { eigen_assert(mp_xpr == other.mp_xpr); return m_index <= other.m_index; }
165 
166 protected:
167 
168  Derived& derived() { return static_cast<Derived&>(*this); }
169  const Derived& derived() const { return static_cast<const Derived&>(*this); }
170 
171  XprType *mp_xpr;
172  Index m_index;
173 };
174 
175 template<typename XprType>
176 class pointer_based_stl_iterator
177 {
178  enum { is_lvalue = internal::is_lvalue<XprType>::value };
179  typedef pointer_based_stl_iterator<typename internal::remove_const<XprType>::type> non_const_iterator;
180  typedef pointer_based_stl_iterator<typename internal::add_const<XprType>::type> const_iterator;
181  typedef typename internal::conditional<internal::is_const<XprType>::value,non_const_iterator,const_iterator>::type other_iterator;
182  // NOTE: in C++03 we cannot declare friend classes through typedefs because we need to write friend class:
183  friend class pointer_based_stl_iterator<typename internal::add_const<XprType>::type>;
184  friend class pointer_based_stl_iterator<typename internal::remove_const<XprType>::type>;
185 public:
186  typedef Index difference_type;
187  typedef typename XprType::Scalar value_type;
188  typedef std::random_access_iterator_tag iterator_category;
189  typedef typename internal::conditional<bool(is_lvalue), value_type*, const value_type*>::type pointer;
190  typedef typename internal::conditional<bool(is_lvalue), value_type&, const value_type&>::type reference;
191 
192 
193  pointer_based_stl_iterator() : m_ptr(0) {}
194  pointer_based_stl_iterator(XprType& xpr, Index index) : m_incr(xpr.innerStride())
195  {
196  m_ptr = xpr.data() + index * m_incr.value();
197  }
198 
199  pointer_based_stl_iterator(const non_const_iterator& other)
200  : m_ptr(other.m_ptr), m_incr(other.m_incr)
201  {}
202 
203  pointer_based_stl_iterator& operator=(const non_const_iterator& other)
204  {
205  m_ptr = other.m_ptr;
206  m_incr.setValue(other.m_incr);
207  return *this;
208  }
209 
210  reference operator*() const { return *m_ptr; }
211  reference operator[](Index i) const { return *(m_ptr+i*m_incr.value()); }
212  pointer operator->() const { return m_ptr; }
213 
214  pointer_based_stl_iterator& operator++() { m_ptr += m_incr.value(); return *this; }
215  pointer_based_stl_iterator& operator--() { m_ptr -= m_incr.value(); return *this; }
216 
217  pointer_based_stl_iterator operator++(int) { pointer_based_stl_iterator prev(*this); operator++(); return prev;}
218  pointer_based_stl_iterator operator--(int) { pointer_based_stl_iterator prev(*this); operator--(); return prev;}
219 
220  friend pointer_based_stl_iterator operator+(const pointer_based_stl_iterator& a, Index b) { pointer_based_stl_iterator ret(a); ret += b; return ret; }
221  friend pointer_based_stl_iterator operator-(const pointer_based_stl_iterator& a, Index b) { pointer_based_stl_iterator ret(a); ret -= b; return ret; }
222  friend pointer_based_stl_iterator operator+(Index a, const pointer_based_stl_iterator& b) { pointer_based_stl_iterator ret(b); ret += a; return ret; }
223  friend pointer_based_stl_iterator operator-(Index a, const pointer_based_stl_iterator& b) { pointer_based_stl_iterator ret(b); ret -= a; return ret; }
224 
225  pointer_based_stl_iterator& operator+=(Index b) { m_ptr += b*m_incr.value(); return *this; }
226  pointer_based_stl_iterator& operator-=(Index b) { m_ptr -= b*m_incr.value(); return *this; }
227 
228  difference_type operator-(const pointer_based_stl_iterator& other) const {
229  return (m_ptr - other.m_ptr)/m_incr.value();
230  }
231 
232  difference_type operator-(const other_iterator& other) const {
233  return (m_ptr - other.m_ptr)/m_incr.value();
234  }
235 
236  bool operator==(const pointer_based_stl_iterator& other) const { return m_ptr == other.m_ptr; }
237  bool operator!=(const pointer_based_stl_iterator& other) const { return m_ptr != other.m_ptr; }
238  bool operator< (const pointer_based_stl_iterator& other) const { return m_ptr < other.m_ptr; }
239  bool operator<=(const pointer_based_stl_iterator& other) const { return m_ptr <= other.m_ptr; }
240  bool operator> (const pointer_based_stl_iterator& other) const { return m_ptr > other.m_ptr; }
241  bool operator>=(const pointer_based_stl_iterator& other) const { return m_ptr >= other.m_ptr; }
242 
243  bool operator==(const other_iterator& other) const { return m_ptr == other.m_ptr; }
244  bool operator!=(const other_iterator& other) const { return m_ptr != other.m_ptr; }
245  bool operator< (const other_iterator& other) const { return m_ptr < other.m_ptr; }
246  bool operator<=(const other_iterator& other) const { return m_ptr <= other.m_ptr; }
247  bool operator> (const other_iterator& other) const { return m_ptr > other.m_ptr; }
248  bool operator>=(const other_iterator& other) const { return m_ptr >= other.m_ptr; }
249 
250 protected:
251 
252  pointer m_ptr;
253  internal::variable_if_dynamic<Index, XprType::InnerStrideAtCompileTime> m_incr;
254 };
255 
256 template<typename _XprType>
257 struct indexed_based_stl_iterator_traits<generic_randaccess_stl_iterator<_XprType> >
258 {
259  typedef _XprType XprType;
260  typedef generic_randaccess_stl_iterator<typename internal::remove_const<XprType>::type> non_const_iterator;
261  typedef generic_randaccess_stl_iterator<typename internal::add_const<XprType>::type> const_iterator;
262 };
263 
264 template<typename XprType>
265 class generic_randaccess_stl_iterator : public indexed_based_stl_iterator_base<generic_randaccess_stl_iterator<XprType> >
266 {
267 public:
268  typedef typename XprType::Scalar value_type;
269 
270 protected:
271 
272  enum {
273  has_direct_access = (internal::traits<XprType>::Flags & DirectAccessBit) ? 1 : 0,
274  is_lvalue = internal::is_lvalue<XprType>::value
275  };
276 
277  typedef indexed_based_stl_iterator_base<generic_randaccess_stl_iterator> Base;
278  using Base::m_index;
279  using Base::mp_xpr;
280 
281  // TODO currently const Transpose/Reshape expressions never returns const references,
282  // so lets return by value too.
283  //typedef typename internal::conditional<bool(has_direct_access), const value_type&, const value_type>::type read_only_ref_t;
284  typedef const value_type read_only_ref_t;
285 
286 public:
287 
288  typedef typename internal::conditional<bool(is_lvalue), value_type *, const value_type *>::type pointer;
289  typedef typename internal::conditional<bool(is_lvalue), value_type&, read_only_ref_t>::type reference;
290 
291  generic_randaccess_stl_iterator() : Base() {}
292  generic_randaccess_stl_iterator(XprType& xpr, Index index) : Base(xpr,index) {}
293  generic_randaccess_stl_iterator(const typename Base::non_const_iterator& other) : Base(other) {}
294  using Base::operator=;
295 
296  reference operator*() const { return (*mp_xpr)(m_index); }
297  reference operator[](Index i) const { return (*mp_xpr)(m_index+i); }
298  pointer operator->() const { return &((*mp_xpr)(m_index)); }
299 };
300 
301 template<typename _XprType, DirectionType Direction>
302 struct indexed_based_stl_iterator_traits<subvector_stl_iterator<_XprType,Direction> >
303 {
304  typedef _XprType XprType;
305  typedef subvector_stl_iterator<typename internal::remove_const<XprType>::type, Direction> non_const_iterator;
306  typedef subvector_stl_iterator<typename internal::add_const<XprType>::type, Direction> const_iterator;
307 };
308 
309 template<typename XprType, DirectionType Direction>
310 class subvector_stl_iterator : public indexed_based_stl_iterator_base<subvector_stl_iterator<XprType,Direction> >
311 {
312 protected:
313 
314  enum { is_lvalue = internal::is_lvalue<XprType>::value };
315 
316  typedef indexed_based_stl_iterator_base<subvector_stl_iterator> Base;
317  using Base::m_index;
318  using Base::mp_xpr;
319 
320  typedef typename internal::conditional<Direction==Vertical,typename XprType::ColXpr,typename XprType::RowXpr>::type SubVectorType;
321  typedef typename internal::conditional<Direction==Vertical,typename XprType::ConstColXpr,typename XprType::ConstRowXpr>::type ConstSubVectorType;
322 
323 
324 public:
325  typedef typename internal::conditional<bool(is_lvalue), SubVectorType, ConstSubVectorType>::type reference;
326  typedef typename reference::PlainObject value_type;
327 
328 private:
329  class subvector_stl_iterator_ptr
330  {
331  public:
332  subvector_stl_iterator_ptr(const reference &subvector) : m_subvector(subvector) {}
333  reference* operator->() { return &m_subvector; }
334  private:
335  reference m_subvector;
336  };
337 public:
338 
339  typedef subvector_stl_iterator_ptr pointer;
340 
341  subvector_stl_iterator() : Base() {}
342  subvector_stl_iterator(XprType& xpr, Index index) : Base(xpr,index) {}
343 
344  reference operator*() const { return (*mp_xpr).template subVector<Direction>(m_index); }
345  reference operator[](Index i) const { return (*mp_xpr).template subVector<Direction>(m_index+i); }
346  pointer operator->() const { return (*mp_xpr).template subVector<Direction>(m_index); }
347 };
348 
349 template<typename _XprType, DirectionType Direction>
350 struct indexed_based_stl_iterator_traits<subvector_stl_reverse_iterator<_XprType,Direction> >
351 {
352  typedef _XprType XprType;
353  typedef subvector_stl_reverse_iterator<typename internal::remove_const<XprType>::type, Direction> non_const_iterator;
354  typedef subvector_stl_reverse_iterator<typename internal::add_const<XprType>::type, Direction> const_iterator;
355 };
356 
357 template<typename XprType, DirectionType Direction>
358 class subvector_stl_reverse_iterator : public indexed_based_stl_reverse_iterator_base<subvector_stl_reverse_iterator<XprType,Direction> >
359 {
360 protected:
361 
362  enum { is_lvalue = internal::is_lvalue<XprType>::value };
363 
364  typedef indexed_based_stl_reverse_iterator_base<subvector_stl_reverse_iterator> Base;
365  using Base::m_index;
366  using Base::mp_xpr;
367 
368  typedef typename internal::conditional<Direction==Vertical,typename XprType::ColXpr,typename XprType::RowXpr>::type SubVectorType;
369  typedef typename internal::conditional<Direction==Vertical,typename XprType::ConstColXpr,typename XprType::ConstRowXpr>::type ConstSubVectorType;
370 
371 
372 public:
373  typedef typename internal::conditional<bool(is_lvalue), SubVectorType, ConstSubVectorType>::type reference;
374  typedef typename reference::PlainObject value_type;
375 
376 private:
377  class subvector_stl_reverse_iterator_ptr
378  {
379  public:
380  subvector_stl_reverse_iterator_ptr(const reference &subvector) : m_subvector(subvector) {}
381  reference* operator->() { return &m_subvector; }
382  private:
383  reference m_subvector;
384  };
385 public:
386 
387  typedef subvector_stl_reverse_iterator_ptr pointer;
388 
389  subvector_stl_reverse_iterator() : Base() {}
390  subvector_stl_reverse_iterator(XprType& xpr, Index index) : Base(xpr,index) {}
391 
392  reference operator*() const { return (*mp_xpr).template subVector<Direction>(m_index); }
393  reference operator[](Index i) const { return (*mp_xpr).template subVector<Direction>(m_index+i); }
394  pointer operator->() const { return (*mp_xpr).template subVector<Direction>(m_index); }
395 };
396 
397 } // namespace internal
398 
399 
404 template<typename Derived>
406 {
407  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
408  return iterator(derived(), 0);
409 }
410 
412 template<typename Derived>
414 {
415  return cbegin();
416 }
417 
422 template<typename Derived>
424 {
425  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
426  return const_iterator(derived(), 0);
427 }
428 
433 template<typename Derived>
435 {
436  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
437  return iterator(derived(), size());
438 }
439 
441 template<typename Derived>
443 {
444  return cend();
445 }
446 
451 template<typename Derived>
453 {
454  EIGEN_STATIC_ASSERT_VECTOR_ONLY(Derived);
455  return const_iterator(derived(), size());
456 }
457 
458 } // namespace Eigen
random_access_iterator_type const_iterator
Definition: DenseBase.h:596
iterator begin()
Definition: StlIterators.h:405
iterator end()
Definition: StlIterators.h:434
const_iterator cbegin() const
Definition: StlIterators.h:423
const_iterator cend() const
Definition: StlIterators.h:452
random_access_iterator_type iterator
Definition: DenseBase.h:594
const unsigned int DirectAccessBit
Definition: Constants.h:155
Namespace containing all symbols from the Eigen library.
Definition: Core:134
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
const Product< MatrixDerived, PermutationDerived, AliasFreeProduct > operator*(const MatrixBase< MatrixDerived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:515