diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 21c54b56c00..2090247381f 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,8 @@ +2003-06-29 Paolo Carlini + + * include/bits/stl_algo.h (LOTS): Fully qualify standard + functions with std::, thus avoiding Koenig lookup. + 2003-06-29 Paolo Carlini * testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-1.cc: diff --git a/libstdc++-v3/include/bits/stl_algo.h b/libstdc++-v3/include/bits/stl_algo.h index ac15b3afcbf..a43c17ac1fb 100644 --- a/libstdc++-v3/include/bits/stl_algo.h +++ b/libstdc++-v3/include/bits/stl_algo.h @@ -295,7 +295,7 @@ namespace std __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>) __glibcpp_function_requires(_EqualOpConcept< typename iterator_traits<_InputIterator>::value_type, _Tp>) - return find(__first, __last, __val, __iterator_category(__first)); + return std::find(__first, __last, __val, __iterator_category(__first)); } /** @@ -315,7 +315,7 @@ namespace std __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>) __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_InputIterator>::value_type>) - return find_if(__first, __last, __pred, __iterator_category(__first)); + return std::find_if(__first, __last, __pred, __iterator_category(__first)); } /** @@ -467,7 +467,7 @@ namespace std _ForwardIterator2 __tmp(__first2); ++__tmp; if (__tmp == __last2) - return find(__first1, __last1, *__first2); + return std::find(__first1, __last1, *__first2); // General case. @@ -478,7 +478,7 @@ namespace std _ForwardIterator1 __current = __first1; while (__first1 != __last1) { - __first1 = find(__first1, __last1, *__first2); + __first1 = std::find(__first1, __last1, *__first2); if (__first1 == __last1) return __last1; @@ -607,7 +607,7 @@ namespace std if (__count <= 0) return __first; else { - __first = find(__first, __last, __val); + __first = std::find(__first, __last, __val); while (__first != __last) { _Integer __n = __count - 1; _ForwardIterator __i = __first; @@ -619,7 +619,7 @@ namespace std if (__n == 0) return __first; else - __first = find(__i, __last, __val); + __first = std::find(__i, __last, __val); } return __last; } @@ -710,7 +710,7 @@ namespace std typename iterator_traits<_ForwardIterator1>::value_type>) for ( ; __first1 != __last1; ++__first1, ++__first2) - iter_swap(__first1, __first2); + std::iter_swap(__first1, __first2); return __first2; } @@ -1048,10 +1048,10 @@ namespace std __glibcpp_function_requires(_EqualOpConcept< typename iterator_traits<_ForwardIterator>::value_type, _Tp>) - __first = find(__first, __last, __value); + __first = std::find(__first, __last, __value); _ForwardIterator __i = __first; return __first == __last ? __first - : remove_copy(++__i, __last, __first, __value); + : std::remove_copy(++__i, __last, __first, __value); } /** @@ -1080,10 +1080,10 @@ namespace std __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_ForwardIterator>::value_type>) - __first = find_if(__first, __last, __pred); + __first = std::find_if(__first, __last, __pred); _ForwardIterator __i = __first; return __first == __last ? __first - : remove_copy_if(++__i, __last, __first, __pred); + : std::remove_copy_if(++__i, __last, __first, __pred); } /** @@ -1129,37 +1129,6 @@ namespace std return ++__result; } - /** - * @brief Copy a sequence, removing consecutive duplicate values. - * @param first An input iterator. - * @param last An input iterator. - * @param result An output iterator. - * @return An iterator designating the end of the resulting sequence. - * - * Copies each element in the range @p [first,last) to the range - * beginning at @p result, except that only the first element is copied - * from groups of consecutive elements that compare equal. - * unique_copy() is stable, so the relative order of elements that are - * copied is unchanged. - */ - template - inline _OutputIterator - unique_copy(_InputIterator __first, _InputIterator __last, - _OutputIterator __result) - { - // concept requirements - __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>) - __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, - typename iterator_traits<_InputIterator>::value_type>) - __glibcpp_function_requires(_EqualityComparableConcept< - typename iterator_traits<_InputIterator>::value_type>) - - typedef typename iterator_traits<_OutputIterator>::iterator_category _IterType; - - if (__first == __last) return __result; - return __unique_copy(__first, __last, __result, _IterType()); - } - /** * @if maint * This is an uglified @@ -1214,6 +1183,37 @@ namespace std return ++__result; } + /** + * @brief Copy a sequence, removing consecutive duplicate values. + * @param first An input iterator. + * @param last An input iterator. + * @param result An output iterator. + * @return An iterator designating the end of the resulting sequence. + * + * Copies each element in the range @p [first,last) to the range + * beginning at @p result, except that only the first element is copied + * from groups of consecutive elements that compare equal. + * unique_copy() is stable, so the relative order of elements that are + * copied is unchanged. + */ + template + inline _OutputIterator + unique_copy(_InputIterator __first, _InputIterator __last, + _OutputIterator __result) + { + // concept requirements + __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>) + __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, + typename iterator_traits<_InputIterator>::value_type>) + __glibcpp_function_requires(_EqualityComparableConcept< + typename iterator_traits<_InputIterator>::value_type>) + + typedef typename iterator_traits<_OutputIterator>::iterator_category _IterType; + + if (__first == __last) return __result; + return std::__unique_copy(__first, __last, __result, _IterType()); + } + /** * @brief Copy a sequence, removing consecutive values using a predicate. * @param first An input iterator. @@ -1243,8 +1243,7 @@ namespace std typedef typename iterator_traits<_OutputIterator>::iterator_category _IterType; if (__first == __last) return __result; - return __unique_copy(__first, __last, -__result, __binary_pred, _IterType()); + return std::__unique_copy(__first, __last, __result, __binary_pred, _IterType()); } /** @@ -1269,8 +1268,8 @@ __result, __binary_pred, _IterType()); __glibcpp_function_requires(_EqualityComparableConcept< typename iterator_traits<_ForwardIterator>::value_type>) - __first = adjacent_find(__first, __last); - return unique_copy(__first, __last, __first); + __first = std::adjacent_find(__first, __last); + return std::unique_copy(__first, __last, __first); } /** @@ -1298,8 +1297,8 @@ __result, __binary_pred, _IterType()); typename iterator_traits<_ForwardIterator>::value_type, typename iterator_traits<_ForwardIterator>::value_type>) - __first = adjacent_find(__first, __last, __binary_pred); - return unique_copy(__first, __last, __first, __binary_pred); + __first = std::adjacent_find(__first, __last, __binary_pred); + return std::unique_copy(__first, __last, __first, __binary_pred); } /** @@ -1313,11 +1312,11 @@ __result, __binary_pred, _IterType()); __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last, bidirectional_iterator_tag) { - while (true) - if (__first == __last || __first == --__last) - return; - else - iter_swap(__first++, __last); + while (true) + if (__first == __last || __first == --__last) + return; + else + std::iter_swap(__first++, __last); } /** @@ -1331,8 +1330,8 @@ __result, __binary_pred, _IterType()); __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last, random_access_iterator_tag) { - while (__first < __last) - iter_swap(__first++, --__last); + while (__first < __last) + std::iter_swap(__first++, --__last); } /** @@ -1353,7 +1352,7 @@ __result, __binary_pred, _IterType()); // concept requirements __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept< _BidirectionalIterator>) - __reverse(__first, __last, __iterator_category(__first)); + std::__reverse(__first, __last, __iterator_category(__first)); } /** @@ -1460,17 +1459,17 @@ __result, __binary_pred, _IterType()); if ((__first == __middle) || (__last == __middle)) return; - __reverse(__first, __middle, bidirectional_iterator_tag()); - __reverse(__middle, __last, bidirectional_iterator_tag()); + std::__reverse(__first, __middle, bidirectional_iterator_tag()); + std::__reverse(__middle, __last, bidirectional_iterator_tag()); while (__first != __middle && __middle != __last) - swap (*__first++, *--__last); + swap(*__first++, *--__last); if (__first == __middle) { - __reverse(__middle, __last, bidirectional_iterator_tag()); + std::__reverse(__middle, __last, bidirectional_iterator_tag()); } else { - __reverse(__first, __middle, bidirectional_iterator_tag()); + std::__reverse(__first, __middle, bidirectional_iterator_tag()); } } @@ -1501,7 +1500,7 @@ __result, __binary_pred, _IterType()); _Distance __l = __n - __k; if (__k == __l) { - swap_ranges(__first, __middle, __middle); + std::swap_ranges(__first, __middle, __middle); return; } @@ -1566,7 +1565,7 @@ __result, __binary_pred, _IterType()); __glibcpp_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>) typedef typename iterator_traits<_ForwardIterator>::iterator_category _IterType; - __rotate(__first, __middle, __last, _IterType()); + std::__rotate(__first, __middle, __last, _IterType()); } /** @@ -1596,7 +1595,7 @@ __result, __binary_pred, _IterType()); __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator, typename iterator_traits<_ForwardIterator>::value_type>) - return copy(__first, __middle, copy(__middle, __last, __result)); + return std::copy(__first, __middle, copy(__middle, __last, __result)); } @@ -1641,7 +1640,7 @@ __result, __binary_pred, _IterType()); if (__first == __last) return; for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) - iter_swap(__i, __first + __random_number((__i - __first) + 1)); + std::iter_swap(__i, __first + std::__random_number((__i - __first) + 1)); } /** @@ -1668,7 +1667,7 @@ __result, __binary_pred, _IterType()); if (__first == __last) return; for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i) - iter_swap(__i, __first + __rand((__i - __first) + 1)); + std::iter_swap(__i, __first + __rand((__i - __first) + 1)); } @@ -1726,7 +1725,7 @@ __result, __binary_pred, _IterType()); --__last; else break; - iter_swap(__first, __last); + std::iter_swap(__first, __last); ++__first; } } @@ -1755,7 +1754,7 @@ __result, __binary_pred, _IterType()); __glibcpp_function_requires(_UnaryPredicateConcept<_Predicate, typename iterator_traits<_ForwardIterator>::value_type>) - return __partition(__first, __last, __pred, __iterator_category(__first)); + return std::__partition(__first, __last, __pred, __iterator_category(__first)); } @@ -1772,15 +1771,15 @@ __result, __binary_pred, _IterType()); if (__len == 1) return __pred(*__first) ? __last : __first; _ForwardIterator __middle = __first; - advance(__middle, __len / 2); - _ForwardIterator __begin = __inplace_stable_partition(__first, __middle, - __pred, - __len / 2); - _ForwardIterator __end = __inplace_stable_partition(__middle, __last, - __pred, - __len - __len / 2); - rotate(__begin, __middle, __end); - advance(__begin, std::distance(__middle, __end)); + std::advance(__middle, __len / 2); + _ForwardIterator __begin = std::__inplace_stable_partition(__first, __middle, + __pred, + __len / 2); + _ForwardIterator __end = std::__inplace_stable_partition(__middle, __last, + __pred, + __len - __len / 2); + std::rotate(__begin, __middle, __end); + std::advance(__begin, std::distance(__middle, __end)); return __begin; } @@ -1809,22 +1808,22 @@ __result, __binary_pred, _IterType()); *__result2 = *__first; ++__result2; } - copy(__buffer, __result2, __result1); + std::copy(__buffer, __result2, __result1); return __result1; } else { _ForwardIterator __middle = __first; - advance(__middle, __len / 2); - _ForwardIterator __begin = __stable_partition_adaptive(__first, __middle, - __pred, - __len / 2, - __buffer, __buffer_size); - _ForwardIterator __end = __stable_partition_adaptive( __middle, __last, - __pred, - __len - __len / 2, - __buffer, __buffer_size); - rotate(__begin, __middle, __end); - advance(__begin, std::distance(__middle, __end)); + std::advance(__middle, __len / 2); + _ForwardIterator __begin = std::__stable_partition_adaptive(__first, __middle, + __pred, + __len / 2, + __buffer, __buffer_size); + _ForwardIterator __end = std::__stable_partition_adaptive( __middle, __last, + __pred, + __len - __len / 2, + __buffer, __buffer_size); + std::rotate(__begin, __middle, __end); + std::advance(__begin, std::distance(__middle, __end)); return __begin; } } @@ -1864,12 +1863,12 @@ __result, __binary_pred, _IterType()); _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first, __last); if (__buf.size() > 0) - return __stable_partition_adaptive(__first, __last, __pred, - _DistanceType(__buf.requested_size()), - __buf.begin(), __buf.size()); + return std::__stable_partition_adaptive(__first, __last, __pred, + _DistanceType(__buf.requested_size()), + __buf.begin(), __buf.size()); else - return __inplace_stable_partition(__first, __last, __pred, - _DistanceType(__buf.requested_size())); + return std::__inplace_stable_partition(__first, __last, __pred, + _DistanceType(__buf.requested_size())); } } @@ -1891,7 +1890,7 @@ __result, __binary_pred, _IterType()); --__last; if (!(__first < __last)) return __first; - iter_swap(__first, __last); + std::iter_swap(__first, __last); ++__first; } } @@ -1914,7 +1913,7 @@ __result, __binary_pred, _IterType()); --__last; if (!(__first < __last)) return __first; - iter_swap(__first, __last); + std::iter_swap(__first, __last); ++__first; } } @@ -1981,11 +1980,11 @@ __result, __binary_pred, _IterType()); { typename iterator_traits<_RandomAccessIterator>::value_type __val = *__i; if (__val < *__first) { - copy_backward(__first, __i, __i + 1); + std::copy_backward(__first, __i, __i + 1); *__first = __val; } else - __unguarded_linear_insert(__i, __val); + std::__unguarded_linear_insert(__i, __val); } } @@ -2005,11 +2004,11 @@ __result, __binary_pred, _IterType()); { typename iterator_traits<_RandomAccessIterator>::value_type __val = *__i; if (__comp(__val, *__first)) { - copy_backward(__first, __i, __i + 1); + std::copy_backward(__first, __i, __i + 1); *__first = __val; } else - __unguarded_linear_insert(__i, __val, __comp); + std::__unguarded_linear_insert(__i, __val, __comp); } } @@ -2025,7 +2024,7 @@ __result, __binary_pred, _IterType()); typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; for (_RandomAccessIterator __i = __first; __i != __last; ++__i) - __unguarded_linear_insert(__i, _ValueType(*__i)); + std::__unguarded_linear_insert(__i, _ValueType(*__i)); } /** @@ -2041,7 +2040,7 @@ __result, __binary_pred, _IterType()); typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; for (_RandomAccessIterator __i = __first; __i != __last; ++__i) - __unguarded_linear_insert(__i, _ValueType(*__i), __comp); + std::__unguarded_linear_insert(__i, _ValueType(*__i), __comp); } /** @@ -2054,11 +2053,11 @@ __result, __binary_pred, _IterType()); __final_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) { if (__last - __first > _S_threshold) { - __insertion_sort(__first, __first + _S_threshold); - __unguarded_insertion_sort(__first + _S_threshold, __last); + std::__insertion_sort(__first, __first + _S_threshold); + std::__unguarded_insertion_sort(__first + _S_threshold, __last); } else - __insertion_sort(__first, __last); + std::__insertion_sort(__first, __last); } /** @@ -2072,11 +2071,11 @@ __result, __binary_pred, _IterType()); _Compare __comp) { if (__last - __first > _S_threshold) { - __insertion_sort(__first, __first + _S_threshold, __comp); - __unguarded_insertion_sort(__first + _S_threshold, __last, __comp); + std::__insertion_sort(__first, __first + _S_threshold, __comp); + std::__unguarded_insertion_sort(__first + _S_threshold, __last, __comp); } else - __insertion_sort(__first, __last, __comp); + std::__insertion_sort(__first, __last, __comp); } /** @@ -2093,397 +2092,6 @@ __result, __binary_pred, _IterType()); return __k; } - /** - * @if maint - * This is a helper function for the sort routine. - * @endif - */ - template - void - __introsort_loop(_RandomAccessIterator __first, _RandomAccessIterator __last, - _Size __depth_limit) - { - typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; - - while (__last - __first > _S_threshold) { - if (__depth_limit == 0) { - partial_sort(__first, __last, __last); - return; - } - --__depth_limit; - _RandomAccessIterator __cut = - __unguarded_partition(__first, __last, - _ValueType(__median(*__first, - *(__first + (__last - __first)/2), - *(__last - 1)))); - __introsort_loop(__cut, __last, __depth_limit); - __last = __cut; - } - } - - /** - * @if maint - * This is a helper function for the sort routine. - * @endif - */ - template - void - __introsort_loop(_RandomAccessIterator __first, _RandomAccessIterator __last, - _Size __depth_limit, _Compare __comp) - { - typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; - - while (__last - __first > _S_threshold) { - if (__depth_limit == 0) { - partial_sort(__first, __last, __last, __comp); - return; - } - --__depth_limit; - _RandomAccessIterator __cut = - __unguarded_partition(__first, __last, - _ValueType(__median(*__first, - *(__first + (__last - __first)/2), - *(__last - 1), __comp)), - __comp); - __introsort_loop(__cut, __last, __depth_limit, __comp); - __last = __cut; - } - } - - /** - * @brief Sort the elements of a sequence. - * @param first An iterator. - * @param last Another iterator. - * @return Nothing. - * - * Sorts the elements in the range @p [first,last) in ascending order, - * such that @p *(i+1)<*i is false for each iterator @p i in the range - * @p [first,last-1). - * - * The relative ordering of equivalent elements is not preserved, use - * @p stable_sort() if this is needed. - */ - template - inline void - sort(_RandomAccessIterator __first, _RandomAccessIterator __last) - { - typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; - - // concept requirements - __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept< - _RandomAccessIterator>) - __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>) - - if (__first != __last) { - __introsort_loop(__first, __last, __lg(__last - __first) * 2); - __final_insertion_sort(__first, __last); - } - } - - /** - * @brief Sort the elements of a sequence using a predicate for comparison. - * @param first An iterator. - * @param last Another iterator. - * @param comp A comparison functor. - * @return Nothing. - * - * Sorts the elements in the range @p [first,last) in ascending order, - * such that @p comp(*(i+1),*i) is false for every iterator @p i in the - * range @p [first,last-1). - * - * The relative ordering of equivalent elements is not preserved, use - * @p stable_sort() if this is needed. - */ - template - inline void - sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) - { - typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; - - // concept requirements - __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept< - _RandomAccessIterator>) - __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _ValueType>) - - if (__first != __last) { - __introsort_loop(__first, __last, __lg(__last - __first) * 2, __comp); - __final_insertion_sort(__first, __last, __comp); - } - } - - - /** - * @if maint - * This is a helper function for the stable sorting routines. - * @endif - */ - template - void - __inplace_stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) - { - if (__last - __first < 15) { - __insertion_sort(__first, __last); - return; - } - _RandomAccessIterator __middle = __first + (__last - __first) / 2; - __inplace_stable_sort(__first, __middle); - __inplace_stable_sort(__middle, __last); - __merge_without_buffer(__first, __middle, __last, - __middle - __first, - __last - __middle); - } - - /** - * @if maint - * This is a helper function for the stable sorting routines. - * @endif - */ - template - void - __inplace_stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, - _Compare __comp) - { - if (__last - __first < 15) { - __insertion_sort(__first, __last, __comp); - return; - } - _RandomAccessIterator __middle = __first + (__last - __first) / 2; - __inplace_stable_sort(__first, __middle, __comp); - __inplace_stable_sort(__middle, __last, __comp); - __merge_without_buffer(__first, __middle, __last, - __middle - __first, - __last - __middle, - __comp); - } - - template - void - __merge_sort_loop(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, - _RandomAccessIterator2 __result, _Distance __step_size) - { - _Distance __two_step = 2 * __step_size; - - while (__last - __first >= __two_step) { - __result = merge(__first, __first + __step_size, - __first + __step_size, __first + __two_step, - __result); - __first += __two_step; - } - - __step_size = std::min(_Distance(__last - __first), __step_size); - merge(__first, __first + __step_size, __first + __step_size, __last, - __result); - } - - template - void - __merge_sort_loop(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, - _RandomAccessIterator2 __result, _Distance __step_size, - _Compare __comp) - { - _Distance __two_step = 2 * __step_size; - - while (__last - __first >= __two_step) { - __result = merge(__first, __first + __step_size, - __first + __step_size, __first + __two_step, - __result, - __comp); - __first += __two_step; - } - __step_size = std::min(_Distance(__last - __first), __step_size); - - merge(__first, __first + __step_size, - __first + __step_size, __last, - __result, - __comp); - } - - enum { _S_chunk_size = 7 }; - - template - void - __chunk_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, - _Distance __chunk_size) - { - while (__last - __first >= __chunk_size) { - __insertion_sort(__first, __first + __chunk_size); - __first += __chunk_size; - } - __insertion_sort(__first, __last); - } - - template - void - __chunk_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, - _Distance __chunk_size, _Compare __comp) - { - while (__last - __first >= __chunk_size) { - __insertion_sort(__first, __first + __chunk_size, __comp); - __first += __chunk_size; - } - __insertion_sort(__first, __last, __comp); - } - - template - void - __merge_sort_with_buffer(_RandomAccessIterator __first, _RandomAccessIterator __last, - _Pointer __buffer) - { - typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance; - - _Distance __len = __last - __first; - _Pointer __buffer_last = __buffer + __len; - - _Distance __step_size = _S_chunk_size; - __chunk_insertion_sort(__first, __last, __step_size); - - while (__step_size < __len) { - __merge_sort_loop(__first, __last, __buffer, __step_size); - __step_size *= 2; - __merge_sort_loop(__buffer, __buffer_last, __first, __step_size); - __step_size *= 2; - } - } - - template - void - __merge_sort_with_buffer(_RandomAccessIterator __first, _RandomAccessIterator __last, - _Pointer __buffer, _Compare __comp) - { - typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance; - - _Distance __len = __last - __first; - _Pointer __buffer_last = __buffer + __len; - - _Distance __step_size = _S_chunk_size; - __chunk_insertion_sort(__first, __last, __step_size, __comp); - - while (__step_size < __len) { - __merge_sort_loop(__first, __last, __buffer, __step_size, __comp); - __step_size *= 2; - __merge_sort_loop(__buffer, __buffer_last, __first, __step_size, __comp); - __step_size *= 2; - } - } - - template - void - __stable_sort_adaptive(_RandomAccessIterator __first, _RandomAccessIterator __last, - _Pointer __buffer, _Distance __buffer_size) - { - _Distance __len = (__last - __first + 1) / 2; - _RandomAccessIterator __middle = __first + __len; - if (__len > __buffer_size) { - __stable_sort_adaptive(__first, __middle, __buffer, __buffer_size); - __stable_sort_adaptive(__middle, __last, __buffer, __buffer_size); - } - else { - __merge_sort_with_buffer(__first, __middle, __buffer); - __merge_sort_with_buffer(__middle, __last, __buffer); - } - __merge_adaptive(__first, __middle, __last, _Distance(__middle - __first), - _Distance(__last - __middle), __buffer, __buffer_size); - } - - template - void - __stable_sort_adaptive(_RandomAccessIterator __first, _RandomAccessIterator __last, - _Pointer __buffer, _Distance __buffer_size, - _Compare __comp) - { - _Distance __len = (__last - __first + 1) / 2; - _RandomAccessIterator __middle = __first + __len; - if (__len > __buffer_size) { - __stable_sort_adaptive(__first, __middle, __buffer, __buffer_size, - __comp); - __stable_sort_adaptive(__middle, __last, __buffer, __buffer_size, - __comp); - } - else { - __merge_sort_with_buffer(__first, __middle, __buffer, __comp); - __merge_sort_with_buffer(__middle, __last, __buffer, __comp); - } - __merge_adaptive(__first, __middle, __last, _Distance(__middle - __first), - _Distance(__last - __middle), __buffer, __buffer_size, - __comp); - } - - /** - * @brief Sort the elements of a sequence, preserving the relative order - * of equivalent elements. - * @param first An iterator. - * @param last Another iterator. - * @return Nothing. - * - * Sorts the elements in the range @p [first,last) in ascending order, - * such that @p *(i+1)<*i is false for each iterator @p i in the range - * @p [first,last-1). - * - * The relative ordering of equivalent elements is preserved, so any two - * elements @p x and @p y in the range @p [first,last) such that - * @p x - inline void - stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) - { - typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; - typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType; - - // concept requirements - __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept< - _RandomAccessIterator>) - __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>) - - _Temporary_buffer<_RandomAccessIterator, _ValueType> buf(__first, __last); - if (buf.begin() == 0) - __inplace_stable_sort(__first, __last); - else - __stable_sort_adaptive(__first, __last, buf.begin(), _DistanceType(buf.size())); - } - - /** - * @brief Sort the elements of a sequence using a predicate for comparison, - * preserving the relative order of equivalent elements. - * @param first An iterator. - * @param last Another iterator. - * @param comp A comparison functor. - * @return Nothing. - * - * Sorts the elements in the range @p [first,last) in ascending order, - * such that @p comp(*(i+1),*i) is false for each iterator @p i in the - * range @p [first,last-1). - * - * The relative ordering of equivalent elements is preserved, so any two - * elements @p x and @p y in the range @p [first,last) such that - * @p comp(x,y) is false and @p comp(y,x) is false will have the same - * relative ordering after calling @p stable_sort(). - */ - template - inline void - stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) - { - typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; - typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType; - - // concept requirements - __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept< - _RandomAccessIterator>) - __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, - _ValueType, _ValueType>) - - _Temporary_buffer<_RandomAccessIterator, _ValueType> buf(__first, __last); - if (buf.begin() == 0) - __inplace_stable_sort(__first, __last, __comp); - else - __stable_sort_adaptive(__first, __last, buf.begin(), _DistanceType(buf.size()), - __comp); - } - /** * @brief Sort the smallest elements of a sequence. * @param first An iterator. @@ -2512,11 +2120,11 @@ __result, __binary_pred, _IterType()); _RandomAccessIterator>) __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>) - make_heap(__first, __middle); + std::make_heap(__first, __middle); for (_RandomAccessIterator __i = __middle; __i < __last; ++__i) if (*__i < *__first) - __pop_heap(__first, __middle, __i, _ValueType(*__i)); - sort_heap(__first, __middle); + std::__pop_heap(__first, __middle, __i, _ValueType(*__i)); + std::sort_heap(__first, __middle); } /** @@ -2552,11 +2160,11 @@ __result, __binary_pred, _IterType()); __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _ValueType>) - make_heap(__first, __middle, __comp); + std::make_heap(__first, __middle, __comp); for (_RandomAccessIterator __i = __middle; __i < __last; ++__i) if (__comp(*__i, *__first)) - __pop_heap(__first, __middle, __i, _ValueType(*__i), __comp); - sort_heap(__first, __middle, __comp); + std::__pop_heap(__first, __middle, __i, _ValueType(*__i), __comp); + std::sort_heap(__first, __middle, __comp); } /** @@ -2599,15 +2207,15 @@ __result, __binary_pred, _IterType()); ++__result_real_last; ++__first; } - make_heap(__result_first, __result_real_last); + std::make_heap(__result_first, __result_real_last); while (__first != __last) { if (*__first < *__result_first) - __adjust_heap(__result_first, _DistanceType(0), - _DistanceType(__result_real_last - __result_first), - _InputValueType(*__first)); + std::__adjust_heap(__result_first, _DistanceType(0), + _DistanceType(__result_real_last - __result_first), + _InputValueType(*__first)); ++__first; } - sort_heap(__result_first, __result_real_last); + std::sort_heap(__result_first, __result_real_last); return __result_real_last; } @@ -2655,107 +2263,136 @@ __result, __binary_pred, _IterType()); ++__result_real_last; ++__first; } - make_heap(__result_first, __result_real_last, __comp); + std::make_heap(__result_first, __result_real_last, __comp); while (__first != __last) { if (__comp(*__first, *__result_first)) - __adjust_heap(__result_first, _DistanceType(0), - _DistanceType(__result_real_last - __result_first), - _InputValueType(*__first), - __comp); + std::__adjust_heap(__result_first, _DistanceType(0), + _DistanceType(__result_real_last - __result_first), + _InputValueType(*__first), + __comp); ++__first; } - sort_heap(__result_first, __result_real_last, __comp); + std::sort_heap(__result_first, __result_real_last, __comp); return __result_real_last; } /** - * @brief Sort a sequence just enough to find a particular position. + * @if maint + * This is a helper function for the sort routine. + * @endif + */ + template + void + __introsort_loop(_RandomAccessIterator __first, _RandomAccessIterator __last, + _Size __depth_limit) + { + typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; + + while (__last - __first > _S_threshold) { + if (__depth_limit == 0) { + std::partial_sort(__first, __last, __last); + return; + } + --__depth_limit; + _RandomAccessIterator __cut = + std::__unguarded_partition(__first, __last, + _ValueType(std::__median(*__first, + *(__first + (__last - __first)/2), + *(__last - 1)))); + std::__introsort_loop(__cut, __last, __depth_limit); + __last = __cut; + } + } + + /** + * @if maint + * This is a helper function for the sort routine. + * @endif + */ + template + void + __introsort_loop(_RandomAccessIterator __first, _RandomAccessIterator __last, + _Size __depth_limit, _Compare __comp) + { + typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; + + while (__last - __first > _S_threshold) { + if (__depth_limit == 0) { + std::partial_sort(__first, __last, __last, __comp); + return; + } + --__depth_limit; + _RandomAccessIterator __cut = + std::__unguarded_partition(__first, __last, + _ValueType(std::__median(*__first, + *(__first + (__last - __first)/2), + *(__last - 1), __comp)), __comp); + std::__introsort_loop(__cut, __last, __depth_limit, __comp); + __last = __cut; + } + } + + /** + * @brief Sort the elements of a sequence. * @param first An iterator. - * @param nth Another iterator. * @param last Another iterator. * @return Nothing. * - * Rearranges the elements in the range @p [first,last) so that @p *nth - * is the same element that would have been in that position had the - * whole sequence been sorted. - * whole sequence been sorted. The elements either side of @p *nth are - * not completely sorted, but for any iterator @i in the range - * @p [first,nth) and any iterator @j in the range @p [nth,last) it - * holds that @p *j<*i is false. + * Sorts the elements in the range @p [first,last) in ascending order, + * such that @p *(i+1)<*i is false for each iterator @p i in the range + * @p [first,last-1). + * + * The relative ordering of equivalent elements is not preserved, use + * @p stable_sort() if this is needed. */ template - void - nth_element(_RandomAccessIterator __first, - _RandomAccessIterator __nth, - _RandomAccessIterator __last) + inline void + sort(_RandomAccessIterator __first, _RandomAccessIterator __last) { typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; // concept requirements - __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>) + __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept< + _RandomAccessIterator>) __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>) - while (__last - __first > 3) { - _RandomAccessIterator __cut = - __unguarded_partition(__first, __last, - _ValueType(__median(*__first, - *(__first + (__last - __first)/2), - *(__last - 1)))); - if (__cut <= __nth) - __first = __cut; - else - __last = __cut; + if (__first != __last) { + std::__introsort_loop(__first, __last, __lg(__last - __first) * 2); + std::__final_insertion_sort(__first, __last); } - __insertion_sort(__first, __last); } /** - * @brief Sort a sequence just enough to find a particular position - * using a predicate for comparison. + * @brief Sort the elements of a sequence using a predicate for comparison. * @param first An iterator. - * @param nth Another iterator. * @param last Another iterator. * @param comp A comparison functor. * @return Nothing. * - * Rearranges the elements in the range @p [first,last) so that @p *nth - * is the same element that would have been in that position had the - * whole sequence been sorted. The elements either side of @p *nth are - * not completely sorted, but for any iterator @i in the range - * @p [first,nth) and any iterator @j in the range @p [nth,last) it - * holds that @p comp(*j,*i) is false. + * Sorts the elements in the range @p [first,last) in ascending order, + * such that @p comp(*(i+1),*i) is false for every iterator @p i in the + * range @p [first,last-1). + * + * The relative ordering of equivalent elements is not preserved, use + * @p stable_sort() if this is needed. */ template - void - nth_element(_RandomAccessIterator __first, - _RandomAccessIterator __nth, - _RandomAccessIterator __last, - _Compare __comp) + inline void + sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) { typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; // concept requirements - __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>) - __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, - _ValueType, _ValueType>) + __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept< + _RandomAccessIterator>) + __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _ValueType>) - while (__last - __first > 3) { - _RandomAccessIterator __cut = - __unguarded_partition(__first, __last, - _ValueType(__median(*__first, - *(__first + (__last - __first)/2), - *(__last - 1), - __comp)), - __comp); - if (__cut <= __nth) - __first = __cut; - else - __last = __cut; + if (__first != __last) { + std::__introsort_loop(__first, __last, __lg(__last - __first) * 2, __comp); + std::__final_insertion_sort(__first, __last, __comp); } - __insertion_sort(__first, __last, __comp); } - /** * @brief Finds the first position in which @a val could be inserted * without changing the ordering. @@ -2789,7 +2426,7 @@ __result, __binary_pred, _IterType()); while (__len > 0) { __half = __len >> 1; __middle = __first; - advance(__middle, __half); + std::advance(__middle, __half); if (*__middle < __val) { __first = __middle; ++__first; @@ -2834,7 +2471,7 @@ __result, __binary_pred, _IterType()); while (__len > 0) { __half = __len >> 1; __middle = __first; - advance(__middle, __half); + std::advance(__middle, __half); if (__comp(*__middle, __val)) { __first = __middle; ++__first; @@ -2876,7 +2513,7 @@ __result, __binary_pred, _IterType()); while (__len > 0) { __half = __len >> 1; __middle = __first; - advance(__middle, __half); + std::advance(__middle, __half); if (__val < *__middle) __len = __half; else { @@ -2921,7 +2558,7 @@ __result, __binary_pred, _IterType()); while (__len > 0) { __half = __len >> 1; __middle = __first; - advance(__middle, __half); + std::advance(__middle, __half); if (__comp(__val, *__middle)) __len = __half; else { @@ -2934,170 +2571,136 @@ __result, __binary_pred, _IterType()); } /** - * @brief Finds the largest subrange in which @a val could be inserted - * at any place in it without changing the ordering. - * @param first An iterator. - * @param last Another iterator. - * @param val The search term. - * @return An pair of iterators defining the subrange. - * @ingroup binarysearch - * - * This is equivalent to - * @code - * std::make_pair(lower_bound(first, last, val), - * upper_bound(first, last, val)) - * @endcode - * but does not actually call those functions. + * @if maint + * This is a helper function for the merge routines. + * @endif */ - template - pair<_ForwardIterator, _ForwardIterator> - equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val) + template + void + __merge_without_buffer(_BidirectionalIterator __first, + _BidirectionalIterator __middle, + _BidirectionalIterator __last, + _Distance __len1, _Distance __len2) { - typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; - typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType; - - // concept requirements - // See comments on lower_bound. - __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>) - __glibcpp_function_requires(_SameTypeConcept<_Tp, _ValueType>) - __glibcpp_function_requires(_LessThanComparableConcept<_Tp>) - - _DistanceType __len = std::distance(__first, __last); - _DistanceType __half; - _ForwardIterator __middle, __left, __right; - - while (__len > 0) { - __half = __len >> 1; - __middle = __first; - advance(__middle, __half); - if (*__middle < __val) { - __first = __middle; - ++__first; - __len = __len - __half - 1; - } - else if (__val < *__middle) - __len = __half; - else { - __left = lower_bound(__first, __middle, __val); - advance(__first, __len); - __right = upper_bound(++__middle, __first, __val); - return pair<_ForwardIterator, _ForwardIterator>(__left, __right); - } + if (__len1 == 0 || __len2 == 0) + return; + if (__len1 + __len2 == 2) { + if (*__middle < *__first) + std::iter_swap(__first, __middle); + return; } - return pair<_ForwardIterator, _ForwardIterator>(__first, __first); - } - - /** - * @brief Finds the largest subrange in which @a val could be inserted - * at any place in it without changing the ordering. - * @param first An iterator. - * @param last Another iterator. - * @param val The search term. - * @param comp A functor to use for comparisons. - * @return An pair of iterators defining the subrange. - * @ingroup binarysearch - * - * This is equivalent to - * @code - * std::make_pair(lower_bound(first, last, val, comp), - * upper_bound(first, last, val, comp)) - * @endcode - * but does not actually call those functions. - */ - template - pair<_ForwardIterator, _ForwardIterator> - equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val, - _Compare __comp) - { - typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; - typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType; - - // concept requirements - __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>) - __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _Tp>) - __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp, _ValueType>) - - _DistanceType __len = std::distance(__first, __last); - _DistanceType __half; - _ForwardIterator __middle, __left, __right; - - while (__len > 0) { - __half = __len >> 1; - __middle = __first; - advance(__middle, __half); - if (__comp(*__middle, __val)) { - __first = __middle; - ++__first; - __len = __len - __half - 1; - } - else if (__comp(__val, *__middle)) - __len = __half; - else { - __left = lower_bound(__first, __middle, __val, __comp); - advance(__first, __len); - __right = upper_bound(++__middle, __first, __val, __comp); - return pair<_ForwardIterator, _ForwardIterator>(__left, __right); - } + _BidirectionalIterator __first_cut = __first; + _BidirectionalIterator __second_cut = __middle; + _Distance __len11 = 0; + _Distance __len22 = 0; + if (__len1 > __len2) { + __len11 = __len1 / 2; + std::advance(__first_cut, __len11); + __second_cut = std::lower_bound(__middle, __last, *__first_cut); + __len22 = std::distance(__middle, __second_cut); } - return pair<_ForwardIterator, _ForwardIterator>(__first, __first); + else { + __len22 = __len2 / 2; + std::advance(__second_cut, __len22); + __first_cut = std::upper_bound(__first, __middle, *__second_cut); + __len11 = std::distance(__first, __first_cut); + } + std::rotate(__first_cut, __middle, __second_cut); + _BidirectionalIterator __new_middle = __first_cut; + std::advance(__new_middle, std::distance(__middle, __second_cut)); + std::__merge_without_buffer(__first, __first_cut, __new_middle, + __len11, __len22); + std::__merge_without_buffer(__new_middle, __second_cut, __last, + __len1 - __len11, __len2 - __len22); } /** - * @brief Determines whether an element exists in a range. - * @param first An iterator. - * @param last Another iterator. - * @param val The search term. - * @return True if @a val (or its equivelent) is in [@a first,@a last ]. - * @ingroup binarysearch - * - * Note that this does not actually return an iterator to @a val. For - * that, use std::find or a container's specialized find member functions. + * @if maint + * This is a helper function for the merge routines. + * @endif */ - template - bool - binary_search(_ForwardIterator __first, _ForwardIterator __last, - const _Tp& __val) + template + void + __merge_without_buffer(_BidirectionalIterator __first, + _BidirectionalIterator __middle, + _BidirectionalIterator __last, + _Distance __len1, _Distance __len2, + _Compare __comp) { - // concept requirements - // See comments on lower_bound. - __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>) - __glibcpp_function_requires(_SameTypeConcept<_Tp, - typename iterator_traits<_ForwardIterator>::value_type>) - __glibcpp_function_requires(_LessThanComparableConcept<_Tp>) - - _ForwardIterator __i = lower_bound(__first, __last, __val); - return __i != __last && !(__val < *__i); + if (__len1 == 0 || __len2 == 0) + return; + if (__len1 + __len2 == 2) { + if (__comp(*__middle, *__first)) + std::iter_swap(__first, __middle); + return; + } + _BidirectionalIterator __first_cut = __first; + _BidirectionalIterator __second_cut = __middle; + _Distance __len11 = 0; + _Distance __len22 = 0; + if (__len1 > __len2) { + __len11 = __len1 / 2; + std::advance(__first_cut, __len11); + __second_cut = std::lower_bound(__middle, __last, *__first_cut, __comp); + __len22 = std::distance(__middle, __second_cut); + } + else { + __len22 = __len2 / 2; + std::advance(__second_cut, __len22); + __first_cut = std::upper_bound(__first, __middle, *__second_cut, __comp); + __len11 = std::distance(__first, __first_cut); + } + std::rotate(__first_cut, __middle, __second_cut); + _BidirectionalIterator __new_middle = __first_cut; + std::advance(__new_middle, std::distance(__middle, __second_cut)); + std::__merge_without_buffer(__first, __first_cut, __new_middle, + __len11, __len22, __comp); + std::__merge_without_buffer(__new_middle, __second_cut, __last, + __len1 - __len11, __len2 - __len22, __comp); } /** - * @brief Determines whether an element exists in a range. - * @param first An iterator. - * @param last Another iterator. - * @param val The search term. - * @param comp A functor to use for comparisons. - * @return True if @a val (or its equivelent) is in [@a first,@a last ]. - * @ingroup binarysearch - * - * Note that this does not actually return an iterator to @a val. For - * that, use std::find or a container's specialized find member functions. - * - * The comparison function should have the same effects on ordering as - * the function used for the initial sort. + * @if maint + * This is a helper function for the stable sorting routines. + * @endif */ - template - bool - binary_search(_ForwardIterator __first, _ForwardIterator __last, - const _Tp& __val, _Compare __comp) + template + void + __inplace_stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) { - // concept requirements - __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>) - __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, - typename iterator_traits<_ForwardIterator>::value_type, _Tp>) - __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp, - typename iterator_traits<_ForwardIterator>::value_type>) + if (__last - __first < 15) { + std::__insertion_sort(__first, __last); + return; + } + _RandomAccessIterator __middle = __first + (__last - __first) / 2; + std::__inplace_stable_sort(__first, __middle); + std::__inplace_stable_sort(__middle, __last); + std::__merge_without_buffer(__first, __middle, __last, + __middle - __first, + __last - __middle); + } - _ForwardIterator __i = lower_bound(__first, __last, __val, __comp); - return __i != __last && !__comp(__val, *__i); + /** + * @if maint + * This is a helper function for the stable sorting routines. + * @endif + */ + template + void + __inplace_stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, + _Compare __comp) + { + if (__last - __first < 15) { + std::__insertion_sort(__first, __last, __comp); + return; + } + _RandomAccessIterator __middle = __first + (__last - __first) / 2; + std::__inplace_stable_sort(__first, __middle, __comp); + std::__inplace_stable_sort(__middle, __last, __comp); + std::__merge_without_buffer(__first, __middle, __last, + __middle - __first, + __last - __middle, + __comp); } /** @@ -3144,7 +2747,7 @@ __result, __binary_pred, _IterType()); } ++__result; } - return copy(__first2, __last2, copy(__first1, __last1, __result)); + return std::copy(__first2, __last2, std::copy(__first1, __last1, __result)); } /** @@ -3197,128 +2800,118 @@ __result, __binary_pred, _IterType()); } ++__result; } - return copy(__first2, __last2, copy(__first1, __last1, __result)); + return std::copy(__first2, __last2, std::copy(__first1, __last1, __result)); } - /** - * @if maint - * This is a helper function for the merge routines. - * @endif - */ - template - void - __merge_without_buffer(_BidirectionalIterator __first, - _BidirectionalIterator __middle, - _BidirectionalIterator __last, - _Distance __len1, _Distance __len2) - { - if (__len1 == 0 || __len2 == 0) - return; - if (__len1 + __len2 == 2) { - if (*__middle < *__first) - iter_swap(__first, __middle); - return; - } - _BidirectionalIterator __first_cut = __first; - _BidirectionalIterator __second_cut = __middle; - _Distance __len11 = 0; - _Distance __len22 = 0; - if (__len1 > __len2) { - __len11 = __len1 / 2; - advance(__first_cut, __len11); - __second_cut = lower_bound(__middle, __last, *__first_cut); - __len22 = std::distance(__middle, __second_cut); - } - else { - __len22 = __len2 / 2; - advance(__second_cut, __len22); - __first_cut = upper_bound(__first, __middle, *__second_cut); - __len11 = std::distance(__first, __first_cut); - } - rotate(__first_cut, __middle, __second_cut); - _BidirectionalIterator __new_middle = __first_cut; - advance(__new_middle, std::distance(__middle, __second_cut)); - __merge_without_buffer(__first, __first_cut, __new_middle, - __len11, __len22); - __merge_without_buffer(__new_middle, __second_cut, __last, - __len1 - __len11, __len2 - __len22); - } - - /** - * @if maint - * This is a helper function for the merge routines. - * @endif - */ - template - void - __merge_without_buffer(_BidirectionalIterator __first, - _BidirectionalIterator __middle, - _BidirectionalIterator __last, - _Distance __len1, _Distance __len2, - _Compare __comp) - { - if (__len1 == 0 || __len2 == 0) - return; - if (__len1 + __len2 == 2) { - if (__comp(*__middle, *__first)) - iter_swap(__first, __middle); - return; - } - _BidirectionalIterator __first_cut = __first; - _BidirectionalIterator __second_cut = __middle; - _Distance __len11 = 0; - _Distance __len22 = 0; - if (__len1 > __len2) { - __len11 = __len1 / 2; - advance(__first_cut, __len11); - __second_cut = lower_bound(__middle, __last, *__first_cut, __comp); - __len22 = std::distance(__middle, __second_cut); - } - else { - __len22 = __len2 / 2; - advance(__second_cut, __len22); - __first_cut = upper_bound(__first, __middle, *__second_cut, __comp); - __len11 = std::distance(__first, __first_cut); - } - rotate(__first_cut, __middle, __second_cut); - _BidirectionalIterator __new_middle = __first_cut; - advance(__new_middle, std::distance(__middle, __second_cut)); - __merge_without_buffer(__first, __first_cut, __new_middle, - __len11, __len22, __comp); - __merge_without_buffer(__new_middle, __second_cut, __last, - __len1 - __len11, __len2 - __len22, __comp); - } - - /** - * @if maint - * This is a helper function for the merge routines. - * @endif - */ - template - _BidirectionalIterator1 - __rotate_adaptive(_BidirectionalIterator1 __first, - _BidirectionalIterator1 __middle, - _BidirectionalIterator1 __last, - _Distance __len1, _Distance __len2, - _BidirectionalIterator2 __buffer, - _Distance __buffer_size) + void + __merge_sort_loop(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, + _RandomAccessIterator2 __result, _Distance __step_size) { - _BidirectionalIterator2 __buffer_end; - if (__len1 > __len2 && __len2 <= __buffer_size) { - __buffer_end = copy(__middle, __last, __buffer); - copy_backward(__first, __middle, __last); - return copy(__buffer, __buffer_end, __first); + _Distance __two_step = 2 * __step_size; + + while (__last - __first >= __two_step) { + __result = std::merge(__first, __first + __step_size, + __first + __step_size, __first + __two_step, + __result); + __first += __two_step; } - else if (__len1 <= __buffer_size) { - __buffer_end = copy(__first, __middle, __buffer); - copy(__middle, __last, __first); - return copy_backward(__buffer, __buffer_end, __last); + + __step_size = std::min(_Distance(__last - __first), __step_size); + std::merge(__first, __first + __step_size, __first + __step_size, __last, + __result); + } + + template + void + __merge_sort_loop(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last, + _RandomAccessIterator2 __result, _Distance __step_size, + _Compare __comp) + { + _Distance __two_step = 2 * __step_size; + + while (__last - __first >= __two_step) { + __result = std::merge(__first, __first + __step_size, + __first + __step_size, __first + __two_step, + __result, + __comp); + __first += __two_step; } - else { - rotate(__first, __middle, __last); - advance(__first, std::distance(__middle, __last)); - return __first; + __step_size = std::min(_Distance(__last - __first), __step_size); + + std::merge(__first, __first + __step_size, + __first + __step_size, __last, + __result, + __comp); + } + + enum { _S_chunk_size = 7 }; + + template + void + __chunk_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, + _Distance __chunk_size) + { + while (__last - __first >= __chunk_size) { + std::__insertion_sort(__first, __first + __chunk_size); + __first += __chunk_size; + } + std::__insertion_sort(__first, __last); + } + + template + void + __chunk_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, + _Distance __chunk_size, _Compare __comp) + { + while (__last - __first >= __chunk_size) { + std::__insertion_sort(__first, __first + __chunk_size, __comp); + __first += __chunk_size; + } + std::__insertion_sort(__first, __last, __comp); + } + + template + void + __merge_sort_with_buffer(_RandomAccessIterator __first, _RandomAccessIterator __last, + _Pointer __buffer) + { + typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance; + + _Distance __len = __last - __first; + _Pointer __buffer_last = __buffer + __len; + + _Distance __step_size = _S_chunk_size; + std::__chunk_insertion_sort(__first, __last, __step_size); + + while (__step_size < __len) { + std::__merge_sort_loop(__first, __last, __buffer, __step_size); + __step_size *= 2; + std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size); + __step_size *= 2; + } + } + + template + void + __merge_sort_with_buffer(_RandomAccessIterator __first, _RandomAccessIterator __last, + _Pointer __buffer, _Compare __comp) + { + typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance; + + _Distance __len = __last - __first; + _Pointer __buffer_last = __buffer + __len; + + _Distance __step_size = _S_chunk_size; + std::__chunk_insertion_sort(__first, __last, __step_size, __comp); + + while (__step_size < __len) { + std::__merge_sort_loop(__first, __last, __buffer, __step_size, __comp); + __step_size *= 2; + std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size, __comp); + __step_size *= 2; } } @@ -3335,22 +2928,22 @@ __result, __binary_pred, _IterType()); _BidirectionalIterator3 __result) { if (__first1 == __last1) - return copy_backward(__first2, __last2, __result); + return std::copy_backward(__first2, __last2, __result); if (__first2 == __last2) - return copy_backward(__first1, __last1, __result); + return std::copy_backward(__first1, __last1, __result); --__last1; --__last2; while (true) { if (*__last2 < *__last1) { *--__result = *__last1; if (__first1 == __last1) - return copy_backward(__first2, ++__last2, __result); + return std::copy_backward(__first2, ++__last2, __result); --__last1; } else { *--__result = *__last2; if (__first2 == __last2) - return copy_backward(__first1, ++__last1, __result); + return std::copy_backward(__first1, ++__last1, __result); --__last2; } } @@ -3370,27 +2963,60 @@ __result, __binary_pred, _IterType()); _Compare __comp) { if (__first1 == __last1) - return copy_backward(__first2, __last2, __result); + return std::copy_backward(__first2, __last2, __result); if (__first2 == __last2) - return copy_backward(__first1, __last1, __result); + return std::copy_backward(__first1, __last1, __result); --__last1; --__last2; while (true) { if (__comp(*__last2, *__last1)) { *--__result = *__last1; if (__first1 == __last1) - return copy_backward(__first2, ++__last2, __result); + return std::copy_backward(__first2, ++__last2, __result); --__last1; } else { *--__result = *__last2; if (__first2 == __last2) - return copy_backward(__first1, ++__last1, __result); + return std::copy_backward(__first1, ++__last1, __result); --__last2; } } } + /** + * @if maint + * This is a helper function for the merge routines. + * @endif + */ + template + _BidirectionalIterator1 + __rotate_adaptive(_BidirectionalIterator1 __first, + _BidirectionalIterator1 __middle, + _BidirectionalIterator1 __last, + _Distance __len1, _Distance __len2, + _BidirectionalIterator2 __buffer, + _Distance __buffer_size) + { + _BidirectionalIterator2 __buffer_end; + if (__len1 > __len2 && __len2 <= __buffer_size) { + __buffer_end = std::copy(__middle, __last, __buffer); + std::copy_backward(__first, __middle, __last); + return std::copy(__buffer, __buffer_end, __first); + } + else if (__len1 <= __buffer_size) { + __buffer_end = std::copy(__first, __middle, __buffer); + std::copy(__middle, __last, __first); + return std::copy_backward(__buffer, __buffer_end, __last); + } + else { + std::rotate(__first, __middle, __last); + std::advance(__first, std::distance(__middle, __last)); + return __first; + } + } + /** * @if maint * This is a helper function for the merge routines. @@ -3405,12 +3031,12 @@ __result, __binary_pred, _IterType()); _Pointer __buffer, _Distance __buffer_size) { if (__len1 <= __len2 && __len1 <= __buffer_size) { - _Pointer __buffer_end = copy(__first, __middle, __buffer); - merge(__buffer, __buffer_end, __middle, __last, __first); + _Pointer __buffer_end = std::copy(__first, __middle, __buffer); + std::merge(__buffer, __buffer_end, __middle, __last, __first); } else if (__len2 <= __buffer_size) { - _Pointer __buffer_end = copy(__middle, __last, __buffer); - __merge_backward(__first, __middle, __buffer, __buffer_end, __last); + _Pointer __buffer_end = std::copy(__middle, __last, __buffer); + std::__merge_backward(__first, __middle, __buffer, __buffer_end, __last); } else { _BidirectionalIterator __first_cut = __first; @@ -3419,24 +3045,24 @@ __result, __binary_pred, _IterType()); _Distance __len22 = 0; if (__len1 > __len2) { __len11 = __len1 / 2; - advance(__first_cut, __len11); - __second_cut = lower_bound(__middle, __last, *__first_cut); + std::advance(__first_cut, __len11); + __second_cut = std::lower_bound(__middle, __last, *__first_cut); __len22 = std::distance(__middle, __second_cut); } else { __len22 = __len2 / 2; - advance(__second_cut, __len22); - __first_cut = upper_bound(__first, __middle, *__second_cut); + std::advance(__second_cut, __len22); + __first_cut = std::upper_bound(__first, __middle, *__second_cut); __len11 = std::distance(__first, __first_cut); } _BidirectionalIterator __new_middle = - __rotate_adaptive(__first_cut, __middle, __second_cut, - __len1 - __len11, __len22, __buffer, - __buffer_size); - __merge_adaptive(__first, __first_cut, __new_middle, __len11, - __len22, __buffer, __buffer_size); - __merge_adaptive(__new_middle, __second_cut, __last, __len1 - __len11, - __len2 - __len22, __buffer, __buffer_size); + std::__rotate_adaptive(__first_cut, __middle, __second_cut, + __len1 - __len11, __len22, __buffer, + __buffer_size); + std::__merge_adaptive(__first, __first_cut, __new_middle, __len11, + __len22, __buffer, __buffer_size); + std::__merge_adaptive(__new_middle, __second_cut, __last, __len1 - __len11, + __len2 - __len22, __buffer, __buffer_size); } } @@ -3456,13 +3082,13 @@ __result, __binary_pred, _IterType()); _Compare __comp) { if (__len1 <= __len2 && __len1 <= __buffer_size) { - _Pointer __buffer_end = copy(__first, __middle, __buffer); - merge(__buffer, __buffer_end, __middle, __last, __first, __comp); + _Pointer __buffer_end = std::copy(__first, __middle, __buffer); + std::merge(__buffer, __buffer_end, __middle, __last, __first, __comp); } else if (__len2 <= __buffer_size) { - _Pointer __buffer_end = copy(__middle, __last, __buffer); - __merge_backward(__first, __middle, __buffer, __buffer_end, __last, - __comp); + _Pointer __buffer_end = std::copy(__middle, __last, __buffer); + std::__merge_backward(__first, __middle, __buffer, __buffer_end, __last, + __comp); } else { _BidirectionalIterator __first_cut = __first; @@ -3471,24 +3097,24 @@ __result, __binary_pred, _IterType()); _Distance __len22 = 0; if (__len1 > __len2) { __len11 = __len1 / 2; - advance(__first_cut, __len11); - __second_cut = lower_bound(__middle, __last, *__first_cut, __comp); + std::advance(__first_cut, __len11); + __second_cut = std::lower_bound(__middle, __last, *__first_cut, __comp); __len22 = std::distance(__middle, __second_cut); } else { __len22 = __len2 / 2; - advance(__second_cut, __len22); - __first_cut = upper_bound(__first, __middle, *__second_cut, __comp); + std::advance(__second_cut, __len22); + __first_cut = std::upper_bound(__first, __middle, *__second_cut, __comp); __len11 = std::distance(__first, __first_cut); } _BidirectionalIterator __new_middle = - __rotate_adaptive(__first_cut, __middle, __second_cut, - __len1 - __len11, __len22, __buffer, - __buffer_size); - __merge_adaptive(__first, __first_cut, __new_middle, __len11, - __len22, __buffer, __buffer_size, __comp); - __merge_adaptive(__new_middle, __second_cut, __last, __len1 - __len11, - __len2 - __len22, __buffer, __buffer_size, __comp); + std::__rotate_adaptive(__first_cut, __middle, __second_cut, + __len1 - __len11, __len22, __buffer, + __buffer_size); + std::__merge_adaptive(__first, __first_cut, __new_middle, __len11, + __len22, __buffer, __buffer_size, __comp); + std::__merge_adaptive(__new_middle, __second_cut, __last, __len1 - __len11, + __len2 - __len22, __buffer, __buffer_size, __comp); } } @@ -3533,10 +3159,10 @@ __result, __binary_pred, _IterType()); _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first, __last); if (__buf.begin() == 0) - __merge_without_buffer(__first, __middle, __last, __len1, __len2); + std::__merge_without_buffer(__first, __middle, __last, __len1, __len2); else - __merge_adaptive(__first, __middle, __last, __len1, __len2, - __buf.begin(), _DistanceType(__buf.size())); + std::__merge_adaptive(__first, __middle, __last, __len1, __len2, + __buf.begin(), _DistanceType(__buf.size())); } /** @@ -3586,11 +3212,380 @@ __result, __binary_pred, _IterType()); _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first, __last); if (__buf.begin() == 0) - __merge_without_buffer(__first, __middle, __last, __len1, __len2, __comp); + std::__merge_without_buffer(__first, __middle, __last, __len1, __len2, __comp); else - __merge_adaptive(__first, __middle, __last, __len1, __len2, - __buf.begin(), _DistanceType(__buf.size()), - __comp); + std::__merge_adaptive(__first, __middle, __last, __len1, __len2, + __buf.begin(), _DistanceType(__buf.size()), + __comp); + } + + template + void + __stable_sort_adaptive(_RandomAccessIterator __first, _RandomAccessIterator __last, + _Pointer __buffer, _Distance __buffer_size) + { + _Distance __len = (__last - __first + 1) / 2; + _RandomAccessIterator __middle = __first + __len; + if (__len > __buffer_size) { + std::__stable_sort_adaptive(__first, __middle, __buffer, __buffer_size); + std::__stable_sort_adaptive(__middle, __last, __buffer, __buffer_size); + } + else { + std::__merge_sort_with_buffer(__first, __middle, __buffer); + std::__merge_sort_with_buffer(__middle, __last, __buffer); + } + std::__merge_adaptive(__first, __middle, __last, _Distance(__middle - __first), + _Distance(__last - __middle), __buffer, __buffer_size); + } + + template + void + __stable_sort_adaptive(_RandomAccessIterator __first, _RandomAccessIterator __last, + _Pointer __buffer, _Distance __buffer_size, + _Compare __comp) + { + _Distance __len = (__last - __first + 1) / 2; + _RandomAccessIterator __middle = __first + __len; + if (__len > __buffer_size) { + std::__stable_sort_adaptive(__first, __middle, __buffer, __buffer_size, + __comp); + std::__stable_sort_adaptive(__middle, __last, __buffer, __buffer_size, + __comp); + } + else { + std::__merge_sort_with_buffer(__first, __middle, __buffer, __comp); + std::__merge_sort_with_buffer(__middle, __last, __buffer, __comp); + } + std::__merge_adaptive(__first, __middle, __last, _Distance(__middle - __first), + _Distance(__last - __middle), __buffer, __buffer_size, + __comp); + } + + /** + * @brief Sort the elements of a sequence, preserving the relative order + * of equivalent elements. + * @param first An iterator. + * @param last Another iterator. + * @return Nothing. + * + * Sorts the elements in the range @p [first,last) in ascending order, + * such that @p *(i+1)<*i is false for each iterator @p i in the range + * @p [first,last-1). + * + * The relative ordering of equivalent elements is preserved, so any two + * elements @p x and @p y in the range @p [first,last) such that + * @p x + inline void + stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last) + { + typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; + typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType; + + // concept requirements + __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept< + _RandomAccessIterator>) + __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>) + + _Temporary_buffer<_RandomAccessIterator, _ValueType> buf(__first, __last); + if (buf.begin() == 0) + std::__inplace_stable_sort(__first, __last); + else + std::__stable_sort_adaptive(__first, __last, buf.begin(), _DistanceType(buf.size())); + } + + /** + * @brief Sort the elements of a sequence using a predicate for comparison, + * preserving the relative order of equivalent elements. + * @param first An iterator. + * @param last Another iterator. + * @param comp A comparison functor. + * @return Nothing. + * + * Sorts the elements in the range @p [first,last) in ascending order, + * such that @p comp(*(i+1),*i) is false for each iterator @p i in the + * range @p [first,last-1). + * + * The relative ordering of equivalent elements is preserved, so any two + * elements @p x and @p y in the range @p [first,last) such that + * @p comp(x,y) is false and @p comp(y,x) is false will have the same + * relative ordering after calling @p stable_sort(). + */ + template + inline void + stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp) + { + typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; + typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType; + + // concept requirements + __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept< + _RandomAccessIterator>) + __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, + _ValueType, _ValueType>) + + _Temporary_buffer<_RandomAccessIterator, _ValueType> buf(__first, __last); + if (buf.begin() == 0) + std::__inplace_stable_sort(__first, __last, __comp); + else + std::__stable_sort_adaptive(__first, __last, buf.begin(), _DistanceType(buf.size()), + __comp); + } + + /** + * @brief Sort a sequence just enough to find a particular position. + * @param first An iterator. + * @param nth Another iterator. + * @param last Another iterator. + * @return Nothing. + * + * Rearranges the elements in the range @p [first,last) so that @p *nth + * is the same element that would have been in that position had the + * whole sequence been sorted. + * whole sequence been sorted. The elements either side of @p *nth are + * not completely sorted, but for any iterator @i in the range + * @p [first,nth) and any iterator @j in the range @p [nth,last) it + * holds that @p *j<*i is false. + */ + template + void + nth_element(_RandomAccessIterator __first, + _RandomAccessIterator __nth, + _RandomAccessIterator __last) + { + typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; + + // concept requirements + __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>) + __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>) + + while (__last - __first > 3) { + _RandomAccessIterator __cut = + std::__unguarded_partition(__first, __last, + _ValueType(std::__median(*__first, + *(__first + (__last - __first)/2), + *(__last - 1)))); + if (__cut <= __nth) + __first = __cut; + else + __last = __cut; + } + std::__insertion_sort(__first, __last); + } + + /** + * @brief Sort a sequence just enough to find a particular position + * using a predicate for comparison. + * @param first An iterator. + * @param nth Another iterator. + * @param last Another iterator. + * @param comp A comparison functor. + * @return Nothing. + * + * Rearranges the elements in the range @p [first,last) so that @p *nth + * is the same element that would have been in that position had the + * whole sequence been sorted. The elements either side of @p *nth are + * not completely sorted, but for any iterator @i in the range + * @p [first,nth) and any iterator @j in the range @p [nth,last) it + * holds that @p comp(*j,*i) is false. + */ + template + void + nth_element(_RandomAccessIterator __first, + _RandomAccessIterator __nth, + _RandomAccessIterator __last, + _Compare __comp) + { + typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType; + + // concept requirements + __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>) + __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, + _ValueType, _ValueType>) + + while (__last - __first > 3) { + _RandomAccessIterator __cut = + std::__unguarded_partition(__first, __last, + _ValueType(std::__median(*__first, + *(__first + (__last - __first)/2), + *(__last - 1), + __comp)), __comp); + if (__cut <= __nth) + __first = __cut; + else + __last = __cut; + } + std::__insertion_sort(__first, __last, __comp); + } + + /** + * @brief Finds the largest subrange in which @a val could be inserted + * at any place in it without changing the ordering. + * @param first An iterator. + * @param last Another iterator. + * @param val The search term. + * @return An pair of iterators defining the subrange. + * @ingroup binarysearch + * + * This is equivalent to + * @code + * std::make_pair(lower_bound(first, last, val), + * upper_bound(first, last, val)) + * @endcode + * but does not actually call those functions. + */ + template + pair<_ForwardIterator, _ForwardIterator> + equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val) + { + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType; + + // concept requirements + // See comments on lower_bound. + __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>) + __glibcpp_function_requires(_SameTypeConcept<_Tp, _ValueType>) + __glibcpp_function_requires(_LessThanComparableConcept<_Tp>) + + _DistanceType __len = std::distance(__first, __last); + _DistanceType __half; + _ForwardIterator __middle, __left, __right; + + while (__len > 0) { + __half = __len >> 1; + __middle = __first; + std::advance(__middle, __half); + if (*__middle < __val) { + __first = __middle; + ++__first; + __len = __len - __half - 1; + } + else if (__val < *__middle) + __len = __half; + else { + __left = std::lower_bound(__first, __middle, __val); + std::advance(__first, __len); + __right = std::upper_bound(++__middle, __first, __val); + return std::pair<_ForwardIterator, _ForwardIterator>(__left, __right); + } + } + return std::pair<_ForwardIterator, _ForwardIterator>(__first, __first); + } + + /** + * @brief Finds the largest subrange in which @a val could be inserted + * at any place in it without changing the ordering. + * @param first An iterator. + * @param last Another iterator. + * @param val The search term. + * @param comp A functor to use for comparisons. + * @return An pair of iterators defining the subrange. + * @ingroup binarysearch + * + * This is equivalent to + * @code + * std::make_pair(lower_bound(first, last, val, comp), + * upper_bound(first, last, val, comp)) + * @endcode + * but does not actually call those functions. + */ + template + pair<_ForwardIterator, _ForwardIterator> + equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val, + _Compare __comp) + { + typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType; + typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType; + + // concept requirements + __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>) + __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _Tp>) + __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp, _ValueType>) + + _DistanceType __len = std::distance(__first, __last); + _DistanceType __half; + _ForwardIterator __middle, __left, __right; + + while (__len > 0) { + __half = __len >> 1; + __middle = __first; + std::advance(__middle, __half); + if (__comp(*__middle, __val)) { + __first = __middle; + ++__first; + __len = __len - __half - 1; + } + else if (__comp(__val, *__middle)) + __len = __half; + else { + __left = std::lower_bound(__first, __middle, __val, __comp); + std::advance(__first, __len); + __right = std::upper_bound(++__middle, __first, __val, __comp); + return std::pair<_ForwardIterator, _ForwardIterator>(__left, __right); + } + } + return std::pair<_ForwardIterator, _ForwardIterator>(__first, __first); + } + + /** + * @brief Determines whether an element exists in a range. + * @param first An iterator. + * @param last Another iterator. + * @param val The search term. + * @return True if @a val (or its equivelent) is in [@a first,@a last ]. + * @ingroup binarysearch + * + * Note that this does not actually return an iterator to @a val. For + * that, use std::find or a container's specialized find member functions. + */ + template + bool + binary_search(_ForwardIterator __first, _ForwardIterator __last, + const _Tp& __val) + { + // concept requirements + // See comments on lower_bound. + __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>) + __glibcpp_function_requires(_SameTypeConcept<_Tp, + typename iterator_traits<_ForwardIterator>::value_type>) + __glibcpp_function_requires(_LessThanComparableConcept<_Tp>) + + _ForwardIterator __i = std::lower_bound(__first, __last, __val); + return __i != __last && !(__val < *__i); + } + + /** + * @brief Determines whether an element exists in a range. + * @param first An iterator. + * @param last Another iterator. + * @param val The search term. + * @param comp A functor to use for comparisons. + * @return True if @a val (or its equivelent) is in [@a first,@a last ]. + * @ingroup binarysearch + * + * Note that this does not actually return an iterator to @a val. For + * that, use std::find or a container's specialized find member functions. + * + * The comparison function should have the same effects on ordering as + * the function used for the initial sort. + */ + template + bool + binary_search(_ForwardIterator __first, _ForwardIterator __last, + const _Tp& __val, _Compare __comp) + { + // concept requirements + __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>) + __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, + typename iterator_traits<_ForwardIterator>::value_type, _Tp>) + __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp, + typename iterator_traits<_ForwardIterator>::value_type>) + + _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp); + return __i != __last && !__comp(__val, *__i); } // Set algorithms: includes, set_union, set_intersection, set_difference, @@ -3682,7 +3677,7 @@ __result, __binary_pred, _IterType()); } ++__result; } - return copy(__first2, __last2, copy(__first1, __last1, __result)); + return std::copy(__first2, __last2, std::copy(__first1, __last1, __result)); } template @@ -3816,7 +3811,7 @@ __result, __binary_pred, _IterType()); ++__first1; ++__first2; } - return copy(__first1, __last1, __result); + return std::copy(__first1, __last1, __result); } template @@ -3885,7 +3880,7 @@ __result, __binary_pred, _IterType()); ++__first1; ++__first2; } - return copy(__first2, __last2, copy(__first1, __last1, __result)); + return std::copy(__first2, __last2, std::copy(__first1, __last1, __result)); } template::value_type, typename iterator_traits<_ForwardIterator2>::value_type>) - return __find_end(__first1, __last1, __first2, __last2, - __iterator_category(__first1), - __iterator_category(__first2)); + return std::__find_end(__first1, __last1, __first2, __last2, + __iterator_category(__first1), + __iterator_category(__first2)); } template::value_type, typename iterator_traits<_ForwardIterator2>::value_type>) - return __find_end(__first1, __last1, __first2, __last2, - __iterator_category(__first1), - __iterator_category(__first2), - __comp); + return std::__find_end(__first1, __last1, __first2, __last2, + __iterator_category(__first1), + __iterator_category(__first2), + __comp); } } // namespace std