stl_algo.h (LOTS): Fully qualify standard functions with std::, thus avoiding Koenig...
authorPaolo Carlini <pcarlini@unitus.it>
Mon, 30 Jun 2003 01:46:42 +0000 (03:46 +0200)
committerPaolo Carlini <paolo@gcc.gnu.org>
Mon, 30 Jun 2003 01:46:42 +0000 (01:46 +0000)
2003-06-29  Paolo Carlini  <pcarlini@unitus.it>

* include/bits/stl_algo.h (LOTS): Fully qualify standard
functions with std::, thus avoiding Koenig lookup.

From-SVN: r68690

libstdc++-v3/ChangeLog
libstdc++-v3/include/bits/stl_algo.h

index 21c54b5..2090247 100644 (file)
@@ -1,5 +1,10 @@
 2003-06-29  Paolo Carlini  <pcarlini@unitus.it>
 
+       * include/bits/stl_algo.h (LOTS): Fully qualify standard
+       functions with std::, thus avoiding Koenig lookup.
+
+2003-06-29  Paolo Carlini  <pcarlini@unitus.it>
+
        * testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-1.cc:
        Improve type correctness-wise.
        * testsuite/27_io/basic_filebuf/overflow/wchar_t/11305-2.cc:
index ac15b3a..a43c17a 100644 (file)
@@ -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);
     }
 
   /**
@@ -1130,37 +1130,6 @@ namespace std
     }
 
   /**
-   *  @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<typename _InputIterator, typename _OutputIterator>
-    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
    *  unique_copy(_InputIterator, _InputIterator, _OutputIterator, _BinaryPredicate)
@@ -1215,6 +1184,37 @@ namespace std
     }
 
   /**
+   *  @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<typename _InputIterator, typename _OutputIterator>
+    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.
    *  @param  last         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);
     }
 
   /**
@@ -2094,6 +2093,190 @@ __result, __binary_pred, _IterType());
     }
 
   /**
+   *  @brief Sort the smallest elements of a sequence.
+   *  @param  first   An iterator.
+   *  @param  middle  Another iterator.
+   *  @param  last    Another iterator.
+   *  @return  Nothing.
+   *
+   *  Sorts the smallest @p (middle-first) elements in the range
+   *  @p [first,last) and moves them to the range @p [first,middle). The
+   *  order of the remaining elements in the range @p [middle,last) is
+   *  undefined.
+   *  After the sort if @p i and @j are iterators in the range
+   *  @p [first,middle) such that @i precedes @j and @k is an iterator in
+   *  the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
+  */
+  template<typename _RandomAccessIterator>
+    void
+    partial_sort(_RandomAccessIterator __first,
+                _RandomAccessIterator __middle,
+                _RandomAccessIterator __last)
+    {
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
+
+      // concept requirements
+      __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
+      __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
+
+      std::make_heap(__first, __middle);
+      for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
+       if (*__i < *__first)
+         std::__pop_heap(__first, __middle, __i, _ValueType(*__i));
+      std::sort_heap(__first, __middle);
+    }
+
+  /**
+   *  @brief Sort the smallest elements of a sequence using a predicate
+   *         for comparison.
+   *  @param  first   An iterator.
+   *  @param  middle  Another iterator.
+   *  @param  last    Another iterator.
+   *  @param  comp    A comparison functor.
+   *  @return  Nothing.
+   *
+   *  Sorts the smallest @p (middle-first) elements in the range
+   *  @p [first,last) and moves them to the range @p [first,middle). The
+   *  order of the remaining elements in the range @p [middle,last) is
+   *  undefined.
+   *  After the sort if @p i and @j are iterators in the range
+   *  @p [first,middle) such that @i precedes @j and @k is an iterator in
+   *  the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
+   *  are both false.
+  */
+  template<typename _RandomAccessIterator, typename _Compare>
+    void
+    partial_sort(_RandomAccessIterator __first,
+                _RandomAccessIterator __middle,
+                _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>)
+
+      std::make_heap(__first, __middle, __comp);
+      for (_RandomAccessIterator __i = __middle; __i < __last; ++__i)
+       if (__comp(*__i, *__first))
+         std::__pop_heap(__first, __middle, __i, _ValueType(*__i), __comp);
+      std::sort_heap(__first, __middle, __comp);
+    }
+
+  /**
+   *  @brief Copy the smallest elements of a sequence.
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @param  result_first   A random-access iterator.
+   *  @param  result_last    Another random-access iterator.
+   *  @return   An iterator indicating the end of the resulting sequence.
+   *
+   *  Copies and sorts the smallest N values from the range @p [first,last)
+   *  to the range beginning at @p result_first, where the number of
+   *  elements to be copied, @p N, is the smaller of @p (last-first) and
+   *  @p (result_last-result_first).
+   *  After the sort if @p i and @j are iterators in the range
+   *  @p [result_first,result_first+N) such that @i precedes @j then
+   *  @p *j<*i is false.
+   *  The value returned is @p result_first+N.
+  */
+  template<typename _InputIterator, typename _RandomAccessIterator>
+    _RandomAccessIterator
+    partial_sort_copy(_InputIterator __first, _InputIterator __last,
+                     _RandomAccessIterator __result_first,
+                     _RandomAccessIterator __result_last)
+    {
+      typedef typename iterator_traits<_InputIterator>::value_type _InputValueType;
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type _OutputValueType;
+      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
+
+      // concept requirements
+      __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcpp_function_requires(_ConvertibleConcept<_InputValueType, _OutputValueType>)
+      __glibcpp_function_requires(_LessThanComparableConcept<_OutputValueType>)
+      __glibcpp_function_requires(_LessThanComparableConcept<_InputValueType>)
+
+      if (__result_first == __result_last) return __result_last;
+      _RandomAccessIterator __result_real_last = __result_first;
+      while(__first != __last && __result_real_last != __result_last) {
+       *__result_real_last = *__first;
+       ++__result_real_last;
+       ++__first;
+      }
+      std::make_heap(__result_first, __result_real_last);
+      while (__first != __last) {
+       if (*__first < *__result_first)
+         std::__adjust_heap(__result_first, _DistanceType(0),
+                            _DistanceType(__result_real_last - __result_first),
+                            _InputValueType(*__first));
+       ++__first;
+      }
+      std::sort_heap(__result_first, __result_real_last);
+      return __result_real_last;
+    }
+
+  /**
+   *  @brief Copy the smallest elements of a sequence using a predicate for
+   *         comparison.
+   *  @param  first   An input iterator.
+   *  @param  last    Another input iterator.
+   *  @param  result_first   A random-access iterator.
+   *  @param  result_last    Another random-access iterator.
+   *  @param  comp    A comparison functor.
+   *  @return   An iterator indicating the end of the resulting sequence.
+   *
+   *  Copies and sorts the smallest N values from the range @p [first,last)
+   *  to the range beginning at @p result_first, where the number of
+   *  elements to be copied, @p N, is the smaller of @p (last-first) and
+   *  @p (result_last-result_first).
+   *  After the sort if @p i and @j are iterators in the range
+   *  @p [result_first,result_first+N) such that @i precedes @j then
+   *  @p comp(*j,*i) is false.
+   *  The value returned is @p result_first+N.
+  */
+  template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
+    _RandomAccessIterator
+    partial_sort_copy(_InputIterator __first, _InputIterator __last,
+                     _RandomAccessIterator __result_first,
+                     _RandomAccessIterator __result_last,
+                     _Compare __comp)
+    {
+      typedef typename iterator_traits<_InputIterator>::value_type _InputValueType;
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type _OutputValueType;
+      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
+
+      // concept requirements
+      __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>)
+      __glibcpp_function_requires(_ConvertibleConcept<_InputValueType, _OutputValueType>)
+      __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _OutputValueType, _OutputValueType>)
+
+      if (__result_first == __result_last) return __result_last;
+      _RandomAccessIterator __result_real_last = __result_first;
+      while(__first != __last && __result_real_last != __result_last) {
+       *__result_real_last = *__first;
+       ++__result_real_last;
+       ++__first;
+      }
+      std::make_heap(__result_first, __result_real_last, __comp);
+      while (__first != __last) {
+       if (__comp(*__first, *__result_first))
+         std::__adjust_heap(__result_first, _DistanceType(0),
+                            _DistanceType(__result_real_last - __result_first),
+                            _InputValueType(*__first),
+                            __comp);
+       ++__first;
+      }
+      std::sort_heap(__result_first, __result_real_last, __comp);
+      return __result_real_last;
+    }
+
+  /**
    *  @if maint
    *  This is a helper function for the sort routine.
    *  @endif
@@ -2107,16 +2290,16 @@ __result, __binary_pred, _IterType());
 
       while (__last - __first > _S_threshold) {
        if (__depth_limit == 0) {
-         partial_sort(__first, __last, __last);
+         std::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);
+         std::__unguarded_partition(__first, __last,
+                                    _ValueType(std::__median(*__first,
+                                                             *(__first + (__last - __first)/2),
+                                                             *(__last - 1))));
+       std::__introsort_loop(__cut, __last, __depth_limit);
        __last = __cut;
       }
     }
@@ -2135,17 +2318,16 @@ __result, __binary_pred, _IterType());
 
       while (__last - __first > _S_threshold) {
        if (__depth_limit == 0) {
-         partial_sort(__first, __last, __last, __comp);
+         std::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);
+         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;
       }
     }
@@ -2175,8 +2357,8 @@ __result, __binary_pred, _IterType());
       __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
 
       if (__first != __last) {
-       __introsort_loop(__first, __last, __lg(__last - __first) * 2);
-       __final_insertion_sort(__first, __last);
+       std::__introsort_loop(__first, __last, __lg(__last - __first) * 2);
+       std::__final_insertion_sort(__first, __last);
       }
     }
 
@@ -2206,1391 +2388,1204 @@ __result, __binary_pred, _IterType());
       __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);
+       std::__introsort_loop(__first, __last, __lg(__last - __first) * 2, __comp);
+       std::__final_insertion_sort(__first, __last, __comp);
       }
     }
 
-
   /**
-   *  @if maint
-   *  This is a helper function for the stable sorting routines.
-   *  @endif
+   *  @brief Finds the first position in which @a val could be inserted
+   *         without changing the ordering.
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @param  val     The search term.
+   *  @return  An iterator pointing to the first element "not less than" @a val,
+   *           or end() if every element is less than @a val.
+   *  @ingroup binarysearch
   */
