1 // Core algorithmic facilities -*- C++ -*-
3 // Copyright (C) 2001-2015 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
71 #include <bits/predefined_ops.h>
73 namespace std _GLIBCXX_VISIBILITY(default)
75 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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>
84 template<typename _ForwardIterator1, typename _ForwardIterator2>
86 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
88 typedef typename iterator_traits<_ForwardIterator1>::value_type
90 _ValueType1 __tmp = _GLIBCXX_MOVE(*__a);
91 *__a = _GLIBCXX_MOVE(*__b);
92 *__b = _GLIBCXX_MOVE(__tmp);
97 struct __iter_swap<true>
99 template<typename _ForwardIterator1, typename _ForwardIterator2>
101 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
109 * @brief Swaps the contents of two iterators.
110 * @ingroup mutating_algorithms
111 * @param __a An iterator.
112 * @param __b Another iterator.
115 * This function swaps the values pointed to by two iterators, not the
116 * iterators themselves.
118 template<typename _ForwardIterator1, typename _ForwardIterator2>
120 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
122 // concept requirements
123 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
125 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
128 #if __cplusplus < 201103L
129 typedef typename iterator_traits<_ForwardIterator1>::value_type
131 typedef typename iterator_traits<_ForwardIterator2>::value_type
134 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
136 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
139 typedef typename iterator_traits<_ForwardIterator1>::reference
141 typedef typename iterator_traits<_ForwardIterator2>::reference
143 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
144 && __are_same<_ValueType1&, _ReferenceType1>::__value
145 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
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).
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.
164 template<typename _ForwardIterator1, typename _ForwardIterator2>
166 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
167 _ForwardIterator2 __first2)
169 // concept requirements
170 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
172 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
174 __glibcxx_requires_valid_range(__first1, __last1);
176 for (; __first1 != __last1; ++__first1, ++__first2)
177 std::iter_swap(__first1, __first2);
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.
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.
192 template<typename _Tp>
195 min(const _Tp& __a, const _Tp& __b)
197 // concept requirements
198 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
199 //return __b < __a ? __b : __a;
206 * @brief This does what you think it does.
207 * @ingroup sorting_algorithms
208 * @param __a A thing of arbitrary type.
209 * @param __b Another thing of arbitrary type.
210 * @return The greater of the parameters.
212 * This is the simple classic generic implementation. It will work on
213 * temporary expressions, since they are only evaluated once, unlike a
214 * preprocessor macro.
216 template<typename _Tp>
219 max(const _Tp& __a, const _Tp& __b)
221 // concept requirements
222 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
223 //return __a < __b ? __b : __a;
230 * @brief This does what you think it does.
231 * @ingroup sorting_algorithms
232 * @param __a A thing of arbitrary type.
233 * @param __b Another thing of arbitrary type.
234 * @param __comp A @link comparison_functors comparison functor@endlink.
235 * @return The lesser of the parameters.
237 * This will work on temporary expressions, since they are only evaluated
238 * once, unlike a preprocessor macro.
240 template<typename _Tp, typename _Compare>
243 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
245 //return __comp(__b, __a) ? __b : __a;
246 if (__comp(__b, __a))
252 * @brief This does what you think it does.
253 * @ingroup sorting_algorithms
254 * @param __a A thing of arbitrary type.
255 * @param __b Another thing of arbitrary type.
256 * @param __comp A @link comparison_functors comparison functor@endlink.
257 * @return The greater of the parameters.
259 * This will work on temporary expressions, since they are only evaluated
260 * once, unlike a preprocessor macro.
262 template<typename _Tp, typename _Compare>
265 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
267 //return __comp(__a, __b) ? __b : __a;
268 if (__comp(__a, __b))
273 // If _Iterator is a __normal_iterator return its base (a plain pointer,
274 // normally) otherwise return it untouched. See copy, fill, ...
275 template<typename _Iterator>
277 : _Iter_base<_Iterator, __is_normal_iterator<_Iterator>::__value>
280 template<typename _Iterator>
281 inline typename _Niter_base<_Iterator>::iterator_type
282 __niter_base(_Iterator __it)
283 { return std::_Niter_base<_Iterator>::_S_base(__it); }
285 // Likewise, for move_iterator.
286 template<typename _Iterator>
288 : _Iter_base<_Iterator, __is_move_iterator<_Iterator>::__value>
291 template<typename _Iterator>
292 inline typename _Miter_base<_Iterator>::iterator_type
293 __miter_base(_Iterator __it)
294 { return std::_Miter_base<_Iterator>::_S_base(__it); }
296 // All of these auxiliary structs serve two purposes. (1) Replace
297 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
298 // because the input and output ranges are permitted to overlap.)
299 // (2) If we're using random access iterators, then write the loop as
300 // a for loop with an explicit count.
302 template<bool, bool, typename>
305 template<typename _II, typename _OI>
307 __copy_m(_II __first, _II __last, _OI __result)
309 for (; __first != __last; ++__result, ++__first)
310 *__result = *__first;
315 #if __cplusplus >= 201103L
316 template<typename _Category>
317 struct __copy_move<true, false, _Category>
319 template<typename _II, typename _OI>
321 __copy_m(_II __first, _II __last, _OI __result)
323 for (; __first != __last; ++__result, ++__first)
324 *__result = std::move(*__first);
331 struct __copy_move<false, false, random_access_iterator_tag>
333 template<typename _II, typename _OI>
335 __copy_m(_II __first, _II __last, _OI __result)
337 typedef typename iterator_traits<_II>::difference_type _Distance;
338 for(_Distance __n = __last - __first; __n > 0; --__n)
340 *__result = *__first;
348 #if __cplusplus >= 201103L
350 struct __copy_move<true, false, random_access_iterator_tag>
352 template<typename _II, typename _OI>
354 __copy_m(_II __first, _II __last, _OI __result)
356 typedef typename iterator_traits<_II>::difference_type _Distance;
357 for(_Distance __n = __last - __first; __n > 0; --__n)
359 *__result = std::move(*__first);
368 template<bool _IsMove>
369 struct __copy_move<_IsMove, true, random_access_iterator_tag>
371 template<typename _Tp>
373 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
375 #if __cplusplus >= 201103L
376 // trivial types can have deleted assignment
377 static_assert( is_copy_assignable<_Tp>::value,
378 "type is not assignable" );
380 const ptrdiff_t _Num = __last - __first;
382 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
383 return __result + _Num;
387 template<bool _IsMove, typename _II, typename _OI>
389 __copy_move_a(_II __first, _II __last, _OI __result)
391 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
392 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
393 typedef typename iterator_traits<_II>::iterator_category _Category;
394 const bool __simple = (__is_trivial(_ValueTypeI)
395 && __is_pointer<_II>::__value
396 && __is_pointer<_OI>::__value
397 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
399 return std::__copy_move<_IsMove, __simple,
400 _Category>::__copy_m(__first, __last, __result);
403 // Helpers for streambuf iterators (either istream or ostream).
404 // NB: avoid including <iosfwd>, relatively large.
405 template<typename _CharT>
408 template<typename _CharT, typename _Traits>
409 class istreambuf_iterator;
411 template<typename _CharT, typename _Traits>
412 class ostreambuf_iterator;
414 template<bool _IsMove, typename _CharT>
415 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
416 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
417 __copy_move_a2(_CharT*, _CharT*,
418 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
420 template<bool _IsMove, typename _CharT>
421 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
422 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
423 __copy_move_a2(const _CharT*, const _CharT*,
424 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
426 template<bool _IsMove, typename _CharT>
427 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
429 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
430 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
432 template<bool _IsMove, typename _II, typename _OI>
434 __copy_move_a2(_II __first, _II __last, _OI __result)
436 return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
437 std::__niter_base(__last),
438 std::__niter_base(__result)));
442 * @brief Copies the range [first,last) into result.
443 * @ingroup mutating_algorithms
444 * @param __first An input iterator.
445 * @param __last An input iterator.
446 * @param __result An output iterator.
447 * @return result + (first - last)
449 * This inline function will boil down to a call to @c memmove whenever
450 * possible. Failing that, if random access iterators are passed, then the
451 * loop count will be known (and therefore a candidate for compiler
452 * optimizations such as unrolling). Result may not be contained within
453 * [first,last); the copy_backward function should be used instead.
455 * Note that the end of the output range is permitted to be contained
456 * within [first,last).
458 template<typename _II, typename _OI>
460 copy(_II __first, _II __last, _OI __result)
462 // concept requirements
463 __glibcxx_function_requires(_InputIteratorConcept<_II>)
464 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
465 typename iterator_traits<_II>::value_type>)
466 __glibcxx_requires_valid_range(__first, __last);
468 return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
469 (std::__miter_base(__first), std::__miter_base(__last),
473 #if __cplusplus >= 201103L
475 * @brief Moves the range [first,last) into result.
476 * @ingroup mutating_algorithms
477 * @param __first An input iterator.
478 * @param __last An input iterator.
479 * @param __result An output iterator.
480 * @return result + (first - last)
482 * This inline function will boil down to a call to @c memmove whenever
483 * possible. Failing that, if random access iterators are passed, then the
484 * loop count will be known (and therefore a candidate for compiler
485 * optimizations such as unrolling). Result may not be contained within
486 * [first,last); the move_backward function should be used instead.
488 * Note that the end of the output range is permitted to be contained
489 * within [first,last).
491 template<typename _II, typename _OI>
493 move(_II __first, _II __last, _OI __result)
495 // concept requirements
496 __glibcxx_function_requires(_InputIteratorConcept<_II>)
497 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
498 typename iterator_traits<_II>::value_type>)
499 __glibcxx_requires_valid_range(__first, __last);
501 return std::__copy_move_a2<true>(std::__miter_base(__first),
502 std::__miter_base(__last), __result);
505 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
507 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
510 template<bool, bool, typename>
511 struct __copy_move_backward
513 template<typename _BI1, typename _BI2>
515 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
517 while (__first != __last)
518 *--__result = *--__last;
523 #if __cplusplus >= 201103L
524 template<typename _Category>
525 struct __copy_move_backward<true, false, _Category>
527 template<typename _BI1, typename _BI2>
529 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
531 while (__first != __last)
532 *--__result = std::move(*--__last);
539 struct __copy_move_backward<false, false, random_access_iterator_tag>
541 template<typename _BI1, typename _BI2>
543 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
545 typename iterator_traits<_BI1>::difference_type __n;
546 for (__n = __last - __first; __n > 0; --__n)
547 *--__result = *--__last;
552 #if __cplusplus >= 201103L
554 struct __copy_move_backward<true, false, random_access_iterator_tag>
556 template<typename _BI1, typename _BI2>
558 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
560 typename iterator_traits<_BI1>::difference_type __n;
561 for (__n = __last - __first; __n > 0; --__n)
562 *--__result = std::move(*--__last);
568 template<bool _IsMove>
569 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
571 template<typename _Tp>
573 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
575 #if __cplusplus >= 201103L
576 // trivial types can have deleted assignment
577 static_assert( is_copy_assignable<_Tp>::value,
578 "type is not assignable" );
580 const ptrdiff_t _Num = __last - __first;
582 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
583 return __result - _Num;
587 template<bool _IsMove, typename _BI1, typename _BI2>
589 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
591 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
592 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
593 typedef typename iterator_traits<_BI1>::iterator_category _Category;
594 const bool __simple = (__is_trivial(_ValueType1)
595 && __is_pointer<_BI1>::__value
596 && __is_pointer<_BI2>::__value
597 && __are_same<_ValueType1, _ValueType2>::__value);
599 return std::__copy_move_backward<_IsMove, __simple,
600 _Category>::__copy_move_b(__first,
605 template<bool _IsMove, typename _BI1, typename _BI2>
607 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
609 return _BI2(std::__copy_move_backward_a<_IsMove>
610 (std::__niter_base(__first), std::__niter_base(__last),
611 std::__niter_base(__result)));
615 * @brief Copies the range [first,last) into result.
616 * @ingroup mutating_algorithms
617 * @param __first A bidirectional iterator.
618 * @param __last A bidirectional iterator.
619 * @param __result A bidirectional iterator.
620 * @return result - (first - last)
622 * The function has the same effect as copy, but starts at the end of the
623 * range and works its way to the start, returning the start of the result.
624 * This inline function will boil down to a call to @c memmove whenever
625 * possible. Failing that, if random access iterators are passed, then the
626 * loop count will be known (and therefore a candidate for compiler
627 * optimizations such as unrolling).
629 * Result may not be in the range (first,last]. Use copy instead. Note
630 * that the start of the output range may overlap [first,last).
632 template<typename _BI1, typename _BI2>
634 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
636 // concept requirements
637 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
638 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
639 __glibcxx_function_requires(_ConvertibleConcept<
640 typename iterator_traits<_BI1>::value_type,
641 typename iterator_traits<_BI2>::value_type>)
642 __glibcxx_requires_valid_range(__first, __last);
644 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
645 (std::__miter_base(__first), std::__miter_base(__last),
649 #if __cplusplus >= 201103L
651 * @brief Moves the range [first,last) into result.
652 * @ingroup mutating_algorithms
653 * @param __first A bidirectional iterator.
654 * @param __last A bidirectional iterator.
655 * @param __result A bidirectional iterator.
656 * @return result - (first - last)
658 * The function has the same effect as move, but starts at the end of the
659 * range and works its way to the start, returning the start of the result.
660 * This inline function will boil down to a call to @c memmove whenever
661 * possible. Failing that, if random access iterators are passed, then the
662 * loop count will be known (and therefore a candidate for compiler
663 * optimizations such as unrolling).
665 * Result may not be in the range (first,last]. Use move instead. Note
666 * that the start of the output range may overlap [first,last).
668 template<typename _BI1, typename _BI2>
670 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
672 // concept requirements
673 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
674 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
675 __glibcxx_function_requires(_ConvertibleConcept<
676 typename iterator_traits<_BI1>::value_type,
677 typename iterator_traits<_BI2>::value_type>)
678 __glibcxx_requires_valid_range(__first, __last);
680 return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
681 std::__miter_base(__last),
685 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
687 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
690 template<typename _ForwardIterator, typename _Tp>
692 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
693 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
696 for (; __first != __last; ++__first)
700 template<typename _ForwardIterator, typename _Tp>
702 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
703 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
706 const _Tp __tmp = __value;
707 for (; __first != __last; ++__first)
711 // Specialization: for char types we can use memset.
712 template<typename _Tp>
714 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
715 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
717 const _Tp __tmp = __c;
718 __builtin_memset(__first, static_cast<unsigned char>(__tmp),
723 * @brief Fills the range [first,last) with copies of value.
724 * @ingroup mutating_algorithms
725 * @param __first A forward iterator.
726 * @param __last A forward iterator.
727 * @param __value A reference-to-const of arbitrary type.
730 * This function fills a range with copies of the same value. For char
731 * types filling contiguous areas of memory, this becomes an inline call
732 * to @c memset or @c wmemset.
734 template<typename _ForwardIterator, typename _Tp>
736 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
738 // concept requirements
739 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
741 __glibcxx_requires_valid_range(__first, __last);
743 std::__fill_a(std::__niter_base(__first), std::__niter_base(__last),
747 template<typename _OutputIterator, typename _Size, typename _Tp>
749 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
750 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
752 for (__decltype(__n + 0) __niter = __n;
753 __niter > 0; --__niter, ++__first)
758 template<typename _OutputIterator, typename _Size, typename _Tp>
760 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
761 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
763 const _Tp __tmp = __value;
764 for (__decltype(__n + 0) __niter = __n;
765 __niter > 0; --__niter, ++__first)
770 template<typename _Size, typename _Tp>
772 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
773 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
775 std::__fill_a(__first, __first + __n, __c);
776 return __first + __n;
780 * @brief Fills the range [first,first+n) with copies of value.
781 * @ingroup mutating_algorithms
782 * @param __first An output iterator.
783 * @param __n The count of copies to perform.
784 * @param __value A reference-to-const of arbitrary type.
785 * @return The iterator at first+n.
787 * This function fills a range with copies of the same value. For char
788 * types filling contiguous areas of memory, this becomes an inline call
789 * to @c memset or @ wmemset.
791 * _GLIBCXX_RESOLVE_LIB_DEFECTS
792 * DR 865. More algorithms that throw away information
794 template<typename _OI, typename _Size, typename _Tp>
796 fill_n(_OI __first, _Size __n, const _Tp& __value)
798 // concept requirements
799 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
801 return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value));
804 template<bool _BoolType>
807 template<typename _II1, typename _II2>
809 equal(_II1 __first1, _II1 __last1, _II2 __first2)
811 for (; __first1 != __last1; ++__first1, ++__first2)
812 if (!(*__first1 == *__first2))
821 template<typename _Tp>
823 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
825 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
826 * (__last1 - __first1));
830 template<typename _II1, typename _II2>
832 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
834 typedef typename iterator_traits<_II1>::value_type _ValueType1;
835 typedef typename iterator_traits<_II2>::value_type _ValueType2;
836 const bool __simple = ((__is_integer<_ValueType1>::__value
837 || __is_pointer<_ValueType1>::__value)
838 && __is_pointer<_II1>::__value
839 && __is_pointer<_II2>::__value
840 && __are_same<_ValueType1, _ValueType2>::__value);
842 return std::__equal<__simple>::equal(__first1, __last1, __first2);
845 template<typename, typename>
848 template<typename _II1, typename _II2>
850 __newlast1(_II1, _II1 __last1, _II2, _II2)
853 template<typename _II>
855 __cnd2(_II __first, _II __last)
856 { return __first != __last; }
860 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
862 template<typename _RAI1, typename _RAI2>
864 __newlast1(_RAI1 __first1, _RAI1 __last1,
865 _RAI2 __first2, _RAI2 __last2)
867 const typename iterator_traits<_RAI1>::difference_type
868 __diff1 = __last1 - __first1;
869 const typename iterator_traits<_RAI2>::difference_type
870 __diff2 = __last2 - __first2;
871 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
874 template<typename _RAI>
880 template<typename _II1, typename _II2, typename _Compare>
882 __lexicographical_compare_impl(_II1 __first1, _II1 __last1,
883 _II2 __first2, _II2 __last2,
886 typedef typename iterator_traits<_II1>::iterator_category _Category1;
887 typedef typename iterator_traits<_II2>::iterator_category _Category2;
888 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
890 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
891 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
892 ++__first1, ++__first2)
894 if (__comp(__first1, __first2))
896 if (__comp(__first2, __first1))
899 return __first1 == __last1 && __first2 != __last2;
902 template<bool _BoolType>
903 struct __lexicographical_compare
905 template<typename _II1, typename _II2>
906 static bool __lc(_II1, _II1, _II2, _II2);
909 template<bool _BoolType>
910 template<typename _II1, typename _II2>
912 __lexicographical_compare<_BoolType>::
913 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
915 return std::__lexicographical_compare_impl(__first1, __last1,
917 __gnu_cxx::__ops::__iter_less_iter());
921 struct __lexicographical_compare<true>
923 template<typename _Tp, typename _Up>
925 __lc(const _Tp* __first1, const _Tp* __last1,
926 const _Up* __first2, const _Up* __last2)
928 const size_t __len1 = __last1 - __first1;
929 const size_t __len2 = __last2 - __first2;
930 const int __result = __builtin_memcmp(__first1, __first2,
931 std::min(__len1, __len2));
932 return __result != 0 ? __result < 0 : __len1 < __len2;
936 template<typename _II1, typename _II2>
938 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
939 _II2 __first2, _II2 __last2)
941 typedef typename iterator_traits<_II1>::value_type _ValueType1;
942 typedef typename iterator_traits<_II2>::value_type _ValueType2;
943 const bool __simple =
944 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
945 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
946 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
947 && __is_pointer<_II1>::__value
948 && __is_pointer<_II2>::__value);
950 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
954 template<typename _ForwardIterator, typename _Tp, typename _Compare>
956 __lower_bound(_ForwardIterator __first, _ForwardIterator __last,
957 const _Tp& __val, _Compare __comp)
959 typedef typename iterator_traits<_ForwardIterator>::difference_type
962 _DistanceType __len = std::distance(__first, __last);
966 _DistanceType __half = __len >> 1;
967 _ForwardIterator __middle = __first;
968 std::advance(__middle, __half);
969 if (__comp(__middle, __val))
973 __len = __len - __half - 1;
982 * @brief Finds the first position in which @a val could be inserted
983 * without changing the ordering.
984 * @param __first An iterator.
985 * @param __last Another iterator.
986 * @param __val The search term.
987 * @return An iterator pointing to the first element <em>not less
988 * than</em> @a val, or end() if every element is less than
990 * @ingroup binary_search_algorithms
992 template<typename _ForwardIterator, typename _Tp>
993 inline _ForwardIterator
994 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
997 // concept requirements
998 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
999 __glibcxx_function_requires(_LessThanOpConcept<
1000 typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
1001 __glibcxx_requires_partitioned_lower(__first, __last, __val);
1003 return std::__lower_bound(__first, __last, __val,
1004 __gnu_cxx::__ops::__iter_less_val());
1007 /// This is a helper function for the sort routines and for random.tcc.
1008 // Precondition: __n > 0.
1009 inline _GLIBCXX_CONSTEXPR int
1011 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1013 inline _GLIBCXX_CONSTEXPR unsigned
1015 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
1017 inline _GLIBCXX_CONSTEXPR long
1019 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1021 inline _GLIBCXX_CONSTEXPR unsigned long
1022 __lg(unsigned long __n)
1023 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
1025 inline _GLIBCXX_CONSTEXPR long long
1027 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1029 inline _GLIBCXX_CONSTEXPR unsigned long long
1030 __lg(unsigned long long __n)
1031 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1033 _GLIBCXX_END_NAMESPACE_VERSION
1035 _GLIBCXX_BEGIN_NAMESPACE_ALGO
1038 * @brief Tests a range for element-wise equality.
1039 * @ingroup non_mutating_algorithms
1040 * @param __first1 An input iterator.
1041 * @param __last1 An input iterator.
1042 * @param __first2 An input iterator.
1043 * @return A boolean true or false.
1045 * This compares the elements of two ranges using @c == and returns true or
1046 * false depending on whether all of the corresponding elements of the
1049 template<typename _II1, typename _II2>
1051 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1053 // concept requirements
1054 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1055 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1056 __glibcxx_function_requires(_EqualOpConcept<
1057 typename iterator_traits<_II1>::value_type,
1058 typename iterator_traits<_II2>::value_type>)
1059 __glibcxx_requires_valid_range(__first1, __last1);
1061 return std::__equal_aux(std::__niter_base(__first1),
1062 std::__niter_base(__last1),
1063 std::__niter_base(__first2));
1067 * @brief Tests a range for element-wise equality.
1068 * @ingroup non_mutating_algorithms
1069 * @param __first1 An input iterator.
1070 * @param __last1 An input iterator.
1071 * @param __first2 An input iterator.
1072 * @param __binary_pred A binary predicate @link functors
1074 * @return A boolean true or false.
1076 * This compares the elements of two ranges using the binary_pred
1077 * parameter, and returns true or
1078 * false depending on whether all of the corresponding elements of the
1081 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1083 equal(_IIter1 __first1, _IIter1 __last1,
1084 _IIter2 __first2, _BinaryPredicate __binary_pred)
1086 // concept requirements
1087 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1088 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1089 __glibcxx_requires_valid_range(__first1, __last1);
1091 for (; __first1 != __last1; ++__first1, ++__first2)
1092 if (!bool(__binary_pred(*__first1, *__first2)))
1097 #if __cplusplus > 201103L
1099 #define __cpp_lib_robust_nonmodifying_seq_ops 201304
1102 * @brief Tests a range for element-wise equality.
1103 * @ingroup non_mutating_algorithms
1104 * @param __first1 An input iterator.
1105 * @param __last1 An input iterator.
1106 * @param __first2 An input iterator.
1107 * @param __last2 An input iterator.
1108 * @return A boolean true or false.
1110 * This compares the elements of two ranges using @c == and returns true or
1111 * false depending on whether all of the corresponding elements of the
1114 template<typename _II1, typename _II2>
1116 equal(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
1118 // concept requirements
1119 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1120 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1121 __glibcxx_function_requires(_EqualOpConcept<
1122 typename iterator_traits<_II1>::value_type,
1123 typename iterator_traits<_II2>::value_type>)
1124 __glibcxx_requires_valid_range(__first1, __last1);
1125 __glibcxx_requires_valid_range(__first2, __last2);
1127 using _RATag = random_access_iterator_tag;
1128 using _Cat1 = typename iterator_traits<_II1>::iterator_category;
1129 using _Cat2 = typename iterator_traits<_II2>::iterator_category;
1130 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1133 auto __d1 = std::distance(__first1, __last1);
1134 auto __d2 = std::distance(__first2, __last2);
1137 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2);
1140 for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
1141 if (!(*__first1 == *__first2))
1143 return __first1 == __last1 && __first2 == __last2;
1147 * @brief Tests a range for element-wise equality.
1148 * @ingroup non_mutating_algorithms
1149 * @param __first1 An input iterator.
1150 * @param __last1 An input iterator.
1151 * @param __first2 An input iterator.
1152 * @param __last2 An input iterator.
1153 * @param __binary_pred A binary predicate @link functors
1155 * @return A boolean true or false.
1157 * This compares the elements of two ranges using the binary_pred
1158 * parameter, and returns true or
1159 * false depending on whether all of the corresponding elements of the
1162 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1164 equal(_IIter1 __first1, _IIter1 __last1,
1165 _IIter2 __first2, _IIter2 __last2, _BinaryPredicate __binary_pred)
1167 // concept requirements
1168 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1169 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1170 __glibcxx_requires_valid_range(__first1, __last1);
1171 __glibcxx_requires_valid_range(__first2, __last2);
1173 using _RATag = random_access_iterator_tag;
1174 using _Cat1 = typename iterator_traits<_IIter1>::iterator_category;
1175 using _Cat2 = typename iterator_traits<_IIter2>::iterator_category;
1176 using _RAIters = __and_<is_same<_Cat1, _RATag>, is_same<_Cat2, _RATag>>;
1179 auto __d1 = std::distance(__first1, __last1);
1180 auto __d2 = std::distance(__first2, __last2);
1183 return _GLIBCXX_STD_A::equal(__first1, __last1, __first2,
1187 for (; __first1 != __last1 && __first2 != __last2; ++__first1, ++__first2)
1188 if (!bool(__binary_pred(*__first1, *__first2)))
1190 return __first1 == __last1 && __first2 == __last2;
1195 * @brief Performs @b dictionary comparison on ranges.
1196 * @ingroup sorting_algorithms
1197 * @param __first1 An input iterator.
1198 * @param __last1 An input iterator.
1199 * @param __first2 An input iterator.
1200 * @param __last2 An input iterator.
1201 * @return A boolean true or false.
1203 * <em>Returns true if the sequence of elements defined by the range
1204 * [first1,last1) is lexicographically less than the sequence of elements
1205 * defined by the range [first2,last2). Returns false otherwise.</em>
1206 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1207 * then this is an inline call to @c memcmp.
1209 template<typename _II1, typename _II2>
1211 lexicographical_compare(_II1 __first1, _II1 __last1,
1212 _II2 __first2, _II2 __last2)
1214 #ifdef _GLIBCXX_CONCEPT_CHECKS
1215 // concept requirements
1216 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1217 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1219 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1220 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1221 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1222 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1223 __glibcxx_requires_valid_range(__first1, __last1);
1224 __glibcxx_requires_valid_range(__first2, __last2);
1226 return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1227 std::__niter_base(__last1),
1228 std::__niter_base(__first2),
1229 std::__niter_base(__last2));
1233 * @brief Performs @b dictionary comparison on ranges.
1234 * @ingroup sorting_algorithms
1235 * @param __first1 An input iterator.
1236 * @param __last1 An input iterator.
1237 * @param __first2 An input iterator.
1238 * @param __last2 An input iterator.
1239 * @param __comp A @link comparison_functors comparison functor@endlink.
1240 * @return A boolean true or false.
1242 * The same as the four-parameter @c lexicographical_compare, but uses the
1243 * comp parameter instead of @c <.
1245 template<typename _II1, typename _II2, typename _Compare>
1247 lexicographical_compare(_II1 __first1, _II1 __last1,
1248 _II2 __first2, _II2 __last2, _Compare __comp)
1250 // concept requirements
1251 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1252 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1253 __glibcxx_requires_valid_range(__first1, __last1);
1254 __glibcxx_requires_valid_range(__first2, __last2);
1256 return std::__lexicographical_compare_impl
1257 (__first1, __last1, __first2, __last2,
1258 __gnu_cxx::__ops::__iter_comp_iter(__comp));
1261 template<typename _InputIterator1, typename _InputIterator2,
1262 typename _BinaryPredicate>
1263 pair<_InputIterator1, _InputIterator2>
1264 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1265 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1267 while (__first1 != __last1 && __binary_pred(__first1, __first2))
1272 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1276 * @brief Finds the places in ranges which don't match.
1277 * @ingroup non_mutating_algorithms
1278 * @param __first1 An input iterator.
1279 * @param __last1 An input iterator.
1280 * @param __first2 An input iterator.
1281 * @return A pair of iterators pointing to the first mismatch.
1283 * This compares the elements of two ranges using @c == and returns a pair
1284 * of iterators. The first iterator points into the first range, the
1285 * second iterator points into the second range, and the elements pointed
1286 * to by the iterators are not equal.
1288 template<typename _InputIterator1, typename _InputIterator2>
1289 inline pair<_InputIterator1, _InputIterator2>
1290 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1291 _InputIterator2 __first2)
1293 // concept requirements
1294 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1295 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1296 __glibcxx_function_requires(_EqualOpConcept<
1297 typename iterator_traits<_InputIterator1>::value_type,
1298 typename iterator_traits<_InputIterator2>::value_type>)
1299 __glibcxx_requires_valid_range(__first1, __last1);
1301 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1302 __gnu_cxx::__ops::__iter_equal_to_iter());
1306 * @brief Finds the places in ranges which don't match.
1307 * @ingroup non_mutating_algorithms
1308 * @param __first1 An input iterator.
1309 * @param __last1 An input iterator.
1310 * @param __first2 An input iterator.
1311 * @param __binary_pred A binary predicate @link functors
1313 * @return A pair of iterators pointing to the first mismatch.
1315 * This compares the elements of two ranges using the binary_pred
1316 * parameter, 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 typename _BinaryPredicate>
1323 inline pair<_InputIterator1, _InputIterator2>
1324 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1325 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1327 // concept requirements
1328 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1329 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1330 __glibcxx_requires_valid_range(__first1, __last1);
1332 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2,
1333 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1336 #if __cplusplus > 201103L
1338 template<typename _InputIterator1, typename _InputIterator2,
1339 typename _BinaryPredicate>
1340 pair<_InputIterator1, _InputIterator2>
1341 __mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1342 _InputIterator2 __first2, _InputIterator2 __last2,
1343 _BinaryPredicate __binary_pred)
1345 while (__first1 != __last1 && __first2 != __last2
1346 && __binary_pred(__first1, __first2))
1351 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1355 * @brief Finds the places in ranges which don't match.
1356 * @ingroup non_mutating_algorithms
1357 * @param __first1 An input iterator.
1358 * @param __last1 An input iterator.
1359 * @param __first2 An input iterator.
1360 * @param __last2 An input iterator.
1361 * @return A pair of iterators pointing to the first mismatch.
1363 * This compares the elements of two ranges using @c == and returns a pair
1364 * of iterators. The first iterator points into the first range, the
1365 * second iterator points into the second range, and the elements pointed
1366 * to by the iterators are not equal.
1368 template<typename _InputIterator1, typename _InputIterator2>
1369 inline pair<_InputIterator1, _InputIterator2>
1370 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1371 _InputIterator2 __first2, _InputIterator2 __last2)
1373 // concept requirements
1374 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1375 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1376 __glibcxx_function_requires(_EqualOpConcept<
1377 typename iterator_traits<_InputIterator1>::value_type,
1378 typename iterator_traits<_InputIterator2>::value_type>)
1379 __glibcxx_requires_valid_range(__first1, __last1);
1380 __glibcxx_requires_valid_range(__first2, __last2);
1382 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1383 __gnu_cxx::__ops::__iter_equal_to_iter());
1387 * @brief Finds the places in ranges which don't match.
1388 * @ingroup non_mutating_algorithms
1389 * @param __first1 An input iterator.
1390 * @param __last1 An input iterator.
1391 * @param __first2 An input iterator.
1392 * @param __last2 An input iterator.
1393 * @param __binary_pred A binary predicate @link functors
1395 * @return A pair of iterators pointing to the first mismatch.
1397 * This compares the elements of two ranges using the binary_pred
1398 * parameter, and returns a pair
1399 * of iterators. The first iterator points into the first range, the
1400 * second iterator points into the second range, and the elements pointed
1401 * to by the iterators are not equal.
1403 template<typename _InputIterator1, typename _InputIterator2,
1404 typename _BinaryPredicate>
1405 inline pair<_InputIterator1, _InputIterator2>
1406 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1407 _InputIterator2 __first2, _InputIterator2 __last2,
1408 _BinaryPredicate __binary_pred)
1410 // concept requirements
1411 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1412 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1413 __glibcxx_requires_valid_range(__first1, __last1);
1414 __glibcxx_requires_valid_range(__first2, __last2);
1416 return _GLIBCXX_STD_A::__mismatch(__first1, __last1, __first2, __last2,
1417 __gnu_cxx::__ops::__iter_comp_iter(__binary_pred));
1421 _GLIBCXX_END_NAMESPACE_ALGO
1424 // NB: This file is included within many other C++ includes, as a way
1425 // of getting the base algorithms. So, make sure that parallel bits
1426 // come in too if requested.
1427 #ifdef _GLIBCXX_PARALLEL
1428 # include <parallel/algobase.h>