Expression Templates Library (ETL)
iterator.hpp
Go to the documentation of this file.
1 //=======================================================================
2 // Copyright (c) 2014-2023 Baptiste Wicht
3 // Distributed under the terms of the MIT License.
4 // (See accompanying file LICENSE or copy at
5 // http://opensource.org/licenses/MIT)
6 //=======================================================================
7 
13 #pragma once
14 
15 #include <iterator>
16 
17 namespace etl {
18 
23 template <typename Expr>
24 struct iterator {
25 private:
26  Expr* expr;
27  size_t i;
28 
29 public:
30  using iterator_category = std::random_access_iterator_tag;
32  using reference = decltype(std::declval<Expr>()[i]);
33  using pointer = std::add_pointer_t<decltype(std::declval<Expr>()[i])>;
34  using difference_type = size_t;
35 
41  iterator(Expr& expr, size_t i) : expr(&expr), i(i) {}
42 
48  return (*expr)[i];
49  }
50 
57  return (*expr)[i + n];
58  }
59 
64  auto operator-> () {
65  return &(*expr)[i];
66  }
67 
73  --i;
74  return *this;
75  }
76 
82  iterator prev(*this);
83  --i;
84  return prev;
85  }
86 
92  ++i;
93  return *this;
94  }
95 
101  iterator prev(*this);
102  ++i;
103  return prev;
104  }
105 
112  i += n;
113  return *this;
114  }
115 
122  i -= n;
123  return *this;
124  }
125 
132  iterator it(*this);
133  it += n;
134  return it;
135  }
136 
143  iterator it(*this);
144  it -= n;
145  return it;
146  }
147 
154  return i - it.i;
155  }
156 
162  bool operator==(const iterator& other) const {
163  return expr == other.expr && i == other.i;
164  }
165 
171  bool operator!=(const iterator& other) const {
172  return !(*this == other);
173  }
174 
180  bool operator>(const iterator& other) const {
181  return i > other.i;
182  }
183 
189  bool operator>=(const iterator& other) const {
190  return i >= other.i;
191  }
192 
198  bool operator<(const iterator& other) const {
199  return i < other.i;
200  }
201 
207  bool operator<=(const iterator& other) const {
208  return i <= other.i;
209  }
210 };
211 
212 } //end of namespace etl
iterator & operator-=(difference_type n)
Back away the iterator n positions.
Definition: iterator.hpp:121
iterator operator++(int)
Postincrement the iterator.
Definition: iterator.hpp:100
auto operator->()
Dereference the iterator to get the current value.
Definition: iterator.hpp:64
iterator operator-(difference_type n)
Creates a new iterator poiting to the current position minus n.
Definition: iterator.hpp:142
difference_type operator-(const iterator &it)
Computes the difference between two iterators.
Definition: iterator.hpp:153
iterator operator+(difference_type n)
Creates a new iterator poiting to the current position plus n.
Definition: iterator.hpp:131
reference operator*()
Dereference the iterator to get the current value.
Definition: iterator.hpp:47
decltype(std::declval< Expr >()[i]) reference
The type of reference.
Definition: iterator.hpp:32
size_t difference_type
The type used for subtracting two iterators.
Definition: iterator.hpp:34
bool operator!=(const iterator &other) const
Compare two iterators.
Definition: iterator.hpp:171
bool operator==(const iterator &other) const
Compare two iterators.
Definition: iterator.hpp:162
reference operator[](difference_type n)
Dereferences the iterator at n forward position.
Definition: iterator.hpp:56
bool operator<=(const iterator &other) const
Compare two iterators.
Definition: iterator.hpp:207
iterator & operator+=(difference_type n)
Advances the iterator n positions.
Definition: iterator.hpp:111
iterator operator--(int)
Postdecrement the iterator.
Definition: iterator.hpp:81
Root namespace for the ETL library.
Definition: adapter.hpp:15
bool operator<(const iterator &other) const
Compare two iterators.
Definition: iterator.hpp:198
Configurable iterator for ETL expressions.
Definition: iterator.hpp:24
bool operator>=(const iterator &other) const
Compare two iterators.
Definition: iterator.hpp:189
iterator(Expr &expr, size_t i)
Construct a new iterator.
Definition: iterator.hpp:41
iterator & operator++()
Preincrement the iterator.
Definition: iterator.hpp:91
typename decay_traits< E >::value_type value_t
Traits to extract the value type out of an ETL type.
Definition: tmp.hpp:81
std::add_pointer_t< decltype(std::declval< Expr >()[i])> pointer
The type of pointer.
Definition: iterator.hpp:33
iterator & operator--()
Predecrement the iterator.
Definition: iterator.hpp:72
value_t< Expr > value_type
The value type.
Definition: iterator.hpp:31
bool operator>(const iterator &other) const
Compare two iterators.
Definition: iterator.hpp:180