You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

169 lines
5.1KB

  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2014-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. #ifndef RANGES_V3_VIEW_REVERSE_HPP
  14. #define RANGES_V3_VIEW_REVERSE_HPP
  15. #include <iterator>
  16. #include <utility>
  17. #include <meta/meta.hpp>
  18. #include <range/v3/range_fwd.hpp>
  19. #include <range/v3/iterator/operations.hpp>
  20. #include <range/v3/iterator/reverse_iterator.hpp>
  21. #include <range/v3/range/access.hpp>
  22. #include <range/v3/range/primitives.hpp>
  23. #include <range/v3/range/traits.hpp>
  24. #include <range/v3/utility/box.hpp>
  25. #include <range/v3/utility/get.hpp>
  26. #include <range/v3/utility/optional.hpp>
  27. #include <range/v3/utility/static_const.hpp>
  28. #include <range/v3/view/adaptor.hpp>
  29. #include <range/v3/view/all.hpp>
  30. #include <range/v3/view/view.hpp>
  31. namespace ranges
  32. {
  33. /// \addtogroup group-views
  34. /// @{
  35. template<typename Rng>
  36. struct RANGES_EMPTY_BASES reverse_view
  37. : view_interface<reverse_view<Rng>, range_cardinality<Rng>::value>
  38. , private detail::non_propagating_cache<iterator_t<Rng>, reverse_view<Rng>,
  39. !common_range<Rng>>
  40. {
  41. private:
  42. CPP_assert(bidirectional_range<Rng>);
  43. Rng rng_;
  44. constexpr reverse_iterator<iterator_t<Rng>> begin_(std::true_type)
  45. {
  46. return make_reverse_iterator(ranges::end(rng_));
  47. }
  48. constexpr reverse_iterator<iterator_t<Rng>> begin_(std::false_type)
  49. {
  50. using cache_t =
  51. detail::non_propagating_cache<iterator_t<Rng>, reverse_view<Rng>>;
  52. auto & end_ = static_cast<cache_t &>(*this);
  53. if(!end_)
  54. end_ = ranges::next(ranges::begin(rng_), ranges::end(rng_));
  55. return make_reverse_iterator(*end_);
  56. }
  57. public:
  58. reverse_view() = default;
  59. explicit constexpr reverse_view(Rng rng)
  60. : rng_(detail::move(rng))
  61. {}
  62. Rng base() const
  63. {
  64. return rng_;
  65. }
  66. constexpr reverse_iterator<iterator_t<Rng>> begin()
  67. {
  68. return begin_(meta::bool_<(bool)common_range<Rng>>{});
  69. }
  70. template<bool Const = true>
  71. constexpr auto begin() const
  72. -> CPP_ret(reverse_iterator<iterator_t<meta::const_if_c<Const, Rng>>>)( //
  73. requires Const && common_range<meta::const_if_c<Const, Rng>>)
  74. {
  75. return make_reverse_iterator(ranges::end(rng_));
  76. }
  77. constexpr reverse_iterator<iterator_t<Rng>> end()
  78. {
  79. return make_reverse_iterator(ranges::begin(rng_));
  80. }
  81. template<bool Const = true>
  82. constexpr auto end() const
  83. -> CPP_ret(reverse_iterator<iterator_t<meta::const_if_c<Const, Rng>>>)( //
  84. requires Const && common_range<meta::const_if_c<Const, Rng>>)
  85. {
  86. return make_reverse_iterator(ranges::begin(rng_));
  87. }
  88. CPP_member
  89. constexpr auto CPP_fun(size)()(requires sized_range<Rng>)
  90. {
  91. return ranges::size(rng_);
  92. }
  93. CPP_member
  94. constexpr auto CPP_fun(size)()(const requires sized_range<Rng const>)
  95. {
  96. return ranges::size(rng_);
  97. }
  98. };
  99. template<typename Rng>
  100. struct reverse_view<reverse_view<Rng>> : Rng
  101. {
  102. CPP_assert(bidirectional_range<Rng>);
  103. CPP_assert(
  104. same_as<detail::decay_t<decltype(std::declval<reverse_view<Rng>>().base())>,
  105. Rng>);
  106. reverse_view() = default;
  107. explicit constexpr reverse_view(reverse_view<Rng> rng)
  108. : Rng(rng.base())
  109. {}
  110. constexpr reverse_view<Rng> base() const
  111. {
  112. return reverse_view<Rng>{*this};
  113. }
  114. };
  115. #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
  116. template<typename Rng>
  117. reverse_view(Rng &&)->reverse_view<views::all_t<Rng>>;
  118. template<typename Rng>
  119. reverse_view(reverse_view<Rng>)->reverse_view<reverse_view<Rng>>;
  120. #endif
  121. namespace views
  122. {
  123. struct reverse_fn
  124. {
  125. template<typename Rng>
  126. constexpr auto operator()(Rng && rng) const
  127. -> CPP_ret(reverse_view<all_t<Rng>>)( //
  128. requires viewable_range<Rng> && bidirectional_range<Rng>)
  129. {
  130. return reverse_view<all_t<Rng>>{all(static_cast<Rng &&>(rng))};
  131. }
  132. };
  133. /// \relates reverse_fn
  134. /// \ingroup group-views
  135. RANGES_INLINE_VARIABLE(view<reverse_fn>, reverse)
  136. } // namespace views
  137. namespace cpp20
  138. {
  139. namespace views
  140. {
  141. using ranges::views::reverse;
  142. }
  143. CPP_template(typename Rng)( //
  144. requires view_<Rng> && bidirectional_range<Rng>) //
  145. using reverse_view = ranges::reverse_view<Rng>;
  146. } // namespace cpp20
  147. /// @}
  148. } // namespace ranges
  149. #include <range/v3/detail/satisfy_boost_range.hpp>
  150. RANGES_SATISFY_BOOST_RANGE(::ranges::reverse_view)
  151. #endif