xref: /freebsd/contrib/llvm-project/libcxx/include/__ranges/concepts.h (revision 62ff619dcc3540659a319be71c9a489f1659e14a)
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_CONCEPTS_H
10 #define _LIBCPP___RANGES_CONCEPTS_H
11 
12 #include <__concepts/constructible.h>
13 #include <__concepts/movable.h>
14 #include <__concepts/same_as.h>
15 #include <__config>
16 #include <__iterator/concepts.h>
17 #include <__iterator/incrementable_traits.h>
18 #include <__iterator/iter_move.h>
19 #include <__iterator/iterator_traits.h>
20 #include <__iterator/readable_traits.h>
21 #include <__ranges/access.h>
22 #include <__ranges/data.h>
23 #include <__ranges/enable_borrowed_range.h>
24 #include <__ranges/enable_view.h>
25 #include <__ranges/size.h>
26 #include <initializer_list>
27 #include <type_traits>
28 
29 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
30 #pragma GCC system_header
31 #endif
32 
33 _LIBCPP_BEGIN_NAMESPACE_STD
34 
35 #if !defined(_LIBCPP_HAS_NO_CONCEPTS)
36 
37 namespace ranges {
38 
39   // [range.range]
40 
41   template <class _Tp>
42   concept range = requires(_Tp& __t) {
43     ranges::begin(__t); // sometimes equality-preserving
44     ranges::end(__t);
45   };
46 
47   template <class _Tp>
48   concept input_range = range<_Tp> && input_iterator<iterator_t<_Tp>>;
49 
50   template<class _Range>
51   concept borrowed_range = range<_Range> &&
52     (is_lvalue_reference_v<_Range> || enable_borrowed_range<remove_cvref_t<_Range>>);
53 
54   // `iterator_t` defined in <__ranges/access.h>
55 
56   template <range _Rp>
57   using sentinel_t = decltype(ranges::end(declval<_Rp&>()));
58 
59   template <range _Rp>
60   using range_difference_t = iter_difference_t<iterator_t<_Rp>>;
61 
62   template <range _Rp>
63   using range_value_t = iter_value_t<iterator_t<_Rp>>;
64 
65   template <range _Rp>
66   using range_reference_t = iter_reference_t<iterator_t<_Rp>>;
67 
68   template <range _Rp>
69   using range_rvalue_reference_t = iter_rvalue_reference_t<iterator_t<_Rp>>;
70 
71 #if !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
72 
73   // [range.sized]
74   template <class _Tp>
75   concept sized_range = range<_Tp> && requires(_Tp& __t) { ranges::size(__t); };
76 
77   template<sized_range _Rp>
78   using range_size_t = decltype(ranges::size(declval<_Rp&>()));
79 
80   // `disable_sized_range` defined in `<__ranges/size.h>`
81 
82   // [range.view], views
83 
84   // `enable_view` defined in <__ranges/enable_view.h>
85   // `view_base` defined in <__ranges/enable_view.h>
86 
87   template <class _Tp>
88   concept view =
89     range<_Tp> &&
90     movable<_Tp> &&
91     enable_view<_Tp>;
92 
93   template <class _Range>
94   concept __simple_view =
95     view<_Range> && range<const _Range> &&
96     same_as<iterator_t<_Range>, iterator_t<const _Range>> &&
97     same_as<sentinel_t<_Range>, sentinel_t<const _Range>>;
98 
99   // [range.refinements], other range refinements
100   template <class _Rp, class _Tp>
101   concept output_range = range<_Rp> && output_iterator<iterator_t<_Rp>, _Tp>;
102 
103   template <class _Tp>
104   concept forward_range = input_range<_Tp> && forward_iterator<iterator_t<_Tp>>;
105 
106   template <class _Tp>
107   concept bidirectional_range = forward_range<_Tp> && bidirectional_iterator<iterator_t<_Tp>>;
108 
109   template <class _Tp>
110   concept random_access_range =
111       bidirectional_range<_Tp> && random_access_iterator<iterator_t<_Tp>>;
112 
113   template<class _Tp>
114   concept contiguous_range =
115     random_access_range<_Tp> &&
116     contiguous_iterator<iterator_t<_Tp>> &&
117     requires(_Tp& __t) {
118       { ranges::data(__t) } -> same_as<add_pointer_t<range_reference_t<_Tp>>>;
119     };
120 
121   template <class _Tp>
122   concept common_range = range<_Tp> && same_as<iterator_t<_Tp>, sentinel_t<_Tp>>;
123 
124   template <class _Tp>
125   inline constexpr bool __is_std_initializer_list = false;
126 
127   template <class _Ep>
128   inline constexpr bool __is_std_initializer_list<initializer_list<_Ep>> = true;
129 
130   template <class _Tp>
131   concept viewable_range =
132     range<_Tp> &&
133     ((view<remove_cvref_t<_Tp>> && constructible_from<remove_cvref_t<_Tp>, _Tp>) ||
134      (!view<remove_cvref_t<_Tp>> &&
135       (is_lvalue_reference_v<_Tp> ||
136        (movable<remove_reference_t<_Tp>> && !__is_std_initializer_list<remove_cvref_t<_Tp>>))));
137 
138 #endif // !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
139 
140 } // namespace ranges
141 
142 #endif // !defined(_LIBCPP_HAS_NO_CONCEPTS)
143 
144 _LIBCPP_END_NAMESPACE_STD
145 
146 #endif // _LIBCPP___RANGES_CONCEPTS_H
147