1 // Core algorithmic facilities -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
4 // 2011 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
29 * Hewlett-Packard Company
31 * Permission to use, copy, modify, distribute and sell this software
32 * and its documentation for any purpose is hereby granted without fee,
33 * provided that the above copyright notice appear in all copies and
34 * that both that copyright notice and this permission notice appear
35 * in supporting documentation. Hewlett-Packard Company makes no
36 * representations about the suitability of this software for any
37 * purpose. It is provided "as is" without express or implied warranty.
40 * Copyright (c) 1996-1998
41 * Silicon Graphics Computer Systems, Inc.
43 * Permission to use, copy, modify, distribute and sell this software
44 * and its documentation for any purpose is hereby granted without fee,
45 * provided that the above copyright notice appear in all copies and
46 * that both that copyright notice and this permission notice appear
47 * in supporting documentation. Silicon Graphics makes no
48 * representations about the suitability of this software for any
49 * purpose. It is provided "as is" without express or implied warranty.
52 /** @file bits/stl_algobase.h
53 * This is an internal header file, included by other library headers.
54 * Do not attempt to use it directly. @headername{algorithm}
57 #ifndef _STL_ALGOBASE_H
58 #define _STL_ALGOBASE_H 1
60 #include <bits/c++config.h>
61 #include <bits/functexcept.h>
62 #include <bits/cpp_type_traits.h>
63 #include <ext/type_traits.h>
64 #include <ext/numeric_traits.h>
65 #include <bits/stl_pair.h>
66 #include <bits/stl_iterator_base_types.h>
67 #include <bits/stl_iterator_base_funcs.h>
68 #include <bits/stl_iterator.h>
69 #include <bits/concept_check.h>
70 #include <debug/debug.h>
71 #include <bits/move.h> // For std::swap and _GLIBCXX_MOVE
73 namespace std _GLIBCXX_VISIBILITY(default)
75 _GLIBCXX_BEGIN_NAMESPACE_VERSION
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)
107 * @brief Swaps the contents of two iterators.
108 * @ingroup mutating_algorithms
109 * @param __a An iterator.
110 * @param __b Another iterator.
113 * This function swaps the values pointed to by two iterators, not the
114 * iterators themselves.
116 template<typename _ForwardIterator1, typename _ForwardIterator2>
118 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
120 typedef typename iterator_traits<_ForwardIterator1>::value_type
122 typedef typename iterator_traits<_ForwardIterator2>::value_type
125 // concept requirements
126 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
128 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
130 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
132 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
135 typedef typename iterator_traits<_ForwardIterator1>::reference
137 typedef typename iterator_traits<_ForwardIterator2>::reference
139 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
140 && __are_same<_ValueType1&, _ReferenceType1>::__value
141 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
146 * @brief Swap the elements of two sequences.
147 * @ingroup mutating_algorithms
148 * @param __first1 A forward iterator.
149 * @param __last1 A forward iterator.
150 * @param __first2 A forward iterator.
151 * @return An iterator equal to @p first2+(last1-first1).
153 * Swaps each element in the range @p [first1,last1) with the
154 * corresponding element in the range @p [first2,(last1-first1)).
155 * The ranges must not overlap.
157 template<typename _ForwardIterator1, typename _ForwardIterator2>
159 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
160 _ForwardIterator2 __first2)
162 // concept requirements
163 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
165 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
167 __glibcxx_requires_valid_range(__first1, __last1);
169 for (; __first1 != __last1; ++__first1, ++__first2)
170 std::iter_swap(__first1, __first2);
175 * @brief This does what you think it does.
176 * @ingroup sorting_algorithms
177 * @param __a A thing of arbitrary type.
178 * @param __b Another thing of arbitrary type.
179 * @return The lesser of the parameters.
181 * This is the simple classic generic implementation. It will work on
182 * temporary expressions, since they are only evaluated once, unlike a
183 * preprocessor macro.
185 template<typename _Tp>
187 min(const _Tp& __a, const _Tp& __b)
189 // concept requirements
190 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
191 //return __b < __a ? __b : __a;
198 * @brief This does what you think it does.
199 * @ingroup sorting_algorithms
200 * @param __a A thing of arbitrary type.
201 * @param __b Another thing of arbitrary type.
202 * @return The greater of the parameters.
204 * This is the simple classic generic implementation. It will work on
205 * temporary expressions, since they are only evaluated once, unlike a
206 * preprocessor macro.
208 template<typename _Tp>
210 max(const _Tp& __a, const _Tp& __b)
212 // concept requirements
213 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
214 //return __a < __b ? __b : __a;
221 * @brief This does what you think it does.
222 * @ingroup sorting_algorithms
223 * @param __a A thing of arbitrary type.
224 * @param __b Another thing of arbitrary type.
225 * @param __comp A @link comparison_functors comparison functor@endlink.
226 * @return The lesser of the parameters.
228 * This will work on temporary expressions, since they are only evaluated
229 * once, unlike a preprocessor macro.
231 template<typename _Tp, typename _Compare>
233 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
235 //return __comp(__b, __a) ? __b : __a;
236 if (__comp(__b, __a))
242 * @brief This does what you think it does.
243 * @ingroup sorting_algorithms
244 * @param __a A thing of arbitrary type.
245 * @param __b Another thing of arbitrary type.
246 * @param __comp A @link comparison_functors comparison functor@endlink.
247 * @return The greater of the parameters.
249 * This will work on temporary expressions, since they are only evaluated
250 * once, unlike a preprocessor macro.
252 template<typename _Tp, typename _Compare>
254 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
256 //return __comp(__a, __b) ? __b : __a;
257 if (__comp(__a, __b))
262 // If _Iterator is a __normal_iterator return its base (a plain pointer,
263 // normally) otherwise return it untouched. See copy, fill, ...
264 template<typename _Iterator>
266 : _Iter_base<_Iterator, __is_normal_iterator<_Iterator>::__value>
269 template<typename _Iterator>
270 inline typename _Niter_base<_Iterator>::iterator_type
271 __niter_base(_Iterator __it)
272 { return std::_Niter_base<_Iterator>::_S_base(__it); }
274 // Likewise, for move_iterator.
275 template<typename _Iterator>
277 : _Iter_base<_Iterator, __is_move_iterator<_Iterator>::__value>
280 template<typename _Iterator>
281 inline typename _Miter_base<_Iterator>::iterator_type
282 __miter_base(_Iterator __it)
283 { return std::_Miter_base<_Iterator>::_S_base(__it); }
285 // All of these auxiliary structs serve two purposes. (1) Replace
286 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
287 // because the input and output ranges are permitted to overlap.)
288 // (2) If we're using random access iterators, then write the loop as
289 // a for loop with an explicit count.
291 template<bool, bool, typename>
294 template<typename _II, typename _OI>
296 __copy_m(_II __first, _II __last, _OI __result)
298 for (; __first != __last; ++__result, ++__first)
299 *__result = *__first;
304 #ifdef __GXX_EXPERIMENTAL_CXX0X__
305 template<typename _Category>
306 struct __copy_move<true, false, _Category>
308 template<typename _II, typename _OI>
310 __copy_m(_II __first, _II __last, _OI __result)
312 for (; __first != __last; ++__result, ++__first)
313 *__result = std::move(*__first);
320 struct __copy_move<false, false, random_access_iterator_tag>
322 template<typename _II, typename _OI>
324 __copy_m(_II __first, _II __last, _OI __result)
326 typedef typename iterator_traits<_II>::difference_type _Distance;
327 for(_Distance __n = __last - __first; __n > 0; --__n)
329 *__result = *__first;
337 #ifdef __GXX_EXPERIMENTAL_CXX0X__
339 struct __copy_move<true, false, random_access_iterator_tag>
341 template<typename _II, typename _OI>
343 __copy_m(_II __first, _II __last, _OI __result)
345 typedef typename iterator_traits<_II>::difference_type _Distance;
346 for(_Distance __n = __last - __first; __n > 0; --__n)
348 *__result = std::move(*__first);
357 template<bool _IsMove>
358 struct __copy_move<_IsMove, true, random_access_iterator_tag>
360 template<typename _Tp>
362 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
364 const ptrdiff_t _Num = __last - __first;
366 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
367 return __result + _Num;
371 template<bool _IsMove, typename _II, typename _OI>
373 __copy_move_a(_II __first, _II __last, _OI __result)
375 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
376 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
377 typedef typename iterator_traits<_II>::iterator_category _Category;
378 const bool __simple = (__is_trivial(_ValueTypeI)
379 && __is_pointer<_II>::__value
380 && __is_pointer<_OI>::__value
381 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
383 return std::__copy_move<_IsMove, __simple,
384 _Category>::__copy_m(__first, __last, __result);
387 // Helpers for streambuf iterators (either istream or ostream).
388 // NB: avoid including <iosfwd>, relatively large.
389 template<typename _CharT>
392 template<typename _CharT, typename _Traits>
393 class istreambuf_iterator;
395 template<typename _CharT, typename _Traits>
396 class ostreambuf_iterator;
398 template<bool _IsMove, typename _CharT>
399 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
400 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
401 __copy_move_a2(_CharT*, _CharT*,
402 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
404 template<bool _IsMove, typename _CharT>
405 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
406 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
407 __copy_move_a2(const _CharT*, const _CharT*,
408 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
410 template<bool _IsMove, typename _CharT>
411 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
413 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
414 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
416 template<bool _IsMove, typename _II, typename _OI>
418 __copy_move_a2(_II __first, _II __last, _OI __result)
420 return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
421 std::__niter_base(__last),
422 std::__niter_base(__result)));
426 * @brief Copies the range [first,last) into result.
427 * @ingroup mutating_algorithms
428 * @param __first An input iterator.
429 * @param __last An input iterator.
430 * @param __result An output iterator.
431 * @return result + (first - last)
433 * This inline function will boil down to a call to @c memmove whenever
434 * possible. Failing that, if random access iterators are passed, then the
435 * loop count will be known (and therefore a candidate for compiler
436 * optimizations such as unrolling). Result may not be contained within
437 * [first,last); the copy_backward function should be used instead.
439 * Note that the end of the output range is permitted to be contained
440 * within [first,last).
442 template<typename _II, typename _OI>
444 copy(_II __first, _II __last, _OI __result)
446 // concept requirements
447 __glibcxx_function_requires(_InputIteratorConcept<_II>)
448 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
449 typename iterator_traits<_II>::value_type>)
450 __glibcxx_requires_valid_range(__first, __last);
452 return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
453 (std::__miter_base(__first), std::__miter_base(__last),
457 #ifdef __GXX_EXPERIMENTAL_CXX0X__
459 * @brief Moves the range [first,last) into result.
460 * @ingroup mutating_algorithms
461 * @param __first An input iterator.
462 * @param __last An input iterator.
463 * @param __result An output iterator.
464 * @return result + (first - last)
466 * This inline function will boil down to a call to @c memmove whenever
467 * possible. Failing that, if random access iterators are passed, then the
468 * loop count will be known (and therefore a candidate for compiler
469 * optimizations such as unrolling). Result may not be contained within
470 * [first,last); the move_backward function should be used instead.
472 * Note that the end of the output range is permitted to be contained
473 * within [first,last).
475 template<typename _II, typename _OI>
477 move(_II __first, _II __last, _OI __result)
479 // concept requirements
480 __glibcxx_function_requires(_InputIteratorConcept<_II>)
481 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
482 typename iterator_traits<_II>::value_type>)
483 __glibcxx_requires_valid_range(__first, __last);
485 return std::__copy_move_a2<true>(std::__miter_base(__first),
486 std::__miter_base(__last), __result);
489 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
491 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
494 template<bool, bool, typename>
495 struct __copy_move_backward
497 template<typename _BI1, typename _BI2>
499 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
501 while (__first != __last)
502 *--__result = *--__last;
507 #ifdef __GXX_EXPERIMENTAL_CXX0X__
508 template<typename _Category>
509 struct __copy_move_backward<true, false, _Category>
511 template<typename _BI1, typename _BI2>
513 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
515 while (__first != __last)
516 *--__result = std::move(*--__last);
523 struct __copy_move_backward<false, false, random_access_iterator_tag>
525 template<typename _BI1, typename _BI2>
527 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
529 typename iterator_traits<_BI1>::difference_type __n;
530 for (__n = __last - __first; __n > 0; --__n)
531 *--__result = *--__last;
536 #ifdef __GXX_EXPERIMENTAL_CXX0X__
538 struct __copy_move_backward<true, false, random_access_iterator_tag>
540 template<typename _BI1, typename _BI2>
542 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
544 typename iterator_traits<_BI1>::difference_type __n;
545 for (__n = __last - __first; __n > 0; --__n)
546 *--__result = std::move(*--__last);
552 template<bool _IsMove>
553 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
555 template<typename _Tp>
557 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
559 const ptrdiff_t _Num = __last - __first;
561 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
562 return __result - _Num;
566 template<bool _IsMove, typename _BI1, typename _BI2>
568 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
570 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
571 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
572 typedef typename iterator_traits<_BI1>::iterator_category _Category;
573 const bool __simple = (__is_trivial(_ValueType1)
574 && __is_pointer<_BI1>::__value
575 && __is_pointer<_BI2>::__value
576 && __are_same<_ValueType1, _ValueType2>::__value);
578 return std::__copy_move_backward<_IsMove, __simple,
579 _Category>::__copy_move_b(__first,
584 template<bool _IsMove, typename _BI1, typename _BI2>
586 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
588 return _BI2(std::__copy_move_backward_a<_IsMove>
589 (std::__niter_base(__first), std::__niter_base(__last),
590 std::__niter_base(__result)));
594 * @brief Copies the range [first,last) into result.
595 * @ingroup mutating_algorithms
596 * @param __first A bidirectional iterator.
597 * @param __last A bidirectional iterator.
598 * @param __result A bidirectional iterator.
599 * @return result - (first - last)
601 * The function has the same effect as copy, but starts at the end of the
602 * range and works its way to the start, returning the start of the result.
603 * This inline function will boil down to a call to @c memmove whenever
604 * possible. Failing that, if random access iterators are passed, then the
605 * loop count will be known (and therefore a candidate for compiler
606 * optimizations such as unrolling).
608 * Result may not be in the range [first,last). Use copy instead. Note
609 * that the start of the output range may overlap [first,last).
611 template<typename _BI1, typename _BI2>
613 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
615 // concept requirements
616 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
617 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
618 __glibcxx_function_requires(_ConvertibleConcept<
619 typename iterator_traits<_BI1>::value_type,
620 typename iterator_traits<_BI2>::value_type>)
621 __glibcxx_requires_valid_range(__first, __last);
623 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
624 (std::__miter_base(__first), std::__miter_base(__last),
628 #ifdef __GXX_EXPERIMENTAL_CXX0X__
630 * @brief Moves the range [first,last) into result.
631 * @ingroup mutating_algorithms
632 * @param __first A bidirectional iterator.
633 * @param __last A bidirectional iterator.
634 * @param __result A bidirectional iterator.
635 * @return result - (first - last)
637 * The function has the same effect as move, but starts at the end of the
638 * range and works its way to the start, returning the start of the result.
639 * This inline function will boil down to a call to @c memmove whenever
640 * possible. Failing that, if random access iterators are passed, then the
641 * loop count will be known (and therefore a candidate for compiler
642 * optimizations such as unrolling).
644 * Result may not be in the range (first,last]. Use move instead. Note
645 * that the start of the output range may overlap [first,last).
647 template<typename _BI1, typename _BI2>
649 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
651 // concept requirements
652 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
653 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
654 __glibcxx_function_requires(_ConvertibleConcept<
655 typename iterator_traits<_BI1>::value_type,
656 typename iterator_traits<_BI2>::value_type>)
657 __glibcxx_requires_valid_range(__first, __last);
659 return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
660 std::__miter_base(__last),
664 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
666 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
669 template<typename _ForwardIterator, typename _Tp>
671 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
672 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
675 for (; __first != __last; ++__first)
679 template<typename _ForwardIterator, typename _Tp>
681 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
682 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
685 const _Tp __tmp = __value;
686 for (; __first != __last; ++__first)
690 // Specialization: for char types we can use memset.
691 template<typename _Tp>
693 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
694 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
696 const _Tp __tmp = __c;
697 __builtin_memset(__first, static_cast<unsigned char>(__tmp),
702 * @brief Fills the range [first,last) with copies of value.
703 * @ingroup mutating_algorithms
704 * @param __first A forward iterator.
705 * @param __last A forward iterator.
706 * @param __value A reference-to-const of arbitrary type.
709 * This function fills a range with copies of the same value. For char
710 * types filling contiguous areas of memory, this becomes an inline call
711 * to @c memset or @c wmemset.
713 template<typename _ForwardIterator, typename _Tp>
715 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
717 // concept requirements
718 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
720 __glibcxx_requires_valid_range(__first, __last);
722 std::__fill_a(std::__niter_base(__first), std::__niter_base(__last),
726 template<typename _OutputIterator, typename _Size, typename _Tp>
728 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
729 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
731 for (__decltype(__n + 0) __niter = __n;
732 __niter > 0; --__niter, ++__first)
737 template<typename _OutputIterator, typename _Size, typename _Tp>
739 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
740 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
742 const _Tp __tmp = __value;
743 for (__decltype(__n + 0) __niter = __n;
744 __niter > 0; --__niter, ++__first)
749 template<typename _Size, typename _Tp>
751 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
752 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
754 std::__fill_a(__first, __first + __n, __c);
755 return __first + __n;
759 * @brief Fills the range [first,first+n) with copies of value.
760 * @ingroup mutating_algorithms
761 * @param __first An output iterator.
762 * @param __n The count of copies to perform.
763 * @param __value A reference-to-const of arbitrary type.
764 * @return The iterator at first+n.
766 * This function fills a range with copies of the same value. For char
767 * types filling contiguous areas of memory, this becomes an inline call
768 * to @c memset or @ wmemset.
770 * _GLIBCXX_RESOLVE_LIB_DEFECTS
771 * DR 865. More algorithms that throw away information
773 template<typename _OI, typename _Size, typename _Tp>
775 fill_n(_OI __first, _Size __n, const _Tp& __value)
777 // concept requirements
778 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
780 return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value));
783 template<bool _BoolType>
786 template<typename _II1, typename _II2>
788 equal(_II1 __first1, _II1 __last1, _II2 __first2)
790 for (; __first1 != __last1; ++__first1, ++__first2)
791 if (!(*__first1 == *__first2))
800 template<typename _Tp>
802 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
804 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
805 * (__last1 - __first1));
809 template<typename _II1, typename _II2>
811 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
813 typedef typename iterator_traits<_II1>::value_type _ValueType1;
814 typedef typename iterator_traits<_II2>::value_type _ValueType2;
815 const bool __simple = ((__is_integer<_ValueType1>::__value
816 || __is_pointer<_ValueType1>::__value)
817 && __is_pointer<_II1>::__value
818 && __is_pointer<_II2>::__value
819 && __are_same<_ValueType1, _ValueType2>::__value);
821 return std::__equal<__simple>::equal(__first1, __last1, __first2);
825 template<typename, typename>
828 template<typename _II1, typename _II2>
830 __newlast1(_II1, _II1 __last1, _II2, _II2)
833 template<typename _II>
835 __cnd2(_II __first, _II __last)
836 { return __first != __last; }
840 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
842 template<typename _RAI1, typename _RAI2>
844 __newlast1(_RAI1 __first1, _RAI1 __last1,
845 _RAI2 __first2, _RAI2 __last2)
847 const typename iterator_traits<_RAI1>::difference_type
848 __diff1 = __last1 - __first1;
849 const typename iterator_traits<_RAI2>::difference_type
850 __diff2 = __last2 - __first2;
851 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
854 template<typename _RAI>
860 template<bool _BoolType>
861 struct __lexicographical_compare
863 template<typename _II1, typename _II2>
864 static bool __lc(_II1, _II1, _II2, _II2);
867 template<bool _BoolType>
868 template<typename _II1, typename _II2>
870 __lexicographical_compare<_BoolType>::
871 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
873 typedef typename iterator_traits<_II1>::iterator_category _Category1;
874 typedef typename iterator_traits<_II2>::iterator_category _Category2;
875 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
877 __last1 = __rai_type::__newlast1(__first1, __last1,
879 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
880 ++__first1, ++__first2)
882 if (*__first1 < *__first2)
884 if (*__first2 < *__first1)
887 return __first1 == __last1 && __first2 != __last2;
891 struct __lexicographical_compare<true>
893 template<typename _Tp, typename _Up>
895 __lc(const _Tp* __first1, const _Tp* __last1,
896 const _Up* __first2, const _Up* __last2)
898 const size_t __len1 = __last1 - __first1;
899 const size_t __len2 = __last2 - __first2;
900 const int __result = __builtin_memcmp(__first1, __first2,
901 std::min(__len1, __len2));
902 return __result != 0 ? __result < 0 : __len1 < __len2;
906 template<typename _II1, typename _II2>
908 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
909 _II2 __first2, _II2 __last2)
911 typedef typename iterator_traits<_II1>::value_type _ValueType1;
912 typedef typename iterator_traits<_II2>::value_type _ValueType2;
913 const bool __simple =
914 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
915 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
916 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
917 && __is_pointer<_II1>::__value
918 && __is_pointer<_II2>::__value);
920 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
925 * @brief Finds the first position in which @a val could be inserted
926 * without changing the ordering.
927 * @param __first An iterator.
928 * @param __last Another iterator.
929 * @param __val The search term.
930 * @return An iterator pointing to the first element <em>not less
931 * than</em> @a val, or end() if every element is less than
933 * @ingroup binary_search_algorithms
935 template<typename _ForwardIterator, typename _Tp>
937 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
940 typedef typename iterator_traits<_ForwardIterator>::value_type
942 typedef typename iterator_traits<_ForwardIterator>::difference_type
945 // concept requirements
946 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
947 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
948 __glibcxx_requires_partitioned_lower(__first, __last, __val);
950 _DistanceType __len = std::distance(__first, __last);
954 _DistanceType __half = __len >> 1;
955 _ForwardIterator __middle = __first;
956 std::advance(__middle, __half);
957 if (*__middle < __val)
961 __len = __len - __half - 1;
969 /// This is a helper function for the sort routines and for random.tcc.
970 // Precondition: __n > 0.
971 template<typename _Size>
976 for (__k = 0; __n != 0; __n >>= 1)
983 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
987 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
991 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
994 __lg(unsigned long __n)
995 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
999 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1001 inline unsigned long long
1002 __lg(unsigned long long __n)
1003 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
1005 _GLIBCXX_END_NAMESPACE_VERSION
1007 _GLIBCXX_BEGIN_NAMESPACE_ALGO
1010 * @brief Tests a range for element-wise equality.
1011 * @ingroup non_mutating_algorithms
1012 * @param __first1 An input iterator.
1013 * @param __last1 An input iterator.
1014 * @param __first2 An input iterator.
1015 * @return A boolean true or false.
1017 * This compares the elements of two ranges using @c == and returns true or
1018 * false depending on whether all of the corresponding elements of the
1021 template<typename _II1, typename _II2>
1023 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1025 // concept requirements
1026 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1027 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1028 __glibcxx_function_requires(_EqualOpConcept<
1029 typename iterator_traits<_II1>::value_type,
1030 typename iterator_traits<_II2>::value_type>)
1031 __glibcxx_requires_valid_range(__first1, __last1);
1033 return std::__equal_aux(std::__niter_base(__first1),
1034 std::__niter_base(__last1),
1035 std::__niter_base(__first2));
1039 * @brief Tests a range for element-wise equality.
1040 * @ingroup non_mutating_algorithms
1041 * @param __first1 An input iterator.
1042 * @param __last1 An input iterator.
1043 * @param __first2 An input iterator.
1044 * @param __binary_pred A binary predicate @link functors
1046 * @return A boolean true or false.
1048 * This compares the elements of two ranges using the binary_pred
1049 * parameter, and returns true or
1050 * false depending on whether all of the corresponding elements of the
1053 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1055 equal(_IIter1 __first1, _IIter1 __last1,
1056 _IIter2 __first2, _BinaryPredicate __binary_pred)
1058 // concept requirements
1059 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1060 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1061 __glibcxx_requires_valid_range(__first1, __last1);
1063 for (; __first1 != __last1; ++__first1, ++__first2)
1064 if (!bool(__binary_pred(*__first1, *__first2)))
1070 * @brief Performs @b dictionary comparison on ranges.
1071 * @ingroup sorting_algorithms
1072 * @param __first1 An input iterator.
1073 * @param __last1 An input iterator.
1074 * @param __first2 An input iterator.
1075 * @param __last2 An input iterator.
1076 * @return A boolean true or false.
1078 * <em>Returns true if the sequence of elements defined by the range
1079 * [first1,last1) is lexicographically less than the sequence of elements
1080 * defined by the range [first2,last2). Returns false otherwise.</em>
1081 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1082 * then this is an inline call to @c memcmp.
1084 template<typename _II1, typename _II2>
1086 lexicographical_compare(_II1 __first1, _II1 __last1,
1087 _II2 __first2, _II2 __last2)
1089 // concept requirements
1090 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1091 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1092 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1093 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1094 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1095 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1096 __glibcxx_requires_valid_range(__first1, __last1);
1097 __glibcxx_requires_valid_range(__first2, __last2);
1099 return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1100 std::__niter_base(__last1),
1101 std::__niter_base(__first2),
1102 std::__niter_base(__last2));
1106 * @brief Performs @b dictionary comparison on ranges.
1107 * @ingroup sorting_algorithms
1108 * @param __first1 An input iterator.
1109 * @param __last1 An input iterator.
1110 * @param __first2 An input iterator.
1111 * @param __last2 An input iterator.
1112 * @param __comp A @link comparison_functors comparison functor@endlink.
1113 * @return A boolean true or false.
1115 * The same as the four-parameter @c lexicographical_compare, but uses the
1116 * comp parameter instead of @c <.
1118 template<typename _II1, typename _II2, typename _Compare>
1120 lexicographical_compare(_II1 __first1, _II1 __last1,
1121 _II2 __first2, _II2 __last2, _Compare __comp)
1123 typedef typename iterator_traits<_II1>::iterator_category _Category1;
1124 typedef typename iterator_traits<_II2>::iterator_category _Category2;
1125 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
1127 // concept requirements
1128 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1129 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1130 __glibcxx_requires_valid_range(__first1, __last1);
1131 __glibcxx_requires_valid_range(__first2, __last2);
1133 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1134 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
1135 ++__first1, ++__first2)
1137 if (__comp(*__first1, *__first2))
1139 if (__comp(*__first2, *__first1))
1142 return __first1 == __last1 && __first2 != __last2;
1146 * @brief Finds the places in ranges which don't match.
1147 * @ingroup non_mutating_algorithms
1148 * @param __first1 An input iterator.
1149 * @param __last1 An input iterator.
1150 * @param __first2 An input iterator.
1151 * @return A pair of iterators pointing to the first mismatch.
1153 * This compares the elements of two ranges using @c == and returns a pair
1154 * of iterators. The first iterator points into the first range, the
1155 * second iterator points into the second range, and the elements pointed
1156 * to by the iterators are not equal.
1158 template<typename _InputIterator1, typename _InputIterator2>
1159 pair<_InputIterator1, _InputIterator2>
1160 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1161 _InputIterator2 __first2)
1163 // concept requirements
1164 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1165 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1166 __glibcxx_function_requires(_EqualOpConcept<
1167 typename iterator_traits<_InputIterator1>::value_type,
1168 typename iterator_traits<_InputIterator2>::value_type>)
1169 __glibcxx_requires_valid_range(__first1, __last1);
1171 while (__first1 != __last1 && *__first1 == *__first2)
1176 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1180 * @brief Finds the places in ranges which don't match.
1181 * @ingroup non_mutating_algorithms
1182 * @param __first1 An input iterator.
1183 * @param __last1 An input iterator.
1184 * @param __first2 An input iterator.
1185 * @param __binary_pred A binary predicate @link functors
1187 * @return A pair of iterators pointing to the first mismatch.
1189 * This compares the elements of two ranges using the binary_pred
1190 * parameter, and returns a pair
1191 * of iterators. The first iterator points into the first range, the
1192 * second iterator points into the second range, and the elements pointed
1193 * to by the iterators are not equal.
1195 template<typename _InputIterator1, typename _InputIterator2,
1196 typename _BinaryPredicate>
1197 pair<_InputIterator1, _InputIterator2>
1198 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1199 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1201 // concept requirements
1202 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1203 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1204 __glibcxx_requires_valid_range(__first1, __last1);
1206 while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
1211 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1214 _GLIBCXX_END_NAMESPACE_ALGO
1217 // NB: This file is included within many other C++ includes, as a way
1218 // of getting the base algorithms. So, make sure that parallel bits
1219 // come in too if requested.
1220 #ifdef _GLIBCXX_PARALLEL
1221 # include <parallel/algobase.h>