Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:09:55

0001 /// \file
0002 // Range v3 library
0003 //
0004 //  Copyright Casey Carter 2018-present
0005 //
0006 //  Use, modification and distribution is subject to the
0007 //  Boost Software License, Version 1.0. (See accompanying
0008 //  file LICENSE_1_0.txt or copy at
0009 //  http://www.boost.org/LICENSE_1_0.txt)
0010 //
0011 // Project home: https://github.com/ericniebler/range-v3
0012 //
0013 
0014 #ifndef RANGES_V3_VIEW_ENUMERATE_HPP
0015 #define RANGES_V3_VIEW_ENUMERATE_HPP
0016 
0017 #include <range/v3/core.hpp>
0018 #include <range/v3/iterator/unreachable_sentinel.hpp>
0019 #include <range/v3/view/all.hpp>
0020 #include <range/v3/view/facade.hpp>
0021 #include <range/v3/view/zip.hpp>
0022 
0023 #include <range/v3/detail/prologue.hpp>
0024 
0025 namespace ranges
0026 {
0027     /// \cond
0028     namespace detail
0029     {
0030         // Counts from zero up.
0031         // See https://github.com/ericniebler/range-v3/issues/1141
0032         // for why we don't just use iota_view.
0033         template<typename Size, typename Diff>
0034         struct index_view : view_facade<index_view<Size, Diff>, infinite>
0035         {
0036         private:
0037             friend range_access;
0038 
0039             struct cursor
0040             {
0041                 using difference_type = Diff;
0042 
0043             private:
0044                 friend range_access;
0045                 Size index_{0};
0046 
0047                 Size read() const
0048                 {
0049                     return index_;
0050                 }
0051                 void next()
0052                 {
0053                     ++index_;
0054                 }
0055                 bool equal(cursor const & that) const
0056                 {
0057                     return that.index_ == index_;
0058                 }
0059                 void prev()
0060                 {
0061                     --index_;
0062                 }
0063                 void advance(Diff n)
0064                 {
0065                     index_ += static_cast<Size>(n);
0066                 }
0067                 Diff distance_to(cursor const & that) const
0068                 {
0069                     return static_cast<Diff>(static_cast<Diff>(that.index_) -
0070                                              static_cast<Diff>(index_));
0071                 }
0072 
0073             public:
0074                 cursor() = default;
0075             };
0076             cursor begin_cursor() const
0077             {
0078                 return cursor{};
0079             }
0080             unreachable_sentinel_t end_cursor() const
0081             {
0082                 return unreachable;
0083             }
0084 
0085         public:
0086             index_view() = default;
0087         };
0088 
0089     } // namespace detail
0090 
0091     template<typename Size, typename Diff>
0092     RANGES_INLINE_VAR constexpr bool enable_borrowed_range<detail::index_view<Size, Diff>> =
0093         true;
0094 
0095     /// \endcond
0096     /// \addtogroup group-views
0097     /// @{
0098     namespace views
0099     {
0100         /// Lazily pairs each element in a source range with
0101         /// its corresponding index.
0102         struct enumerate_fn
0103         {
0104             template(typename Rng)(
0105                 requires viewable_range<Rng>)
0106             auto operator()(Rng && rng) const
0107             {
0108                 using D = range_difference_t<Rng>;
0109                 using S = detail::iter_size_t<iterator_t<Rng>>;
0110                 return zip(detail::index_view<S, D>(), all(static_cast<Rng &&>(rng)));
0111             }
0112         };
0113 
0114         /// \relates enumerate_fn
0115         /// \ingroup group-views
0116         RANGES_INLINE_VARIABLE(view_closure<enumerate_fn>, enumerate)
0117     } // namespace views
0118     /// @}
0119 } // namespace ranges
0120 
0121 #include <range/v3/detail/epilogue.hpp>
0122 
0123 #endif