]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_algobase.h
re PR libstdc++/66327 (-fsanitize=nonnull-attribute errors in stl_algobase.h)
[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 __builtin_memset(__first, static_cast<unsigned char>(__tmp),
709 __last - __first);
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 (__first1 == 0 || __first2 == 0)
816 return __first1 == __last1;
817
818 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
819 * (__last1 - __first1));
820 }
821 };
822
823 template<typename _II1, typename _II2>
824 inline bool
825 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
826 {
827 typedef typename iterator_traits<_II1>::value_type _ValueType1;
828 typedef typename iterator_traits<_II2>::value_type _ValueType2;
829 const bool __simple = ((__is_integer<_ValueType1>::__value
830 || __is_pointer<_ValueType1>::__value)
831 && __is_pointer<_II1>::__value
832 && __is_pointer<_II2>::__value
833 && __are_same<_ValueType1, _ValueType2>::__value);
834
835 return std::__equal<__simple>::equal(__first1, __last1, __first2);
836 }
837
838 template<typename, typename>
839 struct __lc_rai
840 {
841 template<typename _II1, typename _II2>
842 static _II1
843 __newlast1(_II1, _II1 __last1, _II2, _II2)
844 { return __last1; }
845
846 template<typename _II>
847 static bool
848 __cnd2(_II __first, _II __last)
849 { return __first != __last; }
850 };
851
852 template<>
853 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
854 {
855 template<typename _RAI1, typename _RAI2>
856 static _RAI1
857 __newlast1(_RAI1 __first1, _RAI1 __last1,
858 _RAI2 __first2, _RAI2 __last2)
859 {
860 const typename iterator_traits<_RAI1>::difference_type
861 __diff1 = __last1 - __first1;
862 const typename iterator_traits<_RAI2>::difference_type
863 __diff2 = __last2 - __first2;
864 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
865 }
866
867 template<typename _RAI>
868 static bool
869 __cnd2(_RAI, _RAI)
870 { return true; }
871 };
872
873 template<typename _II1, typename _II2, typename _Compare>
874 bool
875 __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
876 _II2 __first2, _II2 __last2,
877 _Compare __comp)
878 {
879 typedef typename iterator_traits<_II1>::iterator_category _Category1;
880 typedef typename iterator_traits<_II2>::iterator_category _Category2;
881 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
882
883 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
884 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
885 ++__first1, (void)++__first2)
886 {
887 if (__comp(__first1, __first2))
888 return true;
889 if (__comp(__first2, __first1))
890 return false;
891 }
892 return __first1 == __last1 && __first2 != __last2;
893 }
894
895 template<bool _BoolType>
896 struct __lexicographical_compare
897 {
898 template<typename _II1, typename _II2>
899 static bool __lc(_II1, _II1, _II2, _II2);
900 };
901
902 template<bool _BoolType>
903 template<typename _II1, typename _II2>
904 bool
905 __lexicographical_compare<_BoolType>::
906 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
907 {
908 return std::__lexicographical_compare_impl(__first1, __last1,
909 __first2, __last2,
910 __gnu_cxx::__ops::__iter_less_iter());
911 }
912
913 template<>
914 struct __lexicographical_compare<true>
915 {
916 template<typename _Tp, typename _Up>
917 static bool
918 __lc(const _Tp* __first1, const _Tp* __last1,
919 const _Up* __first2, const _Up* __last2)
920 {
921 const size_t __len1 = __last1 - __first1;
922 const size_t __len2 = __last2 - __first2;
923 if (__len1 && __len2)
924 {
925 if (int __result = __builtin_memcmp(__first1, __first2,
926 std::min(__len1, __len2)))
927 {
928 return __result < 0;
929 }
930 }
931 return __len1 < __len2;
932 }
933 };
934
935 template<typename _II1, typename _II2>
936 inline bool
937 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
938 _II2 __first2, _II2 __last2)
939 {
940 typedef typename iterator_traits<_II1>::value_type _ValueType1;
941 typedef typename iterator_traits<_II2>::value_type _ValueType2;
942 const bool __simple =
943 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
944 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
945 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
946 && __is_pointer<_II1>::__value
947 && __is_pointer<_II2>::__value);
948
949 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
950 __first2, __last2);
951 }
952
953 template<typename _ForwardIterator, typename _Tp, typename _Compare>
954 _ForwardIterator
955 __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
956 const _Tp& __val, _Compare __comp)
957 {
958 typedef typename iterator_traits<_ForwardIterator>::difference_type
959 _DistanceType;
960
961 _DistanceType __len = std::distance(__first, __last);
962
963 while (__len > 0)
964 {
965 _DistanceType __half = __len >> 1;
966 _ForwardIterator __middle = __first;
967 std::advance(__middle, __half);
968 if (__comp(__middle, __val))
969 {
970 __first = __middle;
971 ++__first;
972 __len = __len - __half - 1;
973 }
974 else
975 __len = __half;
976 }
977 return __first;
978 }
979
980 /**
981 * @brief Finds the first position in which @a val could be inserted
982 * without changing the ordering.
983 * @param __first An iterator.
984 * @param __last Another iterator.
985 * @param __val The search term.
986 * @return An iterator pointing to the first element <em>not less
987 * than</em> @a val, or end() if every element is less than
988 * @a val.
989 * @ingroup binary_search_algorithms
990 */
991 template<typename _ForwardIterator, typename _Tp>
992 inline _ForwardIterator
993 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
994 const _Tp& __val)
995 {
996 // concept requirements
997 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
998 __glibcxx_function_requires(_LessThanOpConcept<
999 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1000 __glibcxx_requires_partitioned_lower(__first, __last, __val);
1001
1002 return std::__lower_bound(__first, __last, __val,
1003 __gnu_cxx::__ops::__iter_less_val());
1004 }
1005
1006 /// This is a helper function for the sort routines and for random.tcc.
1007 // Precondition: __n > 0.
1008 inline _GLIBCXX_CONSTEXPR int
1009 __lg(int __n)
1010 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1011
1012 inline _GLIBCXX_CONSTEXPR unsigned
1013 __lg(unsigned __n)
1014 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1015
1016 inline _GLIBCXX_CONSTEXPR long
1017 __lg(long __n)
1018 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1019
1020 inline _GLIBCXX_CONSTEXPR unsigned long
1021 __lg(unsigned long __n)
1022 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1023
1024 inline _GLIBCXX_CONSTEXPR long long
1025 __lg(long long __n)
1026 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1027
1028 inline _GLIBCXX_CONSTEXPR unsigned long long
1029 __lg(unsigned long long __n)
1030 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1031
1032 _GLIBCXX_END_NAMESPACE_VERSION
1033
1034 _GLIBCXX_BEGIN_NAMESPACE_ALGO
1035
1036 /**
1037 * @brief Tests a range for element-wise equality.
1038 * @ingroup non_mutating_algorithms
1039 * @param __first1 An input iterator.
1040 * @param __last1 An input iterator.
1041 * @param __first2 An input iterator.
1042 * @return A boolean true or false.
1043 *
1044 * This compares the elements of two ranges using @c == and returns true or
1045 * false depending on whether all of the corresponding elements of the
1046 * ranges are equal.
1047 */
1048 template<typename _II1, typename _II2>
1049 inline bool
1050 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1051 {
1052 // concept requirements
1053 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1054 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1055 __glibcxx_function_requires(_EqualOpConcept<
1056 typename iterator_traits<_II1>::value_type,
1057 typename iterator_traits<_II2>::value_type>)
1058 __glibcxx_requires_valid_range(__first1, __last1);
1059
1060 return std::__equal_aux(std::__niter_base(__first1),
1061 std::__niter_base(__last1),
1062 std::__niter_base(__first2));
1063 }
1064
1065 /**
1066 * @brief Tests a range for element-wise equality.
1067 * @ingroup non_mutating_algorithms
1068 * @param __first1 An input iterator.
1069 * @param __last1 An input iterator.
1070 * @param __first2 An input iterator.
1071 * @param __binary_pred A binary predicate @link functors
1072 * functor@endlink.
1073 * @return A boolean true or false.
1074 *
1075 * This compares the elements of two ranges using the binary_pred
1076 * parameter, and returns true or
1077 * false depending on whether all of the corresponding elements of the
1078 * ranges are equal.
1079 */
1080 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1081 inline bool
1082 equal(_IIter1 __first1, _IIter1 __last1,
1083 _IIter2 __first2, _BinaryPredicate __binary_pred)
1084 {
1085 // concept requirements
1086 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1087 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1088 __glibcxx_requires_valid_range(__first1, __last1);
1089
1090 for (; __first1 != __last1; ++__first1, (void)++__first2)
1091 if (!bool(__binary_pred(*__first1, *__first2)))
1092 return false;
1093 return true;
1094 }
1095
1096 #if __cplusplus > 201103L
1097
1098 #define __cpp_lib_robust_nonmodifying_seq_ops 201304
1099
1100 /**
1101 * @brief Tests a range for element-wise equality.
1102 * @ingroup non_mutating_algorithms
1103 * @param __first1 An input iterator.
1104 * @param __last1 An input iterator.
1105 * @param __first2 An input iterator.
1106 * @param __last2 An input iterator.
1107 * @return A boolean true or false.
1108 *
1109 * This compares the elements of two ranges using @c == and returns true or
1110 * false depending on whether all of the corresponding elements of the
1111 * ranges are equal.
1112 */
1113 template<typename _II1, typename _II2>
1114 inline bool
1115 equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1116 {
1117 // concept requirements
1118 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1119 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1120 __glibcxx_function_requires(_EqualOpConcept<
1121 typename iterator_traits<_II1>::value_type,
1122 typename iterator_traits<_II2>::value_type>)
1123 __glibcxx_requires_valid_range(__first1, __last1);
1124 __glibcxx_requires_valid_range(__first2, __last2);
1125
1126 using _RATag = random_access_iterator_tag;
1127 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1128 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1129 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1130 if (_RAIters())
1131 {
1132 auto __d1 = std::distance(__first1, __last1);
1133 auto __d2 = std::distance(__first2, __last2);
1134 if (__d1 != __d2)
1135 return false;
1136 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
1137 }
1138
1139 for (; __first1 != __last1 && __first2 != __last2;
1140 ++__first1, (void)++__first2)
1141 if (!(*__first1 == *__first2))
1142 return false;
1143 return __first1 == __last1 && __first2 == __last2;
1144 }
1145
1146 /**
1147 * @brief Tests a range for element-wise equality.
1148 * @ingroup non_mutating_algorithms
1149 * @param __first1 An input iterator.
1150 * @param __last1 An input iterator.
1151 * @param __first2 An input iterator.
1152 * @param __last2 An input iterator.
1153 * @param __binary_pred A binary predicate @link functors
1154 * functor@endlink.
1155 * @return A boolean true or false.
1156 *
1157 * This compares the elements of two ranges using the binary_pred
1158 * parameter, and returns true or
1159 * false depending on whether all of the corresponding elements of the
1160 * ranges are equal.
1161 */
1162 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1163 inline bool
1164 equal(_IIter1 __first1, _IIter1 __last1,
1165 _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
1166 {
1167 // concept requirements
1168 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1169 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1170 __glibcxx_requires_valid_range(__first1, __last1);
1171 __glibcxx_requires_valid_range(__first2, __last2);
1172
1173 using _RATag = random_access_iterator_tag;
1174 using _Cat1 = typename iterator_traits<_IIter1>::iterator_category;
1175 using _Cat2 = typename iterator_traits<_IIter2>::iterator_category;
1176 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1177 if (_RAIters())
1178 {
1179 auto __d1 = std::distance(__first1, __last1);
1180 auto __d2 = std::distance(__first2, __last2);
1181 if (__d1 != __d2)
1182 return false;
1183 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
1184 __binary_pred);
1185 }
1186
1187 for (; __first1 != __last1 && __first2 != __last2;
1188 ++__first1, (void)++__first2)
1189 if (!bool(__binary_pred(*__first1, *__first2)))
1190 return false;
1191 return __first1 == __last1 && __first2 == __last2;
1192 }
1193 #endif
1194
1195 /**
1196 * @brief Performs @b dictionary comparison on ranges.
1197 * @ingroup sorting_algorithms
1198 * @param __first1 An input iterator.
1199 * @param __last1 An input iterator.
1200 * @param __first2 An input iterator.
1201 * @param __last2 An input iterator.
1202 * @return A boolean true or false.
1203 *
1204 * <em>Returns true if the sequence of elements defined by the range
1205 * [first1,last1) is lexicographically less than the sequence of elements
1206 * defined by the range [first2,last2). Returns false otherwise.</em>
1207 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1208 * then this is an inline call to @c memcmp.
1209 */
1210 template<typename _II1, typename _II2>
1211 inline bool
1212 lexicographical_compare(_II1 __first1, _II1 __last1,
1213 _II2 __first2, _II2 __last2)
1214 {
1215 #ifdef _GLIBCXX_CONCEPT_CHECKS
1216 // concept requirements
1217 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1218 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1219 #endif
1220 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1221 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1222 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1223 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1224 __glibcxx_requires_valid_range(__first1, __last1);
1225 __glibcxx_requires_valid_range(__first2, __last2);
1226
1227 return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1228 std::__niter_base(__last1),
1229 std::__niter_base(__first2),
1230 std::__niter_base(__last2));
1231 }
1232
1233 /**
1234 * @brief Performs @b dictionary comparison on ranges.
1235 * @ingroup sorting_algorithms
1236 * @param __first1 An input iterator.
1237 * @param __last1 An input iterator.
1238 * @param __first2 An input iterator.
1239 * @param __last2 An input iterator.
1240 * @param __comp A @link comparison_functors comparison functor@endlink.
1241 * @return A boolean true or false.
1242 *
1243 * The same as the four-parameter @c lexicographical_compare, but uses the
1244 * comp parameter instead of @c <.
1245 */
1246 template<typename _II1, typename _II2, typename _Compare>
1247 inline bool
1248 lexicographical_compare(_II1 __first1, _II1 __last1,
1249 _II2 __first2, _II2 __last2, _Compare __comp)
1250 {
1251 // concept requirements
1252 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1253 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1254 __glibcxx_requires_valid_range(__first1, __last1);
1255 __glibcxx_requires_valid_range(__first2, __last2);
1256
1257 return std::__lexicographical_compare_impl
1258 (__first1, __last1, __first2, __last2,
1259 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1260 }
1261
1262 template<typename _InputIterator1, typename _InputIterator2,
1263 typename _BinaryPredicate>
1264 pair<_InputIterator1, _InputIterator2>
1265 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1266 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1267 {
1268 while (__first1 != __last1 && __binary_pred(__first1, __first2))
1269 {
1270 ++__first1;
1271 ++__first2;
1272 }
1273 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1274 }
1275
1276 /**
1277 * @brief Finds the places in ranges which don't match.
1278 * @ingroup non_mutating_algorithms
1279 * @param __first1 An input iterator.
1280 * @param __last1 An input iterator.
1281 * @param __first2 An input iterator.
1282 * @return A pair of iterators pointing to the first mismatch.
1283 *
1284 * This compares the elements of two ranges using @c == and returns a pair
1285 * of iterators. The first iterator points into the first range, the
1286 * second iterator points into the second range, and the elements pointed
1287 * to by the iterators are not equal.
1288 */
1289 template<typename _InputIterator1, typename _InputIterator2>
1290 inline pair<_InputIterator1, _InputIterator2>
1291 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1292 _InputIterator2 __first2)
1293 {
1294 // concept requirements
1295 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1296 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1297 __glibcxx_function_requires(_EqualOpConcept<
1298 typename iterator_traits<_InputIterator1>::value_type,
1299 typename iterator_traits<_InputIterator2>::value_type>)
1300 __glibcxx_requires_valid_range(__first1, __last1);
1301
1302 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1303 __gnu_cxx::__ops::__iter_equal_to_iter());
1304 }
1305
1306 /**
1307 * @brief Finds the places in ranges which don't match.
1308 * @ingroup non_mutating_algorithms
1309 * @param __first1 An input iterator.
1310 * @param __last1 An input iterator.
1311 * @param __first2 An input iterator.
1312 * @param __binary_pred A binary predicate @link functors
1313 * functor@endlink.
1314 * @return A pair of iterators pointing to the first mismatch.
1315 *
1316 * This compares the elements of two ranges using the binary_pred
1317 * parameter, and returns a pair
1318 * of iterators. The first iterator points into the first range, the
1319 * second iterator points into the second range, and the elements pointed
1320 * to by the iterators are not equal.
1321 */
1322 template<typename _InputIterator1, typename _InputIterator2,
1323 typename _BinaryPredicate>
1324 inline pair<_InputIterator1, _InputIterator2>
1325 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1326 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1327 {
1328 // concept requirements
1329 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1330 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1331 __glibcxx_requires_valid_range(__first1, __last1);
1332
1333 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1334 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1335 }
1336
1337 #if __cplusplus > 201103L
1338
1339 template<typename _InputIterator1, typename _InputIterator2,
1340 typename _BinaryPredicate>
1341 pair<_InputIterator1, _InputIterator2>
1342 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1343 _InputIterator2 __first2, _InputIterator2 __last2,
1344 _BinaryPredicate __binary_pred)
1345 {
1346 while (__first1 != __last1 && __first2 != __last2
1347 && __binary_pred(__first1, __first2))
1348 {
1349 ++__first1;
1350 ++__first2;
1351 }
1352 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1353 }
1354
1355 /**
1356 * @brief Finds the places in ranges which don't match.
1357 * @ingroup non_mutating_algorithms
1358 * @param __first1 An input iterator.
1359 * @param __last1 An input iterator.
1360 * @param __first2 An input iterator.
1361 * @param __last2 An input iterator.
1362 * @return A pair of iterators pointing to the first mismatch.
1363 *
1364 * This compares the elements of two ranges using @c == and returns a pair
1365 * of iterators. The first iterator points into the first range, the
1366 * second iterator points into the second range, and the elements pointed
1367 * to by the iterators are not equal.
1368 */
1369 template<typename _InputIterator1, typename _InputIterator2>
1370 inline pair<_InputIterator1, _InputIterator2>
1371 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1372 _InputIterator2 __first2, _InputIterator2 __last2)
1373 {
1374 // concept requirements
1375 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1376 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1377 __glibcxx_function_requires(_EqualOpConcept<
1378 typename iterator_traits<_InputIterator1>::value_type,
1379 typename iterator_traits<_InputIterator2>::value_type>)
1380 __glibcxx_requires_valid_range(__first1, __last1);
1381 __glibcxx_requires_valid_range(__first2, __last2);
1382
1383 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1384 __gnu_cxx::__ops::__iter_equal_to_iter());
1385 }
1386
1387 /**
1388 * @brief Finds the places in ranges which don't match.
1389 * @ingroup non_mutating_algorithms
1390 * @param __first1 An input iterator.
1391 * @param __last1 An input iterator.
1392 * @param __first2 An input iterator.
1393 * @param __last2 An input iterator.
1394 * @param __binary_pred A binary predicate @link functors
1395 * functor@endlink.
1396 * @return A pair of iterators pointing to the first mismatch.
1397 *
1398 * This compares the elements of two ranges using the binary_pred
1399 * parameter, and returns a pair
1400 * of iterators. The first iterator points into the first range, the
1401 * second iterator points into the second range, and the elements pointed
1402 * to by the iterators are not equal.
1403 */
1404 template<typename _InputIterator1, typename _InputIterator2,
1405 typename _BinaryPredicate>
1406 inline pair<_InputIterator1, _InputIterator2>
1407 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1408 _InputIterator2 __first2, _InputIterator2 __last2,
1409 _BinaryPredicate __binary_pred)
1410 {
1411 // concept requirements
1412 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1413 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1414 __glibcxx_requires_valid_range(__first1, __last1);
1415 __glibcxx_requires_valid_range(__first2, __last2);
1416
1417 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1418 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1419 }
1420 #endif
1421
1422 _GLIBCXX_END_NAMESPACE_ALGO
1423 } // namespace std
1424
1425 // NB: This file is included within many other C++ includes, as a way
1426 // of getting the base algorithms. So, make sure that parallel bits
1427 // come in too if requested.
1428 #ifdef _GLIBCXX_PARALLEL
1429 # include <parallel/algobase.h>
1430 #endif
1431
1432 #endif