elements_view.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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_ELEMENTS_VIEW_H
  10. #define _LIBCPP___RANGES_ELEMENTS_VIEW_H
  11. #include <__compare/three_way_comparable.h>
  12. #include <__concepts/constructible.h>
  13. #include <__concepts/convertible_to.h>
  14. #include <__concepts/derived_from.h>
  15. #include <__concepts/equality_comparable.h>
  16. #include <__config>
  17. #include <__fwd/get.h>
  18. #include <__iterator/concepts.h>
  19. #include <__iterator/iterator_traits.h>
  20. #include <__ranges/access.h>
  21. #include <__ranges/all.h>
  22. #include <__ranges/concepts.h>
  23. #include <__ranges/enable_borrowed_range.h>
  24. #include <__ranges/range_adaptor.h>
  25. #include <__ranges/size.h>
  26. #include <__ranges/view_interface.h>
  27. #include <__tuple_dir/tuple_element.h>
  28. #include <__tuple_dir/tuple_like.h>
  29. #include <__tuple_dir/tuple_size.h>
  30. #include <__type_traits/is_reference.h>
  31. #include <__type_traits/maybe_const.h>
  32. #include <__type_traits/remove_cv.h>
  33. #include <__type_traits/remove_cvref.h>
  34. #include <__type_traits/remove_reference.h>
  35. #include <__utility/declval.h>
  36. #include <__utility/forward.h>
  37. #include <__utility/move.h>
  38. #include <cstddef>
  39. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  40. # pragma GCC system_header
  41. #endif
  42. _LIBCPP_BEGIN_NAMESPACE_STD
  43. #if _LIBCPP_STD_VER >= 20
  44. namespace ranges {
  45. template <class _Tp, size_t _Np>
  46. concept __has_tuple_element = __tuple_like<_Tp> && _Np < tuple_size<_Tp>::value;
  47. template <class _Tp, size_t _Np>
  48. concept __returnable_element = is_reference_v<_Tp> || move_constructible<tuple_element_t<_Np, _Tp>>;
  49. template <input_range _View, size_t _Np>
  50. requires view<_View> && __has_tuple_element<range_value_t<_View>, _Np> &&
  51. __has_tuple_element<remove_reference_t<range_reference_t<_View>>, _Np> &&
  52. __returnable_element<range_reference_t<_View>, _Np>
  53. class elements_view : public view_interface<elements_view<_View, _Np>> {
  54. private:
  55. template <bool>
  56. class __iterator;
  57. template <bool>
  58. class __sentinel;
  59. public:
  60. _LIBCPP_HIDE_FROM_ABI elements_view()
  61. requires default_initializable<_View>
  62. = default;
  63. _LIBCPP_HIDE_FROM_ABI constexpr explicit elements_view(_View __base) : __base_(std::move(__base)) {}
  64. _LIBCPP_HIDE_FROM_ABI constexpr _View base() const&
  65. requires copy_constructible<_View>
  66. {
  67. return __base_;
  68. }
  69. _LIBCPP_HIDE_FROM_ABI constexpr _View base() && { return std::move(__base_); }
  70. _LIBCPP_HIDE_FROM_ABI constexpr auto begin()
  71. requires(!__simple_view<_View>)
  72. {
  73. return __iterator</*_Const=*/false>(ranges::begin(__base_));
  74. }
  75. _LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
  76. requires range<const _View>
  77. {
  78. return __iterator</*_Const=*/true>(ranges::begin(__base_));
  79. }
  80. _LIBCPP_HIDE_FROM_ABI constexpr auto end()
  81. requires(!__simple_view<_View> && !common_range<_View>)
  82. {
  83. return __sentinel</*_Const=*/false>{ranges::end(__base_)};
  84. }
  85. _LIBCPP_HIDE_FROM_ABI constexpr auto end()
  86. requires(!__simple_view<_View> && common_range<_View>)
  87. {
  88. return __iterator</*_Const=*/false>{ranges::end(__base_)};
  89. }
  90. _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
  91. requires range<const _View>
  92. {
  93. return __sentinel</*_Const=*/true>{ranges::end(__base_)};
  94. }
  95. _LIBCPP_HIDE_FROM_ABI constexpr auto end() const
  96. requires common_range<const _View>
  97. {
  98. return __iterator</*_Const=*/true>{ranges::end(__base_)};
  99. }
  100. _LIBCPP_HIDE_FROM_ABI constexpr auto size()
  101. requires sized_range<_View>
  102. {
  103. return ranges::size(__base_);
  104. }
  105. _LIBCPP_HIDE_FROM_ABI constexpr auto size() const
  106. requires sized_range<const _View>
  107. {
  108. return ranges::size(__base_);
  109. }
  110. private:
  111. _LIBCPP_NO_UNIQUE_ADDRESS _View __base_ = _View();
  112. };
  113. template <class, size_t>
  114. struct __elements_view_iterator_category_base {};
  115. template <forward_range _Base, size_t _Np>
  116. struct __elements_view_iterator_category_base<_Base, _Np> {
  117. static consteval auto __get_iterator_category() {
  118. using _Result = decltype(std::get<_Np>(*std::declval<iterator_t<_Base>>()));
  119. using _Cat = typename iterator_traits<iterator_t<_Base>>::iterator_category;
  120. if constexpr (!is_lvalue_reference_v<_Result>) {
  121. return input_iterator_tag{};
  122. } else if constexpr (derived_from<_Cat, random_access_iterator_tag>) {
  123. return random_access_iterator_tag{};
  124. } else {
  125. return _Cat{};
  126. }
  127. }
  128. using iterator_category = decltype(__get_iterator_category());
  129. };
  130. template <input_range _View, size_t _Np>
  131. requires view<_View> && __has_tuple_element<range_value_t<_View>, _Np> &&
  132. __has_tuple_element<remove_reference_t<range_reference_t<_View>>, _Np> &&
  133. __returnable_element<range_reference_t<_View>, _Np>
  134. template <bool _Const>
  135. class elements_view<_View, _Np>::__iterator
  136. : public __elements_view_iterator_category_base<__maybe_const<_Const, _View>, _Np> {
  137. template <bool>
  138. friend class __iterator;
  139. template <bool>
  140. friend class __sentinel;
  141. using _Base = __maybe_const<_Const, _View>;
  142. iterator_t<_Base> __current_ = iterator_t<_Base>();
  143. _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __get_element(const iterator_t<_Base>& __i) {
  144. if constexpr (is_reference_v<range_reference_t<_Base>>) {
  145. return std::get<_Np>(*__i);
  146. } else {
  147. using _Element = remove_cv_t<tuple_element_t<_Np, range_reference_t<_Base>>>;
  148. return static_cast<_Element>(std::get<_Np>(*__i));
  149. }
  150. }
  151. static consteval auto __get_iterator_concept() {
  152. if constexpr (random_access_range<_Base>) {
  153. return random_access_iterator_tag{};
  154. } else if constexpr (bidirectional_range<_Base>) {
  155. return bidirectional_iterator_tag{};
  156. } else if constexpr (forward_range<_Base>) {
  157. return forward_iterator_tag{};
  158. } else {
  159. return input_iterator_tag{};
  160. }
  161. }
  162. public:
  163. using iterator_concept = decltype(__get_iterator_concept());
  164. using value_type = remove_cvref_t<tuple_element_t<_Np, range_value_t<_Base>>>;
  165. using difference_type = range_difference_t<_Base>;
  166. _LIBCPP_HIDE_FROM_ABI __iterator()
  167. requires default_initializable<iterator_t<_Base>>
  168. = default;
  169. _LIBCPP_HIDE_FROM_ABI constexpr explicit __iterator(iterator_t<_Base> __current) : __current_(std::move(__current)) {}
  170. _LIBCPP_HIDE_FROM_ABI constexpr __iterator(__iterator<!_Const> __i)
  171. requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>>
  172. : __current_(std::move(__i.__current_)) {}
  173. _LIBCPP_HIDE_FROM_ABI constexpr const iterator_t<_Base>& base() const& noexcept { return __current_; }
  174. _LIBCPP_HIDE_FROM_ABI constexpr iterator_t<_Base> base() && { return std::move(__current_); }
  175. _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator*() const { return __get_element(__current_); }
  176. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
  177. ++__current_;
  178. return *this;
  179. }
  180. _LIBCPP_HIDE_FROM_ABI constexpr void operator++(int) { ++__current_; }
  181. _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator++(int)
  182. requires forward_range<_Base>
  183. {
  184. auto temp = *this;
  185. ++__current_;
  186. return temp;
  187. }
  188. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator--()
  189. requires bidirectional_range<_Base>
  190. {
  191. --__current_;
  192. return *this;
  193. }
  194. _LIBCPP_HIDE_FROM_ABI constexpr __iterator operator--(int)
  195. requires bidirectional_range<_Base>
  196. {
  197. auto temp = *this;
  198. --__current_;
  199. return temp;
  200. }
  201. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator+=(difference_type __n)
  202. requires random_access_range<_Base>
  203. {
  204. __current_ += __n;
  205. return *this;
  206. }
  207. _LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator-=(difference_type __n)
  208. requires random_access_range<_Base>
  209. {
  210. __current_ -= __n;
  211. return *this;
  212. }
  213. _LIBCPP_HIDE_FROM_ABI constexpr decltype(auto) operator[](difference_type __n) const
  214. requires random_access_range<_Base>
  215. {
  216. return __get_element(__current_ + __n);
  217. }
  218. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y)
  219. requires equality_comparable<iterator_t<_Base>>
  220. {
  221. return __x.__current_ == __y.__current_;
  222. }
  223. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<(const __iterator& __x, const __iterator& __y)
  224. requires random_access_range<_Base>
  225. {
  226. return __x.__current_ < __y.__current_;
  227. }
  228. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>(const __iterator& __x, const __iterator& __y)
  229. requires random_access_range<_Base>
  230. {
  231. return __y < __x;
  232. }
  233. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator<=(const __iterator& __x, const __iterator& __y)
  234. requires random_access_range<_Base>
  235. {
  236. return !(__y < __x);
  237. }
  238. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator>=(const __iterator& __x, const __iterator& __y)
  239. requires random_access_range<_Base>
  240. {
  241. return !(__x < __y);
  242. }
  243. _LIBCPP_HIDE_FROM_ABI friend constexpr auto operator<=>(const __iterator& __x, const __iterator& __y)
  244. requires random_access_range<_Base> && three_way_comparable<iterator_t<_Base>>
  245. {
  246. return __x.__current_ <=> __y.__current_;
  247. }
  248. _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(const __iterator& __x, difference_type __y)
  249. requires random_access_range<_Base>
  250. {
  251. return __iterator{__x} += __y;
  252. }
  253. _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator+(difference_type __x, const __iterator& __y)
  254. requires random_access_range<_Base>
  255. {
  256. return __y + __x;
  257. }
  258. _LIBCPP_HIDE_FROM_ABI friend constexpr __iterator operator-(const __iterator& __x, difference_type __y)
  259. requires random_access_range<_Base>
  260. {
  261. return __iterator{__x} -= __y;
  262. }
  263. _LIBCPP_HIDE_FROM_ABI friend constexpr difference_type operator-(const __iterator& __x, const __iterator& __y)
  264. requires sized_sentinel_for<iterator_t<_Base>, iterator_t<_Base>>
  265. {
  266. return __x.__current_ - __y.__current_;
  267. }
  268. };
  269. template <input_range _View, size_t _Np>
  270. requires view<_View> && __has_tuple_element<range_value_t<_View>, _Np> &&
  271. __has_tuple_element<remove_reference_t<range_reference_t<_View>>, _Np> &&
  272. __returnable_element<range_reference_t<_View>, _Np>
  273. template <bool _Const>
  274. class elements_view<_View, _Np>::__sentinel {
  275. private:
  276. using _Base = __maybe_const<_Const, _View>;
  277. _LIBCPP_NO_UNIQUE_ADDRESS sentinel_t<_Base> __end_ = sentinel_t<_Base>();
  278. template <bool>
  279. friend class __sentinel;
  280. template <bool _AnyConst>
  281. _LIBCPP_HIDE_FROM_ABI static constexpr decltype(auto) __get_current(const __iterator<_AnyConst>& __iter) {
  282. return (__iter.__current_);
  283. }
  284. public:
  285. _LIBCPP_HIDE_FROM_ABI __sentinel() = default;
  286. _LIBCPP_HIDE_FROM_ABI constexpr explicit __sentinel(sentinel_t<_Base> __end) : __end_(std::move(__end)) {}
  287. _LIBCPP_HIDE_FROM_ABI constexpr __sentinel(__sentinel<!_Const> __other)
  288. requires _Const && convertible_to<sentinel_t<_View>, sentinel_t<_Base>>
  289. : __end_(std::move(__other.__end_)) {}
  290. _LIBCPP_HIDE_FROM_ABI constexpr sentinel_t<_Base> base() const { return __end_; }
  291. template <bool _OtherConst>
  292. requires sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
  293. _LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
  294. return __get_current(__x) == __y.__end_;
  295. }
  296. template <bool _OtherConst>
  297. requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
  298. _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
  299. operator-(const __iterator<_OtherConst>& __x, const __sentinel& __y) {
  300. return __get_current(__x) - __y.__end_;
  301. }
  302. template <bool _OtherConst>
  303. requires sized_sentinel_for<sentinel_t<_Base>, iterator_t<__maybe_const<_OtherConst, _View>>>
  304. _LIBCPP_HIDE_FROM_ABI friend constexpr range_difference_t<__maybe_const<_OtherConst, _View>>
  305. operator-(const __sentinel& __x, const __iterator<_OtherConst>& __y) {
  306. return __x.__end_ - __get_current(__y);
  307. }
  308. };
  309. template <class _Tp, size_t _Np>
  310. inline constexpr bool enable_borrowed_range<elements_view<_Tp, _Np>> = enable_borrowed_range<_Tp>;
  311. template <class _Tp>
  312. using keys_view = elements_view<_Tp, 0>;
  313. template <class _Tp>
  314. using values_view = elements_view<_Tp, 1>;
  315. namespace views {
  316. namespace __elements {
  317. template <size_t _Np>
  318. struct __fn : __range_adaptor_closure<__fn<_Np>> {
  319. template <class _Range>
  320. [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const
  321. /**/ noexcept(noexcept(elements_view<all_t<_Range&&>, _Np>(std::forward<_Range>(__range))))
  322. /*------*/ -> decltype(elements_view<all_t<_Range&&>, _Np>(std::forward<_Range>(__range))) {
  323. /*-------------*/ return elements_view<all_t<_Range&&>, _Np>(std::forward<_Range>(__range));
  324. }
  325. };
  326. } // namespace __elements
  327. inline namespace __cpo {
  328. template <size_t _Np>
  329. inline constexpr auto elements = __elements::__fn<_Np>{};
  330. inline constexpr auto keys = elements<0>;
  331. inline constexpr auto values = elements<1>;
  332. } // namespace __cpo
  333. } // namespace views
  334. } // namespace ranges
  335. #endif // _LIBCPP_STD_VER >= 20
  336. _LIBCPP_END_NAMESPACE_STD
  337. #endif // _LIBCPP___RANGES_ELEMENTS_VIEW_H