/// \file // Range v3 library // // Copyright Casey Carter 2018-present // // Use, modification and distribution is subject to the // Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) // // Project home: https://github.com/ericniebler/range-v3 // #ifndef RANGES_V3_VIEW_ENUMERATE_HPP #define RANGES_V3_VIEW_ENUMERATE_HPP #include #include #include #include #include namespace ranges { /// \cond namespace detail { // Counts from zero up. // See https://github.com/ericniebler/range-v3/issues/1141 // for why we don't just use iota_view. template struct index_view : view_facade, infinite> { private: friend range_access; struct cursor { using difference_type = Diff; private: friend range_access; Size index_{0}; Size read() const { return index_; } void next() { ++index_; } bool equal(cursor const & that) const { return that.index_ == index_; } void prev() { --index_; } void advance(Diff n) { index_ += static_cast(n); } Diff distance_to(cursor const & that) const { return static_cast(static_cast(that.index_) - static_cast(index_)); } public: cursor() = default; }; cursor begin_cursor() const { return cursor{}; } unreachable_sentinel_t end_cursor() const { return unreachable; } public: index_view() = default; }; } // namespace detail /// \endcond /// \addtogroup group-views /// @{ namespace views { /// Lazily pairs each element in a source range with /// its corresponding index. struct enumerate_fn { template auto CPP_fun(operator())(Rng && rng)(const requires viewable_range) { using D = range_difference_t; using S = detail::iter_size_t>; return zip(detail::index_view(), all(static_cast(rng))); } }; /// \relates enumerate_fn /// \ingroup group-views RANGES_INLINE_VARIABLE(view, enumerate) } // namespace views /// @} } // namespace ranges #endif