-  template<typename _RandomAccessIterator>
-    void
-    __inplace_stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
+  template<typename _ForwardIterator, typename _Tp>
+    _ForwardIterator
+    lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val)
     {
-      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);
-    }
+      typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+      typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
 
-  /**
-   *  @if maint
-   *  This is a helper function for the stable sorting routines.
-   *  @endif
-  */
-  template<typename _RandomAccessIterator, typename _Compare>
-    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);
-    }
+      // concept requirements
+      // Note that these are slightly stricter than those of the 4-argument
+      // version, defined next.  The difference is in the strictness of the
+      // comparison operations... so for looser checking, define your own
+      // comparison function, as was intended.
+      __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcpp_function_requires(_SameTypeConcept<_Tp, _ValueType>)
+      __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
 
-  template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
-          typename _Distance>
-    void
-    __merge_sort_loop(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last,
-                     _RandomAccessIterator2 __result, _Distance __step_size)
-    {
-      _Distance __two_step = 2 * __step_size;
+      _DistanceType __len = std::distance(__first, __last);
+      _DistanceType __half;
+      _ForwardIterator __middle;
 
-      while (__last - __first >= __two_step) {
-       __result = merge(__first, __first + __step_size,
-                        __first + __step_size, __first + __two_step,
-                        __result);
-       __first += __two_step;
+      while (__len > 0) {
+       __half = __len >> 1;
+       __middle = __first;
+       std::advance(__middle, __half);
+       if (*__middle < __val) {
+         __first = __middle;
+         ++__first;
+         __len = __len - __half - 1;
+       }
+       else
+         __len = __half;
       }
-
-      __step_size = std::min(_Distance(__last - __first), __step_size);
-      merge(__first, __first + __step_size, __first + __step_size, __last,
-           __result);
+      return __first;
     }
 
-  template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
-          typename _Distance, typename _Compare>
-    void
-    __merge_sort_loop(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last,
-                     _RandomAccessIterator2 __result, _Distance __step_size,
-                     _Compare __comp)
+  /**
+   *  @brief Finds the first position in which @a val could be inserted
+   *         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 iterator pointing to the first element "not less than" @a val,
+   *           or end() if every element is less than @a val.
+   *  @ingroup binarysearch
+   *
+   *  The comparison function should have the same effects on ordering as
+   *  the function used for the initial sort.
+  */
+  template<typename _ForwardIterator, typename _Tp, typename _Compare>
+    _ForwardIterator
+    lower_bound(_ForwardIterator __first, _ForwardIterator __last,
+               const _Tp& __val, _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);
-    }
+      typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+      typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
 
-  enum { _S_chunk_size = 7 };
+      // concept requirements
+      __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _Tp>)
 
-  template<typename _RandomAccessIterator, typename _Distance>
-    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);
-    }
+      _DistanceType __len = std::distance(__first, __last);
+      _DistanceType __half;
+      _ForwardIterator __middle;
 
-  template<typename _RandomAccessIterator, typename _Distance, typename _Compare>
-    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;
+      while (__len > 0) {
+       __half = __len >> 1;
+       __middle = __first;
+       std::advance(__middle, __half);
+       if (__comp(*__middle, __val)) {
+         __first = __middle;
+         ++__first;
+         __len = __len - __half - 1;
+       }
+       else
+         __len = __half;
       }
-      __insertion_sort(__first, __last, __comp);
+      return __first;
     }
 
-  template<typename _RandomAccessIterator, typename _Pointer>
-    void
-    __merge_sort_with_buffer(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                             _Pointer __buffer)
+  /**
+   *  @brief Finds the last position in which @a val could be inserted
+   *         without changing the ordering.
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @param  val     The search term.
+   *  @return  An iterator pointing to the first element greater than @a val,
+   *           or end() if no elements are greater than @a val.
+   *  @ingroup binarysearch
+  */
+  template<typename _ForwardIterator, typename _Tp>
+    _ForwardIterator
+    upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
+      typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+      typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
 
-      _Distance __len = __last - __first;
-      _Pointer __buffer_last = __buffer + __len;
+      // concept requirements
+      // See comments on lower_bound.
+      __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcpp_function_requires(_SameTypeConcept<_Tp, _ValueType>)
+      __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
 
-      _Distance __step_size = _S_chunk_size;
-      __chunk_insertion_sort(__first, __last, __step_size);
+      _DistanceType __len = std::distance(__first, __last);
+      _DistanceType __half;
+      _ForwardIterator __middle;
 
-      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;
+      while (__len > 0) {
+       __half = __len >> 1;
+       __middle = __first;
+       std::advance(__middle, __half);
+       if (__val < *__middle)
+         __len = __half;
+       else {
+         __first = __middle;
+         ++__first;
+         __len = __len - __half - 1;
+       }
       }
+      return __first;
     }
 
-  template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
-    void
-    __merge_sort_with_buffer(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                             _Pointer __buffer, _Compare __comp)
+  /**
+   *  @brief Finds the last position in which @a val could be inserted
+   *         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 iterator pointing to the first element greater than @a val,
+   *           or end() if no elements are greater than @a val.
+   *  @ingroup binarysearch
+   *
+   *  The comparison function should have the same effects on ordering as
+   *  the function used for the initial sort.
+  */
+  template<typename _ForwardIterator, typename _Tp, typename _Compare>
+    _ForwardIterator
+    upper_bound(_ForwardIterator __first, _ForwardIterator __last,
+               const _Tp& __val, _Compare __comp)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
+      typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
+      typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
 
