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