| 1 |
/// \file |
| 2 |
// Range v3 library |
| 3 |
// |
| 4 |
// Copyright Casey Carter 2018-present |
| 5 |
// |
| 6 |
// Use, modification and distribution is subject to the |
| 7 |
// Boost Software License, Version 1.0. (See accompanying |
| 8 |
// file LICENSE_1_0.txt or copy at |
| 9 |
// http://www.boost.org/LICENSE_1_0.txt) |
| 10 |
// |
| 11 |
// Project home: https://github.com/ericniebler/range-v3 |
| 12 |
// |
| 13 |
|
| 14 |
#ifndef RANGES_V3_VIEW_ENUMERATE_HPP |
| 15 |
#define RANGES_V3_VIEW_ENUMERATE_HPP |
| 16 |
|
| 17 |
#include <range/v3/core.hpp> |
| 18 |
#include <range/v3/iterator/unreachable_sentinel.hpp> |
| 19 |
#include <range/v3/view/all.hpp> |
| 20 |
#include <range/v3/view/facade.hpp> |
| 21 |
#include <range/v3/view/zip.hpp> |
| 22 |
|
| 23 |
#include <range/v3/detail/prologue.hpp> |
| 24 |
|
| 25 |
namespace ranges |
| 26 |
{ |
| 27 |
/// \cond |
| 28 |
namespace detail |
| 29 |
{ |
| 30 |
// Counts from zero up. |
| 31 |
// See https://github.com/ericniebler/range-v3/issues/1141 |
| 32 |
// for why we don't just use iota_view. |
| 33 |
template<typename Size, typename Diff> |
| 34 |
struct index_view : view_facade<index_view<Size, Diff>, infinite> |
| 35 |
{ |
| 36 |
private: |
| 37 |
friend range_access; |
| 38 |
|
| 39 |
struct cursor |
| 40 |
{ |
| 41 |
using difference_type = Diff; |
| 42 |
|
| 43 |
private: |
| 44 |
friend range_access; |
| 45 |
Size index_{0}; |
| 46 |
|
| 47 |
Size read() const |
| 48 |
{ |
| 49 |
return index_; |
| 50 |
} |
| 51 |
void next() |
| 52 |
{ |
| 53 |
++index_; |
| 54 |
} |
| 55 |
bool equal(cursor const & that) const |
| 56 |
{ |
| 57 |
return that.index_ == index_; |
| 58 |
} |
| 59 |
void prev() |
| 60 |
{ |
| 61 |
--index_; |
| 62 |
} |
| 63 |
void advance(Diff n) |
| 64 |
{ |
| 65 |
index_ += static_cast<Size>(n); |
| 66 |
} |
| 67 |
Diff distance_to(cursor const & that) const |
| 68 |
{ |
| 69 |
return static_cast<Diff>(static_cast<Diff>(that.index_) - |
| 70 |
static_cast<Diff>(index_)); |
| 71 |
} |
| 72 |
|
| 73 |
public: |
| 74 |
cursor() = default; |
| 75 |
}; |
| 76 |
cursor begin_cursor() const |
| 77 |
{ |
| 78 |
return cursor{}; |
| 79 |
} |
| 80 |
unreachable_sentinel_t end_cursor() const |
| 81 |
{ |
| 82 |
return unreachable; |
| 83 |
} |
| 84 |
|
| 85 |
public: |
| 86 |
index_view() = default; |
| 87 |
}; |
| 88 |
|
| 89 |
} // namespace detail |
| 90 |
|
| 91 |
template<typename Size, typename Diff> |
| 92 |
RANGES_INLINE_VAR constexpr bool enable_borrowed_range<detail::index_view<Size, Diff>> = |
| 93 |
true; |
| 94 |
|
| 95 |
/// \endcond |
| 96 |
/// \addtogroup group-views |
| 97 |
/// @{ |
| 98 |
namespace views |
| 99 |
{ |
| 100 |
/// Lazily pairs each element in a source range with |
| 101 |
/// its corresponding index. |
| 102 |
struct enumerate_fn |
| 103 |
{ |
| 104 |
template(typename Rng)( |
| 105 |
requires viewable_range<Rng>) |
| 106 |
auto operator()(Rng && rng) const |
| 107 |
{ |
| 108 |
using D = range_difference_t<Rng>; |
| 109 |
using S = detail::iter_size_t<iterator_t<Rng>>; |
| 110 |
return zip(detail::index_view<S, D>(), all(static_cast<Rng &&>(rng))); |
| 111 |
} |
| 112 |
}; |
| 113 |
|
| 114 |
/// \relates enumerate_fn |
| 115 |
/// \ingroup group-views |
| 116 |
RANGES_INLINE_VARIABLE(view_closure<enumerate_fn>, enumerate) |
| 117 |
} // namespace views |
| 118 |
/// @} |
| 119 |
} // namespace ranges |
| 120 |
|
| 121 |
#include <range/v3/detail/epilogue.hpp> |
| 122 |
|
| 123 |
#endif |