-      _Distance __len = __last - __first;
-      _Pointer __buffer_last = __buffer + __len;
+      // concept requirements
+      __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp, _ValueType>)
 
-      _Distance __step_size = _S_chunk_size;
-      __chunk_insertion_sort(__first, __last, __step_size, __comp);
+      _DistanceType __len = std::distance(__first, __last);
+      _DistanceType __half;
+      _ForwardIterator __middle;
 
-      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;
+      while (__len > 0) {
+       __half = __len >> 1;
+       __middle = __first;
+       std::advance(__middle, __half);
+       if (__comp(__val, *__middle))
+         __len = __half;
+       else {
+         __first = __middle;
+         ++__first;
+         __len = __len - __half - 1;
+       }
       }
+      return __first;
     }
 
-  template<typename _RandomAccessIterator, typename _Pointer, typename _Distance>
+  /**
+   *  @if maint
+   *  This is a helper function for the merge routines.
+   *  @endif
+  */
+  template<typename _BidirectionalIterator, typename _Distance>
     void
-    __stable_sort_adaptive(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                           _Pointer __buffer, _Distance __buffer_size)
+    __merge_without_buffer(_BidirectionalIterator __first,
+                          _BidirectionalIterator __middle,
+                          _BidirectionalIterator __last,
+                          _Distance __len1, _Distance __len2)
     {
-      _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);
+      if (__len1 == 0 || __len2 == 0)
+       return;
+      if (__len1 + __len2 == 2) {
+       if (*__middle < *__first)
+         std::iter_swap(__first, __middle);
+       return;
       }
-      __merge_adaptive(__first, __middle, __last, _Distance(__middle - __first),
-                       _Distance(__last - __middle), __buffer, __buffer_size);
-    }
-
-  template<typename _RandomAccessIterator, typename _Pointer, typename _Distance,
-           typename _Compare>
-    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);
+      _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);
       }
       else {
-       __merge_sort_with_buffer(__first, __middle, __buffer, __comp);
-       __merge_sort_with_buffer(__middle, __last, __buffer, __comp);
+       __len22 = __len2 / 2;
+       std::advance(__second_cut, __len22);
+       __first_cut = std::upper_bound(__first, __middle, *__second_cut);
+       __len11 = std::distance(__first, __first_cut);
       }
-      __merge_adaptive(__first, __middle, __last, _Distance(__middle - __first),
-                       _Distance(__last - __middle), __buffer, __buffer_size,
-                       __comp);
+      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 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<y is false and @p y<x is false will have the same relative
-   *  ordering after calling @p stable_sort().
-  */
-  template<typename _RandomAccessIterator>
-    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().
+   *  @if maint
+   *  This is a helper function for the merge routines.
+   *  @endif
   */
-  template<typename _RandomAccessIterator, typename _Compare>
-    inline void
-    stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
+  template<typename _BidirectionalIterator, typename _Distance, typename _Compare>
+    void
+    __merge_without_buffer(_BidirectionalIterator __first,
+                           _BidirectionalIterator __middle,
+                          _BidirectionalIterator __last,
+                          _Distance __len1, _Distance __len2,
+                          _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);
+      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 Sort the smallest elements of a sequence.
-   *  @param  first   An iterator.
-   *  @param  middle  Another iterator.
-   *  @param  last    Another iterator.
-   *  @return  Nothing.
-   *
-   *  Sorts the smallest @p (middle-first) elements in the range
-   *  @p [first,last) and moves them to the range @p [first,middle). The
-   *  order of the remaining elements in the range @p [middle,last) is
-   *  undefined.
-   *  After the sort if @p i and @j are iterators in the range
-   *  @p [first,middle) such that @i precedes @j and @k is an iterator in
-   *  the range @p [middle,last) then @p *j<*i and @p *k<*i are both false.
+   *  @if maint
+   *  This is a helper function for the stable sorting routines.
+   *  @endif
   */
   template<typename _RandomAccessIterator>
     void
-    partial_sort(_RandomAccessIterator __first,
-                _RandomAccessIterator __middle,
-                _RandomAccessIterator __last)
+    __inplace_stable_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>)
-
-      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);
+      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);
     }
 
   /**
-   *  @brief Sort the smallest elements of a sequence using a predicate
-   *         for comparison.
-   *  @param  first   An iterator.
-   *  @param  middle  Another iterator.
-   *  @param  last    Another iterator.
-   *  @param  comp    A comparison functor.
-   *  @return  Nothing.
-   *
-   *  Sorts the smallest @p (middle-first) elements in the range
-   *  @p [first,last) and moves them to the range @p [first,middle). The
-   *  order of the remaining elements in the range @p [middle,last) is
-   *  undefined.
-   *  After the sort if @p i and @j are iterators in the range
-   *  @p [first,middle) such that @i precedes @j and @k is an iterator in
-   *  the range @p [middle,last) then @p *comp(j,*i) and @p comp(*k,*i)
-   *  are both false.
+   *  @if maint
+   *  This is a helper function for the stable sorting routines.
+   *  @endif
   */
   template<typename _RandomAccessIterator, typename _Compare>
     void
-    partial_sort(_RandomAccessIterator __first,
-                _RandomAccessIterator __middle,
-                _RandomAccessIterator __last,
-                _Compare __comp)
+    __inplace_stable_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>)
-
-      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);
+      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);
     }
 
   /**
-   *  @brief Copy the smallest elements of a sequence.
-   *  @param  first   An iterator.
-   *  @param  last    Another iterator.
-   *  @param  result_first   A random-access iterator.
-   *  @param  result_last    Another random-access iterator.
-   *  @return   An iterator indicating the end of the resulting sequence.
+   *  @brief Merges two sorted ranges.
+   *  @param  first1  An iterator.
+   *  @param  first2  Another iterator.
+   *  @param  last1   Another iterator.
+   *  @param  last2   Another iterator.
+   *  @param  result  An iterator pointing to the end of the merged range.
+   *  @return  An iterator pointing to the first element "not less than" @a val.
    *
-   *  Copies and sorts the smallest N values from the range @p [first,last)
-   *  to the range beginning at @p result_first, where the number of
-   *  elements to be copied, @p N, is the smaller of @p (last-first) and
-   *  @p (result_last-result_first).
-   *  After the sort if @p i and @j are iterators in the range
-   *  @p [result_first,result_first+N) such that @i precedes @j then
-   *  @p *j<*i is false.
-   *  The value returned is @p result_first+N.
+   *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
+   *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
+   *  must be sorted, and the output range must not overlap with either of
+   *  the input ranges.  The sort is @e stable, that is, for equivalent
+   *  elements in the two ranges, elements from the first range will always
+   *  come before elements from the second.
   */
-  template<typename _InputIterator, typename _RandomAccessIterator>
-    _RandomAccessIterator
-    partial_sort_copy(_InputIterator __first, _InputIterator __last,
-                     _RandomAccessIterator __result_first,
-                     _RandomAccessIterator __result_last)
+  template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
+    _OutputIterator
+    merge(_InputIterator1 __first1, _InputIterator1 __last1,
+         _InputIterator2 __first2, _InputIterator2 __last2,
+         _OutputIterator __result)
     {
-      typedef typename iterator_traits<_InputIterator>::value_type _InputValueType;
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _OutputValueType;
-      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
-
       // concept requirements
-      __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcpp_function_requires(_ConvertibleConcept<_InputValueType, _OutputValueType>)
-      __glibcpp_function_requires(_LessThanComparableConcept<_OutputValueType>)
-      __glibcpp_function_requires(_LessThanComparableConcept<_InputValueType>)
+      __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
+           typename iterator_traits<_InputIterator1>::value_type>)
+      __glibcpp_function_requires(_SameTypeConcept<
+           typename iterator_traits<_InputIterator1>::value_type,
+           typename iterator_traits<_InputIterator2>::value_type>)
+      __glibcpp_function_requires(_LessThanComparableConcept<
+           typename iterator_traits<_InputIterator1>::value_type>)
 
