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.

drop_last.hpp 11KB

5 vuotta sitten
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Andrey Diduh 2019
  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_DROP_LAST_HPP
  14. #define RANGES_V3_VIEW_DROP_LAST_HPP
  15. #include <type_traits>
  16. #include <meta/meta.hpp>
  17. #include <range/v3/functional/bind_back.hpp>
  18. #include <range/v3/functional/pipeable.hpp>
  19. #include <range/v3/iterator/counted_iterator.hpp>
  20. #include <range/v3/iterator/default_sentinel.hpp>
  21. #include <range/v3/iterator/operations.hpp>
  22. #include <range/v3/range/access.hpp>
  23. #include <range/v3/range/concepts.hpp>
  24. #include <range/v3/range/primitives.hpp>
  25. #include <range/v3/utility/optional.hpp>
  26. #include <range/v3/utility/static_const.hpp>
  27. #include <range/v3/view/adaptor.hpp>
  28. #include <range/v3/view/all.hpp>
  29. #include <range/v3/view/interface.hpp>
  30. #include <range/v3/view/view.hpp>
  31. namespace ranges
  32. {
  33. /// \cond
  34. namespace detail
  35. {
  36. namespace drop_last_view
  37. {
  38. template<typename Rng>
  39. range_size_t<Rng> get_size(Rng & rng, range_difference_t<Rng> n_)
  40. {
  41. RANGES_EXPECT(n_ >= 0);
  42. range_size_t<Rng> const initial_size = ranges::size(rng);
  43. range_size_t<Rng> const n = static_cast<range_size_t<Rng>>(n_);
  44. RANGES_EXPECT(initial_size >= n);
  45. return initial_size > n ? initial_size - n : 0;
  46. }
  47. template<typename Rng>
  48. auto get_end(Rng & rng, range_difference_t<Rng> n, int)
  49. -> CPP_ret(iterator_t<Rng>)( //
  50. requires random_access_range<Rng> && sized_range<Rng>)
  51. {
  52. return begin(rng) + static_cast<range_difference_t<Rng>>(
  53. drop_last_view::get_size(rng, n));
  54. }
  55. template<typename Rng>
  56. auto get_end(Rng & rng, range_difference_t<Rng> n, long)
  57. -> CPP_ret(iterator_t<Rng>)( //
  58. requires bidirectional_range<Rng> && common_range<Rng>)
  59. {
  60. return prev(end(rng), n, begin(rng));
  61. }
  62. enum class mode_enum
  63. {
  64. bidi,
  65. forward,
  66. sized,
  67. invalid
  68. };
  69. template<mode_enum Mode>
  70. using mode_t = std::integral_constant<mode_enum, Mode>;
  71. using mode_bidi = mode_t<mode_enum::bidi>;
  72. using mode_forward = mode_t<mode_enum::forward>;
  73. using mode_sized = mode_t<mode_enum::sized>;
  74. using mode_invalid = mode_t<mode_enum::invalid>;
  75. template<typename Rng>
  76. constexpr mode_enum get_mode() noexcept
  77. {
  78. // keep range bound
  79. // Sized Bidi O(N)
  80. return (random_access_range<Rng> && view_<Rng> && sized_range<Rng> &&
  81. view_<Rng>) ||
  82. (bidirectional_range<Rng> && view_<Rng> &&
  83. common_range<Rng> && view_<Rng>) //
  84. ? mode_enum::bidi //
  85. : sized_range<Rng> && view_<Rng> //
  86. ? mode_enum::sized //
  87. : forward_range<Rng> && view_<Rng> //
  88. ? mode_enum::forward //
  89. : mode_enum::invalid; //
  90. // max performance
  91. // Sized Bidi O(1)
  92. // Sized Bidi use mode::sized instead of mode::bidi - thus become unbound.
  93. /*return (random_access_range<Rng> && view_<Rng> && sized_range<Rng> &&
  94. view_<Rng>) || (bidirectional_range<Rng> && view_<Rng> &&
  95. common_range<Rng> && view_<Rng>) ? mode::bidi : sized_range<Rng> &&
  96. view_<Rng> ? mode::sized : bidirectional_range<Rng> && view_<Rng> &&
  97. common_range<Rng> && view_<Rng> ? mode::bidi : forward_range<Rng> &&
  98. view_<Rng> ? mode::forward : mode::invalid;*/
  99. }
  100. template<typename Rng>
  101. using mode_of = mode_t<drop_last_view::get_mode<Rng>()>;
  102. } // namespace drop_last_view
  103. } // namespace detail
  104. /// \endcond
  105. template<typename Rng, typename = detail::drop_last_view::mode_of<Rng>>
  106. struct drop_last_view
  107. {};
  108. template<typename Rng>
  109. struct drop_last_view<Rng, detail::drop_last_view::mode_bidi>
  110. : view_interface<drop_last_view<Rng, detail::drop_last_view::mode_bidi>,
  111. is_finite<Rng>::value
  112. ? finite
  113. : range_cardinality<Rng>::value> // finite at best
  114. {
  115. CPP_assert(
  116. (random_access_range<Rng> && view_<Rng> && sized_range<Rng> && view_<Rng>) ||
  117. (bidirectional_range<Rng> && view_<Rng> && common_range<Rng> && view_<Rng>));
  118. private:
  119. friend range_access;
  120. using difference_t = range_difference_t<Rng>;
  121. Rng rng_;
  122. difference_t n_;
  123. detail::non_propagating_cache<iterator_t<Rng>> end_;
  124. public:
  125. drop_last_view() = default;
  126. constexpr drop_last_view(Rng rng, difference_t n)
  127. : rng_(std::move(rng))
  128. , n_(n)
  129. {
  130. RANGES_EXPECT(n >= 0);
  131. }
  132. iterator_t<Rng> begin()
  133. {
  134. return ranges::begin(rng_);
  135. }
  136. sentinel_t<Rng> end()
  137. {
  138. if(!end_)
  139. end_ = detail::drop_last_view::get_end(rng_, n_, 0);
  140. return *end_;
  141. }
  142. template<typename CRng = Rng const>
  143. auto begin() const -> CPP_ret(iterator_t<CRng>)( //
  144. requires random_access_range<CRng> && sized_range<CRng>)
  145. {
  146. return ranges::begin(rng_);
  147. }
  148. template<typename CRng = Rng const>
  149. auto end() const -> CPP_ret(iterator_t<CRng>)( //
  150. requires random_access_range<CRng> && sized_range<CRng>)
  151. {
  152. return detail::drop_last_view::get_end(rng_, n_, 0);
  153. }
  154. auto CPP_fun(size)()( //
  155. requires sized_range<Rng>)
  156. {
  157. return detail::drop_last_view::get_size(rng_, n_);
  158. }
  159. CPP_member
  160. auto CPP_fun(size)()(const requires sized_range<Rng const>)
  161. {
  162. return detail::drop_last_view::get_size(rng_, n_);
  163. }
  164. Rng & base()
  165. {
  166. return rng_;
  167. }
  168. Rng const & base() const
  169. {
  170. return rng_;
  171. }
  172. };
  173. template<typename Rng>
  174. struct drop_last_view<Rng, detail::drop_last_view::mode_forward>
  175. : view_adaptor<drop_last_view<Rng, detail::drop_last_view::mode_forward>, Rng,
  176. is_finite<Rng>::value
  177. ? finite
  178. : range_cardinality<Rng>::value> // finite at best (but
  179. // unknown is expected)
  180. {
  181. CPP_assert(forward_range<Rng> && view_<Rng>);
  182. private:
  183. friend range_access;
  184. using difference_t = range_difference_t<Rng>;
  185. difference_t n_;
  186. detail::non_propagating_cache<iterator_t<Rng>> probe_begin;
  187. struct adaptor : adaptor_base
  188. {
  189. iterator_t<Rng> probe_;
  190. adaptor() = default;
  191. adaptor(iterator_t<Rng> probe_first)
  192. : probe_(std::move(probe_first))
  193. {}
  194. void next(iterator_t<Rng> & it)
  195. {
  196. ++it;
  197. ++probe_;
  198. }
  199. };
  200. struct sentinel_adaptor : adaptor_base
  201. {
  202. template<typename I, typename S>
  203. bool empty(I const &, adaptor const & ia, S const & s) const
  204. {
  205. return ia.probe_ == s;
  206. }
  207. };
  208. adaptor begin_adaptor()
  209. {
  210. if(!probe_begin)
  211. probe_begin = next(begin(this->base()), n_, end(this->base()));
  212. return {*probe_begin};
  213. }
  214. sentinel_adaptor end_adaptor()
  215. {
  216. return {};
  217. }
  218. public:
  219. drop_last_view() = default;
  220. constexpr drop_last_view(Rng rng, difference_t n)
  221. : drop_last_view::view_adaptor(std::move(rng))
  222. , n_(n)
  223. {
  224. RANGES_EXPECT(n >= 0);
  225. }
  226. CPP_member
  227. auto CPP_fun(size)()( //
  228. requires sized_range<Rng>)
  229. {
  230. return detail::drop_last_view::get_size(this->base(), n_);
  231. }
  232. CPP_member
  233. auto CPP_fun(size)()(const requires sized_range<Rng const>)
  234. {
  235. return detail::drop_last_view::get_size(this->base(), n_);
  236. }
  237. };
  238. template<typename Rng>
  239. struct drop_last_view<Rng, detail::drop_last_view::mode_sized>
  240. : view_interface<drop_last_view<Rng, detail::drop_last_view::mode_sized>, finite>
  241. {
  242. CPP_assert(sized_range<Rng> && view_<Rng>);
  243. private:
  244. friend range_access;
  245. using difference_t = range_difference_t<Rng>;
  246. Rng rng_;
  247. difference_t n_;
  248. public:
  249. drop_last_view() = default;
  250. constexpr drop_last_view(Rng rng, difference_t n)
  251. : rng_(std::move(rng))
  252. , n_(n)
  253. {
  254. RANGES_EXPECT(n >= 0);
  255. }
  256. counted_iterator<iterator_t<Rng>> begin()
  257. {
  258. return {ranges::begin(rng_), static_cast<difference_t>(size())};
  259. }
  260. template<typename CRng = Rng const>
  261. auto begin() const -> CPP_ret(counted_iterator<iterator_t<CRng>>)( //
  262. requires sized_range<CRng>)
  263. {
  264. return {ranges::begin(rng_), static_cast<difference_t>(size())};
  265. }
  266. default_sentinel_t end() const
  267. {
  268. return {};
  269. }
  270. range_size_t<Rng> size()
  271. {
  272. return detail::drop_last_view::get_size(this->base(), n_);
  273. }
  274. CPP_member
  275. auto CPP_fun(size)()(const requires sized_range<Rng const>)
  276. {
  277. return detail::drop_last_view::get_size(this->base(), n_);
  278. }
  279. Rng & base()
  280. {
  281. return rng_;
  282. }
  283. Rng const & base() const
  284. {
  285. return rng_;
  286. }
  287. };
  288. #if RANGES_CXX_DEDUCTION_GUIDES >= RANGES_CXX_DEDUCTION_GUIDES_17
  289. template<typename Rng>
  290. drop_last_view(Rng &&, range_difference_t<Rng>)->drop_last_view<views::all_t<Rng>>;
  291. #endif
  292. namespace views
  293. {
  294. struct drop_last_fn
  295. {
  296. private:
  297. friend view_access;
  298. template<typename Int>
  299. static constexpr auto CPP_fun(bind)(drop_last_fn drop_last, Int n)( //
  300. requires integral<Int>)
  301. {
  302. return make_pipeable(bind_back(drop_last, n));
  303. }
  304. public:
  305. template<typename Rng>
  306. constexpr auto operator()(Rng && rng, range_difference_t<Rng> n) const
  307. -> CPP_ret(drop_last_view<all_t<Rng>>)( //
  308. requires sized_range<Rng> || forward_range<Rng>)
  309. {
  310. return {all(static_cast<Rng &&>(rng)), n};
  311. }
  312. };
  313. RANGES_INLINE_VARIABLE(view<drop_last_fn>, drop_last)
  314. } // namespace views
  315. } // namespace ranges
  316. #include <range/v3/detail/satisfy_boost_range.hpp>
  317. RANGES_SATISFY_BOOST_RANGE(::ranges::drop_last_view)
  318. #endif