cache1.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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_CACHE1_HPP
  14. #define RANGES_V3_VIEW_CACHE1_HPP
  15. #include <range/v3/range_fwd.hpp>
  16. #include <range/v3/detail/range_access.hpp>
  17. #include <range/v3/iterator/concepts.hpp>
  18. #include <range/v3/range/access.hpp>
  19. #include <range/v3/range/concepts.hpp>
  20. #include <range/v3/range/primitives.hpp>
  21. #include <range/v3/range/traits.hpp>
  22. #include <range/v3/utility/optional.hpp>
  23. #include <range/v3/view/adaptor.hpp>
  24. #include <range/v3/view/all.hpp>
  25. #include <range/v3/detail/prologue.hpp>
  26. namespace ranges
  27. {
  28. /// \addtogroup group-views
  29. /// @{
  30. template<typename Rng>
  31. struct cache1_view : view_facade<cache1_view<Rng>, range_cardinality<Rng>::value>
  32. {
  33. private:
  34. CPP_assert(view_<Rng>);
  35. CPP_assert(input_range<Rng>);
  36. CPP_assert(constructible_from<range_value_t<Rng>, range_reference_t<Rng>>);
  37. friend range_access;
  38. Rng rng_;
  39. bool dirty_ = true;
  40. detail::non_propagating_cache<range_value_t<Rng>> cache_;
  41. CPP_member
  42. auto update_(range_reference_t<Rng> && val) //
  43. -> CPP_ret(void)(
  44. requires assignable_from<range_value_t<Rng> &, range_reference_t<Rng>>)
  45. {
  46. if(!cache_)
  47. cache_.emplace(static_cast<range_reference_t<Rng> &&>(val));
  48. else
  49. *cache_ = static_cast<range_reference_t<Rng> &&>(val);
  50. }
  51. CPP_member
  52. auto update_(range_reference_t<Rng> && val) //
  53. -> CPP_ret(void)(
  54. requires (!assignable_from<range_value_t<Rng> &, range_reference_t<Rng>>))
  55. {
  56. cache_.emplace(static_cast<range_reference_t<Rng> &&>(val));
  57. }
  58. struct cursor;
  59. struct sentinel
  60. {
  61. private:
  62. friend cursor;
  63. sentinel_t<Rng> last_;
  64. public:
  65. sentinel() = default;
  66. constexpr explicit sentinel(sentinel_t<Rng> last)
  67. : last_(std::move(last))
  68. {}
  69. };
  70. struct cursor
  71. {
  72. private:
  73. cache1_view * parent_;
  74. iterator_t<Rng> current_;
  75. public:
  76. using value_type = range_value_t<Rng>;
  77. using single_pass = std::true_type;
  78. using difference_type = range_difference_t<Rng>;
  79. cursor() = default;
  80. constexpr explicit cursor(cache1_view * parent, iterator_t<Rng> current)
  81. : parent_(parent)
  82. , current_(std::move(current))
  83. {}
  84. range_value_t<Rng> && read() const
  85. {
  86. if(parent_->dirty_)
  87. {
  88. parent_->update_(*current_);
  89. parent_->dirty_ = false;
  90. }
  91. return std::move(*parent_->cache_);
  92. }
  93. void next()
  94. {
  95. ++current_;
  96. parent_->dirty_ = true;
  97. }
  98. bool equal(cursor const & that) const
  99. {
  100. return current_ == that.current_;
  101. }
  102. bool equal(sentinel const & that) const
  103. {
  104. return current_ == that.last_;
  105. }
  106. CPP_member
  107. auto distance_to(cursor const & that) const //
  108. -> CPP_ret(difference_type)(
  109. requires sized_sentinel_for<iterator_t<Rng>, iterator_t<Rng>>)
  110. {
  111. return that.current_ - current_;
  112. }
  113. CPP_member
  114. auto distance_to(sentinel const & that) const //
  115. -> CPP_ret(difference_type)(
  116. requires sized_sentinel_for<sentinel_t<Rng>, iterator_t<Rng>>)
  117. {
  118. return that.last_ - current_;
  119. }
  120. };
  121. cursor begin_cursor()
  122. {
  123. dirty_ = true;
  124. return cursor{this, ranges::begin(rng_)};
  125. }
  126. cursor end_cursor_impl(std::true_type)
  127. {
  128. return cursor{this, ranges::end(rng_)};
  129. }
  130. sentinel end_cursor_impl(std::false_type)
  131. {
  132. return sentinel{ranges::end(rng_)};
  133. }
  134. auto end_cursor()
  135. {
  136. return end_cursor_impl(meta::bool_<(bool)common_range<Rng>>{});
  137. }
  138. public:
  139. cache1_view() = default;
  140. constexpr explicit cache1_view(Rng rng)
  141. : rng_{std::move(rng)}
  142. {}
  143. CPP_auto_member
  144. constexpr auto CPP_fun(size)()(
  145. requires sized_range<Rng>)
  146. {
  147. return ranges::size(rng_);
  148. }
  149. };
  150. #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
  151. template<typename Rng>
  152. cache1_view(Rng &&) //
  153. -> cache1_view<views::all_t<Rng>>;
  154. #endif
  155. namespace views
  156. {
  157. struct cache1_fn
  158. {
  159. /// \brief Caches the most recent element within the view so that
  160. /// dereferencing the view's iterator multiple times doesn't incur any
  161. /// recomputation. This can be useful in adaptor pipelines that include
  162. /// combinations of \c view::filter and \c view::transform, for instance.
  163. /// \note \c views::cache1 is always single-pass.
  164. template(typename Rng)(
  165. requires viewable_range<Rng> AND input_range<Rng> AND
  166. constructible_from<range_value_t<Rng>, range_reference_t<Rng>>)
  167. constexpr cache1_view<all_t<Rng>> operator()(Rng && rng) const //
  168. {
  169. return cache1_view<all_t<Rng>>{all(static_cast<Rng &&>(rng))};
  170. }
  171. };
  172. /// \relates cache1_fn
  173. /// \ingroup group-views
  174. RANGES_INLINE_VARIABLE(view_closure<cache1_fn>, cache1)
  175. } // namespace views
  176. /// @}
  177. } // namespace ranges
  178. #include <range/v3/detail/epilogue.hpp>
  179. #include <range/v3/detail/satisfy_boost_range.hpp>
  180. RANGES_SATISFY_BOOST_RANGE(::ranges::cache1_view)
  181. #endif