-      if (__result_first == __result_last) return __result_last;
-      _RandomAccessIterator __result_real_last = __result_first;
-      while(__first != __last && __result_real_last != __result_last) {
-       *__result_real_last = *__first;
-       ++__result_real_last;
-       ++__first;
-      }
-      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));
-       ++__first;
+      while (__first1 != __last1 && __first2 != __last2) {
+       if (*__first2 < *__first1) {
+         *__result = *__first2;
+         ++__first2;
+       }
+       else {
+         *__result = *__first1;
+         ++__first1;
+       }
+       ++__result;
       }
-      sort_heap(__result_first, __result_real_last);
-      return __result_real_last;
+      return std::copy(__first2, __last2, std::copy(__first1, __last1, __result));
     }
 
   /**
-   *  @brief Copy the smallest elements of a sequence using a predicate for
-   *         comparison.
-   *  @param  first   An input iterator.
-   *  @param  last    Another input iterator.
-   *  @param  result_first   A random-access iterator.
-   *  @param  result_last    Another random-access iterator.
-   *  @param  comp    A comparison functor.
-   *  @return   An iterator indicating the end of the resulting sequence.
+   *  @brief Merges two sorted ranges.
+   *  @param  first1  An iterator.
+   *  @param  first2  Another iterator.
+   *  @param  last1   Another iterator.
+   *  @param  last2   Another iterator.
+   *  @param  result  An iterator pointing to the end of the merged range.
+   *  @param  comp    A functor to use for comparisons.
+   *  @return  An iterator pointing to the first element "not less than" @a val.
    *
-   *  Copies and sorts the smallest N values from the range @p [first,last)
-   *  to the range beginning at @p result_first, where the number of
-   *  elements to be copied, @p N, is the smaller of @p (last-first) and
-   *  @p (result_last-result_first).
-   *  After the sort if @p i and @j are iterators in the range
-   *  @p [result_first,result_first+N) such that @i precedes @j then
-   *  @p comp(*j,*i) is false.
-   *  The value returned is @p result_first+N.
+   *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
+   *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
+   *  must be sorted, and the output range must not overlap with either of
+   *  the input ranges.  The sort is @e stable, that is, for equivalent
+   *  elements in the two ranges, elements from the first range will always
+   *  come before elements from the second.
+   *
+   *  The comparison function should have the same effects on ordering as
+   *  the function used for the initial sort.
   */
-  template<typename _InputIterator, typename _RandomAccessIterator, typename _Compare>
-    _RandomAccessIterator
-    partial_sort_copy(_InputIterator __first, _InputIterator __last,
-                     _RandomAccessIterator __result_first,
-                     _RandomAccessIterator __result_last,
-                     _Compare __comp)
+  template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
+          typename _Compare>
+    _OutputIterator
+    merge(_InputIterator1 __first1, _InputIterator1 __last1,
+         _InputIterator2 __first2, _InputIterator2 __last2,
+         _OutputIterator __result, _Compare __comp)
     {
-      typedef typename iterator_traits<_InputIterator>::value_type _InputValueType;
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _OutputValueType;
-      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
-
       // concept requirements
-      __glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>)
-      __glibcpp_function_requires(_ConvertibleConcept<_InputValueType, _OutputValueType>)
+      __glibcpp_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcpp_function_requires(_SameTypeConcept<
+           typename iterator_traits<_InputIterator1>::value_type,
+           typename iterator_traits<_InputIterator2>::value_type>)
+      __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
+           typename iterator_traits<_InputIterator1>::value_type>)
       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
-                                 _OutputValueType, _OutputValueType>)
+           typename iterator_traits<_InputIterator1>::value_type,
+           typename iterator_traits<_InputIterator2>::value_type>)
 
-      if (__result_first == __result_last) return __result_last;
-      _RandomAccessIterator __result_real_last = __result_first;
-      while(__first != __last && __result_real_last != __result_last) {
-       *__result_real_last = *__first;
-       ++__result_real_last;
-       ++__first;
-      }
-      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);
-       ++__first;
+      while (__first1 != __last1 && __first2 != __last2) {
+       if (__comp(*__first2, *__first1)) {
+         *__result = *__first2;
+         ++__first2;
+       }
+       else {
+         *__result = *__first1;
+         ++__first1;
+       }
+       ++__result;
       }
-      sort_heap(__result_first, __result_real_last, __comp);
-      return __result_real_last;
+      return std::copy(__first2, __last2, std::copy(__first1, __last1, __result));
     }
 
-  /**
-   *  @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<typename _RandomAccessIterator>
+  template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
+          typename _Distance>
     void
-    nth_element(_RandomAccessIterator __first,
-               _RandomAccessIterator __nth,
-               _RandomAccessIterator __last)
+    __merge_sort_loop(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last,
+                     _RandomAccessIterator2 __result, _Distance __step_size)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
-
-      // concept requirements
-      __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>)
-      __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
+      _Distance __two_step = 2 * __step_size;
 
-      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;
+      while (__last - __first >= __two_step) {
+       __result = std::merge(__first, __first + __step_size,
+                             __first + __step_size, __first + __two_step,
+                             __result);
+       __first += __two_step;
       }
-      __insertion_sort(__first, __last);
+
+      __step_size = std::min(_Distance(__last - __first), __step_size);
+      std::merge(__first, __first + __step_size, __first + __step_size, __last,
+                __result);
     }
 
-  /**
-   *  @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<typename _RandomAccessIterator, typename _Compare>
+  template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
+          typename _Distance, typename _Compare>
     void
-    nth_element(_RandomAccessIterator __first,
-               _RandomAccessIterator __nth,
-               _RandomAccessIterator __last,
-                           _Compare __comp)
+    __merge_sort_loop(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last,
+                     _RandomAccessIterator2 __result, _Distance __step_size,
+                     _Compare __comp)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
+      _Distance __two_step = 2 * __step_size;
 
-      // concept requirements
-      __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>)
-      __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
-                                 _ValueType, _ValueType>)
+      while (__last - __first >= __two_step) {
+       __result = std::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);
 
-      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;
+      std::merge(__first, __first + __step_size,
+                __first + __step_size, __last,
+                __result,
+                __comp);
+    }
+
+  enum { _S_chunk_size = 7 };
+
+  template<typename _RandomAccessIterator, typename _Distance>
+    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;
       }
-      __insertion_sort(__first, __last, __comp);
+      std::__insertion_sort(__first, __last);
     }
 
+  template<typename _RandomAccessIterator, typename _Distance, typename _Compare>
+    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);
+    }
 
-  /**
-   *  @brief Finds the first position in which @a val could be inserted
-   *         without changing the ordering.
-   *  @param  first   An iterator.
-   *  @param  last    Another iterator.
-   *  @param  val     The search term.
-   *  @return  An iterator pointing to the first element "not less than" @a val,
-   *           or end() if every element is less than @a val.
-   *  @ingroup binarysearch
-  */
-  template<typename _ForwardIterator, typename _Tp>
-    _ForwardIterator
-    lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val)
+  template<typename _RandomAccessIterator, typename _Pointer>
+    void
+    __merge_sort_with_buffer(_RandomAccessIterator __first, _RandomAccessIterator __last,
+                             _Pointer __buffer)
     {
-      typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
-      typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
+      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
 
-      // concept requirements
-      // Note that these are slightly stricter than those of the 4-argument
-      // version, defined next.  The difference is in the strictness of the
-      // comparison operations... so for looser checking, define your own
-      // comparison function, as was intended.
-      __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcpp_function_requires(_SameTypeConcept<_Tp, _ValueType>)
-      __glibcpp_function_requires(_LessThanComparableConcept<_Tp>)
+      _Distance __len = __last - __first;
+      _Pointer __buffer_last = __buffer + __len;
 
-      _DistanceType __len = std::distance(__first, __last);
-      _DistanceType __half;
-      _ForwardIterator __middle;
+      _Distance __step_size = _S_chunk_size;
+      std::__chunk_insertion_sort(__first, __last, __step_size);
 
-      while (__len > 0) {
-       __half = __len >> 1;
-       __middle = __first;
-       advance(__middle, __half);
-       if (*__middle < __val) {
-         __first = __middle;
-         ++__first;
-         __len = __len - __half - 1;
-       }
-       else
-         __len = __half;
+      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;
       }
-      return __first;
     }
 
