tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
reverse_iterator.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_ITERATOR_REVERSE_ITERATOR_HPP
5#define TETL_ITERATOR_REVERSE_ITERATOR_HPP
6
7#include <etl/_iterator/iterator_traits.hpp>
8#include <etl/_memory/addressof.hpp>
9
10namespace etl {
11
12/// reverse_iterator is an iterator adaptor that reverses the direction
13/// of a given iterator. In other words, when provided with a bidirectional
14/// iterator, `reverse_iterator` produces a new iterator that moves from the end
15/// to the beginning of the sequence defined by the underlying bidirectional
16/// iterator. This is the iterator returned by member functions `rbegin()` and
17/// `rend()` of the standard library containers.
18///
19/// https://en.cppreference.com/w/cpp/iterator/reverse_iterator
20///
21/// \ingroup iterator
22template <typename Iter>
23struct reverse_iterator {
24 using iterator_type = Iter;
25 using value_type = typename iterator_traits<Iter>::value_type;
26 using difference_type = typename etl::iterator_traits<Iter>::difference_type;
27 using reference = typename etl::iterator_traits<Iter>::reference;
28 using pointer = typename etl::iterator_traits<Iter>::pointer;
29 using iterator_category = typename etl::iterator_traits<Iter>::iterator_category;
30
31 /// Constructs a new iterator adaptor.
32 ///
33 /// Default constructor. The underlying iterator is
34 /// value-initialized. Operations on the resulting iterator have defined
35 /// behavior if and only if the corresponding operations on a
36 /// value-initialized Iterator also have defined behavior.
37 constexpr reverse_iterator()
38 : _current()
39 {
40 }
41
42 /// Constructs a new iterator adaptor.
43 ///
44 /// The underlying iterator is initialized with x.
45 constexpr explicit reverse_iterator(Iter x)
46 : _current(x)
47 {
48 }
49
50 /// Constructs a new iterator adaptor.
51 ///
52 /// The underlying iterator is initialized with that of other.
53 template <typename Other>
54 constexpr reverse_iterator(reverse_iterator<Other> const& other)
55 : _current(other.base())
56 {
57 }
58
59 /// The underlying iterator is assigned the value of the underlying
60 /// iterator of other, i.e. other.base().
61 template <typename Other>
62 constexpr auto operator=(reverse_iterator<Other> const& other) -> reverse_iterator&
63 {
64 _current = other.base();
65 return *this;
66 }
67
68 /// Returns the underlying base iterator.
69 [[nodiscard]] constexpr auto base() const -> Iter
70 {
71 return _current;
72 }
73
74 /// Returns a reference to the element previous to current.
75 constexpr auto operator*() const -> reference
76 {
77 auto tmp = _current;
78 return *--tmp;
79 }
80
81 /// Returns a pointer to the element previous to current.
82 constexpr auto operator->() const -> pointer
83 {
84 return etl::addressof(operator*());
85 }
86
87 /// Pre-increments by one respectively.
88 constexpr auto operator++() -> reverse_iterator&
89 {
90 --_current;
91 return *this;
92 }
93
94 /// Pre-increments by one respectively.
95 constexpr auto operator++(int) -> reverse_iterator
96 {
97 auto tmp(*this);
98 --_current;
99 return tmp;
100 }
101
102 /// Pre-decrements by one respectively.
103 constexpr auto operator--() -> reverse_iterator&
104 {
105 ++_current;
106 return *this;
107 }
108
109 /// Pre-decrements by one respectively.
110 constexpr auto operator--(int) -> reverse_iterator
111 {
112 auto tmp(*this);
113 ++_current;
114 return tmp;
115 }
116
117 /// Returns an iterator which is advanced by n positions.
118 constexpr auto operator+(difference_type n) const -> reverse_iterator
119 {
120 return reverse_iterator(_current - n);
121 }
122
123 /// Advances the iterator by n or -n positions respectively.
124 constexpr auto operator+=(difference_type n) -> reverse_iterator&
125 {
126 _current -= n;
127 return *this;
128 }
129
130 /// Returns an iterator which is advanced by -n positions.
131 constexpr auto operator-(difference_type n) const -> reverse_iterator
132 {
133 return reverse_iterator(_current + n);
134 }
135
136 /// Advances the iterator by n or -n positions respectively.
137 constexpr auto operator-=(difference_type n) -> reverse_iterator&
138 {
139 _current += n;
140 return *this;
141 }
142
143 /// Returns a reference to the element at specified relative location.
144 constexpr auto operator[](difference_type n) const -> reference
145 {
146 return *(*this + n);
147 }
148
149private:
150 Iter _current;
151};
152
153/// Convenience function template that constructs a etl::reverse_iterator
154/// for the given iterator i (which must be a LegacyBidirectionalIterator) with
155/// the type deduced from the type of the argument.
156template <typename Iter>
157[[nodiscard]] constexpr auto make_reverse_iterator(Iter i) noexcept -> etl::reverse_iterator<Iter>
158{
159 return etl::reverse_iterator<Iter>(i);
160}
161
162/// Compares the underlying iterators. Inverse comparisons are applied in
163/// order to take into account that the iterator order is reversed.
164template <typename Iter1, typename Iter2>
165[[nodiscard]] constexpr auto
166operator==(etl::reverse_iterator<Iter1> const& lhs, etl::reverse_iterator<Iter2> const& rhs) -> bool
167{
168 return lhs.base() == rhs.base();
169}
170
171/// Compares the underlying iterators. Inverse comparisons are applied in
172/// order to take into account that the iterator order is reversed.
173template <typename Iter1, typename Iter2>
174[[nodiscard]] constexpr auto
175operator!=(etl::reverse_iterator<Iter1> const& lhs, etl::reverse_iterator<Iter2> const& rhs) -> bool
176{
177 return lhs.base() != rhs.base();
178}
179
180/// Compares the underlying iterators. Inverse comparisons are applied in
181/// order to take into account that the iterator order is reversed.
182template <typename Iter1, typename Iter2>
183[[nodiscard]] constexpr auto operator<(etl::reverse_iterator<Iter1> const& lhs, etl::reverse_iterator<Iter2> const& rhs)
184 -> bool
185{
186 return lhs.base() < rhs.base();
187}
188
189/// Compares the underlying iterators. Inverse comparisons are applied in
190/// order to take into account that the iterator order is reversed.
191template <typename Iter1, typename Iter2>
192[[nodiscard]] constexpr auto
193operator<=(etl::reverse_iterator<Iter1> const& lhs, etl::reverse_iterator<Iter2> const& rhs) -> bool
194{
195 return lhs.base() <= rhs.base();
196}
197
198/// Compares the underlying iterators. Inverse comparisons are applied in
199/// order to take into account that the iterator order is reversed.
200template <typename Iter1, typename Iter2>
201[[nodiscard]] constexpr auto operator>(etl::reverse_iterator<Iter1> const& lhs, etl::reverse_iterator<Iter2> const& rhs)
202 -> bool
203{
204 return lhs.base() > rhs.base();
205}
206
207/// Compares the underlying iterators. Inverse comparisons are applied in
208/// order to take into account that the iterator order is reversed.
209template <typename Iter1, typename Iter2>
210[[nodiscard]] constexpr auto
211operator>=(etl::reverse_iterator<Iter1> const& lhs, etl::reverse_iterator<Iter2> const& rhs) -> bool
212{
213 return lhs.base() >= rhs.base();
214}
215
216/// Returns the iterator it incremented by n.
217template <typename Iter>
218[[nodiscard]] constexpr auto operator+(
219 typename reverse_iterator<Iter>::difference_type n,
220 reverse_iterator<Iter> const& it
221) noexcept(noexcept(it.base() - n)) -> reverse_iterator<Iter>
222{
223 return reverse_iterator<Iter>(it.base() - n);
224}
225
226/// Returns the distance between two iterator adaptors.
227template <typename Iterator1, typename Iterator2>
228constexpr auto operator-(reverse_iterator<Iterator1> const& lhs, reverse_iterator<Iterator2> const& rhs) noexcept(
229 noexcept(rhs.base() - lhs.base())
230) -> decltype(rhs.base() - lhs.base())
231{
232 return rhs.base() - lhs.base();
233}
234
235} // namespace etl
236
237#endif // TETL_ITERATOR_REVERSE_ITERATOR_HPP
Definition adjacent_find.hpp:9
constexpr auto operator-(reverse_iterator< Iterator1 > const &lhs, reverse_iterator< Iterator2 > const &rhs) noexcept(noexcept(rhs.base() - lhs.base())) -> decltype(rhs.base() - lhs.base())
Returns the distance between two iterator adaptors.
Definition reverse_iterator.hpp:228
constexpr auto operator+(typename reverse_iterator< Iter >::difference_type n, reverse_iterator< Iter > const &it) noexcept(noexcept(it.base() - n)) -> reverse_iterator< Iter >
Returns the iterator it incremented by n.
Definition reverse_iterator.hpp:218
constexpr auto operator<(etl::reverse_iterator< Iter1 > const &lhs, etl::reverse_iterator< Iter2 > const &rhs) -> bool
Compares the underlying iterators. Inverse comparisons are applied in order to take into account that...
Definition reverse_iterator.hpp:183
constexpr auto operator==(etl::reverse_iterator< Iter1 > const &lhs, etl::reverse_iterator< Iter2 > const &rhs) -> bool
Compares the underlying iterators. Inverse comparisons are applied in order to take into account that...
Definition reverse_iterator.hpp:166
constexpr auto operator!=(etl::reverse_iterator< Iter1 > const &lhs, etl::reverse_iterator< Iter2 > const &rhs) -> bool
Compares the underlying iterators. Inverse comparisons are applied in order to take into account that...
Definition reverse_iterator.hpp:175
constexpr auto operator<=(etl::reverse_iterator< Iter1 > const &lhs, etl::reverse_iterator< Iter2 > const &rhs) -> bool
Compares the underlying iterators. Inverse comparisons are applied in order to take into account that...
Definition reverse_iterator.hpp:193
constexpr auto operator>(etl::reverse_iterator< Iter1 > const &lhs, etl::reverse_iterator< Iter2 > const &rhs) -> bool
Compares the underlying iterators. Inverse comparisons are applied in order to take into account that...
Definition reverse_iterator.hpp:201
constexpr auto operator>=(etl::reverse_iterator< Iter1 > const &lhs, etl::reverse_iterator< Iter2 > const &rhs) -> bool
Compares the underlying iterators. Inverse comparisons are applied in order to take into account that...
Definition reverse_iterator.hpp:211
constexpr auto make_reverse_iterator(Iter i) noexcept -> etl::reverse_iterator< Iter >
Convenience function template that constructs a etl::reverse_iterator for the given iterator i (which...
Definition reverse_iterator.hpp:157
iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIt...
Definition iterator_traits.hpp:48
constexpr reverse_iterator(Iter x)
Constructs a new iterator adaptor.
Definition reverse_iterator.hpp:45
constexpr auto operator+(difference_type n) const -> reverse_iterator
Returns an iterator which is advanced by n positions.
Definition reverse_iterator.hpp:118
constexpr auto operator--() -> reverse_iterator &
Pre-decrements by one respectively.
Definition reverse_iterator.hpp:103
constexpr reverse_iterator()
Constructs a new iterator adaptor.
Definition reverse_iterator.hpp:37
constexpr auto operator+=(difference_type n) -> reverse_iterator &
Advances the iterator by n or -n positions respectively.
Definition reverse_iterator.hpp:124
constexpr auto operator[](difference_type n) const -> reference
Returns a reference to the element at specified relative location.
Definition reverse_iterator.hpp:144
constexpr auto operator++(int) -> reverse_iterator
Pre-increments by one respectively.
Definition reverse_iterator.hpp:95
constexpr reverse_iterator(reverse_iterator< Other > const &other)
Constructs a new iterator adaptor.
Definition reverse_iterator.hpp:54
constexpr auto operator++() -> reverse_iterator &
Pre-increments by one respectively.
Definition reverse_iterator.hpp:88
constexpr auto operator->() const -> pointer
Returns a pointer to the element previous to current.
Definition reverse_iterator.hpp:82
constexpr auto operator-(difference_type n) const -> reverse_iterator
Returns an iterator which is advanced by -n positions.
Definition reverse_iterator.hpp:131
constexpr auto base() const -> Iter
Returns the underlying base iterator.
Definition reverse_iterator.hpp:69
constexpr auto operator=(reverse_iterator< Other > const &other) -> reverse_iterator &
The underlying iterator is assigned the value of the underlying iterator of other,...
Definition reverse_iterator.hpp:62
constexpr auto operator--(int) -> reverse_iterator
Pre-decrements by one respectively.
Definition reverse_iterator.hpp:110
constexpr auto operator*() const -> reference
Returns a reference to the element previous to current.
Definition reverse_iterator.hpp:75
constexpr auto operator-=(difference_type n) -> reverse_iterator &
Advances the iterator by n or -n positions respectively.
Definition reverse_iterator.hpp:137