libstdc++
range_access.h
Go to the documentation of this file.
1 // <range_access.h> -*- C++ -*-
2 
3 // Copyright (C) 2010-2020 Free Software Foundation, Inc.
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.
27  * Do not attempt to use it directly. @headername{iterator}
28  */
29 
30 #ifndef _GLIBCXX_RANGE_ACCESS_H
31 #define _GLIBCXX_RANGE_ACCESS_H 1
32 
33 #pragma GCC system_header
34 
35 #if __cplusplus >= 201103L
36 #include <initializer_list>
37 #include <bits/iterator_concepts.h>
38 
39 namespace std _GLIBCXX_VISIBILITY(default)
40 {
41 _GLIBCXX_BEGIN_NAMESPACE_VERSION
42 
43  /**
44  * @brief Return an iterator pointing to the first element of
45  * the container.
46  * @param __cont Container.
47  */
48  template<typename _Container>
49  inline _GLIBCXX17_CONSTEXPR auto
50  begin(_Container& __cont) -> decltype(__cont.begin())
51  { return __cont.begin(); }
52 
53  /**
54  * @brief Return an iterator pointing to the first element of
55  * the const container.
56  * @param __cont Container.
57  */
58  template<typename _Container>
59  inline _GLIBCXX17_CONSTEXPR auto
60  begin(const _Container& __cont) -> decltype(__cont.begin())
61  { return __cont.begin(); }
62 
63  /**
64  * @brief Return an iterator pointing to one past the last element of
65  * the container.
66  * @param __cont Container.
67  */
68  template<typename _Container>
69  inline _GLIBCXX17_CONSTEXPR auto
70  end(_Container& __cont) -> decltype(__cont.end())
71  { return __cont.end(); }
72 
73  /**
74  * @brief Return an iterator pointing to one past the last element of
75  * the const container.
76  * @param __cont Container.
77  */
78  template<typename _Container>
79  inline _GLIBCXX17_CONSTEXPR auto
80  end(const _Container& __cont) -> decltype(__cont.end())
81  { return __cont.end(); }
82 
83  /**
84  * @brief Return an iterator pointing to the first element of the array.
85  * @param __arr Array.
86  */
87  template<typename _Tp, size_t _Nm>
88  inline _GLIBCXX14_CONSTEXPR _Tp*
89  begin(_Tp (&__arr)[_Nm])
90  { return __arr; }
91 
92  /**
93  * @brief Return an iterator pointing to one past the last element
94  * of the array.
95  * @param __arr Array.
96  */
97  template<typename _Tp, size_t _Nm>
98  inline _GLIBCXX14_CONSTEXPR _Tp*
99  end(_Tp (&__arr)[_Nm])
100  { return __arr + _Nm; }
101 
102 #if __cplusplus >= 201402L
103 
104  template<typename _Tp> class valarray;
105  // These overloads must be declared for cbegin and cend to use them.
106  template<typename _Tp> _Tp* begin(valarray<_Tp>&);
107  template<typename _Tp> const _Tp* begin(const valarray<_Tp>&);
108  template<typename _Tp> _Tp* end(valarray<_Tp>&);
109  template<typename _Tp> const _Tp* end(const valarray<_Tp>&);
110 
111  /**
112  * @brief Return an iterator pointing to the first element of
113  * the const container.
114  * @param __cont Container.
115  */
116  template<typename _Container>
117  inline constexpr auto
118  cbegin(const _Container& __cont) noexcept(noexcept(std::begin(__cont)))
119  -> decltype(std::begin(__cont))
120  { return std::begin(__cont); }
121 
122  /**
123  * @brief Return an iterator pointing to one past the last element of
124  * the const container.
125  * @param __cont Container.
126  */
127  template<typename _Container>
128  inline constexpr auto
129  cend(const _Container& __cont) noexcept(noexcept(std::end(__cont)))
130  -> decltype(std::end(__cont))
131  { return std::end(__cont); }
132 
133  /**
134  * @brief Return a reverse iterator pointing to the last element of
135  * the container.
136  * @param __cont Container.
137  */
138  template<typename _Container>
139  inline _GLIBCXX17_CONSTEXPR auto
140  rbegin(_Container& __cont) -> decltype(__cont.rbegin())
141  { return __cont.rbegin(); }
142 
143  /**
144  * @brief Return a reverse iterator pointing to the last element of
145  * the const container.
146  * @param __cont Container.
147  */
148  template<typename _Container>
149  inline _GLIBCXX17_CONSTEXPR auto
150  rbegin(const _Container& __cont) -> decltype(__cont.rbegin())
151  { return __cont.rbegin(); }
152 
153  /**
154  * @brief Return a reverse iterator pointing one past the first element of
155  * the container.
156  * @param __cont Container.
157  */
158  template<typename _Container>
159  inline _GLIBCXX17_CONSTEXPR auto
160  rend(_Container& __cont) -> decltype(__cont.rend())
161  { return __cont.rend(); }
162 
163  /**
164  * @brief Return a reverse iterator pointing one past the first element of
165  * the const container.
166  * @param __cont Container.
167  */
168  template<typename _Container>
169  inline _GLIBCXX17_CONSTEXPR auto
170  rend(const _Container& __cont) -> decltype(__cont.rend())
171  { return __cont.rend(); }
172 
173  /**
174  * @brief Return a reverse iterator pointing to the last element of
175  * the array.
176  * @param __arr Array.
177  */
178  template<typename _Tp, size_t _Nm>
179  inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Tp*>
180  rbegin(_Tp (&__arr)[_Nm])
181  { return reverse_iterator<_Tp*>(__arr + _Nm); }
182 
183  /**
184  * @brief Return a reverse iterator pointing one past the first element of
185  * the array.
186  * @param __arr Array.
187  */
188  template<typename _Tp, size_t _Nm>
189  inline _GLIBCXX17_CONSTEXPR reverse_iterator<_Tp*>
190  rend(_Tp (&__arr)[_Nm])
191  { return reverse_iterator<_Tp*>(__arr); }
192 
193  /**
194  * @brief Return a reverse iterator pointing to the last element of
195  * the initializer_list.
196  * @param __il initializer_list.
197  */
198  template<typename _Tp>
199  inline _GLIBCXX17_CONSTEXPR reverse_iterator<const _Tp*>
201  { return reverse_iterator<const _Tp*>(__il.end()); }
202 
203  /**
204  * @brief Return a reverse iterator pointing one past the first element of
205  * the initializer_list.
206  * @param __il initializer_list.
207  */
208  template<typename _Tp>
209  inline _GLIBCXX17_CONSTEXPR reverse_iterator<const _Tp*>
211  { return reverse_iterator<const _Tp*>(__il.begin()); }
212 
213  /**
214  * @brief Return a reverse iterator pointing to the last element of
215  * the const container.
216  * @param __cont Container.
217  */
218  template<typename _Container>
219  inline _GLIBCXX17_CONSTEXPR auto
220  crbegin(const _Container& __cont) -> decltype(std::rbegin(__cont))
221  { return std::rbegin(__cont); }
222 
223  /**
224  * @brief Return a reverse iterator pointing one past the first element of
225  * the const container.
226  * @param __cont Container.
227  */
228  template<typename _Container>
229  inline _GLIBCXX17_CONSTEXPR auto
230  crend(const _Container& __cont) -> decltype(std::rend(__cont))
231  { return std::rend(__cont); }
232 
233 #endif // C++14
234 
235 #if __cplusplus >= 201703L
236 #define __cpp_lib_nonmember_container_access 201411
237 
238  /**
239  * @brief Return the size of a container.
240  * @param __cont Container.
241  */
242  template <typename _Container>
243  constexpr auto
244  size(const _Container& __cont) noexcept(noexcept(__cont.size()))
245  -> decltype(__cont.size())
246  { return __cont.size(); }
247 
248  /**
249  * @brief Return the size of an array.
250  */
251  template <typename _Tp, size_t _Nm>
252  constexpr size_t
253  size(const _Tp (&)[_Nm]) noexcept
254  { return _Nm; }
255 
256  /**
257  * @brief Return whether a container is empty.
258  * @param __cont Container.
259  */
260  template <typename _Container>
261  [[nodiscard]] constexpr auto
262  empty(const _Container& __cont) noexcept(noexcept(__cont.empty()))
263  -> decltype(__cont.empty())
264  { return __cont.empty(); }
265 
266  /**
267  * @brief Return whether an array is empty (always false).
268  */
269  template <typename _Tp, size_t _Nm>
270  [[nodiscard]] constexpr bool
271  empty(const _Tp (&)[_Nm]) noexcept
272  { return false; }
273 
274  /**
275  * @brief Return whether an initializer_list is empty.
276  * @param __il Initializer list.
277  */
278  template <typename _Tp>
279  [[nodiscard]] constexpr bool
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
289  data(_Container& __cont) noexcept(noexcept(__cont.data()))
290  -> decltype(__cont.data())
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
299  data(const _Container& __cont) noexcept(noexcept(__cont.data()))
300  -> decltype(__cont.data())
301  { return __cont.data(); }
302 
303  /**
304  * @brief Return the data pointer of an array.
305  * @param __array Array.
306  */
307  template <typename _Tp, size_t _Nm>
308  constexpr _Tp*
309  data(_Tp (&__array)[_Nm]) noexcept
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 
323 #if __cplusplus > 201703L
324  template<typename _Container>
325  constexpr auto
326  ssize(const _Container& __cont)
327  noexcept(noexcept(__cont.size()))
328  -> common_type_t<ptrdiff_t, make_signed_t<decltype(__cont.size())>>
329  {
330  using type = make_signed_t<decltype(__cont.size())>;
331  return static_cast<common_type_t<ptrdiff_t, type>>(__cont.size());
332  }
333 
334  template<typename _Tp, ptrdiff_t _Num>
335  constexpr ptrdiff_t
336  ssize(const _Tp (&)[_Num]) noexcept
337  { return _Num; }
338 
339 #ifdef __cpp_lib_concepts
340 namespace ranges
341 {
342  template<typename>
343  inline constexpr bool disable_sized_range = false;
344 
345  template<typename _Tp>
346  inline constexpr bool enable_safe_range = false;
347 
348  namespace __detail
349  {
350  template<integral _Tp>
351  constexpr make_unsigned_t<_Tp>
352  __to_unsigned_like(_Tp __t) noexcept
353  { return __t; }
354 
355  template<typename _Tp, bool _MaxDiff = same_as<_Tp, __max_diff_type>>
356  using __make_unsigned_like_t
357  = conditional_t<_MaxDiff, __max_size_type, make_unsigned_t<_Tp>>;
358 
359  // Part of the constraints of ranges::safe_range
360  template<typename _Tp>
361  concept __maybe_safe_range
362  = is_lvalue_reference_v<_Tp> || enable_safe_range<remove_cvref_t<_Tp>>;
363 
364  } // namespace __detail
365 
366  namespace __cust_access
367  {
368  using std::ranges::__detail::__maybe_safe_range;
369  using std::__detail::__class_or_enum;
370 
371  template<typename _Tp>
372  constexpr decay_t<_Tp>
373  __decay_copy(_Tp&& __t)
374  noexcept(is_nothrow_convertible_v<_Tp, decay_t<_Tp>>)
375  { return std::forward<_Tp>(__t); }
376 
377  template<typename _Tp>
378  concept __member_begin = requires(_Tp& __t)
379  {
380  { __decay_copy(__t.begin()) } -> input_or_output_iterator;
381  };
382 
383  template<typename _Tp> void begin(_Tp&&) = delete;
384  template<typename _Tp> void begin(initializer_list<_Tp>&&) = delete;
385 
386  template<typename _Tp>
387  concept __adl_begin = __class_or_enum<remove_reference_t<_Tp>>
388  && requires(_Tp& __t)
389  {
390  { __decay_copy(begin(__t)) } -> input_or_output_iterator;
391  };
392 
393  struct _Begin
394  {
395  private:
396  template<typename _Tp>
397  static constexpr bool
398  _S_noexcept()
399  {
400  if constexpr (is_array_v<remove_reference_t<_Tp>>)
401  return true;
402  else if constexpr (__member_begin<_Tp>)
403  return noexcept(__decay_copy(std::declval<_Tp&>().begin()));
404  else
405  return noexcept(__decay_copy(begin(std::declval<_Tp&>())));
406  }
407 
408  public:
409  template<__maybe_safe_range _Tp>
410  requires is_array_v<remove_reference_t<_Tp>> || __member_begin<_Tp>
411  || __adl_begin<_Tp>
412  constexpr auto
413  operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp>())
414  {
415  if constexpr (is_array_v<remove_reference_t<_Tp>>)
416  {
417  static_assert(is_lvalue_reference_v<_Tp>);
418  return __t;
419  }
420  else if constexpr (__member_begin<_Tp>)
421  return __t.begin();
422  else
423  return begin(__t);
424  }
425  };
426 
427  template<typename _Tp>
428  concept __member_end = requires(_Tp& __t)
429  {
430  { __decay_copy(__t.end()) }
431  -> sentinel_for<decltype(_Begin{}(std::forward<_Tp>(__t)))>;
432  };
433 
434  template<typename _Tp> void end(_Tp&&) = delete;
435  template<typename _Tp> void end(initializer_list<_Tp>&&) = delete;
436 
437  template<typename _Tp>
438  concept __adl_end = __class_or_enum<remove_reference_t<_Tp>>
439  && requires(_Tp& __t)
440  {
441  { __decay_copy(end(__t)) }
442  -> sentinel_for<decltype(_Begin{}(std::forward<_Tp>(__t)))>;
443  };
444 
445  struct _End
446  {
447  private:
448  template<typename _Tp>
449  static constexpr bool
450  _S_noexcept()
451  {
452  if constexpr (is_array_v<remove_reference_t<_Tp>>)
453  return true;
454  else if constexpr (__member_end<_Tp>)
455  return noexcept(__decay_copy(std::declval<_Tp&>().end()));
456  else
457  return noexcept(__decay_copy(end(std::declval<_Tp&>())));
458  }
459 
460  public:
461  template<__maybe_safe_range _Tp>
462  requires is_array_v<remove_reference_t<_Tp>> || __member_end<_Tp>
463  || __adl_end<_Tp>
464  constexpr auto
465  operator()(_Tp&& __t) const noexcept(_S_noexcept<_Tp>())
466  {
467  if constexpr (is_array_v<remove_reference_t<_Tp>>)
468  {
469  static_assert(is_lvalue_reference_v<_Tp>);
470  static_assert(is_bounded_array_v<remove_reference_t<_Tp>>);
471  return __t + extent_v<remove_reference_t<_Tp>>;
472  }
473  else if constexpr (__member_end<_Tp>)
474  return __t.end();
475  else
476  return end(__t);
477  }
478  };
479 
480  template<typename _Tp>
481  constexpr decltype(auto)
482  __as_const(_Tp&& __t) noexcept
483  {
484  if constexpr (is_lvalue_reference_v<_Tp>)
485  return static_cast<const remove_reference_t<_Tp>&>(__t);
486  else
487  return static_cast<const _Tp&&>(__t);
488  }
489 
490  struct _CBegin
491  {
492  template<typename _Tp>
493  constexpr auto
494  operator()(_Tp&& __e) const
495  noexcept(noexcept(_Begin{}(__cust_access::__as_const((_Tp&&)__e))))
496  requires requires { _Begin{}(__cust_access::__as_const((_Tp&&)__e)); }
497  {
498  return _Begin{}(__cust_access::__as_const(std::forward<_Tp>(__e)));
499  }
500  };
501 
502  struct _CEnd
503  {
504  template<typename _Tp>
505  constexpr auto
506  operator()(_Tp&& __e) const
507  noexcept(noexcept(_End{}(__cust_access::__as_const((_Tp&&)__e))))
508  requires requires { _End{}(__cust_access::__as_const((_Tp&&)__e)); }
509  {
510  return _End{}(__cust_access::__as_const(std::forward<_Tp>(__e)));
511  }
512  };
513 
514  template<typename _Tp>
515  concept __member_rbegin = requires(_Tp& __t)
516  {
517  { __decay_copy(__t.rbegin()) } -> input_or_output_iterator;
518  };
519 
520  template<typename _Tp> void rbegin(_Tp&&) = delete;
521 
522  template<typename _Tp>
523  concept __adl_rbegin = __class_or_enum<remove_reference_t<_Tp>>
524  && requires(_Tp& __t)
525  {
526  { __decay_copy(rbegin(__t)) } -> input_or_output_iterator;
527  };
528 
529  template<typename _Tp>
530  concept __reversable = requires(_Tp& __t)
531  {
532  { _Begin{}(__t) } -> bidirectional_iterator;
533  { _End{}(__t) } -> same_as<decltype(_Begin{}(__t))>;
534  };
535 
536  struct _RBegin
537  {
538  private:
539  template<typename _Tp>
540  static constexpr bool
541  _S_noexcept()
542  {
543  if constexpr (__member_rbegin<_Tp>)
544  return noexcept(__decay_copy(std::declval<_Tp&>().rbegin()));
545  else if constexpr (__adl_rbegin<_Tp>)
546  return noexcept(__decay_copy(rbegin(std::declval<_Tp&>())));
547  else
548  {
549  if constexpr (noexcept(_End{}(std::declval<_Tp&>())))
550  {
551  using _It = decltype(_End{}(std::declval<_Tp&>()));
552  // std::reverse_iterator copy-initializes its member.
553  return is_nothrow_copy_constructible_v<_It>;
554  }
555  else
556  return false;
557  }
558  }
559 
560  public:
561  template<__maybe_safe_range _Tp>
562  requires __member_rbegin<_Tp> || __adl_rbegin<_Tp> || __reversable<_Tp>
563  constexpr auto
564  operator()(_Tp&& __t) const
565  noexcept(_S_noexcept<_Tp>())
566  {
567  if constexpr (__member_rbegin<_Tp>)
568  return __t.rbegin();
569  else if constexpr (__adl_rbegin<_Tp>)
570  return rbegin(__t);
571  else
572  return std::make_reverse_iterator(_End{}(__t));
573  }
574  };
575 
576  template<typename _Tp>
577  concept __member_rend = requires(_Tp& __t)
578  {
579  { __decay_copy(__t.rend()) }
580  -> sentinel_for<decltype(_RBegin{}(__t))>;
581  };
582 
583  template<typename _Tp> void rend(_Tp&&) = delete;
584 
585  template<typename _Tp>
586  concept __adl_rend = __class_or_enum<remove_reference_t<_Tp>>
587  && requires(_Tp& __t)
588  {
589  { __decay_copy(rend(__t)) }
590  -> sentinel_for<decltype(_RBegin{}(std::forward<_Tp>(__t)))>;
591  };
592 
593  struct _REnd
594  {
595  private:
596  template<typename _Tp>
597  static constexpr bool
598  _S_noexcept()
599  {
600  if constexpr (__member_rend<_Tp>)
601  return noexcept(__decay_copy(std::declval<_Tp&>().rend()));
602  else if constexpr (__adl_rend<_Tp>)
603  return noexcept(__decay_copy(rend(std::declval<_Tp&>())));
604  else
605  {
606  if constexpr (noexcept(_Begin{}(std::declval<_Tp&>())))
607  {
608  using _It = decltype(_Begin{}(std::declval<_Tp&>()));
609  // std::reverse_iterator copy-initializes its member.
610  return is_nothrow_copy_constructible_v<_It>;
611  }
612  else
613  return false;
614  }
615  }
616 
617  public:
618  template<__maybe_safe_range _Tp>
619  requires __member_rend<_Tp> || __adl_rend<_Tp> || __reversable<_Tp>
620  constexpr auto
621  operator()(_Tp&& __t) const
622  noexcept(_S_noexcept<_Tp>())
623  {
624  if constexpr (__member_rend<_Tp>)
625  return __t.rend();
626  else if constexpr (__adl_rend<_Tp>)
627  return rend(__t);
628  else
629  return std::make_reverse_iterator(_Begin{}(__t));
630  }
631  };
632 
633  struct _CRBegin
634  {
635  template<typename _Tp>
636  constexpr auto
637  operator()(_Tp&& __e) const
638  noexcept(noexcept(_RBegin{}(__cust_access::__as_const((_Tp&&)__e))))
639  requires requires { _RBegin{}(__cust_access::__as_const((_Tp&&)__e)); }
640  {
641  return _RBegin{}(__cust_access::__as_const(std::forward<_Tp>(__e)));
642  }
643  };
644 
645  struct _CREnd
646  {
647  template<typename _Tp>
648  constexpr auto
649  operator()(_Tp&& __e) const
650  noexcept(noexcept(_REnd{}(__cust_access::__as_const((_Tp&&)__e))))
651  requires requires { _REnd{}(__cust_access::__as_const((_Tp&&)__e)); }
652  {
653  return _REnd{}(__cust_access::__as_const(std::forward<_Tp>(__e)));
654  }
655  };
656 
657  template<typename _Tp>
658  concept __member_size = !disable_sized_range<remove_cvref_t<_Tp>>
659  && requires(_Tp&& __t)
660  {
661  { __decay_copy(std::forward<_Tp>(__t).size()) }
662  -> __detail::__is_integer_like;
663  };
664 
665  template<typename _Tp> void size(_Tp&&) = delete;
666 
667  template<typename _Tp>
668  concept __adl_size = __class_or_enum<remove_reference_t<_Tp>>
669  && !disable_sized_range<remove_cvref_t<_Tp>>
670  && requires(_Tp&& __t)
671  {
672  { __decay_copy(size(std::forward<_Tp>(__t))) }
673  -> __detail::__is_integer_like;
674  };
675 
676  template<typename _Tp>
677  concept __sentinel_size = requires(_Tp&& __t)
678  {
679  { _Begin{}(std::forward<_Tp>(__t)) } -> forward_iterator;
680 
681  { _End{}(std::forward<_Tp>(__t)) }
682  -> sized_sentinel_for<decltype(_Begin{}(std::forward<_Tp>(__t)))>;
683  };
684 
685  struct _Size
686  {
687  private:
688  template<typename _Tp>
689  static constexpr bool
690  _S_noexcept()
691  {
692  if constexpr (is_array_v<remove_reference_t<_Tp>>)
693  return true;
694  else if constexpr (__member_size<_Tp>)
695  return noexcept(__decay_copy(std::declval<_Tp>().size()));
696  else if constexpr (__adl_size<_Tp>)
697  return noexcept(__decay_copy(size(std::declval<_Tp>())));
698  else if constexpr (__sentinel_size<_Tp>)
699  return noexcept(_End{}(std::declval<_Tp>())
700  - _Begin{}(std::declval<_Tp>()));
701  }
702 
703  public:
704  template<typename _Tp>
705  requires is_array_v<remove_reference_t<_Tp>>
706  || __member_size<_Tp> || __adl_size<_Tp> || __sentinel_size<_Tp>
707  constexpr auto
708  operator()(_Tp&& __e) const noexcept(_S_noexcept<_Tp>())
709  {
710  if constexpr (is_array_v<remove_reference_t<_Tp>>)
711  {
712  static_assert(is_bounded_array_v<remove_reference_t<_Tp>>);
713  return extent_v<remove_reference_t<_Tp>>;
714  }
715  else if constexpr (__member_size<_Tp>)
716  return std::forward<_Tp>(__e).size();
717  else if constexpr (__adl_size<_Tp>)
718  return size(std::forward<_Tp>(__e));
719  else if constexpr (__sentinel_size<_Tp>)
720  return __detail::__to_unsigned_like(
721  _End{}(std::forward<_Tp>(__e))
722  - _Begin{}(std::forward<_Tp>(__e)));
723  }
724  };
725 
726  template<typename _Tp>
727  concept __member_empty = requires(_Tp&& __t)
728  { bool(std::forward<_Tp>(__t).empty()); };
729 
730  template<typename _Tp>
731  concept __size0_empty = requires(_Tp&& __t)
732  { _Size{}(std::forward<_Tp>(__t)) == 0; };
733 
734  template<typename _Tp>
735  concept __eq_iter_empty = requires(_Tp&& __t)
736  {
737  { _Begin{}(std::forward<_Tp>(__t)) } -> forward_iterator;
738  bool(_Begin{}(std::forward<_Tp>(__t))
739  == _End{}(std::forward<_Tp>(__t)));
740  };
741 
742  struct _Empty
743  {
744  private:
745  template<typename _Tp>
746  static constexpr bool
747  _S_noexcept()
748  {
749  if constexpr (__member_empty<_Tp>)
750  return noexcept(std::declval<_Tp>().empty());
751  else if constexpr (__size0_empty<_Tp>)
752  return noexcept(_Size{}(std::declval<_Tp>()) == 0);
753  else
754  return noexcept(bool(_Begin{}(std::declval<_Tp>())
755  == _End{}(std::declval<_Tp>())));
756  }
757 
758  public:
759  template<typename _Tp>
760  requires __member_empty<_Tp> || __size0_empty<_Tp>
761  || __eq_iter_empty<_Tp>
762  constexpr auto
763  operator()(_Tp&& __e) const noexcept(_S_noexcept<_Tp>())
764  {
765  if constexpr (__member_empty<_Tp>)
766  return bool(std::forward<_Tp>(__e).empty());
767  else if constexpr (__size0_empty<_Tp>)
768  return _Size{}(std::forward<_Tp>(__e)) == 0;
769  else
770  return bool(_Begin{}(std::forward<_Tp>(__e))
771  == _End{}(std::forward<_Tp>(__e)));
772  }
773  };
774 
775  template<typename _Tp>
776  concept __pointer_to_object = is_pointer_v<_Tp>
777  && is_object_v<remove_pointer_t<_Tp>>;
778 
779  template<typename _Tp>
780  concept __member_data = is_lvalue_reference_v<_Tp>
781  && requires(_Tp __t) { { __t.data() } -> __pointer_to_object; };
782 
783  template<typename _Tp>
784  concept __begin_data = requires(_Tp&& __t)
785  { { _Begin{}(std::forward<_Tp>(__t)) } -> contiguous_iterator; };
786 
787  struct _Data
788  {
789  private:
790  template<typename _Tp>
791  static constexpr bool
792  _S_noexcept()
793  {
794  if constexpr (__member_data<_Tp>)
795  return noexcept(__decay_copy(std::declval<_Tp>().data()));
796  else
797  return noexcept(_Begin{}(std::declval<_Tp>()));
798  }
799 
800  public:
801  template<typename _Tp> requires __member_data<_Tp> || __begin_data<_Tp>
802  constexpr auto
803  operator()(_Tp&& __e) const noexcept(_S_noexcept<_Tp>())
804  {
805  if constexpr (__member_data<_Tp>)
806  return __e.data();
807  else
808  return std::to_address(_Begin{}(std::forward<_Tp>(__e)));
809  }
810  };
811 
812  struct _CData
813  {
814  template<typename _Tp>
815  constexpr auto
816  operator()(_Tp&& __e) const
817  noexcept(noexcept(_Data{}(__cust_access::__as_const((_Tp&&)__e))))
818  requires requires { _Data{}(__cust_access::__as_const((_Tp&&)__e)); }
819  {
820  return _Data{}(__cust_access::__as_const(std::forward<_Tp>(__e)));
821  }
822  };
823 
824  } // namespace __cust_access
825 
826  inline namespace __cust
827  {
828  inline constexpr __cust_access::_Begin begin{};
829  inline constexpr __cust_access::_End end{};
830  inline constexpr __cust_access::_CBegin cbegin{};
831  inline constexpr __cust_access::_CEnd cend{};
832  inline constexpr __cust_access::_RBegin rbegin{};
833  inline constexpr __cust_access::_REnd rend{};
834  inline constexpr __cust_access::_CRBegin crbegin{};
835  inline constexpr __cust_access::_CREnd crend{};
836  inline constexpr __cust_access::_Size size{};
837  inline constexpr __cust_access::_Empty empty{};
838  inline constexpr __cust_access::_Data data{};
839  inline constexpr __cust_access::_CData cdata{};
840  }
841 
842  /// [range.range] The range concept.
843  template<typename _Tp>
844  concept range = requires(_Tp& __t)
845  {
846  ranges::begin(__t);
847  ranges::end(__t);
848  };
849 
850  /// [range.range] The safe_range concept.
851  template<typename _Tp>
852  concept safe_range = range<_Tp> && __detail::__maybe_safe_range<_Tp>;
853 
854  template<range _Range>
855  using iterator_t = decltype(ranges::begin(std::declval<_Range&>()));
856 
857  template<range _Range>
858  using sentinel_t = decltype(ranges::end(std::declval<_Range&>()));
859 
860  template<range _Range>
861  using range_difference_t = iter_difference_t<iterator_t<_Range>>;
862 
863  template<range _Range>
864  using range_value_t = iter_value_t<iterator_t<_Range>>;
865 
866  template<range _Range>
867  using range_reference_t = iter_reference_t<iterator_t<_Range>>;
868 
869  template<range _Range>
870  using range_rvalue_reference_t
871  = iter_rvalue_reference_t<iterator_t<_Range>>;
872 
873  /// [range.sized] The sized_range concept.
874  template<typename _Tp>
875  concept sized_range = range<_Tp>
876  && requires(_Tp& __t) { ranges::size(__t); };
877 
878  // [range.refinements]
879 
880  /// A range for which ranges::begin returns an output iterator.
881  template<typename _Range, typename _Tp>
882  concept output_range
883  = range<_Range> && output_iterator<iterator_t<_Range>, _Tp>;
884 
885  /// A range for which ranges::begin returns an input iterator.
886  template<typename _Tp>
887  concept input_range = range<_Tp> && input_iterator<iterator_t<_Tp>>;
888 
889  /// A range for which ranges::begin returns a forward iterator.
890  template<typename _Tp>
891  concept forward_range
892  = input_range<_Tp> && forward_iterator<iterator_t<_Tp>>;
893 
894  /// A range for which ranges::begin returns a bidirectional iterator.
895  template<typename _Tp>
896  concept bidirectional_range
897  = forward_range<_Tp> && bidirectional_iterator<iterator_t<_Tp>>;
898 
899  /// A range for which ranges::begin returns a random access iterator.
900  template<typename _Tp>
901  concept random_access_range
902  = bidirectional_range<_Tp> && random_access_iterator<iterator_t<_Tp>>;
903 
904  /// A range for which ranges::begin returns a contiguous iterator.
905  template<typename _Tp>
906  concept contiguous_range
907  = random_access_range<_Tp> && contiguous_iterator<iterator_t<_Tp>>
908  && requires(_Tp& __t)
909  {
910  { ranges::data(__t) } -> same_as<add_pointer_t<range_reference_t<_Tp>>>;
911  };
912 
913  /// A range for which ranges::begin and ranges::end return the same type.
914  template<typename _Tp>
915  concept common_range
916  = range<_Tp> && same_as<iterator_t<_Tp>, sentinel_t<_Tp>>;
917 
918  // [range.iter.ops] range iterator operations
919 
920  template<input_or_output_iterator _It>
921  constexpr void
922  advance(_It& __it, iter_difference_t<_It> __n)
923  {
924  if constexpr (random_access_iterator<_It>)
925  __it += __n;
926  else if constexpr (bidirectional_iterator<_It>)
927  {
928  if (__n > 0)
929  {
930  do
931  {
932  ++__it;
933  }
934  while (--__n);
935  }
936  else if (__n < 0)
937  {
938  do
939  {
940  --__it;
941  }
942  while (++__n);
943  }
944  }
945  else
946  {
947 #ifdef __cpp_lib_is_constant_evaluated
948  if (std::is_constant_evaluated() && __n < 0)
949  throw "attempt to decrement a non-bidirectional iterator";
950 #endif
951  __glibcxx_assert(__n >= 0);
952  while (__n-- > 0)
953  ++__it;
954  }
955  }
956 
957  template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
958  constexpr void
959  advance(_It& __it, _Sent __bound)
960  {
961  if constexpr (assignable_from<_It&, _Sent>)
962  __it = std::move(__bound);
963  else if constexpr (sized_sentinel_for<_Sent, _It>)
964  ranges::advance(__it, __bound - __it);
965  else
966  {
967  while (__it != __bound)
968  ++__it;
969  }
970  }
971 
972  template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
973  constexpr iter_difference_t<_It>
974  advance(_It& __it, iter_difference_t<_It> __n, _Sent __bound)
975  {
976  if constexpr (sized_sentinel_for<_Sent, _It>)
977  {
978  const auto __diff = __bound - __it;
979 #ifdef __cpp_lib_is_constant_evaluated
980  if (std::is_constant_evaluated()
981  && !(__n == 0 || __diff == 0 || (__n < 0 == __diff < 0)))
982  throw "inconsistent directions for distance and bound";
983 #endif
984  // n and bound must not lead in opposite directions:
985  __glibcxx_assert(__n == 0 || __diff == 0 || (__n < 0 == __diff < 0));
986  const auto __absdiff = __diff < 0 ? -__diff : __diff;
987  const auto __absn = __n < 0 ? -__n : __n;;
988  if (__absn >= __absdiff)
989  {
990  ranges::advance(__it, __bound);
991  return __n - __diff;
992  }
993  else
994  {
995  ranges::advance(__it, __n);
996  return 0;
997  }
998  }
999  else if (__it == __bound || __n == 0)
1000  return iter_difference_t<_It>(0);
1001  else if (__n > 0)
1002  {
1003  iter_difference_t<_It> __m = 0;
1004  do
1005  {
1006  ++__it;
1007  ++__m;
1008  }
1009  while (__m != __n && __it != __bound);
1010  return __n - __m;
1011  }
1012  else if constexpr (bidirectional_iterator<_It> && same_as<_It, _Sent>)
1013  {
1014  iter_difference_t<_It> __m = 0;
1015  do
1016  {
1017  --__it;
1018  --__m;
1019  }
1020  while (__m != __n && __it != __bound);
1021  return __n - __m;
1022  }
1023  else
1024  {
1025 #ifdef __cpp_lib_is_constant_evaluated
1026  if (std::is_constant_evaluated() && __n < 0)
1027  throw "attempt to decrement a non-bidirectional iterator";
1028 #endif
1029  __glibcxx_assert(__n >= 0);
1030  return __n;
1031  }
1032  }
1033 
1034  template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
1035  constexpr iter_difference_t<_It>
1036  distance(_It __first, _Sent __last)
1037  {
1038  if constexpr (sized_sentinel_for<_Sent, _It>)
1039  return __last - __first;
1040  else
1041  {
1042  iter_difference_t<_It> __n = 0;
1043  while (__first != __last)
1044  {
1045  ++__first;
1046  ++__n;
1047  }
1048  return __n;
1049  }
1050  }
1051 
1052  template<range _Range>
1053  constexpr range_difference_t<_Range>
1054  distance(_Range&& __r)
1055  {
1056  if constexpr (sized_range<_Range>)
1057  return static_cast<range_difference_t<_Range>>(ranges::size(__r));
1058  else
1059  return ranges::distance(ranges::begin(__r), ranges::end(__r));
1060  }
1061 
1062  template<input_or_output_iterator _It>
1063  constexpr _It
1064  next(_It __x)
1065  {
1066  ++__x;
1067  return __x;
1068  }
1069 
1070  template<input_or_output_iterator _It>
1071  constexpr _It
1072  next(_It __x, iter_difference_t<_It> __n)
1073  {
1074  ranges::advance(__x, __n);
1075  return __x;
1076  }
1077 
1078  template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
1079  constexpr _It
1080  next(_It __x, _Sent __bound)
1081  {
1082  ranges::advance(__x, __bound);
1083  return __x;
1084  }
1085 
1086  template<input_or_output_iterator _It, sentinel_for<_It> _Sent>
1087  constexpr _It
1088  next(_It __x, iter_difference_t<_It> __n, _Sent __bound)
1089  {
1090  ranges::advance(__x, __n, __bound);
1091  return __x;
1092  }
1093 
1094  template<bidirectional_iterator _It>
1095  constexpr _It
1096  prev(_It __x)
1097  {
1098  --__x;
1099  return __x;
1100  }
1101 
1102  template<bidirectional_iterator _It>
1103  constexpr _It
1104  prev(_It __x, iter_difference_t<_It> __n)
1105  {
1106  ranges::advance(__x, -__n);
1107  return __x;
1108  }
1109 
1110  template<bidirectional_iterator _It>
1111  constexpr _It
1112  prev(_It __x, iter_difference_t<_It> __n, _It __bound)
1113  {
1114  ranges::advance(__x, -__n, __bound);
1115  return __x;
1116  }
1117 
1118 } // namespace ranges
1119 #endif // library concepts
1120 #endif // C++20
1121 _GLIBCXX_END_NAMESPACE_VERSION
1122 } // namespace
1123 
1124 #endif // C++11
1125 
1126 #endif // _GLIBCXX_RANGE_ACCESS_H
std::make_signed_t
typename make_signed< _Tp >::type make_signed_t
Alias template for make_signed.
Definition: type_traits:1960
iterator_concepts.h
std::common_type_t
typename common_type< _Tp... >::type common_type_t
Alias template for common_type.
Definition: type_traits:2561
std::cend
constexpr auto cend(const _Container &__cont) noexcept(noexcept(std::end(__cont))) -> decltype(std::end(__cont))
Return an iterator pointing to one past the last element of the const container.
Definition: range_access.h:129
std::begin
_Tp * begin(valarray< _Tp > &__va)
Return an iterator pointing to the first element of the valarray.
Definition: valarray:1214
std::make_reverse_iterator
constexpr reverse_iterator< _Iterator > make_reverse_iterator(_Iterator __i)
Generator function for reverse_iterator.
Definition: bits/stl_iterator.h:451
std::remove_reference_t
typename remove_reference< _Tp >::type remove_reference_t
Alias template for remove_reference.
Definition: type_traits:1634
std::move
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:101
std::initializer_list
initializer_list
Definition: initializer_list:47
std::advance
constexpr void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
Definition: stl_iterator_base_funcs.h:202
std::reverse_iterator
Definition: bits/stl_iterator.h:110
initializer_list
std::crend
constexpr auto crend(const _Container &__cont) -> decltype(std::rend(__cont))
Return a reverse iterator pointing one past the first element of the const container.
Definition: range_access.h:230
std::rend
constexpr auto rend(_Container &__cont) -> decltype(__cont.rend())
Return a reverse iterator pointing one past the first element of the container.
Definition: range_access.h:160
std
ISO C++ entities toplevel namespace is std.
std::end
constexpr _Tp * end(_Tp(&__arr)[_Nm])
Return an iterator pointing to one past the last element of the array.
Definition: range_access.h:99
std::distance
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
Definition: stl_iterator_base_funcs.h:138
std::begin
constexpr _Tp * begin(_Tp(&__arr)[_Nm])
Return an iterator pointing to the first element of the array.
Definition: range_access.h:89
std::crbegin
constexpr auto crbegin(const _Container &__cont) -> decltype(std::rbegin(__cont))
Return a reverse iterator pointing to the last element of the const container.
Definition: range_access.h:220
std::cbegin
constexpr auto cbegin(const _Container &__cont) noexcept(noexcept(std::begin(__cont))) -> decltype(std::begin(__cont))
Return an iterator pointing to the first element of the const container.
Definition: range_access.h:118
std::rbegin
constexpr auto rbegin(_Container &__cont) -> decltype(__cont.rbegin())
Return a reverse iterator pointing to the last element of the container.
Definition: range_access.h:140
std::end
_Tp * end(valarray< _Tp > &__va)
Return an iterator pointing to one past the last element of the valarray.
Definition: valarray:1234