to.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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_TO_H
  10. #define _LIBCPP___RANGES_TO_H
  11. #include <__algorithm/ranges_copy.h>
  12. #include <__concepts/constructible.h>
  13. #include <__concepts/convertible_to.h>
  14. #include <__concepts/derived_from.h>
  15. #include <__concepts/same_as.h>
  16. #include <__config>
  17. #include <__functional/bind_back.h>
  18. #include <__iterator/back_insert_iterator.h>
  19. #include <__iterator/insert_iterator.h>
  20. #include <__iterator/iterator_traits.h>
  21. #include <__ranges/access.h>
  22. #include <__ranges/concepts.h>
  23. #include <__ranges/from_range.h>
  24. #include <__ranges/range_adaptor.h>
  25. #include <__ranges/size.h>
  26. #include <__ranges/transform_view.h>
  27. #include <__type_traits/add_pointer.h>
  28. #include <__type_traits/is_const.h>
  29. #include <__type_traits/is_volatile.h>
  30. #include <__type_traits/type_identity.h>
  31. #include <__utility/declval.h>
  32. #include <__utility/forward.h>
  33. #include <cstddef>
  34. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  35. # pragma GCC system_header
  36. #endif
  37. _LIBCPP_BEGIN_NAMESPACE_STD
  38. #if _LIBCPP_STD_VER >= 23
  39. namespace ranges {
  40. template <class _Container>
  41. constexpr bool __reservable_container =
  42. sized_range<_Container> && requires(_Container& __c, range_size_t<_Container> __n) {
  43. __c.reserve(__n);
  44. { __c.capacity() } -> same_as<decltype(__n)>;
  45. { __c.max_size() } -> same_as<decltype(__n)>;
  46. };
  47. template <class _Container, class _Ref>
  48. constexpr bool __container_insertable = requires(_Container& __c, _Ref&& __ref) {
  49. requires(
  50. requires { __c.push_back(std::forward<_Ref>(__ref)); } ||
  51. requires { __c.insert(__c.end(), std::forward<_Ref>(__ref)); });
  52. };
  53. template <class _Ref, class _Container>
  54. _LIBCPP_HIDE_FROM_ABI constexpr auto __container_inserter(_Container& __c) {
  55. if constexpr (requires { __c.push_back(std::declval<_Ref>()); }) {
  56. return std::back_inserter(__c);
  57. } else {
  58. return std::inserter(__c, __c.end());
  59. }
  60. }
  61. // Note: making this a concept allows short-circuiting the second condition.
  62. template <class _Container, class _Range>
  63. concept __try_non_recursive_conversion =
  64. !input_range<_Container> || convertible_to<range_reference_t<_Range>, range_value_t<_Container>>;
  65. template <class _Container, class _Range, class... _Args>
  66. concept __constructible_from_iter_pair =
  67. common_range<_Range> && requires { typename iterator_traits<iterator_t<_Range>>::iterator_category; } &&
  68. derived_from<typename iterator_traits<iterator_t<_Range>>::iterator_category, input_iterator_tag> &&
  69. constructible_from<_Container, iterator_t<_Range>, sentinel_t<_Range>, _Args...>;
  70. template <class>
  71. concept __always_false = false;
  72. // `ranges::to` base template -- the `_Container` type is a simple type template parameter.
  73. template <class _Container, input_range _Range, class... _Args>
  74. requires(!view<_Container>)
  75. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr _Container to(_Range&& __range, _Args&&... __args) {
  76. // Mandates: C is a cv-unqualified class type.
  77. static_assert(!is_const_v<_Container>, "The target container cannot be const-qualified, please remove the const");
  78. static_assert(
  79. !is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
  80. // First see if the non-recursive case applies -- the conversion target is either:
  81. // - a range with a convertible value type;
  82. // - a non-range type which might support being created from the input argument(s) (e.g. an `optional`).
  83. if constexpr (__try_non_recursive_conversion<_Container, _Range>) {
  84. // Case 1 -- construct directly from the given range.
  85. if constexpr (constructible_from<_Container, _Range, _Args...>) {
  86. return _Container(std::forward<_Range>(__range), std::forward<_Args>(__args)...);
  87. }
  88. // Case 2 -- construct using the `from_range_t` tagged constructor.
  89. else if constexpr (constructible_from<_Container, from_range_t, _Range, _Args...>) {
  90. return _Container(from_range, std::forward<_Range>(__range), std::forward<_Args>(__args)...);
  91. }
  92. // Case 3 -- construct from a begin-end iterator pair.
  93. else if constexpr (__constructible_from_iter_pair<_Container, _Range, _Args...>) {
  94. return _Container(ranges::begin(__range), ranges::end(__range), std::forward<_Args>(__args)...);
  95. }
  96. // Case 4 -- default-construct (or construct from the extra arguments) and insert, reserving the size if possible.
  97. else if constexpr (constructible_from<_Container, _Args...> &&
  98. __container_insertable<_Container, range_reference_t<_Range>>) {
  99. _Container __result(std::forward<_Args>(__args)...);
  100. if constexpr (sized_range<_Range> && __reservable_container<_Container>) {
  101. __result.reserve(static_cast<range_size_t<_Container>>(ranges::size(__range)));
  102. }
  103. ranges::copy(__range, ranges::__container_inserter<range_reference_t<_Range>>(__result));
  104. return __result;
  105. } else {
  106. static_assert(__always_false<_Container>, "ranges::to: unable to convert to the given container type.");
  107. }
  108. // Try the recursive case.
  109. } else if constexpr (input_range<range_reference_t<_Range>>) {
  110. return ranges::to<_Container>(
  111. __range | views::transform([](auto&& __elem) {
  112. return ranges::to<range_value_t<_Container>>(std::forward<decltype(__elem)>(__elem));
  113. }),
  114. std::forward<_Args>(__args)...);
  115. } else {
  116. static_assert(__always_false<_Container>, "ranges::to: unable to convert to the given container type.");
  117. }
  118. }
  119. template <class _Range>
  120. struct __minimal_input_iterator {
  121. using iterator_category = input_iterator_tag;
  122. using value_type = range_value_t<_Range>;
  123. using difference_type = ptrdiff_t;
  124. using pointer = add_pointer_t<range_reference_t<_Range>>;
  125. using reference = range_reference_t<_Range>;
  126. reference operator*() const;
  127. pointer operator->() const;
  128. __minimal_input_iterator& operator++();
  129. __minimal_input_iterator operator++(int);
  130. bool operator==(const __minimal_input_iterator&) const;
  131. };
  132. // Deduces the full type of the container from the given template template parameter.
  133. template <template <class...> class _Container, input_range _Range, class... _Args>
  134. struct _Deducer {
  135. _LIBCPP_HIDE_FROM_ABI static constexpr auto __deduce_func() {
  136. using _InputIter = __minimal_input_iterator<_Range>;
  137. // Case 1 -- can construct directly from the given range.
  138. if constexpr (requires { _Container(std::declval<_Range>(), std::declval<_Args>()...); }) {
  139. using _Result = decltype( //
  140. _Container(std::declval<_Range>(), std::declval<_Args>()...));
  141. return type_identity<_Result>{};
  142. // Case 2 -- can construct from the given range using the `from_range_t` tagged constructor.
  143. } else if constexpr ( //
  144. requires { _Container(from_range, std::declval<_Range>(), std::declval<_Args>()...); }) {
  145. using _Result = //
  146. decltype(_Container(from_range, std::declval<_Range>(), std::declval<_Args>()...));
  147. return type_identity<_Result>{};
  148. // Case 3 -- can construct from a begin-end iterator pair.
  149. } else if constexpr ( //
  150. requires { _Container(std::declval<_InputIter>(), std::declval<_InputIter>(), std::declval<_Args>()...); }) {
  151. using _Result =
  152. decltype(_Container(std::declval<_InputIter>(), std::declval<_InputIter>(), std::declval<_Args>()...));
  153. return type_identity<_Result>{};
  154. } else {
  155. static_assert(__always_false<_Range>,
  156. "ranges::to: unable to deduce the container type from the template template argument.");
  157. }
  158. }
  159. using type = typename decltype(__deduce_func())::type;
  160. };
  161. // `ranges::to` specialization -- `_Container` is a template template parameter requiring deduction to figure out the
  162. // container element type.
  163. template <template <class...> class _Container, input_range _Range, class... _Args>
  164. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto to(_Range&& __range, _Args&&... __args) {
  165. using _DeduceExpr = typename _Deducer<_Container, _Range, _Args...>::type;
  166. return ranges::to<_DeduceExpr>(std::forward<_Range>(__range), std::forward<_Args>(__args)...);
  167. }
  168. // Range adaptor closure object 1 -- wrapping the `ranges::to` version where `_Container` is a simple type template
  169. // parameter.
  170. template <class _Container, class... _Args>
  171. requires(!view<_Container>)
  172. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto to(_Args&&... __args) {
  173. // Mandates: C is a cv-unqualified class type.
  174. static_assert(!is_const_v<_Container>, "The target container cannot be const-qualified, please remove the const");
  175. static_assert(
  176. !is_volatile_v<_Container>, "The target container cannot be volatile-qualified, please remove the volatile");
  177. auto __to_func = []<input_range _Range, class... _Tail>(_Range && __range, _Tail && ... __tail)
  178. requires requires { //
  179. /**/ ranges::to<_Container>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);
  180. }
  181. {
  182. return ranges::to<_Container>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);
  183. };
  184. return __range_adaptor_closure_t(std::__bind_back(__to_func, std::forward<_Args>(__args)...));
  185. }
  186. // Range adaptor closure object 2 -- wrapping the `ranges::to` version where `_Container` is a template template
  187. // parameter.
  188. template <template <class...> class _Container, class... _Args>
  189. _LIBCPP_NODISCARD_EXT _LIBCPP_HIDE_FROM_ABI constexpr auto to(_Args&&... __args) {
  190. // clang-format off
  191. auto __to_func = []<input_range _Range, class... _Tail,
  192. class _DeducedExpr = typename _Deducer<_Container, _Range, _Tail...>::type>
  193. (_Range&& __range, _Tail&& ... __tail)
  194. requires requires { //
  195. /**/ ranges::to<_DeducedExpr>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);
  196. }
  197. {
  198. return ranges::to<_DeducedExpr>(std::forward<_Range>(__range), std::forward<_Tail>(__tail)...);
  199. };
  200. // clang-format on
  201. return __range_adaptor_closure_t(std::__bind_back(__to_func, std::forward<_Args>(__args)...));
  202. }
  203. } // namespace ranges
  204. #endif // _LIBCPP_STD_VER >= 23
  205. _LIBCPP_END_NAMESPACE_STD
  206. #endif // _LIBCPP___RANGES_TO_H