486 lines · plain
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP_RANGES11#define _LIBCPP_RANGES12 13/*14 15#include <compare> // see [compare.syn]16#include <initializer_list> // see [initializer.list.syn]17#include <iterator> // see [iterator.synopsis]18 19namespace std::ranges {20 inline namespace unspecified {21 // [range.access], range access22 inline constexpr unspecified begin = unspecified;23 inline constexpr unspecified end = unspecified;24 inline constexpr unspecified cbegin = unspecified;25 inline constexpr unspecified cend = unspecified;26 27 inline constexpr unspecified size = unspecified;28 inline constexpr unspecified ssize = unspecified;29 }30 31 // [range.range], ranges32 template<class T>33 concept range = see below;34 35 template<class T>36 inline constexpr bool enable_borrowed_range = false;37 38 template<class T>39 using iterator_t = decltype(ranges::begin(declval<T&>()));40 template<range R>41 using sentinel_t = decltype(ranges::end(declval<R&>()));42 template<range R>43 using range_difference_t = iter_difference_t<iterator_t<R>>;44 template<sized_range R>45 using range_size_t = decltype(ranges::size(declval<R&>()));46 template<range R>47 using range_value_t = iter_value_t<iterator_t<R>>;48 template<range R>49 using range_reference_t = iter_reference_t<iterator_t<R>>;50 template<range R>51 using range_rvalue_reference_t = iter_rvalue_reference_t<iterator_t<R>>;52 template <range R>53 using range_common_reference_t = iter_common_reference_t<iterator_t<R>>;54 55 // [range.sized], sized ranges56 template<class>57 inline constexpr bool disable_sized_range = false;58 59 template<class T>60 concept sized_range = ...;61 62 // [range.view], views63 template<class T>64 inline constexpr bool enable_view = ...;65 66 struct view_base { };67 68 template<class T>69 concept view = ...;70 71 // [range.refinements], other range refinements72 template<class R, class T>73 concept output_range = see below;74 75 template<class T>76 concept input_range = see below;77 78 template<class T>79 concept forward_range = see below;80 81 template<class T>82 concept bidirectional_range = see below;83 84 template<class T>85 concept random_access_range = see below;86 87 template<class T>88 concept contiguous_range = see below;89 90 template <class _Tp>91 concept common_range = see below;92 93 template<class T>94 concept viewable_range = see below;95 96 // [range.adaptor.object], range adaptor objects97 template<class D>98 requires is_class_v<D> && same_as<D, remove_cv_t<D>>99 class range_adaptor_closure { }; // Since c++23100 101 // [view.interface], class template view_interface102 template<class D>103 requires is_class_v<D> && same_as<D, remove_cv_t<D>>104 class view_interface;105 106 // [range.subrange], sub-ranges107 enum class subrange_kind : bool { unsized, sized };108 109 template<input_or_output_iterator I, sentinel_for<I> S = I, subrange_kind K = see below>110 requires (K == subrange_kind::sized || !sized_sentinel_for<S, I>)111 class subrange;112 113 template<class I, class S, subrange_kind K>114 inline constexpr bool enable_borrowed_range<subrange<I, S, K>> = true;115 116 // [range.dangling], dangling iterator handling117 struct dangling;118 119 template<range R>120 using borrowed_iterator_t = see below;121 122 template<range R>123 using borrowed_subrange_t = see below;124 125 // [range.elements], elements view126 template<input_range V, size_t N>127 requires see below128 class elements_view;129 130 template<class T, size_t N>131 inline constexpr bool enable_borrowed_range<elements_view<T, N>> =132 enable_borrowed_range<T>;133 134 template<class R>135 using keys_view = elements_view<R, 0>;136 template<class R>137 using values_view = elements_view<R, 1>;138 139 namespace views {140 template<size_t N>141 inline constexpr unspecified elements = unspecified;142 inline constexpr auto keys = elements<0>;143 inline constexpr auto values = elements<1>;144 }145 146 // [range.utility.conv], range conversions147 template<class C, input_range R, class... Args> requires (!view<C>)148 constexpr C to(R&& r, Args&&... args); // Since C++23149 template<template<class...> class C, input_range R, class... Args>150 constexpr auto to(R&& r, Args&&... args); // Since C++23151 template<class C, class... Args> requires (!view<C>)152 constexpr auto to(Args&&... args); // Since C++23153 template<template<class...> class C, class... Args>154 constexpr auto to(Args&&... args); // Since C++23155 156 // [range.empty], empty view157 template<class T>158 requires is_object_v<T>159 class empty_view;160 161 template<class T>162 inline constexpr bool enable_borrowed_range<empty_view<T>> = true;163 164 namespace views {165 template<class T>166 inline constexpr empty_view<T> empty{};167 }168 169 // [range.all], all view170 namespace views {171 inline constexpr unspecified all = unspecified;172 173 template<viewable_range R>174 using all_t = decltype(all(declval<R>()));175 }176 177 template<range R>178 requires is_object_v<R>179 class ref_view;180 181 template<class T>182 inline constexpr bool enable_borrowed_range<ref_view<T>> = true;183 184 template<range R>185 requires see below186 class owning_view;187 188 template<class T>189 inline constexpr bool enable_borrowed_range<owning_view<T>> = enable_borrowed_range<T>;190 191 // [range.filter], filter view192 template<input_range V, indirect_unary_predicate<iterator_t<V>> Pred>193 requires view<V> && is_object_v<Pred>194 class filter_view;195 196 namespace views {197 inline constexpr unspecified filter = unspecified;198 }199 200 // [range.drop], drop view201 template<view V>202 class drop_view;203 204 template<class T>205 inline constexpr bool enable_borrowed_range<drop_view<T>> = enable_borrowed_range<T>;206 207 // [range.drop.while], drop while view208 template<view V, class Pred>209 requires input_range<V> && is_object_v<Pred> &&210 indirect_unary_predicate<const Pred, iterator_t<V>>211 class drop_while_view;212 213 template<class T, class Pred>214 inline constexpr bool enable_borrowed_range<drop_while_view<T, Pred>> =215 enable_borrowed_range<T>;216 217 namespace views { inline constexpr unspecified drop_while = unspecified; }218 219 // [range.transform], transform view220 template<input_range V, copy_constructible F>221 requires view<V> && is_object_v<F> &&222 regular_invocable<F&, range_reference_t<V>> &&223 can-reference<invoke_result_t<F&, range_reference_t<V>>>224 class transform_view;225 226 // [range.counted], counted view227 namespace views { inline constexpr unspecified counted = unspecified; }228 229 // [range.common], common view230 template<view V>231 requires (!common_range<V> && copyable<iterator_t<V>>)232 class common_view;233 234 // [range.reverse], reverse view235 template<view V>236 requires bidirectional_range<V>237 class reverse_view;238 239 template<class T>240 inline constexpr bool enable_borrowed_range<reverse_view<T>> = enable_borrowed_range<T>;241 242 template<class T>243 inline constexpr bool enable_borrowed_range<common_view<T>> = enable_borrowed_range<T>;244 245 // [range.take], take view246 template<view> class take_view;247 248 template<class T>249 inline constexpr bool enable_borrowed_range<take_view<T>> = enable_borrowed_range<T>;250 251 // [range.take.while], take while view252 template<view V, class Pred>253 requires input_range<V> && is_object_v<Pred> &&254 indirect_unary_predicate<const Pred, iterator_t<V>>255 class take_while_view;256 257 namespace views { inline constexpr unspecified take_while = unspecified; }258 259 template<copy_constructible T>260 requires is_object_v<T>261 class single_view;262 263 template<weakly_incrementable W, semiregular Bound = unreachable_sentinel_t>264 requires weakly-equality-comparable-with<W, Bound> && copyable<W>265 class iota_view;266 267 template<class W, class Bound>268 inline constexpr bool enable_borrowed_range<iota_view<W, Bound>> = true;269 270 namespace views {271 inline constexpr unspecified iota = unspecified;272 inline constexpr unspecified indices = unspecified; // Since C++26273 }274 275 // [range.repeat], repeat view276 template<class T>277 concept integer-like-with-usable-difference-type = // exposition only278 is-signed-integer-like<T> || (is-integer-like<T> && weakly_incrementable<T>);279 280 template<move_constructible T, semiregular Bound = unreachable_sentinel_t>281 requires (is_object_v<T> && same_as<T, remove_cv_t<T>> &&282 (integer-like-with-usable-difference-type<Bound> ||283 same_as<Bound, unreachable_sentinel_t>))284 class repeat_view;285 286 namespace views { inline constexpr unspecified repeat = unspecified; }287 288 // [range.join], join view289 template<input_range V>290 requires view<V> && input_range<range_reference_t<V>>291 class join_view;292 293 // [range.join.with], join with view294 template<input_range V, forward_range Pattern>295 requires view<V> && input_range<range_reference_t<V>>296 && view<Pattern>297 && concatable<range_reference_t<V>, Pattern>298 class join_with_view; // since C++23299 300 namespace views { inline constexpr unspecified join_with = unspecified; } // since C++23301 302 // [range.lazy.split], lazy split view303 template<class R>304 concept tiny-range = see below; // exposition only305 306 template<input_range V, forward_range Pattern>307 requires view<V> && view<Pattern> &&308 indirectly_comparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to> &&309 (forward_range<V> || tiny-range<Pattern>)310 class lazy_split_view;311 312 // [range.split], split view313 template<forward_range V, forward_range Pattern>314 requires view<V> && view<Pattern> &&315 indirectly_comparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to>316 class split_view;317 318 namespace views {319 inline constexpr unspecified lazy_split = unspecified;320 inline constexpr unspecified split = unspecified;321 }322 323 // [range.istream], istream view324 template<movable Val, class CharT, class Traits = char_traits<CharT>>325 requires see below326 class basic_istream_view;327 328 template<class Val>329 using istream_view = basic_istream_view<Val, char>;330 331 template<class Val>332 using wistream_view = basic_istream_view<Val, wchar_t>;333 334 namespace views { template<class T> inline constexpr unspecified istream = unspecified; }335 336 // [range.zip], zip view337 template<input_range... Views>338 requires (view<Views> && ...) && (sizeof...(Views) > 0)339 class zip_view; // C++23340 341 template<class... Views>342 inline constexpr bool enable_borrowed_range<zip_view<Views...>> = // C++23343 (enable_borrowed_range<Views> && ...);344 345 namespace views { inline constexpr unspecified zip = unspecified; } // C++23346 347 // [range.zip.transform], zip transform view348 template<move_constructible F, input_range... Views>349 requires (view<Views> && ...) && (sizeof...(Views) > 0) && is_object_v<F> &&350 regular_invocable<F&, range_reference_t<Views>...> &&351 can-reference<invoke_result_t<F&, range_reference_t<Views>...>>352 class zip_transform_view; // C++23353 354 namespace views { inline constexpr unspecified zip_transform = unspecified; } // C++23355 356 357 // [range.as.rvalue]358 template <view V>359 requires input_range<V>360 class as_rvalue_view; // C++23361 362 namespace views { inline constexpr unspecified as_rvalue ) unspecified; } // C++23363 364 [range.chunk.by]365 template<forward_range V, indirect_binary_predicate<iterator_t<V>, iterator_t<V>> Pred>366 requires view<V> && is_object_v<Pred>367 class chunk_by_view; // C++23368 369 namespace views { inline constexpr unspecified chunk_by = unspecified; } // C++23370}371 372namespace std {373 namespace views = ranges::views;374 375 template<class T> struct tuple_size;376 template<size_t I, class T> struct tuple_element;377 378 template<class I, class S, ranges::subrange_kind K>379 struct tuple_size<ranges::subrange<I, S, K>>380 : integral_constant<size_t, 2> {};381 382 template<class I, class S, ranges::subrange_kind K>383 struct tuple_element<0, ranges::subrange<I, S, K>> {384 using type = I;385 };386 387 template<class I, class S, ranges::subrange_kind K>388 struct tuple_element<1, ranges::subrange<I, S, K>> {389 using type = S;390 };391 392 template<class I, class S, ranges::subrange_kind K>393 struct tuple_element<0, const ranges::subrange<I, S, K>> {394 using type = I;395 };396 397 template<class I, class S, ranges::subrange_kind K>398 struct tuple_element<1, const ranges::subrange<I, S, K>> {399 using type = S;400 };401 402 struct from_range_t { explicit from_range_t() = default; }; // Since C++23403 inline constexpr from_range_t from_range{}; // Since C++23404}405*/406 407#if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)408# include <__cxx03/__config>409#else410# include <__config>411 412# if _LIBCPP_STD_VER >= 20413# include <__ranges/access.h>414# include <__ranges/all.h>415# include <__ranges/common_view.h>416# include <__ranges/concepts.h>417# include <__ranges/counted.h>418# include <__ranges/dangling.h>419# include <__ranges/data.h>420# include <__ranges/drop_view.h>421# include <__ranges/drop_while_view.h>422# include <__ranges/elements_view.h>423# include <__ranges/empty.h>424# include <__ranges/empty_view.h>425# include <__ranges/enable_borrowed_range.h>426# include <__ranges/enable_view.h>427# include <__ranges/filter_view.h>428# include <__ranges/iota_view.h>429# include <__ranges/join_view.h>430# include <__ranges/lazy_split_view.h>431# include <__ranges/rbegin.h>432# include <__ranges/ref_view.h>433# include <__ranges/rend.h>434# include <__ranges/reverse_view.h>435# include <__ranges/single_view.h>436# include <__ranges/size.h>437# include <__ranges/split_view.h>438# include <__ranges/subrange.h>439# include <__ranges/take_view.h>440# include <__ranges/take_while_view.h>441# include <__ranges/transform_view.h>442# include <__ranges/view_interface.h>443# include <__ranges/views.h>444 445# if _LIBCPP_HAS_LOCALIZATION446# include <__ranges/istream_view.h>447# endif448# endif449 450# if _LIBCPP_STD_VER >= 23451# include <__ranges/as_rvalue_view.h>452# include <__ranges/chunk_by_view.h>453# include <__ranges/from_range.h>454# include <__ranges/join_with_view.h>455# include <__ranges/repeat_view.h>456# include <__ranges/to.h>457# include <__ranges/zip_transform_view.h>458# include <__ranges/zip_view.h>459# endif460 461# include <version>462 463// standard-mandated includes464 465// [ranges.syn]466# include <compare>467# include <initializer_list>468# include <iterator>469 470// [tuple.helper]471# include <__tuple/tuple_element.h>472# include <__tuple/tuple_size.h>473 474# if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)475# pragma GCC system_header476# endif477 478# if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20479# include <cstdlib>480# include <iosfwd>481# include <type_traits>482# endif483#endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)484 485#endif // _LIBCPP_RANGES486