-  /**
-   *  @brief Finds the first position in which @a val could be inserted
-   *         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 iterator pointing to the first element "not less than" @a val,
-   *           or end() if every element is less than @a val.
-   *  @ingroup binarysearch
-   *
-   *  The comparison function should have the same effects on ordering as
-   *  the function used for the initial sort.
-  */
-  template<typename _ForwardIterator, typename _Tp, typename _Compare>
-    _ForwardIterator
-    lower_bound(_ForwardIterator __first, _ForwardIterator __last,
-               const _Tp& __val, _Compare __comp)
+  template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
+    void
+    __merge_sort_with_buffer(_RandomAccessIterator __first, _RandomAccessIterator __last,
+                             _Pointer __buffer, _Compare __comp)
     {
-      typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
-      typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
+      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
 
-      // concept requirements
-      __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _Tp>)
+      _Distance __len = __last - __first;
+      _Pointer __buffer_last = __buffer + __len;
 
-      _DistanceType __len = std::distance(__first, __last);
-      _DistanceType __half;
-      _ForwardIterator __middle;
+      _Distance __step_size = _S_chunk_size;
+      std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
 
-      while (__len > 0) {
-       __half = __len >> 1;
-       __middle = __first;
-       advance(__middle, __half);
-       if (__comp(*__middle, __val)) {
-         __first = __middle;
-         ++__first;
-         __len = __len - __half - 1;
-       }
-       else
-         __len = __half;
+      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;
       }
-      return __first;
     }
 
   /**
-   *  @brief Finds the last position in which @a val could be inserted
-   *         without changing the ordering.
-   *  @param  first   An iterator.
-   *  @param  last    Another iterator.
-   *  @param  val     The search term.
-   *  @return  An iterator pointing to the first element greater than @a val,
-   *           or end() if no elements are greater than @a val.
-   *  @ingroup binarysearch
+   *  @if maint
+   *  This is a helper function for the merge routines.
+   *  @endif
   */
-  template<typename _ForwardIterator, typename _Tp>
-    _ForwardIterator
-    upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val)
+  template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
+          typename _BidirectionalIterator3>
+    _BidirectionalIterator3
+    __merge_backward(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
+                    _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2,
+                    _BidirectionalIterator3 __result)
     {
-      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;
-
-      while (__len > 0) {
-       __half = __len >> 1;
-       __middle = __first;
-       advance(__middle, __half);
-       if (__val < *__middle)
-         __len = __half;
+      if (__first1 == __last1)
+       return std::copy_backward(__first2, __last2, __result);
+      if (__first2 == __last2)
+       return std::copy_backward(__first1, __last1, __result);
+      --__last1;
+      --__last2;
+      while (true) {
+       if (*__last2 < *__last1) {
+         *--__result = *__last1;
+         if (__first1 == __last1)
+           return std::copy_backward(__first2, ++__last2, __result);
+         --__last1;
+       }
        else {
-         __first = __middle;
-         ++__first;
-         __len = __len - __half - 1;
+         *--__result = *__last2;
+         if (__first2 == __last2)
+           return std::copy_backward(__first1, ++__last1, __result);
+         --__last2;
        }
       }
-      return __first;
     }
 
   /**
-   *  @brief Finds the last position in which @a val could be inserted
-   *         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 iterator pointing to the first element greater than @a val,
-   *           or end() if no elements are greater than @a val.
-   *  @ingroup binarysearch
-   *
-   *  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 merge routines.
+   *  @endif
   */
-  template<typename _ForwardIterator, typename _Tp, typename _Compare>
-    _ForwardIterator
-    upper_bound(_ForwardIterator __first, _ForwardIterator __last,
-               const _Tp& __val, _Compare __comp)
+  template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
+          typename _BidirectionalIterator3, typename _Compare>
+    _BidirectionalIterator3
+    __merge_backward(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
+                    _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2,
+                    _BidirectionalIterator3 __result,
+                    _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, _Tp, _ValueType>)
-
-      _DistanceType __len = std::distance(__first, __last);
-      _DistanceType __half;
-      _ForwardIterator __middle;
-
-      while (__len > 0) {
-       __half = __len >> 1;
-       __middle = __first;
-       advance(__middle, __half);
-       if (__comp(__val, *__middle))
-         __len = __half;
+      if (__first1 == __last1)
+       return std::copy_backward(__first2, __last2, __result);
+      if (__first2 == __last2)
+       return std::copy_backward(__first1, __last1, __result);
+      --__last1;
+      --__last2;
+      while (true) {
+       if (__comp(*__last2, *__last1)) {
+         *--__result = *__last1;
+         if (__first1 == __last1)
+           return std::copy_backward(__first2, ++__last2, __result);
+         --__last1;
+       }
        else {
-         __first = __middle;
-         ++__first;
-         __len = __len - __half - 1;
+         *--__result = *__last2;
+         if (__first2 == __last2)
+           return std::copy_backward(__first1, ++__last1, __result);
+         --__last2;
        }
       }
-      return __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.
-   *  @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<typename _ForwardIterator, typename _Tp>
-    pair<_ForwardIterator, _ForwardIterator>
-    equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val)
+  template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
+          typename _Distance>
+    _BidirectionalIterator1
+    __rotate_adaptive(_BidirectionalIterator1 __first,
+                     _BidirectionalIterator1 __middle,
+                     _BidirectionalIterator1 __last,
+                     _Distance __len1, _Distance __len2,
+                     _BidirectionalIterator2 __buffer,
+                     _Distance __buffer_size)
     {
-      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);
-       }
+      _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);
       }
-      return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
+      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.
+   *  @endif
+  */
+  template<typename _BidirectionalIterator, typename _Distance, typename _Pointer>
+    void
+    __merge_adaptive(_BidirectionalIterator __first,
+                     _BidirectionalIterator __middle,
+                    _BidirectionalIterator __last,
+                    _Distance __len1, _Distance __len2,
+                    _Pointer __buffer, _Distance __buffer_size)
+    {
+         if (__len1 <= __len2 && __len1 <= __buffer_size) {
+           _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
+           std::merge(__buffer, __buffer_end, __middle, __last, __first);
+         }
+         else if (__len2 <= __buffer_size) {
+           _Pointer __buffer_end = std::copy(__middle, __last, __buffer);
+           std::__merge_backward(__first, __middle, __buffer, __buffer_end, __last);
+         }
+         else {
+           _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);
+           }
+           else {
+                 __len22 = __len2 / 2;
+                 std::advance(__second_cut, __len22);
+                 __first_cut = std::upper_bound(__first, __middle, *__second_cut);
+                 __len11 = std::distance(__first, __first_cut);
+           }
+           _BidirectionalIterator __new_middle =
+                 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);
+         }
     }
 
   /**
-   *  @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.
+   *  @if maint
+   *  This is a helper function for the merge routines.
+   *  @endif
   */
