ranges 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. template <range R>
  45. using range_common_reference_t = iter_common_reference_t<iterator_t<R>>;
  46. // [range.sized], sized ranges
  47. template<class>
  48. inline constexpr bool disable_sized_range = false;
  49. template<class T>
  50. concept sized_range = ...;
  51. // [range.view], views
  52. template<class T>
  53. inline constexpr bool enable_view = ...;
  54. struct view_base { };
  55. template<class T>
  56. concept view = ...;
  57. // [range.refinements], other range refinements
  58. template<class R, class T>
  59. concept output_range = see below;
  60. template<class T>
  61. concept input_range = see below;
  62. template<class T>
  63. concept forward_range = see below;
  64. template<class T>
  65. concept bidirectional_range = see below;
  66. template<class T>
  67. concept random_access_range = see below;
  68. template<class T>
  69. concept contiguous_range = see below;
  70. template <class _Tp>
  71. concept common_range = see below;
  72. template<class T>
  73. concept viewable_range = see below;
  74. // [view.interface], class template view_interface
  75. template<class D>
  76. requires is_class_v<D> && same_as<D, remove_cv_t<D>>
  77. class view_interface;
  78. // [range.subrange], sub-ranges
  79. enum class subrange_kind : bool { unsized, sized };
  80. template<input_or_output_iterator I, sentinel_for<I> S = I, subrange_kind K = see below>
  81. requires (K == subrange_kind::sized || !sized_sentinel_for<S, I>)
  82. class subrange;
  83. template<class I, class S, subrange_kind K>
  84. inline constexpr bool enable_borrowed_range<subrange<I, S, K>> = true;
  85. // [range.dangling], dangling iterator handling
  86. struct dangling;
  87. template<range R>
  88. using borrowed_iterator_t = see below;
  89. template<range R>
  90. using borrowed_subrange_t = see below;
  91. // [range.elements], elements view
  92. template<input_range V, size_t N>
  93. requires see below
  94. class elements_view;
  95. template<class T, size_t N>
  96. inline constexpr bool enable_borrowed_range<elements_view<T, N>> =
  97. enable_borrowed_range<T>;
  98. template<class R>
  99. using keys_view = elements_view<R, 0>;
  100. template<class R>
  101. using values_view = elements_view<R, 1>;
  102. namespace views {
  103. template<size_t N>
  104. inline constexpr unspecified elements = unspecified;
  105. inline constexpr auto keys = elements<0>;
  106. inline constexpr auto values = elements<1>;
  107. }
  108. // [range.utility.conv], range conversions
  109. template<class C, input_range R, class... Args> requires (!view<C>)
  110. constexpr C to(R&& r, Args&&... args); // Since C++23
  111. template<template<class...> class C, input_range R, class... Args>
  112. constexpr auto to(R&& r, Args&&... args); // Since C++23
  113. template<class C, class... Args> requires (!view<C>)
  114. constexpr auto to(Args&&... args); // Since C++23
  115. template<template<class...> class C, class... Args>
  116. constexpr auto to(Args&&... args); // Since C++23
  117. // [range.empty], empty view
  118. template<class T>
  119. requires is_object_v<T>
  120. class empty_view;
  121. template<class T>
  122. inline constexpr bool enable_borrowed_range<empty_view<T>> = true;
  123. namespace views {
  124. template<class T>
  125. inline constexpr empty_view<T> empty{};
  126. }
  127. // [range.all], all view
  128. namespace views {
  129. inline constexpr unspecified all = unspecified;
  130. template<viewable_range R>
  131. using all_t = decltype(all(declval<R>()));
  132. }
  133. template<range R>
  134. requires is_object_v<R>
  135. class ref_view;
  136. template<class T>
  137. inline constexpr bool enable_borrowed_range<ref_view<T>> = true;
  138. template<range R>
  139. requires see below
  140. class owning_view;
  141. template<class T>
  142. inline constexpr bool enable_borrowed_range<owning_view<T>> = enable_borrowed_range<T>;
  143. // [range.filter], filter view
  144. template<input_range V, indirect_unary_predicate<iterator_t<V>> Pred>
  145. requires view<V> && is_object_v<Pred>
  146. class filter_view;
  147. namespace views {
  148. inline constexpr unspecified filter = unspecified;
  149. }
  150. // [range.drop], drop view
  151. template<view V>
  152. class drop_view;
  153. template<class T>
  154. inline constexpr bool enable_borrowed_range<drop_view<T>> = enable_borrowed_range<T>;
  155. // [range.drop.while], drop while view
  156. template<view V, class Pred>
  157. requires input_range<V> && is_object_v<Pred> &&
  158. indirect_unary_predicate<const Pred, iterator_t<V>>
  159. class drop_while_view;
  160. template<class T, class Pred>
  161. inline constexpr bool enable_borrowed_range<drop_while_view<T, Pred>> =
  162. enable_borrowed_range<T>;
  163. namespace views { inline constexpr unspecified drop_while = unspecified; }
  164. // [range.transform], transform view
  165. template<input_range V, copy_constructible F>
  166. requires view<V> && is_object_v<F> &&
  167. regular_invocable<F&, range_reference_t<V>> &&
  168. can-reference<invoke_result_t<F&, range_reference_t<V>>>
  169. class transform_view;
  170. // [range.counted], counted view
  171. namespace views { inline constexpr unspecified counted = unspecified; }
  172. // [range.common], common view
  173. template<view V>
  174. requires (!common_range<V> && copyable<iterator_t<V>>)
  175. class common_view;
  176. // [range.reverse], reverse view
  177. template<view V>
  178. requires bidirectional_range<V>
  179. class reverse_view;
  180. template<class T>
  181. inline constexpr bool enable_borrowed_range<reverse_view<T>> = enable_borrowed_range<T>;
  182. template<class T>
  183. inline constexpr bool enable_borrowed_range<common_view<T>> = enable_borrowed_range<T>;
  184. // [range.take], take view
  185. template<view> class take_view;
  186. template<class T>
  187. inline constexpr bool enable_borrowed_range<take_view<T>> = enable_borrowed_range<T>;
  188. // [range.take.while], take while view
  189. template<view V, class Pred>
  190. requires input_range<V> && is_object_v<Pred> &&
  191. indirect_unary_predicate<const Pred, iterator_t<V>>
  192. class take_while_view;
  193. namespace views { inline constexpr unspecified take_while = unspecified; }
  194. template<copy_constructible T>
  195. requires is_object_v<T>
  196. class single_view;
  197. template<weakly_incrementable W, semiregular Bound = unreachable_sentinel_t>
  198. requires weakly-equality-comparable-with<W, Bound> && copyable<W>
  199. class iota_view;
  200. template<class W, class Bound>
  201. inline constexpr bool enable_borrowed_range<iota_view<W, Bound>> = true;
  202. // [range.repeat], repeat view
  203. template<class T>
  204. concept integer-like-with-usable-difference-type = // exposition only
  205. is-signed-integer-like<T> || (is-integer-like<T> && weakly_incrementable<T>);
  206. template<move_constructible T, semiregular Bound = unreachable_sentinel_t>
  207. requires (is_object_v<T> && same_as<T, remove_cv_t<T>> &&
  208. (integer-like-with-usable-difference-type<Bound> ||
  209. same_as<Bound, unreachable_sentinel_t>))
  210. class repeat_view;
  211. namespace views { inline constexpr unspecified repeat = unspecified; }
  212. // [range.join], join view
  213. template<input_range V>
  214. requires view<V> && input_range<range_reference_t<V>>
  215. class join_view;
  216. // [range.lazy.split], lazy split view
  217. template<class R>
  218. concept tiny-range = see below; // exposition only
  219. template<input_range V, forward_range Pattern>
  220. requires view<V> && view<Pattern> &&
  221. indirectly_comparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to> &&
  222. (forward_range<V> || tiny-range<Pattern>)
  223. class lazy_split_view;
  224. // [range.split], split view
  225. template<forward_range V, forward_range Pattern>
  226. requires view<V> && view<Pattern> &&
  227. indirectly_comparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to>
  228. class split_view;
  229. namespace views {
  230. inline constexpr unspecified lazy_split = unspecified;
  231. inline constexpr unspecified split = unspecified;
  232. }
  233. // [range.istream], istream view
  234. template<movable Val, class CharT, class Traits = char_traits<CharT>>
  235. requires see below
  236. class basic_istream_view;
  237. template<class Val>
  238. using istream_view = basic_istream_view<Val, char>;
  239. template<class Val>
  240. using wistream_view = basic_istream_view<Val, wchar_t>;
  241. namespace views { template<class T> inline constexpr unspecified istream = unspecified; }
  242. // [range.zip], zip view
  243. template<input_range... Views>
  244. requires (view<Views> && ...) && (sizeof...(Views) > 0)
  245. class zip_view; // C++23
  246. template<class... Views>
  247. inline constexpr bool enable_borrowed_range<zip_view<Views...>> = // C++23
  248. (enable_borrowed_range<Views> && ...);
  249. namespace views { inline constexpr unspecified zip = unspecified; } // C++23
  250. // [range.as.rvalue]
  251. template <view V>
  252. requires input_range<V>
  253. class as_rvalue_view; // C++23
  254. namespace views { inline constexpr unspecified as_rvalue ) unspecified; } // C++23
  255. [range.chunk.by]
  256. template<forward_range V, indirect_binary_predicate<iterator_t<V>, iterator_t<V>> Pred>
  257. requires view<V> && is_object_v<Pred>
  258. class chunk_by_view; // C++23
  259. namespace views { inline constexpr unspecified chunk_by = unspecified; } // C++23
  260. }
  261. namespace std {
  262. namespace views = ranges::views;
  263. template<class T> struct tuple_size;
  264. template<size_t I, class T> struct tuple_element;
  265. template<class I, class S, ranges::subrange_kind K>
  266. struct tuple_size<ranges::subrange<I, S, K>>
  267. : integral_constant<size_t, 2> {};
  268. template<class I, class S, ranges::subrange_kind K>
  269. struct tuple_element<0, ranges::subrange<I, S, K>> {
  270. using type = I;
  271. };
  272. template<class I, class S, ranges::subrange_kind K>
  273. struct tuple_element<1, ranges::subrange<I, S, K>> {
  274. using type = S;
  275. };
  276. template<class I, class S, ranges::subrange_kind K>
  277. struct tuple_element<0, const ranges::subrange<I, S, K>> {
  278. using type = I;
  279. };
  280. template<class I, class S, ranges::subrange_kind K>
  281. struct tuple_element<1, const ranges::subrange<I, S, K>> {
  282. using type = S;
  283. };
  284. struct from_range_t { explicit from_range_t() = default; }; // Since C++23
  285. inline constexpr from_range_t from_range{}; // Since C++23
  286. }
  287. */
  288. #include <__assert> // all public C++ headers provide the assertion handler
  289. #include <__config>
  290. #include <__ranges/access.h>
  291. #include <__ranges/all.h>
  292. #include <__ranges/as_rvalue_view.h>
  293. #include <__ranges/chunk_by_view.h>
  294. #include <__ranges/common_view.h>
  295. #include <__ranges/concepts.h>
  296. #include <__ranges/counted.h>
  297. #include <__ranges/dangling.h>
  298. #include <__ranges/data.h>
  299. #include <__ranges/drop_view.h>
  300. #include <__ranges/drop_while_view.h>
  301. #include <__ranges/elements_view.h>
  302. #include <__ranges/empty.h>
  303. #include <__ranges/empty_view.h>
  304. #include <__ranges/enable_borrowed_range.h>
  305. #include <__ranges/enable_view.h>
  306. #include <__ranges/filter_view.h>
  307. #include <__ranges/from_range.h>
  308. #include <__ranges/iota_view.h>
  309. #include <__ranges/join_view.h>
  310. #include <__ranges/lazy_split_view.h>
  311. #include <__ranges/rbegin.h>
  312. #include <__ranges/ref_view.h>
  313. #include <__ranges/rend.h>
  314. #include <__ranges/repeat_view.h>
  315. #include <__ranges/reverse_view.h>
  316. #include <__ranges/single_view.h>
  317. #include <__ranges/size.h>
  318. #include <__ranges/split_view.h>
  319. #include <__ranges/subrange.h>
  320. #include <__ranges/take_view.h>
  321. #include <__ranges/take_while_view.h>
  322. #include <__ranges/to.h>
  323. #include <__ranges/transform_view.h>
  324. #include <__ranges/view_interface.h>
  325. #include <__ranges/views.h>
  326. #include <__ranges/zip_view.h>
  327. #include <version>
  328. #if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
  329. #include <__ranges/istream_view.h>
  330. #endif
  331. // standard-mandated includes
  332. // [ranges.syn]
  333. #include <compare>
  334. #include <initializer_list>
  335. #include <iterator>
  336. // [tuple.helper]
  337. #include <__tuple/tuple_element.h>
  338. #include <__tuple/tuple_size.h>
  339. #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
  340. # pragma GCC system_header
  341. #endif
  342. #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
  343. # include <cstdlib>
  344. # include <type_traits>
  345. #endif
  346. #endif // _LIBCPP_RANGES