2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_RANGES
11 #define _LIBCPP_RANGES
15 #include <compare> // see [compare.syn]
16 #include <initializer_list> // see [initializer.list.syn]
17 #include <iterator> // see [iterator.synopsis]
19 namespace std::ranges {
20 inline namespace unspecified {
21 // [range.access], range access
22 inline constexpr unspecified begin = unspecified;
23 inline constexpr unspecified end = unspecified;
24 inline constexpr unspecified cbegin = unspecified;
25 inline constexpr unspecified cend = unspecified;
27 inline constexpr unspecified size = unspecified;
28 inline constexpr unspecified ssize = unspecified;
31 // [range.range], ranges
33 concept range = see below;
36 inline constexpr bool enable_borrowed_range = false;
39 using iterator_t = decltype(ranges::begin(declval<T&>()));
41 using sentinel_t = decltype(ranges::end(declval<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&>()));
47 using range_value_t = iter_value_t<iterator_t<R>>;
49 using range_reference_t = iter_reference_t<iterator_t<R>>;
51 using range_rvalue_reference_t = iter_rvalue_reference_t<iterator_t<R>>;
53 using range_common_reference_t = iter_common_reference_t<iterator_t<R>>;
55 // [range.sized], sized ranges
57 inline constexpr bool disable_sized_range = false;
60 concept sized_range = ...;
62 // [range.view], views
64 inline constexpr bool enable_view = ...;
71 // [range.refinements], other range refinements
72 template<class R, class T>
73 concept output_range = see below;
76 concept input_range = see below;
79 concept forward_range = see below;
82 concept bidirectional_range = see below;
85 concept random_access_range = see below;
88 concept contiguous_range = see below;
91 concept common_range = see below;
94 concept viewable_range = see below;
96 // [range.adaptor.object], range adaptor objects
98 requires is_class_v<D> && same_as<D, remove_cv_t<D>>
99 class range_adaptor_closure { }; // Since c++23
101 // [view.interface], class template view_interface
103 requires is_class_v<D> && same_as<D, remove_cv_t<D>>
104 class view_interface;
106 // [range.subrange], sub-ranges
107 enum class subrange_kind : bool { unsized, sized };
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>)
113 template<class I, class S, subrange_kind K>
114 inline constexpr bool enable_borrowed_range<subrange<I, S, K>> = true;
116 // [range.dangling], dangling iterator handling
120 using borrowed_iterator_t = see below;
123 using borrowed_subrange_t = see below;
125 // [range.elements], elements view
126 template<input_range V, size_t N>
130 template<class T, size_t N>
131 inline constexpr bool enable_borrowed_range<elements_view<T, N>> =
132 enable_borrowed_range<T>;
135 using keys_view = elements_view<R, 0>;
137 using values_view = elements_view<R, 1>;
141 inline constexpr unspecified elements = unspecified;
142 inline constexpr auto keys = elements<0>;
143 inline constexpr auto values = elements<1>;
146 // [range.utility.conv], range conversions
147 template<class C, input_range R, class... Args> requires (!view<C>)
148 constexpr C to(R&& r, Args&&... args); // Since C++23
149 template<template<class...> class C, input_range R, class... Args>
150 constexpr auto to(R&& r, Args&&... args); // Since C++23
151 template<class C, class... Args> requires (!view<C>)
152 constexpr auto to(Args&&... args); // Since C++23
153 template<template<class...> class C, class... Args>
154 constexpr auto to(Args&&... args); // Since C++23
156 // [range.empty], empty view
158 requires is_object_v<T>
162 inline constexpr bool enable_borrowed_range<empty_view<T>> = true;
166 inline constexpr empty_view<T> empty{};
169 // [range.all], all view
171 inline constexpr unspecified all = unspecified;
173 template<viewable_range R>
174 using all_t = decltype(all(declval<R>()));
178 requires is_object_v<R>
182 inline constexpr bool enable_borrowed_range<ref_view<T>> = true;
189 inline constexpr bool enable_borrowed_range<owning_view<T>> = enable_borrowed_range<T>;
191 // [range.filter], filter view
192 template<input_range V, indirect_unary_predicate<iterator_t<V>> Pred>
193 requires view<V> && is_object_v<Pred>
197 inline constexpr unspecified filter = unspecified;
200 // [range.drop], drop view
205 inline constexpr bool enable_borrowed_range<drop_view<T>> = enable_borrowed_range<T>;
207 // [range.drop.while], drop while view
208 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;
213 template<class T, class Pred>
214 inline constexpr bool enable_borrowed_range<drop_while_view<T, Pred>> =
215 enable_borrowed_range<T>;
217 namespace views { inline constexpr unspecified drop_while = unspecified; }
219 // [range.transform], transform view
220 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;
226 // [range.counted], counted view
227 namespace views { inline constexpr unspecified counted = unspecified; }
229 // [range.common], common view
231 requires (!common_range<V> && copyable<iterator_t<V>>)
234 // [range.reverse], reverse view
236 requires bidirectional_range<V>
240 inline constexpr bool enable_borrowed_range<reverse_view<T>> = enable_borrowed_range<T>;
243 inline constexpr bool enable_borrowed_range<common_view<T>> = enable_borrowed_range<T>;
245 // [range.take], take view
246 template<view> class take_view;
249 inline constexpr bool enable_borrowed_range<take_view<T>> = enable_borrowed_range<T>;
251 // [range.take.while], take while view
252 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;
257 namespace views { inline constexpr unspecified take_while = unspecified; }
259 template<copy_constructible T>
260 requires is_object_v<T>
263 template<weakly_incrementable W, semiregular Bound = unreachable_sentinel_t>
264 requires weakly-equality-comparable-with<W, Bound> && copyable<W>
267 template<class W, class Bound>
268 inline constexpr bool enable_borrowed_range<iota_view<W, Bound>> = true;
270 // [range.repeat], repeat view
272 concept integer-like-with-usable-difference-type = // exposition only
273 is-signed-integer-like<T> || (is-integer-like<T> && weakly_incrementable<T>);
275 template<move_constructible T, semiregular Bound = unreachable_sentinel_t>
276 requires (is_object_v<T> && same_as<T, remove_cv_t<T>> &&
277 (integer-like-with-usable-difference-type<Bound> ||
278 same_as<Bound, unreachable_sentinel_t>))
281 namespace views { inline constexpr unspecified repeat = unspecified; }
283 // [range.join], join view
284 template<input_range V>
285 requires view<V> && input_range<range_reference_t<V>>
288 // [range.lazy.split], lazy split view
290 concept tiny-range = see below; // exposition only
292 template<input_range V, forward_range Pattern>
293 requires view<V> && view<Pattern> &&
294 indirectly_comparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to> &&
295 (forward_range<V> || tiny-range<Pattern>)
296 class lazy_split_view;
298 // [range.split], split view
299 template<forward_range V, forward_range Pattern>
300 requires view<V> && view<Pattern> &&
301 indirectly_comparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to>
305 inline constexpr unspecified lazy_split = unspecified;
306 inline constexpr unspecified split = unspecified;
309 // [range.istream], istream view
310 template<movable Val, class CharT, class Traits = char_traits<CharT>>
312 class basic_istream_view;
315 using istream_view = basic_istream_view<Val, char>;
318 using wistream_view = basic_istream_view<Val, wchar_t>;
320 namespace views { template<class T> inline constexpr unspecified istream = unspecified; }
322 // [range.zip], zip view
323 template<input_range... Views>
324 requires (view<Views> && ...) && (sizeof...(Views) > 0)
325 class zip_view; // C++23
327 template<class... Views>
328 inline constexpr bool enable_borrowed_range<zip_view<Views...>> = // C++23
329 (enable_borrowed_range<Views> && ...);
331 namespace views { inline constexpr unspecified zip = unspecified; } // C++23
335 requires input_range<V>
336 class as_rvalue_view; // C++23
338 namespace views { inline constexpr unspecified as_rvalue ) unspecified; } // C++23
341 template<forward_range V, indirect_binary_predicate<iterator_t<V>, iterator_t<V>> Pred>
342 requires view<V> && is_object_v<Pred>
343 class chunk_by_view; // C++23
345 namespace views { inline constexpr unspecified chunk_by = unspecified; } // C++23
349 namespace views = ranges::views;
351 template<class T> struct tuple_size;
352 template<size_t I, class T> struct tuple_element;
354 template<class I, class S, ranges::subrange_kind K>
355 struct tuple_size<ranges::subrange<I, S, K>>
356 : integral_constant<size_t, 2> {};
358 template<class I, class S, ranges::subrange_kind K>
359 struct tuple_element<0, ranges::subrange<I, S, K>> {
363 template<class I, class S, ranges::subrange_kind K>
364 struct tuple_element<1, ranges::subrange<I, S, K>> {
368 template<class I, class S, ranges::subrange_kind K>
369 struct tuple_element<0, const ranges::subrange<I, S, K>> {
373 template<class I, class S, ranges::subrange_kind K>
374 struct tuple_element<1, const ranges::subrange<I, S, K>> {
378 struct from_range_t { explicit from_range_t() = default; }; // Since C++23
379 inline constexpr from_range_t from_range{}; // Since C++23
385 #if _LIBCPP_STD_VER >= 20
386 # include <__ranges/access.h>
387 # include <__ranges/all.h>
388 # include <__ranges/common_view.h>
389 # include <__ranges/concepts.h>
390 # include <__ranges/counted.h>
391 # include <__ranges/dangling.h>
392 # include <__ranges/data.h>
393 # include <__ranges/drop_view.h>
394 # include <__ranges/drop_while_view.h>
395 # include <__ranges/elements_view.h>
396 # include <__ranges/empty.h>
397 # include <__ranges/empty_view.h>
398 # include <__ranges/enable_borrowed_range.h>
399 # include <__ranges/enable_view.h>
400 # include <__ranges/filter_view.h>
401 # include <__ranges/iota_view.h>
402 # include <__ranges/join_view.h>
403 # include <__ranges/lazy_split_view.h>
404 # include <__ranges/rbegin.h>
405 # include <__ranges/ref_view.h>
406 # include <__ranges/rend.h>
407 # include <__ranges/reverse_view.h>
408 # include <__ranges/single_view.h>
409 # include <__ranges/size.h>
410 # include <__ranges/split_view.h>
411 # include <__ranges/subrange.h>
412 # include <__ranges/take_view.h>
413 # include <__ranges/take_while_view.h>
414 # include <__ranges/transform_view.h>
415 # include <__ranges/view_interface.h>
416 # include <__ranges/views.h>
418 # if _LIBCPP_HAS_LOCALIZATION
419 # include <__ranges/istream_view.h>
423 #if _LIBCPP_STD_VER >= 23
424 # include <__ranges/as_rvalue_view.h>
425 # include <__ranges/chunk_by_view.h>
426 # include <__ranges/from_range.h>
427 # include <__ranges/repeat_view.h>
428 # include <__ranges/to.h>
429 # include <__ranges/zip_view.h>
434 // standard-mandated includes
438 #include <initializer_list>
442 #include <__tuple/tuple_element.h>
443 #include <__tuple/tuple_size.h>
445 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
446 # pragma GCC system_header
449 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
452 # include <type_traits>
455 #endif // _LIBCPP_RANGES