libstdc++
stl_algobase.h
Go to the documentation of this file.
1 // Core algorithmic facilities -*- C++ -*-
2 
3 // Copyright (C) 2001-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 /*
26  *
27  * Copyright (c) 1994
28  * Hewlett-Packard Company
29  *
30  * Permission to use, copy, modify, distribute and sell this software
31  * and its documentation for any purpose is hereby granted without fee,
32  * provided that the above copyright notice appear in all copies and
33  * that both that copyright notice and this permission notice appear
34  * in supporting documentation. Hewlett-Packard Company makes no
35  * representations about the suitability of this software for any
36  * purpose. It is provided "as is" without express or implied warranty.
37  *
38  *
39  * Copyright (c) 1996-1998
40  * Silicon Graphics Computer Systems, Inc.
41  *
42  * Permission to use, copy, modify, distribute and sell this software
43  * and its documentation for any purpose is hereby granted without fee,
44  * provided that the above copyright notice appear in all copies and
45  * that both that copyright notice and this permission notice appear
46  * in supporting documentation. Silicon Graphics makes no
47  * representations about the suitability of this software for any
48  * purpose. It is provided "as is" without express or implied warranty.
49  */
50 
51 /** @file bits/stl_algobase.h
52  * This is an internal header file, included by other library headers.
53  * Do not attempt to use it directly. @headername{algorithm}
54  */
55 
56 #ifndef _STL_ALGOBASE_H
57 #define _STL_ALGOBASE_H 1
58 
59 #include <bits/c++config.h>
60 #include <bits/functexcept.h>
61 #include <bits/cpp_type_traits.h>
62 #include <ext/type_traits.h>
63 #include <ext/numeric_traits.h>
64 #include <bits/stl_pair.h>
67 #include <bits/stl_iterator.h>
68 #include <bits/concept_check.h>
69 #include <debug/debug.h>
70 #include <bits/move.h> // For std::swap
71 #include <bits/predefined_ops.h>
72 #if __cplusplus >= 201103L
73 # include <type_traits>
74 #endif
75 #if __cplusplus > 201703L
76 # include <compare>
77 #endif
78 
79 namespace std _GLIBCXX_VISIBILITY(default)
80 {
81 _GLIBCXX_BEGIN_NAMESPACE_VERSION
82 
83  /*
84  * A constexpr wrapper for __builtin_memcmp.
85  * @param __num The number of elements of type _Tp (not bytes).
86  */
87  template<typename _Tp, typename _Up>
88  _GLIBCXX14_CONSTEXPR
89  inline int
90  __memcmp(const _Tp* __first1, const _Up* __first2, size_t __num)
91  {
92 #if __cplusplus >= 201103L
93  static_assert(sizeof(_Tp) == sizeof(_Up), "can be compared with memcmp");
94 #endif
95 #ifdef __cpp_lib_is_constant_evaluated
96  if (std::is_constant_evaluated())
97  {
98  for(; __num > 0; ++__first1, ++__first2, --__num)
99  if (*__first1 != *__first2)
100  return *__first1 < *__first2 ? -1 : 1;
101  return 0;
102  }
103  else
104 #endif
105  return __builtin_memcmp(__first1, __first2, sizeof(_Tp) * __num);
106  }
107 
108 #if __cplusplus < 201103L
109  // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
110  // nutshell, we are partially implementing the resolution of DR 187,
111  // when it's safe, i.e., the value_types are equal.
112  template<bool _BoolType>
113  struct __iter_swap
114  {
115  template<typename _ForwardIterator1, typename _ForwardIterator2>
116  static void
117  iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
118  {
119  typedef typename iterator_traits<_ForwardIterator1>::value_type
120  _ValueType1;
121  _ValueType1 __tmp = *__a;
122  *__a = *__b;
123  *__b = __tmp;
124  }
125  };
126 
127  template<>
128  struct __iter_swap<true>
129  {
130  template<typename _ForwardIterator1, typename _ForwardIterator2>
131  static void
132  iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
133  {
134  swap(*__a, *__b);
135  }
136  };
137 #endif // C++03
138 
139  /**
140  * @brief Swaps the contents of two iterators.
141  * @ingroup mutating_algorithms
142  * @param __a An iterator.
143  * @param __b Another iterator.
144  * @return Nothing.
145  *
146  * This function swaps the values pointed to by two iterators, not the
147  * iterators themselves.
148  */
149  template<typename _ForwardIterator1, typename _ForwardIterator2>
150  _GLIBCXX20_CONSTEXPR
151  inline void
152  iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
153  {
154  // concept requirements
155  __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
156  _ForwardIterator1>)
157  __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
158  _ForwardIterator2>)
159 
160 #if __cplusplus < 201103L
162  _ValueType1;
164  _ValueType2;
165 
166  __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
167  _ValueType2>)
168  __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
169  _ValueType1>)
170 
172  _ReferenceType1;
174  _ReferenceType2;
175  std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
176  && __are_same<_ValueType1&, _ReferenceType1>::__value
177  && __are_same<_ValueType2&, _ReferenceType2>::__value>::
178  iter_swap(__a, __b);
179 #else
180  // _GLIBCXX_RESOLVE_LIB_DEFECTS
181  // 187. iter_swap underspecified
182  swap(*__a, *__b);
183 #endif
184  }
185 
186  /**
187  * @brief Swap the elements of two sequences.
188  * @ingroup mutating_algorithms
189  * @param __first1 A forward iterator.
190  * @param __last1 A forward iterator.
191  * @param __first2 A forward iterator.
192  * @return An iterator equal to @p first2+(last1-first1).
193  *
194  * Swaps each element in the range @p [first1,last1) with the
195  * corresponding element in the range @p [first2,(last1-first1)).
196  * The ranges must not overlap.
197  */
198  template<typename _ForwardIterator1, typename _ForwardIterator2>
199  _GLIBCXX20_CONSTEXPR
200  _ForwardIterator2
201  swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
202  _ForwardIterator2 __first2)
203  {
204  // concept requirements
205  __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
206  _ForwardIterator1>)
207  __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
208  _ForwardIterator2>)
209  __glibcxx_requires_valid_range(__first1, __last1);
210 
211  for (; __first1 != __last1; ++__first1, (void)++__first2)
212  std::iter_swap(__first1, __first2);
213  return __first2;
214  }
215 
216  /**
217  * @brief This does what you think it does.
218  * @ingroup sorting_algorithms
219  * @param __a A thing of arbitrary type.
220  * @param __b Another thing of arbitrary type.
221  * @return The lesser of the parameters.
222  *
223  * This is the simple classic generic implementation. It will work on
224  * temporary expressions, since they are only evaluated once, unlike a
225  * preprocessor macro.
226  */
227  template<typename _Tp>
228  _GLIBCXX14_CONSTEXPR
229  inline const _Tp&
230  min(const _Tp& __a, const _Tp& __b)
231  {
232  // concept requirements
233  __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
234  //return __b < __a ? __b : __a;
235  if (__b < __a)
236  return __b;
237  return __a;
238  }
239 
240  /**
241  * @brief This does what you think it does.
242  * @ingroup sorting_algorithms
243  * @param __a A thing of arbitrary type.
244  * @param __b Another thing of arbitrary type.
245  * @return The greater of the parameters.
246  *
247  * This is the simple classic generic implementation. It will work on
248  * temporary expressions, since they are only evaluated once, unlike a
249  * preprocessor macro.
250  */
251  template<typename _Tp>
252  _GLIBCXX14_CONSTEXPR
253  inline const _Tp&
254  max(const _Tp& __a, const _Tp& __b)
255  {
256  // concept requirements
257  __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
258  //return __a < __b ? __b : __a;
259  if (__a < __b)
260  return __b;
261  return __a;
262  }
263 
264  /**
265  * @brief This does what you think it does.
266  * @ingroup sorting_algorithms
267  * @param __a A thing of arbitrary type.
268  * @param __b Another thing of arbitrary type.
269  * @param __comp A @link comparison_functors comparison functor@endlink.
270  * @return The lesser of the parameters.
271  *
272  * This will work on temporary expressions, since they are only evaluated
273  * once, unlike a preprocessor macro.
274  */
275  template<typename _Tp, typename _Compare>
276  _GLIBCXX14_CONSTEXPR
277  inline const _Tp&
278  min(const _Tp& __a, const _Tp& __b, _Compare __comp)
279  {
280  //return __comp(__b, __a) ? __b : __a;
281  if (__comp(__b, __a))
282  return __b;
283  return __a;
284  }
285 
286  /**
287  * @brief This does what you think it does.
288  * @ingroup sorting_algorithms
289  * @param __a A thing of arbitrary type.
290  * @param __b Another thing of arbitrary type.
291  * @param __comp A @link comparison_functors comparison functor@endlink.
292  * @return The greater of the parameters.
293  *
294  * This will work on temporary expressions, since they are only evaluated
295  * once, unlike a preprocessor macro.
296  */
297  template<typename _Tp, typename _Compare>
298  _GLIBCXX14_CONSTEXPR
299  inline const _Tp&
300  max(const _Tp& __a, const _Tp& __b, _Compare __comp)
301  {
302  //return __comp(__a, __b) ? __b : __a;
303  if (__comp(__a, __b))
304  return __b;
305  return __a;
306  }
307 
308  // Fallback implementation of the function in bits/stl_iterator.h used to
309  // remove the __normal_iterator wrapper. See copy, fill, ...
310  template<typename _Iterator>
311  _GLIBCXX20_CONSTEXPR
312  inline _Iterator
313  __niter_base(_Iterator __it)
315  { return __it; }
316 
317  // Reverse the __niter_base transformation to get a
318  // __normal_iterator back again (this assumes that __normal_iterator
319  // is only used to wrap random access iterators, like pointers).
320  template<typename _From, typename _To>
321  _GLIBCXX20_CONSTEXPR
322  inline _From
323  __niter_wrap(_From __from, _To __res)
324  { return __from + (__res - std::__niter_base(__from)); }
325 
326  // No need to wrap, iterator already has the right type.
327  template<typename _Iterator>
328  _GLIBCXX20_CONSTEXPR
329  inline _Iterator
330  __niter_wrap(const _Iterator&, _Iterator __res)
331  { return __res; }
332 
333  // All of these auxiliary structs serve two purposes. (1) Replace
334  // calls to copy with memmove whenever possible. (Memmove, not memcpy,
335  // because the input and output ranges are permitted to overlap.)
336  // (2) If we're using random access iterators, then write the loop as
337  // a for loop with an explicit count.
338 
339  template<bool _IsMove, bool _IsSimple, typename _Category>
340  struct __copy_move
341  {
342  template<typename _II, typename _OI>
343  _GLIBCXX20_CONSTEXPR
344  static _OI
345  __copy_m(_II __first, _II __last, _OI __result)
346  {
347  for (; __first != __last; ++__result, (void)++__first)
348  *__result = *__first;
349  return __result;
350  }
351  };
352 
353 #if __cplusplus >= 201103L
354  template<typename _Category>
355  struct __copy_move<true, false, _Category>
356  {
357  template<typename _II, typename _OI>
358  _GLIBCXX20_CONSTEXPR
359  static _OI
360  __copy_m(_II __first, _II __last, _OI __result)
361  {
362  for (; __first != __last; ++__result, (void)++__first)
363  *__result = std::move(*__first);
364  return __result;
365  }
366  };
367 #endif
368 
369  template<>
370  struct __copy_move<false, false, random_access_iterator_tag>
371  {
372  template<typename _II, typename _OI>
373  _GLIBCXX20_CONSTEXPR
374  static _OI
375  __copy_m(_II __first, _II __last, _OI __result)
376  {
377  typedef typename iterator_traits<_II>::difference_type _Distance;
378  for(_Distance __n = __last - __first; __n > 0; --__n)
379  {
380  *__result = *__first;
381  ++__first;
382  ++__result;
383  }
384  return __result;
385  }
386  };
387 
388 #if __cplusplus >= 201103L
389  template<>
390  struct __copy_move<true, false, random_access_iterator_tag>
391  {
392  template<typename _II, typename _OI>
393  _GLIBCXX20_CONSTEXPR
394  static _OI
395  __copy_m(_II __first, _II __last, _OI __result)
396  {
397  typedef typename iterator_traits<_II>::difference_type _Distance;
398  for(_Distance __n = __last - __first; __n > 0; --__n)
399  {
400  *__result = std::move(*__first);
401  ++__first;
402  ++__result;
403  }
404  return __result;
405  }
406  };
407 #endif
408 
409  template<bool _IsMove>
410  struct __copy_move<_IsMove, true, random_access_iterator_tag>
411  {
412  template<typename _Tp>
413  _GLIBCXX20_CONSTEXPR
414  static _Tp*
415  __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
416  {
417 #if __cplusplus >= 201103L
418  using __assignable = conditional<_IsMove,
419  is_move_assignable<_Tp>,
420  is_copy_assignable<_Tp>>;
421  // trivial types can have deleted assignment
422  static_assert( __assignable::type::value, "type is not assignable" );
423 #endif
424  const ptrdiff_t _Num = __last - __first;
425  if (_Num)
426  __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
427  return __result + _Num;
428  }
429  };
430 
431  // Helpers for streambuf iterators (either istream or ostream).
432  // NB: avoid including <iosfwd>, relatively large.
433  template<typename _CharT>
434  struct char_traits;
435 
436  template<typename _CharT, typename _Traits>
437  class istreambuf_iterator;
438 
439  template<typename _CharT, typename _Traits>
440  class ostreambuf_iterator;
441 
442  template<bool _IsMove, typename _CharT>
443  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
444  ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
445  __copy_move_a2(_CharT*, _CharT*,
446  ostreambuf_iterator<_CharT, char_traits<_CharT> >);
447 
448  template<bool _IsMove, typename _CharT>
449  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
450  ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
451  __copy_move_a2(const _CharT*, const _CharT*,
452  ostreambuf_iterator<_CharT, char_traits<_CharT> >);
453 
454  template<bool _IsMove, typename _CharT>
455  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
456  _CharT*>::__type
457  __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
458  istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
459 
460  template<bool _IsMove, typename _II, typename _OI>
461  _GLIBCXX20_CONSTEXPR
462  inline _OI
463  __copy_move_a2(_II __first, _II __last, _OI __result)
464  {
465  typedef typename iterator_traits<_II>::iterator_category _Category;
466 #ifdef __cpp_lib_is_constant_evaluated
467  if (std::is_constant_evaluated())
468  return std::__copy_move<_IsMove, false, _Category>::
469  __copy_m(__first, __last, __result);
470 #endif
471  return std::__copy_move<_IsMove, __memcpyable<_OI, _II>::__value,
472  _Category>::__copy_m(__first, __last, __result);
473  }
474 
475 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
476 
477  template<typename _Tp, typename _Ref, typename _Ptr>
479 
480 _GLIBCXX_END_NAMESPACE_CONTAINER
481 
482  template<bool _IsMove,
483  typename _Tp, typename _Ref, typename _Ptr, typename _OI>
484  _OI
485  __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
486  _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
487  _OI);
488 
489  template<bool _IsMove,
490  typename _ITp, typename _IRef, typename _IPtr, typename _OTp>
491  _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
492  __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
493  _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
494  _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>);
495 
496  template<bool _IsMove, typename _II, typename _Tp>
497  typename __gnu_cxx::__enable_if<
498  __is_random_access_iter<_II>::__value,
499  _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> >::__type
500  __copy_move_a1(_II, _II, _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>);
501 
502  template<bool _IsMove, typename _II, typename _OI>
503  _GLIBCXX20_CONSTEXPR
504  inline _OI
505  __copy_move_a1(_II __first, _II __last, _OI __result)
506  { return std::__copy_move_a2<_IsMove>(__first, __last, __result); }
507 
508  template<bool _IsMove, typename _II, typename _OI>
509  _GLIBCXX20_CONSTEXPR
510  inline _OI
511  __copy_move_a(_II __first, _II __last, _OI __result)
512  {
513  return std::__niter_wrap(__result,
514  std::__copy_move_a1<_IsMove>(std::__niter_base(__first),
515  std::__niter_base(__last),
516  std::__niter_base(__result)));
517  }
518 
519  template<bool _IsMove,
520  typename _Ite, typename _Seq, typename _Cat, typename _OI>
521  _OI
522  __copy_move_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
523  const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
524  _OI);
525 
526  template<bool _IsMove,
527  typename _II, typename _Ite, typename _Seq, typename _Cat>
529  __copy_move_a(_II, _II,
530  const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&);
531 
532  template<bool _IsMove,
533  typename _IIte, typename _ISeq, typename _ICat,
534  typename _OIte, typename _OSeq, typename _OCat>
536  __copy_move_a(const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
537  const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
538  const ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>&);
539 
540  /**
541  * @brief Copies the range [first,last) into result.
542  * @ingroup mutating_algorithms
543  * @param __first An input iterator.
544  * @param __last An input iterator.
545  * @param __result An output iterator.
546  * @return result + (first - last)
547  *
548  * This inline function will boil down to a call to @c memmove whenever
549  * possible. Failing that, if random access iterators are passed, then the
550  * loop count will be known (and therefore a candidate for compiler
551  * optimizations such as unrolling). Result may not be contained within
552  * [first,last); the copy_backward function should be used instead.
553  *
554  * Note that the end of the output range is permitted to be contained
555  * within [first,last).
556  */
557  template<typename _II, typename _OI>
558  _GLIBCXX20_CONSTEXPR
559  inline _OI
560  copy(_II __first, _II __last, _OI __result)
561  {
562  // concept requirements
563  __glibcxx_function_requires(_InputIteratorConcept<_II>)
564  __glibcxx_function_requires(_OutputIteratorConcept<_OI,
566  __glibcxx_requires_can_increment_range(__first, __last, __result);
567 
568  return std::__copy_move_a<__is_move_iterator<_II>::__value>
569  (std::__miter_base(__first), std::__miter_base(__last), __result);
570  }
571 
572 #if __cplusplus >= 201103L
573  /**
574  * @brief Moves the range [first,last) into result.
575  * @ingroup mutating_algorithms
576  * @param __first An input iterator.
577  * @param __last An input iterator.
578  * @param __result An output iterator.
579  * @return result + (first - last)
580  *
581  * This inline function will boil down to a call to @c memmove whenever
582  * possible. Failing that, if random access iterators are passed, then the
583  * loop count will be known (and therefore a candidate for compiler
584  * optimizations such as unrolling). Result may not be contained within
585  * [first,last); the move_backward function should be used instead.
586  *
587  * Note that the end of the output range is permitted to be contained
588  * within [first,last).
589  */
590  template<typename _II, typename _OI>
591  _GLIBCXX20_CONSTEXPR
592  inline _OI
593  move(_II __first, _II __last, _OI __result)
594  {
595  // concept requirements
596  __glibcxx_function_requires(_InputIteratorConcept<_II>)
597  __glibcxx_function_requires(_OutputIteratorConcept<_OI,
599  __glibcxx_requires_can_increment_range(__first, __last, __result);
600 
601  return std::__copy_move_a<true>(std::__miter_base(__first),
602  std::__miter_base(__last), __result);
603  }
604 
605 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
606 #else
607 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
608 #endif
609 
610  template<bool _IsMove, bool _IsSimple, typename _Category>
611  struct __copy_move_backward
612  {
613  template<typename _BI1, typename _BI2>
614  _GLIBCXX20_CONSTEXPR
615  static _BI2
616  __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
617  {
618  while (__first != __last)
619  *--__result = *--__last;
620  return __result;
621  }
622  };
623 
624 #if __cplusplus >= 201103L
625  template<typename _Category>
626  struct __copy_move_backward<true, false, _Category>
627  {
628  template<typename _BI1, typename _BI2>
629  _GLIBCXX20_CONSTEXPR
630  static _BI2
631  __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
632  {
633  while (__first != __last)
634  *--__result = std::move(*--__last);
635  return __result;
636  }
637  };
638 #endif
639 
640  template<>
641  struct __copy_move_backward<false, false, random_access_iterator_tag>
642  {
643  template<typename _BI1, typename _BI2>
644  _GLIBCXX20_CONSTEXPR
645  static _BI2
646  __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
647  {
648  typename iterator_traits<_BI1>::difference_type
649  __n = __last - __first;
650  for (; __n > 0; --__n)
651  *--__result = *--__last;
652  return __result;
653  }
654  };
655 
656 #if __cplusplus >= 201103L
657  template<>
658  struct __copy_move_backward<true, false, random_access_iterator_tag>
659  {
660  template<typename _BI1, typename _BI2>
661  _GLIBCXX20_CONSTEXPR
662  static _BI2
663  __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
664  {
665  typename iterator_traits<_BI1>::difference_type
666  __n = __last - __first;
667  for (; __n > 0; --__n)
668  *--__result = std::move(*--__last);
669  return __result;
670  }
671  };
672 #endif
673 
674  template<bool _IsMove>
675  struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
676  {
677  template<typename _Tp>
678  _GLIBCXX20_CONSTEXPR
679  static _Tp*
680  __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
681  {
682 #if __cplusplus >= 201103L
683  using __assignable = conditional<_IsMove,
684  is_move_assignable<_Tp>,
685  is_copy_assignable<_Tp>>;
686  // trivial types can have deleted assignment
687  static_assert( __assignable::type::value, "type is not assignable" );
688 #endif
689  const ptrdiff_t _Num = __last - __first;
690  if (_Num)
691  __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
692  return __result - _Num;
693  }
694  };
695 
696  template<bool _IsMove, typename _BI1, typename _BI2>
697  _GLIBCXX20_CONSTEXPR
698  inline _BI2
699  __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
700  {
701  typedef typename iterator_traits<_BI1>::iterator_category _Category;
702 #ifdef __cpp_lib_is_constant_evaluated
703  if (std::is_constant_evaluated())
704  return std::__copy_move_backward<_IsMove, false, _Category>::
705  __copy_move_b(__first, __last, __result);
706 #endif
707  return std::__copy_move_backward<_IsMove,
708  __memcpyable<_BI2, _BI1>::__value,
709  _Category>::__copy_move_b(__first,
710  __last,
711  __result);
712  }
713 
714  template<bool _IsMove, typename _BI1, typename _BI2>
715  _GLIBCXX20_CONSTEXPR
716  inline _BI2
717  __copy_move_backward_a1(_BI1 __first, _BI1 __last, _BI2 __result)
718  { return std::__copy_move_backward_a2<_IsMove>(__first, __last, __result); }
719 
720  template<bool _IsMove,
721  typename _Tp, typename _Ref, typename _Ptr, typename _OI>
722  _OI
723  __copy_move_backward_a1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
724  _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
725  _OI);
726 
727  template<bool _IsMove,
728  typename _ITp, typename _IRef, typename _IPtr, typename _OTp>
729  _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
730  __copy_move_backward_a1(
731  _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
732  _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
733  _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>);
734 
735  template<bool _IsMove, typename _II, typename _Tp>
736  typename __gnu_cxx::__enable_if<
737  __is_random_access_iter<_II>::__value,
738  _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> >::__type
739  __copy_move_backward_a1(_II, _II,
740  _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>);
741 
742  template<bool _IsMove, typename _II, typename _OI>
743  _GLIBCXX20_CONSTEXPR
744  inline _OI
745  __copy_move_backward_a(_II __first, _II __last, _OI __result)
746  {
747  return std::__niter_wrap(__result,
748  std::__copy_move_backward_a1<_IsMove>
749  (std::__niter_base(__first), std::__niter_base(__last),
750  std::__niter_base(__result)));
751  }
752 
753  template<bool _IsMove,
754  typename _Ite, typename _Seq, typename _Cat, typename _OI>
755  _OI
756  __copy_move_backward_a(
757  const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
758  const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
759  _OI);
760 
761  template<bool _IsMove,
762  typename _II, typename _Ite, typename _Seq, typename _Cat>
764  __copy_move_backward_a(_II, _II,
765  const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&);
766 
767  template<bool _IsMove,
768  typename _IIte, typename _ISeq, typename _ICat,
769  typename _OIte, typename _OSeq, typename _OCat>
771  __copy_move_backward_a(
772  const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
773  const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
774  const ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>&);
775 
776  /**
777  * @brief Copies the range [first,last) into result.
778  * @ingroup mutating_algorithms
779  * @param __first A bidirectional iterator.
780  * @param __last A bidirectional iterator.
781  * @param __result A bidirectional iterator.
782  * @return result - (first - last)
783  *
784  * The function has the same effect as copy, but starts at the end of the
785  * range and works its way to the start, returning the start of the result.
786  * This inline function will boil down to a call to @c memmove whenever
787  * possible. Failing that, if random access iterators are passed, then the
788  * loop count will be known (and therefore a candidate for compiler
789  * optimizations such as unrolling).
790  *
791  * Result may not be in the range (first,last]. Use copy instead. Note
792  * that the start of the output range may overlap [first,last).
793  */
794  template<typename _BI1, typename _BI2>
795  _GLIBCXX20_CONSTEXPR
796  inline _BI2
797  copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
798  {
799  // concept requirements
800  __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
801  __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
802  __glibcxx_function_requires(_ConvertibleConcept<
805  __glibcxx_requires_can_decrement_range(__first, __last, __result);
806 
807  return std::__copy_move_backward_a<__is_move_iterator<_BI1>::__value>
808  (std::__miter_base(__first), std::__miter_base(__last), __result);
809  }
810 
811 #if __cplusplus >= 201103L
812  /**
813  * @brief Moves the range [first,last) into result.
814  * @ingroup mutating_algorithms
815  * @param __first A bidirectional iterator.
816  * @param __last A bidirectional iterator.
817  * @param __result A bidirectional iterator.
818  * @return result - (first - last)
819  *
820  * The function has the same effect as move, but starts at the end of the
821  * range and works its way to the start, returning the start of the result.
822  * This inline function will boil down to a call to @c memmove whenever
823  * possible. Failing that, if random access iterators are passed, then the
824  * loop count will be known (and therefore a candidate for compiler
825  * optimizations such as unrolling).
826  *
827  * Result may not be in the range (first,last]. Use move instead. Note
828  * that the start of the output range may overlap [first,last).
829  */
830  template<typename _BI1, typename _BI2>
831  _GLIBCXX20_CONSTEXPR
832  inline _BI2
833  move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
834  {
835  // concept requirements
836  __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
837  __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
838  __glibcxx_function_requires(_ConvertibleConcept<
841  __glibcxx_requires_can_decrement_range(__first, __last, __result);
842 
843  return std::__copy_move_backward_a<true>(std::__miter_base(__first),
844  std::__miter_base(__last),
845  __result);
846  }
847 
848 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
849 #else
850 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
851 #endif
852 
853  template<typename _ForwardIterator, typename _Tp>
854  _GLIBCXX20_CONSTEXPR
855  inline typename
856  __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
857  __fill_a1(_ForwardIterator __first, _ForwardIterator __last,
858  const _Tp& __value)
859  {
860  for (; __first != __last; ++__first)
861  *__first = __value;
862  }
863 
864  template<typename _ForwardIterator, typename _Tp>
865  _GLIBCXX20_CONSTEXPR
866  inline typename
867  __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
868  __fill_a1(_ForwardIterator __first, _ForwardIterator __last,
869  const _Tp& __value)
870  {
871  const _Tp __tmp = __value;
872  for (; __first != __last; ++__first)
873  *__first = __tmp;
874  }
875 
876  // Specialization: for char types we can use memset.
877  template<typename _Tp>
878  _GLIBCXX20_CONSTEXPR
879  inline typename
880  __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
881  __fill_a1(_Tp* __first, _Tp* __last, const _Tp& __c)
882  {
883  const _Tp __tmp = __c;
884 #if __cpp_lib_is_constant_evaluated
885  if (std::is_constant_evaluated())
886  {
887  for (; __first != __last; ++__first)
888  *__first = __tmp;
889  return;
890  }
891 #endif
892  if (const size_t __len = __last - __first)
893  __builtin_memset(__first, static_cast<unsigned char>(__tmp), __len);
894  }
895 
896  template<typename _Ite, typename _Cont, typename _Tp>
897  _GLIBCXX20_CONSTEXPR
898  inline void
899  __fill_a1(::__gnu_cxx::__normal_iterator<_Ite, _Cont> __first,
900  ::__gnu_cxx::__normal_iterator<_Ite, _Cont> __last,
901  const _Tp& __value)
902  { std::__fill_a1(__first.base(), __last.base(), __value); }
903 
904  template<typename _Tp, typename _VTp>
905  void
906  __fill_a1(const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
907  const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
908  const _VTp&);
909 
910  template<typename _FIte, typename _Tp>
911  _GLIBCXX20_CONSTEXPR
912  inline void
913  __fill_a(_FIte __first, _FIte __last, const _Tp& __value)
914  { std::__fill_a1(__first, __last, __value); }
915 
916  template<typename _Ite, typename _Seq, typename _Cat, typename _Tp>
917  void
918  __fill_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
919  const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
920  const _Tp&);
921 
922  /**
923  * @brief Fills the range [first,last) with copies of value.
924  * @ingroup mutating_algorithms
925  * @param __first A forward iterator.
926  * @param __last A forward iterator.
927  * @param __value A reference-to-const of arbitrary type.
928  * @return Nothing.
929  *
930  * This function fills a range with copies of the same value. For char
931  * types filling contiguous areas of memory, this becomes an inline call
932  * to @c memset or @c wmemset.
933  */
934  template<typename _ForwardIterator, typename _Tp>
935  _GLIBCXX20_CONSTEXPR
936  inline void
937  fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
938  {
939  // concept requirements
940  __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
941  _ForwardIterator>)
942  __glibcxx_requires_valid_range(__first, __last);
943 
944  std::__fill_a(__first, __last, __value);
945  }
946 
947  // Used by fill_n, generate_n, etc. to convert _Size to an integral type:
948  inline _GLIBCXX_CONSTEXPR int
949  __size_to_integer(int __n) { return __n; }
950  inline _GLIBCXX_CONSTEXPR unsigned
951  __size_to_integer(unsigned __n) { return __n; }
952  inline _GLIBCXX_CONSTEXPR long
953  __size_to_integer(long __n) { return __n; }
954  inline _GLIBCXX_CONSTEXPR unsigned long
955  __size_to_integer(unsigned long __n) { return __n; }
956  inline _GLIBCXX_CONSTEXPR long long
957  __size_to_integer(long long __n) { return __n; }
958  inline _GLIBCXX_CONSTEXPR unsigned long long
959  __size_to_integer(unsigned long long __n) { return __n; }
960 
961 #if defined(__GLIBCXX_TYPE_INT_N_0)
962  inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_0
963  __size_to_integer(__GLIBCXX_TYPE_INT_N_0 __n) { return __n; }
964  inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_0
965  __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_0 __n) { return __n; }
966 #endif
967 #if defined(__GLIBCXX_TYPE_INT_N_1)
968  inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_1
969  __size_to_integer(__GLIBCXX_TYPE_INT_N_1 __n) { return __n; }
970  inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_1
971  __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_1 __n) { return __n; }
972 #endif
973 #if defined(__GLIBCXX_TYPE_INT_N_2)
974  inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_2
975  __size_to_integer(__GLIBCXX_TYPE_INT_N_2 __n) { return __n; }
976  inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_2
977  __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_2 __n) { return __n; }
978 #endif
979 #if defined(__GLIBCXX_TYPE_INT_N_3)
980  inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_3
981  __size_to_integer(__GLIBCXX_TYPE_INT_N_3 __n) { return __n; }
982  inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_3
983  __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_3 __n) { return __n; }
984 #endif
985 
986  inline _GLIBCXX_CONSTEXPR long long
987  __size_to_integer(float __n) { return __n; }
988  inline _GLIBCXX_CONSTEXPR long long
989  __size_to_integer(double __n) { return __n; }
990  inline _GLIBCXX_CONSTEXPR long long
991  __size_to_integer(long double __n) { return __n; }
992 #if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
993  inline _GLIBCXX_CONSTEXPR long long
994  __size_to_integer(__float128 __n) { return __n; }
995 #endif
996 
997  template<typename _OutputIterator, typename _Size, typename _Tp>
998  _GLIBCXX20_CONSTEXPR
999  inline typename
1000  __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
1001  __fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
1002  {
1003  for (; __n > 0; --__n, (void) ++__first)
1004  *__first = __value;
1005  return __first;
1006  }
1007 
1008  template<typename _OutputIterator, typename _Size, typename _Tp>
1009  _GLIBCXX20_CONSTEXPR
1010  inline typename
1011  __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
1012  __fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
1013  {
1014  const _Tp __tmp = __value;
1015  for (; __n > 0; --__n, (void) ++__first)
1016  *__first = __tmp;
1017  return __first;
1018  }
1019 
1020  template<typename _Ite, typename _Seq, typename _Cat, typename _Size,
1021  typename _Tp>
1023  __fill_n_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __first,
1024  _Size __n, const _Tp& __value,
1026 
1027  template<typename _OutputIterator, typename _Size, typename _Tp>
1028  _GLIBCXX20_CONSTEXPR
1029  inline _OutputIterator
1030  __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
1032  {
1033 #if __cplusplus >= 201103L
1034  static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
1035 #endif
1036  return __fill_n_a1(__first, __n, __value);
1037  }
1038 
1039  template<typename _OutputIterator, typename _Size, typename _Tp>
1040  _GLIBCXX20_CONSTEXPR
1041  inline _OutputIterator
1042  __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
1044  {
1045 #if __cplusplus >= 201103L
1046  static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
1047 #endif
1048  return __fill_n_a1(__first, __n, __value);
1049  }
1050 
1051  template<typename _OutputIterator, typename _Size, typename _Tp>
1052  _GLIBCXX20_CONSTEXPR
1053  inline _OutputIterator
1054  __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
1056  {
1057 #if __cplusplus >= 201103L
1058  static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
1059 #endif
1060  if (__n <= 0)
1061  return __first;
1062 
1063  __glibcxx_requires_can_increment(__first, __n);
1064 
1065  std::__fill_a(__first, __first + __n, __value);
1066  return __first + __n;
1067  }
1068 
1069  /**
1070  * @brief Fills the range [first,first+n) with copies of value.
1071  * @ingroup mutating_algorithms
1072  * @param __first An output iterator.
1073  * @param __n The count of copies to perform.
1074  * @param __value A reference-to-const of arbitrary type.
1075  * @return The iterator at first+n.
1076  *
1077  * This function fills a range with copies of the same value. For char
1078  * types filling contiguous areas of memory, this becomes an inline call
1079  * to @c memset or @c wmemset.
1080  *
1081  * If @p __n is negative, the function does nothing.
1082  */
1083  // _GLIBCXX_RESOLVE_LIB_DEFECTS
1084  // DR 865. More algorithms that throw away information
1085  // DR 426. search_n(), fill_n(), and generate_n() with negative n
1086  template<typename _OI, typename _Size, typename _Tp>
1087  _GLIBCXX20_CONSTEXPR
1088  inline _OI
1089  fill_n(_OI __first, _Size __n, const _Tp& __value)
1090  {
1091  // concept requirements
1092  __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
1093 
1094  return std::__fill_n_a(__first, std::__size_to_integer(__n), __value,
1095  std::__iterator_category(__first));
1096  }
1097 
1098  template<bool _BoolType>
1099  struct __equal
1100  {
1101  template<typename _II1, typename _II2>
1102  _GLIBCXX20_CONSTEXPR
1103  static bool
1104  equal(_II1 __first1, _II1 __last1, _II2 __first2)
1105  {
1106  for (; __first1 != __last1; ++__first1, (void) ++__first2)
1107  if (!(*__first1 == *__first2))
1108  return false;
1109  return true;
1110  }
1111  };
1112 
1113  template<>
1114  struct __equal<true>
1115  {
1116  template<typename _Tp>
1117  _GLIBCXX20_CONSTEXPR
1118  static bool
1119  equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
1120  {
1121  if (const size_t __len = (__last1 - __first1))
1122  return !std::__memcmp(__first1, __first2, __len);
1123  return true;
1124  }
1125  };
1126 
1127  template<typename _Tp, typename _Ref, typename _Ptr, typename _II>
1128  typename __gnu_cxx::__enable_if<
1129  __is_random_access_iter<_II>::__value, bool>::__type
1130  __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
1131  _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
1132  _II);
1133 
1134  template<typename _Tp1, typename _Ref1, typename _Ptr1,
1135  typename _Tp2, typename _Ref2, typename _Ptr2>
1136  bool
1137  __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1138  _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1139  _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
1140 
1141  template<typename _II, typename _Tp, typename _Ref, typename _Ptr>
1142  typename __gnu_cxx::__enable_if<
1143  __is_random_access_iter<_II>::__value, bool>::__type
1144  __equal_aux1(_II, _II,
1145  _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>);
1146 
1147  template<typename _II1, typename _II2>
1148  _GLIBCXX20_CONSTEXPR
1149  inline bool
1150  __equal_aux1(_II1 __first1, _II1 __last1, _II2 __first2)
1151  {
1152  typedef typename iterator_traits<_II1>::value_type _ValueType1;
1153  const bool __simple = ((__is_integer<_ValueType1>::__value
1154  || __is_pointer<_ValueType1>::__value)
1155  && __memcmpable<_II1, _II2>::__value);
1156  return std::__equal<__simple>::equal(__first1, __last1, __first2);
1157  }
1158 
1159  template<typename _II1, typename _II2>
1160  _GLIBCXX20_CONSTEXPR
1161  inline bool
1162  __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
1163  {
1164  return std::__equal_aux1(std::__niter_base(__first1),
1165  std::__niter_base(__last1),
1166  std::__niter_base(__first2));
1167  }
1168 
1169  template<typename _II1, typename _Seq1, typename _Cat1, typename _II2>
1170  bool
1171  __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1172  const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1173  _II2);
1174 
1175  template<typename _II1, typename _II2, typename _Seq2, typename _Cat2>
1176  bool
1177  __equal_aux(_II1, _II1,
1178  const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
1179 
1180  template<typename _II1, typename _Seq1, typename _Cat1,
1181  typename _II2, typename _Seq2, typename _Cat2>
1182  bool
1183  __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1184  const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1185  const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
1186 
1187  template<typename, typename>
1188  struct __lc_rai
1189  {
1190  template<typename _II1, typename _II2>
1191  _GLIBCXX20_CONSTEXPR
1192  static _II1
1193  __newlast1(_II1, _II1 __last1, _II2, _II2)
1194  { return __last1; }
1195 
1196  template<typename _II>
1197  _GLIBCXX20_CONSTEXPR
1198  static bool
1199  __cnd2(_II __first, _II __last)
1200  { return __first != __last; }
1201  };
1202 
1203  template<>
1204  struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
1205  {
1206  template<typename _RAI1, typename _RAI2>
1207  _GLIBCXX20_CONSTEXPR
1208  static _RAI1
1209  __newlast1(_RAI1 __first1, _RAI1 __last1,
1210  _RAI2 __first2, _RAI2 __last2)
1211  {
1212  const typename iterator_traits<_RAI1>::difference_type
1213  __diff1 = __last1 - __first1;
1214  const typename iterator_traits<_RAI2>::difference_type
1215  __diff2 = __last2 - __first2;
1216  return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
1217  }
1218 
1219  template<typename _RAI>
1220  static _GLIBCXX20_CONSTEXPR bool
1221  __cnd2(_RAI, _RAI)
1222  { return true; }
1223  };
1224 
1225  template<typename _II1, typename _II2, typename _Compare>
1226  _GLIBCXX20_CONSTEXPR
1227  bool
1228  __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
1229  _II2 __first2, _II2 __last2,
1230  _Compare __comp)
1231  {
1232  typedef typename iterator_traits<_II1>::iterator_category _Category1;
1233  typedef typename iterator_traits<_II2>::iterator_category _Category2;
1234  typedef std::__lc_rai<_Category1, _Category2> __rai_type;
1235 
1236  __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1237  for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
1238  ++__first1, (void)++__first2)
1239  {
1240  if (__comp(__first1, __first2))
1241  return true;
1242  if (__comp(__first2, __first1))
1243  return false;
1244  }
1245  return __first1 == __last1 && __first2 != __last2;
1246  }
1247 
1248  template<bool _BoolType>
1249  struct __lexicographical_compare
1250  {
1251  template<typename _II1, typename _II2>
1252  _GLIBCXX20_CONSTEXPR
1253  static bool
1254  __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1255  {
1256  using __gnu_cxx::__ops::__iter_less_iter;
1257  return std::__lexicographical_compare_impl(__first1, __last1,
1258  __first2, __last2,
1259  __iter_less_iter());
1260  }
1261  };
1262 
1263  template<>
1264  struct __lexicographical_compare<true>
1265  {
1266  template<typename _Tp, typename _Up>
1267  _GLIBCXX20_CONSTEXPR
1268  static bool
1269  __lc(const _Tp* __first1, const _Tp* __last1,
1270  const _Up* __first2, const _Up* __last2)
1271  {
1272  const size_t __len1 = __last1 - __first1;
1273  const size_t __len2 = __last2 - __first2;
1274  if (const size_t __len = std::min(__len1, __len2))
1275  if (int __result = std::__memcmp(__first1, __first2, __len))
1276  return __result < 0;
1277  return __len1 < __len2;
1278  }
1279  };
1280 
1281  template<typename _II1, typename _II2>
1282  _GLIBCXX20_CONSTEXPR
1283  inline bool
1284  __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
1285  _II2 __first2, _II2 __last2)
1286  {
1287  typedef typename iterator_traits<_II1>::value_type _ValueType1;
1288  typedef typename iterator_traits<_II2>::value_type _ValueType2;
1289  const bool __simple =
1290  (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
1291  && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
1292  && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
1293  && __is_pointer<_II1>::__value
1294  && __is_pointer<_II2>::__value
1295 #if __cplusplus > 201703L && __cpp_lib_concepts
1296  // For C++20 iterator_traits<volatile T*>::value_type is non-volatile
1297  // so __is_byte<T> could be true, but we can't use memcmp with
1298  // volatile data.
1299  && !is_volatile_v<remove_reference_t<iter_reference_t<_II1>>>
1300  && !is_volatile_v<remove_reference_t<iter_reference_t<_II2>>>
1301 #endif
1302  );
1303 
1304  return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
1305  __first2, __last2);
1306  }
1307 
1308  template<typename _ForwardIterator, typename _Tp, typename _Compare>
1309  _GLIBCXX20_CONSTEXPR
1310  _ForwardIterator
1311  __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
1312  const _Tp& __val, _Compare __comp)
1313  {
1314  typedef typename iterator_traits<_ForwardIterator>::difference_type
1315  _DistanceType;
1316 
1317  _DistanceType __len = std::distance(__first, __last);
1318 
1319  while (__len > 0)
1320  {
1321  _DistanceType __half = __len >> 1;
1322  _ForwardIterator __middle = __first;
1323  std::advance(__middle, __half);
1324  if (__comp(__middle, __val))
1325  {
1326  __first = __middle;
1327  ++__first;
1328  __len = __len - __half - 1;
1329  }
1330  else
1331  __len = __half;
1332  }
1333  return __first;
1334  }
1335 
1336  /**
1337  * @brief Finds the first position in which @a val could be inserted
1338  * without changing the ordering.
1339  * @param __first An iterator.
1340  * @param __last Another iterator.
1341  * @param __val The search term.
1342  * @return An iterator pointing to the first element <em>not less
1343  * than</em> @a val, or end() if every element is less than
1344  * @a val.
1345  * @ingroup binary_search_algorithms
1346  */
1347  template<typename _ForwardIterator, typename _Tp>
1348  _GLIBCXX20_CONSTEXPR
1349  inline _ForwardIterator
1350  lower_bound(_ForwardIterator __first, _ForwardIterator __last,
1351  const _Tp& __val)
1352  {
1353  // concept requirements
1354  __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1355  __glibcxx_function_requires(_LessThanOpConcept<
1357  __glibcxx_requires_partitioned_lower(__first, __last, __val);
1358 
1359  return std::__lower_bound(__first, __last, __val,
1360  __gnu_cxx::__ops::__iter_less_val());
1361  }
1362 
1363  /// This is a helper function for the sort routines and for random.tcc.
1364  // Precondition: __n > 0.
1365  inline _GLIBCXX_CONSTEXPR int
1366  __lg(int __n)
1367  { return (int)sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1368 
1369  inline _GLIBCXX_CONSTEXPR unsigned
1370  __lg(unsigned __n)
1371  { return (int)sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1372 
1373  inline _GLIBCXX_CONSTEXPR long
1374  __lg(long __n)
1375  { return (int)sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1376 
1377  inline _GLIBCXX_CONSTEXPR unsigned long
1378  __lg(unsigned long __n)
1379  { return (int)sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1380 
1381  inline _GLIBCXX_CONSTEXPR long long
1382  __lg(long long __n)
1383  { return (int)sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1384 
1385  inline _GLIBCXX_CONSTEXPR unsigned long long
1386  __lg(unsigned long long __n)
1387  { return (int)sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1388 
1389 _GLIBCXX_BEGIN_NAMESPACE_ALGO
1390 
1391  /**
1392  * @brief Tests a range for element-wise equality.
1393  * @ingroup non_mutating_algorithms
1394  * @param __first1 An input iterator.
1395  * @param __last1 An input iterator.
1396  * @param __first2 An input iterator.
1397  * @return A boolean true or false.
1398  *
1399  * This compares the elements of two ranges using @c == and returns true or
1400  * false depending on whether all of the corresponding elements of the
1401  * ranges are equal.
1402  */
1403  template<typename _II1, typename _II2>
1404  _GLIBCXX20_CONSTEXPR
1405  inline bool
1406  equal(_II1 __first1, _II1 __last1, _II2 __first2)
1407  {
1408  // concept requirements
1409  __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1410  __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1411  __glibcxx_function_requires(_EqualOpConcept<
1414  __glibcxx_requires_can_increment_range(__first1, __last1, __first2);
1415 
1416  return std::__equal_aux(__first1, __last1, __first2);
1417  }
1418 
1419  /**
1420  * @brief Tests a range for element-wise equality.
1421  * @ingroup non_mutating_algorithms
1422  * @param __first1 An input iterator.
1423  * @param __last1 An input iterator.
1424  * @param __first2 An input iterator.
1425  * @param __binary_pred A binary predicate @link functors
1426  * functor@endlink.
1427  * @return A boolean true or false.
1428  *
1429  * This compares the elements of two ranges using the binary_pred
1430  * parameter, and returns true or
1431  * false depending on whether all of the corresponding elements of the
1432  * ranges are equal.
1433  */
1434  template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1435  _GLIBCXX20_CONSTEXPR
1436  inline bool
1437  equal(_IIter1 __first1, _IIter1 __last1,
1438  _IIter2 __first2, _BinaryPredicate __binary_pred)
1439  {
1440  // concept requirements
1441  __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1442  __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1443  __glibcxx_requires_valid_range(__first1, __last1);
1444 
1445  for (; __first1 != __last1; ++__first1, (void)++__first2)
1446  if (!bool(__binary_pred(*__first1, *__first2)))
1447  return false;
1448  return true;
1449  }
1450 
1451 #if __cplusplus >= 201103L
1452  // 4-iterator version of std::equal<It1, It2> for use in C++11.
1453  template<typename _II1, typename _II2>
1454  _GLIBCXX20_CONSTEXPR
1455  inline bool
1456  __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1457  {
1458  using _RATag = random_access_iterator_tag;
1459  using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1460  using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1461  using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1462  if (_RAIters())
1463  {
1464  auto __d1 = std::distance(__first1, __last1);
1465  auto __d2 = std::distance(__first2, __last2);
1466  if (__d1 != __d2)
1467  return false;
1468  return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
1469  }
1470 
1471  for (; __first1 != __last1 && __first2 != __last2;
1472  ++__first1, (void)++__first2)
1473  if (!(*__first1 == *__first2))
1474  return false;
1475  return __first1 == __last1 && __first2 == __last2;
1476  }
1477 
1478  // 4-iterator version of std::equal<It1, It2, BinaryPred> for use in C++11.
1479  template<typename _II1, typename _II2, typename _BinaryPredicate>
1480  _GLIBCXX20_CONSTEXPR
1481  inline bool
1482  __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2,
1483  _BinaryPredicate __binary_pred)
1484  {
1485  using _RATag = random_access_iterator_tag;
1486  using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1487  using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1488  using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1489  if (_RAIters())
1490  {
1491  auto __d1 = std::distance(__first1, __last1);
1492  auto __d2 = std::distance(__first2, __last2);
1493  if (__d1 != __d2)
1494  return false;
1495  return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
1496  __binary_pred);
1497  }
1498 
1499  for (; __first1 != __last1 && __first2 != __last2;
1500  ++__first1, (void)++__first2)
1501  if (!bool(__binary_pred(*__first1, *__first2)))
1502  return false;
1503  return __first1 == __last1 && __first2 == __last2;
1504  }
1505 #endif // C++11
1506 
1507 #if __cplusplus > 201103L
1508 
1509 #define __cpp_lib_robust_nonmodifying_seq_ops 201304
1510 
1511  /**
1512  * @brief Tests a range for element-wise equality.
1513  * @ingroup non_mutating_algorithms
1514  * @param __first1 An input iterator.
1515  * @param __last1 An input iterator.
1516  * @param __first2 An input iterator.
1517  * @param __last2 An input iterator.
1518  * @return A boolean true or false.
1519  *
1520  * This compares the elements of two ranges using @c == and returns true or
1521  * false depending on whether all of the corresponding elements of the
1522  * ranges are equal.
1523  */
1524  template<typename _II1, typename _II2>
1525  _GLIBCXX20_CONSTEXPR
1526  inline bool
1527  equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1528  {
1529  // concept requirements
1530  __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1531  __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1532  __glibcxx_function_requires(_EqualOpConcept<
1535  __glibcxx_requires_valid_range(__first1, __last1);
1536  __glibcxx_requires_valid_range(__first2, __last2);
1537 
1538  return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2);
1539  }
1540 
1541  /**
1542  * @brief Tests a range for element-wise equality.
1543  * @ingroup non_mutating_algorithms
1544  * @param __first1 An input iterator.
1545  * @param __last1 An input iterator.
1546  * @param __first2 An input iterator.
1547  * @param __last2 An input iterator.
1548  * @param __binary_pred A binary predicate @link functors
1549  * functor@endlink.
1550  * @return A boolean true or false.
1551  *
1552  * This compares the elements of two ranges using the binary_pred
1553  * parameter, and returns true or
1554  * false depending on whether all of the corresponding elements of the
1555  * ranges are equal.
1556  */
1557  template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1558  _GLIBCXX20_CONSTEXPR
1559  inline bool
1560  equal(_IIter1 __first1, _IIter1 __last1,
1561  _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
1562  {
1563  // concept requirements
1564  __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1565  __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1566  __glibcxx_requires_valid_range(__first1, __last1);
1567  __glibcxx_requires_valid_range(__first2, __last2);
1568 
1569  return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2,
1570  __binary_pred);
1571  }
1572 #endif // C++14
1573 
1574  /**
1575  * @brief Performs @b dictionary comparison on ranges.
1576  * @ingroup sorting_algorithms
1577  * @param __first1 An input iterator.
1578  * @param __last1 An input iterator.
1579  * @param __first2 An input iterator.
1580  * @param __last2 An input iterator.
1581  * @return A boolean true or false.
1582  *
1583  * <em>Returns true if the sequence of elements defined by the range
1584  * [first1,last1) is lexicographically less than the sequence of elements
1585  * defined by the range [first2,last2). Returns false otherwise.</em>
1586  * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1587  * then this is an inline call to @c memcmp.
1588  */
1589  template<typename _II1, typename _II2>
1590  _GLIBCXX20_CONSTEXPR
1591  inline bool
1592  lexicographical_compare(_II1 __first1, _II1 __last1,
1593  _II2 __first2, _II2 __last2)
1594  {
1595 #ifdef _GLIBCXX_CONCEPT_CHECKS
1596  // concept requirements
1597  typedef typename iterator_traits<_II1>::value_type _ValueType1;
1598  typedef typename iterator_traits<_II2>::value_type _ValueType2;
1599 #endif
1600  __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1601  __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1602  __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1603  __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1604  __glibcxx_requires_valid_range(__first1, __last1);
1605  __glibcxx_requires_valid_range(__first2, __last2);
1606 
1607  return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1608  std::__niter_base(__last1),
1609  std::__niter_base(__first2),
1610  std::__niter_base(__last2));
1611  }
1612 
1613  /**
1614  * @brief Performs @b dictionary comparison on ranges.
1615  * @ingroup sorting_algorithms
1616  * @param __first1 An input iterator.
1617  * @param __last1 An input iterator.
1618  * @param __first2 An input iterator.
1619  * @param __last2 An input iterator.
1620  * @param __comp A @link comparison_functors comparison functor@endlink.
1621  * @return A boolean true or false.
1622  *
1623  * The same as the four-parameter @c lexicographical_compare, but uses the
1624  * comp parameter instead of @c <.
1625  */
1626  template<typename _II1, typename _II2, typename _Compare>
1627  _GLIBCXX20_CONSTEXPR
1628  inline bool
1629  lexicographical_compare(_II1 __first1, _II1 __last1,
1630  _II2 __first2, _II2 __last2, _Compare __comp)
1631  {
1632  // concept requirements
1633  __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1634  __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1635  __glibcxx_requires_valid_range(__first1, __last1);
1636  __glibcxx_requires_valid_range(__first2, __last2);
1637 
1638  return std::__lexicographical_compare_impl
1639  (__first1, __last1, __first2, __last2,
1640  __gnu_cxx::__ops::__iter_comp_iter(__comp));
1641  }
1642 
1643 #if __cpp_lib_three_way_comparison
1644  // Iter points to a contiguous range of unsigned narrow character type
1645  // or std::byte, suitable for comparison by memcmp.
1646  template<typename _Iter>
1647  concept __is_byte_iter = contiguous_iterator<_Iter>
1648  && __is_byte<iter_value_t<_Iter>>::__value != 0
1649  && !__gnu_cxx::__numeric_traits<iter_value_t<_Iter>>::__is_signed;
1650 
1651  // Return a struct with two members, initialized to the smaller of x and y
1652  // (or x if they compare equal) and the result of the comparison x <=> y.
1653  template<typename _Tp>
1654  constexpr auto
1655  __min_cmp(_Tp __x, _Tp __y)
1656  {
1657  struct _Res {
1658  _Tp _M_min;
1659  decltype(__x <=> __y) _M_cmp;
1660  };
1661  auto __c = __x <=> __y;
1662  if (__c > 0)
1663  return _Res{__y, __c};
1664  return _Res{__x, __c};
1665  }
1666 
1667  /**
1668  * @brief Performs dictionary comparison on ranges.
1669  * @ingroup sorting_algorithms
1670  * @param __first1 An input iterator.
1671  * @param __last1 An input iterator.
1672  * @param __first2 An input iterator.
1673  * @param __last2 An input iterator.
1674  * @param __comp A @link comparison_functors comparison functor@endlink.
1675  * @return The comparison category that `__comp(*__first1, *__first2)`
1676  * returns.
1677  */
1678  template<typename _InputIter1, typename _InputIter2, typename _Comp>
1679  constexpr auto
1680  lexicographical_compare_three_way(_InputIter1 __first1,
1681  _InputIter1 __last1,
1682  _InputIter2 __first2,
1683  _InputIter2 __last2,
1684  _Comp __comp)
1685  -> decltype(__comp(*__first1, *__first2))
1686  {
1687  // concept requirements
1688  __glibcxx_function_requires(_InputIteratorConcept<_InputIter1>)
1689  __glibcxx_function_requires(_InputIteratorConcept<_InputIter2>)
1690  __glibcxx_requires_valid_range(__first1, __last1);
1691  __glibcxx_requires_valid_range(__first2, __last2);
1692 
1693 #if __cpp_lib_is_constant_evaluated
1694  using _Cat = decltype(__comp(*__first1, *__first2));
1695  static_assert(same_as<common_comparison_category_t<_Cat>, _Cat>);
1696 
1697  if (!std::is_constant_evaluated())
1698  if constexpr (same_as<_Comp, __detail::_Synth3way>
1699  || same_as<_Comp, compare_three_way>)
1700  if constexpr (__is_byte_iter<_InputIter1>)
1701  if constexpr (__is_byte_iter<_InputIter2>)
1702  {
1703  const auto [__len, __lencmp]
1704  = std::__min_cmp(__last1 - __first1, __last2 - __first2);
1705  if (__len)
1706  {
1707  const auto __c
1708  = __builtin_memcmp(&*__first1, &*__first2, __len) <=> 0;
1709  if (__c != 0)
1710  return __c;
1711  }
1712  return __lencmp;
1713  }
1714 #endif // is_constant_evaluated
1715  while (__first1 != __last1)
1716  {
1717  if (__first2 == __last2)
1718  return strong_ordering::greater;
1719  if (auto __cmp = __comp(*__first1, *__first2); __cmp != 0)
1720  return __cmp;
1721  ++__first1;
1722  ++__first2;
1723  }
1724  return (__first2 == __last2) <=> true; // See PR 94006
1725  }
1726 
1727  template<typename _InputIter1, typename _InputIter2>
1728  constexpr auto
1729  lexicographical_compare_three_way(_InputIter1 __first1,
1730  _InputIter1 __last1,
1731  _InputIter2 __first2,
1732  _InputIter2 __last2)
1733  {
1734  return std::lexicographical_compare_three_way(__first1, __last1,
1735  __first2, __last2,
1736  compare_three_way{});
1737  }
1738 #endif // three_way_comparison
1739 
1740  template<typename _InputIterator1, typename _InputIterator2,
1741  typename _BinaryPredicate>
1742  _GLIBCXX20_CONSTEXPR
1743  pair<_InputIterator1, _InputIterator2>
1744  __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1745  _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1746  {
1747  while (__first1 != __last1 && __binary_pred(__first1, __first2))
1748  {
1749  ++__first1;
1750  ++__first2;
1751  }
1752  return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1753  }
1754 
1755  /**
1756  * @brief Finds the places in ranges which don't match.
1757  * @ingroup non_mutating_algorithms
1758  * @param __first1 An input iterator.
1759  * @param __last1 An input iterator.
1760  * @param __first2 An input iterator.
1761  * @return A pair of iterators pointing to the first mismatch.
1762  *
1763  * This compares the elements of two ranges using @c == and returns a pair
1764  * of iterators. The first iterator points into the first range, the
1765  * second iterator points into the second range, and the elements pointed
1766  * to by the iterators are not equal.
1767  */
1768  template<typename _InputIterator1, typename _InputIterator2>
1769  _GLIBCXX20_CONSTEXPR
1770  inline pair<_InputIterator1, _InputIterator2>
1771  mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1772  _InputIterator2 __first2)
1773  {
1774  // concept requirements
1775  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1776  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1777  __glibcxx_function_requires(_EqualOpConcept<
1780  __glibcxx_requires_valid_range(__first1, __last1);
1781 
1782  return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1783  __gnu_cxx::__ops::__iter_equal_to_iter());
1784  }
1785 
1786  /**
1787  * @brief Finds the places in ranges which don't match.
1788  * @ingroup non_mutating_algorithms
1789  * @param __first1 An input iterator.
1790  * @param __last1 An input iterator.
1791  * @param __first2 An input iterator.
1792  * @param __binary_pred A binary predicate @link functors
1793  * functor@endlink.
1794  * @return A pair of iterators pointing to the first mismatch.
1795  *
1796  * This compares the elements of two ranges using the binary_pred
1797  * parameter, and returns a pair
1798  * of iterators. The first iterator points into the first range, the
1799  * second iterator points into the second range, and the elements pointed
1800  * to by the iterators are not equal.
1801  */
1802  template<typename _InputIterator1, typename _InputIterator2,
1803  typename _BinaryPredicate>
1804  _GLIBCXX20_CONSTEXPR
1805  inline pair<_InputIterator1, _InputIterator2>
1806  mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1807  _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1808  {
1809  // concept requirements
1810  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1811  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1812  __glibcxx_requires_valid_range(__first1, __last1);
1813 
1814  return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1815  __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1816  }
1817 
1818 #if __cplusplus > 201103L
1819 
1820  template<typename _InputIterator1, typename _InputIterator2,
1821  typename _BinaryPredicate>
1822  _GLIBCXX20_CONSTEXPR
1823  pair<_InputIterator1, _InputIterator2>
1824  __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1825  _InputIterator2 __first2, _InputIterator2 __last2,
1826  _BinaryPredicate __binary_pred)
1827  {
1828  while (__first1 != __last1 && __first2 != __last2
1829  && __binary_pred(__first1, __first2))
1830  {
1831  ++__first1;
1832  ++__first2;
1833  }
1834  return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1835  }
1836 
1837  /**
1838  * @brief Finds the places in ranges which don't match.
1839  * @ingroup non_mutating_algorithms
1840  * @param __first1 An input iterator.
1841  * @param __last1 An input iterator.
1842  * @param __first2 An input iterator.
1843  * @param __last2 An input iterator.
1844  * @return A pair of iterators pointing to the first mismatch.
1845  *
1846  * This compares the elements of two ranges using @c == and returns a pair
1847  * of iterators. The first iterator points into the first range, the
1848  * second iterator points into the second range, and the elements pointed
1849  * to by the iterators are not equal.
1850  */
1851  template<typename _InputIterator1, typename _InputIterator2>
1852  _GLIBCXX20_CONSTEXPR
1853  inline pair<_InputIterator1, _InputIterator2>
1854  mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1855  _InputIterator2 __first2, _InputIterator2 __last2)
1856  {
1857  // concept requirements
1858  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1859  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1860  __glibcxx_function_requires(_EqualOpConcept<
1863  __glibcxx_requires_valid_range(__first1, __last1);
1864  __glibcxx_requires_valid_range(__first2, __last2);
1865 
1866  return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1867  __gnu_cxx::__ops::__iter_equal_to_iter());
1868  }
1869 
1870  /**
1871  * @brief Finds the places in ranges which don't match.
1872  * @ingroup non_mutating_algorithms
1873  * @param __first1 An input iterator.
1874  * @param __last1 An input iterator.
1875  * @param __first2 An input iterator.
1876  * @param __last2 An input iterator.
1877  * @param __binary_pred A binary predicate @link functors
1878  * functor@endlink.
1879  * @return A pair of iterators pointing to the first mismatch.
1880  *
1881  * This compares the elements of two ranges using the binary_pred
1882  * parameter, and returns a pair
1883  * of iterators. The first iterator points into the first range, the
1884  * second iterator points into the second range, and the elements pointed
1885  * to by the iterators are not equal.
1886  */
1887  template<typename _InputIterator1, typename _InputIterator2,
1888  typename _BinaryPredicate>
1889  _GLIBCXX20_CONSTEXPR
1890  inline pair<_InputIterator1, _InputIterator2>
1891  mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1892  _InputIterator2 __first2, _InputIterator2 __last2,
1893  _BinaryPredicate __binary_pred)
1894  {
1895  // concept requirements
1896  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1897  __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1898  __glibcxx_requires_valid_range(__first1, __last1);
1899  __glibcxx_requires_valid_range(__first2, __last2);
1900 
1901  return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1902  __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1903  }
1904 #endif
1905 
1906 _GLIBCXX_END_NAMESPACE_ALGO
1907 
1908  /// This is an overload used by find algos for the Input Iterator case.
1909  template<typename _InputIterator, typename _Predicate>
1910  _GLIBCXX20_CONSTEXPR
1911  inline _InputIterator
1912  __find_if(_InputIterator __first, _InputIterator __last,
1913  _Predicate __pred, input_iterator_tag)
1914  {
1915  while (__first != __last && !__pred(__first))
1916  ++__first;
1917  return __first;
1918  }
1919 
1920  /// This is an overload used by find algos for the RAI case.
1921  template<typename _RandomAccessIterator, typename _Predicate>
1922  _GLIBCXX20_CONSTEXPR
1923  _RandomAccessIterator
1924  __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
1925  _Predicate __pred, random_access_iterator_tag)
1926  {
1928  __trip_count = (__last - __first) >> 2;
1929 
1930  for (; __trip_count > 0; --__trip_count)
1931  {
1932  if (__pred(__first))
1933  return __first;
1934  ++__first;
1935 
1936  if (__pred(__first))
1937  return __first;
1938  ++__first;
1939 
1940  if (__pred(__first))
1941  return __first;
1942  ++__first;
1943 
1944  if (__pred(__first))
1945  return __first;
1946  ++__first;
1947  }
1948 
1949  switch (__last - __first)
1950  {
1951  case 3:
1952  if (__pred(__first))
1953  return __first;
1954  ++__first;
1955  // FALLTHRU
1956  case 2:
1957  if (__pred(__first))
1958  return __first;
1959  ++__first;
1960  // FALLTHRU
1961  case 1:
1962  if (__pred(__first))
1963  return __first;
1964  ++__first;
1965  // FALLTHRU
1966  case 0:
1967  default:
1968  return __last;
1969  }
1970  }
1971 
1972  template<typename _Iterator, typename _Predicate>
1973  _GLIBCXX20_CONSTEXPR
1974  inline _Iterator
1975  __find_if(_Iterator __first, _Iterator __last, _Predicate __pred)
1976  {
1977  return __find_if(__first, __last, __pred,
1978  std::__iterator_category(__first));
1979  }
1980 
1981  template<typename _InputIterator, typename _Predicate>
1982  _GLIBCXX20_CONSTEXPR
1983  typename iterator_traits<_InputIterator>::difference_type
1984  __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1985  {
1986  typename iterator_traits<_InputIterator>::difference_type __n = 0;
1987  for (; __first != __last; ++__first)
1988  if (__pred(__first))
1989  ++__n;
1990  return __n;
1991  }
1992 
1993 #if __cplusplus >= 201103L
1994  template<typename _ForwardIterator1, typename _ForwardIterator2,
1995  typename _BinaryPredicate>
1996  _GLIBCXX20_CONSTEXPR
1997  bool
1998  __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
1999  _ForwardIterator2 __first2, _BinaryPredicate __pred)
2000  {
2001  // Efficiently compare identical prefixes: O(N) if sequences
2002  // have the same elements in the same order.
2003  for (; __first1 != __last1; ++__first1, (void)++__first2)
2004  if (!__pred(__first1, __first2))
2005  break;
2006 
2007  if (__first1 == __last1)
2008  return true;
2009 
2010  // Establish __last2 assuming equal ranges by iterating over the
2011  // rest of the list.
2012  _ForwardIterator2 __last2 = __first2;
2013  std::advance(__last2, std::distance(__first1, __last1));
2014  for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
2015  {
2016  if (__scan != std::__find_if(__first1, __scan,
2017  __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
2018  continue; // We've seen this one before.
2019 
2020  auto __matches
2021  = std::__count_if(__first2, __last2,
2022  __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
2023  if (0 == __matches ||
2024  std::__count_if(__scan, __last1,
2025  __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
2026  != __matches)
2027  return false;
2028  }
2029  return true;
2030  }
2031 
2032  /**
2033  * @brief Checks whether a permutation of the second sequence is equal
2034  * to the first sequence.
2035  * @ingroup non_mutating_algorithms
2036  * @param __first1 Start of first range.
2037  * @param __last1 End of first range.
2038  * @param __first2 Start of second range.
2039  * @return true if there exists a permutation of the elements in the range
2040  * [__first2, __first2 + (__last1 - __first1)), beginning with
2041  * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
2042  * returns true; otherwise, returns false.
2043  */
2044  template<typename _ForwardIterator1, typename _ForwardIterator2>
2045  _GLIBCXX20_CONSTEXPR
2046  inline bool
2047  is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2048  _ForwardIterator2 __first2)
2049  {
2050  // concept requirements
2051  __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
2052  __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
2053  __glibcxx_function_requires(_EqualOpConcept<
2056  __glibcxx_requires_valid_range(__first1, __last1);
2057 
2058  return std::__is_permutation(__first1, __last1, __first2,
2059  __gnu_cxx::__ops::__iter_equal_to_iter());
2060  }
2061 #endif // C++11
2062 
2063 _GLIBCXX_END_NAMESPACE_VERSION
2064 } // namespace std
2065 
2066 // NB: This file is included within many other C++ includes, as a way
2067 // of getting the base algorithms. So, make sure that parallel bits
2068 // come in too if requested.
2069 #ifdef _GLIBCXX_PARALLEL
2070 # include <parallel/algobase.h>
2071 #endif
2072 
2073 #endif
is_nothrow_copy_constructible
Definition: type_traits:1039
Safe iterator wrapper.
Definition: debug.h:61
constexpr void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
constexpr void iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
Swaps the contents of two iterators.
Definition: stl_algobase.h:152
constexpr const _Tp & min(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:230
constexpr _BI2 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
Moves the range [first,last) into result.
Definition: stl_algobase.h:833
constexpr const _Tp & max(const _Tp &, const _Tp &)
This does what you think it does.
Definition: stl_algobase.h:254
A deque::iterator.
Definition: stl_algobase.h:478
constexpr iterator_traits< _Iter >::iterator_category __iterator_category(const _Iter &)
Marking output iterators.
constexpr std::remove_reference< _Tp >::type && move(_Tp &&__t) noexcept
Convert a value to an rvalue.
Definition: move.h:101
constexpr iterator_traits< _InputIterator >::difference_type distance(_InputIterator __first, _InputIterator __last)
A generalization of pointer arithmetic.
constexpr _InputIterator __find_if(_InputIterator __first, _InputIterator __last, _Predicate __pred, input_iterator_tag)
This is an overload used by find algos for the Input Iterator case.
Parallel STL function calls corresponding to the stl_algobase.h header. The functions defined here ma...
Random-access iterators support a superset of bidirectional iterator operations.
Marking input iterators.
Traits class for iterators.
constexpr int __lg(int __n)
This is a helper function for the sort routines and for random.tcc.