transform_view.h 14 KB

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