]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_algobase.h
libstdc++ std::fill overload for std::vector<bool>::iterator
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_algobase.h
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>
65 #include <bits/stl_iterator_base_types.h>
66 #include <bits/stl_iterator_base_funcs.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
161 typedef typename iterator_traits<_ForwardIterator1>::value_type
162 _ValueType1;
163 typedef typename iterator_traits<_ForwardIterator2>::value_type
164 _ValueType2;
165
166 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
167 _ValueType2>)
168 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
169 _ValueType1>)
170
171 typedef typename iterator_traits<_ForwardIterator1>::reference
172 _ReferenceType1;
173 typedef typename iterator_traits<_ForwardIterator2>::reference
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)
314 _GLIBCXX_NOEXCEPT_IF(std::is_nothrow_copy_constructible<_Iterator>::value)
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 _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
432
433 template<typename _Tp, typename _Ref, typename _Ptr>
434 struct _Deque_iterator;
435
436 struct _Bit_iterator;
437
438 _GLIBCXX_END_NAMESPACE_CONTAINER
439
440 // Helpers for streambuf iterators (either istream or ostream).
441 // NB: avoid including <iosfwd>, relatively large.
442 template<typename _CharT>
443 struct char_traits;
444
445 template<typename _CharT, typename _Traits>
446 class istreambuf_iterator;
447
448 template<typename _CharT, typename _Traits>
449 class ostreambuf_iterator;
450
451 template<bool _IsMove, typename _CharT>
452 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
453 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
454 __copy_move_a2(_CharT*, _CharT*,
455 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
456
457 template<bool _IsMove, typename _CharT>
458 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
459 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
460 __copy_move_a2(const _CharT*, const _CharT*,
461 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
462
463 template<bool _IsMove, typename _CharT>
464 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
465 _CharT*>::__type
466 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
467 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
468
469 template<bool _IsMove, typename _II, typename _OI>
470 _GLIBCXX20_CONSTEXPR
471 inline _OI
472 __copy_move_a2(_II __first, _II __last, _OI __result)
473 {
474 typedef typename iterator_traits<_II>::iterator_category _Category;
475 #ifdef __cpp_lib_is_constant_evaluated
476 if (std::is_constant_evaluated())
477 return std::__copy_move<_IsMove, false, _Category>::
478 __copy_m(__first, __last, __result);
479 #endif
480 return std::__copy_move<_IsMove, __memcpyable<_OI, _II>::__value,
481 _Category>::__copy_m(__first, __last, __result);
482 }
483
484 template<bool _IsMove,
485 typename _Tp, typename _Ref, typename _Ptr, typename _OI>
486 _OI
487 __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
488 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
489 _OI);
490
491 template<bool _IsMove,
492 typename _ITp, typename _IRef, typename _IPtr, typename _OTp>
493 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
494 __copy_move_a1(_GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
495 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
496 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>);
497
498 template<bool _IsMove, typename _II, typename _Tp>
499 typename __gnu_cxx::__enable_if<
500 __is_random_access_iter<_II>::__value,
501 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> >::__type
502 __copy_move_a1(_II, _II, _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>);
503
504 template<bool _IsMove, typename _II, typename _OI>
505 _GLIBCXX20_CONSTEXPR
506 inline _OI
507 __copy_move_a1(_II __first, _II __last, _OI __result)
508 { return std::__copy_move_a2<_IsMove>(__first, __last, __result); }
509
510 template<bool _IsMove, typename _II, typename _OI>
511 _GLIBCXX20_CONSTEXPR
512 inline _OI
513 __copy_move_a(_II __first, _II __last, _OI __result)
514 {
515 return std::__niter_wrap(__result,
516 std::__copy_move_a1<_IsMove>(std::__niter_base(__first),
517 std::__niter_base(__last),
518 std::__niter_base(__result)));
519 }
520
521 template<bool _IsMove,
522 typename _Ite, typename _Seq, typename _Cat, typename _OI>
523 _OI
524 __copy_move_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
525 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
526 _OI);
527
528 template<bool _IsMove,
529 typename _II, typename _Ite, typename _Seq, typename _Cat>
530 __gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>
531 __copy_move_a(_II, _II,
532 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&);
533
534 template<bool _IsMove,
535 typename _IIte, typename _ISeq, typename _ICat,
536 typename _OIte, typename _OSeq, typename _OCat>
537 ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>
538 __copy_move_a(const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
539 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
540 const ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>&);
541
542 /**
543 * @brief Copies the range [first,last) into result.
544 * @ingroup mutating_algorithms
545 * @param __first An input iterator.
546 * @param __last An input iterator.
547 * @param __result An output iterator.
548 * @return result + (first - last)
549 *
550 * This inline function will boil down to a call to @c memmove whenever
551 * possible. Failing that, if random access iterators are passed, then the
552 * loop count will be known (and therefore a candidate for compiler
553 * optimizations such as unrolling). Result may not be contained within
554 * [first,last); the copy_backward function should be used instead.
555 *
556 * Note that the end of the output range is permitted to be contained
557 * within [first,last).
558 */
559 template<typename _II, typename _OI>
560 _GLIBCXX20_CONSTEXPR
561 inline _OI
562 copy(_II __first, _II __last, _OI __result)
563 {
564 // concept requirements
565 __glibcxx_function_requires(_InputIteratorConcept<_II>)
566 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
567 typename iterator_traits<_II>::value_type>)
568 __glibcxx_requires_can_increment_range(__first, __last, __result);
569
570 return std::__copy_move_a<__is_move_iterator<_II>::__value>
571 (std::__miter_base(__first), std::__miter_base(__last), __result);
572 }
573
574 #if __cplusplus >= 201103L
575 /**
576 * @brief Moves the range [first,last) into result.
577 * @ingroup mutating_algorithms
578 * @param __first An input iterator.
579 * @param __last An input iterator.
580 * @param __result An output iterator.
581 * @return result + (first - last)
582 *
583 * This inline function will boil down to a call to @c memmove whenever
584 * possible. Failing that, if random access iterators are passed, then the
585 * loop count will be known (and therefore a candidate for compiler
586 * optimizations such as unrolling). Result may not be contained within
587 * [first,last); the move_backward function should be used instead.
588 *
589 * Note that the end of the output range is permitted to be contained
590 * within [first,last).
591 */
592 template<typename _II, typename _OI>
593 _GLIBCXX20_CONSTEXPR
594 inline _OI
595 move(_II __first, _II __last, _OI __result)
596 {
597 // concept requirements
598 __glibcxx_function_requires(_InputIteratorConcept<_II>)
599 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
600 typename iterator_traits<_II>::value_type>)
601 __glibcxx_requires_can_increment_range(__first, __last, __result);
602
603 return std::__copy_move_a<true>(std::__miter_base(__first),
604 std::__miter_base(__last), __result);
605 }
606
607 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
608 #else
609 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
610 #endif
611
612 template<bool _IsMove, bool _IsSimple, typename _Category>
613 struct __copy_move_backward
614 {
615 template<typename _BI1, typename _BI2>
616 _GLIBCXX20_CONSTEXPR
617 static _BI2
618 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
619 {
620 while (__first != __last)
621 *--__result = *--__last;
622 return __result;
623 }
624 };
625
626 #if __cplusplus >= 201103L
627 template<typename _Category>
628 struct __copy_move_backward<true, false, _Category>
629 {
630 template<typename _BI1, typename _BI2>
631 _GLIBCXX20_CONSTEXPR
632 static _BI2
633 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
634 {
635 while (__first != __last)
636 *--__result = std::move(*--__last);
637 return __result;
638 }
639 };
640 #endif
641
642 template<>
643 struct __copy_move_backward<false, false, random_access_iterator_tag>
644 {
645 template<typename _BI1, typename _BI2>
646 _GLIBCXX20_CONSTEXPR
647 static _BI2
648 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
649 {
650 typename iterator_traits<_BI1>::difference_type
651 __n = __last - __first;
652 for (; __n > 0; --__n)
653 *--__result = *--__last;
654 return __result;
655 }
656 };
657
658 #if __cplusplus >= 201103L
659 template<>
660 struct __copy_move_backward<true, false, random_access_iterator_tag>
661 {
662 template<typename _BI1, typename _BI2>
663 _GLIBCXX20_CONSTEXPR
664 static _BI2
665 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
666 {
667 typename iterator_traits<_BI1>::difference_type
668 __n = __last - __first;
669 for (; __n > 0; --__n)
670 *--__result = std::move(*--__last);
671 return __result;
672 }
673 };
674 #endif
675
676 template<bool _IsMove>
677 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
678 {
679 template<typename _Tp>
680 _GLIBCXX20_CONSTEXPR
681 static _Tp*
682 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
683 {
684 #if __cplusplus >= 201103L
685 using __assignable = conditional<_IsMove,
686 is_move_assignable<_Tp>,
687 is_copy_assignable<_Tp>>;
688 // trivial types can have deleted assignment
689 static_assert( __assignable::type::value, "type is not assignable" );
690 #endif
691 const ptrdiff_t _Num = __last - __first;
692 if (_Num)
693 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
694 return __result - _Num;
695 }
696 };
697
698 template<bool _IsMove, typename _BI1, typename _BI2>
699 _GLIBCXX20_CONSTEXPR
700 inline _BI2
701 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
702 {
703 typedef typename iterator_traits<_BI1>::iterator_category _Category;
704 #ifdef __cpp_lib_is_constant_evaluated
705 if (std::is_constant_evaluated())
706 return std::__copy_move_backward<_IsMove, false, _Category>::
707 __copy_move_b(__first, __last, __result);
708 #endif
709 return std::__copy_move_backward<_IsMove,
710 __memcpyable<_BI2, _BI1>::__value,
711 _Category>::__copy_move_b(__first,
712 __last,
713 __result);
714 }
715
716 template<bool _IsMove, typename _BI1, typename _BI2>
717 _GLIBCXX20_CONSTEXPR
718 inline _BI2
719 __copy_move_backward_a1(_BI1 __first, _BI1 __last, _BI2 __result)
720 { return std::__copy_move_backward_a2<_IsMove>(__first, __last, __result); }
721
722 template<bool _IsMove,
723 typename _Tp, typename _Ref, typename _Ptr, typename _OI>
724 _OI
725 __copy_move_backward_a1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
726 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
727 _OI);
728
729 template<bool _IsMove,
730 typename _ITp, typename _IRef, typename _IPtr, typename _OTp>
731 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>
732 __copy_move_backward_a1(
733 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
734 _GLIBCXX_STD_C::_Deque_iterator<_ITp, _IRef, _IPtr>,
735 _GLIBCXX_STD_C::_Deque_iterator<_OTp, _OTp&, _OTp*>);
736
737 template<bool _IsMove, typename _II, typename _Tp>
738 typename __gnu_cxx::__enable_if<
739 __is_random_access_iter<_II>::__value,
740 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*> >::__type
741 __copy_move_backward_a1(_II, _II,
742 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>);
743
744 template<bool _IsMove, typename _II, typename _OI>
745 _GLIBCXX20_CONSTEXPR
746 inline _OI
747 __copy_move_backward_a(_II __first, _II __last, _OI __result)
748 {
749 return std::__niter_wrap(__result,
750 std::__copy_move_backward_a1<_IsMove>
751 (std::__niter_base(__first), std::__niter_base(__last),
752 std::__niter_base(__result)));
753 }
754
755 template<bool _IsMove,
756 typename _Ite, typename _Seq, typename _Cat, typename _OI>
757 _OI
758 __copy_move_backward_a(
759 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
760 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
761 _OI);
762
763 template<bool _IsMove,
764 typename _II, typename _Ite, typename _Seq, typename _Cat>
765 __gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>
766 __copy_move_backward_a(_II, _II,
767 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&);
768
769 template<bool _IsMove,
770 typename _IIte, typename _ISeq, typename _ICat,
771 typename _OIte, typename _OSeq, typename _OCat>
772 ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>
773 __copy_move_backward_a(
774 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
775 const ::__gnu_debug::_Safe_iterator<_IIte, _ISeq, _ICat>&,
776 const ::__gnu_debug::_Safe_iterator<_OIte, _OSeq, _OCat>&);
777
778 /**
779 * @brief Copies the range [first,last) into result.
780 * @ingroup mutating_algorithms
781 * @param __first A bidirectional iterator.
782 * @param __last A bidirectional iterator.
783 * @param __result A bidirectional iterator.
784 * @return result - (first - last)
785 *
786 * The function has the same effect as copy, but starts at the end of the
787 * range and works its way to the start, returning the start of the result.
788 * This inline function will boil down to a call to @c memmove whenever
789 * possible. Failing that, if random access iterators are passed, then the
790 * loop count will be known (and therefore a candidate for compiler
791 * optimizations such as unrolling).
792 *
793 * Result may not be in the range (first,last]. Use copy instead. Note
794 * that the start of the output range may overlap [first,last).
795 */
796 template<typename _BI1, typename _BI2>
797 _GLIBCXX20_CONSTEXPR
798 inline _BI2
799 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
800 {
801 // concept requirements
802 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
803 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
804 __glibcxx_function_requires(_ConvertibleConcept<
805 typename iterator_traits<_BI1>::value_type,
806 typename iterator_traits<_BI2>::value_type>)
807 __glibcxx_requires_can_decrement_range(__first, __last, __result);
808
809 return std::__copy_move_backward_a<__is_move_iterator<_BI1>::__value>
810 (std::__miter_base(__first), std::__miter_base(__last), __result);
811 }
812
813 #if __cplusplus >= 201103L
814 /**
815 * @brief Moves the range [first,last) into result.
816 * @ingroup mutating_algorithms
817 * @param __first A bidirectional iterator.
818 * @param __last A bidirectional iterator.
819 * @param __result A bidirectional iterator.
820 * @return result - (first - last)
821 *
822 * The function has the same effect as move, but starts at the end of the
823 * range and works its way to the start, returning the start of the result.
824 * This inline function will boil down to a call to @c memmove whenever
825 * possible. Failing that, if random access iterators are passed, then the
826 * loop count will be known (and therefore a candidate for compiler
827 * optimizations such as unrolling).
828 *
829 * Result may not be in the range (first,last]. Use move instead. Note
830 * that the start of the output range may overlap [first,last).
831 */
832 template<typename _BI1, typename _BI2>
833 _GLIBCXX20_CONSTEXPR
834 inline _BI2
835 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
836 {
837 // concept requirements
838 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
839 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
840 __glibcxx_function_requires(_ConvertibleConcept<
841 typename iterator_traits<_BI1>::value_type,
842 typename iterator_traits<_BI2>::value_type>)
843 __glibcxx_requires_can_decrement_range(__first, __last, __result);
844
845 return std::__copy_move_backward_a<true>(std::__miter_base(__first),
846 std::__miter_base(__last),
847 __result);
848 }
849
850 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
851 #else
852 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
853 #endif
854
855 template<typename _ForwardIterator, typename _Tp>
856 _GLIBCXX20_CONSTEXPR
857 inline typename
858 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
859 __fill_a1(_ForwardIterator __first, _ForwardIterator __last,
860 const _Tp& __value)
861 {
862 for (; __first != __last; ++__first)
863 *__first = __value;
864 }
865
866 template<typename _ForwardIterator, typename _Tp>
867 _GLIBCXX20_CONSTEXPR
868 inline typename
869 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
870 __fill_a1(_ForwardIterator __first, _ForwardIterator __last,
871 const _Tp& __value)
872 {
873 const _Tp __tmp = __value;
874 for (; __first != __last; ++__first)
875 *__first = __tmp;
876 }
877
878 // Specialization: for char types we can use memset.
879 template<typename _Tp>
880 _GLIBCXX20_CONSTEXPR
881 inline typename
882 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
883 __fill_a1(_Tp* __first, _Tp* __last, const _Tp& __c)
884 {
885 const _Tp __tmp = __c;
886 #if __cpp_lib_is_constant_evaluated
887 if (std::is_constant_evaluated())
888 {
889 for (; __first != __last; ++__first)
890 *__first = __tmp;
891 return;
892 }
893 #endif
894 if (const size_t __len = __last - __first)
895 __builtin_memset(__first, static_cast<unsigned char>(__tmp), __len);
896 }
897
898 template<typename _Ite, typename _Cont, typename _Tp>
899 _GLIBCXX20_CONSTEXPR
900 inline void
901 __fill_a1(::__gnu_cxx::__normal_iterator<_Ite, _Cont> __first,
902 ::__gnu_cxx::__normal_iterator<_Ite, _Cont> __last,
903 const _Tp& __value)
904 { std::__fill_a1(__first.base(), __last.base(), __value); }
905
906 template<typename _Tp, typename _VTp>
907 void
908 __fill_a1(const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
909 const _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Tp&, _Tp*>&,
910 const _VTp&);
911
912 void
913 __fill_a1(_GLIBCXX_STD_C::_Bit_iterator, _GLIBCXX_STD_C::_Bit_iterator,
914 const bool&);
915
916 template<typename _FIte, typename _Tp>
917 _GLIBCXX20_CONSTEXPR
918 inline void
919 __fill_a(_FIte __first, _FIte __last, const _Tp& __value)
920 { std::__fill_a1(__first, __last, __value); }
921
922 template<typename _Ite, typename _Seq, typename _Cat, typename _Tp>
923 void
924 __fill_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
925 const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>&,
926 const _Tp&);
927
928 /**
929 * @brief Fills the range [first,last) with copies of value.
930 * @ingroup mutating_algorithms
931 * @param __first A forward iterator.
932 * @param __last A forward iterator.
933 * @param __value A reference-to-const of arbitrary type.
934 * @return Nothing.
935 *
936 * This function fills a range with copies of the same value. For char
937 * types filling contiguous areas of memory, this becomes an inline call
938 * to @c memset or @c wmemset.
939 */
940 template<typename _ForwardIterator, typename _Tp>
941 _GLIBCXX20_CONSTEXPR
942 inline void
943 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
944 {
945 // concept requirements
946 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
947 _ForwardIterator>)
948 __glibcxx_requires_valid_range(__first, __last);
949
950 std::__fill_a(__first, __last, __value);
951 }
952
953 // Used by fill_n, generate_n, etc. to convert _Size to an integral type:
954 inline _GLIBCXX_CONSTEXPR int
955 __size_to_integer(int __n) { return __n; }
956 inline _GLIBCXX_CONSTEXPR unsigned
957 __size_to_integer(unsigned __n) { return __n; }
958 inline _GLIBCXX_CONSTEXPR long
959 __size_to_integer(long __n) { return __n; }
960 inline _GLIBCXX_CONSTEXPR unsigned long
961 __size_to_integer(unsigned long __n) { return __n; }
962 inline _GLIBCXX_CONSTEXPR long long
963 __size_to_integer(long long __n) { return __n; }
964 inline _GLIBCXX_CONSTEXPR unsigned long long
965 __size_to_integer(unsigned long long __n) { return __n; }
966
967 #if defined(__GLIBCXX_TYPE_INT_N_0)
968 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_0
969 __size_to_integer(__GLIBCXX_TYPE_INT_N_0 __n) { return __n; }
970 inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_0
971 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_0 __n) { return __n; }
972 #endif
973 #if defined(__GLIBCXX_TYPE_INT_N_1)
974 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_1
975 __size_to_integer(__GLIBCXX_TYPE_INT_N_1 __n) { return __n; }
976 inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_1
977 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_1 __n) { return __n; }
978 #endif
979 #if defined(__GLIBCXX_TYPE_INT_N_2)
980 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_2
981 __size_to_integer(__GLIBCXX_TYPE_INT_N_2 __n) { return __n; }
982 inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_2
983 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_2 __n) { return __n; }
984 #endif
985 #if defined(__GLIBCXX_TYPE_INT_N_3)
986 inline _GLIBCXX_CONSTEXPR unsigned __GLIBCXX_TYPE_INT_N_3
987 __size_to_integer(__GLIBCXX_TYPE_INT_N_3 __n) { return __n; }
988 inline _GLIBCXX_CONSTEXPR __GLIBCXX_TYPE_INT_N_3
989 __size_to_integer(unsigned __GLIBCXX_TYPE_INT_N_3 __n) { return __n; }
990 #endif
991
992 inline _GLIBCXX_CONSTEXPR long long
993 __size_to_integer(float __n) { return __n; }
994 inline _GLIBCXX_CONSTEXPR long long
995 __size_to_integer(double __n) { return __n; }
996 inline _GLIBCXX_CONSTEXPR long long
997 __size_to_integer(long double __n) { return __n; }
998 #if !defined(__STRICT_ANSI__) && defined(_GLIBCXX_USE_FLOAT128)
999 inline _GLIBCXX_CONSTEXPR long long
1000 __size_to_integer(__float128 __n) { return __n; }
1001 #endif
1002
1003 template<typename _OutputIterator, typename _Size, typename _Tp>
1004 _GLIBCXX20_CONSTEXPR
1005 inline typename
1006 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
1007 __fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
1008 {
1009 for (; __n > 0; --__n, (void) ++__first)
1010 *__first = __value;
1011 return __first;
1012 }
1013
1014 template<typename _OutputIterator, typename _Size, typename _Tp>
1015 _GLIBCXX20_CONSTEXPR
1016 inline typename
1017 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
1018 __fill_n_a1(_OutputIterator __first, _Size __n, const _Tp& __value)
1019 {
1020 const _Tp __tmp = __value;
1021 for (; __n > 0; --__n, (void) ++__first)
1022 *__first = __tmp;
1023 return __first;
1024 }
1025
1026 template<typename _Ite, typename _Seq, typename _Cat, typename _Size,
1027 typename _Tp>
1028 ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>
1029 __fill_n_a(const ::__gnu_debug::_Safe_iterator<_Ite, _Seq, _Cat>& __first,
1030 _Size __n, const _Tp& __value,
1031 std::input_iterator_tag);
1032
1033 template<typename _OutputIterator, typename _Size, typename _Tp>
1034 _GLIBCXX20_CONSTEXPR
1035 inline _OutputIterator
1036 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
1037 std::output_iterator_tag)
1038 {
1039 #if __cplusplus >= 201103L
1040 static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
1041 #endif
1042 return __fill_n_a1(__first, __n, __value);
1043 }
1044
1045 template<typename _OutputIterator, typename _Size, typename _Tp>
1046 _GLIBCXX20_CONSTEXPR
1047 inline _OutputIterator
1048 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
1049 std::input_iterator_tag)
1050 {
1051 #if __cplusplus >= 201103L
1052 static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
1053 #endif
1054 return __fill_n_a1(__first, __n, __value);
1055 }
1056
1057 template<typename _OutputIterator, typename _Size, typename _Tp>
1058 _GLIBCXX20_CONSTEXPR
1059 inline _OutputIterator
1060 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value,
1061 std::random_access_iterator_tag)
1062 {
1063 #if __cplusplus >= 201103L
1064 static_assert(is_integral<_Size>{}, "fill_n must pass integral size");
1065 #endif
1066 if (__n <= 0)
1067 return __first;
1068
1069 __glibcxx_requires_can_increment(__first, __n);
1070
1071 std::__fill_a(__first, __first + __n, __value);
1072 return __first + __n;
1073 }
1074
1075 /**
1076 * @brief Fills the range [first,first+n) with copies of value.
1077 * @ingroup mutating_algorithms
1078 * @param __first An output iterator.
1079 * @param __n The count of copies to perform.
1080 * @param __value A reference-to-const of arbitrary type.
1081 * @return The iterator at first+n.
1082 *
1083 * This function fills a range with copies of the same value. For char
1084 * types filling contiguous areas of memory, this becomes an inline call
1085 * to @c memset or @c wmemset.
1086 *
1087 * If @p __n is negative, the function does nothing.
1088 */
1089 // _GLIBCXX_RESOLVE_LIB_DEFECTS
1090 // DR 865. More algorithms that throw away information
1091 // DR 426. search_n(), fill_n(), and generate_n() with negative n
1092 template<typename _OI, typename _Size, typename _Tp>
1093 _GLIBCXX20_CONSTEXPR
1094 inline _OI
1095 fill_n(_OI __first, _Size __n, const _Tp& __value)
1096 {
1097 // concept requirements
1098 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
1099
1100 return std::__fill_n_a(__first, std::__size_to_integer(__n), __value,
1101 std::__iterator_category(__first));
1102 }
1103
1104 template<bool _BoolType>
1105 struct __equal
1106 {
1107 template<typename _II1, typename _II2>
1108 _GLIBCXX20_CONSTEXPR
1109 static bool
1110 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1111 {
1112 for (; __first1 != __last1; ++__first1, (void) ++__first2)
1113 if (!(*__first1 == *__first2))
1114 return false;
1115 return true;
1116 }
1117 };
1118
1119 template<>
1120 struct __equal<true>
1121 {
1122 template<typename _Tp>
1123 _GLIBCXX20_CONSTEXPR
1124 static bool
1125 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
1126 {
1127 if (const size_t __len = (__last1 - __first1))
1128 return !std::__memcmp(__first1, __first2, __len);
1129 return true;
1130 }
1131 };
1132
1133 template<typename _Tp, typename _Ref, typename _Ptr, typename _II>
1134 typename __gnu_cxx::__enable_if<
1135 __is_random_access_iter<_II>::__value, bool>::__type
1136 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
1137 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>,
1138 _II);
1139
1140 template<typename _Tp1, typename _Ref1, typename _Ptr1,
1141 typename _Tp2, typename _Ref2, typename _Ptr2>
1142 bool
1143 __equal_aux1(_GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1144 _GLIBCXX_STD_C::_Deque_iterator<_Tp1, _Ref1, _Ptr1>,
1145 _GLIBCXX_STD_C::_Deque_iterator<_Tp2, _Ref2, _Ptr2>);
1146
1147 template<typename _II, typename _Tp, typename _Ref, typename _Ptr>
1148 typename __gnu_cxx::__enable_if<
1149 __is_random_access_iter<_II>::__value, bool>::__type
1150 __equal_aux1(_II, _II,
1151 _GLIBCXX_STD_C::_Deque_iterator<_Tp, _Ref, _Ptr>);
1152
1153 template<typename _II1, typename _II2>
1154 _GLIBCXX20_CONSTEXPR
1155 inline bool
1156 __equal_aux1(_II1 __first1, _II1 __last1, _II2 __first2)
1157 {
1158 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1159 const bool __simple = ((__is_integer<_ValueType1>::__value
1160 || __is_pointer<_ValueType1>::__value)
1161 && __memcmpable<_II1, _II2>::__value);
1162 return std::__equal<__simple>::equal(__first1, __last1, __first2);
1163 }
1164
1165 template<typename _II1, typename _II2>
1166 _GLIBCXX20_CONSTEXPR
1167 inline bool
1168 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
1169 {
1170 return std::__equal_aux1(std::__niter_base(__first1),
1171 std::__niter_base(__last1),
1172 std::__niter_base(__first2));
1173 }
1174
1175 template<typename _II1, typename _Seq1, typename _Cat1, typename _II2>
1176 bool
1177 __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1178 const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1179 _II2);
1180
1181 template<typename _II1, typename _II2, typename _Seq2, typename _Cat2>
1182 bool
1183 __equal_aux(_II1, _II1,
1184 const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
1185
1186 template<typename _II1, typename _Seq1, typename _Cat1,
1187 typename _II2, typename _Seq2, typename _Cat2>
1188 bool
1189 __equal_aux(const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1190 const ::__gnu_debug::_Safe_iterator<_II1, _Seq1, _Cat1>&,
1191 const ::__gnu_debug::_Safe_iterator<_II2, _Seq2, _Cat2>&);
1192
1193 template<typename, typename>
1194 struct __lc_rai
1195 {
1196 template<typename _II1, typename _II2>
1197 _GLIBCXX20_CONSTEXPR
1198 static _II1
1199 __newlast1(_II1, _II1 __last1, _II2, _II2)
1200 { return __last1; }
1201
1202 template<typename _II>
1203 _GLIBCXX20_CONSTEXPR
1204 static bool
1205 __cnd2(_II __first, _II __last)
1206 { return __first != __last; }
1207 };
1208
1209 template<>
1210 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
1211 {
1212 template<typename _RAI1, typename _RAI2>
1213 _GLIBCXX20_CONSTEXPR
1214 static _RAI1
1215 __newlast1(_RAI1 __first1, _RAI1 __last1,
1216 _RAI2 __first2, _RAI2 __last2)
1217 {
1218 const typename iterator_traits<_RAI1>::difference_type
1219 __diff1 = __last1 - __first1;
1220 const typename iterator_traits<_RAI2>::difference_type
1221 __diff2 = __last2 - __first2;
1222 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
1223 }
1224
1225 template<typename _RAI>
1226 static _GLIBCXX20_CONSTEXPR bool
1227 __cnd2(_RAI, _RAI)
1228 { return true; }
1229 };
1230
1231 template<typename _II1, typename _II2, typename _Compare>
1232 _GLIBCXX20_CONSTEXPR
1233 bool
1234 __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
1235 _II2 __first2, _II2 __last2,
1236 _Compare __comp)
1237 {
1238 typedef typename iterator_traits<_II1>::iterator_category _Category1;
1239 typedef typename iterator_traits<_II2>::iterator_category _Category2;
1240 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
1241
1242 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1243 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
1244 ++__first1, (void)++__first2)
1245 {
1246 if (__comp(__first1, __first2))
1247 return true;
1248 if (__comp(__first2, __first1))
1249 return false;
1250 }
1251 return __first1 == __last1 && __first2 != __last2;
1252 }
1253
1254 template<bool _BoolType>
1255 struct __lexicographical_compare
1256 {
1257 template<typename _II1, typename _II2>
1258 _GLIBCXX20_CONSTEXPR
1259 static bool
1260 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1261 {
1262 using __gnu_cxx::__ops::__iter_less_iter;
1263 return std::__lexicographical_compare_impl(__first1, __last1,
1264 __first2, __last2,
1265 __iter_less_iter());
1266 }
1267 };
1268
1269 template<>
1270 struct __lexicographical_compare<true>
1271 {
1272 template<typename _Tp, typename _Up>
1273 _GLIBCXX20_CONSTEXPR
1274 static bool
1275 __lc(const _Tp* __first1, const _Tp* __last1,
1276 const _Up* __first2, const _Up* __last2)
1277 {
1278 const size_t __len1 = __last1 - __first1;
1279 const size_t __len2 = __last2 - __first2;
1280 if (const size_t __len = std::min(__len1, __len2))
1281 if (int __result = std::__memcmp(__first1, __first2, __len))
1282 return __result < 0;
1283 return __len1 < __len2;
1284 }
1285 };
1286
1287 template<typename _II1, typename _II2>
1288 _GLIBCXX20_CONSTEXPR
1289 inline bool
1290 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
1291 _II2 __first2, _II2 __last2)
1292 {
1293 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1294 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1295 const bool __simple =
1296 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
1297 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
1298 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
1299 && __is_pointer<_II1>::__value
1300 && __is_pointer<_II2>::__value
1301 #if __cplusplus > 201703L && __cpp_lib_concepts
1302 // For C++20 iterator_traits<volatile T*>::value_type is non-volatile
1303 // so __is_byte<T> could be true, but we can't use memcmp with
1304 // volatile data.
1305 && !is_volatile_v<remove_reference_t<iter_reference_t<_II1>>>
1306 && !is_volatile_v<remove_reference_t<iter_reference_t<_II2>>>
1307 #endif
1308 );
1309
1310 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
1311 __first2, __last2);
1312 }
1313
1314 template<typename _ForwardIterator, typename _Tp, typename _Compare>
1315 _GLIBCXX20_CONSTEXPR
1316 _ForwardIterator
1317 __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
1318 const _Tp& __val, _Compare __comp)
1319 {
1320 typedef typename iterator_traits<_ForwardIterator>::difference_type
1321 _DistanceType;
1322
1323 _DistanceType __len = std::distance(__first, __last);
1324
1325 while (__len > 0)
1326 {
1327 _DistanceType __half = __len >> 1;
1328 _ForwardIterator __middle = __first;
1329 std::advance(__middle, __half);
1330 if (__comp(__middle, __val))
1331 {
1332 __first = __middle;
1333 ++__first;
1334 __len = __len - __half - 1;
1335 }
1336 else
1337 __len = __half;
1338 }
1339 return __first;
1340 }
1341
1342 /**
1343 * @brief Finds the first position in which @a val could be inserted
1344 * without changing the ordering.
1345 * @param __first An iterator.
1346 * @param __last Another iterator.
1347 * @param __val The search term.
1348 * @return An iterator pointing to the first element <em>not less
1349 * than</em> @a val, or end() if every element is less than
1350 * @a val.
1351 * @ingroup binary_search_algorithms
1352 */
1353 template<typename _ForwardIterator, typename _Tp>
1354 _GLIBCXX20_CONSTEXPR
1355 inline _ForwardIterator
1356 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
1357 const _Tp& __val)
1358 {
1359 // concept requirements
1360 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
1361 __glibcxx_function_requires(_LessThanOpConcept<
1362 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1363 __glibcxx_requires_partitioned_lower(__first, __last, __val);
1364
1365 return std::__lower_bound(__first, __last, __val,
1366 __gnu_cxx::__ops::__iter_less_val());
1367 }
1368
1369 /// This is a helper function for the sort routines and for random.tcc.
1370 // Precondition: __n > 0.
1371 inline _GLIBCXX_CONSTEXPR int
1372 __lg(int __n)
1373 { return (int)sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1374
1375 inline _GLIBCXX_CONSTEXPR unsigned
1376 __lg(unsigned __n)
1377 { return (int)sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1378
1379 inline _GLIBCXX_CONSTEXPR long
1380 __lg(long __n)
1381 { return (int)sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1382
1383 inline _GLIBCXX_CONSTEXPR unsigned long
1384 __lg(unsigned long __n)
1385 { return (int)sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1386
1387 inline _GLIBCXX_CONSTEXPR long long
1388 __lg(long long __n)
1389 { return (int)sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1390
1391 inline _GLIBCXX_CONSTEXPR unsigned long long
1392 __lg(unsigned long long __n)
1393 { return (int)sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1394
1395 _GLIBCXX_BEGIN_NAMESPACE_ALGO
1396
1397 /**
1398 * @brief Tests a range for element-wise equality.
1399 * @ingroup non_mutating_algorithms
1400 * @param __first1 An input iterator.
1401 * @param __last1 An input iterator.
1402 * @param __first2 An input iterator.
1403 * @return A boolean true or false.
1404 *
1405 * This compares the elements of two ranges using @c == and returns true or
1406 * false depending on whether all of the corresponding elements of the
1407 * ranges are equal.
1408 */
1409 template<typename _II1, typename _II2>
1410 _GLIBCXX20_CONSTEXPR
1411 inline bool
1412 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1413 {
1414 // concept requirements
1415 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1416 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1417 __glibcxx_function_requires(_EqualOpConcept<
1418 typename iterator_traits<_II1>::value_type,
1419 typename iterator_traits<_II2>::value_type>)
1420 __glibcxx_requires_can_increment_range(__first1, __last1, __first2);
1421
1422 return std::__equal_aux(__first1, __last1, __first2);
1423 }
1424
1425 /**
1426 * @brief Tests a range for element-wise equality.
1427 * @ingroup non_mutating_algorithms
1428 * @param __first1 An input iterator.
1429 * @param __last1 An input iterator.
1430 * @param __first2 An input iterator.
1431 * @param __binary_pred A binary predicate @link functors
1432 * functor@endlink.
1433 * @return A boolean true or false.
1434 *
1435 * This compares the elements of two ranges using the binary_pred
1436 * parameter, and returns true or
1437 * false depending on whether all of the corresponding elements of the
1438 * ranges are equal.
1439 */
1440 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1441 _GLIBCXX20_CONSTEXPR
1442 inline bool
1443 equal(_IIter1 __first1, _IIter1 __last1,
1444 _IIter2 __first2, _BinaryPredicate __binary_pred)
1445 {
1446 // concept requirements
1447 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1448 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1449 __glibcxx_requires_valid_range(__first1, __last1);
1450
1451 for (; __first1 != __last1; ++__first1, (void)++__first2)
1452 if (!bool(__binary_pred(*__first1, *__first2)))
1453 return false;
1454 return true;
1455 }
1456
1457 #if __cplusplus >= 201103L
1458 // 4-iterator version of std::equal<It1, It2> for use in C++11.
1459 template<typename _II1, typename _II2>
1460 _GLIBCXX20_CONSTEXPR
1461 inline bool
1462 __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1463 {
1464 using _RATag = random_access_iterator_tag;
1465 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1466 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1467 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1468 if (_RAIters())
1469 {
1470 auto __d1 = std::distance(__first1, __last1);
1471 auto __d2 = std::distance(__first2, __last2);
1472 if (__d1 != __d2)
1473 return false;
1474 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
1475 }
1476
1477 for (; __first1 != __last1 && __first2 != __last2;
1478 ++__first1, (void)++__first2)
1479 if (!(*__first1 == *__first2))
1480 return false;
1481 return __first1 == __last1 && __first2 == __last2;
1482 }
1483
1484 // 4-iterator version of std::equal<It1, It2, BinaryPred> for use in C++11.
1485 template<typename _II1, typename _II2, typename _BinaryPredicate>
1486 _GLIBCXX20_CONSTEXPR
1487 inline bool
1488 __equal4(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2,
1489 _BinaryPredicate __binary_pred)
1490 {
1491 using _RATag = random_access_iterator_tag;
1492 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1493 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1494 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1495 if (_RAIters())
1496 {
1497 auto __d1 = std::distance(__first1, __last1);
1498 auto __d2 = std::distance(__first2, __last2);
1499 if (__d1 != __d2)
1500 return false;
1501 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
1502 __binary_pred);
1503 }
1504
1505 for (; __first1 != __last1 && __first2 != __last2;
1506 ++__first1, (void)++__first2)
1507 if (!bool(__binary_pred(*__first1, *__first2)))
1508 return false;
1509 return __first1 == __last1 && __first2 == __last2;
1510 }
1511 #endif // C++11
1512
1513 #if __cplusplus > 201103L
1514
1515 #define __cpp_lib_robust_nonmodifying_seq_ops 201304
1516
1517 /**
1518 * @brief Tests a range for element-wise equality.
1519 * @ingroup non_mutating_algorithms
1520 * @param __first1 An input iterator.
1521 * @param __last1 An input iterator.
1522 * @param __first2 An input iterator.
1523 * @param __last2 An input iterator.
1524 * @return A boolean true or false.
1525 *
1526 * This compares the elements of two ranges using @c == and returns true or
1527 * false depending on whether all of the corresponding elements of the
1528 * ranges are equal.
1529 */
1530 template<typename _II1, typename _II2>
1531 _GLIBCXX20_CONSTEXPR
1532 inline bool
1533 equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1534 {
1535 // concept requirements
1536 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1537 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1538 __glibcxx_function_requires(_EqualOpConcept<
1539 typename iterator_traits<_II1>::value_type,
1540 typename iterator_traits<_II2>::value_type>)
1541 __glibcxx_requires_valid_range(__first1, __last1);
1542 __glibcxx_requires_valid_range(__first2, __last2);
1543
1544 return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2);
1545 }
1546
1547 /**
1548 * @brief Tests a range for element-wise equality.
1549 * @ingroup non_mutating_algorithms
1550 * @param __first1 An input iterator.
1551 * @param __last1 An input iterator.
1552 * @param __first2 An input iterator.
1553 * @param __last2 An input iterator.
1554 * @param __binary_pred A binary predicate @link functors
1555 * functor@endlink.
1556 * @return A boolean true or false.
1557 *
1558 * This compares the elements of two ranges using the binary_pred
1559 * parameter, and returns true or
1560 * false depending on whether all of the corresponding elements of the
1561 * ranges are equal.
1562 */
1563 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1564 _GLIBCXX20_CONSTEXPR
1565 inline bool
1566 equal(_IIter1 __first1, _IIter1 __last1,
1567 _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
1568 {
1569 // concept requirements
1570 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1571 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1572 __glibcxx_requires_valid_range(__first1, __last1);
1573 __glibcxx_requires_valid_range(__first2, __last2);
1574
1575 return _GLIBCXX_STD_A::__equal4(__first1, __last1, __first2, __last2,
1576 __binary_pred);
1577 }
1578 #endif // C++14
1579
1580 /**
1581 * @brief Performs @b dictionary comparison on ranges.
1582 * @ingroup sorting_algorithms
1583 * @param __first1 An input iterator.
1584 * @param __last1 An input iterator.
1585 * @param __first2 An input iterator.
1586 * @param __last2 An input iterator.
1587 * @return A boolean true or false.
1588 *
1589 * <em>Returns true if the sequence of elements defined by the range
1590 * [first1,last1) is lexicographically less than the sequence of elements
1591 * defined by the range [first2,last2). Returns false otherwise.</em>
1592 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1593 * then this is an inline call to @c memcmp.
1594 */
1595 template<typename _II1, typename _II2>
1596 _GLIBCXX20_CONSTEXPR
1597 inline bool
1598 lexicographical_compare(_II1 __first1, _II1 __last1,
1599 _II2 __first2, _II2 __last2)
1600 {
1601 #ifdef _GLIBCXX_CONCEPT_CHECKS
1602 // concept requirements
1603 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1604 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1605 #endif
1606 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1607 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1608 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1609 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1610 __glibcxx_requires_valid_range(__first1, __last1);
1611 __glibcxx_requires_valid_range(__first2, __last2);
1612
1613 return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1614 std::__niter_base(__last1),
1615 std::__niter_base(__first2),
1616 std::__niter_base(__last2));
1617 }
1618
1619 /**
1620 * @brief Performs @b dictionary comparison on ranges.
1621 * @ingroup sorting_algorithms
1622 * @param __first1 An input iterator.
1623 * @param __last1 An input iterator.
1624 * @param __first2 An input iterator.
1625 * @param __last2 An input iterator.
1626 * @param __comp A @link comparison_functors comparison functor@endlink.
1627 * @return A boolean true or false.
1628 *
1629 * The same as the four-parameter @c lexicographical_compare, but uses the
1630 * comp parameter instead of @c <.
1631 */
1632 template<typename _II1, typename _II2, typename _Compare>
1633 _GLIBCXX20_CONSTEXPR
1634 inline bool
1635 lexicographical_compare(_II1 __first1, _II1 __last1,
1636 _II2 __first2, _II2 __last2, _Compare __comp)
1637 {
1638 // concept requirements
1639 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1640 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1641 __glibcxx_requires_valid_range(__first1, __last1);
1642 __glibcxx_requires_valid_range(__first2, __last2);
1643
1644 return std::__lexicographical_compare_impl
1645 (__first1, __last1, __first2, __last2,
1646 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1647 }
1648
1649 #if __cpp_lib_three_way_comparison
1650 // Iter points to a contiguous range of unsigned narrow character type
1651 // or std::byte, suitable for comparison by memcmp.
1652 template<typename _Iter>
1653 concept __is_byte_iter = contiguous_iterator<_Iter>
1654 && __is_byte<iter_value_t<_Iter>>::__value != 0
1655 && !__gnu_cxx::__numeric_traits<iter_value_t<_Iter>>::__is_signed;
1656
1657 // Return a struct with two members, initialized to the smaller of x and y
1658 // (or x if they compare equal) and the result of the comparison x <=> y.
1659 template<typename _Tp>
1660 constexpr auto
1661 __min_cmp(_Tp __x, _Tp __y)
1662 {
1663 struct _Res {
1664 _Tp _M_min;
1665 decltype(__x <=> __y) _M_cmp;
1666 };
1667 auto __c = __x <=> __y;
1668 if (__c > 0)
1669 return _Res{__y, __c};
1670 return _Res{__x, __c};
1671 }
1672
1673 /**
1674 * @brief Performs dictionary comparison on ranges.
1675 * @ingroup sorting_algorithms
1676 * @param __first1 An input iterator.
1677 * @param __last1 An input iterator.
1678 * @param __first2 An input iterator.
1679 * @param __last2 An input iterator.
1680 * @param __comp A @link comparison_functors comparison functor@endlink.
1681 * @return The comparison category that `__comp(*__first1, *__first2)`
1682 * returns.
1683 */
1684 template<typename _InputIter1, typename _InputIter2, typename _Comp>
1685 constexpr auto
1686 lexicographical_compare_three_way(_InputIter1 __first1,
1687 _InputIter1 __last1,
1688 _InputIter2 __first2,
1689 _InputIter2 __last2,
1690 _Comp __comp)
1691 -> decltype(__comp(*__first1, *__first2))
1692 {
1693 // concept requirements
1694 __glibcxx_function_requires(_InputIteratorConcept<_InputIter1>)
1695 __glibcxx_function_requires(_InputIteratorConcept<_InputIter2>)
1696 __glibcxx_requires_valid_range(__first1, __last1);
1697 __glibcxx_requires_valid_range(__first2, __last2);
1698
1699 #if __cpp_lib_is_constant_evaluated
1700 using _Cat = decltype(__comp(*__first1, *__first2));
1701 static_assert(same_as<common_comparison_category_t<_Cat>, _Cat>);
1702
1703 if (!std::is_constant_evaluated())
1704 if constexpr (same_as<_Comp, __detail::_Synth3way>
1705 || same_as<_Comp, compare_three_way>)
1706 if constexpr (__is_byte_iter<_InputIter1>)
1707 if constexpr (__is_byte_iter<_InputIter2>)
1708 {
1709 const auto [__len, __lencmp]
1710 = std::__min_cmp(__last1 - __first1, __last2 - __first2);
1711 if (__len)
1712 {
1713 const auto __c
1714 = __builtin_memcmp(&*__first1, &*__first2, __len) <=> 0;
1715 if (__c != 0)
1716 return __c;
1717 }
1718 return __lencmp;
1719 }
1720 #endif // is_constant_evaluated
1721 while (__first1 != __last1)
1722 {
1723 if (__first2 == __last2)
1724 return strong_ordering::greater;
1725 if (auto __cmp = __comp(*__first1, *__first2); __cmp != 0)
1726 return __cmp;
1727 ++__first1;
1728 ++__first2;
1729 }
1730 return (__first2 == __last2) <=> true; // See PR 94006
1731 }
1732
1733 template<typename _InputIter1, typename _InputIter2>
1734 constexpr auto
1735 lexicographical_compare_three_way(_InputIter1 __first1,
1736 _InputIter1 __last1,
1737 _InputIter2 __first2,
1738 _InputIter2 __last2)
1739 {
1740 return std::lexicographical_compare_three_way(__first1, __last1,
1741 __first2, __last2,
1742 compare_three_way{});
1743 }
1744 #endif // three_way_comparison
1745
1746 template<typename _InputIterator1, typename _InputIterator2,
1747 typename _BinaryPredicate>
1748 _GLIBCXX20_CONSTEXPR
1749 pair<_InputIterator1, _InputIterator2>
1750 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1751 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1752 {
1753 while (__first1 != __last1 && __binary_pred(__first1, __first2))
1754 {
1755 ++__first1;
1756 ++__first2;
1757 }
1758 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1759 }
1760
1761 /**
1762 * @brief Finds the places in ranges which don't match.
1763 * @ingroup non_mutating_algorithms
1764 * @param __first1 An input iterator.
1765 * @param __last1 An input iterator.
1766 * @param __first2 An input iterator.
1767 * @return A pair of iterators pointing to the first mismatch.
1768 *
1769 * This compares the elements of two ranges using @c == and returns a pair
1770 * of iterators. The first iterator points into the first range, the
1771 * second iterator points into the second range, and the elements pointed
1772 * to by the iterators are not equal.
1773 */
1774 template<typename _InputIterator1, typename _InputIterator2>
1775 _GLIBCXX20_CONSTEXPR
1776 inline pair<_InputIterator1, _InputIterator2>
1777 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1778 _InputIterator2 __first2)
1779 {
1780 // concept requirements
1781 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1782 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1783 __glibcxx_function_requires(_EqualOpConcept<
1784 typename iterator_traits<_InputIterator1>::value_type,
1785 typename iterator_traits<_InputIterator2>::value_type>)
1786 __glibcxx_requires_valid_range(__first1, __last1);
1787
1788 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1789 __gnu_cxx::__ops::__iter_equal_to_iter());
1790 }
1791
1792 /**
1793 * @brief Finds the places in ranges which don't match.
1794 * @ingroup non_mutating_algorithms
1795 * @param __first1 An input iterator.
1796 * @param __last1 An input iterator.
1797 * @param __first2 An input iterator.
1798 * @param __binary_pred A binary predicate @link functors
1799 * functor@endlink.
1800 * @return A pair of iterators pointing to the first mismatch.
1801 *
1802 * This compares the elements of two ranges using the binary_pred
1803 * parameter, and returns a pair
1804 * of iterators. The first iterator points into the first range, the
1805 * second iterator points into the second range, and the elements pointed
1806 * to by the iterators are not equal.
1807 */
1808 template<typename _InputIterator1, typename _InputIterator2,
1809 typename _BinaryPredicate>
1810 _GLIBCXX20_CONSTEXPR
1811 inline pair<_InputIterator1, _InputIterator2>
1812 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1813 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1814 {
1815 // concept requirements
1816 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1817 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1818 __glibcxx_requires_valid_range(__first1, __last1);
1819
1820 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1821 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1822 }
1823
1824 #if __cplusplus > 201103L
1825
1826 template<typename _InputIterator1, typename _InputIterator2,
1827 typename _BinaryPredicate>
1828 _GLIBCXX20_CONSTEXPR
1829 pair<_InputIterator1, _InputIterator2>
1830 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1831 _InputIterator2 __first2, _InputIterator2 __last2,
1832 _BinaryPredicate __binary_pred)
1833 {
1834 while (__first1 != __last1 && __first2 != __last2
1835 && __binary_pred(__first1, __first2))
1836 {
1837 ++__first1;
1838 ++__first2;
1839 }
1840 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1841 }
1842
1843 /**
1844 * @brief Finds the places in ranges which don't match.
1845 * @ingroup non_mutating_algorithms
1846 * @param __first1 An input iterator.
1847 * @param __last1 An input iterator.
1848 * @param __first2 An input iterator.
1849 * @param __last2 An input iterator.
1850 * @return A pair of iterators pointing to the first mismatch.
1851 *
1852 * This compares the elements of two ranges using @c == and returns a pair
1853 * of iterators. The first iterator points into the first range, the
1854 * second iterator points into the second range, and the elements pointed
1855 * to by the iterators are not equal.
1856 */
1857 template<typename _InputIterator1, typename _InputIterator2>
1858 _GLIBCXX20_CONSTEXPR
1859 inline pair<_InputIterator1, _InputIterator2>
1860 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1861 _InputIterator2 __first2, _InputIterator2 __last2)
1862 {
1863 // concept requirements
1864 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1865 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1866 __glibcxx_function_requires(_EqualOpConcept<
1867 typename iterator_traits<_InputIterator1>::value_type,
1868 typename iterator_traits<_InputIterator2>::value_type>)
1869 __glibcxx_requires_valid_range(__first1, __last1);
1870 __glibcxx_requires_valid_range(__first2, __last2);
1871
1872 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1873 __gnu_cxx::__ops::__iter_equal_to_iter());
1874 }
1875
1876 /**
1877 * @brief Finds the places in ranges which don't match.
1878 * @ingroup non_mutating_algorithms
1879 * @param __first1 An input iterator.
1880 * @param __last1 An input iterator.
1881 * @param __first2 An input iterator.
1882 * @param __last2 An input iterator.
1883 * @param __binary_pred A binary predicate @link functors
1884 * functor@endlink.
1885 * @return A pair of iterators pointing to the first mismatch.
1886 *
1887 * This compares the elements of two ranges using the binary_pred
1888 * parameter, and returns a pair
1889 * of iterators. The first iterator points into the first range, the
1890 * second iterator points into the second range, and the elements pointed
1891 * to by the iterators are not equal.
1892 */
1893 template<typename _InputIterator1, typename _InputIterator2,
1894 typename _BinaryPredicate>
1895 _GLIBCXX20_CONSTEXPR
1896 inline pair<_InputIterator1, _InputIterator2>
1897 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1898 _InputIterator2 __first2, _InputIterator2 __last2,
1899 _BinaryPredicate __binary_pred)
1900 {
1901 // concept requirements
1902 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1903 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1904 __glibcxx_requires_valid_range(__first1, __last1);
1905 __glibcxx_requires_valid_range(__first2, __last2);
1906
1907 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1908 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1909 }
1910 #endif
1911
1912 _GLIBCXX_END_NAMESPACE_ALGO
1913
1914 /// This is an overload used by find algos for the Input Iterator case.
1915 template<typename _InputIterator, typename _Predicate>
1916 _GLIBCXX20_CONSTEXPR
1917 inline _InputIterator
1918 __find_if(_InputIterator __first, _InputIterator __last,
1919 _Predicate __pred, input_iterator_tag)
1920 {
1921 while (__first != __last && !__pred(__first))
1922 ++__first;
1923 return __first;
1924 }
1925
1926 /// This is an overload used by find algos for the RAI case.
1927 template<typename _RandomAccessIterator, typename _Predicate>
1928 _GLIBCXX20_CONSTEXPR
1929 _RandomAccessIterator
1930 __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
1931 _Predicate __pred, random_access_iterator_tag)
1932 {
1933 typename iterator_traits<_RandomAccessIterator>::difference_type
1934 __trip_count = (__last - __first) >> 2;
1935
1936 for (; __trip_count > 0; --__trip_count)
1937 {
1938 if (__pred(__first))
1939 return __first;
1940 ++__first;
1941
1942 if (__pred(__first))
1943 return __first;
1944 ++__first;
1945
1946 if (__pred(__first))
1947 return __first;
1948 ++__first;
1949
1950 if (__pred(__first))
1951 return __first;
1952 ++__first;
1953 }
1954
1955 switch (__last - __first)
1956 {
1957 case 3:
1958 if (__pred(__first))
1959 return __first;
1960 ++__first;
1961 case 2:
1962 if (__pred(__first))
1963 return __first;
1964 ++__first;
1965 case 1:
1966 if (__pred(__first))
1967 return __first;
1968 ++__first;
1969 case 0:
1970 default:
1971 return __last;
1972 }
1973 }
1974
1975 template<typename _Iterator, typename _Predicate>
1976 _GLIBCXX20_CONSTEXPR
1977 inline _Iterator
1978 __find_if(_Iterator __first, _Iterator __last, _Predicate __pred)
1979 {
1980 return __find_if(__first, __last, __pred,
1981 std::__iterator_category(__first));
1982 }
1983
1984 template<typename _InputIterator, typename _Predicate>
1985 _GLIBCXX20_CONSTEXPR
1986 typename iterator_traits<_InputIterator>::difference_type
1987 __count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
1988 {
1989 typename iterator_traits<_InputIterator>::difference_type __n = 0;
1990 for (; __first != __last; ++__first)
1991 if (__pred(__first))
1992 ++__n;
1993 return __n;
1994 }
1995
1996 #if __cplusplus >= 201103L
1997 template<typename _ForwardIterator1, typename _ForwardIterator2,
1998 typename _BinaryPredicate>
1999 _GLIBCXX20_CONSTEXPR
2000 bool
2001 __is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2002 _ForwardIterator2 __first2, _BinaryPredicate __pred)
2003 {
2004 // Efficiently compare identical prefixes: O(N) if sequences
2005 // have the same elements in the same order.
2006 for (; __first1 != __last1; ++__first1, (void)++__first2)
2007 if (!__pred(__first1, __first2))
2008 break;
2009
2010 if (__first1 == __last1)
2011 return true;
2012
2013 // Establish __last2 assuming equal ranges by iterating over the
2014 // rest of the list.
2015 _ForwardIterator2 __last2 = __first2;
2016 std::advance(__last2, std::distance(__first1, __last1));
2017 for (_ForwardIterator1 __scan = __first1; __scan != __last1; ++__scan)
2018 {
2019 if (__scan != std::__find_if(__first1, __scan,
2020 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan)))
2021 continue; // We've seen this one before.
2022
2023 auto __matches
2024 = std::__count_if(__first2, __last2,
2025 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan));
2026 if (0 == __matches ||
2027 std::__count_if(__scan, __last1,
2028 __gnu_cxx::__ops::__iter_comp_iter(__pred, __scan))
2029 != __matches)
2030 return false;
2031 }
2032 return true;
2033 }
2034
2035 /**
2036 * @brief Checks whether a permutation of the second sequence is equal
2037 * to the first sequence.
2038 * @ingroup non_mutating_algorithms
2039 * @param __first1 Start of first range.
2040 * @param __last1 End of first range.
2041 * @param __first2 Start of second range.
2042 * @return true if there exists a permutation of the elements in the range
2043 * [__first2, __first2 + (__last1 - __first1)), beginning with
2044 * ForwardIterator2 begin, such that equal(__first1, __last1, begin)
2045 * returns true; otherwise, returns false.
2046 */
2047 template<typename _ForwardIterator1, typename _ForwardIterator2>
2048 _GLIBCXX20_CONSTEXPR
2049 inline bool
2050 is_permutation(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
2051 _ForwardIterator2 __first2)
2052 {
2053 // concept requirements
2054 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
2055 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator2>)
2056 __glibcxx_function_requires(_EqualOpConcept<
2057 typename iterator_traits<_ForwardIterator1>::value_type,
2058 typename iterator_traits<_ForwardIterator2>::value_type>)
2059 __glibcxx_requires_valid_range(__first1, __last1);
2060
2061 return std::__is_permutation(__first1, __last1, __first2,
2062 __gnu_cxx::__ops::__iter_equal_to_iter());
2063 }
2064 #endif // C++11
2065
2066 _GLIBCXX_END_NAMESPACE_VERSION
2067 } // namespace std
2068
2069 // NB: This file is included within many other C++ includes, as a way
2070 // of getting the base algorithms. So, make sure that parallel bits
2071 // come in too if requested.
2072 #ifdef _GLIBCXX_PARALLEL
2073 # include <parallel/algobase.h>
2074 #endif
2075
2076 #endif