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