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