From 0af234a3293c632479ed708388fd73547851a9fd Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Thu, 21 Oct 2021 10:52:08 +0200 Subject: [PATCH] src: Implement the `enumerate()` range filter --- src/ranges.hh | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/src/ranges.hh b/src/ranges.hh index bd3edd9b..3480cab9 100644 --- a/src/ranges.hh +++ b/src/ranges.hh @@ -5,6 +5,7 @@ #include #include #include +#include #include "constexpr_utils.hh" @@ -155,6 +156,53 @@ inline auto filter(Filter f) }); } +template +struct EnumerateView +{ + using RangeIt = IteratorOf; + + struct Iterator : std::iterator::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>{std::forward(range)}; + }); +} + template struct TransformView {