transform_view.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. // -*- C++ -*-
  2. //===----------------------------------------------------------------------===//
  3. //
  4. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  5. // See https://llvm.org/LICENSE.txt for license information.
  6. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef _LIBCPP___RANGES_TRANSFORM_VIEW_H
  10. #define _LIBCPP___RANGES_TRANSFORM_VIEW_H
  11. #include <__compare/three_way_comparable.h>
  12. #include <__concepts/constructible.h>
  13. #include <__concepts/convertible_to.h>
  14. #include <__concepts/copyable.h>
  15. #include <__concepts/derived_from.h>
  16. #include <__concepts/equality_comparable.h>
  17. #include <__concepts/invocable.h>
  18. #include <__config>
  19. #include <__functional/bind_back.h>
  20. #include <__functional/invoke.h>
  21. #include <__functional/perfect_forward.h>
  22. #include <__iterator/concepts.h>
  23. #include <__iterator/iterator_traits.h>
  24. #include <__memory/addressof.h>
  25. #include <__ranges/access.h>
  26. #include <__ranges/all.h>
  27. #include <__ranges/concepts.h>
  28. #include <__ranges/empty.h>
  29. #include <__ranges/movable_box.h>
  30. #include <__ranges/range_adaptor.h>
  31. #include <__ranges/size.h>
  32. #include <__ranges/view_interface.h>
  33. #include <__type_traits/conditional.h>
  34. #include <__type_traits/decay.h>
  35. #include <__type_traits/is_nothrow_constructible.h>
  36. #include <__type_traits/is_object.h>
  37. #include <__type_traits/is_reference.h>
  38. #include <__type_traits/maybe_const.h>
  39. #include <__type_traits/remove_cvref.h>
  40. #include <__utility/forward.h>
  41. #include <__utility/in_place.h>
  42. #include <__utility/move.h>
  43. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  44. # pragma GCC system_header
  45. #endif
  46. _LIBCPP_BEGIN_NAMESPACE_STD
  47. #if _LIBCPP_STD_VER >= 20
  48. namespace ranges {
  49. template<class _Fn, class _View>
  50. concept __regular_invocable_with_range_ref =
  51. regular_invocable<_Fn, range_reference_t<_View>>;
  52. template<class _View, class _Fn>
  53. concept __transform_view_constraints =
  54. view<_View> && is_object_v<_Fn> &&
  55. regular_invocable<_Fn&, range_reference_t<_View>> &&
  56. __can_reference<invoke_result_t<_Fn&, range_reference_t<_View>>>;
  57. # if _LIBCPP_STD_VER >= 23
  58. template <input_range _View, move_constructible _Fn>
  59. # else
  60. template <input_range _View, copy_constructible _Fn>
  61. # endif
  62. requires __transform_view_constraints<_View, _Fn>
  63. class transform_view : public view_interface<transform_view<_View, _Fn>> {
  64. template<bool> class __iterator;
  65. template<bool> class __sentinel;
  66. _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Fn> __func_;
  67. _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
  68. public:
  69. _LIBCPP_HIDE_FROM_ABI
  70. transform_view()
  71. requires default_initializable<_View> && default_initializable<_Fn> = default;
  72. _LIBCPP_HIDE_FROM_ABI
  73. constexpr _LIBCPP_EXPLICIT_SINCE_CXX23 transform_view(_View __base, _Fn __func)
  74. : __func_(std::in_place, std::move(__func)), __base_(std::move(__base)) {}
  75. _LIBCPP_HIDE_FROM_ABI
  76. constexpr _View base() const& requires copy_constructible<_View> { return __base_; }
  77. _LIBCPP_HIDE_FROM_ABI
  78. constexpr _View base() && { return std::move(__base_); }
  79. _LIBCPP_HIDE_FROM_ABI
  80. constexpr __iterator<false> begin() {
  81. return __iterator<false>{*this, ranges::begin(__base_)};
  82. }
  83. _LIBCPP_HIDE_FROM_ABI
  84. constexpr __iterator<true> begin() const
  85. requires range<const _View> &&
  86. __regular_invocable_with_range_ref<const _Fn&, const _View>
  87. {
  88. return __iterator<true>(*this, ranges::begin(__base_));
  89. }
  90. _LIBCPP_HIDE_FROM_ABI
  91. constexpr __sentinel<false> end() {
  92. return __sentinel<false>(ranges::end(__base_));
  93. }
  94. _LIBCPP_HIDE_FROM_ABI
  95. constexpr __iterator<false> end()
  96. requires common_range<_View>
  97. {
  98. return __iterator<false>(*this, ranges::end(__base_));
  99. }
  100. _LIBCPP_HIDE_FROM_ABI
  101. constexpr __sentinel<true> end() const
  102. requires range<const _View> &&
  103. __regular_invocable_with_range_ref<const _Fn&, const _View>
  104. {
  105. return __sentinel<true>(ranges::end(__base_));
  106. }
  107. _LIBCPP_HIDE_FROM_ABI
  108. constexpr __iterator<true> end() const
  109. requires common_range<const _View> &&
  110. __regular_invocable_with_range_ref<const _Fn&, const _View>
  111. {
  112. return __iterator<true>(*this, ranges::end(__base_));
  113. }
  114. _LIBCPP_HIDE_FROM_ABI
  115. constexpr auto size() requires sized_range<_View> { return ranges::size(__base_); }
  116. _LIBCPP_HIDE_FROM_ABI
  117. constexpr auto size() const requires sized_range<const _View> { return ranges::size(__base_); }
  118. };
  119. template<class _Range, class _Fn>
  120. transform_view(_Range&&, _Fn) -> transform_view<views::all_t<_Range>, _Fn>;
  121. template<class _View>
  122. struct __transform_view_iterator_concept { using type = input_iterator_tag; };
  123. template<random_access_range _View>
  124. struct __transform_view_iterator_concept<_View> { using type = random_access_iterator_tag; };
  125. template<bidirectional_range _View>
  126. struct __transform_view_iterator_concept<_View> { using type = bidirectional_iterator_tag; };
  127. template<forward_range _View>
  128. struct __transform_view_iterator_concept<_View> { using type = forward_iterator_tag; };
  129. template<class, class>
  130. struct __transform_view_iterator_category_base {};
  131. template<forward_range _View, class _Fn>
  132. struct __transform_view_iterator_category_base<_View, _Fn> {
  133. using _Cat = typename iterator_traits<iterator_t<_View>>::iterator_category;
  134. using iterator_category = conditional_t<
  135. is_reference_v<invoke_result_t<_Fn&, range_reference_t<_View>>>,
  136. conditional_t<
  137. derived_from<_Cat, contiguous_iterator_tag>,
  138. random_access_iterator_tag,
  139. _Cat
  140. >,
  141. input_iterator_tag
  142. >;
  143. };
  144. # if _LIBCPP_STD_VER >= 23
  145. template <input_range _View, move_constructible _Fn>
  146. # else
  147. template <input_range _View, copy_constructible _Fn>
  148. # endif
  149. requires __transform_view_constraints<_View, _Fn>
  150. template <bool _Const>
  151. class transform_view<_View, _Fn>::__iterator : public __transform_view_iterator_category_base<_View, _Fn> {
  152. using _Parent = __maybe_const<_Const, transform_view>;
  153. using _Base = __maybe_const<_Const, _View>;
  154. _Parent *__parent_ = nullptr;
  155. template<bool>
  156. friend class transform_view<_View, _Fn>::__iterator;
  157. template<bool>
  158. friend class transform_view<_View, _Fn>::__sentinel;
  159. public:
  160. iterator_t<_Base> __current_ = iterator_t<_Base>();
  161. using iterator_concept = typename __transform_view_iterator_concept<_View>::type;
  162. using value_type = remove_cvref_t<invoke_result_t<_Fn&, range_reference_t<_Base>>>;
  163. using difference_type = range_difference_t<_Base>;
  164. _LIBCPP_HIDE_FROM_ABI
  165. __iterator() requires default_initializable<iterator_t<_Base>> = default;
  166. _LIBCPP_HIDE_FROM_ABI
  167. constexpr __iterator(_Parent& __parent, iterator_t<_Base> __current)
  168. : __parent_(std::addressof(__parent)), __current_(std::move(__current)) {}
  169. // Note: `__i` should always be `__iterator<false>`, but directly using
  170. // `__iterator<false>` is ill-formed when `_Const` is false
  171. // (see http://wg21.link/class.copy.ctor#5).
  172. _LIBCPP_HIDE_FROM_ABI
  173. constexpr __iterator(__iterator<!_Const> __i)
  174. requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>>
  175. : __parent_(__i.__parent_), __current_(std::move(__i.__current_)) {}
  176. _LIBCPP_HIDE_FROM_ABI
  177. constexpr const iterator_t<_Base>& base() const& noexcept {
  178. return __current_;
  179. }
  180. _LIBCPP_HIDE_FROM_ABI
  181. constexpr iterator_t<_Base> base() && {
  182. return std::move(__current_);
  183. }
  184. _LIBCPP_HIDE_FROM_ABI
  185. constexpr decltype(auto) operator*() const
  186. noexcept(noexcept(std::invoke(*__parent_->__func_, *__current_)))
  187. {
  188. return std::invoke(*__parent_->__func_, *__current_);
  189. }
  190. _LIBCPP_HIDE_FROM_ABI
  191. constexpr __iterator& operator++() {
  192. ++__current_;
  193. return *this;
  194. }
  195. _LIBCPP_HIDE_FROM_ABI
  196. constexpr void operator++(int) { ++__current_; }
  197. _LIBCPP_HIDE_FROM_ABI
  198. constexpr __iterator operator++(int)
  199. requires forward_range<_Base>
  200. {
  201. auto __tmp = *this;
  202. ++*this;
  203. return __tmp;
  204. }
  205. _LIBCPP_HIDE_FROM_ABI
  206. constexpr __iterator& operator--()
  207. requires bidirectional_range<_Base>
  208. {
  209. --__current_;
  210. return *this;
  211. }
  212. _LIBCPP_HIDE_FROM_ABI
  213. constexpr __iterator operator--(int)
  214. requires bidirectional_range<_Base>
  215. {
  216. auto __tmp = *this;
  217. --*this;
  218. return __tmp;
  219. }
  220. _LIBCPP_HIDE_FROM_ABI
  221. constexpr __iterator& operator+=(difference_type __n)
  222. requires random_access_range<_Base>
  223. {
  224. __current_ += __n;
  225. return *this;
  226. }
  227. _LIBCPP_HIDE_FROM_ABI
  228. constexpr __iterator& operator-=(difference_type __n)
  229. requires random_access_range<_Base>
  230. {
  231. __current_ -= __n;
  232. return *this;
  233. }
  234. _LIBCPP_HIDE_FROM_ABI
  235. constexpr decltype(auto) operator[](difference_type __n) const
  236. noexcept(noexcept(std::invoke(*__parent_->__func_, __current_[__n])))
  237. requires random_access_range<_Base>
  238. {
  239. return std::invoke(*__parent_->__func_, __current_[__n]);
  240. }
  241. _LIBCPP_HIDE_FROM_ABI
  242. friend constexpr bool operator==(const __iterator& __x, const __iterator& __y)
  243. requires equality_comparable<iterator_t<_Base>>
  244. {
  245. return __x.__current_ == __y.__current_;
  246. }
  247. _LIBCPP_HIDE_FROM_ABI
  248. friend constexpr bool operator<(const __iterator& __x, const __iterator& __y)
  249. requires random_access_range<_Base>
  250. {
  251. return __x.__current_ < __y.__current_;
  252. }
  253. _LIBCPP_HIDE_FROM_ABI
  254. friend constexpr bool operator>(const __iterator& __x, const __iterator& __y)
  255. requires random_access_range<_Base>
  256. {
  257. return __x.__current_ > __y.__current_;
  258. }
  259. _LIBCPP_HIDE_FROM_ABI
  260. friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y)
  261. requires random_access_range<_Base>
  262. {
  263. return __x.__current_ <= __y.__current_;
  264. }
  265. _LIBCPP_HIDE_FROM_ABI
  266. friend constexpr bool operator>=(const __iterator& __x, const __iterator& __y)
  267. requires random_access_range<_Base>
  268. {
  269. return __x.__current_ >= __y.__current_;
  270. }
  271. _LIBCPP_HIDE_FROM_ABI
  272. friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y)
  273. requires random_access_range<_Base> && three_way_comparable<iterator_t<_Base>>
  274. {
  275. return __x.__current_ <=> __y.__current_;
  276. }
  277. _LIBCPP_HIDE_FROM_ABI
  278. friend constexpr __iterator operator+(__iterator __i, difference_type __n)
  279. requires random_access_range<_Base>
  280. {
  281. return __iterator{*__i.__parent_, __i.__current_ + __n};
  282. }
  283. _LIBCPP_HIDE_FROM_ABI
  284. friend constexpr __iterator operator+(difference_type __n, __iterator __i)
  285. requires random_access_range<_Base>
  286. {
  287. return __iterator{*__i.__parent_, __i.__current_ + __n};
  288. }
  289. _LIBCPP_HIDE_FROM_ABI
  290. friend constexpr __iterator operator-(__iterator __i, difference_type __n)
  291. requires random_access_range<_Base>
  292. {
  293. return __iterator{*__i.__parent_, __i.__current_ - __n};
  294. }
  295. _LIBCPP_HIDE_FROM_ABI
  296. friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
  297. requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
  298. {
  299. return __x.__current_ - __y.__current_;
  300. }
  301. _LIBCPP_HIDE_FROM_ABI
  302. friend constexpr decltype(auto) iter_move(const __iterator& __i)
  303. noexcept(noexcept(*__i))
  304. {
  305. if constexpr (is_lvalue_reference_v<decltype(*__i)>)
  306. return std::move(*__i);
  307. else
  308. return *__i;
  309. }
  310. };
  311. # if _LIBCPP_STD_VER >= 23
  312. template <input_range _View, move_constructible _Fn>
  313. # else
  314. template <input_range _View, copy_constructible _Fn>
  315. # endif
  316. requires __transform_view_constraints<_View, _Fn>
  317. template <bool _Const>
  318. class transform_view<_View, _Fn>::__sentinel {
  319. using _Parent = __maybe_const<_Const, transform_view>;
  320. using _Base = __maybe_const<_Const, _View>;
  321. sentinel_t<_Base> __end_ = sentinel_t<_Base>();
  322. template<bool>
  323. friend class transform_view<_View, _Fn>::__iterator;
  324. template<bool>
  325. friend class transform_view<_View, _Fn>::__sentinel;
  326. public:
  327. _LIBCPP_HIDE_FROM_ABI
  328. __sentinel() = default;
  329. _LIBCPP_HIDE_FROM_ABI
  330. constexpr explicit __sentinel(sentinel_t<_Base> __end) : __end_(__end) {}
  331. // Note: `__i` should always be `__sentinel<false>`, but directly using
  332. // `__sentinel<false>` is ill-formed when `_Const` is false
  333. // (see http://wg21.link/class.copy.ctor#5).
  334. _LIBCPP_HIDE_FROM_ABI
  335. constexpr __sentinel(__sentinel<!_Const> __i)
  336. requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>
  337. : __end_(std::move(__i.__end_)) {}
  338. _LIBCPP_HIDE_FROM_ABI
  339. constexpr sentinel_t<_Base> base() const { return __end_; }
  340. template<bool _OtherConst>
  341. requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
  342. _LIBCPP_HIDE_FROM_ABI
  343. friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
  344. return __x.__current_ == __y.__end_;
  345. }
  346. template<bool _OtherConst>
  347. requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
  348. _LIBCPP_HIDE_FROM_ABI
  349. friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
  350. operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
  351. return __x.__current_ - __y.__end_;
  352. }
  353. template<bool _OtherConst>
  354. requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
  355. _LIBCPP_HIDE_FROM_ABI
  356. friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
  357. operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) {
  358. return __x.__end_ - __y.__current_;
  359. }
  360. };
  361. namespace views {
  362. namespace __transform {
  363. struct __fn {
  364. template<class _Range, class _Fn>
  365. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  366. constexpr auto operator()(_Range&& __range, _Fn&& __f) const
  367. noexcept(noexcept(transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f))))
  368. -> decltype( transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f)))
  369. { return transform_view(std::forward<_Range>(__range), std::forward<_Fn>(__f)); }
  370. template<class _Fn>
  371. requires constructible_from<decay_t<_Fn>, _Fn>
  372. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI
  373. constexpr auto operator()(_Fn&& __f) const
  374. noexcept(is_nothrow_constructible_v<decay_t<_Fn>, _Fn>)
  375. { return __range_adaptor_closure_t(std::__bind_back(*this, std::forward<_Fn>(__f))); }
  376. };
  377. } // namespace __transform
  378. inline namespace __cpo {
  379. inline constexpr auto transform = __transform::__fn{};
  380. } // namespace __cpo
  381. } // namespace views
  382. } // namespace ranges
  383. #endif // _LIBCPP_STD_VER >= 20
  384. _LIBCPP_END_NAMESPACE_STD
  385. #endif // _LIBCPP___RANGES_TRANSFORM_VIEW_H