3#ifndef RPNX_ITERATOR_HPP
4#define RPNX_ITERATOR_HPP
26 using traits = std::iterator_traits< It >;
28 static_assert(std::is_base_of_v< std::forward_iterator_tag, typename traits::iterator_category >,
"rpnx::bounded_iterator requires a forward iterator");
59 if (m_current == m_last) [[unlikely]]
61 throw std::out_of_range(
"rpnx::bounded_iterator: dereference at end");
69 if (m_current == m_last) [[unlikely]]
71 throw std::out_of_range(
"rpnx::bounded_iterator: dereference at end");
73 return std::addressof(*m_current);
79 if (m_current == m_last) [[unlikely]]
81 throw std::out_of_range(
"rpnx::bounded_iterator: ++ at end");
98 return m_current == rhs.m_current;
104 return !(*
this == rhs);
122 template <
class It >
125 using traits = std::iterator_traits< It >;
127 static_assert(std::is_base_of_v< std::random_access_iterator_tag, typename traits::iterator_category >,
"rpnx::bounded_iterator requires a random-access iterator");
160 if (!(m_current >= m_first && m_current < m_last)) [[unlikely]]
162 throw std::out_of_range(
"rpnx::bounded_iterator: dereference out of range");
170 if (!(m_current >= m_first && m_current < m_last)) [[unlikely]]
172 throw std::out_of_range(
"rpnx::bounded_iterator: dereference out of range");
174 return std::addressof(*m_current);
180 if (!(m_current < m_last)) [[unlikely]]
182 throw std::out_of_range(
"rpnx::bounded_iterator: ++ past end");
199 if (!(m_current > m_first)) [[unlikely]]
201 throw std::out_of_range(
"rpnx::bounded_iterator: -- before begin");
220 auto max = std::distance(m_current, m_last);
221 if (n > max) [[unlikely]]
223 throw std::out_of_range(
"rpnx::bounded_iterator: += past end");
230 auto max = std::distance(m_first, m_current);
231 if (n < -max) [[unlikely]]
233 throw std::out_of_range(
"rpnx::bounded_iterator: += before begin");
243 return (*
this) += (-n);
277 return m_current - other.m_current;
283 return m_current == other.m_current;
289 return !(*
this == other);
295 return m_current < other.m_current;
301 return other < *
this;
306 return !(other < *
this);
311 return !(*
this < other);
321 template <
class It >
328 template <
class It >
Random-access iterator adapter with bidirectional range checks.
Definition iterator.hpp:124
constexpr bool operator!=(const bidirectional_bounded_iterator &other) const
Compares positions for inequality.
Definition iterator.hpp:287
constexpr reference operator*() const
Dereferences the current position.
Definition iterator.hpp:158
typename traits::difference_type difference_type
Signed iterator-distance type.
Definition iterator.hpp:137
constexpr reference operator[](difference_type n) const
Dereferences an offset position.
Definition iterator.hpp:269
typename traits::reference reference
Reference type returned by dereference.
Definition iterator.hpp:141
constexpr bidirectional_bounded_iterator operator+(difference_type n) const
Returns an iterator moved by an offset.
Definition iterator.hpp:247
friend constexpr bidirectional_bounded_iterator operator+(difference_type n, const bidirectional_bounded_iterator &it)
Returns an iterator moved by an offset.
Definition iterator.hpp:255
constexpr difference_type operator-(const bidirectional_bounded_iterator &other) const
Computes the distance from another iterator.
Definition iterator.hpp:275
typename traits::pointer pointer
Pointer type returned by member access.
Definition iterator.hpp:139
constexpr bool operator<(const bidirectional_bounded_iterator &other) const
Tests whether this position precedes another.
Definition iterator.hpp:293
typename traits::iterator_category iterator_category
Iterator category exposed by the wrapped iterator.
Definition iterator.hpp:133
typename traits::value_type value_type
Iterated value type.
Definition iterator.hpp:135
constexpr bidirectional_bounded_iterator & operator--()
Retreats one position.
Definition iterator.hpp:197
constexpr bidirectional_bounded_iterator & operator-=(difference_type n)
Moves backward by an offset with range checking.
Definition iterator.hpp:241
constexpr bidirectional_bounded_iterator operator++(int)
Advances one position.
Definition iterator.hpp:189
constexpr bool operator>=(const bidirectional_bounded_iterator &other) const
Tests whether this position does not precede another.
Definition iterator.hpp:309
constexpr bidirectional_bounded_iterator operator-(difference_type n) const
Returns an iterator moved backward by an offset.
Definition iterator.hpp:261
constexpr pointer operator->() const
Accesses the current element.
Definition iterator.hpp:168
constexpr bool operator==(const bidirectional_bounded_iterator &other) const
Compares positions for equality.
Definition iterator.hpp:281
bidirectional_bounded_iterator()=default
Constructs value-initialized current, first, and last iterators.
constexpr bidirectional_bounded_iterator operator--(int)
Retreats one position.
Definition iterator.hpp:208
constexpr bool operator<=(const bidirectional_bounded_iterator &other) const
Tests whether this position does not follow another.
Definition iterator.hpp:304
constexpr bidirectional_bounded_iterator(It current, It first, It last)
Constructs a bounded adapter over an underlying range.
Definition iterator.hpp:153
constexpr bidirectional_bounded_iterator & operator++()
Advances one position.
Definition iterator.hpp:178
constexpr bool operator>(const bidirectional_bounded_iterator &other) const
Tests whether this position follows another.
Definition iterator.hpp:299
constexpr bidirectional_bounded_iterator & operator+=(difference_type n)
Moves by an offset with range checking.
Definition iterator.hpp:216
It iterator_type
Wrapped iterator type.
Definition iterator.hpp:131
Forward iterator adapter that checks access against an end sentinel.
Definition iterator.hpp:25
constexpr reference operator*() const
Dereferences the current position.
Definition iterator.hpp:57
It iterator_type
Wrapped iterator type.
Definition iterator.hpp:32
bounded_iterator()=default
Constructs a value-initialized iterator and sentinel.
typename traits::value_type value_type
Iterated value type.
Definition iterator.hpp:36
constexpr bounded_iterator(It current, It last)
Constructs an adapter for a current position and end sentinel.
Definition iterator.hpp:52
constexpr bool operator!=(const bounded_iterator &rhs) const
Compares current positions for inequality.
Definition iterator.hpp:102
typename traits::difference_type difference_type
Signed iterator-distance type.
Definition iterator.hpp:38
constexpr pointer operator->() const
Accesses the current element.
Definition iterator.hpp:67
typename traits::pointer pointer
Pointer type returned by member access.
Definition iterator.hpp:40
constexpr bounded_iterator operator++(int)
Advances to the next position.
Definition iterator.hpp:88
constexpr bounded_iterator & operator++()
Advances to the next position.
Definition iterator.hpp:77
constexpr bool operator==(const bounded_iterator &rhs) const
Compares current positions.
Definition iterator.hpp:96
typename traits::iterator_category iterator_category
Iterator category exposed by the wrapped iterator.
Definition iterator.hpp:34
typename traits::reference reference
Reference type returned by dereference.
Definition iterator.hpp:42
Containers, iterator adapters, callable wrappers, and value utilities.
Definition annex.hpp:14
constexpr bidirectional_bounded_iterator< It > make_bounded_iterator(It it, It first, It last) noexcept
Creates a two-sided bounded random-access iterator.
Definition iterator.hpp:322