]>
Commit | Line | Data |
---|---|---|
f67a9881 PC |
1 | // <range_access.h> -*- C++ -*- |
2 | ||
a5544970 | 3 | // Copyright (C) 2010-2019 Free Software Foundation, Inc. |
f67a9881 PC |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the | |
7 | // terms of the GNU General Public License as published by the | |
8 | // Free Software Foundation; either version 3, or (at your option) | |
9 | // any later version. | |
10 | ||
11 | // This library is distributed in the hope that it will be useful, | |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | // GNU General Public License for more details. | |
15 | ||
16 | // Under Section 7 of GPL version 3, you are granted additional | |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
24 | ||
25 | /** @file bits/range_access.h | |
26 | * This is an internal header file, included by other library headers. | |
f910786b | 27 | * Do not attempt to use it directly. @headername{iterator} |
f67a9881 PC |
28 | */ |
29 | ||
30 | #ifndef _GLIBCXX_RANGE_ACCESS_H | |
31 | #define _GLIBCXX_RANGE_ACCESS_H 1 | |
32 | ||
33 | #pragma GCC system_header | |
34 | ||
734f5023 | 35 | #if __cplusplus >= 201103L |
8bae22b7 | 36 | #include <initializer_list> |
12ffa228 BK |
37 | namespace std _GLIBCXX_VISIBILITY(default) |
38 | { | |
39 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
f67a9881 PC |
40 | |
41 | /** | |
42 | * @brief Return an iterator pointing to the first element of | |
43 | * the container. | |
93c66bc6 | 44 | * @param __cont Container. |
f67a9881 | 45 | */ |
a460d06d | 46 | template<typename _Container> |
06db9920 | 47 | inline _GLIBCXX17_CONSTEXPR auto |
f67a9881 PC |
48 | begin(_Container& __cont) -> decltype(__cont.begin()) |
49 | { return __cont.begin(); } | |
50 | ||
51 | /** | |
52 | * @brief Return an iterator pointing to the first element of | |
53 | * the const container. | |
93c66bc6 | 54 | * @param __cont Container. |
f67a9881 | 55 | */ |
a460d06d | 56 | template<typename _Container> |
06db9920 | 57 | inline _GLIBCXX17_CONSTEXPR auto |
f67a9881 PC |
58 | begin(const _Container& __cont) -> decltype(__cont.begin()) |
59 | { return __cont.begin(); } | |
60 | ||
61 | /** | |
62 | * @brief Return an iterator pointing to one past the last element of | |
63 | * the container. | |
93c66bc6 | 64 | * @param __cont Container. |
f67a9881 | 65 | */ |
a460d06d | 66 | template<typename _Container> |
06db9920 | 67 | inline _GLIBCXX17_CONSTEXPR auto |
f67a9881 PC |
68 | end(_Container& __cont) -> decltype(__cont.end()) |
69 | { return __cont.end(); } | |
70 | ||
71 | /** | |
72 | * @brief Return an iterator pointing to one past the last element of | |
73 | * the const container. | |
93c66bc6 | 74 | * @param __cont Container. |
f67a9881 | 75 | */ |
a460d06d | 76 | template<typename _Container> |
06db9920 | 77 | inline _GLIBCXX17_CONSTEXPR auto |
f67a9881 PC |
78 | end(const _Container& __cont) -> decltype(__cont.end()) |
79 | { return __cont.end(); } | |
80 | ||
81 | /** | |
82 | * @brief Return an iterator pointing to the first element of the array. | |
93c66bc6 | 83 | * @param __arr Array. |
f67a9881 | 84 | */ |
a460d06d | 85 | template<typename _Tp, size_t _Nm> |
8bae22b7 | 86 | inline _GLIBCXX14_CONSTEXPR _Tp* |
f67a9881 PC |
87 | begin(_Tp (&__arr)[_Nm]) |
88 | { return __arr; } | |
89 | ||
90 | /** | |
91 | * @brief Return an iterator pointing to one past the last element | |
92 | * of the array. | |
93c66bc6 | 93 | * @param __arr Array. |
f67a9881 | 94 | */ |
a460d06d | 95 | template<typename _Tp, size_t _Nm> |
8bae22b7 | 96 | inline _GLIBCXX14_CONSTEXPR _Tp* |
f67a9881 PC |
97 | end(_Tp (&__arr)[_Nm]) |
98 | { return __arr + _Nm; } | |
99 | ||
8bae22b7 | 100 | #if __cplusplus >= 201402L |
e994d230 JW |
101 | |
102 | template<typename _Tp> class valarray; | |
103 | // These overloads must be declared for cbegin and cend to use them. | |
104 | template<typename _Tp> _Tp* begin(valarray<_Tp>&); | |
105 | template<typename _Tp> const _Tp* begin(const valarray<_Tp>&); | |
106 | template<typename _Tp> _Tp* end(valarray<_Tp>&); | |
107 | template<typename _Tp> const _Tp* end(const valarray<_Tp>&); | |
108 | ||
8bae22b7 VV |
109 | /** |
110 | * @brief Return an iterator pointing to the first element of | |
111 | * the const container. | |
112 | * @param __cont Container. | |
113 | */ | |
a460d06d | 114 | template<typename _Container> |
8bae22b7 VV |
115 | inline constexpr auto |
116 | cbegin(const _Container& __cont) noexcept(noexcept(std::begin(__cont))) | |
117 | -> decltype(std::begin(__cont)) | |
118 | { return std::begin(__cont); } | |
119 | ||
120 | /** | |
121 | * @brief Return an iterator pointing to one past the last element of | |
122 | * the const container. | |
123 | * @param __cont Container. | |
124 | */ | |
a460d06d | 125 | template<typename _Container> |
8bae22b7 VV |
126 | inline constexpr auto |
127 | cend(const _Container& __cont) noexcept(noexcept(std::end(__cont))) | |
128 | -> decltype(std::end(__cont)) | |
129 | { return std::end(__cont); } | |
130 | ||
131 | /** | |
132 | * @brief Return a reverse iterator pointing to the last element of | |
133 | * the container. | |
134 | * @param __cont Container. | |
135 | */ | |
a460d06d | 136 | template<typename _Container> |
06db9920 | 137 | inline _GLIBCXX17_CONSTEXPR auto |
8bae22b7 VV |
138 | rbegin(_Container& __cont) -> decltype(__cont.rbegin()) |
139 | { return __cont.rbegin(); } | |
140 | ||
141 | /** | |
142 | * @brief Return a reverse iterator pointing to the last element of | |
143 | * the const container. | |
144 | * @param __cont Container. | |
145 | */ | |
a460d06d | 146 | template<typename _Container> |
06db9920 | 147 | inline _GLIBCXX17_CONSTEXPR auto |
8bae22b7 VV |
148 | rbegin(const _Container& __cont) -> decltype(__cont.rbegin()) |
149 | { return __cont.rbegin(); } | |
150 | ||
151 | /** | |
152 | * @brief Return a reverse iterator pointing one past the first element of | |
153 | * the container. | |
154 | * @param __cont Container. | |
155 | */ | |
a460d06d | 156 | template<typename _Container> |
06db9920 | 157 | inline _GLIBCXX17_CONSTEXPR auto |
8bae22b7 VV |
158 | rend(_Container& __cont) -> decltype(__cont.rend()) |
159 | { return __cont.rend(); } | |
160 | ||
161 | /** | |
162 | * @brief Return a reverse iterator pointing one past the first element of | |
163 | * the const container. | |
164 | * @param __cont Container. | |
165 | */ | |
a460d06d | 166 | template<typename _Container> |
06db9920 | 167 | inline _GLIBCXX17_CONSTEXPR auto |
8bae22b7 VV |
168 | rend(const _Container& __cont) -> decltype(__cont.rend()) |
169 | { return __cont.rend(); } | |
170 | ||
171 | /** | |
172 | * @brief Return a reverse iterator pointing to the last element of | |
173 | * the array. | |
174 | * @param __arr Array. | |
175 | */ | |
a460d06d | 176 | template<typename _Tp, size_t _Nm> |
06db9920 | 177 | inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Tp*> |
8bae22b7 VV |
178 | rbegin(_Tp (&__arr)[_Nm]) |
179 | { return reverse_iterator<_Tp*>(__arr + _Nm); } | |
180 | ||
181 | /** | |
182 | * @brief Return a reverse iterator pointing one past the first element of | |
183 | * the array. | |
184 | * @param __arr Array. | |
185 | */ | |
a460d06d | 186 | template<typename _Tp, size_t _Nm> |
06db9920 | 187 | inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Tp*> |
8bae22b7 VV |
188 | rend(_Tp (&__arr)[_Nm]) |
189 | { return reverse_iterator<_Tp*>(__arr); } | |
190 | ||
191 | /** | |
192 | * @brief Return a reverse iterator pointing to the last element of | |
193 | * the initializer_list. | |
194 | * @param __il initializer_list. | |
195 | */ | |
a460d06d | 196 | template<typename _Tp> |
06db9920 | 197 | inline _GLIBCXX17_CONSTEXPR reverse_iterator<const _Tp*> |
8bae22b7 VV |
198 | rbegin(initializer_list<_Tp> __il) |
199 | { return reverse_iterator<const _Tp*>(__il.end()); } | |
200 | ||
201 | /** | |
202 | * @brief Return a reverse iterator pointing one past the first element of | |
203 | * the initializer_list. | |
204 | * @param __il initializer_list. | |
205 | */ | |
a460d06d | 206 | template<typename _Tp> |
06db9920 | 207 | inline _GLIBCXX17_CONSTEXPR reverse_iterator<const _Tp*> |
8bae22b7 VV |
208 | rend(initializer_list<_Tp> __il) |
209 | { return reverse_iterator<const _Tp*>(__il.begin()); } | |
210 | ||
211 | /** | |
212 | * @brief Return a reverse iterator pointing to the last element of | |
213 | * the const container. | |
214 | * @param __cont Container. | |
215 | */ | |
a460d06d | 216 | template<typename _Container> |
06db9920 | 217 | inline _GLIBCXX17_CONSTEXPR auto |
8bae22b7 VV |
218 | crbegin(const _Container& __cont) -> decltype(std::rbegin(__cont)) |
219 | { return std::rbegin(__cont); } | |
220 | ||
221 | /** | |
222 | * @brief Return a reverse iterator pointing one past the first element of | |
223 | * the const container. | |
224 | * @param __cont Container. | |
225 | */ | |
a460d06d | 226 | template<typename _Container> |
06db9920 | 227 | inline _GLIBCXX17_CONSTEXPR auto |
8bae22b7 VV |
228 | crend(const _Container& __cont) -> decltype(std::rend(__cont)) |
229 | { return std::rend(__cont); } | |
230 | ||
231 | #endif // C++14 | |
232 | ||
19491228 | 233 | #if __cplusplus >= 201703L |
db23e4c4 | 234 | #define __cpp_lib_nonmember_container_access 201411 |
a460d06d VV |
235 | |
236 | /** | |
237 | * @brief Return the size of a container. | |
238 | * @param __cont Container. | |
239 | */ | |
240 | template <typename _Container> | |
241 | constexpr auto | |
19491228 JW |
242 | size(const _Container& __cont) noexcept(noexcept(__cont.size())) |
243 | -> decltype(__cont.size()) | |
a460d06d VV |
244 | { return __cont.size(); } |
245 | ||
246 | /** | |
247 | * @brief Return the size of an array. | |
248 | * @param __array Array. | |
249 | */ | |
93411db8 | 250 | template <typename _Tp, size_t _Nm> |
a460d06d | 251 | constexpr size_t |
93411db8 JW |
252 | size(const _Tp (&/*__array*/)[_Nm]) noexcept |
253 | { return _Nm; } | |
a460d06d VV |
254 | |
255 | /** | |
256 | * @brief Return whether a container is empty. | |
257 | * @param __cont Container. | |
258 | */ | |
259 | template <typename _Container> | |
d69f1ec7 | 260 | [[nodiscard]] constexpr auto |
19491228 JW |
261 | empty(const _Container& __cont) noexcept(noexcept(__cont.empty())) |
262 | -> decltype(__cont.empty()) | |
a460d06d VV |
263 | { return __cont.empty(); } |
264 | ||
265 | /** | |
266 | * @brief Return whether an array is empty (always false). | |
267 | * @param __array Container. | |
268 | */ | |
93411db8 | 269 | template <typename _Tp, size_t _Nm> |
d69f1ec7 | 270 | [[nodiscard]] constexpr bool |
93411db8 | 271 | empty(const _Tp (&/*__array*/)[_Nm]) noexcept |
a460d06d VV |
272 | { return false; } |
273 | ||
274 | /** | |
275 | * @brief Return whether an initializer_list is empty. | |
276 | * @param __il Initializer list. | |
277 | */ | |
278 | template <typename _Tp> | |
d69f1ec7 | 279 | [[nodiscard]] constexpr bool |
a460d06d VV |
280 | empty(initializer_list<_Tp> __il) noexcept |
281 | { return __il.size() == 0;} | |
282 | ||
283 | /** | |
284 | * @brief Return the data pointer of a container. | |
285 | * @param __cont Container. | |
286 | */ | |
287 | template <typename _Container> | |
288 | constexpr auto | |
19491228 JW |
289 | data(_Container& __cont) noexcept(noexcept(__cont.data())) |
290 | -> decltype(__cont.data()) | |
a460d06d VV |
291 | { return __cont.data(); } |
292 | ||
293 | /** | |
294 | * @brief Return the data pointer of a const container. | |
295 | * @param __cont Container. | |
296 | */ | |
297 | template <typename _Container> | |
298 | constexpr auto | |
19491228 JW |
299 | data(const _Container& __cont) noexcept(noexcept(__cont.data())) |
300 | -> decltype(__cont.data()) | |
a460d06d VV |
301 | { return __cont.data(); } |
302 | ||
303 | /** | |
304 | * @brief Return the data pointer of an array. | |
305 | * @param __array Array. | |
306 | */ | |
93411db8 | 307 | template <typename _Tp, size_t _Nm> |
a460d06d | 308 | constexpr _Tp* |
93411db8 | 309 | data(_Tp (&__array)[_Nm]) noexcept |
a460d06d VV |
310 | { return __array; } |
311 | ||
312 | /** | |
313 | * @brief Return the data pointer of an initializer list. | |
314 | * @param __il Initializer list. | |
315 | */ | |
316 | template <typename _Tp> | |
317 | constexpr const _Tp* | |
318 | data(initializer_list<_Tp> __il) noexcept | |
319 | { return __il.begin(); } | |
320 | ||
321 | #endif // C++17 | |
322 | ||
12ffa228 BK |
323 | _GLIBCXX_END_NAMESPACE_VERSION |
324 | } // namespace | |
f67a9881 | 325 | |
734f5023 | 326 | #endif // C++11 |
f67a9881 PC |
327 | |
328 | #endif // _GLIBCXX_RANGE_ACCESS_H |