File indexing completed on 2025-01-18 10:09:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
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
0028 namespace detail
0029 {
0030
0031
0032
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 }
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
0096
0097
0098 namespace views
0099 {
0100
0101
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
0115
0116 RANGES_INLINE_VARIABLE(view_closure<enumerate_fn>, enumerate)
0117 }
0118
0119 }
0120
0121 #include <range/v3/detail/epilogue.hpp>
0122
0123 #endif