]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_algobase.h
re PR libstdc++/66354 ([UBSAN] stl_algobase.h:708:7: runtime error: null pointer...
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stl_algobase.h
1 // Core algorithmic facilities -*- C++ -*-
2
3 // Copyright (C) 2001-2015 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 and _GLIBCXX_MOVE
71 #include <bits/predefined_ops.h>
72
73 namespace std _GLIBCXX_VISIBILITY(default)
74 {
75 _GLIBCXX_BEGIN_NAMESPACE_VERSION
76
77 #if __cplusplus < 201103L
78 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
79 // nutshell, we are partially implementing the resolution of DR 187,
80 // when it's safe, i.e., the value_types are equal.
81 template<bool _BoolType>
82 struct __iter_swap
83 {
84 template<typename _ForwardIterator1, typename _ForwardIterator2>
85 static void
86 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
87 {
88 typedef typename iterator_traits<_ForwardIterator1>::value_type
89 _ValueType1;
90 _ValueType1 __tmp = _GLIBCXX_MOVE(*__a);
91 *__a = _GLIBCXX_MOVE(*__b);
92 *__b = _GLIBCXX_MOVE(__tmp);
93 }
94 };
95
96 template<>
97 struct __iter_swap<true>
98 {
99 template<typename _ForwardIterator1, typename _ForwardIterator2>
100 static void
101 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
102 {
103 swap(*__a, *__b);
104 }
105 };
106 #endif
107
108 /**
109 * @brief Swaps the contents of two iterators.
110 * @ingroup mutating_algorithms
111 * @param __a An iterator.
112 * @param __b Another iterator.
113 * @return Nothing.
114 *
115 * This function swaps the values pointed to by two iterators, not the
116 * iterators themselves.
117 */
118 template<typename _ForwardIterator1, typename _ForwardIterator2>
119 inline void
120 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
121 {
122 // concept requirements
123 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
124 _ForwardIterator1>)
125 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
126 _ForwardIterator2>)
127
128 #if __cplusplus < 201103L
129 typedef typename iterator_traits<_ForwardIterator1>::value_type
130 _ValueType1;
131 typedef typename iterator_traits<_ForwardIterator2>::value_type
132 _ValueType2;
133
134 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
135 _ValueType2>)
136 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
137 _ValueType1>)
138
139 typedef typename iterator_traits<_ForwardIterator1>::reference
140 _ReferenceType1;
141 typedef typename iterator_traits<_ForwardIterator2>::reference
142 _ReferenceType2;
143 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
144 && __are_same<_ValueType1&, _ReferenceType1>::__value
145 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
146 iter_swap(__a, __b);
147 #else
148 swap(*__a, *__b);
149 #endif
150 }
151
152 /**
153 * @brief Swap the elements of two sequences.
154 * @ingroup mutating_algorithms
155 * @param __first1 A forward iterator.
156 * @param __last1 A forward iterator.
157 * @param __first2 A forward iterator.
158 * @return An iterator equal to @p first2+(last1-first1).
159 *
160 * Swaps each element in the range @p [first1,last1) with the
161 * corresponding element in the range @p [first2,(last1-first1)).
162 * The ranges must not overlap.
163 */
164 template<typename _ForwardIterator1, typename _ForwardIterator2>
165 _ForwardIterator2
166 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
167 _ForwardIterator2 __first2)
168 {
169 // concept requirements
170 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
171 _ForwardIterator1>)
172 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
173 _ForwardIterator2>)
174 __glibcxx_requires_valid_range(__first1, __last1);
175
176 for (; __first1 != __last1; ++__first1, (void)++__first2)
177 std::iter_swap(__first1, __first2);
178 return __first2;
179 }
180
181 /**
182 * @brief This does what you think it does.
183 * @ingroup sorting_algorithms
184 * @param __a A thing of arbitrary type.
185 * @param __b Another thing of arbitrary type.
186 * @return The lesser of the parameters.
187 *
188 * This is the simple classic generic implementation. It will work on
189 * temporary expressions, since they are only evaluated once, unlike a
190 * preprocessor macro.
191 */
192 template<typename _Tp>
193 _GLIBCXX14_CONSTEXPR
194 inline const _Tp&
195 min(const _Tp& __a, const _Tp& __b)
196 {
197 // concept requirements
198 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
199 //return __b < __a ? __b : __a;
200 if (__b < __a)
201 return __b;
202 return __a;
203 }
204
205 /**
206 * @brief This does what you think it does.
207 * @ingroup sorting_algorithms
208 * @param __a A thing of arbitrary type.
209 * @param __b Another thing of arbitrary type.
210 * @return The greater of the parameters.
211 *
212 * This is the simple classic generic implementation. It will work on
213 * temporary expressions, since they are only evaluated once, unlike a
214 * preprocessor macro.
215 */
216 template<typename _Tp>
217 _GLIBCXX14_CONSTEXPR
218 inline const _Tp&
219 max(const _Tp& __a, const _Tp& __b)
220 {
221 // concept requirements
222 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
223 //return __a < __b ? __b : __a;
224 if (__a < __b)
225 return __b;
226 return __a;
227 }
228
229 /**
230 * @brief This does what you think it does.
231 * @ingroup sorting_algorithms
232 * @param __a A thing of arbitrary type.
233 * @param __b Another thing of arbitrary type.
234 * @param __comp A @link comparison_functors comparison functor@endlink.
235 * @return The lesser of the parameters.
236 *
237 * This will work on temporary expressions, since they are only evaluated
238 * once, unlike a preprocessor macro.
239 */
240 template<typename _Tp, typename _Compare>
241 _GLIBCXX14_CONSTEXPR
242 inline const _Tp&
243 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
244 {
245 //return __comp(__b, __a) ? __b : __a;
246 if (__comp(__b, __a))
247 return __b;
248 return __a;
249 }
250
251 /**
252 * @brief This does what you think it does.
253 * @ingroup sorting_algorithms
254 * @param __a A thing of arbitrary type.
255 * @param __b Another thing of arbitrary type.
256 * @param __comp A @link comparison_functors comparison functor@endlink.
257 * @return The greater of the parameters.
258 *
259 * This will work on temporary expressions, since they are only evaluated
260 * once, unlike a preprocessor macro.
261 */
262 template<typename _Tp, typename _Compare>
263 _GLIBCXX14_CONSTEXPR
264 inline const _Tp&
265 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
266 {
267 //return __comp(__a, __b) ? __b : __a;
268 if (__comp(__a, __b))
269 return __b;
270 return __a;
271 }
272
273 // Fallback implementation of the function in bits/stl_iterator.h used to
274 // remove the __normal_iterator wrapper. See copy, fill, ...
275 template<typename _Iterator>
276 inline _Iterator
277 __niter_base(_Iterator __it)
278 { return __it; }
279
280 // Likewise for move_iterator.
281 template<typename _Iterator>
282 inline _Iterator
283 __miter_base(_Iterator __it)
284 { return __it; }
285
286 // All of these auxiliary structs serve two purposes. (1) Replace
287 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
288 // because the input and output ranges are permitted to overlap.)
289 // (2) If we're using random access iterators, then write the loop as
290 // a for loop with an explicit count.
291
292 template<bool, bool, typename>
293 struct __copy_move
294 {
295 template<typename _II, typename _OI>
296 static _OI
297 __copy_m(_II __first, _II __last, _OI __result)
298 {
299 for (; __first != __last; ++__result, (void)++__first)
300 *__result = *__first;
301 return __result;
302 }
303 };
304
305 #if __cplusplus >= 201103L
306 template<typename _Category>
307 struct __copy_move<true, false, _Category>
308 {
309 template<typename _II, typename _OI>
310 static _OI
311 __copy_m(_II __first, _II __last, _OI __result)
312 {
313 for (; __first != __last; ++__result, (void)++__first)
314 *__result = std::move(*__first);
315 return __result;
316 }
317 };
318 #endif
319
320 template<>
321 struct __copy_move<false, false, random_access_iterator_tag>
322 {
323 template<typename _II, typename _OI>
324 static _OI
325 __copy_m(_II __first, _II __last, _OI __result)
326 {
327 typedef typename iterator_traits<_II>::difference_type _Distance;
328 for(_Distance __n = __last - __first; __n > 0; --__n)
329 {
330 *__result = *__first;
331 ++__first;
332 ++__result;
333 }
334 return __result;
335 }
336 };
337
338 #if __cplusplus >= 201103L
339 template<>
340 struct __copy_move<true, false, random_access_iterator_tag>
341 {
342 template<typename _II, typename _OI>
343 static _OI
344 __copy_m(_II __first, _II __last, _OI __result)
345 {
346 typedef typename iterator_traits<_II>::difference_type _Distance;
347 for(_Distance __n = __last - __first; __n > 0; --__n)
348 {
349 *__result = std::move(*__first);
350 ++__first;
351 ++__result;
352 }
353 return __result;
354 }
355 };
356 #endif
357
358 template<bool _IsMove>
359 struct __copy_move<_IsMove, true, random_access_iterator_tag>
360 {
361 template<typename _Tp>
362 static _Tp*
363 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
364 {
365 #if __cplusplus >= 201103L
366 // trivial types can have deleted assignment
367 static_assert( is_copy_assignable<_Tp>::value,
368 "type is not assignable" );
369 #endif
370 const ptrdiff_t _Num = __last - __first;
371 if (_Num)
372 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
373 return __result + _Num;
374 }
375 };
376
377 template<bool _IsMove, typename _II, typename _OI>
378 inline _OI
379 __copy_move_a(_II __first, _II __last, _OI __result)
380 {
381 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
382 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
383 typedef typename iterator_traits<_II>::iterator_category _Category;
384 const bool __simple = (__is_trivial(_ValueTypeI)
385 && __is_pointer<_II>::__value
386 && __is_pointer<_OI>::__value
387 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
388
389 return std::__copy_move<_IsMove, __simple,
390 _Category>::__copy_m(__first, __last, __result);
391 }
392
393 // Helpers for streambuf iterators (either istream or ostream).
394 // NB: avoid including <iosfwd>, relatively large.
395 template<typename _CharT>
396 struct char_traits;
397
398 template<typename _CharT, typename _Traits>
399 class istreambuf_iterator;
400
401 template<typename _CharT, typename _Traits>
402 class ostreambuf_iterator;
403
404 template<bool _IsMove, typename _CharT>
405 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
406 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
407 __copy_move_a2(_CharT*, _CharT*,
408 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
409
410 template<bool _IsMove, typename _CharT>
411 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
412 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
413 __copy_move_a2(const _CharT*, const _CharT*,
414 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
415
416 template<bool _IsMove, typename _CharT>
417 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
418 _CharT*>::__type
419 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
420 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
421
422 template<bool _IsMove, typename _II, typename _OI>
423 inline _OI
424 __copy_move_a2(_II __first, _II __last, _OI __result)
425 {
426 return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
427 std::__niter_base(__last),
428 std::__niter_base(__result)));
429 }
430
431 /**
432 * @brief Copies the range [first,last) into result.
433 * @ingroup mutating_algorithms
434 * @param __first An input iterator.
435 * @param __last An input iterator.
436 * @param __result An output iterator.
437 * @return result + (first - last)
438 *
439 * This inline function will boil down to a call to @c memmove whenever
440 * possible. Failing that, if random access iterators are passed, then the
441 * loop count will be known (and therefore a candidate for compiler
442 * optimizations such as unrolling). Result may not be contained within
443 * [first,last); the copy_backward function should be used instead.
444 *
445 * Note that the end of the output range is permitted to be contained
446 * within [first,last).
447 */
448 template<typename _II, typename _OI>
449 inline _OI
450 copy(_II __first, _II __last, _OI __result)
451 {
452 // concept requirements
453 __glibcxx_function_requires(_InputIteratorConcept<_II>)
454 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
455 typename iterator_traits<_II>::value_type>)
456 __glibcxx_requires_valid_range(__first, __last);
457
458 return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
459 (std::__miter_base(__first), std::__miter_base(__last),
460 __result));
461 }
462
463 #if __cplusplus >= 201103L
464 /**
465 * @brief Moves the range [first,last) into result.
466 * @ingroup mutating_algorithms
467 * @param __first An input iterator.
468 * @param __last An input iterator.
469 * @param __result An output iterator.
470 * @return result + (first - last)
471 *
472 * This inline function will boil down to a call to @c memmove whenever
473 * possible. Failing that, if random access iterators are passed, then the
474 * loop count will be known (and therefore a candidate for compiler
475 * optimizations such as unrolling). Result may not be contained within
476 * [first,last); the move_backward function should be used instead.
477 *
478 * Note that the end of the output range is permitted to be contained
479 * within [first,last).
480 */
481 template<typename _II, typename _OI>
482 inline _OI
483 move(_II __first, _II __last, _OI __result)
484 {
485 // concept requirements
486 __glibcxx_function_requires(_InputIteratorConcept<_II>)
487 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
488 typename iterator_traits<_II>::value_type>)
489 __glibcxx_requires_valid_range(__first, __last);
490
491 return std::__copy_move_a2<true>(std::__miter_base(__first),
492 std::__miter_base(__last), __result);
493 }
494
495 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
496 #else
497 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
498 #endif
499
500 template<bool, bool, typename>
501 struct __copy_move_backward
502 {
503 template<typename _BI1, typename _BI2>
504 static _BI2
505 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
506 {
507 while (__first != __last)
508 *--__result = *--__last;
509 return __result;
510 }
511 };
512
513 #if __cplusplus >= 201103L
514 template<typename _Category>
515 struct __copy_move_backward<true, false, _Category>
516 {
517 template<typename _BI1, typename _BI2>
518 static _BI2
519 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
520 {
521 while (__first != __last)
522 *--__result = std::move(*--__last);
523 return __result;
524 }
525 };
526 #endif
527
528 template<>
529 struct __copy_move_backward<false, false, random_access_iterator_tag>
530 {
531 template<typename _BI1, typename _BI2>
532 static _BI2
533 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
534 {
535 typename iterator_traits<_BI1>::difference_type __n;
536 for (__n = __last - __first; __n > 0; --__n)
537 *--__result = *--__last;
538 return __result;
539 }
540 };
541
542 #if __cplusplus >= 201103L
543 template<>
544 struct __copy_move_backward<true, false, random_access_iterator_tag>
545 {
546 template<typename _BI1, typename _BI2>
547 static _BI2
548 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
549 {
550 typename iterator_traits<_BI1>::difference_type __n;
551 for (__n = __last - __first; __n > 0; --__n)
552 *--__result = std::move(*--__last);
553 return __result;
554 }
555 };
556 #endif
557
558 template<bool _IsMove>
559 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
560 {
561 template<typename _Tp>
562 static _Tp*
563 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
564 {
565 #if __cplusplus >= 201103L
566 // trivial types can have deleted assignment
567 static_assert( is_copy_assignable<_Tp>::value,
568 "type is not assignable" );
569 #endif
570 const ptrdiff_t _Num = __last - __first;
571 if (_Num)
572 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
573 return __result - _Num;
574 }
575 };
576
577 template<bool _IsMove, typename _BI1, typename _BI2>
578 inline _BI2
579 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
580 {
581 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
582 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
583 typedef typename iterator_traits<_BI1>::iterator_category _Category;
584 const bool __simple = (__is_trivial(_ValueType1)
585 && __is_pointer<_BI1>::__value
586 && __is_pointer<_BI2>::__value
587 && __are_same<_ValueType1, _ValueType2>::__value);
588
589 return std::__copy_move_backward<_IsMove, __simple,
590 _Category>::__copy_move_b(__first,
591 __last,
592 __result);
593 }
594
595 template<bool _IsMove, typename _BI1, typename _BI2>
596 inline _BI2
597 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
598 {
599 return _BI2(std::__copy_move_backward_a<_IsMove>
600 (std::__niter_base(__first), std::__niter_base(__last),
601 std::__niter_base(__result)));
602 }
603
604 /**
605 * @brief Copies the range [first,last) into result.
606 * @ingroup mutating_algorithms
607 * @param __first A bidirectional iterator.
608 * @param __last A bidirectional iterator.
609 * @param __result A bidirectional iterator.
610 * @return result - (first - last)
611 *
612 * The function has the same effect as copy, but starts at the end of the
613 * range and works its way to the start, returning the start of the result.
614 * This inline function will boil down to a call to @c memmove whenever
615 * possible. Failing that, if random access iterators are passed, then the
616 * loop count will be known (and therefore a candidate for compiler
617 * optimizations such as unrolling).
618 *
619 * Result may not be in the range (first,last]. Use copy instead. Note
620 * that the start of the output range may overlap [first,last).
621 */
622 template<typename _BI1, typename _BI2>
623 inline _BI2
624 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
625 {
626 // concept requirements
627 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
628 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
629 __glibcxx_function_requires(_ConvertibleConcept<
630 typename iterator_traits<_BI1>::value_type,
631 typename iterator_traits<_BI2>::value_type>)
632 __glibcxx_requires_valid_range(__first, __last);
633
634 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
635 (std::__miter_base(__first), std::__miter_base(__last),
636 __result));
637 }
638
639 #if __cplusplus >= 201103L
640 /**
641 * @brief Moves the range [first,last) into result.
642 * @ingroup mutating_algorithms
643 * @param __first A bidirectional iterator.
644 * @param __last A bidirectional iterator.
645 * @param __result A bidirectional iterator.
646 * @return result - (first - last)
647 *
648 * The function has the same effect as move, but starts at the end of the
649 * range and works its way to the start, returning the start of the result.
650 * This inline function will boil down to a call to @c memmove whenever
651 * possible. Failing that, if random access iterators are passed, then the
652 * loop count will be known (and therefore a candidate for compiler
653 * optimizations such as unrolling).
654 *
655 * Result may not be in the range (first,last]. Use move instead. Note
656 * that the start of the output range may overlap [first,last).
657 */
658 template<typename _BI1, typename _BI2>
659 inline _BI2
660 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
661 {
662 // concept requirements
663 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
664 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
665 __glibcxx_function_requires(_ConvertibleConcept<
666 typename iterator_traits<_BI1>::value_type,
667 typename iterator_traits<_BI2>::value_type>)
668 __glibcxx_requires_valid_range(__first, __last);
669
670 return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
671 std::__miter_base(__last),
672 __result);
673 }
674
675 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
676 #else
677 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
678 #endif
679
680 template<typename _ForwardIterator, typename _Tp>
681 inline typename
682 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
683 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
684 const _Tp& __value)
685 {
686 for (; __first != __last; ++__first)
687 *__first = __value;
688 }
689
690 template<typename _ForwardIterator, typename _Tp>
691 inline typename
692 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
693 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
694 const _Tp& __value)
695 {
696 const _Tp __tmp = __value;
697 for (; __first != __last; ++__first)
698 *__first = __tmp;
699 }
700
701 // Specialization: for char types we can use memset.
702 template<typename _Tp>
703 inline typename
704 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
705 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
706 {
707 const _Tp __tmp = __c;
708 if (const size_t __len = __last - __first)
709 __builtin_memset(__first, static_cast<unsigned char>(__tmp), __len);
710 }
711
712 /**
713 * @brief Fills the range [first,last) with copies of value.
714 * @ingroup mutating_algorithms
715 * @param __first A forward iterator.
716 * @param __last A forward iterator.
717 * @param __value A reference-to-const of arbitrary type.
718 * @return Nothing.
719 *
720 * This function fills a range with copies of the same value. For char
721 * types filling contiguous areas of memory, this becomes an inline call
722 * to @c memset or @c wmemset.
723 */
724 template<typename _ForwardIterator, typename _Tp>
725 inline void
726 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
727 {
728 // concept requirements
729 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
730 _ForwardIterator>)
731 __glibcxx_requires_valid_range(__first, __last);
732
733 std::__fill_a(std::__niter_base(__first), std::__niter_base(__last),
734 __value);
735 }
736
737 template<typename _OutputIterator, typename _Size, typename _Tp>
738 inline typename
739 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
740 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
741 {
742 for (__decltype(__n + 0) __niter = __n;
743 __niter > 0; --__niter, ++__first)
744 *__first = __value;
745 return __first;
746 }
747
748 template<typename _OutputIterator, typename _Size, typename _Tp>
749 inline typename
750 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
751 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
752 {
753 const _Tp __tmp = __value;
754 for (__decltype(__n + 0) __niter = __n;
755 __niter > 0; --__niter, ++__first)
756 *__first = __tmp;
757 return __first;
758 }
759
760 template<typename _Size, typename _Tp>
761 inline typename
762 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
763 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
764 {
765 std::__fill_a(__first, __first + __n, __c);
766 return __first + __n;
767 }
768
769 /**
770 * @brief Fills the range [first,first+n) with copies of value.
771 * @ingroup mutating_algorithms
772 * @param __first An output iterator.
773 * @param __n The count of copies to perform.
774 * @param __value A reference-to-const of arbitrary type.
775 * @return The iterator at first+n.
776 *
777 * This function fills a range with copies of the same value. For char
778 * types filling contiguous areas of memory, this becomes an inline call
779 * to @c memset or @ wmemset.
780 *
781 * _GLIBCXX_RESOLVE_LIB_DEFECTS
782 * DR 865. More algorithms that throw away information
783 */
784 template<typename _OI, typename _Size, typename _Tp>
785 inline _OI
786 fill_n(_OI __first, _Size __n, const _Tp& __value)
787 {
788 // concept requirements
789 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
790
791 return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value));
792 }
793
794 template<bool _BoolType>
795 struct __equal
796 {
797 template<typename _II1, typename _II2>
798 static bool
799 equal(_II1 __first1, _II1 __last1, _II2 __first2)
800 {
801 for (; __first1 != __last1; ++__first1, (void)++__first2)
802 if (!(*__first1 == *__first2))
803 return false;
804 return true;
805 }
806 };
807
808 template<>
809 struct __equal<true>
810 {
811 template<typename _Tp>
812 static bool
813 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
814 {
815 if (const size_t __len = (__last1 - __first1))
816 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp) * __len);
817 return true;
818 }
819 };
820
821 template<typename _II1, typename _II2>
822 inline bool
823 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
824 {
825 typedef typename iterator_traits<_II1>::value_type _ValueType1;
826 typedef typename iterator_traits<_II2>::value_type _ValueType2;
827 const bool __simple = ((__is_integer<_ValueType1>::__value
828 || __is_pointer<_ValueType1>::__value)
829 && __is_pointer<_II1>::__value
830 && __is_pointer<_II2>::__value
831 && __are_same<_ValueType1, _ValueType2>::__value);
832
833 return std::__equal<__simple>::equal(__first1, __last1, __first2);
834 }
835
836 template<typename, typename>
837 struct __lc_rai
838 {
839 template<typename _II1, typename _II2>
840 static _II1
841 __newlast1(_II1, _II1 __last1, _II2, _II2)
842 { return __last1; }
843
844 template<typename _II>
845 static bool
846 __cnd2(_II __first, _II __last)
847 { return __first != __last; }
848 };
849
850 template<>
851 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
852 {
853 template<typename _RAI1, typename _RAI2>
854 static _RAI1
855 __newlast1(_RAI1 __first1, _RAI1 __last1,
856 _RAI2 __first2, _RAI2 __last2)
857 {
858 const typename iterator_traits<_RAI1>::difference_type
859 __diff1 = __last1 - __first1;
860 const typename iterator_traits<_RAI2>::difference_type
861 __diff2 = __last2 - __first2;
862 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
863 }
864
865 template<typename _RAI>
866 static bool
867 __cnd2(_RAI, _RAI)
868 { return true; }
869 };
870
871 template<typename _II1, typename _II2, typename _Compare>
872 bool
873 __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
874 _II2 __first2, _II2 __last2,
875 _Compare __comp)
876 {
877 typedef typename iterator_traits<_II1>::iterator_category _Category1;
878 typedef typename iterator_traits<_II2>::iterator_category _Category2;
879 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
880
881 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
882 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
883 ++__first1, (void)++__first2)
884 {
885 if (__comp(__first1, __first2))
886 return true;
887 if (__comp(__first2, __first1))
888 return false;
889 }
890 return __first1 == __last1 && __first2 != __last2;
891 }
892
893 template<bool _BoolType>
894 struct __lexicographical_compare
895 {
896 template<typename _II1, typename _II2>
897 static bool __lc(_II1, _II1, _II2, _II2);
898 };
899
900 template<bool _BoolType>
901 template<typename _II1, typename _II2>
902 bool
903 __lexicographical_compare<_BoolType>::
904 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
905 {
906 return std::__lexicographical_compare_impl(__first1, __last1,
907 __first2, __last2,
908 __gnu_cxx::__ops::__iter_less_iter());
909 }
910
911 template<>
912 struct __lexicographical_compare<true>
913 {
914 template<typename _Tp, typename _Up>
915 static bool
916 __lc(const _Tp* __first1, const _Tp* __last1,
917 const _Up* __first2, const _Up* __last2)
918 {
919 const size_t __len1 = __last1 - __first1;
920 const size_t __len2 = __last2 - __first2;
921 if (const size_t __len = std::min(__len1, __len2))
922 if (int __result = __builtin_memcmp(__first1, __first2, __len))
923 return __result < 0;
924 return __len1 < __len2;
925 }
926 };
927
928 template<typename _II1, typename _II2>
929 inline bool
930 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
931 _II2 __first2, _II2 __last2)
932 {
933 typedef typename iterator_traits<_II1>::value_type _ValueType1;
934 typedef typename iterator_traits<_II2>::value_type _ValueType2;
935 const bool __simple =
936 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
937 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
938 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
939 && __is_pointer<_II1>::__value
940 && __is_pointer<_II2>::__value);
941
942 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
943 __first2, __last2);
944 }
945
946 template<typename _ForwardIterator, typename _Tp, typename _Compare>
947 _ForwardIterator
948 __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
949 const _Tp& __val, _Compare __comp)
950 {
951 typedef typename iterator_traits<_ForwardIterator>::difference_type
952 _DistanceType;
953
954 _DistanceType __len = std::distance(__first, __last);
955
956 while (__len > 0)
957 {
958 _DistanceType __half = __len >> 1;
959 _ForwardIterator __middle = __first;
960 std::advance(__middle, __half);
961 if (__comp(__middle, __val))
962 {
963 __first = __middle;
964 ++__first;
965 __len = __len - __half - 1;
966 }
967 else
968 __len = __half;
969 }
970 return __first;
971 }
972
973 /**
974 * @brief Finds the first position in which @a val could be inserted
975 * without changing the ordering.
976 * @param __first An iterator.
977 * @param __last Another iterator.
978 * @param __val The search term.
979 * @return An iterator pointing to the first element <em>not less
980 * than</em> @a val, or end() if every element is less than
981 * @a val.
982 * @ingroup binary_search_algorithms
983 */
984 template<typename _ForwardIterator, typename _Tp>
985 inline _ForwardIterator
986 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
987 const _Tp& __val)
988 {
989 // concept requirements
990 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
991 __glibcxx_function_requires(_LessThanOpConcept<
992 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
993 __glibcxx_requires_partitioned_lower(__first, __last, __val);
994
995 return std::__lower_bound(__first, __last, __val,
996 __gnu_cxx::__ops::__iter_less_val());
997 }
998
999 /// This is a helper function for the sort routines and for random.tcc.
1000 // Precondition: __n > 0.
1001 inline _GLIBCXX_CONSTEXPR int
1002 __lg(int __n)
1003 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1004
1005 inline _GLIBCXX_CONSTEXPR unsigned
1006 __lg(unsigned __n)
1007 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1008
1009 inline _GLIBCXX_CONSTEXPR long
1010 __lg(long __n)
1011 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1012
1013 inline _GLIBCXX_CONSTEXPR unsigned long
1014 __lg(unsigned long __n)
1015 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1016
1017 inline _GLIBCXX_CONSTEXPR long long
1018 __lg(long long __n)
1019 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1020
1021 inline _GLIBCXX_CONSTEXPR unsigned long long
1022 __lg(unsigned long long __n)
1023 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1024
1025 _GLIBCXX_END_NAMESPACE_VERSION
1026
1027 _GLIBCXX_BEGIN_NAMESPACE_ALGO
1028
1029 /**
1030 * @brief Tests a range for element-wise equality.
1031 * @ingroup non_mutating_algorithms
1032 * @param __first1 An input iterator.
1033 * @param __last1 An input iterator.
1034 * @param __first2 An input iterator.
1035 * @return A boolean true or false.
1036 *
1037 * This compares the elements of two ranges using @c == and returns true or
1038 * false depending on whether all of the corresponding elements of the
1039 * ranges are equal.
1040 */
1041 template<typename _II1, typename _II2>
1042 inline bool
1043 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1044 {
1045 // concept requirements
1046 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1047 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1048 __glibcxx_function_requires(_EqualOpConcept<
1049 typename iterator_traits<_II1>::value_type,
1050 typename iterator_traits<_II2>::value_type>)
1051 __glibcxx_requires_valid_range(__first1, __last1);
1052
1053 return std::__equal_aux(std::__niter_base(__first1),
1054 std::__niter_base(__last1),
1055 std::__niter_base(__first2));
1056 }
1057
1058 /**
1059 * @brief Tests a range for element-wise equality.
1060 * @ingroup non_mutating_algorithms
1061 * @param __first1 An input iterator.
1062 * @param __last1 An input iterator.
1063 * @param __first2 An input iterator.
1064 * @param __binary_pred A binary predicate @link functors
1065 * functor@endlink.
1066 * @return A boolean true or false.
1067 *
1068 * This compares the elements of two ranges using the binary_pred
1069 * parameter, and returns true or
1070 * false depending on whether all of the corresponding elements of the
1071 * ranges are equal.
1072 */
1073 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1074 inline bool
1075 equal(_IIter1 __first1, _IIter1 __last1,
1076 _IIter2 __first2, _BinaryPredicate __binary_pred)
1077 {
1078 // concept requirements
1079 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1080 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1081 __glibcxx_requires_valid_range(__first1, __last1);
1082
1083 for (; __first1 != __last1; ++__first1, (void)++__first2)
1084 if (!bool(__binary_pred(*__first1, *__first2)))
1085 return false;
1086 return true;
1087 }
1088
1089 #if __cplusplus > 201103L
1090
1091 #define __cpp_lib_robust_nonmodifying_seq_ops 201304
1092
1093 /**
1094 * @brief Tests a range for element-wise equality.
1095 * @ingroup non_mutating_algorithms
1096 * @param __first1 An input iterator.
1097 * @param __last1 An input iterator.
1098 * @param __first2 An input iterator.
1099 * @param __last2 An input iterator.
1100 * @return A boolean true or false.
1101 *
1102 * This compares the elements of two ranges using @c == and returns true or
1103 * false depending on whether all of the corresponding elements of the
1104 * ranges are equal.
1105 */
1106 template<typename _II1, typename _II2>
1107 inline bool
1108 equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1109 {
1110 // concept requirements
1111 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1112 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1113 __glibcxx_function_requires(_EqualOpConcept<
1114 typename iterator_traits<_II1>::value_type,
1115 typename iterator_traits<_II2>::value_type>)
1116 __glibcxx_requires_valid_range(__first1, __last1);
1117 __glibcxx_requires_valid_range(__first2, __last2);
1118
1119 using _RATag = random_access_iterator_tag;
1120 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1121 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1122 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1123 if (_RAIters())
1124 {
1125 auto __d1 = std::distance(__first1, __last1);
1126 auto __d2 = std::distance(__first2, __last2);
1127 if (__d1 != __d2)
1128 return false;
1129 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
1130 }
1131
1132 for (; __first1 != __last1 && __first2 != __last2;
1133 ++__first1, (void)++__first2)
1134 if (!(*__first1 == *__first2))
1135 return false;
1136 return __first1 == __last1 && __first2 == __last2;
1137 }
1138
1139 /**
1140 * @brief Tests a range for element-wise equality.
1141 * @ingroup non_mutating_algorithms
1142 * @param __first1 An input iterator.
1143 * @param __last1 An input iterator.
1144 * @param __first2 An input iterator.
1145 * @param __last2 An input iterator.
1146 * @param __binary_pred A binary predicate @link functors
1147 * functor@endlink.
1148 * @return A boolean true or false.
1149 *
1150 * This compares the elements of two ranges using the binary_pred
1151 * parameter, and returns true or
1152 * false depending on whether all of the corresponding elements of the
1153 * ranges are equal.
1154 */
1155 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1156 inline bool
1157 equal(_IIter1 __first1, _IIter1 __last1,
1158 _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
1159 {
1160 // concept requirements
1161 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1162 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1163 __glibcxx_requires_valid_range(__first1, __last1);
1164 __glibcxx_requires_valid_range(__first2, __last2);
1165
1166 using _RATag = random_access_iterator_tag;
1167 using _Cat1 = typename iterator_traits<_IIter1>::iterator_category;
1168 using _Cat2 = typename iterator_traits<_IIter2>::iterator_category;
1169 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1170 if (_RAIters())
1171 {
1172 auto __d1 = std::distance(__first1, __last1);
1173 auto __d2 = std::distance(__first2, __last2);
1174 if (__d1 != __d2)
1175 return false;
1176 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
1177 __binary_pred);
1178 }
1179
1180 for (; __first1 != __last1 && __first2 != __last2;
1181 ++__first1, (void)++__first2)
1182 if (!bool(__binary_pred(*__first1, *__first2)))
1183 return false;
1184 return __first1 == __last1 && __first2 == __last2;
1185 }
1186 #endif
1187
1188 /**
1189 * @brief Performs @b dictionary comparison on ranges.
1190 * @ingroup sorting_algorithms
1191 * @param __first1 An input iterator.
1192 * @param __last1 An input iterator.
1193 * @param __first2 An input iterator.
1194 * @param __last2 An input iterator.
1195 * @return A boolean true or false.
1196 *
1197 * <em>Returns true if the sequence of elements defined by the range
1198 * [first1,last1) is lexicographically less than the sequence of elements
1199 * defined by the range [first2,last2). Returns false otherwise.</em>
1200 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1201 * then this is an inline call to @c memcmp.
1202 */
1203 template<typename _II1, typename _II2>
1204 inline bool
1205 lexicographical_compare(_II1 __first1, _II1 __last1,
1206 _II2 __first2, _II2 __last2)
1207 {
1208 #ifdef _GLIBCXX_CONCEPT_CHECKS
1209 // concept requirements
1210 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1211 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1212 #endif
1213 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1214 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1215 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1216 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1217 __glibcxx_requires_valid_range(__first1, __last1);
1218 __glibcxx_requires_valid_range(__first2, __last2);
1219
1220 return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1221 std::__niter_base(__last1),
1222 std::__niter_base(__first2),
1223 std::__niter_base(__last2));
1224 }
1225
1226 /**
1227 * @brief Performs @b dictionary comparison on ranges.
1228 * @ingroup sorting_algorithms
1229 * @param __first1 An input iterator.
1230 * @param __last1 An input iterator.
1231 * @param __first2 An input iterator.
1232 * @param __last2 An input iterator.
1233 * @param __comp A @link comparison_functors comparison functor@endlink.
1234 * @return A boolean true or false.
1235 *
1236 * The same as the four-parameter @c lexicographical_compare, but uses the
1237 * comp parameter instead of @c <.
1238 */
1239 template<typename _II1, typename _II2, typename _Compare>
1240 inline bool
1241 lexicographical_compare(_II1 __first1, _II1 __last1,
1242 _II2 __first2, _II2 __last2, _Compare __comp)
1243 {
1244 // concept requirements
1245 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1246 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1247 __glibcxx_requires_valid_range(__first1, __last1);
1248 __glibcxx_requires_valid_range(__first2, __last2);
1249
1250 return std::__lexicographical_compare_impl
1251 (__first1, __last1, __first2, __last2,
1252 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1253 }
1254
1255 template<typename _InputIterator1, typename _InputIterator2,
1256 typename _BinaryPredicate>
1257 pair<_InputIterator1, _InputIterator2>
1258 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1259 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1260 {
1261 while (__first1 != __last1 && __binary_pred(__first1, __first2))
1262 {
1263 ++__first1;
1264 ++__first2;
1265 }
1266 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1267 }
1268
1269 /**
1270 * @brief Finds the places in ranges which don't match.
1271 * @ingroup non_mutating_algorithms
1272 * @param __first1 An input iterator.
1273 * @param __last1 An input iterator.
1274 * @param __first2 An input iterator.
1275 * @return A pair of iterators pointing to the first mismatch.
1276 *
1277 * This compares the elements of two ranges using @c == and returns a pair
1278 * of iterators. The first iterator points into the first range, the
1279 * second iterator points into the second range, and the elements pointed
1280 * to by the iterators are not equal.
1281 */
1282 template<typename _InputIterator1, typename _InputIterator2>
1283 inline pair<_InputIterator1, _InputIterator2>
1284 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1285 _InputIterator2 __first2)
1286 {
1287 // concept requirements
1288 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1289 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1290 __glibcxx_function_requires(_EqualOpConcept<
1291 typename iterator_traits<_InputIterator1>::value_type,
1292 typename iterator_traits<_InputIterator2>::value_type>)
1293 __glibcxx_requires_valid_range(__first1, __last1);
1294
1295 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1296 __gnu_cxx::__ops::__iter_equal_to_iter());
1297 }
1298
1299 /**
1300 * @brief Finds the places in ranges which don't match.
1301 * @ingroup non_mutating_algorithms
1302 * @param __first1 An input iterator.
1303 * @param __last1 An input iterator.
1304 * @param __first2 An input iterator.
1305 * @param __binary_pred A binary predicate @link functors
1306 * functor@endlink.
1307 * @return A pair of iterators pointing to the first mismatch.
1308 *
1309 * This compares the elements of two ranges using the binary_pred
1310 * parameter, and returns a pair
1311 * of iterators. The first iterator points into the first range, the
1312 * second iterator points into the second range, and the elements pointed
1313 * to by the iterators are not equal.
1314 */
1315 template<typename _InputIterator1, typename _InputIterator2,
1316 typename _BinaryPredicate>
1317 inline pair<_InputIterator1, _InputIterator2>
1318 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1319 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1320 {
1321 // concept requirements
1322 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1323 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1324 __glibcxx_requires_valid_range(__first1, __last1);
1325
1326 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1327 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1328 }
1329
1330 #if __cplusplus > 201103L
1331
1332 template<typename _InputIterator1, typename _InputIterator2,
1333 typename _BinaryPredicate>
1334 pair<_InputIterator1, _InputIterator2>
1335 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1336 _InputIterator2 __first2, _InputIterator2 __last2,
1337 _BinaryPredicate __binary_pred)
1338 {
1339 while (__first1 != __last1 && __first2 != __last2
1340 && __binary_pred(__first1, __first2))
1341 {
1342 ++__first1;
1343 ++__first2;
1344 }
1345 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1346 }
1347
1348 /**
1349 * @brief Finds the places in ranges which don't match.
1350 * @ingroup non_mutating_algorithms
1351 * @param __first1 An input iterator.
1352 * @param __last1 An input iterator.
1353 * @param __first2 An input iterator.
1354 * @param __last2 An input iterator.
1355 * @return A pair of iterators pointing to the first mismatch.
1356 *
1357 * This compares the elements of two ranges using @c == and returns a pair
1358 * of iterators. The first iterator points into the first range, the
1359 * second iterator points into the second range, and the elements pointed
1360 * to by the iterators are not equal.
1361 */
1362 template<typename _InputIterator1, typename _InputIterator2>
1363 inline pair<_InputIterator1, _InputIterator2>
1364 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1365 _InputIterator2 __first2, _InputIterator2 __last2)
1366 {
1367 // concept requirements
1368 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1369 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1370 __glibcxx_function_requires(_EqualOpConcept<
1371 typename iterator_traits<_InputIterator1>::value_type,
1372 typename iterator_traits<_InputIterator2>::value_type>)
1373 __glibcxx_requires_valid_range(__first1, __last1);
1374 __glibcxx_requires_valid_range(__first2, __last2);
1375
1376 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1377 __gnu_cxx::__ops::__iter_equal_to_iter());
1378 }
1379
1380 /**
1381 * @brief Finds the places in ranges which don't match.
1382 * @ingroup non_mutating_algorithms
1383 * @param __first1 An input iterator.
1384 * @param __last1 An input iterator.
1385 * @param __first2 An input iterator.
1386 * @param __last2 An input iterator.
1387 * @param __binary_pred A binary predicate @link functors
1388 * functor@endlink.
1389 * @return A pair of iterators pointing to the first mismatch.
1390 *
1391 * This compares the elements of two ranges using the binary_pred
1392 * parameter, and returns a pair
1393 * of iterators. The first iterator points into the first range, the
1394 * second iterator points into the second range, and the elements pointed
1395 * to by the iterators are not equal.
1396 */
1397 template<typename _InputIterator1, typename _InputIterator2,
1398 typename _BinaryPredicate>
1399 inline pair<_InputIterator1, _InputIterator2>
1400 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1401 _InputIterator2 __first2, _InputIterator2 __last2,
1402 _BinaryPredicate __binary_pred)
1403 {
1404 // concept requirements
1405 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1406 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1407 __glibcxx_requires_valid_range(__first1, __last1);
1408 __glibcxx_requires_valid_range(__first2, __last2);
1409
1410 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1411 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1412 }
1413 #endif
1414
1415 _GLIBCXX_END_NAMESPACE_ALGO
1416 } // namespace std
1417
1418 // NB: This file is included within many other C++ includes, as a way
1419 // of getting the base algorithms. So, make sure that parallel bits
1420 // come in too if requested.
1421 #ifdef _GLIBCXX_PARALLEL
1422 # include <parallel/algobase.h>
1423 #endif
1424
1425 #endif