Created
February 12, 2018 09:06
-
-
Save deque-blog/aa5c8ebfc5ef669944496add0e3c5a9a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<typename ValueType> | |
class next_iterator | |
: public std::iterator<std::forward_iterator_tag, ValueType> | |
{ | |
public: | |
next_iterator(ValueType current) : m_current(std::move(current)) {} | |
next_iterator(next_iterator const&) = default; | |
friend bool operator== (next_iterator const& lhs, next_iterator const& rhs) | |
{ | |
return lhs.m_current == rhs.m_current; | |
} | |
friend bool operator!= (next_iterator const& lhs, next_iterator const& rhs) | |
{ | |
return lhs.m_current != rhs.m_current; | |
} | |
ValueType operator*() const | |
{ | |
return m_current; | |
} | |
next_iterator& operator++() | |
{ | |
++m_current; | |
return *this; | |
} | |
next_iterator operator++(int) | |
{ | |
next_iterator prev(*this); | |
operator++(); | |
return prev; | |
} | |
private: | |
ValueType m_current; | |
}; | |
template<typename ValueType> | |
class next_range | |
{ | |
public: | |
next_range(ValueType first, ValueType last) | |
: m_first(std::move(first)) | |
, m_last(std::move(last)) | |
{} | |
next_iterator<ValueType> begin() const | |
{ | |
return next_iterator<ValueType>{m_first}; | |
} | |
next_iterator<ValueType> end() const | |
{ | |
return next_iterator<ValueType>{m_last}; | |
} | |
private: | |
ValueType m_first; | |
ValueType m_last; | |
}; | |
template<typename ValueType> | |
next_range<ValueType> range(ValueType const& first, ValueType const& last) | |
{ | |
return next_range<ValueType>{first, last}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment