1 // Core algorithmic facilities -*- C++ -*-
3 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // 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 stl_algobase.h
53 * This is an internal header file, included by other library headers.
54 * You should not attempt to use it directly.
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 _GLIBCXX_BEGIN_NAMESPACE(std)
75 // See http://gcc.gnu.org/ml/libstdc++/2004-08/msg00167.html: in a
76 // nutshell, we are partially implementing the resolution of DR 187,
77 // when it's safe, i.e., the value_types are equal.
78 template<bool _BoolType>
81 template<typename _ForwardIterator1, typename _ForwardIterator2>
83 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
85 typedef typename iterator_traits<_ForwardIterator1>::value_type
87 _ValueType1 __tmp = _GLIBCXX_MOVE(*__a);
88 *__a = _GLIBCXX_MOVE(*__b);
89 *__b = _GLIBCXX_MOVE(__tmp);
94 struct __iter_swap<true>
96 template<typename _ForwardIterator1, typename _ForwardIterator2>
98 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
105 * @brief Swaps the contents of two iterators.
106 * @ingroup mutating_algorithms
107 * @param a An iterator.
108 * @param b Another iterator.
111 * This function swaps the values pointed to by two iterators, not the
112 * iterators themselves.
114 template<typename _ForwardIterator1, typename _ForwardIterator2>
116 iter_swap(_ForwardIterator1 __a, _ForwardIterator2 __b)
118 typedef typename iterator_traits<_ForwardIterator1>::value_type
120 typedef typename iterator_traits<_ForwardIterator2>::value_type
123 // concept requirements
124 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
126 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
128 __glibcxx_function_requires(_ConvertibleConcept<_ValueType1,
130 __glibcxx_function_requires(_ConvertibleConcept<_ValueType2,
133 typedef typename iterator_traits<_ForwardIterator1>::reference
135 typedef typename iterator_traits<_ForwardIterator2>::reference
137 std::__iter_swap<__are_same<_ValueType1, _ValueType2>::__value
138 && __are_same<_ValueType1&, _ReferenceType1>::__value
139 && __are_same<_ValueType2&, _ReferenceType2>::__value>::
144 * @brief Swap the elements of two sequences.
145 * @ingroup mutating_algorithms
146 * @param first1 A forward iterator.
147 * @param last1 A forward iterator.
148 * @param first2 A forward iterator.
149 * @return An iterator equal to @p first2+(last1-first1).
151 * Swaps each element in the range @p [first1,last1) with the
152 * corresponding element in the range @p [first2,(last1-first1)).
153 * The ranges must not overlap.
155 template<typename _ForwardIterator1, typename _ForwardIterator2>
157 swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
158 _ForwardIterator2 __first2)
160 // concept requirements
161 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
163 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
165 __glibcxx_requires_valid_range(__first1, __last1);
167 for (; __first1 != __last1; ++__first1, ++__first2)
168 std::iter_swap(__first1, __first2);
173 * @brief This does what you think it does.
174 * @ingroup sorting_algorithms
175 * @param a A thing of arbitrary type.
176 * @param b Another thing of arbitrary type.
177 * @return The lesser of the parameters.
179 * This is the simple classic generic implementation. It will work on
180 * temporary expressions, since they are only evaluated once, unlike a
181 * preprocessor macro.
183 template<typename _Tp>
185 min(const _Tp& __a, const _Tp& __b)
187 // concept requirements
188 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
189 //return __b < __a ? __b : __a;
196 * @brief This does what you think it does.
197 * @ingroup sorting_algorithms
198 * @param a A thing of arbitrary type.
199 * @param b Another thing of arbitrary type.
200 * @return The greater of the parameters.
202 * This is the simple classic generic implementation. It will work on
203 * temporary expressions, since they are only evaluated once, unlike a
204 * preprocessor macro.
206 template<typename _Tp>
208 max(const _Tp& __a, const _Tp& __b)
210 // concept requirements
211 __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
212 //return __a < __b ? __b : __a;
219 * @brief This does what you think it does.
220 * @ingroup sorting_algorithms
221 * @param a A thing of arbitrary type.
222 * @param b Another thing of arbitrary type.
223 * @param comp A @link comparison_functors comparison functor@endlink.
224 * @return The lesser of the parameters.
226 * This will work on temporary expressions, since they are only evaluated
227 * once, unlike a preprocessor macro.
229 template<typename _Tp, typename _Compare>
231 min(const _Tp& __a, const _Tp& __b, _Compare __comp)
233 //return __comp(__b, __a) ? __b : __a;
234 if (__comp(__b, __a))
240 * @brief This does what you think it does.
241 * @ingroup sorting_algorithms
242 * @param a A thing of arbitrary type.
243 * @param b Another thing of arbitrary type.
244 * @param comp A @link comparison_functors comparison functor@endlink.
245 * @return The greater of the parameters.
247 * This will work on temporary expressions, since they are only evaluated
248 * once, unlike a preprocessor macro.
250 template<typename _Tp, typename _Compare>
252 max(const _Tp& __a, const _Tp& __b, _Compare __comp)
254 //return __comp(__a, __b) ? __b : __a;
255 if (__comp(__a, __b))
260 // If _Iterator is a __normal_iterator return its base (a plain pointer,
261 // normally) otherwise return it untouched. See copy, fill, ...
262 template<typename _Iterator>
264 : _Iter_base<_Iterator, __is_normal_iterator<_Iterator>::__value>
267 template<typename _Iterator>
268 inline typename _Niter_base<_Iterator>::iterator_type
269 __niter_base(_Iterator __it)
270 { return std::_Niter_base<_Iterator>::_S_base(__it); }
272 // Likewise, for move_iterator.
273 template<typename _Iterator>
275 : _Iter_base<_Iterator, __is_move_iterator<_Iterator>::__value>
278 template<typename _Iterator>
279 inline typename _Miter_base<_Iterator>::iterator_type
280 __miter_base(_Iterator __it)
281 { return std::_Miter_base<_Iterator>::_S_base(__it); }
283 // All of these auxiliary structs serve two purposes. (1) Replace
284 // calls to copy with memmove whenever possible. (Memmove, not memcpy,
285 // because the input and output ranges are permitted to overlap.)
286 // (2) If we're using random access iterators, then write the loop as
287 // a for loop with an explicit count.
289 template<bool, bool, typename>
292 template<typename _II, typename _OI>
294 __copy_m(_II __first, _II __last, _OI __result)
296 for (; __first != __last; ++__result, ++__first)
297 *__result = *__first;
302 #ifdef __GXX_EXPERIMENTAL_CXX0X__
303 template<typename _Category>
304 struct __copy_move<true, false, _Category>
306 template<typename _II, typename _OI>
308 __copy_m(_II __first, _II __last, _OI __result)
310 for (; __first != __last; ++__result, ++__first)
311 *__result = std::move(*__first);
318 struct __copy_move<false, false, random_access_iterator_tag>
320 template<typename _II, typename _OI>
322 __copy_m(_II __first, _II __last, _OI __result)
324 typedef typename iterator_traits<_II>::difference_type _Distance;
325 for(_Distance __n = __last - __first; __n > 0; --__n)
327 *__result = *__first;
335 #ifdef __GXX_EXPERIMENTAL_CXX0X__
337 struct __copy_move<true, false, random_access_iterator_tag>
339 template<typename _II, typename _OI>
341 __copy_m(_II __first, _II __last, _OI __result)
343 typedef typename iterator_traits<_II>::difference_type _Distance;
344 for(_Distance __n = __last - __first; __n > 0; --__n)
346 *__result = std::move(*__first);
355 template<bool _IsMove>
356 struct __copy_move<_IsMove, true, random_access_iterator_tag>
358 template<typename _Tp>
360 __copy_m(const _Tp* __first, const _Tp* __last, _Tp* __result)
362 const ptrdiff_t _Num = __last - __first;
364 __builtin_memmove(__result, __first, sizeof(_Tp) * _Num);
365 return __result + _Num;
369 template<bool _IsMove, typename _II, typename _OI>
371 __copy_move_a(_II __first, _II __last, _OI __result)
373 typedef typename iterator_traits<_II>::value_type _ValueTypeI;
374 typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
375 typedef typename iterator_traits<_II>::iterator_category _Category;
376 const bool __simple = (__is_trivial(_ValueTypeI)
377 && __is_pointer<_II>::__value
378 && __is_pointer<_OI>::__value
379 && __are_same<_ValueTypeI, _ValueTypeO>::__value);
381 return std::__copy_move<_IsMove, __simple,
382 _Category>::__copy_m(__first, __last, __result);
385 // Helpers for streambuf iterators (either istream or ostream).
386 // NB: avoid including <iosfwd>, relatively large.
387 template<typename _CharT>
390 template<typename _CharT, typename _Traits>
391 class istreambuf_iterator;
393 template<typename _CharT, typename _Traits>
394 class ostreambuf_iterator;
396 template<bool _IsMove, typename _CharT>
397 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
398 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
399 __copy_move_a2(_CharT*, _CharT*,
400 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
402 template<bool _IsMove, typename _CharT>
403 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
404 ostreambuf_iterator<_CharT, char_traits<_CharT> > >::__type
405 __copy_move_a2(const _CharT*, const _CharT*,
406 ostreambuf_iterator<_CharT, char_traits<_CharT> >);
408 template<bool _IsMove, typename _CharT>
409 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
411 __copy_move_a2(istreambuf_iterator<_CharT, char_traits<_CharT> >,
412 istreambuf_iterator<_CharT, char_traits<_CharT> >, _CharT*);
414 template<bool _IsMove, typename _II, typename _OI>
416 __copy_move_a2(_II __first, _II __last, _OI __result)
418 return _OI(std::__copy_move_a<_IsMove>(std::__niter_base(__first),
419 std::__niter_base(__last),
420 std::__niter_base(__result)));
424 * @brief Copies the range [first,last) into result.
425 * @ingroup mutating_algorithms
426 * @param first An input iterator.
427 * @param last An input iterator.
428 * @param result An output iterator.
429 * @return result + (first - last)
431 * This inline function will boil down to a call to @c memmove whenever
432 * possible. Failing that, if random access iterators are passed, then the
433 * loop count will be known (and therefore a candidate for compiler
434 * optimizations such as unrolling). Result may not be contained within
435 * [first,last); the copy_backward function should be used instead.
437 * Note that the end of the output range is permitted to be contained
438 * within [first,last).
440 template<typename _II, typename _OI>
442 copy(_II __first, _II __last, _OI __result)
444 // concept requirements
445 __glibcxx_function_requires(_InputIteratorConcept<_II>)
446 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
447 typename iterator_traits<_II>::value_type>)
448 __glibcxx_requires_valid_range(__first, __last);
450 return (std::__copy_move_a2<__is_move_iterator<_II>::__value>
451 (std::__miter_base(__first), std::__miter_base(__last),
455 #ifdef __GXX_EXPERIMENTAL_CXX0X__
457 * @brief Moves the range [first,last) into result.
458 * @ingroup mutating_algorithms
459 * @param first An input iterator.
460 * @param last An input iterator.
461 * @param result An output iterator.
462 * @return result + (first - last)
464 * This inline function will boil down to a call to @c memmove whenever
465 * possible. Failing that, if random access iterators are passed, then the
466 * loop count will be known (and therefore a candidate for compiler
467 * optimizations such as unrolling). Result may not be contained within
468 * [first,last); the move_backward function should be used instead.
470 * Note that the end of the output range is permitted to be contained
471 * within [first,last).
473 template<typename _II, typename _OI>
475 move(_II __first, _II __last, _OI __result)
477 // concept requirements
478 __glibcxx_function_requires(_InputIteratorConcept<_II>)
479 __glibcxx_function_requires(_OutputIteratorConcept<_OI,
480 typename iterator_traits<_II>::value_type>)
481 __glibcxx_requires_valid_range(__first, __last);
483 return std::__copy_move_a2<true>(std::__miter_base(__first),
484 std::__miter_base(__last), __result);
487 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::move(_Tp, _Up, _Vp)
489 #define _GLIBCXX_MOVE3(_Tp, _Up, _Vp) std::copy(_Tp, _Up, _Vp)
492 template<bool, bool, typename>
493 struct __copy_move_backward
495 template<typename _BI1, typename _BI2>
497 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
499 while (__first != __last)
500 *--__result = *--__last;
505 #ifdef __GXX_EXPERIMENTAL_CXX0X__
506 template<typename _Category>
507 struct __copy_move_backward<true, false, _Category>
509 template<typename _BI1, typename _BI2>
511 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
513 while (__first != __last)
514 *--__result = std::move(*--__last);
521 struct __copy_move_backward<false, false, random_access_iterator_tag>
523 template<typename _BI1, typename _BI2>
525 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
527 typename iterator_traits<_BI1>::difference_type __n;
528 for (__n = __last - __first; __n > 0; --__n)
529 *--__result = *--__last;
534 #ifdef __GXX_EXPERIMENTAL_CXX0X__
536 struct __copy_move_backward<true, false, random_access_iterator_tag>
538 template<typename _BI1, typename _BI2>
540 __copy_move_b(_BI1 __first, _BI1 __last, _BI2 __result)
542 typename iterator_traits<_BI1>::difference_type __n;
543 for (__n = __last - __first; __n > 0; --__n)
544 *--__result = std::move(*--__last);
550 template<bool _IsMove>
551 struct __copy_move_backward<_IsMove, true, random_access_iterator_tag>
553 template<typename _Tp>
555 __copy_move_b(const _Tp* __first, const _Tp* __last, _Tp* __result)
557 const ptrdiff_t _Num = __last - __first;
559 __builtin_memmove(__result - _Num, __first, sizeof(_Tp) * _Num);
560 return __result - _Num;
564 template<bool _IsMove, typename _BI1, typename _BI2>
566 __copy_move_backward_a(_BI1 __first, _BI1 __last, _BI2 __result)
568 typedef typename iterator_traits<_BI1>::value_type _ValueType1;
569 typedef typename iterator_traits<_BI2>::value_type _ValueType2;
570 typedef typename iterator_traits<_BI1>::iterator_category _Category;
571 const bool __simple = (__is_trivial(_ValueType1)
572 && __is_pointer<_BI1>::__value
573 && __is_pointer<_BI2>::__value
574 && __are_same<_ValueType1, _ValueType2>::__value);
576 return std::__copy_move_backward<_IsMove, __simple,
577 _Category>::__copy_move_b(__first,
582 template<bool _IsMove, typename _BI1, typename _BI2>
584 __copy_move_backward_a2(_BI1 __first, _BI1 __last, _BI2 __result)
586 return _BI2(std::__copy_move_backward_a<_IsMove>
587 (std::__niter_base(__first), std::__niter_base(__last),
588 std::__niter_base(__result)));
592 * @brief Copies the range [first,last) into result.
593 * @ingroup mutating_algorithms
594 * @param first A bidirectional iterator.
595 * @param last A bidirectional iterator.
596 * @param result A bidirectional iterator.
597 * @return result - (first - last)
599 * The function has the same effect as copy, but starts at the end of the
600 * range and works its way to the start, returning the start of the result.
601 * This inline function will boil down to a call to @c memmove whenever
602 * possible. Failing that, if random access iterators are passed, then the
603 * loop count will be known (and therefore a candidate for compiler
604 * optimizations such as unrolling).
606 * Result may not be in the range [first,last). Use copy instead. Note
607 * that the start of the output range may overlap [first,last).
609 template<typename _BI1, typename _BI2>
611 copy_backward(_BI1 __first, _BI1 __last, _BI2 __result)
613 // concept requirements
614 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
615 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
616 __glibcxx_function_requires(_ConvertibleConcept<
617 typename iterator_traits<_BI1>::value_type,
618 typename iterator_traits<_BI2>::value_type>)
619 __glibcxx_requires_valid_range(__first, __last);
621 return (std::__copy_move_backward_a2<__is_move_iterator<_BI1>::__value>
622 (std::__miter_base(__first), std::__miter_base(__last),
626 #ifdef __GXX_EXPERIMENTAL_CXX0X__
628 * @brief Moves the range [first,last) into result.
629 * @ingroup mutating_algorithms
630 * @param first A bidirectional iterator.
631 * @param last A bidirectional iterator.
632 * @param result A bidirectional iterator.
633 * @return result - (first - last)
635 * The function has the same effect as move, but starts at the end of the
636 * range and works its way to the start, returning the start of the result.
637 * This inline function will boil down to a call to @c memmove whenever
638 * possible. Failing that, if random access iterators are passed, then the
639 * loop count will be known (and therefore a candidate for compiler
640 * optimizations such as unrolling).
642 * Result may not be in the range [first,last). Use move instead. Note
643 * that the start of the output range may overlap [first,last).
645 template<typename _BI1, typename _BI2>
647 move_backward(_BI1 __first, _BI1 __last, _BI2 __result)
649 // concept requirements
650 __glibcxx_function_requires(_BidirectionalIteratorConcept<_BI1>)
651 __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<_BI2>)
652 __glibcxx_function_requires(_ConvertibleConcept<
653 typename iterator_traits<_BI1>::value_type,
654 typename iterator_traits<_BI2>::value_type>)
655 __glibcxx_requires_valid_range(__first, __last);
657 return std::__copy_move_backward_a2<true>(std::__miter_base(__first),
658 std::__miter_base(__last),
662 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::move_backward(_Tp, _Up, _Vp)
664 #define _GLIBCXX_MOVE_BACKWARD3(_Tp, _Up, _Vp) std::copy_backward(_Tp, _Up, _Vp)
667 template<typename _ForwardIterator, typename _Tp>
669 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, void>::__type
670 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
673 for (; __first != __last; ++__first)
677 template<typename _ForwardIterator, typename _Tp>
679 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, void>::__type
680 __fill_a(_ForwardIterator __first, _ForwardIterator __last,
683 const _Tp __tmp = __value;
684 for (; __first != __last; ++__first)
688 // Specialization: for char types we can use memset.
689 template<typename _Tp>
691 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, void>::__type
692 __fill_a(_Tp* __first, _Tp* __last, const _Tp& __c)
694 const _Tp __tmp = __c;
695 __builtin_memset(__first, static_cast<unsigned char>(__tmp),
700 * @brief Fills the range [first,last) with copies of value.
701 * @ingroup mutating_algorithms
702 * @param first A forward iterator.
703 * @param last A forward iterator.
704 * @param value A reference-to-const of arbitrary type.
707 * This function fills a range with copies of the same value. For char
708 * types filling contiguous areas of memory, this becomes an inline call
709 * to @c memset or @c wmemset.
711 template<typename _ForwardIterator, typename _Tp>
713 fill(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __value)
715 // concept requirements
716 __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
718 __glibcxx_requires_valid_range(__first, __last);
720 std::__fill_a(std::__niter_base(__first), std::__niter_base(__last),
724 template<typename _OutputIterator, typename _Size, typename _Tp>
726 __gnu_cxx::__enable_if<!__is_scalar<_Tp>::__value, _OutputIterator>::__type
727 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
729 for (__decltype(__n + 0) __niter = __n;
730 __niter > 0; --__niter, ++__first)
735 template<typename _OutputIterator, typename _Size, typename _Tp>
737 __gnu_cxx::__enable_if<__is_scalar<_Tp>::__value, _OutputIterator>::__type
738 __fill_n_a(_OutputIterator __first, _Size __n, const _Tp& __value)
740 const _Tp __tmp = __value;
741 for (__decltype(__n + 0) __niter = __n;
742 __niter > 0; --__niter, ++__first)
747 template<typename _Size, typename _Tp>
749 __gnu_cxx::__enable_if<__is_byte<_Tp>::__value, _Tp*>::__type
750 __fill_n_a(_Tp* __first, _Size __n, const _Tp& __c)
752 std::__fill_a(__first, __first + __n, __c);
753 return __first + __n;
757 * @brief Fills the range [first,first+n) with copies of value.
758 * @ingroup mutating_algorithms
759 * @param first An output iterator.
760 * @param n The count of copies to perform.
761 * @param value A reference-to-const of arbitrary type.
762 * @return The iterator at first+n.
764 * This function fills a range with copies of the same value. For char
765 * types filling contiguous areas of memory, this becomes an inline call
766 * to @c memset or @ wmemset.
768 * _GLIBCXX_RESOLVE_LIB_DEFECTS
769 * DR 865. More algorithms that throw away information
771 template<typename _OI, typename _Size, typename _Tp>
773 fill_n(_OI __first, _Size __n, const _Tp& __value)
775 // concept requirements
776 __glibcxx_function_requires(_OutputIteratorConcept<_OI, _Tp>)
778 return _OI(std::__fill_n_a(std::__niter_base(__first), __n, __value));
781 template<bool _BoolType>
784 template<typename _II1, typename _II2>
786 equal(_II1 __first1, _II1 __last1, _II2 __first2)
788 for (; __first1 != __last1; ++__first1, ++__first2)
789 if (!(*__first1 == *__first2))
798 template<typename _Tp>
800 equal(const _Tp* __first1, const _Tp* __last1, const _Tp* __first2)
802 return !__builtin_memcmp(__first1, __first2, sizeof(_Tp)
803 * (__last1 - __first1));
807 template<typename _II1, typename _II2>
809 __equal_aux(_II1 __first1, _II1 __last1, _II2 __first2)
811 typedef typename iterator_traits<_II1>::value_type _ValueType1;
812 typedef typename iterator_traits<_II2>::value_type _ValueType2;
813 const bool __simple = (__is_integer<_ValueType1>::__value
814 && __is_pointer<_II1>::__value
815 && __is_pointer<_II2>::__value
816 && __are_same<_ValueType1, _ValueType2>::__value);
818 return std::__equal<__simple>::equal(__first1, __last1, __first2);
822 template<typename, typename>
825 template<typename _II1, typename _II2>
827 __newlast1(_II1, _II1 __last1, _II2, _II2)
830 template<typename _II>
832 __cnd2(_II __first, _II __last)
833 { return __first != __last; }
837 struct __lc_rai<random_access_iterator_tag, random_access_iterator_tag>
839 template<typename _RAI1, typename _RAI2>
841 __newlast1(_RAI1 __first1, _RAI1 __last1,
842 _RAI2 __first2, _RAI2 __last2)
844 const typename iterator_traits<_RAI1>::difference_type
845 __diff1 = __last1 - __first1;
846 const typename iterator_traits<_RAI2>::difference_type
847 __diff2 = __last2 - __first2;
848 return __diff2 < __diff1 ? __first1 + __diff2 : __last1;
851 template<typename _RAI>
857 template<bool _BoolType>
858 struct __lexicographical_compare
860 template<typename _II1, typename _II2>
861 static bool __lc(_II1, _II1, _II2, _II2);
864 template<bool _BoolType>
865 template<typename _II1, typename _II2>
867 __lexicographical_compare<_BoolType>::
868 __lc(_II1 __first1, _II1 __last1, _II2 __first2, _II2 __last2)
870 typedef typename iterator_traits<_II1>::iterator_category _Category1;
871 typedef typename iterator_traits<_II2>::iterator_category _Category2;
872 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
874 __last1 = __rai_type::__newlast1(__first1, __last1,
876 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
877 ++__first1, ++__first2)
879 if (*__first1 < *__first2)
881 if (*__first2 < *__first1)
884 return __first1 == __last1 && __first2 != __last2;
888 struct __lexicographical_compare<true>
890 template<typename _Tp, typename _Up>
892 __lc(const _Tp* __first1, const _Tp* __last1,
893 const _Up* __first2, const _Up* __last2)
895 const size_t __len1 = __last1 - __first1;
896 const size_t __len2 = __last2 - __first2;
897 const int __result = __builtin_memcmp(__first1, __first2,
898 std::min(__len1, __len2));
899 return __result != 0 ? __result < 0 : __len1 < __len2;
903 template<typename _II1, typename _II2>
905 __lexicographical_compare_aux(_II1 __first1, _II1 __last1,
906 _II2 __first2, _II2 __last2)
908 typedef typename iterator_traits<_II1>::value_type _ValueType1;
909 typedef typename iterator_traits<_II2>::value_type _ValueType2;
910 const bool __simple =
911 (__is_byte<_ValueType1>::__value && __is_byte<_ValueType2>::__value
912 && !__gnu_cxx::__numeric_traits<_ValueType1>::__is_signed
913 && !__gnu_cxx::__numeric_traits<_ValueType2>::__is_signed
914 && __is_pointer<_II1>::__value
915 && __is_pointer<_II2>::__value);
917 return std::__lexicographical_compare<__simple>::__lc(__first1, __last1,
922 * @brief Finds the first position in which @a val could be inserted
923 * without changing the ordering.
924 * @param first An iterator.
925 * @param last Another iterator.
926 * @param val The search term.
927 * @return An iterator pointing to the first element <em>not less
928 * than</em> @a val, or end() if every element is less than
930 * @ingroup binary_search_algorithms
932 template<typename _ForwardIterator, typename _Tp>
934 lower_bound(_ForwardIterator __first, _ForwardIterator __last,
937 typedef typename iterator_traits<_ForwardIterator>::value_type
939 typedef typename iterator_traits<_ForwardIterator>::difference_type
942 // concept requirements
943 __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
944 __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
945 __glibcxx_requires_partitioned_lower(__first, __last, __val);
947 _DistanceType __len = std::distance(__first, __last);
948 _DistanceType __half;
949 _ForwardIterator __middle;
955 std::advance(__middle, __half);
956 if (*__middle < __val)
960 __len = __len - __half - 1;
968 /// This is a helper function for the sort routines and for random.tcc.
969 // Precondition: __n > 0.
970 template<typename _Size>
975 for (__k = 0; __n != 0; __n >>= 1)
982 { return sizeof(int) * __CHAR_BIT__ - 1 - __builtin_clz(__n); }
986 { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
990 { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
992 _GLIBCXX_END_NAMESPACE
994 _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
997 * @brief Tests a range for element-wise equality.
998 * @ingroup non_mutating_algorithms
999 * @param first1 An input iterator.
1000 * @param last1 An input iterator.
1001 * @param first2 An input iterator.
1002 * @return A boolean true or false.
1004 * This compares the elements of two ranges using @c == and returns true or
1005 * false depending on whether all of the corresponding elements of the
1008 template<typename _II1, typename _II2>
1010 equal(_II1 __first1, _II1 __last1, _II2 __first2)
1012 // concept requirements
1013 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1014 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1015 __glibcxx_function_requires(_EqualOpConcept<
1016 typename iterator_traits<_II1>::value_type,
1017 typename iterator_traits<_II2>::value_type>)
1018 __glibcxx_requires_valid_range(__first1, __last1);
1020 return std::__equal_aux(std::__niter_base(__first1),
1021 std::__niter_base(__last1),
1022 std::__niter_base(__first2));
1026 * @brief Tests a range for element-wise equality.
1027 * @ingroup non_mutating_algorithms
1028 * @param first1 An input iterator.
1029 * @param last1 An input iterator.
1030 * @param first2 An input iterator.
1031 * @param binary_pred A binary predicate @link functors
1033 * @return A boolean true or false.
1035 * This compares the elements of two ranges using the binary_pred
1036 * parameter, and returns true or
1037 * false depending on whether all of the corresponding elements of the
1040 template<typename _IIter1, typename _IIter2, typename _BinaryPredicate>
1042 equal(_IIter1 __first1, _IIter1 __last1,
1043 _IIter2 __first2, _BinaryPredicate __binary_pred)
1045 // concept requirements
1046 __glibcxx_function_requires(_InputIteratorConcept<_IIter1>)
1047 __glibcxx_function_requires(_InputIteratorConcept<_IIter2>)
1048 __glibcxx_requires_valid_range(__first1, __last1);
1050 for (; __first1 != __last1; ++__first1, ++__first2)
1051 if (!bool(__binary_pred(*__first1, *__first2)))
1057 * @brief Performs @b dictionary comparison on ranges.
1058 * @ingroup sorting_algorithms
1059 * @param first1 An input iterator.
1060 * @param last1 An input iterator.
1061 * @param first2 An input iterator.
1062 * @param last2 An input iterator.
1063 * @return A boolean true or false.
1065 * <em>Returns true if the sequence of elements defined by the range
1066 * [first1,last1) is lexicographically less than the sequence of elements
1067 * defined by the range [first2,last2). Returns false otherwise.</em>
1068 * (Quoted from [25.3.8]/1.) If the iterators are all character pointers,
1069 * then this is an inline call to @c memcmp.
1071 template<typename _II1, typename _II2>
1073 lexicographical_compare(_II1 __first1, _II1 __last1,
1074 _II2 __first2, _II2 __last2)
1076 // concept requirements
1077 typedef typename iterator_traits<_II1>::value_type _ValueType1;
1078 typedef typename iterator_traits<_II2>::value_type _ValueType2;
1079 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1080 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1081 __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
1082 __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
1083 __glibcxx_requires_valid_range(__first1, __last1);
1084 __glibcxx_requires_valid_range(__first2, __last2);
1086 return std::__lexicographical_compare_aux(std::__niter_base(__first1),
1087 std::__niter_base(__last1),
1088 std::__niter_base(__first2),
1089 std::__niter_base(__last2));
1093 * @brief Performs @b dictionary comparison on ranges.
1094 * @ingroup sorting_algorithms
1095 * @param first1 An input iterator.
1096 * @param last1 An input iterator.
1097 * @param first2 An input iterator.
1098 * @param last2 An input iterator.
1099 * @param comp A @link comparison_functors comparison functor@endlink.
1100 * @return A boolean true or false.
1102 * The same as the four-parameter @c lexicographical_compare, but uses the
1103 * comp parameter instead of @c <.
1105 template<typename _II1, typename _II2, typename _Compare>
1107 lexicographical_compare(_II1 __first1, _II1 __last1,
1108 _II2 __first2, _II2 __last2, _Compare __comp)
1110 typedef typename iterator_traits<_II1>::iterator_category _Category1;
1111 typedef typename iterator_traits<_II2>::iterator_category _Category2;
1112 typedef std::__lc_rai<_Category1, _Category2> __rai_type;
1114 // concept requirements
1115 __glibcxx_function_requires(_InputIteratorConcept<_II1>)
1116 __glibcxx_function_requires(_InputIteratorConcept<_II2>)
1117 __glibcxx_requires_valid_range(__first1, __last1);
1118 __glibcxx_requires_valid_range(__first2, __last2);
1120 __last1 = __rai_type::__newlast1(__first1, __last1, __first2, __last2);
1121 for (; __first1 != __last1 && __rai_type::__cnd2(__first2, __last2);
1122 ++__first1, ++__first2)
1124 if (__comp(*__first1, *__first2))
1126 if (__comp(*__first2, *__first1))
1129 return __first1 == __last1 && __first2 != __last2;
1133 * @brief Finds the places in ranges which don't match.
1134 * @ingroup non_mutating_algorithms
1135 * @param first1 An input iterator.
1136 * @param last1 An input iterator.
1137 * @param first2 An input iterator.
1138 * @return A pair of iterators pointing to the first mismatch.
1140 * This compares the elements of two ranges using @c == and returns a pair
1141 * of iterators. The first iterator points into the first range, the
1142 * second iterator points into the second range, and the elements pointed
1143 * to by the iterators are not equal.
1145 template<typename _InputIterator1, typename _InputIterator2>
1146 pair<_InputIterator1, _InputIterator2>
1147 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1148 _InputIterator2 __first2)
1150 // concept requirements
1151 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1152 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1153 __glibcxx_function_requires(_EqualOpConcept<
1154 typename iterator_traits<_InputIterator1>::value_type,
1155 typename iterator_traits<_InputIterator2>::value_type>)
1156 __glibcxx_requires_valid_range(__first1, __last1);
1158 while (__first1 != __last1 && *__first1 == *__first2)
1163 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1167 * @brief Finds the places in ranges which don't match.
1168 * @ingroup non_mutating_algorithms
1169 * @param first1 An input iterator.
1170 * @param last1 An input iterator.
1171 * @param first2 An input iterator.
1172 * @param binary_pred A binary predicate @link functors
1174 * @return A pair of iterators pointing to the first mismatch.
1176 * This compares the elements of two ranges using the binary_pred
1177 * parameter, and returns a pair
1178 * of iterators. The first iterator points into the first range, the
1179 * second iterator points into the second range, and the elements pointed
1180 * to by the iterators are not equal.
1182 template<typename _InputIterator1, typename _InputIterator2,
1183 typename _BinaryPredicate>
1184 pair<_InputIterator1, _InputIterator2>
1185 mismatch(_InputIterator1 __first1, _InputIterator1 __last1,
1186 _InputIterator2 __first2, _BinaryPredicate __binary_pred)
1188 // concept requirements
1189 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
1190 __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
1191 __glibcxx_requires_valid_range(__first1, __last1);
1193 while (__first1 != __last1 && bool(__binary_pred(*__first1, *__first2)))
1198 return pair<_InputIterator1, _InputIterator2>(__first1, __first2);
1201 _GLIBCXX_END_NESTED_NAMESPACE
1203 // NB: This file is included within many other C++ includes, as a way
1204 // of getting the base algorithms. So, make sure that parallel bits
1205 // come in too if requested.
1206 #ifdef _GLIBCXX_PARALLEL
1207 # include <parallel/algobase.h>