-  template<typename _ForwardIterator, typename _Tp, typename _Compare>
-    pair<_ForwardIterator, _ForwardIterator>
-    equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val,
-               _Compare __comp)
+  template<typename _BidirectionalIterator, typename _Distance, typename _Pointer,
+          typename _Compare>
+    void
+    __merge_adaptive(_BidirectionalIterator __first,
+                     _BidirectionalIterator __middle,
+                    _BidirectionalIterator __last,
+                    _Distance __len1, _Distance __len2,
+                    _Pointer __buffer, _Distance __buffer_size,
+                    _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);
-       }
-      }
-      return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
+         if (__len1 <= __len2 && __len1 <= __buffer_size) {
+           _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 = std::copy(__middle, __last, __buffer);
+           std::__merge_backward(__first, __middle, __buffer, __buffer_end, __last,
+                                 __comp);
+         }
+         else {
+           _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);
+           }
+           _BidirectionalIterator __new_middle =
+                 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);
+         }
     }
 
   /**
-   *  @brief Determines whether an element exists in a range.
+   *  @brief Merges two sorted ranges in place.
    *  @param  first   An iterator.
+   *  @param  middle  Another 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
+   *  @return  Nothing.
    *
-   *  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.
+   *  Merges two sorted and consecutive ranges, [first,middle) and
+   *  [middle,last), and puts the result in [first,last).  The output will
+   *  be sorted.  The sort is @e stable, that is, for equivalent
+   *  elements in the two ranges, elements from the first range will always
+   *  come before elements from the second.
+   *
+   *  If enough additional memory is available, this takes (last-first)-1
+   *  comparisons.  Otherwise an NlogN algorithm is used, where N is
+   *  distance(first,last).
   */
-  template<typename _ForwardIterator, typename _Tp>
-    bool
-    binary_search(_ForwardIterator __first, _ForwardIterator __last,
-                  const _Tp& __val)
+  template<typename _BidirectionalIterator>
+    void
+    inplace_merge(_BidirectionalIterator __first,
+                 _BidirectionalIterator __middle,
+                 _BidirectionalIterator __last)
     {
+      typedef typename iterator_traits<_BidirectionalIterator>::value_type
+          _ValueType;
+      typedef typename iterator_traits<_BidirectionalIterator>::difference_type
+          _DistanceType;
+
       // 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>)
+      __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<
+           _BidirectionalIterator>)
+      __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
 
-      _ForwardIterator __i = lower_bound(__first, __last, __val);
-      return __i != __last && !(__val < *__i);
+      if (__first == __middle || __middle == __last)
+       return;
+
+      _DistanceType __len1 = std::distance(__first, __middle);
+      _DistanceType __len2 = std::distance(__middle, __last);
+
+      _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first, __last);
+      if (__buf.begin() == 0)
+       std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
+      else
+       std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
+                             __buf.begin(), _DistanceType(__buf.size()));
     }
 
   /**
-   *  @brief Determines whether an element exists in a range.
+   *  @brief Merges two sorted ranges in place.
    *  @param  first   An iterator.
+   *  @param  middle  Another 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
+   *  @return  Nothing.
    *
-   *  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.
+   *  Merges two sorted and consecutive ranges, [first,middle) and
+   *  [middle,last), and puts the result in [first,last).  The output will
+   *  be sorted.  The sort is @e stable, that is, for equivalent
+   *  elements in the two ranges, elements from the first range will always
+   *  come before elements from the second.
+   *
+   *  If enough additional memory is available, this takes (last-first)-1
+   *  comparisons.  Otherwise an NlogN algorithm is used, where N is
+   *  distance(first,last).
    *
    *  The comparison function should have the same effects on ordering as
    *  the function used for the initial sort.
   */
-  template<typename _ForwardIterator, typename _Tp, typename _Compare>
-    bool
-    binary_search(_ForwardIterator __first, _ForwardIterator __last,
-                  const _Tp& __val, _Compare __comp)
+  template<typename _BidirectionalIterator, typename _Compare>
+    void
+    inplace_merge(_BidirectionalIterator __first,
+                 _BidirectionalIterator __middle,
+                 _BidirectionalIterator __last,
+                 _Compare __comp)
     {
+      typedef typename iterator_traits<_BidirectionalIterator>::value_type
+          _ValueType;
+      typedef typename iterator_traits<_BidirectionalIterator>::difference_type
+          _DistanceType;
+
       // concept requirements
-      __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<
+           _BidirectionalIterator>)
       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
-               typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
-      __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp,
-               typename iterator_traits<_ForwardIterator>::value_type>)
+           _ValueType, _ValueType>)
 
-      _ForwardIterator __i = lower_bound(__first, __last, __val, __comp);
-      return __i != __last && !__comp(__val, *__i);
+      if (__first == __middle || __middle == __last)
+       return;
+
+      _DistanceType __len1 = std::distance(__first, __middle);
+      _DistanceType __len2 = std::distance(__middle, __last);
+
+      _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first, __last);
+      if (__buf.begin() == 0)
+       std::__merge_without_buffer(__first, __middle, __last, __len1, __len2, __comp);
+      else
+       std::__merge_adaptive(__first, __middle, __last, __len1, __len2,
+                             __buf.begin(), _DistanceType(__buf.size()),
+                             __comp);
+    }
+
+  template<typename _RandomAccessIterator, typename _Pointer, typename _Distance>
+    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<typename _RandomAccessIterator, typename _Pointer, typename _Distance,
+           typename _Compare>
+    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 Merges two sorted ranges.
-   *  @param  first1  An iterator.
-   *  @param  first2  Another iterator.
-   *  @param  last1   Another iterator.
-   *  @param  last2   Another iterator.
-   *  @param  result  An iterator pointing to the end of the merged range.
-   *  @return  An iterator pointing to the first element "not less than" @a val.
+   *  @brief Sort the elements of a sequence, preserving the relative order
+   *         of equivalent elements.
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @return  Nothing.
    *
-   *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
-   *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
-   *  must be sorted, and the output range must not overlap with either of
-   *  the input ranges.  The sort is @e stable, that is, for equivalent
-   *  elements in the two ranges, elements from the first range will always
-   *  come before elements from the second.
+   *  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<y is false and @p y<x is false will have the same relative
+   *  ordering after calling @p stable_sort().
   */
-  template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
-    _OutputIterator
-    merge(_InputIterator1 __first1, _InputIterator1 __last1,
-         _InputIterator2 __first2, _InputIterator2 __last2,
-         _OutputIterator __result)
+  template<typename _RandomAccessIterator>
+    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(_InputIteratorConcept<_InputIterator1>)
-      __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcpp_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcpp_function_requires(_LessThanComparableConcept<
-           typename iterator_traits<_InputIterator1>::value_type>)
+      __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
+      __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
 
-      while (__first1 != __last1 && __first2 != __last2) {
-       if (*__first2 < *__first1) {
-         *__result = *__first2;
-         ++__first2;
-       }
-       else {
-         *__result = *__first1;
-         ++__first1;
-       }
-       ++__result;
-      }
-      return copy(__first2, __last2, copy(__first1, __last1, __result));
+      _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 Merges two sorted ranges.
-   *  @param  first1  An iterator.
-   *  @param  first2  Another iterator.
-   *  @param  last1   Another iterator.
-   *  @param  last2   Another iterator.
-   *  @param  result  An iterator pointing to the end of the merged range.
-   *  @param  comp    A functor to use for comparisons.
-   *  @return  An iterator pointing to the first element "not less than" @a val.
+   *  @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.
    *
-   *  Merges the ranges [first1,last1) and [first2,last2) into the sorted range
-   *  [result, result + (last1-first1) + (last2-first2)).  Both input ranges
-   *  must be sorted, and the output range must not overlap with either of
-   *  the input ranges.  The sort is @e stable, that is, for equivalent
-   *  elements in the two ranges, elements from the first range will always
-   *  come before elements from the second.
+   *  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 comparison function should have the same effects on ordering as
-   *  the function used for the initial sort.
+   *  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<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
-          typename _Compare>
-    _OutputIterator
-    merge(_InputIterator1 __first1, _InputIterator1 __last1,
-         _InputIterator2 __first2, _InputIterator2 __last2,
-         _OutputIterator __result, _Compare __comp)
+  template<typename _RandomAccessIterator, typename _Compare>
+    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(_InputIteratorConcept<_InputIterator1>)
-      __glibcpp_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcpp_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcpp_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator1>::value_type>)
+      __glibcpp_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
+                                                         _ValueType, _ValueType>)
 
