]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stl_algobase.h
formatter.h (_Debug_msg_id::__msg_irreflexive_ordering): New enum entry.
[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 // All of these auxiliary structs serve two purposes. (1) Replace
281 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
282 // because the input and output ranges are permitted to overlap.)
283 // (2) If we're using random access iterators, then write the loop as
284 // a for loop with an explicit count.
285
286 template<bool, bool, typename>
287 struct __copy_move
288 {
289 template<typename _II, typename _OI>
290 static _OI
291 __copy_m(_II __first, _II __last, _OI __result)
292 {
293 for (; __first != __last; ++__result, (void)++__first)
294 *__result = *__first;
295 return __result;
296 }
297 };
298
299 #if __cplusplus >= 201103L
300 template<typename _Category>
301 struct __copy_move<true, false, _Category>
302 {
303 template<typename _II, typename _OI>
304 static _OI
305 __copy_m(_II __first, _II __last, _OI __result)
306 {
307 for (; __first != __last; ++__result, (void)++__first)
308 *__result = std::move(*__first);
309 return __result;
310 }
311 };
312 #endif
313
314 template<>
315 struct __copy_move<false, false, random_access_iterator_tag>
316 {
317 template<typename _II, typename _OI>
318 static _OI
319 __copy_m(_II __first, _II __last, _OI __result)
320 {
321 typedef typename iterator_traits<_II>::difference_type _Distance;
322 for(_Distance __n = __last - __first; __n > 0; --__n)
323 {
324 *__result = *__first;
325 ++__first;
326 ++__result;
327 }
328 return __result;
329 }
330 };
331
332 #if __cplusplus >= 201103L
333 template<>
334 struct __copy_move<true, false, random_access_iterator_tag>
335 {
336 template<typename _II, typename _OI>
337 static _OI
338 __copy_m(_II __first, _II __last, _OI __result)
339 {
340 typedef typename iterator_traits<_II>::difference_type _Distance;
341 for(_Distance __n = __last - __first; __n > 0; --__n)
342 {
343 *__result = std::move(*__first);
344 ++__first;
345 ++__result;
346 }
347 return __result;
348 }
349 };
350 #endif
351
352 template<bool _IsMove>
353 struct __copy_move<_IsMove, true, random_access_iterator_tag>
354 {
355 template<typename _Tp>
356 static _Tp*
357 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
358 {
359 #if __cplusplus >= 201103L
360 // trivial types can have deleted assignment
361 static_assert( is_copy_assignable<_Tp>::value,
362 "type is not assignable" );
363 #endif
364 const ptrdiff_t _Num = __last - __first;
365 if (_Num)
366 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
367 return __result + _Num;
368 }
369 };
370
371 template<bool _IsMove, typename _II, typename _OI>
372 inline _OI
373 __copy_move_a(_II __first, _II __last, _OI __result)
374 {
375 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
376 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
377 typedef typename iterator_traits<_II>::iterator_category _Category;
378 const bool __simple = (__is_trivial(_ValueTypeI)
379 && __is_pointer<_II>::__value
380 && __is_pointer<_OI>::__value
381 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
382
383 return std::__copy_move<_IsMove, __simple,
384 _Category>::__copy_m(__first, __last, __result);
385 }
386
387 // Helpers for streambuf iterators (either istream or ostream).
388 // NB: avoid including <iosfwd>, relatively large.
389 template<typename _CharT>
390 struct char_traits;
391
392 template<typename _CharT, typename _Traits>
393 class istreambuf_iterator;
394
395 template<typename _CharT, typename _Traits>
396 class ostreambuf_iterator;
397
398 template<bool _IsMove, typename _CharT>
399 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
400 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
401 __copy_move_a2(_CharT*, _CharT*,
402 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
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(const _CharT*, const _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 _CharT*>::__type
413 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
414 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
415
416 template<bool _IsMove, typename _II, typename _OI>
417 inline _OI
418 __copy_move_a2(_II __first, _II __last, _OI __result)
419 {
420 return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
421 std::__niter_base(__last),
422 std::__niter_base(__result)));
423 }
424
425 /**
426 * @brief Copies the range [first,last) into result.
427 * @ingroup mutating_algorithms
428 * @param __first An input iterator.
429 * @param __last An input iterator.
430 * @param __result An output iterator.
431 * @return result + (first - last)
432 *
433 * This inline function will boil down to a call to @c memmove whenever
434 * possible. Failing that, if random access iterators are passed, then the
435 * loop count will be known (and therefore a candidate for compiler
436 * optimizations such as unrolling). Result may not be contained within
437 * [first,last); the copy_backward function should be used instead.
438 *
439 * Note that the end of the output range is permitted to be contained
440 * within [first,last).
441 */
442 template<typename _II, typename _OI>
443 inline _OI
444 copy(_II __first, _II __last, _OI __result)
445 {
446 // concept requirements
447 __glibcxx_function_requires(_InputIteratorConcept<_II>)
448 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
449 typename iterator_traits<_II>::value_type>)
450 __glibcxx_requires_valid_range(__first, __last);
451
452 return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
453 (std::__miter_base(__first), std::__miter_base(__last),
454 __result));
455 }
456
457 #if __cplusplus >= 201103L
458 /**
459 * @brief Moves the range [first,last) into result.
460 * @ingroup mutating_algorithms
461 * @param __first An input iterator.
462 * @param __last An input iterator.
463 * @param __result An output iterator.
464 * @return result + (first - last)
465 *
466 * This inline function will boil down to a call to @c memmove whenever
467 * possible. Failing that, if random access iterators are passed, then the
468 * loop count will be known (and therefore a candidate for compiler
469 * optimizations such as unrolling). Result may not be contained within
470 * [first,last); the move_backward function should be used instead.
471 *
472 * Note that the end of the output range is permitted to be contained
473 * within [first,last).
474 */
475 template<typename _II, typename _OI>
476 inline _OI
477 move(_II __first, _II __last, _OI __result)
478 {
479 // concept requirements
480 __glibcxx_function_requires(_InputIteratorConcept<_II>)
481 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
482 typename iterator_traits<_II>::value_type>)
483 __glibcxx_requires_valid_range(__first, __last);
484
485 return std::__copy_move_a2<true>(std::__miter_base(__first),
486 std::__miter_base(__last), __result);
487 }
488
489 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
490 #else
491 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
492 #endif
493
494 template<bool, bool, typename>
495 struct __copy_move_backward
496 {
497 template<typename _BI1, typename _BI2>
498 static _BI2
499 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
500 {
501 while (__first != __last)
502 *--__result = *--__last;
503 return __result;
504 }
505 };
506
507 #if __cplusplus >= 201103L
508 template<typename _Category>
509 struct __copy_move_backward<true, false, _Category>
510 {
511 template<typename _BI1, typename _BI2>
512 static _BI2
513 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
514 {
515 while (__first != __last)
516 *--__result = std::move(*--__last);
517 return __result;
518 }
519 };
520 #endif
521
522 template<>
523 struct __copy_move_backward<false, false, random_access_iterator_tag>
524 {
525 template<typename _BI1, typename _BI2>
526 static _BI2
527 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
528 {
529 typename iterator_traits<_BI1>::difference_type __n;
530 for (__n = __last - __first; __n > 0; --__n)
531 *--__result = *--__last;
532 return __result;
533 }
534 };
535
536 #if __cplusplus >= 201103L
537 template<>
538 struct __copy_move_backward<true, false, random_access_iterator_tag>
539 {
540 template<typename _BI1, typename _BI2>
541 static _BI2
542 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
543 {
544 typename iterator_traits<_BI1>::difference_type __n;
545 for (__n = __last - __first; __n > 0; --__n)
546 *--__result = std::move(*--__last);
547 return __result;
548 }
549 };
550 #endif
551
552 template<bool _IsMove>
553 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
554 {
555 template<typename _Tp>
556 static _Tp*
557 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
558 {
559 #if __cplusplus >= 201103L
560 // trivial types can have deleted assignment
561 static_assert( is_copy_assignable<_Tp>::value,
562 "type is not assignable" );
563 #endif
564 const ptrdiff_t _Num = __last - __first;
565 if (_Num)
566 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
567 return __result - _Num;
568 }
569 };
570
571 template<bool _IsMove, typename _BI1, typename _BI2>
572 inline _BI2
573 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
574 {
575 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
576 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
577 typedef typename iterator_traits<_BI1>::iterator_category _Category;
578 const bool __simple = (__is_trivial(_ValueType1)
579 && __is_pointer<_BI1>::__value
580 && __is_pointer<_BI2>::__value
581 && __are_same<_ValueType1, _ValueType2>::__value);
582
583 return std::__copy_move_backward<_IsMove, __simple,
584 _Category>::__copy_move_b(__first,
585 __last,
586 __result);
587 }
588
589 template<bool _IsMove, typename _BI1, typename _BI2>
590 inline _BI2
591 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
592 {
593 return _BI2(std::__copy_move_backward_a<_IsMove>
594 (std::__niter_base(__first), std::__niter_base(__last),
595 std::__niter_base(__result)));
596 }
597
598 /**
599 * @brief Copies the range [first,last) into result.
600 * @ingroup mutating_algorithms
601 * @param __first A bidirectional iterator.
602 * @param __last A bidirectional iterator.
603 * @param __result A bidirectional iterator.
604 * @return result - (first - last)
605 *
606 * The function has the same effect as copy, but starts at the end of the
607 * range and works its way to the start, returning the start of the result.
608 * This inline function will boil down to a call to @c memmove whenever
609 * possible. Failing that, if random access iterators are passed, then the
610 * loop count will be known (and therefore a candidate for compiler
611 * optimizations such as unrolling).
612 *
613 * Result may not be in the range (first,last]. Use copy instead. Note
614 * that the start of the output range may overlap [first,last).
615 */
616 template<typename _BI1, typename _BI2>
617 inline _BI2
618 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
619 {
620 // concept requirements
621 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
622 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
623 __glibcxx_function_requires(_ConvertibleConcept<
624 typename iterator_traits<_BI1>::value_type,
625 typename iterator_traits<_BI2>::value_type>)
626 __glibcxx_requires_valid_range(__first, __last);
627
628 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
629 (std::__miter_base(__first), std::__miter_base(__last),
630 __result));
631 }
632
633 #if __cplusplus >= 201103L
634 /**
635 * @brief Moves the range [first,last) into result.
636 * @ingroup mutating_algorithms
637 * @param __first A bidirectional iterator.
638 * @param __last A bidirectional iterator.
639 * @param __result A bidirectional iterator.
640 * @return result - (first - last)
641 *
642 * The function has the same effect as move, but starts at the end of the
643 * range and works its way to the start, returning the start of the result.
644 * This inline function will boil down to a call to @c memmove whenever
645 * possible. Failing that, if random access iterators are passed, then the
646 * loop count will be known (and therefore a candidate for compiler
647 * optimizations such as unrolling).
648 *
649 * Result may not be in the range (first,last]. Use move instead. Note
650 * that the start of the output range may overlap [first,last).
651 */
652 template<typename _BI1, typename _BI2>
653 inline _BI2
654 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
655 {
656 // concept requirements
657 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
658 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
659 __glibcxx_function_requires(_ConvertibleConcept<
660 typename iterator_traits<_BI1>::value_type,
661 typename iterator_traits<_BI2>::value_type>)
662 __glibcxx_requires_valid_range(__first, __last);
663
664 return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
665 std::__miter_base(__last),
666 __result);
667 }
668
669 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
670 #else
671 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
672 #endif
673
674 template<typename _ForwardIterator, typename _Tp>
675 inline typename
676 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
677 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
678 const _Tp& __value)
679 {
680 for (; __first != __last; ++__first)
681 *__first = __value;
682 }
683
684 template<typename _ForwardIterator, typename _Tp>
685 inline typename
686 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
687 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
688 const _Tp& __value)
689 {
690 const _Tp __tmp = __value;
691 for (; __first != __last; ++__first)
692 *__first = __tmp;
693 }
694
695 // Specialization: for char types we can use memset.
696 template<typename _Tp>
697 inline typename
698 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
699 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
700 {
701 const _Tp __tmp = __c;
702 if (const size_t __len = __last - __first)
703 __builtin_memset(__first, static_cast<unsigned char>(__tmp), __len);
704 }
705
706 /**
707 * @brief Fills the range [first,last) with copies of value.
708 * @ingroup mutating_algorithms
709 * @param __first A forward iterator.
710 * @param __last A forward iterator.
711 * @param __value A reference-to-const of arbitrary type.
712 * @return Nothing.
713 *
714 * This function fills a range with copies of the same value. For char
715 * types filling contiguous areas of memory, this becomes an inline call
716 * to @c memset or @c wmemset.
717 */
718 template<typename _ForwardIterator, typename _Tp>
719 inline void
720 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
721 {
722 // concept requirements
723 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
724 _ForwardIterator>)
725 __glibcxx_requires_valid_range(__first, __last);
726
727 std::__fill_a(std::__niter_base(__first), std::__niter_base(__last),
728 __value);
729 }
730
731 template<typename _OutputIterator, typename _Size, typename _Tp>
732 inline typename
733 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
734 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
735 {
736 for (__decltype(__n + 0) __niter = __n;
737 __niter > 0; --__niter, ++__first)
738 *__first = __value;
739 return __first;
740 }
741
742 template<typename _OutputIterator, typename _Size, typename _Tp>
743 inline typename
744 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
745 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
746 {
747 const _Tp __tmp = __value;
748 for (__decltype(__n + 0) __niter = __n;
749 __niter > 0; --__niter, ++__first)
750 *__first = __tmp;
751 return __first;
752 }
753
754 template<typename _Size, typename _Tp>
755 inline typename
756 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
757 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
758 {
759 std::__fill_a(__first, __first + __n, __c);
760 return __first + __n;
761 }
762
763 /**
764 * @brief Fills the range [first,first+n) with copies of value.
765 * @ingroup mutating_algorithms
766 * @param __first An output iterator.
767 * @param __n The count of copies to perform.
768 * @param __value A reference-to-const of arbitrary type.
769 * @return The iterator at first+n.
770 *
771 * This function fills a range with copies of the same value. For char
772 * types filling contiguous areas of memory, this becomes an inline call
773 * to @c memset or @ wmemset.
774 *
775 * _GLIBCXX_RESOLVE_LIB_DEFECTS
776 * DR 865. More algorithms that throw away information
777 */
778 template<typename _OI, typename _Size, typename _Tp>
779 inline _OI
780 fill_n(_OI __first, _Size __n, const _Tp& __value)
781 {
782 // concept requirements
783 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
784
785 return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value));
786 }
787
788 template<bool _BoolType>
789 struct __equal
790 {
791 template<typename _II1, typename _II2>
792 static bool
793 equal(_II1 __first1, _II1 __last1, _II2 __first2)
794 {
795 for (; __first1 != __last1; ++__first1, (void)++__first2)
796 if (!(*__first1 == *__first2))
797 return false;
798 return true;
799 }
800 };
801
802 template<>
803 struct __equal<true>
804 {
805 template<typename _Tp>
806 static bool
807 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
808 {
809 if (const size_t __len = (__last1 - __first1))
810 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp) * __len);
811 return true;
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<typename _II1, typename _II2, typename _Compare>
866 bool
867 __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
868 _II2 __first2, _II2 __last2,
869 _Compare __comp)
870 {
871 typedef typename iterator_traits<_II1>::iterator_category _Category1;
872 typedef typename iterator_traits<_II2>::iterator_category _Category2;
873 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
874
875 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
876 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
877 ++__first1, (void)++__first2)
878 {
879 if (__comp(__first1, __first2))
880 return true;
881 if (__comp(__first2, __first1))
882 return false;
883 }
884 return __first1 == __last1 && __first2 != __last2;
885 }
886
887 template<bool _BoolType>
888 struct __lexicographical_compare
889 {
890 template<typename _II1, typename _II2>
891 static bool __lc(_II1, _II1, _II2, _II2);
892 };
893
894 template<bool _BoolType>
895 template<typename _II1, typename _II2>
896 bool
897 __lexicographical_compare<_BoolType>::
898 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
899 {
900 return std::__lexicographical_compare_impl(__first1, __last1,
901 __first2, __last2,
902 __gnu_cxx::__ops::__iter_less_iter());
903 }
904
905 template<>
906 struct __lexicographical_compare<true>
907 {
908 template<typename _Tp, typename _Up>
909 static bool
910 __lc(const _Tp* __first1, const _Tp* __last1,
911 const _Up* __first2, const _Up* __last2)
912 {
913 const size_t __len1 = __last1 - __first1;
914 const size_t __len2 = __last2 - __first2;
915 if (const size_t __len = std::min(__len1, __len2))
916 if (int __result = __builtin_memcmp(__first1, __first2, __len))
917 return __result < 0;
918 return __len1 < __len2;
919 }
920 };
921
922 template<typename _II1, typename _II2>
923 inline bool
924 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
925 _II2 __first2, _II2 __last2)
926 {
927 typedef typename iterator_traits<_II1>::value_type _ValueType1;
928 typedef typename iterator_traits<_II2>::value_type _ValueType2;
929 const bool __simple =
930 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
931 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
932 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
933 && __is_pointer<_II1>::__value
934 && __is_pointer<_II2>::__value);
935
936 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
937 __first2, __last2);
938 }
939
940 template<typename _ForwardIterator, typename _Tp, typename _Compare>
941 _ForwardIterator
942 __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
943 const _Tp& __val, _Compare __comp)
944 {
945 typedef typename iterator_traits<_ForwardIterator>::difference_type
946 _DistanceType;
947
948 _DistanceType __len = std::distance(__first, __last);
949
950 while (__len > 0)
951 {
952 _DistanceType __half = __len >> 1;
953 _ForwardIterator __middle = __first;
954 std::advance(__middle, __half);
955 if (__comp(__middle, __val))
956 {
957 __first = __middle;
958 ++__first;
959 __len = __len - __half - 1;
960 }
961 else
962 __len = __half;
963 }
964 return __first;
965 }
966
967 /**
968 * @brief Finds the first position in which @a val could be inserted
969 * without changing the ordering.
970 * @param __first An iterator.
971 * @param __last Another iterator.
972 * @param __val The search term.
973 * @return An iterator pointing to the first element <em>not less
974 * than</em> @a val, or end() if every element is less than
975 * @a val.
976 * @ingroup binary_search_algorithms
977 */
978 template<typename _ForwardIterator, typename _Tp>
979 inline _ForwardIterator
980 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
981 const _Tp& __val)
982 {
983 // concept requirements
984 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
985 __glibcxx_function_requires(_LessThanOpConcept<
986 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
987 __glibcxx_requires_partitioned_lower(__first, __last, __val);
988 __glibcxx_requires_irreflexive2(__first, __last);
989
990 return std::__lower_bound(__first, __last, __val,
991 __gnu_cxx::__ops::__iter_less_val());
992 }
993
994 /// This is a helper function for the sort routines and for random.tcc.
995 // Precondition: __n > 0.
996 inline _GLIBCXX_CONSTEXPR int
997 __lg(int __n)
998 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
999
1000 inline _GLIBCXX_CONSTEXPR unsigned
1001 __lg(unsigned __n)
1002 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1003
1004 inline _GLIBCXX_CONSTEXPR long
1005 __lg(long __n)
1006 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1007
1008 inline _GLIBCXX_CONSTEXPR unsigned long
1009 __lg(unsigned long __n)
1010 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1011
1012 inline _GLIBCXX_CONSTEXPR long long
1013 __lg(long long __n)
1014 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1015
1016 inline _GLIBCXX_CONSTEXPR unsigned long long
1017 __lg(unsigned long long __n)
1018 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1019
1020 _GLIBCXX_END_NAMESPACE_VERSION
1021
1022 _GLIBCXX_BEGIN_NAMESPACE_ALGO
1023
1024 /**
1025 * @brief Tests a range for element-wise equality.
1026 * @ingroup non_mutating_algorithms
1027 * @param __first1 An input iterator.
1028 * @param __last1 An input iterator.
1029 * @param __first2 An input iterator.
1030 * @return A boolean true or false.
1031 *
1032 * This compares the elements of two ranges using @c == and returns true or
1033 * false depending on whether all of the corresponding elements of the
1034 * ranges are equal.
1035 */
1036 template<typename _II1, typename _II2>
1037 inline bool
1038 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1039 {
1040 // concept requirements
1041 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1042 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1043 __glibcxx_function_requires(_EqualOpConcept<
1044 typename iterator_traits<_II1>::value_type,
1045 typename iterator_traits<_II2>::value_type>)
1046 __glibcxx_requires_valid_range(__first1, __last1);
1047
1048 return std::__equal_aux(std::__niter_base(__first1),
1049 std::__niter_base(__last1),
1050 std::__niter_base(__first2));
1051 }
1052
1053 /**
1054 * @brief Tests a range for element-wise equality.
1055 * @ingroup non_mutating_algorithms
1056 * @param __first1 An input iterator.
1057 * @param __last1 An input iterator.
1058 * @param __first2 An input iterator.
1059 * @param __binary_pred A binary predicate @link functors
1060 * functor@endlink.
1061 * @return A boolean true or false.
1062 *
1063 * This compares the elements of two ranges using the binary_pred
1064 * parameter, and returns true or
1065 * false depending on whether all of the corresponding elements of the
1066 * ranges are equal.
1067 */
1068 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1069 inline bool
1070 equal(_IIter1 __first1, _IIter1 __last1,
1071 _IIter2 __first2, _BinaryPredicate __binary_pred)
1072 {
1073 // concept requirements
1074 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1075 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1076 __glibcxx_requires_valid_range(__first1, __last1);
1077
1078 for (; __first1 != __last1; ++__first1, (void)++__first2)
1079 if (!bool(__binary_pred(*__first1, *__first2)))
1080 return false;
1081 return true;
1082 }
1083
1084 #if __cplusplus > 201103L
1085
1086 #define __cpp_lib_robust_nonmodifying_seq_ops 201304
1087
1088 /**
1089 * @brief Tests a range for element-wise equality.
1090 * @ingroup non_mutating_algorithms
1091 * @param __first1 An input iterator.
1092 * @param __last1 An input iterator.
1093 * @param __first2 An input iterator.
1094 * @param __last2 An input iterator.
1095 * @return A boolean true or false.
1096 *
1097 * This compares the elements of two ranges using @c == and returns true or
1098 * false depending on whether all of the corresponding elements of the
1099 * ranges are equal.
1100 */
1101 template<typename _II1, typename _II2>
1102 inline bool
1103 equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1104 {
1105 // concept requirements
1106 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1107 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1108 __glibcxx_function_requires(_EqualOpConcept<
1109 typename iterator_traits<_II1>::value_type,
1110 typename iterator_traits<_II2>::value_type>)
1111 __glibcxx_requires_valid_range(__first1, __last1);
1112 __glibcxx_requires_valid_range(__first2, __last2);
1113
1114 using _RATag = random_access_iterator_tag;
1115 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1116 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1117 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1118 if (_RAIters())
1119 {
1120 auto __d1 = std::distance(__first1, __last1);
1121 auto __d2 = std::distance(__first2, __last2);
1122 if (__d1 != __d2)
1123 return false;
1124 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
1125 }
1126
1127 for (; __first1 != __last1 && __first2 != __last2;
1128 ++__first1, (void)++__first2)
1129 if (!(*__first1 == *__first2))
1130 return false;
1131 return __first1 == __last1 && __first2 == __last2;
1132 }
1133
1134 /**
1135 * @brief Tests a range for element-wise equality.
1136 * @ingroup non_mutating_algorithms
1137 * @param __first1 An input iterator.
1138 * @param __last1 An input iterator.
1139 * @param __first2 An input iterator.
1140 * @param __last2 An input iterator.
1141 * @param __binary_pred A binary predicate @link functors
1142 * functor@endlink.
1143 * @return A boolean true or false.
1144 *
1145 * This compares the elements of two ranges using the binary_pred
1146 * parameter, and returns true or
1147 * false depending on whether all of the corresponding elements of the
1148 * ranges are equal.
1149 */
1150 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1151 inline bool
1152 equal(_IIter1 __first1, _IIter1 __last1,
1153 _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
1154 {
1155 // concept requirements
1156 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1157 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1158 __glibcxx_requires_valid_range(__first1, __last1);
1159 __glibcxx_requires_valid_range(__first2, __last2);
1160
1161 using _RATag = random_access_iterator_tag;
1162 using _Cat1 = typename iterator_traits<_IIter1>::iterator_category;
1163 using _Cat2 = typename iterator_traits<_IIter2>::iterator_category;
1164 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1165 if (_RAIters())
1166 {
1167 auto __d1 = std::distance(__first1, __last1);
1168 auto __d2 = std::distance(__first2, __last2);
1169 if (__d1 != __d2)
1170 return false;
1171 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
1172 __binary_pred);
1173 }
1174
1175 for (; __first1 != __last1 && __first2 != __last2;
1176 ++__first1, (void)++__first2)
1177 if (!bool(__binary_pred(*__first1, *__first2)))
1178 return false;
1179 return __first1 == __last1 && __first2 == __last2;
1180 }
1181 #endif
1182
1183 /**
1184 * @brief Performs @b dictionary comparison on ranges.
1185 * @ingroup sorting_algorithms
1186 * @param __first1 An input iterator.
1187 * @param __last1 An input iterator.
1188 * @param __first2 An input iterator.
1189 * @param __last2 An input iterator.
1190 * @return A boolean true or false.
1191 *
1192 * <em>Returns true if the sequence of elements defined by the range
1193 * [first1,last1) is lexicographically less than the sequence of elements
1194 * defined by the range [first2,last2). Returns false otherwise.</em>
1195 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1196 * then this is an inline call to @c memcmp.
1197 */
1198 template<typename _II1, typename _II2>
1199 inline bool
1200 lexicographical_compare(_II1 __first1, _II1 __last1,
1201 _II2 __first2, _II2 __last2)
1202 {
1203 #ifdef _GLIBCXX_CONCEPT_CHECKS
1204 // concept requirements
1205 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1206 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1207 #endif
1208 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1209 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1210 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1211 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1212 __glibcxx_requires_valid_range(__first1, __last1);
1213 __glibcxx_requires_irreflexive2(__first1, __last1);
1214 __glibcxx_requires_valid_range(__first2, __last2);
1215 __glibcxx_requires_irreflexive2(__first2, __last2);
1216
1217 return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1218 std::__niter_base(__last1),
1219 std::__niter_base(__first2),
1220 std::__niter_base(__last2));
1221 }
1222
1223 /**
1224 * @brief Performs @b dictionary comparison on ranges.
1225 * @ingroup sorting_algorithms
1226 * @param __first1 An input iterator.
1227 * @param __last1 An input iterator.
1228 * @param __first2 An input iterator.
1229 * @param __last2 An input iterator.
1230 * @param __comp A @link comparison_functors comparison functor@endlink.
1231 * @return A boolean true or false.
1232 *
1233 * The same as the four-parameter @c lexicographical_compare, but uses the
1234 * comp parameter instead of @c <.
1235 */
1236 template<typename _II1, typename _II2, typename _Compare>
1237 inline bool
1238 lexicographical_compare(_II1 __first1, _II1 __last1,
1239 _II2 __first2, _II2 __last2, _Compare __comp)
1240 {
1241 // concept requirements
1242 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1243 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1244 __glibcxx_requires_valid_range(__first1, __last1);
1245 __glibcxx_requires_irreflexive_pred2(__first1, __last1, __comp);
1246 __glibcxx_requires_valid_range(__first2, __last2);
1247 __glibcxx_requires_irreflexive_pred2(__first2, __last2, __comp);
1248
1249 return std::__lexicographical_compare_impl
1250 (__first1, __last1, __first2, __last2,
1251 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1252 }
1253
1254 template<typename _InputIterator1, typename _InputIterator2,
1255 typename _BinaryPredicate>
1256 pair<_InputIterator1, _InputIterator2>
1257 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1258 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1259 {
1260 while (__first1 != __last1 && __binary_pred(__first1, __first2))
1261 {
1262 ++__first1;
1263 ++__first2;
1264 }
1265 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1266 }
1267
1268 /**
1269 * @brief Finds the places in ranges which don't match.
1270 * @ingroup non_mutating_algorithms
1271 * @param __first1 An input iterator.
1272 * @param __last1 An input iterator.
1273 * @param __first2 An input iterator.
1274 * @return A pair of iterators pointing to the first mismatch.
1275 *
1276 * This compares the elements of two ranges using @c == and returns a pair
1277 * of iterators. The first iterator points into the first range, the
1278 * second iterator points into the second range, and the elements pointed
1279 * to by the iterators are not equal.
1280 */
1281 template<typename _InputIterator1, typename _InputIterator2>
1282 inline pair<_InputIterator1, _InputIterator2>
1283 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1284 _InputIterator2 __first2)
1285 {
1286 // concept requirements
1287 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1288 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1289 __glibcxx_function_requires(_EqualOpConcept<
1290 typename iterator_traits<_InputIterator1>::value_type,
1291 typename iterator_traits<_InputIterator2>::value_type>)
1292 __glibcxx_requires_valid_range(__first1, __last1);
1293
1294 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1295 __gnu_cxx::__ops::__iter_equal_to_iter());
1296 }
1297
1298 /**
1299 * @brief Finds the places in ranges which don't match.
1300 * @ingroup non_mutating_algorithms
1301 * @param __first1 An input iterator.
1302 * @param __last1 An input iterator.
1303 * @param __first2 An input iterator.
1304 * @param __binary_pred A binary predicate @link functors
1305 * functor@endlink.
1306 * @return A pair of iterators pointing to the first mismatch.
1307 *
1308 * This compares the elements of two ranges using the binary_pred
1309 * parameter, and returns a pair
1310 * of iterators. The first iterator points into the first range, the
1311 * second iterator points into the second range, and the elements pointed
1312 * to by the iterators are not equal.
1313 */
1314 template<typename _InputIterator1, typename _InputIterator2,
1315 typename _BinaryPredicate>
1316 inline pair<_InputIterator1, _InputIterator2>
1317 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1318 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1319 {
1320 // concept requirements
1321 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1322 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1323 __glibcxx_requires_valid_range(__first1, __last1);
1324
1325 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1326 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1327 }
1328
1329 #if __cplusplus > 201103L
1330
1331 template<typename _InputIterator1, typename _InputIterator2,
1332 typename _BinaryPredicate>
1333 pair<_InputIterator1, _InputIterator2>
1334 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1335 _InputIterator2 __first2, _InputIterator2 __last2,
1336 _BinaryPredicate __binary_pred)
1337 {
1338 while (__first1 != __last1 && __first2 != __last2
1339 && __binary_pred(__first1, __first2))
1340 {
1341 ++__first1;
1342 ++__first2;
1343 }
1344 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1345 }
1346
1347 /**
1348 * @brief Finds the places in ranges which don't match.
1349 * @ingroup non_mutating_algorithms
1350 * @param __first1 An input iterator.
1351 * @param __last1 An input iterator.
1352 * @param __first2 An input iterator.
1353 * @param __last2 An input iterator.
1354 * @return A pair of iterators pointing to the first mismatch.
1355 *
1356 * This compares the elements of two ranges using @c == 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 inline pair<_InputIterator1, _InputIterator2>
1363 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1364 _InputIterator2 __first2, _InputIterator2 __last2)
1365 {
1366 // concept requirements
1367 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1368 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1369 __glibcxx_function_requires(_EqualOpConcept<
1370 typename iterator_traits<_InputIterator1>::value_type,
1371 typename iterator_traits<_InputIterator2>::value_type>)
1372 __glibcxx_requires_valid_range(__first1, __last1);
1373 __glibcxx_requires_valid_range(__first2, __last2);
1374
1375 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1376 __gnu_cxx::__ops::__iter_equal_to_iter());
1377 }
1378
1379 /**
1380 * @brief Finds the places in ranges which don't match.
1381 * @ingroup non_mutating_algorithms
1382 * @param __first1 An input iterator.
1383 * @param __last1 An input iterator.
1384 * @param __first2 An input iterator.
1385 * @param __last2 An input iterator.
1386 * @param __binary_pred A binary predicate @link functors
1387 * functor@endlink.
1388 * @return A pair of iterators pointing to the first mismatch.
1389 *
1390 * This compares the elements of two ranges using the binary_pred
1391 * parameter, and returns a pair
1392 * of iterators. The first iterator points into the first range, the
1393 * second iterator points into the second range, and the elements pointed
1394 * to by the iterators are not equal.
1395 */
1396 template<typename _InputIterator1, typename _InputIterator2,
1397 typename _BinaryPredicate>
1398 inline pair<_InputIterator1, _InputIterator2>
1399 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1400 _InputIterator2 __first2, _InputIterator2 __last2,
1401 _BinaryPredicate __binary_pred)
1402 {
1403 // concept requirements
1404 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1405 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1406 __glibcxx_requires_valid_range(__first1, __last1);
1407 __glibcxx_requires_valid_range(__first2, __last2);
1408
1409 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1410 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1411 }
1412 #endif
1413
1414 _GLIBCXX_END_NAMESPACE_ALGO
1415 } // namespace std
1416
1417 // NB: This file is included within many other C++ includes, as a way
1418 // of getting the base algorithms. So, make sure that parallel bits
1419 // come in too if requested.
1420 #ifdef _GLIBCXX_PARALLEL
1421 # include <parallel/algobase.h>
1422 #endif
1423
1424 #endif