Return sentinel type from xrange::end()

This commit is contained in:
Kp 2022-10-09 23:15:20 +00:00
parent b5775f1680
commit dded433d2b
2 changed files with 18 additions and 3 deletions

View file

@ -130,6 +130,14 @@ public:
m_idx(i) m_idx(i)
{ {
} }
/* This is a temporary constructor to facilitate conversion to sentinel
* usage in calling algorithms.
*/
template <typename end_index_type>
constexpr xrange_iterator(xrange_endpoint<end_index_type, false> i) :
m_idx(i)
{
}
index_type operator*() const index_type operator*() const
{ {
return m_idx; return m_idx;
@ -152,6 +160,12 @@ public:
} }
[[nodiscard]] [[nodiscard]]
constexpr bool operator==(const xrange_iterator &i) const = default; constexpr bool operator==(const xrange_iterator &i) const = default;
template <typename end_index_type>
[[nodiscard]]
constexpr bool operator==(const xrange_endpoint<end_index_type, false> &i) const
{
return m_idx == i.value;
}
}; };
/* This provides an approximation of the functionality of the Python2 /* This provides an approximation of the functionality of the Python2
@ -237,9 +251,9 @@ public:
{ {
return iterator(static_cast<const begin_type &>(*this)); return iterator(static_cast<const begin_type &>(*this));
} }
iterator end() const end_type end() const
{ {
return iterator(static_cast<const end_type &>(*this)); return *this;
} }
}; };

View file

@ -131,5 +131,6 @@ BOOST_AUTO_TEST_CASE(xrange_iter_values)
{ {
auto &&r = xrange(2u); auto &&r = xrange(2u);
BOOST_TEST(*r.begin() == 0); BOOST_TEST(*r.begin() == 0);
BOOST_TEST(*r.end() == 2); BOOST_TEST(*std::next(r.begin()) == 1);
BOOST_TEST(*std::next(r.begin(), 2) == 2);
} }