ranges 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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
  10. #define _LIBCPP_RANGES
  11. /*
  12. #include <compare> // see [compare.syn]
  13. #include <initializer_list> // see [initializer.list.syn]
  14. #include <iterator> // see [iterator.synopsis]
  15. namespace std::ranges {
  16. inline namespace unspecified {
  17. // [range.access], range access
  18. inline constexpr unspecified begin = unspecified;
  19. inline constexpr unspecified end = unspecified;
  20. inline constexpr unspecified cbegin = unspecified;
  21. inline constexpr unspecified cend = unspecified;
  22. inline constexpr unspecified size = unspecified;
  23. inline constexpr unspecified ssize = unspecified;
  24. }
  25. // [range.range], ranges
  26. template<class T>
  27. concept range = see below;
  28. template<class T>
  29. inline constexpr bool enable_borrowed_range = false;
  30. template<class T>
  31. using iterator_t = decltype(ranges::begin(declval<T&>()));
  32. template<range R>
  33. using sentinel_t = decltype(ranges::end(declval<R&>()));
  34. template<range R>
  35. using range_difference_t = iter_difference_t<iterator_t<R>>;
  36. template<sized_range R>
  37. using range_size_t = decltype(ranges::size(declval<R&>()));
  38. template<range R>
  39. using range_value_t = iter_value_t<iterator_t<R>>;
  40. template<range R>
  41. using range_reference_t = iter_reference_t<iterator_t<R>>;
  42. template<range R>
  43. using range_rvalue_reference_t = iter_rvalue_reference_t<iterator_t<R>>;
  44. // [range.sized], sized ranges
  45. template<class>
  46. inline constexpr bool disable_sized_range = false;
  47. template<class T>
  48. concept sized_range = ...;
  49. // [range.view], views
  50. template<class T>
  51. inline constexpr bool enable_view = ...;
  52. struct view_base { };
  53. template<class T>
  54. concept view = ...;
  55. // [range.refinements], other range refinements
  56. template<class R, class T>
  57. concept output_range = see below;
  58. template<class T>
  59. concept input_range = see below;
  60. template<class T>
  61. concept forward_range = see below;
  62. template<class T>
  63. concept bidirectional_range = see below;
  64. template<class T>
  65. concept random_access_range = see below;
  66. template<class T>
  67. concept contiguous_range = see below;
  68. template <class _Tp>
  69. concept common_range = see below;
  70. template<class T>
  71. concept viewable_range = see below;
  72. // [view.interface], class template view_interface
  73. template<class D>
  74. requires is_class_v<D> && same_as<D, remove_cv_t<D>>
  75. class view_interface;
  76. // [range.subrange], sub-ranges
  77. enum class subrange_kind : bool { unsized, sized };
  78. template<input_or_output_iterator I, sentinel_for<I> S = I, subrange_kind K = see below>
  79. requires (K == subrange_kind::sized || !sized_sentinel_for<S, I>)
  80. class subrange;
  81. template<class I, class S, subrange_kind K>
  82. inline constexpr bool enable_borrowed_range<subrange<I, S, K>> = true;
  83. // [range.dangling], dangling iterator handling
  84. struct dangling;
  85. template<range R>
  86. using borrowed_iterator_t = see below;
  87. template<range R>
  88. using borrowed_subrange_t = see below;
  89. // [range.empty], empty view
  90. template<class T>
  91. requires is_object_v<T>
  92. class empty_view;
  93. template<class T>
  94. inline constexpr bool enable_borrowed_range<empty_view<T>> = true;
  95. namespace views {
  96. template<class T>
  97. inline constexpr empty_view<T> empty{};
  98. }
  99. // [range.all], all view
  100. namespace views {
  101. inline constexpr unspecified all = unspecified;
  102. template<viewable_range R>
  103. using all_t = decltype(all(declval<R>()));
  104. }
  105. template<range R>
  106. requires is_object_v<R>
  107. class ref_view;
  108. template<class T>
  109. inline constexpr bool enable_borrowed_range<ref_view<T>> = true;
  110. template<range R>
  111. requires see below
  112. class owning_view;
  113. template<class T>
  114. inline constexpr bool enable_borrowed_range<owning_view<T>> = enable_borrowed_range<T>;
  115. // [range.filter], filter view
  116. template<input_range V, indirect_unary_predicate<iterator_t<V>> Pred>
  117. requires view<V> && is_object_v<Pred>
  118. class filter_view;
  119. namespace views {
  120. inline constexpr unspecified filter = unspecified;
  121. }
  122. // [range.drop], drop view
  123. template<view V>
  124. class drop_view;
  125. template<class T>
  126. inline constexpr bool enable_borrowed_range<drop_view<T>> = enable_borrowed_range<T>;
  127. // [range.transform], transform view
  128. template<input_range V, copy_constructible F>
  129. requires view<V> && is_object_v<F> &&
  130. regular_invocable<F&, range_reference_t<V>> &&
  131. can-reference<invoke_result_t<F&, range_reference_t<V>>>
  132. class transform_view;
  133. // [range.counted], counted view
  134. namespace views { inline constexpr unspecified counted = unspecified; }
  135. // [range.common], common view
  136. template<view V>
  137. requires (!common_range<V> && copyable<iterator_t<V>>)
  138. class common_view;
  139. // [range.reverse], reverse view
  140. template<view V>
  141. requires bidirectional_range<V>
  142. class reverse_view;
  143. template<class T>
  144. inline constexpr bool enable_borrowed_range<reverse_view<T>> = enable_borrowed_range<T>;
  145. template<class T>
  146. inline constexpr bool enable_borrowed_range<common_view<T>> = enable_borrowed_range<T>;
  147. // [range.take], take view
  148. template<view> class take_view;
  149. template<class T>
  150. inline constexpr bool enable_borrowed_range<take_view<T>> = enable_borrowed_range<T>;
  151. template<copy_constructible T>
  152. requires is_object_v<T>
  153. class single_view;
  154. template<weakly_incrementable W, semiregular Bound = unreachable_sentinel_t>
  155. requires weakly-equality-comparable-with<W, Bound> && copyable<W>
  156. class iota_view;
  157. template<class W, class Bound>
  158. inline constexpr bool enable_borrowed_range<iota_view<W, Bound>> = true;
  159. // [range.join], join view
  160. template<input_range V>
  161. requires view<V> && input_range<range_reference_t<V>>
  162. class join_view;
  163. // [range.lazy.split], lazy split view
  164. template<class R>
  165. concept tiny-range = see below; // exposition only
  166. template<input_range V, forward_range Pattern>
  167. requires view<V> && view<Pattern> &&
  168. indirectly_comparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to> &&
  169. (forward_range<V> || tiny-range<Pattern>)
  170. class lazy_split_view;
  171. namespace views {
  172. inline constexpr unspecified lazy_split = unspecified;
  173. }
  174. // [range.zip], zip view
  175. template<input_range... Views>
  176. requires (view<Views> && ...) && (sizeof...(Views) > 0)
  177. class zip_view; // C++2b
  178. template<class... Views>
  179. inline constexpr bool enable_borrowed_range<zip_view<Views...>> = // C++2b
  180. (enable_borrowed_range<Views> && ...);
  181. namespace views { inline constexpr unspecified zip = unspecified; } // C++2b
  182. }
  183. namespace std {
  184. namespace views = ranges::views;
  185. template<class T> struct tuple_size;
  186. template<size_t I, class T> struct tuple_element;
  187. template<class I, class S, ranges::subrange_kind K>
  188. struct tuple_size<ranges::subrange<I, S, K>>
  189. : integral_constant<size_t, 2> {};
  190. template<class I, class S, ranges::subrange_kind K>
  191. struct tuple_element<0, ranges::subrange<I, S, K>> {
  192. using type = I;
  193. };
  194. template<class I, class S, ranges::subrange_kind K>
  195. struct tuple_element<1, ranges::subrange<I, S, K>> {
  196. using type = S;
  197. };
  198. template<class I, class S, ranges::subrange_kind K>
  199. struct tuple_element<0, const ranges::subrange<I, S, K>> {
  200. using type = I;
  201. };
  202. template<class I, class S, ranges::subrange_kind K>
  203. struct tuple_element<1, const ranges::subrange<I, S, K>> {
  204. using type = S;
  205. };
  206. }
  207. */
  208. #include <__assert> // all public C++ headers provide the assertion handler
  209. #include <__config>
  210. #include <__ranges/access.h>
  211. #include <__ranges/all.h>
  212. #include <__ranges/common_view.h>
  213. #include <__ranges/concepts.h>
  214. #include <__ranges/counted.h>
  215. #include <__ranges/dangling.h>
  216. #include <__ranges/data.h>
  217. #include <__ranges/drop_view.h>
  218. #include <__ranges/empty.h>
  219. #include <__ranges/empty_view.h>
  220. #include <__ranges/enable_borrowed_range.h>
  221. #include <__ranges/enable_view.h>
  222. #include <__ranges/filter_view.h>
  223. #include <__ranges/iota_view.h>
  224. #include <__ranges/join_view.h>
  225. #include <__ranges/lazy_split_view.h>
  226. #include <__ranges/rbegin.h>
  227. #include <__ranges/ref_view.h>
  228. #include <__ranges/rend.h>
  229. #include <__ranges/reverse_view.h>
  230. #include <__ranges/single_view.h>
  231. #include <__ranges/size.h>
  232. #include <__ranges/subrange.h>
  233. #include <__ranges/take_view.h>
  234. #include <__ranges/transform_view.h>
  235. #include <__ranges/view_interface.h>
  236. #include <__ranges/views.h>
  237. #include <__ranges/zip_view.h>
  238. #include <__tuple> // TODO: <ranges> has to export std::tuple_size. Replace this, once <tuple> is granularized.
  239. #include <compare> // Required by the standard.
  240. #include <initializer_list> // Required by the standard.
  241. #include <iterator> // Required by the standard.
  242. #include <type_traits>
  243. #include <version>
  244. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  245. # pragma GCC system_header
  246. #endif
  247. #endif // _LIBCPP_RANGES