src: Implement the enumerate()
range filter
This commit is contained in:
parent
689553c2e9
commit
0af234a329
|
@ -5,6 +5,7 @@
|
|||
#include <utility>
|
||||
#include <iterator>
|
||||
#include <numeric>
|
||||
#include <tuple>
|
||||
|
||||
#include "constexpr_utils.hh"
|
||||
|
||||
|
@ -155,6 +156,53 @@ inline auto filter(Filter f)
|
|||
});
|
||||
}
|
||||
|
||||
template<typename Range>
|
||||
struct EnumerateView
|
||||
{
|
||||
using RangeIt = IteratorOf<Range>;
|
||||
|
||||
struct Iterator : std::iterator<std::forward_iterator_tag,
|
||||
typename std::iterator_traits<RangeIt>::value_type>
|
||||
{
|
||||
Iterator(size_t index, RangeIt it, RangeIt end)
|
||||
: m_index{index}, m_it{std::move(it)}, m_end{std::move(end)} {}
|
||||
|
||||
decltype(auto) operator*() { return std::make_tuple(m_index, *m_it); }
|
||||
Iterator& operator++() { ++m_index; ++m_it; return *this; }
|
||||
Iterator operator++(int) { auto copy = *this; ++(*this); return copy; }
|
||||
|
||||
friend bool operator==(const Iterator& lhs, const Iterator& rhs)
|
||||
{
|
||||
return lhs.m_index == rhs.m_index and lhs.m_it == rhs.m_it;
|
||||
}
|
||||
|
||||
friend bool operator!=(const Iterator& lhs, const Iterator& rhs)
|
||||
{
|
||||
return not (lhs == rhs);
|
||||
}
|
||||
|
||||
const RangeIt& base() const { return m_it; }
|
||||
|
||||
private:
|
||||
size_t m_index;
|
||||
RangeIt m_it;
|
||||
RangeIt m_end;
|
||||
};
|
||||
|
||||
Iterator begin() const { return {0, std::begin(m_range), std::end(m_range)}; }
|
||||
Iterator end() const { return {std::size(m_range), std::end(m_range), std::end(m_range)}; }
|
||||
|
||||
Range m_range;
|
||||
};
|
||||
|
||||
inline auto enumerate()
|
||||
{
|
||||
return make_view_factory([](auto&& range) {
|
||||
using Range = decltype(range);
|
||||
return EnumerateView<decay_range<Range>>{std::forward<Range>(range)};
|
||||
});
|
||||
}
|
||||
|
||||
template<typename Range, typename Transform>
|
||||
struct TransformView
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user