1 // Core algorithmic facilities -*- C++ -*-
3 // Copyright (C) 2001-2013 Free Software Foundation, Inc.
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)
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.
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.
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/>.
28 * Hewlett-Packard Company
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.
39 * Copyright (c) 1996-1998
40 * Silicon Graphics Computer Systems, Inc.
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.
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}
56 #ifndef _STL_ALGOBASE_H
57 #define _STL_ALGOBASE_H 1
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
72 namespace std
_GLIBCXX_VISIBILITY(default)
74 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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
>
83 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
85 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
87 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
89 _ValueType1 __tmp
= _GLIBCXX_MOVE(*__a
);
90 *__a
= _GLIBCXX_MOVE(*__b
);
91 *__b
= _GLIBCXX_MOVE(__tmp
);
96 struct __iter_swap
<true>
98 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
100 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
108 * @brief Swaps the contents of two iterators.
109 * @ingroup mutating_algorithms
110 * @param __a An iterator.
111 * @param __b Another iterator.
114 * This function swaps the values pointed to by two iterators, not the
115 * iterators themselves.
117 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
119 iter_swap(_ForwardIterator1 __a
, _ForwardIterator2 __b
)
121 // concept requirements
122 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
124 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
127 #if __cplusplus < 201103L
128 typedef typename iterator_traits
<_ForwardIterator1
>::value_type
130 typedef typename iterator_traits
<_ForwardIterator2
>::value_type
133 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType1
,
135 __glibcxx_function_requires(_ConvertibleConcept
<_ValueType2
,
138 typedef typename iterator_traits
<_ForwardIterator1
>::reference
140 typedef typename iterator_traits
<_ForwardIterator2
>::reference
142 std::__iter_swap
<__are_same
<_ValueType1
, _ValueType2
>::__value
143 && __are_same
<_ValueType1
&, _ReferenceType1
>::__value
144 && __are_same
<_ValueType2
&, _ReferenceType2
>::__value
>::
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).
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.
163 template<typename _ForwardIterator1
, typename _ForwardIterator2
>
165 swap_ranges(_ForwardIterator1 __first1
, _ForwardIterator1 __last1
,
166 _ForwardIterator2 __first2
)
168 // concept requirements
169 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
171 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
173 __glibcxx_requires_valid_range(__first1
, __last1
);
175 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
176 std::iter_swap(__first1
, __first2
);
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.
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.
191 template<typename _Tp
>
193 min(const _Tp
& __a
, const _Tp
& __b
)
195 // concept requirements
196 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
197 //return __b < __a ? __b : __a;
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.
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.
214 template<typename _Tp
>
216 max(const _Tp
& __a
, const _Tp
& __b
)
218 // concept requirements
219 __glibcxx_function_requires(_LessThanComparableConcept
<_Tp
>)
220 //return __a < __b ? __b : __a;
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.
234 * This will work on temporary expressions, since they are only evaluated
235 * once, unlike a preprocessor macro.
237 template<typename _Tp
, typename _Compare
>
239 min(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
241 //return __comp(__b, __a) ? __b : __a;
242 if (__comp(__b
, __a
))
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.
255 * This will work on temporary expressions, since they are only evaluated
256 * once, unlike a preprocessor macro.
258 template<typename _Tp
, typename _Compare
>
260 max(const _Tp
& __a
, const _Tp
& __b
, _Compare __comp
)
262 //return __comp(__a, __b) ? __b : __a;
263 if (__comp(__a
, __b
))
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
>
272 : _Iter_base
<_Iterator
, __is_normal_iterator
<_Iterator
>::__value
>
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
); }
280 // Likewise, for move_iterator.
281 template<typename _Iterator
>
283 : _Iter_base
<_Iterator
, __is_move_iterator
<_Iterator
>::__value
>
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
); }
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.
297 template<bool, bool, typename
>
300 template<typename _II
, typename _OI
>
302 __copy_m(_II __first
, _II __last
, _OI __result
)
304 for (; __first
!= __last
; ++__result
, ++__first
)
305 *__result
= *__first
;
310 #if __cplusplus >= 201103L
311 template<typename _Category
>
312 struct __copy_move
<true, false, _Category
>
314 template<typename _II
, typename _OI
>
316 __copy_m(_II __first
, _II __last
, _OI __result
)
318 for (; __first
!= __last
; ++__result
, ++__first
)
319 *__result
= std::move(*__first
);
326 struct __copy_move
<false, false, random_access_iterator_tag
>
328 template<typename _II
, typename _OI
>
330 __copy_m(_II __first
, _II __last
, _OI __result
)
332 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
333 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
335 *__result
= *__first
;
343 #if __cplusplus >= 201103L
345 struct __copy_move
<true, false, random_access_iterator_tag
>
347 template<typename _II
, typename _OI
>
349 __copy_m(_II __first
, _II __last
, _OI __result
)
351 typedef typename iterator_traits
<_II
>::difference_type _Distance
;
352 for(_Distance __n
= __last
- __first
; __n
> 0; --__n
)
354 *__result
= std::move(*__first
);
363 template<bool _IsMove
>
364 struct __copy_move
<_IsMove
, true, random_access_iterator_tag
>
366 template<typename _Tp
>
368 __copy_m(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
370 const ptrdiff_t _Num
= __last
- __first
;
372 __builtin_memmove(__result
, __first
, sizeof(_Tp
) * _Num
);
373 return __result
+ _Num
;
377 template<bool _IsMove
, typename _II
, typename _OI
>
379 __copy_move_a(_II __first
, _II __last
, _OI __result
)
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
);
389 return std::__copy_move
<_IsMove
, __simple
,
390 _Category
>::__copy_m(__first
, __last
, __result
);
393 // Helpers for streambuf iterators (either istream or ostream).
394 // NB: avoid including <iosfwd>, relatively large.
395 template<typename _CharT
>
398 template<typename _CharT
, typename _Traits
>
399 class istreambuf_iterator
;
401 template<typename _CharT
, typename _Traits
>
402 class ostreambuf_iterator
;
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
> >);
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
> >);
416 template<bool _IsMove
, typename _CharT
>
417 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
419 __copy_move_a2(istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >,
420 istreambuf_iterator
<_CharT
, char_traits
<_CharT
> >, _CharT
*);
422 template<bool _IsMove
, typename _II
, typename _OI
>
424 __copy_move_a2(_II __first
, _II __last
, _OI __result
)
426 return _OI(std::__copy_move_a
<_IsMove
>(std::__niter_base(__first
),
427 std::__niter_base(__last
),
428 std::__niter_base(__result
)));
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)
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.
445 * Note that the end of the output range is permitted to be contained
446 * within [first,last).
448 template<typename _II
, typename _OI
>
450 copy(_II __first
, _II __last
, _OI __result
)
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
);
458 return (std::__copy_move_a2
<__is_move_iterator
<_II
>::__value
>
459 (std::__miter_base(__first
), std::__miter_base(__last
),
463 #if __cplusplus >= 201103L
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)
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.
478 * Note that the end of the output range is permitted to be contained
479 * within [first,last).
481 template<typename _II
, typename _OI
>
483 move(_II __first
, _II __last
, _OI __result
)
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
);
491 return std::__copy_move_a2
<true>(std::__miter_base(__first
),
492 std::__miter_base(__last
), __result
);
495 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
497 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
500 template<bool, bool, typename
>
501 struct __copy_move_backward
503 template<typename _BI1
, typename _BI2
>
505 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
507 while (__first
!= __last
)
508 *--__result
= *--__last
;
513 #if __cplusplus >= 201103L
514 template<typename _Category
>
515 struct __copy_move_backward
<true, false, _Category
>
517 template<typename _BI1
, typename _BI2
>
519 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
521 while (__first
!= __last
)
522 *--__result
= std::move(*--__last
);
529 struct __copy_move_backward
<false, false, random_access_iterator_tag
>
531 template<typename _BI1
, typename _BI2
>
533 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
535 typename iterator_traits
<_BI1
>::difference_type __n
;
536 for (__n
= __last
- __first
; __n
> 0; --__n
)
537 *--__result
= *--__last
;
542 #if __cplusplus >= 201103L
544 struct __copy_move_backward
<true, false, random_access_iterator_tag
>
546 template<typename _BI1
, typename _BI2
>
548 __copy_move_b(_BI1 __first
, _BI1 __last
, _BI2 __result
)
550 typename iterator_traits
<_BI1
>::difference_type __n
;
551 for (__n
= __last
- __first
; __n
> 0; --__n
)
552 *--__result
= std::move(*--__last
);
558 template<bool _IsMove
>
559 struct __copy_move_backward
<_IsMove
, true, random_access_iterator_tag
>
561 template<typename _Tp
>
563 __copy_move_b(const _Tp
* __first
, const _Tp
* __last
, _Tp
* __result
)
565 const ptrdiff_t _Num
= __last
- __first
;
567 __builtin_memmove(__result
- _Num
, __first
, sizeof(_Tp
) * _Num
);
568 return __result
- _Num
;
572 template<bool _IsMove
, typename _BI1
, typename _BI2
>
574 __copy_move_backward_a(_BI1 __first
, _BI1 __last
, _BI2 __result
)
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
);
584 return std::__copy_move_backward
<_IsMove
, __simple
,
585 _Category
>::__copy_move_b(__first
,
590 template<bool _IsMove
, typename _BI1
, typename _BI2
>
592 __copy_move_backward_a2(_BI1 __first
, _BI1 __last
, _BI2 __result
)
594 return _BI2(std::__copy_move_backward_a
<_IsMove
>
595 (std::__niter_base(__first
), std::__niter_base(__last
),
596 std::__niter_base(__result
)));
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)
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).
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).
617 template<typename _BI1
, typename _BI2
>
619 copy_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
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
);
629 return (std::__copy_move_backward_a2
<__is_move_iterator
<_BI1
>::__value
>
630 (std::__miter_base(__first
), std::__miter_base(__last
),
634 #if __cplusplus >= 201103L
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)
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).
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).
653 template<typename _BI1
, typename _BI2
>
655 move_backward(_BI1 __first
, _BI1 __last
, _BI2 __result
)
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
);
665 return std::__copy_move_backward_a2
<true>(std::__miter_base(__first
),
666 std::__miter_base(__last
),
670 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
672 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
675 template<typename _ForwardIterator
, typename _Tp
>
677 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, void>::__type
678 __fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
681 for (; __first
!= __last
; ++__first
)
685 template<typename _ForwardIterator
, typename _Tp
>
687 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, void>::__type
688 __fill_a(_ForwardIterator __first
, _ForwardIterator __last
,
691 const _Tp __tmp
= __value
;
692 for (; __first
!= __last
; ++__first
)
696 // Specialization: for char types we can use memset.
697 template<typename _Tp
>
699 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, void>::__type
700 __fill_a(_Tp
* __first
, _Tp
* __last
, const _Tp
& __c
)
702 const _Tp __tmp
= __c
;
703 __builtin_memset(__first
, static_cast<unsigned char>(__tmp
),
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.
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.
719 template<typename _ForwardIterator
, typename _Tp
>
721 fill(_ForwardIterator __first
, _ForwardIterator __last
, const _Tp
& __value
)
723 // concept requirements
724 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept
<
726 __glibcxx_requires_valid_range(__first
, __last
);
728 std::__fill_a(std::__niter_base(__first
), std::__niter_base(__last
),
732 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
734 __gnu_cxx::__enable_if
<!__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
735 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
737 for (__decltype(__n
+ 0) __niter
= __n
;
738 __niter
> 0; --__niter
, ++__first
)
743 template<typename _OutputIterator
, typename _Size
, typename _Tp
>
745 __gnu_cxx::__enable_if
<__is_scalar
<_Tp
>::__value
, _OutputIterator
>::__type
746 __fill_n_a(_OutputIterator __first
, _Size __n
, const _Tp
& __value
)
748 const _Tp __tmp
= __value
;
749 for (__decltype(__n
+ 0) __niter
= __n
;
750 __niter
> 0; --__niter
, ++__first
)
755 template<typename _Size
, typename _Tp
>
757 __gnu_cxx::__enable_if
<__is_byte
<_Tp
>::__value
, _Tp
*>::__type
758 __fill_n_a(_Tp
* __first
, _Size __n
, const _Tp
& __c
)
760 std::__fill_a(__first
, __first
+ __n
, __c
);
761 return __first
+ __n
;
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.
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.
776 * _GLIBCXX_RESOLVE_LIB_DEFECTS
777 * DR 865. More algorithms that throw away information
779 template<typename _OI
, typename _Size
, typename _Tp
>
781 fill_n(_OI __first
, _Size __n
, const _Tp
& __value
)
783 // concept requirements
784 __glibcxx_function_requires(_OutputIteratorConcept
<_OI
, _Tp
>)
786 return _OI(std::__fill_n_a(std::__niter_base(__first
), __n
, __value
));
789 template<bool _BoolType
>
792 template<typename _II1
, typename _II2
>
794 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
796 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
797 if (!(*__first1
== *__first2
))
802 #if __cplusplus > 201103L
803 template<typename _II1
, typename _II2
>
805 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
807 for (; __first1
!= __last1
&& __first2
!= __last2
;
808 ++__first1
, ++__first2
)
809 if (!(*__first1
== *__first2
))
819 template<typename _Tp
>
821 equal(const _Tp
* __first1
, const _Tp
* __last1
, const _Tp
* __first2
)
823 return !__builtin_memcmp(__first1
, __first2
, sizeof(_Tp
)
824 * (__last1
- __first1
));
827 #if __cplusplus > 201103L
828 template<typename _Tp
>
830 equal(const _Tp
* __first1
, const _Tp
* __last1
, const _Tp
* __first2
,
833 return !__builtin_memcmp(__first1
, __first2
, sizeof(_Tp
)
834 * (__last1
- __first1
));
839 template<typename _II1
, typename _II2
>
841 __equal_aux(_II1 __first1
, _II1 __last1
, _II2 __first2
)
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
);
851 return std::__equal
<__simple
>::equal(__first1
, __last1
, __first2
);
854 #if __cplusplus > 201103L
855 template<bool _BoolType
>
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
>;
863 template<typename _II1
, typename _II2
>
865 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
867 constexpr bool __ra_iters
= _IsRA
<_II1
>() && _IsRA
<_II2
>();
870 auto __d1
= std::distance(__first1
, __last1
);
871 auto __d2
= std::distance(__first2
, __last2
);
875 for (; __first1
!= __last1
&& __first2
!= __last2
;
876 ++__first1
, ++__first2
)
877 if (!(*__first1
== *__first2
))
879 return __ra_iters
|| (__first1
== __last1
&& __first2
== __last2
);
884 struct __equal2
<true>
886 template<typename _Tp
>
888 equal(const _Tp
* __first1
, const _Tp
* __last1
, const _Tp
* __first2
,
891 if ((__last1
- __first1
) != (__last2
- __first2
))
893 return !__builtin_memcmp(__first1
, __first2
, sizeof(_Tp
)
894 * (__last1
- __first1
));
898 template<typename _II1
, typename _II2
>
900 __equal2_aux(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
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
);
910 return __equal2
<__simple
>::equal(__first1
, __last1
, __first2
, __last2
);
914 template<typename
, typename
>
917 template<typename _II1
, typename _II2
>
919 __newlast1(_II1
, _II1 __last1
, _II2
, _II2
)
922 template<typename _II
>
924 __cnd2(_II __first
, _II __last
)
925 { return __first
!= __last
; }
929 struct __lc_rai
<random_access_iterator_tag
, random_access_iterator_tag
>
931 template<typename _RAI1
, typename _RAI2
>
933 __newlast1(_RAI1 __first1
, _RAI1 __last1
,
934 _RAI2 __first2
, _RAI2 __last2
)
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
;
943 template<typename _RAI
>
949 template<bool _BoolType
>
950 struct __lexicographical_compare
952 template<typename _II1
, typename _II2
>
953 static bool __lc(_II1
, _II1
, _II2
, _II2
);
956 template<bool _BoolType
>
957 template<typename _II1
, typename _II2
>
959 __lexicographical_compare
<_BoolType
>::
960 __lc(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
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
;
966 __last1
= __rai_type::__newlast1(__first1
, __last1
,
968 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
969 ++__first1
, ++__first2
)
971 if (*__first1
< *__first2
)
973 if (*__first2
< *__first1
)
976 return __first1
== __last1
&& __first2
!= __last2
;
980 struct __lexicographical_compare
<true>
982 template<typename _Tp
, typename _Up
>
984 __lc(const _Tp
* __first1
, const _Tp
* __last1
,
985 const _Up
* __first2
, const _Up
* __last2
)
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
;
995 template<typename _II1
, typename _II2
>
997 __lexicographical_compare_aux(_II1 __first1
, _II1 __last1
,
998 _II2 __first2
, _II2 __last2
)
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
);
1009 return std::__lexicographical_compare
<__simple
>::__lc(__first1
, __last1
,
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
1022 * @ingroup binary_search_algorithms
1024 template<typename _ForwardIterator
, typename _Tp
>
1026 lower_bound(_ForwardIterator __first
, _ForwardIterator __last
,
1029 #ifdef _GLIBCXX_CONCEPT_CHECKS
1030 typedef typename iterator_traits
<_ForwardIterator
>::value_type
1033 typedef typename iterator_traits
<_ForwardIterator
>::difference_type
1036 // concept requirements
1037 __glibcxx_function_requires(_ForwardIteratorConcept
<_ForwardIterator
>)
1038 __glibcxx_function_requires(_LessThanOpConcept
<_ValueType
, _Tp
>)
1039 __glibcxx_requires_partitioned_lower(__first
, __last
, __val
);
1041 _DistanceType __len
= std::distance(__first
, __last
);
1045 _DistanceType __half
= __len
>> 1;
1046 _ForwardIterator __middle
= __first
;
1047 std::advance(__middle
, __half
);
1048 if (*__middle
< __val
)
1052 __len
= __len
- __half
- 1;
1060 /// This is a helper function for the sort routines and for random.tcc.
1061 // Precondition: __n > 0.
1062 inline _GLIBCXX_CONSTEXPR
int
1064 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
1066 inline _GLIBCXX_CONSTEXPR
unsigned
1068 { return sizeof(int) * __CHAR_BIT__
- 1 - __builtin_clz(__n
); }
1070 inline _GLIBCXX_CONSTEXPR
long
1072 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
1074 inline _GLIBCXX_CONSTEXPR
unsigned long
1075 __lg(unsigned long __n
)
1076 { return sizeof(long) * __CHAR_BIT__
- 1 - __builtin_clzl(__n
); }
1078 inline _GLIBCXX_CONSTEXPR
long long
1080 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
1082 inline _GLIBCXX_CONSTEXPR
unsigned long long
1083 __lg(unsigned long long __n
)
1084 { return sizeof(long long) * __CHAR_BIT__
- 1 - __builtin_clzll(__n
); }
1086 _GLIBCXX_END_NAMESPACE_VERSION
1088 _GLIBCXX_BEGIN_NAMESPACE_ALGO
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.
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
1102 template<typename _II1
, typename _II2
>
1104 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
)
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
);
1114 return std::__equal_aux(std::__niter_base(__first1
),
1115 std::__niter_base(__last1
),
1116 std::__niter_base(__first2
));
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
1127 * @return A boolean true or false.
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
1134 template<typename _IIter1
, typename _IIter2
, typename _BinaryPredicate
>
1136 equal(_IIter1 __first1
, _IIter1 __last1
,
1137 _IIter2 __first2
, _BinaryPredicate __binary_pred
)
1139 // concept requirements
1140 __glibcxx_function_requires(_InputIteratorConcept
<_IIter1
>)
1141 __glibcxx_function_requires(_InputIteratorConcept
<_IIter2
>)
1142 __glibcxx_requires_valid_range(__first1
, __last1
);
1144 for (; __first1
!= __last1
; ++__first1
, ++__first2
)
1145 if (!bool(__binary_pred(*__first1
, *__first2
)))
1150 #if __cplusplus > 201103L
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.
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
1164 template<typename _II1
, typename _II2
>
1166 equal(_II1 __first1
, _II1 __last1
, _II2 __first2
, _II2 __last2
)
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
);
1177 return std::__equal2_aux(std::__niter_base(__first1
),
1178 std::__niter_base(__last1
),
1179 std::__niter_base(__first2
),
1180 std::__niter_base(__last2
));
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
1192 * @return A boolean true or false.
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
1199 template<typename _IIter1
, typename _IIter2
, typename _BinaryPredicate
>
1201 equal(_IIter1 __first1
, _IIter1 __last1
,
1202 _IIter2 __first2
, _IIter2 __last2
, _BinaryPredicate __binary_pred
)
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
);
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();
1217 auto __d1
= std::distance(__first1
, __last1
);
1218 auto __d2
= std::distance(__first2
, __last2
);
1223 for (; __first1
!= __last1
&& __first2
!= __last2
; ++__first1
, ++__first2
)
1224 if (!bool(__binary_pred(*__first1
, *__first2
)))
1226 return __ra_iters
|| (__first1
== __last1
&& __first2
== __last2
);
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.
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.
1245 template<typename _II1
, typename _II2
>
1247 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1248 _II2 __first2
, _II2 __last2
)
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
;
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
);
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
));
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.
1278 * The same as the four-parameter @c lexicographical_compare, but uses the
1279 * comp parameter instead of @c <.
1281 template<typename _II1
, typename _II2
, typename _Compare
>
1283 lexicographical_compare(_II1 __first1
, _II1 __last1
,
1284 _II2 __first2
, _II2 __last2
, _Compare __comp
)
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
;
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
);
1296 __last1
= __rai_type::__newlast1(__first1
, __last1
, __first2
, __last2
);
1297 for (; __first1
!= __last1
&& __rai_type::__cnd2(__first2
, __last2
);
1298 ++__first1
, ++__first2
)
1300 if (__comp(*__first1
, *__first2
))
1302 if (__comp(*__first2
, *__first1
))
1305 return __first1
== __last1
&& __first2
!= __last2
;
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.
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.
1321 template<typename _InputIterator1
, typename _InputIterator2
>
1322 pair
<_InputIterator1
, _InputIterator2
>
1323 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1324 _InputIterator2 __first2
)
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
);
1334 while (__first1
!= __last1
&& *__first1
== *__first2
)
1339 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
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
1350 * @return A pair of iterators pointing to the first mismatch.
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.
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
)
1364 // concept requirements
1365 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator1
>)
1366 __glibcxx_function_requires(_InputIteratorConcept
<_InputIterator2
>)
1367 __glibcxx_requires_valid_range(__first1
, __last1
);
1369 while (__first1
!= __last1
&& bool(__binary_pred(*__first1
, *__first2
)))
1374 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1377 #if __cplusplus > 201103L
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.
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.
1392 template<typename _InputIterator1
, typename _InputIterator2
>
1393 pair
<_InputIterator1
, _InputIterator2
>
1394 mismatch(_InputIterator1 __first1
, _InputIterator1 __last1
,
1395 _InputIterator2 __first2
, _InputIterator2 __last2
)
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
);
1406 while (__first1
!= __last1
&& __first2
!= __last2
1407 && *__first1
== *__first2
)
1412 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
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
1424 * @return A pair of iterators pointing to the first mismatch.
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.
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
)
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
);
1445 while (__first1
!= __last1
&& __first2
!= __last2
1446 && bool(__binary_pred(*__first1
, *__first2
)))
1451 return pair
<_InputIterator1
, _InputIterator2
>(__first1
, __first2
);
1455 _GLIBCXX_END_NAMESPACE_ALGO
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>