-      while (__first1 != __last1 && __first2 != __last2) {
-       if (__comp(*__first2, *__first1)) {
-         *__result = *__first2;
-         ++__first2;
-       }
-       else {
-         *__result = *__first1;
-         ++__first1;
-       }
-       ++__result;
-      }
-      return copy(__first2, __last2, copy(__first1, __last1, __result));
+      _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);
     }
 
   /**
-   *  @if maint
-   *  This is a helper function for the merge routines.
-   *  @endif
+   *  @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<typename _BidirectionalIterator, typename _Distance>
+  template<typename _RandomAccessIterator>
     void
-    __merge_without_buffer(_BidirectionalIterator __first,
-                          _BidirectionalIterator __middle,
-                          _BidirectionalIterator __last,
-                          _Distance __len1, _Distance __len2)
+    nth_element(_RandomAccessIterator __first,
+               _RandomAccessIterator __nth,
+               _RandomAccessIterator __last)
     {
-      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);
+      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;
       }
-      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);
+      std::__insertion_sort(__first, __last);
     }
 
   /**
-   *  @if maint
-   *  This is a helper function for the merge routines.
-   *  @endif
+   *  @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<typename _BidirectionalIterator, typename _Distance, typename _Compare>
+  template<typename _RandomAccessIterator, typename _Compare>
     void
-    __merge_without_buffer(_BidirectionalIterator __first,
-                           _BidirectionalIterator __middle,
-                          _BidirectionalIterator __last,
-                          _Distance __len1, _Distance __len2,
-                          _Compare __comp)
+    nth_element(_RandomAccessIterator __first,
+               _RandomAccessIterator __nth,
+               _RandomAccessIterator __last,
+                           _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);
-    }
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
 
-  /**
-   *  @if maint
-   *  This is a helper function for the merge routines.
-   *  @endif
-  */
-  template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
-          typename _Distance>
-    _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 = copy(__middle, __last, __buffer);
-       copy_backward(__first, __middle, __last);
-       return copy(__buffer, __buffer_end, __first);
-      }
-      else if (__len1 <= __buffer_size) {
-       __buffer_end = copy(__first, __middle, __buffer);
-       copy(__middle, __last, __first);
-       return copy_backward(__buffer, __buffer_end, __last);
-      }
-      else {
-       rotate(__first, __middle, __last);
-       advance(__first, std::distance(__middle, __last));
-       return __first;
+      // 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);
     }
 
   /**
-   *  @if maint
-   *  This is a helper function for the merge routines.
-   *  @endif
+   *  @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<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
-          typename _BidirectionalIterator3>
-    _BidirectionalIterator3
-    __merge_backward(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
-                    _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2,
-                    _BidirectionalIterator3 __result)
+  template<typename _ForwardIterator, typename _Tp>
+    pair<_ForwardIterator, _ForwardIterator>
+    equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val)
     {
-      if (__first1 == __last1)
-       return copy_backward(__first2, __last2, __result);
-      if (__first2 == __last2)
-       return copy_backward(__first1, __last1, __result);
-      --__last1;
-      --__last2;
-      while (true) {
-       if (*__last2 < *__last1) {
-         *--__result = *__last1;
-         if (__first1 == __last1)
-           return copy_backward(__first2, ++__last2, __result);
-         --__last1;
+      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 {
-         *--__result = *__last2;
-         if (__first2 == __last2)
-           return copy_backward(__first1, ++__last1, __result);
-         --__last2;
+         __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);
     }
 
   /**
-   *  @if maint
-   *  This is a helper function for the merge routines.
-   *  @endif
+   *  @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<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
-          typename _BidirectionalIterator3, typename _Compare>
-    _BidirectionalIterator3
-    __merge_backward(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
-                    _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2,
-                    _BidirectionalIterator3 __result,
-                    _Compare __comp)
+  template<typename _ForwardIterator, typename _Tp, typename _Compare>
+    pair<_ForwardIterator, _ForwardIterator>
+    equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val,
+               _Compare __comp)
     {
-      if (__first1 == __last1)
-       return copy_backward(__first2, __last2, __result);
-      if (__first2 == __last2)
-       return copy_backward(__first1, __last1, __result);
-      --__last1;
-      --__last2;
-      while (true) {
-       if (__comp(*__last2, *__last1)) {
-         *--__result = *__last1;
-         if (__first1 == __last1)
-           return copy_backward(__first2, ++__last2, __result);
-         --__last1;
+      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 {
-         *--__result = *__last2;
-         if (__first2 == __last2)
-           return copy_backward(__first1, ++__last1, __result);
-         --__last2;
+         __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);
     }
 
   /**
-   *  @if maint
-   *  This is a helper function for the merge routines.
-   *  @endif
-  */
-  template<typename _BidirectionalIterator, typename _Distance, typename _Pointer>
-    void
-    __merge_adaptive(_BidirectionalIterator __first,
-                     _BidirectionalIterator __middle,
-                    _BidirectionalIterator __last,
-                    _Distance __len1, _Distance __len2,
-                    _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);
-         }
-         else if (__len2 <= __buffer_size) {
-           _Pointer __buffer_end = copy(__middle, __last, __buffer);
-           __merge_backward(__first, __middle, __buffer, __buffer_end, __last);
-         }
-         else {
-           _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);
-           }
-           _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);
-         }
-    }
-
-  /**
-   *  @if maint
-   *  This is a helper function for the merge routines.
-   *  @endif
-  */
-  template<typename _BidirectionalIterator, typename _Distance, typename _Pointer,
-          typename _Compare>
-    void
-    __merge_adaptive(_BidirectionalIterator __first,
-                     _BidirectionalIterator __middle,
-                    _BidirectionalIterator __last,
-                    _Distance __len1, _Distance __len2,
-                    _Pointer __buffer, _Distance __buffer_size,
-                    _Compare __comp)
-    {
-         if (__len1 <= __len2 && __len1 <= __buffer_size) {
-           _Pointer __buffer_end = copy(__first, __middle, __buffer);
-           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);
-         }
-         else {
-           _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);
-           }
-           _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);
-         }
-    }
-
-  /**
-   *  @brief Merges two sorted ranges in place.
+   *  @brief Determines whether an element exists in a range.
    *  @param  first   An iterator.
-   *  @param  middle  Another iterator.
    *  @param  last    Another iterator.
-   *  @return  Nothing.
-   *
-   *  Merges two sorted and consecutive ranges, [first,middle) and
-   *  [middle,last), and puts the result in [first,last).  The output will
-   *  be sorted.  The sort is @e stable, that is, for equivalent
-   *  elements in the two ranges, elements from the first range will always
-   *  come before elements from the second.
+   *  @param  val     The search term.
+   *  @return  True if @a val (or its equivelent) is in [@a first,@a last ].
+   *  @ingroup binarysearch
    *
-   *  If enough additional memory is available, this takes (last-first)-1
-   *  comparisons.  Otherwise an NlogN algorithm is used, where N is
-   *  distance(first,last).
+   *  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<typename _BidirectionalIterator>
-    void
-    inplace_merge(_BidirectionalIterator __first,
-                 _BidirectionalIterator __middle,
-                 _BidirectionalIterator __last)
+  template<typename _ForwardIterator, typename _Tp>
+    bool
+    binary_search(_ForwardIterator __first, _ForwardIterator __last,
+                  const _Tp& __val)
     {
-      typedef typename iterator_traits<_BidirectionalIterator>::value_type
-          _ValueType;
-      typedef typename iterator_traits<_BidirectionalIterator>::difference_type
-          _DistanceType;
-
       // concept requirements
-      __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<
-           _BidirectionalIterator>)
-      __glibcpp_function_requires(_LessThanComparableConcept<_ValueType>)
-
-      if (__first == __middle || __middle == __last)
-       return;
-
-      _DistanceType __len1 = std::distance(__first, __middle);
-      _DistanceType __len2 = std::distance(__middle, __last);
+      // 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>)
 
-      _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first, __last);
-      if (__buf.begin() == 0)
-       __merge_without_buffer(__first, __middle, __last, __len1, __len2);
-      else
-       __merge_adaptive(__first, __middle, __last, __len1, __len2,
-                        __buf.begin(), _DistanceType(__buf.size()));
+      _ForwardIterator __i = std::lower_bound(__first, __last, __val);
+      return __i != __last && !(__val < *__i);
     }
 
   /**
-   *  @brief Merges two sorted ranges in place.
+   *  @brief Determines whether an element exists in a range.
    *  @param  first   An iterator.
-   *  @param  middle  Another iterator.
    *  @param  last    Another iterator.
+   *  @param  val     The search term.
    *  @param  comp    A functor to use for comparisons.
-   *  @return  Nothing.
-   *
-   *  Merges two sorted and consecutive ranges, [first,middle) and
-   *  [middle,last), and puts the result in [first,last).  The output will
-   *  be sorted.  The sort is @e stable, that is, for equivalent
-   *  elements in the two ranges, elements from the first range will always
-   *  come before elements from the second.
+   *  @return  True if @a val (or its equivelent) is in [@a first,@a last ].
+   *  @ingroup binarysearch
    *
-   *  If enough additional memory is available, this takes (last-first)-1
-   *  comparisons.  Otherwise an NlogN algorithm is used, where N is
-   *  distance(first,last).
+   *  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<typename _BidirectionalIterator, typename _Compare>
-    void
-    inplace_merge(_BidirectionalIterator __first,
-                 _BidirectionalIterator __middle,
-                 _BidirectionalIterator __last,
-                 _Compare __comp)
+  template<typename _ForwardIterator, typename _Tp, typename _Compare>
+    bool
+    binary_search(_ForwardIterator __first, _ForwardIterator __last,
+                  const _Tp& __val, _Compare __comp)
     {
-      typedef typename iterator_traits<_BidirectionalIterator>::value_type
-          _ValueType;
-      typedef typename iterator_traits<_BidirectionalIterator>::difference_type
-          _DistanceType;
-
       // concept requirements
-      __glibcpp_function_requires(_Mutable_BidirectionalIteratorConcept<
-           _BidirectionalIterator>)
+      __glibcpp_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
       __glibcpp_function_requires(_BinaryPredicateConcept<_Compare,
-           _ValueType, _ValueType>)
-
-      if (__first == __middle || __middle == __last)
-       return;
-
-      _DistanceType __len1 = std::distance(__first, __middle);
-      _DistanceType __len2 = std::distance(__middle, __last);
+               typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
+      __glibcpp_function_requires(_BinaryPredicateConcept<_Compare, _Tp,
+               typename iterator_traits<_ForwardIterator>::value_type>)
 
-      _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first, __last);
-      if (__buf.begin() == 0)
-       __merge_without_buffer(__first, __middle, __last, __len1, __len2, __comp);
-      else
-       __merge_adaptive(__first, __middle, __last, __len1, __len2,
-                        __buf.begin(), _DistanceType(__buf.size()),
-                        __comp);
+      _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<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
@@ -3720,7 +3715,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<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
@@ -3816,7 +3811,7 @@ __result, __binary_pred, _IterType());
          ++__first1;
          ++__first2;
        }
-      return copy(__first1, __last1, __result);
+      return std::copy(__first1, __last1, __result);
     }
 
   template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
@@ -3850,7 +3845,7 @@ __result, __binary_pred, _IterType());
          ++__first1;
          ++__first2;
        }
-      return copy(__first1, __last1, __result);
+      return std::copy(__first1, __last1, __result);
     }
 
   template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
@@ -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<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
@@ -3923,7 +3918,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));
     }
 
   // min_element and max_element, with and without an explicitly supplied
@@ -4028,12 +4023,12 @@ __result, __binary_pred, _IterType());
          _BidirectionalIterator __j = __last;
          while (!(*__i < *--__j))
            {}
-         iter_swap(__i, __j);
-         reverse(__ii, __last);
+         std::iter_swap(__i, __j);
+         std::reverse(__ii, __last);
          return true;
        }
        if (__i == __first) {
-         reverse(__first, __last);
+         std::reverse(__first, __last);
          return false;
        }
       }
@@ -4066,12 +4061,12 @@ __result, __binary_pred, _IterType());
          _BidirectionalIterator __j = __last;
          while (!__comp(*__i, *--__j))
            {}
-         iter_swap(__i, __j);
-         reverse(__ii, __last);
+         std::iter_swap(__i, __j);
+         std::reverse(__ii, __last);
          return true;
        }
        if (__i == __first) {
-         reverse(__first, __last);
+         std::reverse(__first, __last);
          return false;
        }
       }
@@ -4102,12 +4097,12 @@ __result, __binary_pred, _IterType());
          _BidirectionalIterator __j = __last;
          while (!(*--__j < *__i))
            {}
-         iter_swap(__i, __j);
-         reverse(__ii, __last);
+         std::iter_swap(__i, __j);
+         std::reverse(__ii, __last);
          return true;
        }
        if (__i == __first) {
-         reverse(__first, __last);
+         std::reverse(__first, __last);
          return false;
        }
       }
@@ -4140,12 +4135,12 @@ __result, __binary_pred, _IterType());
          _BidirectionalIterator __j = __last;
          while (!__comp(*--__j, *__i))
            {}
-         iter_swap(__i, __j);
-         reverse(__ii, __last);
+         std::iter_swap(__i, __j);
+         std::reverse(__ii, __last);
          return true;
        }
        if (__i == __first) {
-         reverse(__first, __last);
+         std::reverse(__first, __last);
          return false;
        }
       }
@@ -4214,7 +4209,7 @@ __result, __binary_pred, _IterType());
        _ForwardIterator1 __result = __last1;
        while (1) {
          _ForwardIterator1 __new_result
-           = search(__first1, __last1, __first2, __last2);
+           = std::search(__first1, __last1, __first2, __last2);
          if (__new_result == __last1)
            return __result;
          else {
@@ -4240,7 +4235,7 @@ __result, __binary_pred, _IterType());
        _ForwardIterator1 __result = __last1;
        while (1) {
          _ForwardIterator1 __new_result
-           = search(__first1, __last1, __first2, __last2, __comp);
+           = std::search(__first1, __last1, __first2, __last2, __comp);
          if (__new_result == __last1)
            return __result;
          else {
@@ -4268,14 +4263,14 @@ __result, __binary_pred, _IterType());
 
       _RevIterator1 __rlast1(__first1);
       _RevIterator2 __rlast2(__first2);
-      _RevIterator1 __rresult = search(_RevIterator1(__last1), __rlast1,
-                                  _RevIterator2(__last2), __rlast2);
+      _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
+                                           _RevIterator2(__last2), __rlast2);
 
       if (__rresult == __rlast1)
        return __last1;
       else {
        _BidirectionalIterator1 __result = __rresult.base();
-       advance(__result, -std::distance(__first2, __last2));
+       std::advance(__result, -std::distance(__first2, __last2));
        return __result;
       }
     }
@@ -4297,15 +4292,15 @@ __result, __binary_pred, _IterType());
 
       _RevIterator1 __rlast1(__first1);
       _RevIterator2 __rlast2(__first2);
-      _RevIterator1 __rresult = search(_RevIterator1(__last1), __rlast1,
-                                  _RevIterator2(__last2), __rlast2,
-                                  __comp);
+      _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
+                                           _RevIterator2(__last2), __rlast2,
+                                           __comp);
 
       if (__rresult == __rlast1)
        return __last1;
       else {
        _BidirectionalIterator1 __result = __rresult.base();
-       advance(__result, -std::distance(__first2, __last2));
+       std::advance(__result, -std::distance(__first2, __last2));
        return __result;
       }
     }
@@ -4324,9 +4319,9 @@ __result, __binary_pred, _IterType());
            typename iterator_traits<_ForwardIterator1>::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<typename _ForwardIterator1, typename _ForwardIterator2,
@@ -4343,10 +4338,10 @@ __result, __binary_pred, _IterType());
            typename iterator_traits<_ForwardIterator1>::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