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