llvm-project
448 строк · 13.5 Кб
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
10#ifndef _LIBCPP_RANGES
11#define _LIBCPP_RANGES
12
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 {
20inline namespace unspecified {
21// [range.access], range access
22inline constexpr unspecified begin = unspecified;
23inline constexpr unspecified end = unspecified;
24inline constexpr unspecified cbegin = unspecified;
25inline constexpr unspecified cend = unspecified;
26
27inline constexpr unspecified size = unspecified;
28inline constexpr unspecified ssize = unspecified;
29}
30
31// [range.range], ranges
32template<class T>
33concept range = see below;
34
35template<class T>
36inline constexpr bool enable_borrowed_range = false;
37
38template<class T>
39using iterator_t = decltype(ranges::begin(declval<T&>()));
40template<range R>
41using sentinel_t = decltype(ranges::end(declval<R&>()));
42template<range R>
43using range_difference_t = iter_difference_t<iterator_t<R>>;
44template<sized_range R>
45using range_size_t = decltype(ranges::size(declval<R&>()));
46template<range R>
47using range_value_t = iter_value_t<iterator_t<R>>;
48template<range R>
49using range_reference_t = iter_reference_t<iterator_t<R>>;
50template<range R>
51using range_rvalue_reference_t = iter_rvalue_reference_t<iterator_t<R>>;
52template <range R>
53using range_common_reference_t = iter_common_reference_t<iterator_t<R>>;
54
55// [range.sized], sized ranges
56template<class>
57inline constexpr bool disable_sized_range = false;
58
59template<class T>
60concept sized_range = ...;
61
62// [range.view], views
63template<class T>
64inline constexpr bool enable_view = ...;
65
66struct view_base { };
67
68template<class T>
69concept view = ...;
70
71// [range.refinements], other range refinements
72template<class R, class T>
73concept output_range = see below;
74
75template<class T>
76concept input_range = see below;
77
78template<class T>
79concept forward_range = see below;
80
81template<class T>
82concept bidirectional_range = see below;
83
84template<class T>
85concept random_access_range = see below;
86
87template<class T>
88concept contiguous_range = see below;
89
90template <class _Tp>
91concept common_range = see below;
92
93template<class T>
94concept viewable_range = see below;
95
96// [range.adaptor.object], range adaptor objects
97template<class D>
98requires is_class_v<D> && same_as<D, remove_cv_t<D>>
99class range_adaptor_closure { }; // Since c++23
100
101// [view.interface], class template view_interface
102template<class D>
103requires is_class_v<D> && same_as<D, remove_cv_t<D>>
104class view_interface;
105
106// [range.subrange], sub-ranges
107enum class subrange_kind : bool { unsized, sized };
108
109template<input_or_output_iterator I, sentinel_for<I> S = I, subrange_kind K = see below>
110requires (K == subrange_kind::sized || !sized_sentinel_for<S, I>)
111class subrange;
112
113template<class I, class S, subrange_kind K>
114inline constexpr bool enable_borrowed_range<subrange<I, S, K>> = true;
115
116// [range.dangling], dangling iterator handling
117struct dangling;
118
119template<range R>
120using borrowed_iterator_t = see below;
121
122template<range R>
123using borrowed_subrange_t = see below;
124
125// [range.elements], elements view
126template<input_range V, size_t N>
127requires see below
128class elements_view;
129
130template<class T, size_t N>
131inline constexpr bool enable_borrowed_range<elements_view<T, N>> =
132enable_borrowed_range<T>;
133
134template<class R>
135using keys_view = elements_view<R, 0>;
136template<class R>
137using values_view = elements_view<R, 1>;
138
139namespace views {
140template<size_t N>
141inline constexpr unspecified elements = unspecified;
142inline constexpr auto keys = elements<0>;
143inline constexpr auto values = elements<1>;
144}
145
146// [range.utility.conv], range conversions
147template<class C, input_range R, class... Args> requires (!view<C>)
148constexpr C to(R&& r, Args&&... args); // Since C++23
149template<template<class...> class C, input_range R, class... Args>
150constexpr auto to(R&& r, Args&&... args); // Since C++23
151template<class C, class... Args> requires (!view<C>)
152constexpr auto to(Args&&... args); // Since C++23
153template<template<class...> class C, class... Args>
154constexpr auto to(Args&&... args); // Since C++23
155
156// [range.empty], empty view
157template<class T>
158requires is_object_v<T>
159class empty_view;
160
161template<class T>
162inline constexpr bool enable_borrowed_range<empty_view<T>> = true;
163
164namespace views {
165template<class T>
166inline constexpr empty_view<T> empty{};
167}
168
169// [range.all], all view
170namespace views {
171inline constexpr unspecified all = unspecified;
172
173template<viewable_range R>
174using all_t = decltype(all(declval<R>()));
175}
176
177template<range R>
178requires is_object_v<R>
179class ref_view;
180
181template<class T>
182inline constexpr bool enable_borrowed_range<ref_view<T>> = true;
183
184template<range R>
185requires see below
186class owning_view;
187
188template<class T>
189inline constexpr bool enable_borrowed_range<owning_view<T>> = enable_borrowed_range<T>;
190
191// [range.filter], filter view
192template<input_range V, indirect_unary_predicate<iterator_t<V>> Pred>
193requires view<V> && is_object_v<Pred>
194class filter_view;
195
196namespace views {
197inline constexpr unspecified filter = unspecified;
198}
199
200// [range.drop], drop view
201template<view V>
202class drop_view;
203
204template<class T>
205inline constexpr bool enable_borrowed_range<drop_view<T>> = enable_borrowed_range<T>;
206
207// [range.drop.while], drop while view
208template<view V, class Pred>
209requires input_range<V> && is_object_v<Pred> &&
210indirect_unary_predicate<const Pred, iterator_t<V>>
211class drop_while_view;
212
213template<class T, class Pred>
214inline constexpr bool enable_borrowed_range<drop_while_view<T, Pred>> =
215enable_borrowed_range<T>;
216
217namespace views { inline constexpr unspecified drop_while = unspecified; }
218
219// [range.transform], transform view
220template<input_range V, copy_constructible F>
221requires view<V> && is_object_v<F> &&
222regular_invocable<F&, range_reference_t<V>> &&
223can-reference<invoke_result_t<F&, range_reference_t<V>>>
224class transform_view;
225
226// [range.counted], counted view
227namespace views { inline constexpr unspecified counted = unspecified; }
228
229// [range.common], common view
230template<view V>
231requires (!common_range<V> && copyable<iterator_t<V>>)
232class common_view;
233
234// [range.reverse], reverse view
235template<view V>
236requires bidirectional_range<V>
237class reverse_view;
238
239template<class T>
240inline constexpr bool enable_borrowed_range<reverse_view<T>> = enable_borrowed_range<T>;
241
242template<class T>
243inline constexpr bool enable_borrowed_range<common_view<T>> = enable_borrowed_range<T>;
244
245// [range.take], take view
246template<view> class take_view;
247
248template<class T>
249inline constexpr bool enable_borrowed_range<take_view<T>> = enable_borrowed_range<T>;
250
251// [range.take.while], take while view
252template<view V, class Pred>
253requires input_range<V> && is_object_v<Pred> &&
254indirect_unary_predicate<const Pred, iterator_t<V>>
255class take_while_view;
256
257namespace views { inline constexpr unspecified take_while = unspecified; }
258
259template<copy_constructible T>
260requires is_object_v<T>
261class single_view;
262
263template<weakly_incrementable W, semiregular Bound = unreachable_sentinel_t>
264requires weakly-equality-comparable-with<W, Bound> && copyable<W>
265class iota_view;
266
267template<class W, class Bound>
268inline constexpr bool enable_borrowed_range<iota_view<W, Bound>> = true;
269
270// [range.repeat], repeat view
271template<class T>
272concept integer-like-with-usable-difference-type = // exposition only
273is-signed-integer-like<T> || (is-integer-like<T> && weakly_incrementable<T>);
274
275template<move_constructible T, semiregular Bound = unreachable_sentinel_t>
276requires (is_object_v<T> && same_as<T, remove_cv_t<T>> &&
277(integer-like-with-usable-difference-type<Bound> ||
278same_as<Bound, unreachable_sentinel_t>))
279class repeat_view;
280
281namespace views { inline constexpr unspecified repeat = unspecified; }
282
283// [range.join], join view
284template<input_range V>
285requires view<V> && input_range<range_reference_t<V>>
286class join_view;
287
288// [range.lazy.split], lazy split view
289template<class R>
290concept tiny-range = see below; // exposition only
291
292template<input_range V, forward_range Pattern>
293requires view<V> && view<Pattern> &&
294indirectly_comparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to> &&
295(forward_range<V> || tiny-range<Pattern>)
296class lazy_split_view;
297
298// [range.split], split view
299template<forward_range V, forward_range Pattern>
300requires view<V> && view<Pattern> &&
301indirectly_comparable<iterator_t<V>, iterator_t<Pattern>, ranges::equal_to>
302class split_view;
303
304namespace views {
305inline constexpr unspecified lazy_split = unspecified;
306inline constexpr unspecified split = unspecified;
307}
308
309// [range.istream], istream view
310template<movable Val, class CharT, class Traits = char_traits<CharT>>
311requires see below
312class basic_istream_view;
313
314template<class Val>
315using istream_view = basic_istream_view<Val, char>;
316
317template<class Val>
318using wistream_view = basic_istream_view<Val, wchar_t>;
319
320namespace views { template<class T> inline constexpr unspecified istream = unspecified; }
321
322// [range.zip], zip view
323template<input_range... Views>
324requires (view<Views> && ...) && (sizeof...(Views) > 0)
325class zip_view; // C++23
326
327template<class... Views>
328inline constexpr bool enable_borrowed_range<zip_view<Views...>> = // C++23
329(enable_borrowed_range<Views> && ...);
330
331namespace views { inline constexpr unspecified zip = unspecified; } // C++23
332
333// [range.as.rvalue]
334template <view V>
335requires input_range<V>
336class as_rvalue_view; // C++23
337
338namespace views { inline constexpr unspecified as_rvalue ) unspecified; } // C++23
339
340[range.chunk.by]
341template<forward_range V, indirect_binary_predicate<iterator_t<V>, iterator_t<V>> Pred>
342requires view<V> && is_object_v<Pred>
343class chunk_by_view; // C++23
344
345namespace views { inline constexpr unspecified chunk_by = unspecified; } // C++23
346}
347
348namespace std {
349namespace views = ranges::views;
350
351template<class T> struct tuple_size;
352template<size_t I, class T> struct tuple_element;
353
354template<class I, class S, ranges::subrange_kind K>
355struct tuple_size<ranges::subrange<I, S, K>>
356: integral_constant<size_t, 2> {};
357
358template<class I, class S, ranges::subrange_kind K>
359struct tuple_element<0, ranges::subrange<I, S, K>> {
360using type = I;
361};
362
363template<class I, class S, ranges::subrange_kind K>
364struct tuple_element<1, ranges::subrange<I, S, K>> {
365using type = S;
366};
367
368template<class I, class S, ranges::subrange_kind K>
369struct tuple_element<0, const ranges::subrange<I, S, K>> {
370using type = I;
371};
372
373template<class I, class S, ranges::subrange_kind K>
374struct tuple_element<1, const ranges::subrange<I, S, K>> {
375using type = S;
376};
377
378struct from_range_t { explicit from_range_t() = default; }; // Since C++23
379inline constexpr from_range_t from_range{}; // Since C++23
380}
381*/
382
383#include <__config>
384#include <__ranges/access.h>
385#include <__ranges/all.h>
386#include <__ranges/as_rvalue_view.h>
387#include <__ranges/chunk_by_view.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/from_range.h>
402#include <__ranges/iota_view.h>
403#include <__ranges/join_view.h>
404#include <__ranges/lazy_split_view.h>
405#include <__ranges/rbegin.h>
406#include <__ranges/ref_view.h>
407#include <__ranges/rend.h>
408#include <__ranges/repeat_view.h>
409#include <__ranges/reverse_view.h>
410#include <__ranges/single_view.h>
411#include <__ranges/size.h>
412#include <__ranges/split_view.h>
413#include <__ranges/subrange.h>
414#include <__ranges/take_view.h>
415#include <__ranges/take_while_view.h>
416#include <__ranges/to.h>
417#include <__ranges/transform_view.h>
418#include <__ranges/view_interface.h>
419#include <__ranges/views.h>
420#include <__ranges/zip_view.h>
421#include <version>
422
423#if !defined(_LIBCPP_HAS_NO_LOCALIZATION)
424# include <__ranges/istream_view.h>
425#endif
426
427// standard-mandated includes
428
429// [ranges.syn]
430#include <compare>
431#include <initializer_list>
432#include <iterator>
433
434// [tuple.helper]
435#include <__tuple/tuple_element.h>
436#include <__tuple/tuple_size.h>
437
438#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
439# pragma GCC system_header
440#endif
441
442#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
443# include <cstdlib>
444# include <iosfwd>
445# include <type_traits>
446#endif
447
448#endif // _LIBCPP_RANGES
449