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