OSDN Git Service

2010-05-21 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_algo.h
index 7cb54eb..fe2edb9 100644 (file)
@@ -1,11 +1,12 @@
 // Algorithm implementation -*- C++ -*-
 
-// Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
+// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
+// Free Software Foundation, Inc.
 //
 // This file is part of the GNU ISO C++ Library.  This library is free
 // software; you can redistribute it and/or modify it under the
 // terms of the GNU General Public License as published by the
-// Free Software Foundation; either version 2, or (at your option)
+// Free Software Foundation; either version 3, or (at your option)
 // any later version.
 
 // This library is distributed in the hope that it will be useful,
 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 // GNU General Public License for more details.
 
-// You should have received a copy of the GNU General Public License along
-// with this library; see the file COPYING.  If not, write to the Free
-// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
-// USA.
+// Under Section 7 of GPL version 3, you are granted additional
+// permissions described in the GCC Runtime Library Exception, version
+// 3.1, as published by the Free Software Foundation.
 
-// As a special exception, you may use this file as part of a free software
-// library without restriction.  Specifically, if other files instantiate
-// templates or use macros or inline functions from this file, or you compile
-// this file and link it with other files to produce an executable, this
-// file does not by itself cause the resulting executable to be covered by
-// the GNU General Public License.  This exception does not however
-// invalidate any other reasons why the executable file might be covered by
-// the GNU General Public License.
+// You should have received a copy of the GNU General Public License and
+// a copy of the GCC Runtime Library Exception along with this program;
+// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
+// <http://www.gnu.org/licenses/>.
 
 /*
  *
  *  You should not attempt to use it directly.
  */
 
-#ifndef _ALGO_H
-#define _ALGO_H 1
+#ifndef _STL_ALGO_H
+#define _STL_ALGO_H 1
 
+#include <cstdlib>             // for rand
+#include <bits/algorithmfwd.h>
 #include <bits/stl_heap.h>
-#include <bits/stl_tempbuf.h>     // for _Temporary_buffer
-#include <debug/debug.h>
+#include <bits/stl_tempbuf.h>  // for _Temporary_buffer
+
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#include <random> // for std::uniform_int_distribution
+#endif
 
 // See concept_check.h for the __glibcxx_*_requires macros.
 
-namespace std
-{
-  /**
-   *  @brief Find the median of three values.
-   *  @param  a  A value.
-   *  @param  b  A value.
-   *  @param  c  A value.
-   *  @return One of @p a, @p b or @p c.
-   *
-   *  If @c {l,m,n} is some convolution of @p {a,b,c} such that @c l<=m<=n
-   *  then the value returned will be @c m.
-   *  This is an SGI extension.
-   *  @ingroup SGIextensions
-  */
-  template<typename _Tp>
-  inline const _Tp&
-    __median(const _Tp& __a, const _Tp& __b, const _Tp& __c)
+_GLIBCXX_BEGIN_NAMESPACE(std)
+
+  /// Swaps the median value of *__a, *__b and *__c to *__a
+  template<typename _Iterator>
+    void
+    __move_median_first(_Iterator __a, _Iterator __b, _Iterator __c)
     {
       // concept requirements
-      __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
-      if (__a < __b)
-       if (__b < __c)
-         return __b;
-       else if (__a < __c)
-         return __c;
-       else
-         return __a;
-      else if (__a < __c)
-       return __a;
-      else if (__b < __c)
-       return __c;
+      __glibcxx_function_requires(_LessThanComparableConcept<
+           typename iterator_traits<_Iterator>::value_type>)
+
+      if (*__a < *__b)
+       {
+         if (*__b < *__c)
+           std::iter_swap(__a, __b);
+         else if (*__a < *__c)
+           std::iter_swap(__a, __c);
+       }
+      else if (*__a < *__c)
+       return;
+      else if (*__b < *__c)
+       std::iter_swap(__a, __c);
       else
-       return __b;
+       std::iter_swap(__a, __b);
     }
 
-  /**
-   *  @brief Find the median of three values using a predicate for comparison.
-   *  @param  a     A value.
-   *  @param  b     A value.
-   *  @param  c     A value.
-   *  @param  comp  A binary predicate.
-   *  @return One of @p a, @p b or @p c.
-   *
-   *  If @c {l,m,n} is some convolution of @p {a,b,c} such that @p comp(l,m)
-   *  and @p comp(m,n) are both true then the value returned will be @c m.
-   *  This is an SGI extension.
-   *  @ingroup SGIextensions
-  */
-  template<typename _Tp, typename _Compare>
-    inline const _Tp&
-    __median(const _Tp& __a, const _Tp& __b, const _Tp& __c, _Compare __comp)
+  /// Swaps the median value of *__a, *__b and *__c under __comp to *__a
+  template<typename _Iterator, typename _Compare>
+    void
+    __move_median_first(_Iterator __a, _Iterator __b, _Iterator __c,
+                       _Compare __comp)
     {
       // concept requirements
-      __glibcxx_function_requires(_BinaryFunctionConcept<_Compare,bool,_Tp,_Tp>)
-      if (__comp(__a, __b))
-       if (__comp(__b, __c))
-         return __b;
-       else if (__comp(__a, __c))
-         return __c;
-       else
-         return __a;
-      else if (__comp(__a, __c))
-       return __a;
-      else if (__comp(__b, __c))
-       return __c;
+      __glibcxx_function_requires(_BinaryFunctionConcept<_Compare, bool,
+           typename iterator_traits<_Iterator>::value_type,
+           typename iterator_traits<_Iterator>::value_type>)
+
+      if (__comp(*__a, *__b))
+       {
+         if (__comp(*__b, *__c))
+           std::iter_swap(__a, __b);
+         else if (__comp(*__a, *__c))
+           std::iter_swap(__a, __c);
+       }
+      else if (__comp(*__a, *__c))
+       return;
+      else if (__comp(*__b, *__c))
+       std::iter_swap(__a, __c);
       else
-       return __b;
+       std::iter_swap(__a, __b);
     }
 
-  /**
-   *  @brief Apply a function to every element of a sequence.
-   *  @param  first  An input iterator.
-   *  @param  last   An input iterator.
-   *  @param  f      A unary function object.
-   *  @return   @p f.
-   *
-   *  Applies the function object @p f to each element in the range
-   *  @p [first,last).  @p f must not modify the order of the sequence.
-   *  If @p f has a return value it is ignored.
-  */
-  template<typename _InputIterator, typename _Function>
-    _Function
-    for_each(_InputIterator __first, _InputIterator __last, _Function __f)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_requires_valid_range(__first, __last);
-      for ( ; __first != __last; ++__first)
-       __f(*__first);
-      return __f;
-    }
+  // for_each
 
-  /**
-   *  @if maint
-   *  This is an overload used by find() for the Input Iterator case.
-   *  @endif
-  */
+  /// This is an overload used by find() for the Input Iterator case.
   template<typename _InputIterator, typename _Tp>
     inline _InputIterator
-    find(_InputIterator __first, _InputIterator __last,
-        const _Tp& __val,
-        input_iterator_tag)
+    __find(_InputIterator __first, _InputIterator __last,
+          const _Tp& __val, input_iterator_tag)
     {
       while (__first != __last && !(*__first == __val))
        ++__first;
       return __first;
     }
 
-  /**
-   *  @if maint
-   *  This is an overload used by find_if() for the Input Iterator case.
-   *  @endif
-  */
+  /// This is an overload used by find_if() for the Input Iterator case.
   template<typename _InputIterator, typename _Predicate>
     inline _InputIterator
-    find_if(_InputIterator __first, _InputIterator __last,
-           _Predicate __pred,
-           input_iterator_tag)
+    __find_if(_InputIterator __first, _InputIterator __last,
+             _Predicate __pred, input_iterator_tag)
     {
-      while (__first != __last && !__pred(*__first))
+      while (__first != __last && !bool(__pred(*__first)))
        ++__first;
       return __first;
     }
 
-  /**
-   *  @if maint
-   *  This is an overload used by find() for the RAI case.
-   *  @endif
-  */
+  /// This is an overload used by find() for the RAI case.
   template<typename _RandomAccessIterator, typename _Tp>
     _RandomAccessIterator
-    find(_RandomAccessIterator __first, _RandomAccessIterator __last,
-        const _Tp& __val,
-        random_access_iterator_tag)
+    __find(_RandomAccessIterator __first, _RandomAccessIterator __last,
+          const _Tp& __val, random_access_iterator_tag)
     {
-      typename iterator_traits<_RandomAccessIterator>::difference_type __trip_count
-       = (__last - __first) >> 2;
+      typename iterator_traits<_RandomAccessIterator>::difference_type
+       __trip_count = (__last - __first) >> 2;
 
-      for ( ; __trip_count > 0 ; --__trip_count) {
-       if (*__first == __val) return __first;
-       ++__first;
+      for (; __trip_count > 0; --__trip_count)
+       {
+         if (*__first == __val)
+           return __first;
+         ++__first;
 
-       if (*__first == __val) return __first;
-       ++__first;
+         if (*__first == __val)
+           return __first;
+         ++__first;
 
-       if (*__first == __val) return __first;
-       ++__first;
+         if (*__first == __val)
+           return __first;
+         ++__first;
 
-       if (*__first == __val) return __first;
-       ++__first;
-      }
+         if (*__first == __val)
+           return __first;
+         ++__first;
+       }
 
-      switch(__last - __first) {
-      case 3:
-       if (*__first == __val) return __first;
-       ++__first;
-      case 2:
-       if (*__first == __val) return __first;
-       ++__first;
-      case 1:
-       if (*__first == __val) return __first;
-       ++__first;
-      case 0:
-      default:
-       return __last;
-      }
+      switch (__last - __first)
+       {
+       case 3:
+         if (*__first == __val)
+           return __first;
+         ++__first;
+       case 2:
+         if (*__first == __val)
+           return __first;
+         ++__first;
+       case 1:
+         if (*__first == __val)
+           return __first;
+         ++__first;
+       case 0:
+       default:
+         return __last;
+       }
     }
 
-  /**
-   *  @if maint
-   *  This is an overload used by find_if() for the RAI case.
-   *  @endif
-  */
+  /// This is an overload used by find_if() for the RAI case.
   template<typename _RandomAccessIterator, typename _Predicate>
     _RandomAccessIterator
-    find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
-           _Predicate __pred,
-           random_access_iterator_tag)
+    __find_if(_RandomAccessIterator __first, _RandomAccessIterator __last,
+             _Predicate __pred, random_access_iterator_tag)
     {
-      typename iterator_traits<_RandomAccessIterator>::difference_type __trip_count
-       = (__last - __first) >> 2;
+      typename iterator_traits<_RandomAccessIterator>::difference_type
+       __trip_count = (__last - __first) >> 2;
 
-      for ( ; __trip_count > 0 ; --__trip_count) {
-       if (__pred(*__first)) return __first;
-       ++__first;
+      for (; __trip_count > 0; --__trip_count)
+       {
+         if (__pred(*__first))
+           return __first;
+         ++__first;
 
-       if (__pred(*__first)) return __first;
-       ++__first;
+         if (__pred(*__first))
+           return __first;
+         ++__first;
 
-       if (__pred(*__first)) return __first;
-       ++__first;
+         if (__pred(*__first))
+           return __first;
+         ++__first;
 
-       if (__pred(*__first)) return __first;
-       ++__first;
-      }
+         if (__pred(*__first))
+           return __first;
+         ++__first;
+       }
 
-      switch(__last - __first) {
-      case 3:
-       if (__pred(*__first)) return __first;
-       ++__first;
-      case 2:
-       if (__pred(*__first)) return __first;
-       ++__first;
-      case 1:
-       if (__pred(*__first)) return __first;
-       ++__first;
-      case 0:
-      default:
-       return __last;
-      }
+      switch (__last - __first)
+       {
+       case 3:
+         if (__pred(*__first))
+           return __first;
+         ++__first;
+       case 2:
+         if (__pred(*__first))
+           return __first;
+         ++__first;
+       case 1:
+         if (__pred(*__first))
+           return __first;
+         ++__first;
+       case 0:
+       default:
+         return __last;
+       }
     }
 
-  /**
-   *  @brief Find the first occurrence of a value in a sequence.
-   *  @param  first  An input iterator.
-   *  @param  last   An input iterator.
-   *  @param  val    The value to find.
-   *  @return   The first iterator @c i in the range @p [first,last)
-   *  such that @c *i == @p val, or @p last if no such iterator exists.
-  */
-  template<typename _InputIterator, typename _Tp>
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+  /// This is an overload used by find_if_not() for the Input Iterator case.
+  template<typename _InputIterator, typename _Predicate>
     inline _InputIterator
-    find(_InputIterator __first, _InputIterator __last,
-        const _Tp& __val)
+    __find_if_not(_InputIterator __first, _InputIterator __last,
+                 _Predicate __pred, input_iterator_tag)
     {
-      // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_function_requires(_EqualOpConcept<
-               typename iterator_traits<_InputIterator>::value_type, _Tp>)
-      __glibcxx_requires_valid_range(__first, __last);
-      return std::find(__first, __last, __val, std::__iterator_category(__first));
+      while (__first != __last && bool(__pred(*__first)))
+       ++__first;
+      return __first;
     }
 
-  /**
-   *  @brief Find the first element in a sequence for which a predicate is true.
-   *  @param  first  An input iterator.
-   *  @param  last   An input iterator.
-   *  @param  pred   A predicate.
-   *  @return   The first iterator @c i in the range @p [first,last)
-   *  such that @p pred(*i) is true, or @p last if no such iterator exists.
-  */
-  template<typename _InputIterator, typename _Predicate>
-    inline _InputIterator
-    find_if(_InputIterator __first, _InputIterator __last,
-           _Predicate __pred)
+  /// This is an overload used by find_if_not() for the RAI case.
+  template<typename _RandomAccessIterator, typename _Predicate>
+    _RandomAccessIterator
+    __find_if_not(_RandomAccessIterator __first, _RandomAccessIterator __last,
+                 _Predicate __pred, random_access_iterator_tag)
     {
-      // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
-             typename iterator_traits<_InputIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
-      return std::find_if(__first, __last, __pred, std::__iterator_category(__first));
+      typename iterator_traits<_RandomAccessIterator>::difference_type
+       __trip_count = (__last - __first) >> 2;
+
+      for (; __trip_count > 0; --__trip_count)
+       {
+         if (!bool(__pred(*__first)))
+           return __first;
+         ++__first;
+
+         if (!bool(__pred(*__first)))
+           return __first;
+         ++__first;
+
+         if (!bool(__pred(*__first)))
+           return __first;
+         ++__first;
+
+         if (!bool(__pred(*__first)))
+           return __first;
+         ++__first;
+       }
+
+      switch (__last - __first)
+       {
+       case 3:
+         if (!bool(__pred(*__first)))
+           return __first;
+         ++__first;
+       case 2:
+         if (!bool(__pred(*__first)))
+           return __first;
+         ++__first;
+       case 1:
+         if (!bool(__pred(*__first)))
+           return __first;
+         ++__first;
+       case 0:
+       default:
+         return __last;
+       }
     }
+#endif
+
+  // set_difference
+  // set_intersection
+  // set_symmetric_difference
+  // set_union
+  // for_each
+  // find
+  // find_if
+  // find_first_of
+  // adjacent_find
+  // count
+  // count_if
+  // search
 
   /**
-   *  @brief Find two adjacent values in a sequence that are equal.
-   *  @param  first  A forward iterator.
-   *  @param  last   A forward iterator.
-   *  @return   The first iterator @c i such that @c i and @c i+1 are both
-   *  valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
-   *  or @p last if no such iterator exists.
+   *  This is an uglified
+   *  search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
+   *  overloaded for forward iterators.
   */
-  template<typename _ForwardIterator>
+  template<typename _ForwardIterator, typename _Integer, typename _Tp>
     _ForwardIterator
-    adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_EqualityComparableConcept<
-           typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
-      if (__first == __last)
-       return __last;
-      _ForwardIterator __next = __first;
-      while(++__next != __last) {
-       if (*__first == *__next)
-         return __first;
-       __first = __next;
-      }
+    __search_n(_ForwardIterator __first, _ForwardIterator __last,
+              _Integer __count, const _Tp& __val,
+              std::forward_iterator_tag)
+    {
+      __first = _GLIBCXX_STD_P::find(__first, __last, __val);
+      while (__first != __last)
+       {
+         typename iterator_traits<_ForwardIterator>::difference_type
+           __n = __count;
+         _ForwardIterator __i = __first;
+         ++__i;
+         while (__i != __last && __n != 1 && *__i == __val)
+           {
+             ++__i;
+             --__n;
+           }
+         if (__n == 1)
+           return __first;
+         if (__i == __last)
+           return __last;
+         __first = _GLIBCXX_STD_P::find(++__i, __last, __val);
+       }
       return __last;
     }
 
   /**
-   *  @brief Find two adjacent values in a sequence using a predicate.
-   *  @param  first         A forward iterator.
-   *  @param  last          A forward iterator.
-   *  @param  binary_pred   A binary predicate.
-   *  @return   The first iterator @c i such that @c i and @c i+1 are both
-   *  valid iterators in @p [first,last) and such that
-   *  @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
-   *  exists.
+   *  This is an uglified
+   *  search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&)
+   *  overloaded for random access iterators.
   */
-  template<typename _ForwardIterator, typename _BinaryPredicate>
-    _ForwardIterator
-    adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
-                 _BinaryPredicate __binary_pred)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
-           typename iterator_traits<_ForwardIterator>::value_type,
-           typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
-      if (__first == __last)
-       return __last;
-      _ForwardIterator __next = __first;
-      while(++__next != __last) {
-       if (__binary_pred(*__first, *__next))
-         return __first;
-       __first = __next;
-      }
-      return __last;
+  template<typename _RandomAccessIter, typename _Integer, typename _Tp>
+    _RandomAccessIter
+    __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
+              _Integer __count, const _Tp& __val, 
+              std::random_access_iterator_tag)
+    {
+      
+      typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
+       _DistanceType;
+
+      _DistanceType __tailSize = __last - __first;
+      const _DistanceType __pattSize = __count;
+
+      if (__tailSize < __pattSize)
+        return __last;
+
+      const _DistanceType __skipOffset = __pattSize - 1;
+      _RandomAccessIter __lookAhead = __first + __skipOffset;
+      __tailSize -= __pattSize;
+
+      while (1) // the main loop...
+       {
+         // __lookAhead here is always pointing to the last element of next 
+         // possible match.
+         while (!(*__lookAhead == __val)) // the skip loop...
+           {
+             if (__tailSize < __pattSize)
+               return __last;  // Failure
+             __lookAhead += __pattSize;
+             __tailSize -= __pattSize;
+           }
+         _DistanceType __remainder = __skipOffset;
+         for (_RandomAccessIter __backTrack = __lookAhead - 1; 
+              *__backTrack == __val; --__backTrack)
+           {
+             if (--__remainder == 0)
+               return (__lookAhead - __skipOffset); // Success
+           }
+         if (__remainder > __tailSize)
+           return __last; // Failure
+         __lookAhead += __remainder;
+         __tailSize -= __remainder;
+       }
     }
 
+  // search_n
+
   /**
-   *  @brief Count the number of copies of a value in a sequence.
-   *  @param  first  An input iterator.
-   *  @param  last   An input iterator.
-   *  @param  value  The value to be counted.
-   *  @return   The number of iterators @c i in the range @p [first,last)
-   *  for which @c *i == @p value
+   *  This is an uglified
+   *  search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
+   *          _BinaryPredicate)
+   *  overloaded for forward iterators.
   */
-  template<typename _InputIterator, typename _Tp>
-    typename iterator_traits<_InputIterator>::difference_type
-    count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
+  template<typename _ForwardIterator, typename _Integer, typename _Tp,
+           typename _BinaryPredicate>
+    _ForwardIterator
+    __search_n(_ForwardIterator __first, _ForwardIterator __last,
+              _Integer __count, const _Tp& __val,
+              _BinaryPredicate __binary_pred, std::forward_iterator_tag)
     {
-      // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_function_requires(_EqualityComparableConcept<
-           typename iterator_traits<_InputIterator>::value_type >)
-      __glibcxx_function_requires(_EqualityComparableConcept<_Tp>)
-      __glibcxx_requires_valid_range(__first, __last);
-      typename iterator_traits<_InputIterator>::difference_type __n = 0;
-      for ( ; __first != __last; ++__first)
-       if (*__first == __value)
-         ++__n;
-      return __n;
+      while (__first != __last && !bool(__binary_pred(*__first, __val)))
+        ++__first;
+
+      while (__first != __last)
+       {
+         typename iterator_traits<_ForwardIterator>::difference_type
+           __n = __count;
+         _ForwardIterator __i = __first;
+         ++__i;
+         while (__i != __last && __n != 1 && bool(__binary_pred(*__i, __val)))
+           {
+             ++__i;
+             --__n;
+           }
+         if (__n == 1)
+           return __first;
+         if (__i == __last)
+           return __last;
+         __first = ++__i;
+         while (__first != __last
+                && !bool(__binary_pred(*__first, __val)))
+           ++__first;
+       }
+      return __last;
     }
 
   /**
-   *  @brief Count the elements of a sequence for which a predicate is true.
-   *  @param  first  An input iterator.
-   *  @param  last   An input iterator.
-   *  @param  pred   A predicate.
-   *  @return   The number of iterators @c i in the range @p [first,last)
-   *  for which @p pred(*i) is true.
+   *  This is an uglified
+   *  search_n(_ForwardIterator, _ForwardIterator, _Integer, const _Tp&,
+   *          _BinaryPredicate)
+   *  overloaded for random access iterators.
   */
-  template<typename _InputIterator, typename _Predicate>
-    typename iterator_traits<_InputIterator>::difference_type
-    count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
-           typename iterator_traits<_InputIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
-      typename iterator_traits<_InputIterator>::difference_type __n = 0;
-      for ( ; __first != __last; ++__first)
-       if (__pred(*__first))
-         ++__n;
-      return __n;
+  template<typename _RandomAccessIter, typename _Integer, typename _Tp,
+          typename _BinaryPredicate>
+    _RandomAccessIter
+    __search_n(_RandomAccessIter __first, _RandomAccessIter __last,
+              _Integer __count, const _Tp& __val,
+              _BinaryPredicate __binary_pred, std::random_access_iterator_tag)
+    {
+      
+      typedef typename std::iterator_traits<_RandomAccessIter>::difference_type
+       _DistanceType;
+
+      _DistanceType __tailSize = __last - __first;
+      const _DistanceType __pattSize = __count;
+
+      if (__tailSize < __pattSize)
+        return __last;
+
+      const _DistanceType __skipOffset = __pattSize - 1;
+      _RandomAccessIter __lookAhead = __first + __skipOffset;
+      __tailSize -= __pattSize;
+
+      while (1) // the main loop...
+       {
+         // __lookAhead here is always pointing to the last element of next 
+         // possible match.
+         while (!bool(__binary_pred(*__lookAhead, __val))) // the skip loop...
+           {
+             if (__tailSize < __pattSize)
+               return __last;  // Failure
+             __lookAhead += __pattSize;
+             __tailSize -= __pattSize;
+           }
+         _DistanceType __remainder = __skipOffset;
+         for (_RandomAccessIter __backTrack = __lookAhead - 1; 
+              __binary_pred(*__backTrack, __val); --__backTrack)
+           {
+             if (--__remainder == 0)
+               return (__lookAhead - __skipOffset); // Success
+           }
+         if (__remainder > __tailSize)
+           return __last; // Failure
+         __lookAhead += __remainder;
+         __tailSize -= __remainder;
+       }
+    }
+
+  // find_end for forward iterators.
+  template<typename _ForwardIterator1, typename _ForwardIterator2>
+    _ForwardIterator1
+    __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
+              _ForwardIterator2 __first2, _ForwardIterator2 __last2,
+              forward_iterator_tag, forward_iterator_tag)
+    {
+      if (__first2 == __last2)
+       return __last1;
+      else
+       {
+         _ForwardIterator1 __result = __last1;
+         while (1)
+           {
+             _ForwardIterator1 __new_result
+               = _GLIBCXX_STD_P::search(__first1, __last1, __first2, __last2);
+             if (__new_result == __last1)
+               return __result;
+             else
+               {
+                 __result = __new_result;
+                 __first1 = __new_result;
+                 ++__first1;
+               }
+           }
+       }
+    }
+
+  template<typename _ForwardIterator1, typename _ForwardIterator2,
+          typename _BinaryPredicate>
+    _ForwardIterator1
+    __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
+              _ForwardIterator2 __first2, _ForwardIterator2 __last2,
+              forward_iterator_tag, forward_iterator_tag,
+              _BinaryPredicate __comp)
+    {
+      if (__first2 == __last2)
+       return __last1;
+      else
+       {
+         _ForwardIterator1 __result = __last1;
+         while (1)
+           {
+             _ForwardIterator1 __new_result
+               = _GLIBCXX_STD_P::search(__first1, __last1, __first2,
+                                        __last2, __comp);
+             if (__new_result == __last1)
+               return __result;
+             else
+               {
+                 __result = __new_result;
+                 __first1 = __new_result;
+                 ++__first1;
+               }
+           }
+       }
+    }
+
+  // find_end for bidirectional iterators (much faster).
+  template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
+    _BidirectionalIterator1
+    __find_end(_BidirectionalIterator1 __first1,
+              _BidirectionalIterator1 __last1,
+              _BidirectionalIterator2 __first2,
+              _BidirectionalIterator2 __last2,
+              bidirectional_iterator_tag, bidirectional_iterator_tag)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_BidirectionalIteratorConcept<
+                                 _BidirectionalIterator1>)
+      __glibcxx_function_requires(_BidirectionalIteratorConcept<
+                                 _BidirectionalIterator2>)
+
+      typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
+      typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
+
+      _RevIterator1 __rlast1(__first1);
+      _RevIterator2 __rlast2(__first2);
+      _RevIterator1 __rresult = _GLIBCXX_STD_P::search(_RevIterator1(__last1),
+                                                      __rlast1,
+                                                      _RevIterator2(__last2),
+                                                      __rlast2);
+
+      if (__rresult == __rlast1)
+       return __last1;
+      else
+       {
+         _BidirectionalIterator1 __result = __rresult.base();
+         std::advance(__result, -std::distance(__first2, __last2));
+         return __result;
+       }
     }
 
+  template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
+          typename _BinaryPredicate>
+    _BidirectionalIterator1
+    __find_end(_BidirectionalIterator1 __first1,
+              _BidirectionalIterator1 __last1,
+              _BidirectionalIterator2 __first2,
+              _BidirectionalIterator2 __last2,
+              bidirectional_iterator_tag, bidirectional_iterator_tag,
+              _BinaryPredicate __comp)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_BidirectionalIteratorConcept<
+                                 _BidirectionalIterator1>)
+      __glibcxx_function_requires(_BidirectionalIteratorConcept<
+                                 _BidirectionalIterator2>)
+
+      typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
+      typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
+
+      _RevIterator1 __rlast1(__first1);
+      _RevIterator2 __rlast2(__first2);
+      _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
+                                           _RevIterator2(__last2), __rlast2,
+                                           __comp);
+
+      if (__rresult == __rlast1)
+       return __last1;
+      else
+       {
+         _BidirectionalIterator1 __result = __rresult.base();
+         std::advance(__result, -std::distance(__first2, __last2));
+         return __result;
+       }
+    }
 
   /**
-   *  @brief Search a sequence for a matching sub-sequence.
-   *  @param  first1  A forward iterator.
-   *  @param  last1   A forward iterator.
-   *  @param  first2  A forward iterator.
-   *  @param  last2   A forward iterator.
-   *  @return   The first iterator @c i in the range
+   *  @brief  Find last matching subsequence in a sequence.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first1  Start of range to search.
+   *  @param  last1   End of range to search.
+   *  @param  first2  Start of sequence to match.
+   *  @param  last2   End of sequence to match.
+   *  @return   The last iterator @c i in the range
    *  @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
    *  for each @c N in the range @p [0,last2-first2), or @p last1 if no
    *  such iterator exists.
@@ -445,7 +632,8 @@ namespace std
    *  Searches the range @p [first1,last1) for a sub-sequence that compares
    *  equal value-by-value with the sequence given by @p [first2,last2) and
    *  returns an iterator to the first element of the sub-sequence, or
-   *  @p last1 if the sub-sequence is not found.
+   *  @p last1 if the sub-sequence is not found.  The sub-sequence will be the
+   *  last such subsequence contained in [first,last1).
    *
    *  Because the sub-sequence must lie completely within the range
    *  @p [first1,last1) it must start at a position less than
@@ -455,9 +643,9 @@ namespace std
    *  @p [first1,last1-(last2-first2))
   */
   template<typename _ForwardIterator1, typename _ForwardIterator2>
-    _ForwardIterator1
-    search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
-          _ForwardIterator2 __first2, _ForwardIterator2 __last2)
+    inline _ForwardIterator1
+    find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
+            _ForwardIterator2 __first2, _ForwardIterator2 __last2)
     {
       // concept requirements
       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
@@ -467,71 +655,45 @@ namespace std
            typename iterator_traits<_ForwardIterator2>::value_type>)
       __glibcxx_requires_valid_range(__first1, __last1);
       __glibcxx_requires_valid_range(__first2, __last2);
-      // Test for empty ranges
-      if (__first1 == __last1 || __first2 == __last2)
-       return __first1;
-
-      // Test for a pattern of length 1.
-      _ForwardIterator2 __tmp(__first2);
-      ++__tmp;
-      if (__tmp == __last2)
-       return std::find(__first1, __last1, *__first2);
-
-      // General case.
-
-      _ForwardIterator2 __p1, __p;
-
-      __p1 = __first2; ++__p1;
-
-      _ForwardIterator1 __current = __first1;
-
-      while (__first1 != __last1) {
-       __first1 = std::find(__first1, __last1, *__first2);
-       if (__first1 == __last1)
-         return __last1;
-
-       __p = __p1;
-       __current = __first1;
-       if (++__current == __last1)
-         return __last1;
-
-       while (*__current == *__p) {
-         if (++__p == __last2)
-           return __first1;
-         if (++__current == __last1)
-           return __last1;
-       }
 
-       ++__first1;
-      }
-      return __first1;
+      return std::__find_end(__first1, __last1, __first2, __last2,
+                            std::__iterator_category(__first1),
+                            std::__iterator_category(__first2));
     }
 
   /**
-   *  @brief Search a sequence for a matching sub-sequence using a predicate.
-   *  @param  first1     A forward iterator.
-   *  @param  last1      A forward iterator.
-   *  @param  first2     A forward iterator.
-   *  @param  last2      A forward iterator.
-   *  @param  predicate  A binary predicate.
-   *  @return   The first iterator @c i in the range
-   *  @p [first1,last1-(last2-first2)) such that
-   *  @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
-   *  @p [0,last2-first2), or @p last1 if no such iterator exists.
+   *  @brief  Find last matching subsequence in a sequence using a predicate.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first1  Start of range to search.
+   *  @param  last1   End of range to search.
+   *  @param  first2  Start of sequence to match.
+   *  @param  last2   End of sequence to match.
+   *  @param  comp    The predicate to use.
+   *  @return   The last iterator @c i in the range
+   *  @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
+   *  (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
+   *  @p last1 if no such iterator exists.
    *
    *  Searches the range @p [first1,last1) for a sub-sequence that compares
-   *  equal value-by-value with the sequence given by @p [first2,last2),
-   *  using @p predicate to determine equality, and returns an iterator
-   *  to the first element of the sub-sequence, or @p last1 if no such
-   *  iterator exists.
+   *  equal value-by-value with the sequence given by @p [first2,last2) using
+   *  comp as a predicate and returns an iterator to the first element of the
+   *  sub-sequence, or @p last1 if the sub-sequence is not found.  The
+   *  sub-sequence will be the last such subsequence contained in
+   *  [first,last1).
    *
-   *  @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
+   *  Because the sub-sequence must lie completely within the range
+   *  @p [first1,last1) it must start at a position less than
+   *  @p last1-(last2-first2) where @p last2-first2 is the length of the
+   *  sub-sequence.
+   *  This means that the returned iterator @c i will be in the range
+   *  @p [first1,last1-(last2-first2))
   */
-  template<typename _ForwardIterator1, typename _ForwardIterator2, typename _BinaryPredicate>
-    _ForwardIterator1
-    search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
-          _ForwardIterator2 __first2, _ForwardIterator2 __last2,
-          _BinaryPredicate  __predicate)
+  template<typename _ForwardIterator1, typename _ForwardIterator2,
+          typename _BinaryPredicate>
+    inline _ForwardIterator1
+    find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
+            _ForwardIterator2 __first2, _ForwardIterator2 __last2,
+            _BinaryPredicate __comp)
     {
       // concept requirements
       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
@@ -542,608 +704,587 @@ namespace std
       __glibcxx_requires_valid_range(__first1, __last1);
       __glibcxx_requires_valid_range(__first2, __last2);
 
-      // Test for empty ranges
-      if (__first1 == __last1 || __first2 == __last2)
-       return __first1;
-
-      // Test for a pattern of length 1.
-      _ForwardIterator2 __tmp(__first2);
-      ++__tmp;
-      if (__tmp == __last2) {
-       while (__first1 != __last1 && !__predicate(*__first1, *__first2))
-         ++__first1;
-       return __first1;
-      }
-
-      // General case.
-
-      _ForwardIterator2 __p1, __p;
-
-      __p1 = __first2; ++__p1;
+      return std::__find_end(__first1, __last1, __first2, __last2,
+                            std::__iterator_category(__first1),
+                            std::__iterator_category(__first2),
+                            __comp);
+    }
 
-      _ForwardIterator1 __current = __first1;
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+  /**
+   *  @brief  Checks that a predicate is true for all the elements
+   *          of a sequence.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first   An input iterator.
+   *  @param  last    An input iterator.
+   *  @param  pred    A predicate.
+   *  @return  True if the check is true, false otherwise.
+   *
+   *  Returns true if @p pred is true for each element in the range
+   *  @p [first,last), and false otherwise.
+  */
+  template<typename _InputIterator, typename _Predicate>
+    inline bool
+    all_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
+    { return __last == std::find_if_not(__first, __last, __pred); }
 
-      while (__first1 != __last1) {
-       while (__first1 != __last1) {
-         if (__predicate(*__first1, *__first2))
-           break;
-         ++__first1;
-       }
-       while (__first1 != __last1 && !__predicate(*__first1, *__first2))
-         ++__first1;
-       if (__first1 == __last1)
-         return __last1;
+  /**
+   *  @brief  Checks that a predicate is false for all the elements
+   *          of a sequence.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first   An input iterator.
+   *  @param  last    An input iterator.
+   *  @param  pred    A predicate.
+   *  @return  True if the check is true, false otherwise.
+   *
+   *  Returns true if @p pred is false for each element in the range
+   *  @p [first,last), and false otherwise.
+  */
+  template<typename _InputIterator, typename _Predicate>
+    inline bool
+    none_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
+    { return __last == _GLIBCXX_STD_P::find_if(__first, __last, __pred); }
 
-       __p = __p1;
-       __current = __first1;
-       if (++__current == __last1) return __last1;
+  /**
+   *  @brief  Checks that a predicate is false for at least an element
+   *          of a sequence.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first   An input iterator.
+   *  @param  last    An input iterator.
+   *  @param  pred    A predicate.
+   *  @return  True if the check is true, false otherwise.
+   *
+   *  Returns true if an element exists in the range @p [first,last) such that
+   *  @p pred is true, and false otherwise.
+  */
+  template<typename _InputIterator, typename _Predicate>
+    inline bool
+    any_of(_InputIterator __first, _InputIterator __last, _Predicate __pred)
+    { return !std::none_of(__first, __last, __pred); }
 
-       while (__predicate(*__current, *__p)) {
-         if (++__p == __last2)
-           return __first1;
-         if (++__current == __last1)
-           return __last1;
-       }
+  /**
+   *  @brief  Find the first element in a sequence for which a
+   *          predicate is false.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first  An input iterator.
+   *  @param  last   An input iterator.
+   *  @param  pred   A predicate.
+   *  @return   The first iterator @c i in the range @p [first,last)
+   *  such that @p pred(*i) is false, or @p last if no such iterator exists.
+  */
+  template<typename _InputIterator, typename _Predicate>
+    inline _InputIterator
+    find_if_not(_InputIterator __first, _InputIterator __last,
+               _Predicate __pred)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
+             typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+      return std::__find_if_not(__first, __last, __pred,
+                               std::__iterator_category(__first));
+    }
 
-       ++__first1;
-      }
-      return __first1;
+  /**
+   *  @brief  Checks whether the sequence is partitioned.
+   *  @ingroup mutating_algorithms
+   *  @param  first  An input iterator.
+   *  @param  last   An input iterator.
+   *  @param  pred   A predicate.
+   *  @return  True if the range @p [first,last) is partioned by @p pred,
+   *  i.e. if all elements that satisfy @p pred appear before those that
+   *  do not.
+  */
+  template<typename _InputIterator, typename _Predicate>
+    inline bool
+    is_partitioned(_InputIterator __first, _InputIterator __last,
+                  _Predicate __pred)
+    {
+      __first = std::find_if_not(__first, __last, __pred);
+      return std::none_of(__first, __last, __pred);
     }
 
   /**
-   *  @brief Search a sequence for a number of consecutive values.
-   *  @param  first  A forward iterator.
-   *  @param  last   A forward iterator.
-   *  @param  count  The number of consecutive values.
-   *  @param  val    The value to find.
-   *  @return   The first iterator @c i in the range @p [first,last-count)
-   *  such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
-   *  or @p last if no such iterator exists.
-   *
-   *  Searches the range @p [first,last) for @p count consecutive elements
-   *  equal to @p val.
+   *  @brief  Find the partition point of a partitioned range.
+   *  @ingroup mutating_algorithms
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @param  pred    A predicate.
+   *  @return  An iterator @p mid such that @p all_of(first, mid, pred)
+   *           and @p none_of(mid, last, pred) are both true.
   */
-  template<typename _ForwardIterator, typename _Integer, typename _Tp>
+  template<typename _ForwardIterator, typename _Predicate>
     _ForwardIterator
-    search_n(_ForwardIterator __first, _ForwardIterator __last,
-            _Integer __count, const _Tp& __val)
+    partition_point(_ForwardIterator __first, _ForwardIterator __last,
+                   _Predicate __pred)
     {
       // concept requirements
       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_EqualityComparableConcept<
-           typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_function_requires(_EqualityComparableConcept<_Tp>)
+      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
+             typename iterator_traits<_ForwardIterator>::value_type>)
+
+      // A specific debug-mode test will be necessary...
       __glibcxx_requires_valid_range(__first, __last);
 
-      if (__count <= 0)
-       return __first;
-      else {
-       __first = std::find(__first, __last, __val);
-       while (__first != __last) {
-         typename iterator_traits<_ForwardIterator>::difference_type __n = __count;
-         _ForwardIterator __i = __first;
-         ++__i;
-         while (__i != __last && __n != 1 && *__i == __val) {
-           ++__i;
-           --__n;
-         }
-         if (__n == 1)
-           return __first;
+      typedef typename iterator_traits<_ForwardIterator>::difference_type
+       _DistanceType;
+
+      _DistanceType __len = std::distance(__first, __last);
+      _DistanceType __half;
+      _ForwardIterator __middle;
+
+      while (__len > 0)
+       {
+         __half = __len >> 1;
+         __middle = __first;
+         std::advance(__middle, __half);
+         if (__pred(*__middle))
+           {
+             __first = __middle;
+             ++__first;
+             __len = __len - __half - 1;
+           }
          else
-           __first = std::find(__i, __last, __val);
+           __len = __half;
        }
-       return __last;
-      }
+      return __first;
     }
+#endif
+
 
   /**
-   *  @brief Search a sequence for a number of consecutive values using a
-   *         predicate.
-   *  @param  first        A forward iterator.
-   *  @param  last         A forward iterator.
-   *  @param  count        The number of consecutive values.
-   *  @param  val          The value to find.
-   *  @param  binary_pred  A binary predicate.
-   *  @return   The first iterator @c i in the range @p [first,last-count)
-   *  such that @p binary_pred(*(i+N),val) is true for each @c N in the
-   *  range @p [0,count), or @p last if no such iterator exists.
+   *  @brief Copy a sequence, removing elements of a given value.
+   *  @ingroup mutating_algorithms
+   *  @param  first   An input iterator.
+   *  @param  last    An input iterator.
+   *  @param  result  An output iterator.
+   *  @param  value   The value to be removed.
+   *  @return   An iterator designating the end of the resulting sequence.
    *
-   *  Searches the range @p [first,last) for @p count consecutive elements
-   *  for which the predicate returns true.
+   *  Copies each element in the range @p [first,last) not equal to @p value
+   *  to the range beginning at @p result.
+   *  remove_copy() is stable, so the relative order of elements that are
+   *  copied is unchanged.
   */
-  template<typename _ForwardIterator, typename _Integer, typename _Tp,
-           typename _BinaryPredicate>
-    _ForwardIterator
-    search_n(_ForwardIterator __first, _ForwardIterator __last,
-            _Integer __count, const _Tp& __val,
-            _BinaryPredicate __binary_pred)
+  template<typename _InputIterator, typename _OutputIterator, typename _Tp>
+    _OutputIterator
+    remove_copy(_InputIterator __first, _InputIterator __last,
+               _OutputIterator __result, const _Tp& __value)
     {
       // concept requirements
-      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
-           typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_function_requires(_EqualOpConcept<
+           typename iterator_traits<_InputIterator>::value_type, _Tp>)
       __glibcxx_requires_valid_range(__first, __last);
 
-      if (__count <= 0)
-       return __first;
-      else {
-       while (__first != __last) {
-         if (__binary_pred(*__first, __val))
-           break;
-         ++__first;
-       }
-       while (__first != __last) {
-         typename iterator_traits<_ForwardIterator>::difference_type __n = __count;
-         _ForwardIterator __i = __first;
-         ++__i;
-         while (__i != __last && __n != 1 && __binary_pred(*__i, __val)) {
-           ++__i;
-           --__n;
-         }
-         if (__n == 1)
-           return __first;
-         else {
-           while (__i != __last) {
-             if (__binary_pred(*__i, __val))
-               break;
-             ++__i;
-           }
-           __first = __i;
+      for (; __first != __last; ++__first)
+       if (!(*__first == __value))
+         {
+           *__result = *__first;
+           ++__result;
          }
-       }
-       return __last;
-      }
+      return __result;
     }
 
   /**
-   *  @brief Swap the elements of two sequences.
-   *  @param  first1  A forward iterator.
-   *  @param  last1   A forward iterator.
-   *  @param  first2  A forward iterator.
-   *  @return   An iterator equal to @p first2+(last1-first1).
+   *  @brief Copy a sequence, removing elements for which a predicate is true.
+   *  @ingroup mutating_algorithms
+   *  @param  first   An input iterator.
+   *  @param  last    An input iterator.
+   *  @param  result  An output iterator.
+   *  @param  pred    A predicate.
+   *  @return   An iterator designating the end of the resulting sequence.
+   *
+   *  Copies each element in the range @p [first,last) for which
+   *  @p pred returns false to the range beginning at @p result.
    *
-   *  Swaps each element in the range @p [first1,last1) with the
-   *  corresponding element in the range @p [first2,(last1-first1)).
-   *  The ranges must not overlap.
+   *  remove_copy_if() is stable, so the relative order of elements that are
+   *  copied is unchanged.
   */
-  template<typename _ForwardIterator1, typename _ForwardIterator2>
-    _ForwardIterator2
-    swap_ranges(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
-               _ForwardIterator2 __first2)
+  template<typename _InputIterator, typename _OutputIterator,
+          typename _Predicate>
+    _OutputIterator
+    remove_copy_if(_InputIterator __first, _InputIterator __last,
+                  _OutputIterator __result, _Predicate __pred)
     {
       // concept requirements
-      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator1>)
-      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator2>)
-      __glibcxx_function_requires(_ConvertibleConcept<
-           typename iterator_traits<_ForwardIterator1>::value_type,
-           typename iterator_traits<_ForwardIterator2>::value_type>)
-      __glibcxx_function_requires(_ConvertibleConcept<
-           typename iterator_traits<_ForwardIterator2>::value_type,
-           typename iterator_traits<_ForwardIterator1>::value_type>)
-      __glibcxx_requires_valid_range(__first1, __last1);
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
+           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
 
-      for ( ; __first1 != __last1; ++__first1, ++__first2)
-       std::iter_swap(__first1, __first2);
-      return __first2;
+      for (; __first != __last; ++__first)
+       if (!bool(__pred(*__first)))
+         {
+           *__result = *__first;
+           ++__result;
+         }
+      return __result;
     }
 
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
   /**
-   *  @brief Perform an operation on a sequence.
-   *  @param  first     An input iterator.
-   *  @param  last      An input iterator.
-   *  @param  result    An output iterator.
-   *  @param  unary_op  A unary operator.
-   *  @return   An output iterator equal to @p result+(last-first).
+   *  @brief Copy the elements of a sequence for which a predicate is true.
+   *  @ingroup mutating_algorithms
+   *  @param  first   An input iterator.
+   *  @param  last    An input iterator.
+   *  @param  result  An output iterator.
+   *  @param  pred    A predicate.
+   *  @return   An iterator designating the end of the resulting sequence.
    *
-   *  Applies the operator to each element in the input range and assigns
-   *  the results to successive elements of the output sequence.
-   *  Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
-   *  range @p [0,last-first).
+   *  Copies each element in the range @p [first,last) for which
+   *  @p pred returns true to the range beginning at @p result.
    *
-   *  @p unary_op must not alter its argument.
+   *  copy_if() is stable, so the relative order of elements that are
+   *  copied is unchanged.
   */
-  template<typename _InputIterator, typename _OutputIterator, typename _UnaryOperation>
+  template<typename _InputIterator, typename _OutputIterator,
+          typename _Predicate>
     _OutputIterator
-    transform(_InputIterator __first, _InputIterator __last,
-             _OutputIterator __result, _UnaryOperation __unary_op)
+    copy_if(_InputIterator __first, _InputIterator __last,
+           _OutputIterator __result, _Predicate __pred)
     {
       // concept requirements
       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-            // "the type returned by a _UnaryOperation"
-            __typeof__(__unary_op(*__first))>)
+           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
+           typename iterator_traits<_InputIterator>::value_type>)
       __glibcxx_requires_valid_range(__first, __last);
 
-      for ( ; __first != __last; ++__first, ++__result)
-       *__result = __unary_op(*__first);
+      for (; __first != __last; ++__first)
+       if (__pred(*__first))
+         {
+           *__result = *__first;
+           ++__result;
+         }
       return __result;
     }
 
-  /**
-   *  @brief Perform an operation on corresponding elements of two sequences.
-   *  @param  first1     An input iterator.
-   *  @param  last1      An input iterator.
-   *  @param  first2     An input iterator.
-   *  @param  result     An output iterator.
-   *  @param  binary_op  A binary operator.
-   *  @return   An output iterator equal to @p result+(last-first).
-   *
-   *  Applies the operator to the corresponding elements in the two
-   *  input ranges and assigns the results to successive elements of the
-   *  output sequence.
-   *  Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
-   *  @c N in the range @p [0,last1-first1).
-   *
-   *  @p binary_op must not alter either of its arguments.
-  */
-  template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
-          typename _BinaryOperation>
+
+  template<typename _InputIterator, typename _Size, typename _OutputIterator>
     _OutputIterator
-    transform(_InputIterator1 __first1, _InputIterator1 __last1,
-             _InputIterator2 __first2, _OutputIterator __result,
-             _BinaryOperation __binary_op)
+    __copy_n(_InputIterator __first, _Size __n,
+            _OutputIterator __result, input_iterator_tag)
     {
-      // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-            // "the type returned by a _BinaryOperation"
-            __typeof__(__binary_op(*__first1,*__first2))>)
-      __glibcxx_requires_valid_range(__first1, __last1);
-
-      for ( ; __first1 != __last1; ++__first1, ++__first2, ++__result)
-       *__result = __binary_op(*__first1, *__first2);
+      for (; __n > 0; --__n)
+       {
+         *__result = *__first;
+         ++__first;
+         ++__result;
+       }
       return __result;
     }
 
+  template<typename _RandomAccessIterator, typename _Size,
+          typename _OutputIterator>
+    inline _OutputIterator
+    __copy_n(_RandomAccessIterator __first, _Size __n,
+            _OutputIterator __result, random_access_iterator_tag)
+    { return std::copy(__first, __first + __n, __result); }
+
   /**
-   *  @brief Replace each occurrence of one value in a sequence with another
-   *         value.
-   *  @param  first      A forward iterator.
-   *  @param  last       A forward iterator.
-   *  @param  old_value  The value to be replaced.
-   *  @param  new_value  The replacement value.
-   *  @return   replace() returns no value.
+   *  @brief Copies the range [first,first+n) into [result,result+n).
+   *  @ingroup mutating_algorithms
+   *  @param  first  An input iterator.
+   *  @param  n      The number of elements to copy.
+   *  @param  result An output iterator.
+   *  @return  result+n.
    *
-   *  For each iterator @c i in the range @p [first,last) if @c *i ==
-   *  @p old_value then the assignment @c *i = @p new_value is performed.
+   *  This inline function will boil down to a call to @c memmove whenever
+   *  possible.  Failing that, if random access iterators are passed, then the
+   *  loop count will be known (and therefore a candidate for compiler
+   *  optimizations such as unrolling).
   */
-  template<typename _ForwardIterator, typename _Tp>
-    void
-    replace(_ForwardIterator __first, _ForwardIterator __last,
-           const _Tp& __old_value, const _Tp& __new_value)
+  template<typename _InputIterator, typename _Size, typename _OutputIterator>
+    inline _OutputIterator
+    copy_n(_InputIterator __first, _Size __n, _OutputIterator __result)
     {
       // concept requirements
-      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_EqualOpConcept<
-           typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
-      __glibcxx_function_requires(_ConvertibleConcept<_Tp,
-           typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+           typename iterator_traits<_InputIterator>::value_type>)
 
-      for ( ; __first != __last; ++__first)
-       if (*__first == __old_value)
-         *__first = __new_value;
+      return std::__copy_n(__first, __n, __result,
+                          std::__iterator_category(__first));
     }
 
   /**
-   *  @brief Replace each value in a sequence for which a predicate returns
-   *         true with another value.
-   *  @param  first      A forward iterator.
-   *  @param  last       A forward iterator.
-   *  @param  pred       A predicate.
-   *  @param  new_value  The replacement value.
-   *  @return   replace_if() returns no value.
+   *  @brief Copy the elements of a sequence to separate output sequences
+   *         depending on the truth value of a predicate.
+   *  @ingroup mutating_algorithms
+   *  @param  first   An input iterator.
+   *  @param  last    An input iterator.
+   *  @param  out_true   An output iterator.
+   *  @param  out_false  An output iterator.
+   *  @param  pred    A predicate.
+   *  @return   A pair designating the ends of the resulting sequences.
    *
-   *  For each iterator @c i in the range @p [first,last) if @p pred(*i)
-   *  is true then the assignment @c *i = @p new_value is performed.
+   *  Copies each element in the range @p [first,last) for which
+   *  @p pred returns true to the range beginning at @p out_true
+   *  and each element for which @p pred returns false to @p out_false.
   */
-  template<typename _ForwardIterator, typename _Predicate, typename _Tp>
-    void
-    replace_if(_ForwardIterator __first, _ForwardIterator __last,
-              _Predicate __pred, const _Tp& __new_value)
+  template<typename _InputIterator, typename _OutputIterator1,
+          typename _OutputIterator2, typename _Predicate>
+    pair<_OutputIterator1, _OutputIterator2>
+    partition_copy(_InputIterator __first, _InputIterator __last,
+                  _OutputIterator1 __out_true, _OutputIterator2 __out_false,
+                  _Predicate __pred)
     {
       // concept requirements
-      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_ConvertibleConcept<_Tp,
-           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator1,
+           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator2,
+           typename iterator_traits<_InputIterator>::value_type>)
       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
-           typename iterator_traits<_ForwardIterator>::value_type>)
+           typename iterator_traits<_InputIterator>::value_type>)
       __glibcxx_requires_valid_range(__first, __last);
-
-      for ( ; __first != __last; ++__first)
+      
+      for (; __first != __last; ++__first)
        if (__pred(*__first))
-         *__first = __new_value;
+         {
+           *__out_true = *__first;
+           ++__out_true;
+         }
+       else
+         {
+           *__out_false = *__first;
+           ++__out_false;
+         }
+
+      return pair<_OutputIterator1, _OutputIterator2>(__out_true, __out_false);
     }
+#endif
 
   /**
-   *  @brief Copy a sequence, replacing each element of one value with another
-   *         value.
-   *  @param  first      An input iterator.
-   *  @param  last       An input iterator.
-   *  @param  result     An output iterator.
-   *  @param  old_value  The value to be replaced.
-   *  @param  new_value  The replacement value.
-   *  @return   The end of the output sequence, @p result+(last-first).
+   *  @brief Remove elements from a sequence.
+   *  @ingroup mutating_algorithms
+   *  @param  first  An input iterator.
+   *  @param  last   An input iterator.
+   *  @param  value  The value to be removed.
+   *  @return   An iterator designating the end of the resulting sequence.
    *
-   *  Copies each element in the input range @p [first,last) to the
-   *  output range @p [result,result+(last-first)) replacing elements
-   *  equal to @p old_value with @p new_value.
+   *  All elements equal to @p value are removed from the range
+   *  @p [first,last).
+   *
+   *  remove() is stable, so the relative order of elements that are
+   *  not removed is unchanged.
+   *
+   *  Elements between the end of the resulting sequence and @p last
+   *  are still present, but their value is unspecified.
   */
-  template<typename _InputIterator, typename _OutputIterator, typename _Tp>
-    _OutputIterator
-    replace_copy(_InputIterator __first, _InputIterator __last,
-                _OutputIterator __result,
-                const _Tp& __old_value, const _Tp& __new_value)
+  template<typename _ForwardIterator, typename _Tp>
+    _ForwardIterator
+    remove(_ForwardIterator __first, _ForwardIterator __last,
+          const _Tp& __value)
     {
       // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
+                                 _ForwardIterator>)
       __glibcxx_function_requires(_EqualOpConcept<
-           typename iterator_traits<_InputIterator>::value_type, _Tp>)
-      __glibcxx_requires_valid_range(__first, __last);
-
-      for ( ; __first != __last; ++__first, ++__result)
-       *__result = *__first == __old_value ? __new_value : *__first;
-      return __result;
-    }
-
-  /**
-   *  @brief Copy a sequence, replacing each value for which a predicate
-   *         returns true with another value.
-   *  @param  first      An input iterator.
-   *  @param  last       An input iterator.
-   *  @param  result     An output iterator.
-   *  @param  pred       A predicate.
-   *  @param  new_value  The replacement value.
-   *  @return   The end of the output sequence, @p result+(last-first).
-   *
-   *  Copies each element in the range @p [first,last) to the range
-   *  @p [result,result+(last-first)) replacing elements for which
-   *  @p pred returns true with @p new_value.
-  */
-  template<typename _InputIterator, typename _OutputIterator, typename _Predicate,
-           typename _Tp>
-    _OutputIterator
-    replace_copy_if(_InputIterator __first, _InputIterator __last,
-                   _OutputIterator __result,
-                   _Predicate __pred, const _Tp& __new_value)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator>::value_type>)
-      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
-           typename iterator_traits<_InputIterator>::value_type>)
+           typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
       __glibcxx_requires_valid_range(__first, __last);
 
-      for ( ; __first != __last; ++__first, ++__result)
-       *__result = __pred(*__first) ? __new_value : *__first;
+      __first = _GLIBCXX_STD_P::find(__first, __last, __value);
+      if(__first == __last)
+        return __first;
+      _ForwardIterator __result = __first;
+      ++__first;
+      for(; __first != __last; ++__first)
+        if(!(*__first == __value))
+          {
+            *__result = _GLIBCXX_MOVE(*__first);
+            ++__result;
+          }
       return __result;
     }
 
   /**
-   *  @brief Assign the result of a function object to each value in a
-   *         sequence.
+   *  @brief Remove elements from a sequence using a predicate.
+   *  @ingroup mutating_algorithms
    *  @param  first  A forward iterator.
    *  @param  last   A forward iterator.
-   *  @param  gen    A function object taking no arguments.
-   *  @return   generate() returns no value.
-   *
-   *  Performs the assignment @c *i = @p gen() for each @c i in the range
-   *  @p [first,last).
-  */
-  template<typename _ForwardIterator, typename _Generator>
-    void
-    generate(_ForwardIterator __first, _ForwardIterator __last, _Generator __gen)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_GeneratorConcept<_Generator,
-           typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
-
-      for ( ; __first != __last; ++__first)
-       *__first = __gen();
-    }
-
-  /**
-   *  @brief Assign the result of a function object to each value in a
-   *         sequence.
-   *  @param  first  A forward iterator.
-   *  @param  n      The length of the sequence.
-   *  @param  gen    A function object taking no arguments.
-   *  @return   The end of the sequence, @p first+n
-   *
-   *  Performs the assignment @c *i = @p gen() for each @c i in the range
-   *  @p [first,first+n).
-  */
-  template<typename _OutputIterator, typename _Size, typename _Generator>
-    _OutputIterator
-    generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-            // "the type returned by a _Generator"
-            __typeof__(__gen())>)
-
-      for ( ; __n > 0; --__n, ++__first)
-       *__first = __gen();
-      return __first;
-    }
-
-  /**
-   *  @brief Copy a sequence, removing elements of a given value.
-   *  @param  first   An input iterator.
-   *  @param  last    An input iterator.
-   *  @param  result  An output iterator.
-   *  @param  value   The value to be removed.
+   *  @param  pred   A predicate.
    *  @return   An iterator designating the end of the resulting sequence.
    *
-   *  Copies each element in the range @p [first,last) not equal to @p value
-   *  to the range beginning at @p result.
-   *  remove_copy() is stable, so the relative order of elements that are
-   *  copied is unchanged.
-  */
-  template<typename _InputIterator, typename _OutputIterator, typename _Tp>
-    _OutputIterator
-    remove_copy(_InputIterator __first, _InputIterator __last,
-               _OutputIterator __result, const _Tp& __value)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator>::value_type>)
-      __glibcxx_function_requires(_EqualOpConcept<
-           typename iterator_traits<_InputIterator>::value_type, _Tp>)
-      __glibcxx_requires_valid_range(__first, __last);
-
-      for ( ; __first != __last; ++__first)
-       if (!(*__first == __value)) {
-         *__result = *__first;
-         ++__result;
-       }
-      return __result;
-    }
-
-  /**
-   *  @brief Copy a sequence, removing elements for which a predicate is true.
-   *  @param  first   An input iterator.
-   *  @param  last    An input iterator.
-   *  @param  result  An output iterator.
-   *  @param  pred    A predicate.
-   *  @return   An iterator designating the end of the resulting sequence.
+   *  All elements for which @p pred returns true are removed from the range
+   *  @p [first,last).
    *
-   *  Copies each element in the range @p [first,last) for which
-   *  @p pred returns true to the range beginning at @p result.
+   *  remove_if() is stable, so the relative order of elements that are
+   *  not removed is unchanged.
    *
-   *  remove_copy_if() is stable, so the relative order of elements that are
-   *  copied is unchanged.
+   *  Elements between the end of the resulting sequence and @p last
+   *  are still present, but their value is unspecified.
   */
-  template<typename _InputIterator, typename _OutputIterator, typename _Predicate>
-    _OutputIterator
-    remove_copy_if(_InputIterator __first, _InputIterator __last,
-                  _OutputIterator __result, _Predicate __pred)
+  template<typename _ForwardIterator, typename _Predicate>
+    _ForwardIterator
+    remove_if(_ForwardIterator __first, _ForwardIterator __last,
+             _Predicate __pred)
     {
       // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
+                                 _ForwardIterator>)
       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
-           typename iterator_traits<_InputIterator>::value_type>)
+           typename iterator_traits<_ForwardIterator>::value_type>)
       __glibcxx_requires_valid_range(__first, __last);
 
-      for ( ; __first != __last; ++__first)
-       if (!__pred(*__first)) {
-         *__result = *__first;
-         ++__result;
-       }
+      __first = _GLIBCXX_STD_P::find_if(__first, __last, __pred);
+      if(__first == __last)
+        return __first;
+      _ForwardIterator __result = __first;
+      ++__first;
+      for(; __first != __last; ++__first)
+        if(!bool(__pred(*__first)))
+          {
+            *__result = _GLIBCXX_MOVE(*__first);
+            ++__result;
+          }
       return __result;
     }
 
   /**
-   *  @brief Remove elements from a sequence.
-   *  @param  first  An input iterator.
-   *  @param  last   An input iterator.
-   *  @param  value  The value to be removed.
-   *  @return   An iterator designating the end of the resulting sequence.
-   *
-   *  All elements equal to @p value are removed from the range
-   *  @p [first,last).
+   *  @brief Remove consecutive duplicate values from a sequence.
+   *  @ingroup mutating_algorithms
+   *  @param  first  A forward iterator.
+   *  @param  last   A forward iterator.
+   *  @return  An iterator designating the end of the resulting sequence.
    *
-   *  remove() is stable, so the relative order of elements that are
+   *  Removes all but the first element from each group of consecutive
+   *  values that compare equal.
+   *  unique() is stable, so the relative order of elements that are
    *  not removed is unchanged.
-   *
    *  Elements between the end of the resulting sequence and @p last
    *  are still present, but their value is unspecified.
   */
-  template<typename _ForwardIterator, typename _Tp>
+  template<typename _ForwardIterator>
     _ForwardIterator
-    remove(_ForwardIterator __first, _ForwardIterator __last,
-          const _Tp& __value)
+    unique(_ForwardIterator __first, _ForwardIterator __last)
     {
       // concept requirements
-      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_ConvertibleConcept<_Tp,
-           typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_function_requires(_EqualOpConcept<
-           typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
+      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
+                                 _ForwardIterator>)
+      __glibcxx_function_requires(_EqualityComparableConcept<
+                    typename iterator_traits<_ForwardIterator>::value_type>)
       __glibcxx_requires_valid_range(__first, __last);
 
-      __first = std::find(__first, __last, __value);
-      _ForwardIterator __i = __first;
-      return __first == __last ? __first
-                              : std::remove_copy(++__i, __last, __first, __value);
+      // Skip the beginning, if already unique.
+      __first = _GLIBCXX_STD_P::adjacent_find(__first, __last);
+      if (__first == __last)
+       return __last;
+
+      // Do the real copy work.
+      _ForwardIterator __dest = __first;
+      ++__first;
+      while (++__first != __last)
+       if (!(*__dest == *__first))
+         *++__dest = _GLIBCXX_MOVE(*__first);
+      return ++__dest;
     }
 
   /**
-   *  @brief Remove elements from a sequence using a predicate.
-   *  @param  first  A forward iterator.
-   *  @param  last   A forward iterator.
-   *  @param  pred   A predicate.
-   *  @return   An iterator designating the end of the resulting sequence.
-   *
-   *  All elements for which @p pred returns true are removed from the range
-   *  @p [first,last).
+   *  @brief Remove consecutive values from a sequence using a predicate.
+   *  @ingroup mutating_algorithms
+   *  @param  first        A forward iterator.
+   *  @param  last         A forward iterator.
+   *  @param  binary_pred  A binary predicate.
+   *  @return  An iterator designating the end of the resulting sequence.
    *
-   *  remove_if() is stable, so the relative order of elements that are
+   *  Removes all but the first element from each group of consecutive
+   *  values for which @p binary_pred returns true.
+   *  unique() is stable, so the relative order of elements that are
    *  not removed is unchanged.
-   *
    *  Elements between the end of the resulting sequence and @p last
    *  are still present, but their value is unspecified.
   */
-  template<typename _ForwardIterator, typename _Predicate>
+  template<typename _ForwardIterator, typename _BinaryPredicate>
     _ForwardIterator
-    remove_if(_ForwardIterator __first, _ForwardIterator __last,
-             _Predicate __pred)
+    unique(_ForwardIterator __first, _ForwardIterator __last,
+           _BinaryPredicate __binary_pred)
     {
       // concept requirements
-      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
-           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
+                                 _ForwardIterator>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
+               typename iterator_traits<_ForwardIterator>::value_type,
+               typename iterator_traits<_ForwardIterator>::value_type>)
       __glibcxx_requires_valid_range(__first, __last);
 
-      __first = std::find_if(__first, __last, __pred);
-      _ForwardIterator __i = __first;
-      return __first == __last ? __first
-                              : std::remove_copy_if(++__i, __last, __first, __pred);
+      // Skip the beginning, if already unique.
+      __first = _GLIBCXX_STD_P::adjacent_find(__first, __last, __binary_pred);
+      if (__first == __last)
+       return __last;
+
+      // Do the real copy work.
+      _ForwardIterator __dest = __first;
+      ++__first;
+      while (++__first != __last)
+       if (!bool(__binary_pred(*__dest, *__first)))
+         *++__dest = _GLIBCXX_MOVE(*__first);
+      return ++__dest;
+    }
+
+  /**
+   *  This is an uglified unique_copy(_InputIterator, _InputIterator,
+   *                                  _OutputIterator)
+   *  overloaded for forward iterators and output iterator as result.
+  */
+  template<typename _ForwardIterator, typename _OutputIterator>
+    _OutputIterator
+    __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
+                 _OutputIterator __result,
+                 forward_iterator_tag, output_iterator_tag)
+    {
+      // concept requirements -- taken care of in dispatching function
+      _ForwardIterator __next = __first;
+      *__result = *__first;
+      while (++__next != __last)
+       if (!(*__first == *__next))
+         {
+           __first = __next;
+           *++__result = *__first;
+         }
+      return ++__result;
     }
 
   /**
-   *  @if maint
-   *  This is an uglified unique_copy(_InputIterator, _InputIterator, _OutputIterator)
-   *  overloaded for output iterators.
-   *  @endif
+   *  This is an uglified unique_copy(_InputIterator, _InputIterator,
+   *                                  _OutputIterator)
+   *  overloaded for input iterators and output iterator as result.
   */
   template<typename _InputIterator, typename _OutputIterator>
     _OutputIterator
     __unique_copy(_InputIterator __first, _InputIterator __last,
                  _OutputIterator __result,
-                 output_iterator_tag)
+                 input_iterator_tag, output_iterator_tag)
     {
       // concept requirements -- taken care of in dispatching function
       typename iterator_traits<_InputIterator>::value_type __value = *__first;
       *__result = __value;
       while (++__first != __last)
-       if (!(__value == *__first)) {
-         __value = *__first;
-         *++__result = __value;
-       }
+       if (!(__value == *__first))
+         {
+           __value = *__first;
+           *++__result = __value;
+         }
       return ++__result;
     }
 
   /**
-   *  @if maint
-   *  This is an uglified unique_copy(_InputIterator, _InputIterator, _OutputIterator)
-   *  overloaded for forward iterators.
-   *  @endif
+   *  This is an uglified unique_copy(_InputIterator, _InputIterator,
+   *                                  _OutputIterator)
+   *  overloaded for input iterators and forward iterator as result.
   */
   template<typename _InputIterator, typename _ForwardIterator>
     _ForwardIterator
     __unique_copy(_InputIterator __first, _InputIterator __last,
                  _ForwardIterator __result,
-                 forward_iterator_tag)
+                 input_iterator_tag, forward_iterator_tag)
     {
       // concept requirements -- taken care of in dispatching function
       *__result = *__first;
@@ -1154,236 +1295,132 @@ namespace std
     }
 
   /**
-   *  @if maint
    *  This is an uglified
-   *  unique_copy(_InputIterator, _InputIterator, _OutputIterator, _BinaryPredicate)
-   *  overloaded for output iterators.
-   *  @endif
+   *  unique_copy(_InputIterator, _InputIterator, _OutputIterator,
+   *              _BinaryPredicate)
+   *  overloaded for forward iterators and output iterator as result.
   */
-  template<typename _InputIterator, typename _OutputIterator, typename _BinaryPredicate>
+  template<typename _ForwardIterator, typename _OutputIterator,
+          typename _BinaryPredicate>
     _OutputIterator
-    __unique_copy(_InputIterator __first, _InputIterator __last,
-                 _OutputIterator __result,
-                 _BinaryPredicate __binary_pred,
-                 output_iterator_tag)
+    __unique_copy(_ForwardIterator __first, _ForwardIterator __last,
+                 _OutputIterator __result, _BinaryPredicate __binary_pred,
+                 forward_iterator_tag, output_iterator_tag)
     {
       // concept requirements -- iterators already checked
       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
-         typename iterator_traits<_InputIterator>::value_type,
-         typename iterator_traits<_InputIterator>::value_type>)
+         typename iterator_traits<_ForwardIterator>::value_type,
+         typename iterator_traits<_ForwardIterator>::value_type>)
 
-      typename iterator_traits<_InputIterator>::value_type __value = *__first;
-      *__result = __value;
-      while (++__first != __last)
-       if (!__binary_pred(__value, *__first)) {
-         __value = *__first;
-         *++__result = __value;
-       }
+      _ForwardIterator __next = __first;
+      *__result = *__first;
+      while (++__next != __last)
+       if (!bool(__binary_pred(*__first, *__next)))
+         {
+           __first = __next;
+           *++__result = *__first;
+         }
       return ++__result;
     }
 
   /**
-   *  @if maint
    *  This is an uglified
-   *  unique_copy(_InputIterator, _InputIterator, _OutputIterator, _BinaryPredicate)
-   *  overloaded for forward iterators.
-   *  @endif
+   *  unique_copy(_InputIterator, _InputIterator, _OutputIterator,
+   *              _BinaryPredicate)
+   *  overloaded for input iterators and output iterator as result.
   */
-  template<typename _InputIterator, typename _ForwardIterator, typename _BinaryPredicate>
-    _ForwardIterator
+  template<typename _InputIterator, typename _OutputIterator,
+          typename _BinaryPredicate>
+    _OutputIterator
     __unique_copy(_InputIterator __first, _InputIterator __last,
-                 _ForwardIterator __result,
-                 _BinaryPredicate __binary_pred,
-                 forward_iterator_tag)
+                 _OutputIterator __result, _BinaryPredicate __binary_pred,
+                 input_iterator_tag, output_iterator_tag)
     {
       // concept requirements -- iterators already checked
       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
-           typename iterator_traits<_ForwardIterator>::value_type,
-           typename iterator_traits<_InputIterator>::value_type>)
+         typename iterator_traits<_InputIterator>::value_type,
+         typename iterator_traits<_InputIterator>::value_type>)
 
-      *__result = *__first;
+      typename iterator_traits<_InputIterator>::value_type __value = *__first;
+      *__result = __value;
       while (++__first != __last)
-       if (!__binary_pred(*__result, *__first)) *++__result = *__first;
+       if (!bool(__binary_pred(__value, *__first)))
+         {
+           __value = *__first;
+           *++__result = __value;
+         }
       return ++__result;
     }
 
   /**
-   *  @brief Copy a sequence, removing consecutive duplicate values.
-   *  @param  first   An input iterator.
-   *  @param  last    An input iterator.
-   *  @param  result  An output iterator.
-   *  @return   An iterator designating the end of the resulting sequence.
-   *
-   *  Copies each element in the range @p [first,last) to the range
-   *  beginning at @p result, except that only the first element is copied
-   *  from groups of consecutive elements that compare equal.
-   *  unique_copy() is stable, so the relative order of elements that are
-   *  copied is unchanged.
-  */
-  template<typename _InputIterator, typename _OutputIterator>
-    inline _OutputIterator
-    unique_copy(_InputIterator __first, _InputIterator __last,
-               _OutputIterator __result)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator>::value_type>)
-      __glibcxx_function_requires(_EqualityComparableConcept<
-           typename iterator_traits<_InputIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
-
-      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.
-   *  @param  result       An output iterator.
-   *  @param  binary_pred  A binary predicate.
-   *  @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 for which @p binary_pred returns
-   *  true.
-   *  unique_copy() is stable, so the relative order of elements that are
-   *  copied is unchanged.
-  */
-  template<typename _InputIterator, typename _OutputIterator, typename _BinaryPredicate>
-    inline _OutputIterator
-    unique_copy(_InputIterator __first, _InputIterator __last,
-               _OutputIterator __result,
-               _BinaryPredicate __binary_pred)
-    {
-      // concept requirements -- predicates checked later
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
-
-      typedef typename iterator_traits<_OutputIterator>::iterator_category _IterType;
-
-      if (__first == __last) return __result;
-      return std::__unique_copy(__first, __last, __result, __binary_pred, _IterType());
-    }
-
-  /**
-   *  @brief Remove consecutive duplicate values from a sequence.
-   *  @param  first  A forward iterator.
-   *  @param  last   A forward iterator.
-   *  @return  An iterator designating the end of the resulting sequence.
-   *
-   *  Removes all but the first element from each group of consecutive
-   *  values that compare equal.
-   *  unique() is stable, so the relative order of elements that are
-   *  not removed is unchanged.
-   *  Elements between the end of the resulting sequence and @p last
-   *  are still present, but their value is unspecified.
-  */
-  template<typename _ForwardIterator>
-    _ForwardIterator
-    unique(_ForwardIterator __first, _ForwardIterator __last)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_EqualityComparableConcept<
-                    typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
-
-      // Skip the beginning, if already unique.
-      __first = std::adjacent_find(__first, __last);
-      if (__first == __last)
-       return __last;
-
-      // Do the real copy work.
-      _ForwardIterator __dest = __first;
-      ++__first;
-      while (++__first != __last)
-       if (!(*__dest == *__first))
-         *++__dest = *__first;
-      return ++__dest;
-    }
-
-  /**
-   *  @brief Remove consecutive values from a sequence using a predicate.
-   *  @param  first        A forward iterator.
-   *  @param  last         A forward iterator.
-   *  @param  binary_pred  A binary predicate.
-   *  @return  An iterator designating the end of the resulting sequence.
-   *
-   *  Removes all but the first element from each group of consecutive
-   *  values for which @p binary_pred returns true.
-   *  unique() is stable, so the relative order of elements that are
-   *  not removed is unchanged.
-   *  Elements between the end of the resulting sequence and @p last
-   *  are still present, but their value is unspecified.
+   *  This is an uglified
+   *  unique_copy(_InputIterator, _InputIterator, _OutputIterator,
+   *              _BinaryPredicate)
+   *  overloaded for input iterators and forward iterator as result.
   */
-  template<typename _ForwardIterator, typename _BinaryPredicate>
+  template<typename _InputIterator, typename _ForwardIterator,
+          typename _BinaryPredicate>
     _ForwardIterator
-    unique(_ForwardIterator __first, _ForwardIterator __last,
-           _BinaryPredicate __binary_pred)
+    __unique_copy(_InputIterator __first, _InputIterator __last,
+                 _ForwardIterator __result, _BinaryPredicate __binary_pred,
+                 input_iterator_tag, forward_iterator_tag)
     {
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
+      // concept requirements -- iterators already checked
       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
-               typename iterator_traits<_ForwardIterator>::value_type,
-               typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
-
-      // Skip the beginning, if already unique.
-      __first = std::adjacent_find(__first, __last, __binary_pred);
-      if (__first == __last)
-       return __last;
+         typename iterator_traits<_ForwardIterator>::value_type,
+         typename iterator_traits<_InputIterator>::value_type>)
 
-      // Do the real copy work.
-      _ForwardIterator __dest = __first;
-      ++__first;
+      *__result = *__first;
       while (++__first != __last)
-       if (!__binary_pred(*__dest, *__first))
-         *++__dest = *__first;
-      return ++__dest;
+       if (!bool(__binary_pred(*__result, *__first)))
+         *++__result = *__first;
+      return ++__result;
     }
 
   /**
-   *  @if maint
-   *  This is an uglified reverse(_BidirectionalIterator, _BidirectionalIterator)
+   *  This is an uglified reverse(_BidirectionalIterator,
+   *                              _BidirectionalIterator)
    *  overloaded for bidirectional iterators.
-   *  @endif
   */
   template<typename _BidirectionalIterator>
     void
     __reverse(_BidirectionalIterator __first, _BidirectionalIterator __last,
-                         bidirectional_iterator_tag)
+             bidirectional_iterator_tag)
     {
       while (true)
        if (__first == __last || __first == --__last)
          return;
        else
-         std::iter_swap(__first++, __last);
+         {
+           std::iter_swap(__first, __last);
+           ++__first;
+         }
     }
 
   /**
-   *  @if maint
-   *  This is an uglified reverse(_BidirectionalIterator, _BidirectionalIterator)
-   *  overloaded for bidirectional iterators.
-   *  @endif
+   *  This is an uglified reverse(_BidirectionalIterator,
+   *                              _BidirectionalIterator)
+   *  overloaded for random access iterators.
   */
   template<typename _RandomAccessIterator>
     void
     __reverse(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                         random_access_iterator_tag)
+             random_access_iterator_tag)
     {
+      if (__first == __last)
+       return;
+      --__last;
       while (__first < __last)
-       std::iter_swap(__first++, --__last);
+       {
+         std::iter_swap(__first, __last);
+         ++__first;
+         --__last;
+       }
     }
 
   /**
    *  @brief Reverse a sequence.
+   *  @ingroup mutating_algorithms
    *  @param  first  A bidirectional iterator.
    *  @param  last   A bidirectional iterator.
    *  @return   reverse() returns no value.
@@ -1399,13 +1436,14 @@ namespace std
     {
       // concept requirements
       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
-                   _BidirectionalIterator>)
+                                 _BidirectionalIterator>)
       __glibcxx_requires_valid_range(__first, __last);
       std::__reverse(__first, __last, std::__iterator_category(__first));
     }
 
   /**
    *  @brief Copy a sequence, reversing its elements.
+   *  @ingroup mutating_algorithms
    *  @param  first   A bidirectional iterator.
    *  @param  last    A bidirectional iterator.
    *  @param  result  An output iterator.
@@ -1422,79 +1460,78 @@ namespace std
   template<typename _BidirectionalIterator, typename _OutputIterator>
     _OutputIterator
     reverse_copy(_BidirectionalIterator __first, _BidirectionalIterator __last,
-                            _OutputIterator __result)
+                _OutputIterator __result)
     {
       // concept requirements
-      __glibcxx_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>)
+      __glibcxx_function_requires(_BidirectionalIteratorConcept<
+                                 _BidirectionalIterator>)
       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
                typename iterator_traits<_BidirectionalIterator>::value_type>)
       __glibcxx_requires_valid_range(__first, __last);
 
-      while (__first != __last) {
-       --__last;
-       *__result = *__last;
-       ++__result;
-      }
+      while (__first != __last)
+       {
+         --__last;
+         *__result = *__last;
+         ++__result;
+       }
       return __result;
     }
 
-
   /**
-   *  @if maint
    *  This is a helper function for the rotate algorithm specialized on RAIs.
    *  It returns the greatest common divisor of two integer values.
-   *  @endif
   */
   template<typename _EuclideanRingElement>
     _EuclideanRingElement
     __gcd(_EuclideanRingElement __m, _EuclideanRingElement __n)
     {
-      while (__n != 0) {
-       _EuclideanRingElement __t = __m % __n;
-       __m = __n;
-       __n = __t;
-      }
+      while (__n != 0)
+       {
+         _EuclideanRingElement __t = __m % __n;
+         __m = __n;
+         __n = __t;
+       }
       return __m;
     }
 
-  /**
-   *  @if maint
-   *  This is a helper function for the rotate algorithm.
-   *  @endif
-  */
+  /// This is a helper function for the rotate algorithm.
   template<typename _ForwardIterator>
     void
     __rotate(_ForwardIterator __first,
             _ForwardIterator __middle,
             _ForwardIterator __last,
-             forward_iterator_tag)
+            forward_iterator_tag)
     {
-      if ((__first == __middle) || (__last  == __middle))
+      if (__first == __middle || __last  == __middle)
        return;
 
       _ForwardIterator __first2 = __middle;
-      do {
-       swap(*__first++, *__first2++);
-       if (__first == __middle)
-         __middle = __first2;
-      } while (__first2 != __last);
+      do
+       {
+         std::iter_swap(__first, __first2);
+         ++__first;
+         ++__first2;
+         if (__first == __middle)
+           __middle = __first2;
+       }
+      while (__first2 != __last);
 
       __first2 = __middle;
 
-      while (__first2 != __last) {
-       swap(*__first++, *__first2++);
-       if (__first == __middle)
-         __middle = __first2;
-       else if (__first2 == __last)
-         __first2 = __middle;
-      }
+      while (__first2 != __last)
+       {
+         std::iter_swap(__first, __first2);
+         ++__first;
+         ++__first2;
+         if (__first == __middle)
+           __middle = __first2;
+         else if (__first2 == __last)
+           __first2 = __middle;
+       }
     }
 
-  /**
-   *  @if maint
-   *  This is a helper function for the rotate algorithm.
-   *  @endif
-  */
+   /// This is a helper function for the rotate algorithm.
   template<typename _BidirectionalIterator>
     void
     __rotate(_BidirectionalIterator __first,
@@ -1504,30 +1541,27 @@ namespace std
     {
       // concept requirements
       __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
-           _BidirectionalIterator>)
+                                 _BidirectionalIterator>)
 
-      if ((__first == __middle) || (__last  == __middle))
+      if (__first == __middle || __last  == __middle)
        return;
 
       std::__reverse(__first,  __middle, bidirectional_iterator_tag());
       std::__reverse(__middle, __last,   bidirectional_iterator_tag());
 
       while (__first != __middle && __middle != __last)
-       swap(*__first++, *--__last);
+       {
+         std::iter_swap(__first, --__last);
+         ++__first;
+       }
 
-      if (__first == __middle) {
+      if (__first == __middle)
        std::__reverse(__middle, __last,   bidirectional_iterator_tag());
-      }
-      else {
+      else
        std::__reverse(__first,  __middle, bidirectional_iterator_tag());
-      }
     }
 
-  /**
-   *  @if maint
-   *  This is a helper function for the rotate algorithm.
-   *  @endif
-  */
+  /// This is a helper function for the rotate algorithm.
   template<typename _RandomAccessIterator>
     void
     __rotate(_RandomAccessIterator __first,
@@ -1537,60 +1571,80 @@ namespace std
     {
       // concept requirements
       __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
-           _RandomAccessIterator>)
+                                 _RandomAccessIterator>)
 
-      if ((__first == __middle) || (__last  == __middle))
+      if (__first == __middle || __last  == __middle)
        return;
 
-      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
+      typedef typename iterator_traits<_RandomAccessIterator>::difference_type
+       _Distance;
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type
+       _ValueType;
 
       _Distance __n = __last   - __first;
       _Distance __k = __middle - __first;
-      _Distance __l = __n - __k;
-
-      if (__k == __l) {
-       std::swap_ranges(__first, __middle, __middle);
-       return;
-      }
 
-      _Distance __d = __gcd(__n, __k);
-
-      for (_Distance __i = 0; __i < __d; __i++) {
-       _ValueType __tmp = *__first;
-       _RandomAccessIterator __p = __first;
-
-       if (__k < __l) {
-         for (_Distance __j = 0; __j < __l/__d; __j++) {
-           if (__p > __first + __l) {
-             *__p = *(__p - __l);
-             __p -= __l;
-           }
-
-           *__p = *(__p + __k);
-           __p += __k;
-         }
+      if (__k == __n - __k)
+       {
+         std::swap_ranges(__first, __middle, __middle);
+         return;
        }
 
-       else {
-         for (_Distance __j = 0; __j < __k/__d - 1; __j ++) {
-           if (__p < __last - __k) {
-             *__p = *(__p + __k);
-             __p += __k;
+      _RandomAccessIterator __p = __first;
+
+      for (;;)
+       {
+         if (__k < __n - __k)
+           {
+             if (__is_pod(_ValueType) && __k == 1)
+               {
+                 _ValueType __t = _GLIBCXX_MOVE(*__p);
+                 _GLIBCXX_MOVE3(__p + 1, __p + __n, __p);
+                 *(__p + __n - 1) = _GLIBCXX_MOVE(__t);
+                 return;
+               }
+             _RandomAccessIterator __q = __p + __k;
+             for (_Distance __i = 0; __i < __n - __k; ++ __i)
+               {
+                 std::iter_swap(__p, __q);
+                 ++__p;
+                 ++__q;
+               }
+             __n %= __k;
+             if (__n == 0)
+               return;
+             std::swap(__n, __k);
+             __k = __n - __k;
+           }
+         else
+           {
+             __k = __n - __k;
+             if (__is_pod(_ValueType) && __k == 1)
+               {
+                 _ValueType __t = _GLIBCXX_MOVE(*(__p + __n - 1));
+                 _GLIBCXX_MOVE_BACKWARD3(__p, __p + __n - 1, __p + __n);
+                 *__p = _GLIBCXX_MOVE(__t);
+                 return;
+               }
+             _RandomAccessIterator __q = __p + __n;
+             __p = __q - __k;
+             for (_Distance __i = 0; __i < __n - __k; ++ __i)
+               {
+                 --__p;
+                 --__q;
+                 std::iter_swap(__p, __q);
+               }
+             __n %= __k;
+             if (__n == 0)
+               return;
+             std::swap(__n, __k);
            }
-
-           *__p = * (__p - __l);
-           __p -= __l;
-         }
        }
-
-       *__p = __tmp;
-       ++__first;
-      }
     }
 
   /**
    *  @brief Rotate the elements of a sequence.
+   *  @ingroup mutating_algorithms
    *  @param  first   A forward iterator.
    *  @param  middle  A forward iterator.
    *  @param  last    A forward iterator.
@@ -1609,19 +1663,23 @@ namespace std
   */
   template<typename _ForwardIterator>
     inline void
-    rotate(_ForwardIterator __first, _ForwardIterator __middle, _ForwardIterator __last)
+    rotate(_ForwardIterator __first, _ForwardIterator __middle,
+          _ForwardIterator __last)
     {
       // concept requirements
-      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
+                                 _ForwardIterator>)
       __glibcxx_requires_valid_range(__first, __middle);
       __glibcxx_requires_valid_range(__middle, __last);
 
-      typedef typename iterator_traits<_ForwardIterator>::iterator_category _IterType;
+      typedef typename iterator_traits<_ForwardIterator>::iterator_category
+       _IterType;
       std::__rotate(__first, __middle, __last, _IterType());
     }
 
   /**
    *  @brief Copy a sequence, rotating its elements.
+   *  @ingroup mutating_algorithms
    *  @param  first   A forward iterator.
    *  @param  middle  A forward iterator.
    *  @param  last    A forward iterator.
@@ -1649,252 +1707,146 @@ namespace std
       __glibcxx_requires_valid_range(__first, __middle);
       __glibcxx_requires_valid_range(__middle, __last);
 
-      return std::copy(__first, __middle, copy(__middle, __last, __result));
-    }
-
-
-  /**
-   *  @if maint
-   *  Return a random number in the range [0, __n).  This function encapsulates
-   *  whether we're using rand (part of the standard C library) or lrand48
-   *  (not standard, but a much better choice whenever it's available).
-   *
-   *  XXX There is no corresponding encapsulation fn to seed the generator.
-   *  @endif
-  */
-  template<typename _Distance>
-    inline _Distance
-    __random_number(_Distance __n)
-    {
-  #ifdef _GLIBCXX_HAVE_LRAND48
-      return lrand48() % __n;
-  #else
-      return rand() % __n;
-  #endif
-    }
-
-
-  /**
-   *  @brief Randomly shuffle the elements of a sequence.
-   *  @param  first   A forward iterator.
-   *  @param  last    A forward iterator.
-   *  @return  Nothing.
-   *
-   *  Reorder the elements in the range @p [first,last) using a random
-   *  distribution, so that every possible ordering of the sequence is
-   *  equally likely.
-  */
-  template<typename _RandomAccessIterator>
-    inline void
-    random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
-           _RandomAccessIterator>)
-      __glibcxx_requires_valid_range(__first, __last);
-
-      if (__first == __last) return;
-      for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
-       std::iter_swap(__i, __first + std::__random_number((__i - __first) + 1));
-    }
-
-  /**
-   *  @brief Shuffle the elements of a sequence using a random number
-   *         generator.
-   *  @param  first   A forward iterator.
-   *  @param  last    A forward iterator.
-   *  @param  rand    The RNG functor or function.
-   *  @return  Nothing.
-   *
-   *  Reorders the elements in the range @p [first,last) using @p rand to
-   *  provide a random distribution. Calling @p rand(N) for a positive
-   *  integer @p N should return a randomly chosen integer from the
-   *  range [0,N).
-  */
-  template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
-    void
-    random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                  _RandomNumberGenerator& __rand)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
-           _RandomAccessIterator>)
-      __glibcxx_requires_valid_range(__first, __last);
-
-      if (__first == __last) return;
-      for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
-       std::iter_swap(__i, __first + __rand((__i - __first) + 1));
+      return std::copy(__first, __middle,
+                       std::copy(__middle, __last, __result));
     }
 
-
-  /**
-   *  @if maint
-   *  This is a helper function...
-   *  @endif
-  */
+  /// This is a helper function...
   template<typename _ForwardIterator, typename _Predicate>
     _ForwardIterator
     __partition(_ForwardIterator __first, _ForwardIterator __last,
-               _Predicate   __pred,
-               forward_iterator_tag)
+               _Predicate __pred, forward_iterator_tag)
     {
-      if (__first == __last) return __first;
+      if (__first == __last)
+       return __first;
 
       while (__pred(*__first))
-       if (++__first == __last) return __first;
+       if (++__first == __last)
+         return __first;
 
       _ForwardIterator __next = __first;
 
       while (++__next != __last)
-       if (__pred(*__next)) {
-         swap(*__first, *__next);
-         ++__first;
-       }
+       if (__pred(*__next))
+         {
+           std::iter_swap(__first, __next);
+           ++__first;
+         }
 
       return __first;
     }
 
-  /**
-   *  @if maint
-   *  This is a helper function...
-   *  @endif
-  */
+  /// This is a helper function...
   template<typename _BidirectionalIterator, typename _Predicate>
     _BidirectionalIterator
     __partition(_BidirectionalIterator __first, _BidirectionalIterator __last,
-               _Predicate __pred,
-               bidirectional_iterator_tag)
+               _Predicate __pred, bidirectional_iterator_tag)
     {
-      while (true) {
-       while (true)
-         if (__first == __last)
-           return __first;
-         else if (__pred(*__first))
-           ++__first;
-         else
-           break;
-       --__last;
-       while (true)
-         if (__first == __last)
-           return __first;
-         else if (!__pred(*__last))
-           --__last;
-         else
-           break;
-       std::iter_swap(__first, __last);
-       ++__first;
-      }
-    }
-
-  /**
-   *  @brief Move elements for which a predicate is true to the beginning
-   *         of a sequence.
-   *  @param  first   A forward iterator.
-   *  @param  last    A forward iterator.
-   *  @param  pred    A predicate functor.
-   *  @return  An iterator @p middle such that @p pred(i) is true for each
-   *  iterator @p i in the range @p [first,middle) and false for each @p i
-   *  in the range @p [middle,last).
-   *  
-   *  @p pred must not modify its operand. @p partition() does not preserve
-   *  the relative ordering of elements in each group, use
-   *  @p stable_partition() if this is needed.
-  */
-  template<typename _ForwardIterator, typename _Predicate>
-    inline _ForwardIterator
-    partition(_ForwardIterator __first, _ForwardIterator __last,
-             _Predicate   __pred)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
-           typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
-
-      return std::__partition(__first, __last, __pred, std::__iterator_category(__first));
+      while (true)
+       {
+         while (true)
+           if (__first == __last)
+             return __first;
+           else if (__pred(*__first))
+             ++__first;
+           else
+             break;
+         --__last;
+         while (true)
+           if (__first == __last)
+             return __first;
+           else if (!bool(__pred(*__last)))
+             --__last;
+           else
+             break;
+         std::iter_swap(__first, __last);
+         ++__first;
+       }
     }
 
+  // partition
 
-  /**
-   *  @if maint
-   *  This is a helper function...
-   *  @endif
-  */
+  /// This is a helper function...
   template<typename _ForwardIterator, typename _Predicate, typename _Distance>
     _ForwardIterator
-    __inplace_stable_partition(_ForwardIterator __first, _ForwardIterator __last,
+    __inplace_stable_partition(_ForwardIterator __first,
+                              _ForwardIterator __last,
                               _Predicate __pred, _Distance __len)
     {
       if (__len == 1)
        return __pred(*__first) ? __last : __first;
       _ForwardIterator __middle = __first;
       std::advance(__middle, __len / 2);
-      _ForwardIterator __begin = std::__inplace_stable_partition(__first, __middle,
+      _ForwardIterator __begin = std::__inplace_stable_partition(__first,
+                                                                __middle,
                                                                 __pred,
                                                                 __len / 2);
       _ForwardIterator __end = std::__inplace_stable_partition(__middle, __last,
                                                               __pred,
-                                                              __len - __len / 2);
+                                                              __len
+                                                              - __len / 2);
       std::rotate(__begin, __middle, __end);
       std::advance(__begin, std::distance(__middle, __end));
       return __begin;
     }
 
-  /**
-   *  @if maint
-   *  This is a helper function...
-   *  @endif
-  */
+  /// This is a helper function...
   template<typename _ForwardIterator, typename _Pointer, typename _Predicate,
           typename _Distance>
     _ForwardIterator
-    __stable_partition_adaptive(_ForwardIterator __first, _ForwardIterator __last,
+    __stable_partition_adaptive(_ForwardIterator __first,
+                               _ForwardIterator __last,
                                _Predicate __pred, _Distance __len,
                                _Pointer __buffer,
                                _Distance __buffer_size)
     {
-      if (__len <= __buffer_size) {
-       _ForwardIterator __result1 = __first;
-       _Pointer __result2 = __buffer;
-       for ( ; __first != __last ; ++__first)
-         if (__pred(*__first)) {
-           *__result1 = *__first;
-           ++__result1;
-         }
-         else {
-           *__result2 = *__first;
-           ++__result2;
-         }
-       std::copy(__buffer, __result2, __result1);
-       return __result1;
-      }
-      else {
-       _ForwardIterator __middle = __first;
-       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;
-      }
+      if (__len <= __buffer_size)
+       {
+         _ForwardIterator __result1 = __first;
+         _Pointer __result2 = __buffer;
+         for (; __first != __last; ++__first)
+           if (__pred(*__first))
+             {
+               *__result1 = _GLIBCXX_MOVE(*__first);
+               ++__result1;
+             }
+           else
+             {
+               *__result2 = _GLIBCXX_MOVE(*__first);
+               ++__result2;
+             }
+         _GLIBCXX_MOVE3(__buffer, __result2, __result1);
+         return __result1;
+       }
+      else
+       {
+         _ForwardIterator __middle = __first;
+         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;
+       }
     }
 
   /**
    *  @brief Move elements for which a predicate is true to the beginning
    *         of a sequence, preserving relative ordering.
+   *  @ingroup mutating_algorithms
    *  @param  first   A forward iterator.
    *  @param  last    A forward iterator.
    *  @param  pred    A predicate functor.
    *  @return  An iterator @p middle such that @p pred(i) is true for each
    *  iterator @p i in the range @p [first,middle) and false for each @p i
    *  in the range @p [middle,last).
-   *  
+   *
    *  Performs the same function as @p partition() with the additional
    *  guarantee that the relative ordering of elements in each group is
    *  preserved, so any two elements @p x and @p y in the range
@@ -1907,7 +1859,8 @@ namespace std
                     _Predicate __pred)
     {
       // concept requirements
-      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
+                                 _ForwardIterator>)
       __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
            typename iterator_traits<_ForwardIterator>::value_type>)
       __glibcxx_requires_valid_range(__first, __last);
@@ -1915,322 +1868,58 @@ namespace std
       if (__first == __last)
        return __first;
       else
-      {
-       typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
-       typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
-
-       _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first, __last);
+       {
+         typedef typename iterator_traits<_ForwardIterator>::value_type
+           _ValueType;
+         typedef typename iterator_traits<_ForwardIterator>::difference_type
+           _DistanceType;
+
+         _Temporary_buffer<_ForwardIterator, _ValueType> __buf(__first,
+                                                               __last);
        if (__buf.size() > 0)
-         return std::__stable_partition_adaptive(__first, __last, __pred,
-                                                 _DistanceType(__buf.requested_size()),
-                                                 __buf.begin(), __buf.size());
-       else
-         return std::__inplace_stable_partition(__first, __last, __pred,
-                                                _DistanceType(__buf.requested_size()));
-      }
-    }
-
-  /**
-   *  @if maint
-   *  This is a helper function...
-   *  @endif
-  */
-  template<typename _RandomAccessIterator, typename _Tp>
-    _RandomAccessIterator
-    __unguarded_partition(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                         _Tp __pivot)
-    {
-      while (true) {
-       while (*__first < __pivot)
-         ++__first;
-       --__last;
-       while (__pivot < *__last)
-         --__last;
-       if (!(__first < __last))
-         return __first;
-       std::iter_swap(__first, __last);
-       ++__first;
-      }
-    }
-
-  /**
-   *  @if maint
-   *  This is a helper function...
-   *  @endif
-  */
-  template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
-    _RandomAccessIterator
-    __unguarded_partition(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                         _Tp __pivot, _Compare __comp)
-    {
-      while (true) {
-       while (__comp(*__first, __pivot))
-         ++__first;
-       --__last;
-       while (__comp(__pivot, *__last))
-         --__last;
-       if (!(__first < __last))
-         return __first;
-       std::iter_swap(__first, __last);
-       ++__first;
-      }
-    }
-
-
-  /**
-   *  @if maint
-   *  @doctodo
-   *  This controls some aspect of the sort routines.
-   *  @endif
-  */
-  enum { _S_threshold = 16 };
-
-  /**
-   *  @if maint
-   *  This is a helper function for the sort routine.
-   *  @endif
-  */
-  template<typename _RandomAccessIterator, typename _Tp>
-    void
-    __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val)
-    {
-      _RandomAccessIterator __next = __last;
-      --__next;
-      while (__val < *__next) {
-       *__last = *__next;
-       __last = __next;
-       --__next;
-      }
-      *__last = __val;
-    }
-
-  /**
-   *  @if maint
-   *  This is a helper function for the sort routine.
-   *  @endif
-  */
-  template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
-    void
-    __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val, _Compare __comp)
-    {
-      _RandomAccessIterator __next = __last;
-      --__next;
-      while (__comp(__val, *__next)) {
-       *__last = *__next;
-       __last = __next;
-       --__next;
-      }
-      *__last = __val;
-    }
-
-  /**
-   *  @if maint
-   *  This is a helper function for the sort routine.
-   *  @endif
-  */
-  template<typename _RandomAccessIterator>
-    void
-    __insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
-    {
-      if (__first == __last) return;
-
-      for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
-      {
-       typename iterator_traits<_RandomAccessIterator>::value_type __val = *__i;
-       if (__val < *__first) {
-         std::copy_backward(__first, __i, __i + 1);
-         *__first = __val;
-       }
+         return
+           std::__stable_partition_adaptive(__first, __last, __pred,
+                                         _DistanceType(__buf.requested_size()),
+                                         __buf.begin(),
+                                         _DistanceType(__buf.size()));
        else
-         std::__unguarded_linear_insert(__i, __val);
-      }
-    }
-
-  /**
-   *  @if maint
-   *  This is a helper function for the sort routine.
-   *  @endif
-  */
-  template<typename _RandomAccessIterator, typename _Compare>
-    void
-    __insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                    _Compare __comp)
-    {
-      if (__first == __last) return;
-
-      for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
-      {
-       typename iterator_traits<_RandomAccessIterator>::value_type __val = *__i;
-       if (__comp(__val, *__first)) {
-         std::copy_backward(__first, __i, __i + 1);
-         *__first = __val;
+         return
+           std::__inplace_stable_partition(__first, __last, __pred,
+                                        _DistanceType(__buf.requested_size()));
        }
-       else
-         std::__unguarded_linear_insert(__i, __val, __comp);
-      }
-    }
-
-  /**
-   *  @if maint
-   *  This is a helper function for the sort routine.
-   *  @endif
-  */
-  template<typename _RandomAccessIterator>
-    inline void
-    __unguarded_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
-    {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
-
-      for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
-       std::__unguarded_linear_insert(__i, _ValueType(*__i));
-    }
-
-  /**
-   *  @if maint
-   *  This is a helper function for the sort routine.
-   *  @endif
-  */
-  template<typename _RandomAccessIterator, typename _Compare>
-    inline void
-    __unguarded_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                              _Compare __comp)
-    {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
-
-      for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
-       std::__unguarded_linear_insert(__i, _ValueType(*__i), __comp);
-    }
-
-  /**
-   *  @if maint
-   *  This is a helper function for the sort routine.
-   *  @endif
-  */
-  template<typename _RandomAccessIterator>
-    void
-    __final_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
-    {
-      if (__last - __first > _S_threshold) {
-       std::__insertion_sort(__first, __first + _S_threshold);
-       std::__unguarded_insertion_sort(__first + _S_threshold, __last);
-      }
-      else
-       std::__insertion_sort(__first, __last);
-    }
-
-  /**
-   *  @if maint
-   *  This is a helper function for the sort routine.
-   *  @endif
-  */
-  template<typename _RandomAccessIterator, typename _Compare>
-    void
-    __final_insertion_sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                          _Compare __comp)
-    {
-      if (__last - __first > _S_threshold) {
-       std::__insertion_sort(__first, __first + _S_threshold, __comp);
-       std::__unguarded_insertion_sort(__first + _S_threshold, __last, __comp);
-      }
-      else
-       std::__insertion_sort(__first, __last, __comp);
-    }
-
-  /**
-   *  @if maint
-   *  This is a helper function for the sort routine.
-   *  @endif
-  */
-  template<typename _Size>
-    inline _Size
-    __lg(_Size __n)
-    {
-      _Size __k;
-      for (__k = 0; __n != 1; __n >>= 1) ++__k;
-      return __k;
     }
 
-  /**
-   *  @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.
-  */
+  /// This is a helper function for the sort routines.
   template<typename _RandomAccessIterator>
     void
-    partial_sort(_RandomAccessIterator __first,
-                _RandomAccessIterator __middle,
-                _RandomAccessIterator __last)
+    __heap_select(_RandomAccessIterator __first,
+                 _RandomAccessIterator __middle,
+                 _RandomAccessIterator __last)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
-
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
-           _RandomAccessIterator>)
-      __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
-      __glibcxx_requires_valid_range(__first, __middle);
-      __glibcxx_requires_valid_range(__middle, __last);
-
       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);
+         std::__pop_heap(__first, __middle, __i);
     }
 
-  /**
-   *  @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.
-  */
+  /// This is a helper function for the sort routines.
   template<typename _RandomAccessIterator, typename _Compare>
     void
-    partial_sort(_RandomAccessIterator __first,
-                _RandomAccessIterator __middle,
-                _RandomAccessIterator __last,
-                _Compare __comp)
+    __heap_select(_RandomAccessIterator __first,
+                 _RandomAccessIterator __middle,
+                 _RandomAccessIterator __last, _Compare __comp)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
-
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
-           _RandomAccessIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-                                                         _ValueType, _ValueType>)
-      __glibcxx_requires_valid_range(__first, __middle);
-      __glibcxx_requires_valid_range(__middle, __last);
-
       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);
+         std::__pop_heap(__first, __middle, __i, __comp);
     }
 
+  // partial_sort
+
   /**
    *  @brief Copy the smallest elements of a sequence.
+   *  @ingroup sorting_algorithms
    *  @param  first   An iterator.
    *  @param  last    Another iterator.
    *  @param  result_first   A random-access iterator.
@@ -2252,33 +1941,42 @@ namespace std
                      _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;
+      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
       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_function_requires(_ConvertibleConcept<_InputValueType, _OutputValueType>)
+      __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
+                                 _OutputValueType>)
+      __glibcxx_function_requires(_LessThanOpConcept<_InputValueType,
+                                                    _OutputValueType>)
       __glibcxx_function_requires(_LessThanComparableConcept<_OutputValueType>)
-      __glibcxx_function_requires(_LessThanComparableConcept<_InputValueType>)
       __glibcxx_requires_valid_range(__first, __last);
       __glibcxx_requires_valid_range(__result_first, __result_last);
 
-      if (__result_first == __result_last) return __result_last;
+      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;
-      }
+      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;
-      }
+      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;
     }
@@ -2286,6 +1984,7 @@ namespace std
   /**
    *  @brief Copy the smallest elements of a sequence using a predicate for
    *         comparison.
+   *  @ingroup sorting_algorithms
    *  @param  first   An input iterator.
    *  @param  last    Another input iterator.
    *  @param  result_first   A random-access iterator.
@@ -2309,214 +2008,379 @@ namespace std
                      _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;
+      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
       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
-      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>)
-      __glibcxx_function_requires(_ConvertibleConcept<_InputValueType, _OutputValueType>)
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+                                 _RandomAccessIterator>)
+      __glibcxx_function_requires(_ConvertibleConcept<_InputValueType,
+                                 _OutputValueType>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _InputValueType, _OutputValueType>)
       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
                                  _OutputValueType, _OutputValueType>)
       __glibcxx_requires_valid_range(__first, __last);
       __glibcxx_requires_valid_range(__result_first, __result_last);
 
-      if (__result_first == __result_last) return __result_last;
+      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;
-      }
+      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;
-      }
+      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
-  */
-  template<typename _RandomAccessIterator, typename _Size>
+  /// This is a helper function for the sort routine.
+  template<typename _RandomAccessIterator>
     void
-    __introsort_loop(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                    _Size __depth_limit)
+    __unguarded_linear_insert(_RandomAccessIterator __last)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
-
-      while (__last - __first > _S_threshold) {
-       if (__depth_limit == 0) {
-         std::partial_sort(__first, __last, __last);
-         return;
+      typename iterator_traits<_RandomAccessIterator>::value_type
+       __val = _GLIBCXX_MOVE(*__last);
+      _RandomAccessIterator __next = __last;
+      --__next;
+      while (__val < *__next)
+       {
+         *__last = _GLIBCXX_MOVE(*__next);
+         __last = __next;
+         --__next;
        }
-       --__depth_limit;
-       _RandomAccessIterator __cut =
-         std::__unguarded_partition(__first, __last,
-                                    _ValueType(std::__median(*__first,
-                                                             *(__first + (__last - __first)/2),
-                                                             *(__last - 1))));
-       std::__introsort_loop(__cut, __last, __depth_limit);
-       __last = __cut;
-      }
+      *__last = _GLIBCXX_MOVE(__val);
     }
 
-  /**
-   *  @if maint
-   *  This is a helper function for the sort routine.
-   *  @endif
-  */
-  template<typename _RandomAccessIterator, typename _Size, typename _Compare>
+  /// This is a helper function for the sort routine.
+  template<typename _RandomAccessIterator, typename _Compare>
     void
-    __introsort_loop(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                    _Size __depth_limit, _Compare __comp)
+    __unguarded_linear_insert(_RandomAccessIterator __last,
+                             _Compare __comp)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
-
-      while (__last - __first > _S_threshold) {
-       if (__depth_limit == 0) {
-         std::partial_sort(__first, __last, __last, __comp);
-         return;
+      typename iterator_traits<_RandomAccessIterator>::value_type
+       __val = _GLIBCXX_MOVE(*__last);
+      _RandomAccessIterator __next = __last;
+      --__next;
+      while (__comp(__val, *__next))
+       {
+         *__last = _GLIBCXX_MOVE(*__next);
+         __last = __next;
+         --__next;
        }
-       --__depth_limit;
-       _RandomAccessIterator __cut =
-         std::__unguarded_partition(__first, __last,
-                                    _ValueType(std::__median(*__first,
-                                                             *(__first + (__last - __first)/2),
-                                                             *(__last - 1), __comp)), __comp);
-       std::__introsort_loop(__cut, __last, __depth_limit, __comp);
-       __last = __cut;
-      }
+      *__last = _GLIBCXX_MOVE(__val);
     }
 
-  /**
-   *  @brief Sort the elements of a sequence.
-   *  @param  first   An iterator.
-   *  @param  last    Another iterator.
-   *  @return  Nothing.
-   *
-   *  Sorts the elements in the range @p [first,last) in ascending order,
-   *  such that @p *(i+1)<*i is false for each iterator @p i in the range
-   *  @p [first,last-1).
-   *
-   *  The relative ordering of equivalent elements is not preserved, use
-   *  @p stable_sort() if this is needed.
-  */
+  /// This is a helper function for the sort routine.
   template<typename _RandomAccessIterator>
-    inline void
-    sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
+    void
+    __insertion_sort(_RandomAccessIterator __first,
+                    _RandomAccessIterator __last)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
-
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
-           _RandomAccessIterator>)
-      __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
-      __glibcxx_requires_valid_range(__first, __last);
+      if (__first == __last)
+       return;
 
-      if (__first != __last) {
-       std::__introsort_loop(__first, __last, __lg(__last - __first) * 2);
-       std::__final_insertion_sort(__first, __last);
-      }
+      for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
+       {
+         if (*__i < *__first)
+           {
+             typename iterator_traits<_RandomAccessIterator>::value_type
+               __val = _GLIBCXX_MOVE(*__i);
+             _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
+             *__first = _GLIBCXX_MOVE(__val);
+           }
+         else
+           std::__unguarded_linear_insert(__i);
+       }
     }
 
-  /**
-   *  @brief Sort the elements of a sequence using a predicate for comparison.
-   *  @param  first   An iterator.
-   *  @param  last    Another iterator.
-   *  @param  comp    A comparison functor.
-   *  @return  Nothing.
-   *
-   *  Sorts the elements in the range @p [first,last) in ascending order,
-   *  such that @p comp(*(i+1),*i) is false for every iterator @p i in the
-   *  range @p [first,last-1).
-   *
-   *  The relative ordering of equivalent elements is not preserved, use
-   *  @p stable_sort() if this is needed.
-  */
+  /// This is a helper function for the sort routine.
   template<typename _RandomAccessIterator, typename _Compare>
+    void
+    __insertion_sort(_RandomAccessIterator __first,
+                    _RandomAccessIterator __last, _Compare __comp)
+    {
+      if (__first == __last) return;
+
+      for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
+       {
+         if (__comp(*__i, *__first))
+           {
+             typename iterator_traits<_RandomAccessIterator>::value_type
+               __val = _GLIBCXX_MOVE(*__i);
+             _GLIBCXX_MOVE_BACKWARD3(__first, __i, __i + 1);
+             *__first = _GLIBCXX_MOVE(__val);
+           }
+         else
+           std::__unguarded_linear_insert(__i, __comp);
+       }
+    }
+
+  /// This is a helper function for the sort routine.
+  template<typename _RandomAccessIterator>
     inline void
-    sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
+    __unguarded_insertion_sort(_RandomAccessIterator __first,
+                              _RandomAccessIterator __last)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type
+       _ValueType;
 
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
-           _RandomAccessIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _ValueType>)
-      __glibcxx_requires_valid_range(__first, __last);
+      for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
+       std::__unguarded_linear_insert(__i);
+    }
+
+  /// This is a helper function for the sort routine.
+  template<typename _RandomAccessIterator, typename _Compare>
+    inline void
+    __unguarded_insertion_sort(_RandomAccessIterator __first,
+                              _RandomAccessIterator __last, _Compare __comp)
+    {
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type
+       _ValueType;
 
-      if (__first != __last) {
-       std::__introsort_loop(__first, __last, __lg(__last - __first) * 2, __comp);
-       std::__final_insertion_sort(__first, __last, __comp);
-      }
+      for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
+       std::__unguarded_linear_insert(__i, __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
+   *  @doctodo
+   *  This controls some aspect of the sort routines.
   */
-  template<typename _ForwardIterator, typename _Tp>
-    _ForwardIterator
-    lower_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val)
+  enum { _S_threshold = 16 };
+
+  /// This is a helper function for the sort routine.
+  template<typename _RandomAccessIterator>
+    void
+    __final_insertion_sort(_RandomAccessIterator __first,
+                          _RandomAccessIterator __last)
     {
-      typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
-      typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
+      if (__last - __first > int(_S_threshold))
+       {
+         std::__insertion_sort(__first, __first + int(_S_threshold));
+         std::__unguarded_insertion_sort(__first + int(_S_threshold), __last);
+       }
+      else
+       std::__insertion_sort(__first, __last);
+    }
 
-      // 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.
-      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_SameTypeConcept<_Tp, _ValueType>)
-      __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
-      __glibcxx_requires_partitioned(__first, __last, __val);
+  /// This is a helper function for the sort routine.
+  template<typename _RandomAccessIterator, typename _Compare>
+    void
+    __final_insertion_sort(_RandomAccessIterator __first,
+                          _RandomAccessIterator __last, _Compare __comp)
+    {
+      if (__last - __first > int(_S_threshold))
+       {
+         std::__insertion_sort(__first, __first + int(_S_threshold), __comp);
+         std::__unguarded_insertion_sort(__first + int(_S_threshold), __last,
+                                         __comp);
+       }
+      else
+       std::__insertion_sort(__first, __last, __comp);
+    }
 
-      _DistanceType __len = std::distance(__first, __last);
-      _DistanceType __half;
-      _ForwardIterator __middle;
+  /// This is a helper function...
+  template<typename _RandomAccessIterator, typename _Tp>
+    _RandomAccessIterator
+    __unguarded_partition(_RandomAccessIterator __first,
+                         _RandomAccessIterator __last, const _Tp& __pivot)
+    {
+      while (true)
+       {
+         while (*__first < __pivot)
+           ++__first;
+         --__last;
+         while (__pivot < *__last)
+           --__last;
+         if (!(__first < __last))
+           return __first;
+         std::iter_swap(__first, __last);
+         ++__first;
+       }
+    }
 
-      while (__len > 0) {
-       __half = __len >> 1;
-       __middle = __first;
-       std::advance(__middle, __half);
-       if (*__middle < __val) {
-         __first = __middle;
+  /// This is a helper function...
+  template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
+    _RandomAccessIterator
+    __unguarded_partition(_RandomAccessIterator __first,
+                         _RandomAccessIterator __last,
+                         const _Tp& __pivot, _Compare __comp)
+    {
+      while (true)
+       {
+         while (__comp(*__first, __pivot))
+           ++__first;
+         --__last;
+         while (__comp(__pivot, *__last))
+           --__last;
+         if (!(__first < __last))
+           return __first;
+         std::iter_swap(__first, __last);
          ++__first;
-         __len = __len - __half - 1;
        }
-       else
-         __len = __half;
-      }
-      return __first;
     }
 
+  /// This is a helper function...
+  template<typename _RandomAccessIterator>
+    inline _RandomAccessIterator
+    __unguarded_partition_pivot(_RandomAccessIterator __first,
+                               _RandomAccessIterator __last)
+    {
+      _RandomAccessIterator __mid = __first + (__last - __first) / 2;
+      std::__move_median_first(__first, __mid, (__last - 1));
+      return std::__unguarded_partition(__first + 1, __last, *__first);
+    }
+
+
+  /// This is a helper function...
+  template<typename _RandomAccessIterator, typename _Compare>
+    inline _RandomAccessIterator
+    __unguarded_partition_pivot(_RandomAccessIterator __first,
+                               _RandomAccessIterator __last, _Compare __comp)
+    {
+      _RandomAccessIterator __mid = __first + (__last - __first) / 2;
+      std::__move_median_first(__first, __mid, (__last - 1), __comp);
+      return std::__unguarded_partition(__first + 1, __last, *__first, __comp);
+    }
+
+  /// This is a helper function for the sort routine.
+  template<typename _RandomAccessIterator, typename _Size>
+    void
+    __introsort_loop(_RandomAccessIterator __first,
+                    _RandomAccessIterator __last,
+                    _Size __depth_limit)
+    {
+      while (__last - __first > int(_S_threshold))
+       {
+         if (__depth_limit == 0)
+           {
+             _GLIBCXX_STD_P::partial_sort(__first, __last, __last);
+             return;
+           }
+         --__depth_limit;
+         _RandomAccessIterator __cut =
+           std::__unguarded_partition_pivot(__first, __last);
+         std::__introsort_loop(__cut, __last, __depth_limit);
+         __last = __cut;
+       }
+    }
+
+  /// This is a helper function for the sort routine.
+  template<typename _RandomAccessIterator, typename _Size, typename _Compare>
+    void
+    __introsort_loop(_RandomAccessIterator __first,
+                    _RandomAccessIterator __last,
+                    _Size __depth_limit, _Compare __comp)
+    {
+      while (__last - __first > int(_S_threshold))
+       {
+         if (__depth_limit == 0)
+           {
+             _GLIBCXX_STD_P::partial_sort(__first, __last, __last, __comp);
+             return;
+           }
+         --__depth_limit;
+         _RandomAccessIterator __cut =
+           std::__unguarded_partition_pivot(__first, __last, __comp);
+         std::__introsort_loop(__cut, __last, __depth_limit, __comp);
+         __last = __cut;
+       }
+    }
+
+  // sort
+
+  template<typename _RandomAccessIterator, typename _Size>
+    void
+    __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
+                 _RandomAccessIterator __last, _Size __depth_limit)
+    {
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type
+       _ValueType;
+
+      while (__last - __first > 3)
+       {
+         if (__depth_limit == 0)
+           {
+             std::__heap_select(__first, __nth + 1, __last);
+
+             // Place the nth largest element in its final position.
+             std::iter_swap(__first, __nth);
+             return;
+           }
+         --__depth_limit;
+         _RandomAccessIterator __cut =
+           std::__unguarded_partition_pivot(__first, __last);
+         if (__cut <= __nth)
+           __first = __cut;
+         else
+           __last = __cut;
+       }
+      std::__insertion_sort(__first, __last);
+    }
+
+  template<typename _RandomAccessIterator, typename _Size, typename _Compare>
+    void
+    __introselect(_RandomAccessIterator __first, _RandomAccessIterator __nth,
+                 _RandomAccessIterator __last, _Size __depth_limit,
+                 _Compare __comp)
+    {
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type
+       _ValueType;
+
+      while (__last - __first > 3)
+       {
+         if (__depth_limit == 0)
+           {
+             std::__heap_select(__first, __nth + 1, __last, __comp);
+             // Place the nth largest element in its final position.
+             std::iter_swap(__first, __nth);
+             return;
+           }
+         --__depth_limit;
+         _RandomAccessIterator __cut =
+           std::__unguarded_partition_pivot(__first, __last, __comp);
+         if (__cut <= __nth)
+           __first = __cut;
+         else
+           __last = __cut;
+       }
+      std::__insertion_sort(__first, __last, __comp);
+    }
+
+  // nth_element
+
+  // lower_bound moved to stl_algobase.h
+
   /**
    *  @brief Finds the first position in which @a val could be inserted
    *         without changing the ordering.
+   *  @ingroup binary_search_algorithms
    *  @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
+   *  @return An iterator pointing to the first element <em>not less
+   *           than</em> @a val, or end() if every element is less
+   *           than @a val.
+   *  @ingroup binary_search_algorithms
    *
    *  The comparison function should have the same effects on ordering as
    *  the function used for the initial sort.
@@ -2526,86 +2390,97 @@ namespace std
     lower_bound(_ForwardIterator __first, _ForwardIterator __last,
                const _Tp& __val, _Compare __comp)
     {
-      typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
-      typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
+      typedef typename iterator_traits<_ForwardIterator>::value_type
+       _ValueType;
+      typedef typename iterator_traits<_ForwardIterator>::difference_type
+       _DistanceType;
 
       // concept requirements
       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _Tp>)
-      __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType, _Tp>)
+      __glibcxx_requires_partitioned_lower_pred(__first, __last,
+                                               __val, __comp);
 
       _DistanceType __len = std::distance(__first, __last);
       _DistanceType __half;
       _ForwardIterator __middle;
 
-      while (__len > 0) {
-       __half = __len >> 1;
-       __middle = __first;
-       std::advance(__middle, __half);
-       if (__comp(*__middle, __val)) {
-         __first = __middle;
-         ++__first;
-         __len = __len - __half - 1;
+      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;
        }
-       else
-         __len = __half;
-      }
       return __first;
     }
 
   /**
    *  @brief Finds the last position in which @a val could be inserted
    *         without changing the ordering.
+   *  @ingroup binary_search_algorithms
    *  @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
+   *  @ingroup binary_search_algorithms
   */
   template<typename _ForwardIterator, typename _Tp>
     _ForwardIterator
-    upper_bound(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val)
+    upper_bound(_ForwardIterator __first, _ForwardIterator __last,
+               const _Tp& __val)
     {
-      typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
-      typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
+      typedef typename iterator_traits<_ForwardIterator>::value_type
+       _ValueType;
+      typedef typename iterator_traits<_ForwardIterator>::difference_type
+       _DistanceType;
 
       // concept requirements
-      // See comments on lower_bound.
       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_SameTypeConcept<_Tp, _ValueType>)
-      __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
-      __glibcxx_requires_partitioned(__first, __last, __val);
+      __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
+      __glibcxx_requires_partitioned_upper(__first, __last, __val);
 
       _DistanceType __len = std::distance(__first, __last);
       _DistanceType __half;
       _ForwardIterator __middle;
 
-      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;
+      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;
     }
 
   /**
    *  @brief Finds the last position in which @a val could be inserted
    *         without changing the ordering.
+   *  @ingroup binary_search_algorithms
    *  @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
+   *  @ingroup binary_search_algorithms
    *
    *  The comparison function should have the same effects on ordering as
    *  the function used for the initial sort.
@@ -2615,392 +2490,243 @@ namespace std
     upper_bound(_ForwardIterator __first, _ForwardIterator __last,
                const _Tp& __val, _Compare __comp)
     {
-      typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
-      typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
+      typedef typename iterator_traits<_ForwardIterator>::value_type
+       _ValueType;
+      typedef typename iterator_traits<_ForwardIterator>::difference_type
+       _DistanceType;
 
       // concept requirements
       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _Tp, _ValueType>)
-      __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _Tp, _ValueType>)
+      __glibcxx_requires_partitioned_upper_pred(__first, __last,
+                                               __val, __comp);
 
       _DistanceType __len = std::distance(__first, __last);
       _DistanceType __half;
       _ForwardIterator __middle;
 
-      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;
+      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;
     }
 
   /**
-   *  @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.
+   *  @ingroup binary_search_algorithms
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @param  val     The search term.
+   *  @return  An pair of iterators defining the subrange.
+   *  @ingroup binary_search_algorithms
+   *
+   *  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 _BidirectionalIterator, typename _Distance>
-    void
-    __merge_without_buffer(_BidirectionalIterator __first,
-                          _BidirectionalIterator __middle,
-                          _BidirectionalIterator __last,
-                          _Distance __len1, _Distance __len2)
+  template<typename _ForwardIterator, typename _Tp>
+    pair<_ForwardIterator, _ForwardIterator>
+    equal_range(_ForwardIterator __first, _ForwardIterator __last,
+               const _Tp& __val)
     {
-      if (__len1 == 0 || __len2 == 0)
-       return;
-      if (__len1 + __len2 == 2) {
-       if (*__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);
-       __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);
-      }
-      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);
+      typedef typename iterator_traits<_ForwardIterator>::value_type
+       _ValueType;
+      typedef typename iterator_traits<_ForwardIterator>::difference_type
+       _DistanceType;
+
+      // concept requirements
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
+      __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>) 
+      __glibcxx_requires_partitioned_lower(__first, __last, __val);
+      __glibcxx_requires_partitioned_upper(__first, __last, __val);      
+
+      _DistanceType __len = std::distance(__first, __last);
+      _DistanceType __half;
+      _ForwardIterator __middle, __left, __right;
+
+      while (__len > 0)
+       {
+         __half = __len >> 1;
+         __middle = __first;
+         std::advance(__middle, __half);
+         if (*__middle < __val)
+           {
+             __first = __middle;
+             ++__first;
+             __len = __len - __half - 1;
+           }
+         else if (__val < *__middle)
+           __len = __half;
+         else
+           {
+             __left = std::lower_bound(__first, __middle, __val);
+             std::advance(__first, __len);
+             __right = std::upper_bound(++__middle, __first, __val);
+             return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
+           }
+       }
+      return 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 binary_search_algorithms
+   *
+   *  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 _BidirectionalIterator, typename _Distance, typename _Compare>
-    void
-    __merge_without_buffer(_BidirectionalIterator __first,
-                           _BidirectionalIterator __middle,
-                          _BidirectionalIterator __last,
-                          _Distance __len1, _Distance __len2,
-                          _Compare __comp)
+  template<typename _ForwardIterator, typename _Tp, typename _Compare>
+    pair<_ForwardIterator, _ForwardIterator>
+    equal_range(_ForwardIterator __first, _ForwardIterator __last,
+               const _Tp& __val,
+               _Compare __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);
-    }
+      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>
-    void
-    __inplace_stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
-    {
-      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);
-    }
+      // concept requirements
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType, _Tp>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _Tp, _ValueType>)
+      __glibcxx_requires_partitioned_lower_pred(__first, __last,
+                                               __val, __comp);
+      __glibcxx_requires_partitioned_upper_pred(__first, __last,
+                                               __val, __comp);
 
-  /**
-   *  @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) {
-       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);
+      _DistanceType __len = std::distance(__first, __last);
+      _DistanceType __half;
+      _ForwardIterator __middle, __left, __right;
+
+      while (__len > 0)
+       {
+         __half = __len >> 1;
+         __middle = __first;
+         std::advance(__middle, __half);
+         if (__comp(*__middle, __val))
+           {
+             __first = __middle;
+             ++__first;
+             __len = __len - __half - 1;
+           }
+         else if (__comp(__val, *__middle))
+           __len = __half;
+         else
+           {
+             __left = std::lower_bound(__first, __middle, __val, __comp);
+             std::advance(__first, __len);
+             __right = std::upper_bound(++__middle, __first, __val, __comp);
+             return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
+           }
+       }
+      return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
     }
 
   /**
-   *  @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 Determines whether an element exists in a range.
+   *  @ingroup binary_search_algorithms
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @param  val     The search term.
+   *  @return  True if @a val (or its equivalent) is in [@a first,@a last ].
    *
-   *  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.
+   *  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 _InputIterator1, typename _InputIterator2, typename _OutputIterator>
-    _OutputIterator
-    merge(_InputIterator1 __first1, _InputIterator1 __last1,
-         _InputIterator2 __first2, _InputIterator2 __last2,
-         _OutputIterator __result)
+  template<typename _ForwardIterator, typename _Tp>
+    bool
+    binary_search(_ForwardIterator __first, _ForwardIterator __last,
+                  const _Tp& __val)
     {
+      typedef typename iterator_traits<_ForwardIterator>::value_type
+       _ValueType;
+
       // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcxx_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_function_requires(_LessThanComparableConcept<
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcxx_requires_sorted(__first1, __last1);
-      __glibcxx_requires_sorted(__first2, __last2);
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_LessThanOpConcept<_Tp, _ValueType>)
+      __glibcxx_requires_partitioned_lower(__first, __last, __val);
+      __glibcxx_requires_partitioned_upper(__first, __last, __val);
 
-      while (__first1 != __last1 && __first2 != __last2) {
-       if (*__first2 < *__first1) {
-         *__result = *__first2;
-         ++__first2;
-       }
-       else {
-         *__result = *__first1;
-         ++__first1;
-       }
-       ++__result;
-      }
-      return std::copy(__first2, __last2, std::copy(__first1, __last1, __result));
+      _ForwardIterator __i = std::lower_bound(__first, __last, __val);
+      return __i != __last && !(__val < *__i);
     }
 
   /**
-   *  @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.
+   *  @brief Determines whether an element exists in a range.
+   *  @ingroup binary_search_algorithms
+   *  @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.
+   *  @return  True if @a val (or its equivalent) is in [@a first,@a last ].
    *
-   *  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.
+   *  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 _InputIterator1, typename _InputIterator2, typename _OutputIterator,
-          typename _Compare>
-    _OutputIterator
-    merge(_InputIterator1 __first1, _InputIterator1 __last1,
-         _InputIterator2 __first2, _InputIterator2 __last2,
-         _OutputIterator __result, _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<_ForwardIterator>::value_type
+       _ValueType;
+
       // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcxx_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator1>::value_type>)
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
-      __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
-
-      while (__first1 != __last1 && __first2 != __last2) {
-       if (__comp(*__first2, *__first1)) {
-         *__result = *__first2;
-         ++__first2;
-       }
-       else {
-         *__result = *__first1;
-         ++__first1;
-       }
-       ++__result;
-      }
-      return std::copy(__first2, __last2, std::copy(__first1, __last1, __result));
-    }
-
-  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;
-
-      while (__last - __first >= __two_step) {
-       __result = std::merge(__first, __first + __step_size,
-                             __first + __step_size, __first + __two_step,
-                             __result);
-       __first += __two_step;
-      }
-
-      __step_size = std::min(_Distance(__last - __first), __step_size);
-      std::merge(__first, __first + __step_size, __first + __step_size, __last,
-                __result);
-    }
+                                 _Tp, _ValueType>)
+      __glibcxx_requires_partitioned_lower_pred(__first, __last,
+                                               __val, __comp);
+      __glibcxx_requires_partitioned_upper_pred(__first, __last,
+                                               __val, __comp);
 
-  template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
-          typename _Distance, typename _Compare>
-    void
-    __merge_sort_loop(_RandomAccessIterator1 __first, _RandomAccessIterator1 __last,
-                     _RandomAccessIterator2 __result, _Distance __step_size,
-                     _Compare __comp)
-    {
-      _Distance __two_step = 2 * __step_size;
-
-      while (__last - __first >= __two_step) {
-       __result = std::merge(__first, __first + __step_size,
-                             __first + __step_size, __first + __two_step,
-                             __result,
-                             __comp);
-       __first += __two_step;
-      }
-      __step_size = std::min(_Distance(__last - __first), __step_size);
-
-      std::merge(__first, __first + __step_size,
-                __first + __step_size, __last,
-                __result,
-                __comp);
-    }
-
-  enum { _S_chunk_size = 7 };
-
-  template<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;
-      }
-      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);
-    }
-
-  template<typename _RandomAccessIterator, typename _Pointer>
-    void
-    __merge_sort_with_buffer(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                             _Pointer __buffer)
-    {
-      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
-
-      _Distance __len = __last - __first;
-      _Pointer __buffer_last = __buffer + __len;
-
-      _Distance __step_size = _S_chunk_size;
-      std::__chunk_insertion_sort(__first, __last, __step_size);
-
-      while (__step_size < __len) {
-       std::__merge_sort_loop(__first, __last, __buffer, __step_size);
-       __step_size *= 2;
-       std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
-       __step_size *= 2;
-      }
+      _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
+      return __i != __last && !bool(__comp(__val, *__i));
     }
 
-  template<typename _RandomAccessIterator, typename _Pointer, typename _Compare>
-    void
-    __merge_sort_with_buffer(_RandomAccessIterator __first, _RandomAccessIterator __last,
-                             _Pointer __buffer, _Compare __comp)
-    {
-      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _Distance;
-
-      _Distance __len = __last - __first;
-      _Pointer __buffer_last = __buffer + __len;
-
-      _Distance __step_size = _S_chunk_size;
-      std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
-
-      while (__step_size < __len) {
-       std::__merge_sort_loop(__first, __last, __buffer, __step_size, __comp);
-       __step_size *= 2;
-       std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size, __comp);
-       __step_size *= 2;
-      }
-    }
+  // merge
 
-  /**
-   *  @if maint
-   *  This is a helper function for the merge routines.
-   *  @endif
-  */
+  /// This is a helper function for the merge routines.
   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
           typename _BidirectionalIterator3>
     _BidirectionalIterator3
-    __merge_backward(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
-                    _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2,
+    __merge_backward(_BidirectionalIterator1 __first1,
+                    _BidirectionalIterator1 __last1,
+                    _BidirectionalIterator2 __first2,
+                    _BidirectionalIterator2 __last2,
                     _BidirectionalIterator3 __result)
     {
       if (__first1 == __last1)
@@ -3009,32 +2735,33 @@ namespace std
        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 {
-         *--__result = *__last2;
-         if (__first2 == __last2)
-           return std::copy_backward(__first1, ++__last1, __result);
-         --__last2;
+      while (true)
+       {
+         if (*__last2 < *__last1)
+           {
+             *--__result = *__last1;
+             if (__first1 == __last1)
+               return std::copy_backward(__first2, ++__last2, __result);
+             --__last1;
+           }
+         else
+           {
+             *--__result = *__last2;
+             if (__first2 == __last2)
+               return std::copy_backward(__first1, ++__last1, __result);
+             --__last2;
+           }
        }
-      }
     }
 
-  /**
-   *  @if maint
-   *  This is a helper function for the merge routines.
-   *  @endif
-  */
+  /// This is a helper function for the merge routines.
   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
           typename _BidirectionalIterator3, typename _Compare>
     _BidirectionalIterator3
-    __merge_backward(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
-                    _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2,
+    __merge_backward(_BidirectionalIterator1 __first1,
+                    _BidirectionalIterator1 __last1,
+                    _BidirectionalIterator2 __first2,
+                    _BidirectionalIterator2 __last2,
                     _BidirectionalIterator3 __result,
                     _Compare __comp)
     {
@@ -3044,27 +2771,26 @@ namespace std
        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 {
-         *--__result = *__last2;
-         if (__first2 == __last2)
-           return std::copy_backward(__first1, ++__last1, __result);
-         --__last2;
+      while (true)
+       {
+         if (__comp(*__last2, *__last1))
+           {
+             *--__result = *__last1;
+             if (__first1 == __last1)
+               return std::copy_backward(__first2, ++__last2, __result);
+             --__last1;
+           }
+         else
+           {
+             *--__result = *__last2;
+             if (__first2 == __last2)
+               return std::copy_backward(__first1, ++__last1, __result);
+             --__last2;
+           }
        }
-      }
     }
 
-  /**
-   *  @if maint
-   *  This is a helper function for the merge routines.
-   *  @endif
-  */
+  /// This is a helper function for the merge routines.
   template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
           typename _Distance>
     _BidirectionalIterator1
@@ -3076,29 +2802,29 @@ namespace std
                      _Distance __buffer_size)
     {
       _BidirectionalIterator2 __buffer_end;
-      if (__len1 > __len2 && __len2 <= __buffer_size) {
-       __buffer_end = std::copy(__middle, __last, __buffer);
-       std::copy_backward(__first, __middle, __last);
-       return std::copy(__buffer, __buffer_end, __first);
-      }
-      else if (__len1 <= __buffer_size) {
-       __buffer_end = std::copy(__first, __middle, __buffer);
-       std::copy(__middle, __last, __first);
-       return std::copy_backward(__buffer, __buffer_end, __last);
-      }
-      else {
-       std::rotate(__first, __middle, __last);
-       std::advance(__first, std::distance(__middle, __last));
-       return __first;
-      }
+      if (__len1 > __len2 && __len2 <= __buffer_size)
+       {
+         __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
+         _GLIBCXX_MOVE_BACKWARD3(__first, __middle, __last);
+         return _GLIBCXX_MOVE3(__buffer, __buffer_end, __first);
+       }
+      else if (__len1 <= __buffer_size)
+       {
+         __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
+         _GLIBCXX_MOVE3(__middle, __last, __first);
+         return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last);
+       }
+      else
+       {
+         std::rotate(__first, __middle, __last);
+         std::advance(__first, std::distance(__middle, __last));
+         return __first;
+       }
     }
 
-  /**
-   *  @if maint
-   *  This is a helper function for the merge routines.
-   *  @endif
-  */
-  template<typename _BidirectionalIterator, typename _Distance, typename _Pointer>
+  /// This is a helper function for the merge routines.
+  template<typename _BidirectionalIterator, typename _Distance,
+          typename _Pointer>
     void
     __merge_adaptive(_BidirectionalIterator __first,
                      _BidirectionalIterator __middle,
@@ -3106,49 +2832,61 @@ namespace std
                     _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);
+      if (__len1 <= __len2 && __len1 <= __buffer_size)
+       {
+         _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
+         _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
+                               _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
+                               _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
+                               _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
+                               __first);
+       }
+      else if (__len2 <= __buffer_size)
+       {
+         _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
+         std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
+                               _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
+                               _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
+                               _GLIBCXX_MAKE_MOVE_ITERATOR(__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);
+         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);
-         }
+         _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);
+       }
     }
 
-  /**
-   *  @if maint
-   *  This is a helper function for the merge routines.
-   *  @endif
-  */
-  template<typename _BidirectionalIterator, typename _Distance, typename _Pointer,
-          typename _Compare>
+  /// This is a helper function for the merge routines.
+  template<typename _BidirectionalIterator, typename _Distance, 
+          typename _Pointer, typename _Compare>
     void
     __merge_adaptive(_BidirectionalIterator __first,
                      _BidirectionalIterator __middle,
@@ -3157,75 +2895,182 @@ namespace std
                     _Pointer __buffer, _Distance __buffer_size,
                     _Compare __comp)
     {
-         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);
+      if (__len1 <= __len2 && __len1 <= __buffer_size)
+       {
+         _Pointer __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
+         _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
+                               _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer_end),
+                               _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
+                               _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
+                               __first, __comp);
+       }
+      else if (__len2 <= __buffer_size)
+       {
+         _Pointer __buffer_end = _GLIBCXX_MOVE3(__middle, __last, __buffer);
+         std::__merge_backward(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
+                               _GLIBCXX_MAKE_MOVE_ITERATOR(__middle),
+                               _GLIBCXX_MAKE_MOVE_ITERATOR(__buffer),
+                               _GLIBCXX_MAKE_MOVE_ITERATOR(__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);
+         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);
-         }
+         _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 Merges two sorted ranges in place.
-   *  @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.
-   *
-   *  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 _BidirectionalIterator>
+  /// This is a helper function for the merge routines.
+  template<typename _BidirectionalIterator, typename _Distance>
     void
-    inplace_merge(_BidirectionalIterator __first,
-                 _BidirectionalIterator __middle,
-                 _BidirectionalIterator __last)
+    __merge_without_buffer(_BidirectionalIterator __first,
+                          _BidirectionalIterator __middle,
+                          _BidirectionalIterator __last,
+                          _Distance __len1, _Distance __len2)
     {
-      typedef typename iterator_traits<_BidirectionalIterator>::value_type
-          _ValueType;
-      typedef typename iterator_traits<_BidirectionalIterator>::difference_type
-          _DistanceType;
-
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
-           _BidirectionalIterator>)
-      __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
+      if (__len1 == 0 || __len2 == 0)
+       return;
+      if (__len1 + __len2 == 2)
+       {
+         if (*__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);
+         __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);
+       }
+      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);
+    }
+
+  /// This is a helper function for the merge routines.
+  template<typename _BidirectionalIterator, typename _Distance,
+          typename _Compare>
+    void
+    __merge_without_buffer(_BidirectionalIterator __first,
+                           _BidirectionalIterator __middle,
+                          _BidirectionalIterator __last,
+                          _Distance __len1, _Distance __len2,
+                          _Compare __comp)
+    {
+      if (__len1 == 0 || __len2 == 0)
+       return;
+      if (__len1 + __len2 == 2)
+       {
+         if (__comp(*__middle, *__first))
+           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 Merges two sorted ranges in place.
+   *  @ingroup sorting_algorithms
+   *  @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.
+   *
+   *  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 _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
+      __glibcxx_function_requires(_Mutable_BidirectionalIteratorConcept<
+           _BidirectionalIterator>)
+      __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
       __glibcxx_requires_sorted(__first, __middle);
       __glibcxx_requires_sorted(__middle, __last);
 
@@ -3235,7 +3080,8 @@ namespace std
       _DistanceType __len1 = std::distance(__first, __middle);
       _DistanceType __len2 = std::distance(__middle, __last);
 
-      _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first, __last);
+      _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
+                                                                 __last);
       if (__buf.begin() == 0)
        std::__merge_without_buffer(__first, __middle, __last, __len1, __len2);
       else
@@ -3245,6 +3091,7 @@ namespace std
 
   /**
    *  @brief Merges two sorted ranges in place.
+   *  @ingroup sorting_algorithms
    *  @param  first   An iterator.
    *  @param  middle  Another iterator.
    *  @param  last    Another iterator.
@@ -3287,397 +3134,260 @@ namespace std
       if (__first == __middle || __middle == __last)
        return;
 
-      _DistanceType __len1 = std::distance(__first, __middle);
-      _DistanceType __len2 = std::distance(__middle, __last);
+      const _DistanceType __len1 = std::distance(__first, __middle);
+      const _DistanceType __len2 = std::distance(__middle, __last);
 
-      _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first, __last);
+      _Temporary_buffer<_BidirectionalIterator, _ValueType> __buf(__first,
+                                                                 __last);
       if (__buf.begin() == 0)
-       std::__merge_without_buffer(__first, __middle, __last, __len1, __len2, __comp);
+       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>
+  template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
+          typename _Distance>
     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);
+    __merge_sort_loop(_RandomAccessIterator1 __first,
+                     _RandomAccessIterator1 __last,
+                     _RandomAccessIterator2 __result,
+                     _Distance __step_size)
+    {
+      const _Distance __two_step = 2 * __step_size;
+
+      while (__last - __first >= __two_step)
+       {
+         __result = _GLIBCXX_STD_P::merge(
+                       _GLIBCXX_MAKE_MOVE_ITERATOR(__first),
+                       _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
+                       _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
+                       _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __two_step),
+                       __result);
+         __first += __two_step;
+       }
+
+      __step_size = std::min(_Distance(__last - __first), __step_size);
+      _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
+                           _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
+                                                       __step_size),
+                           _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
+                                                       __step_size),
+                           _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
+                           __result);
     }
 
-  /**
-   *  @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)
+  template<typename _RandomAccessIterator1, typename _RandomAccessIterator2,
+          typename _Distance, typename _Compare>
+    void
+    __merge_sort_loop(_RandomAccessIterator1 __first,
+                     _RandomAccessIterator1 __last,
+                     _RandomAccessIterator2 __result, _Distance __step_size,
+                     _Compare __comp)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
-      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
-
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
-           _RandomAccessIterator>)
-      __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
-      __glibcxx_requires_valid_range(__first, __last);
+      const _Distance __two_step = 2 * __step_size;
+
+      while (__last - __first >= __two_step)
+       {
+         __result = _GLIBCXX_STD_P::merge(
+                       _GLIBCXX_MAKE_MOVE_ITERATOR(__first),
+                       _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
+                       _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __step_size),
+                       _GLIBCXX_MAKE_MOVE_ITERATOR(__first + __two_step),
+                       __result, __comp);
+         __first += __two_step;
+       }
+      __step_size = std::min(_Distance(__last - __first), __step_size);
 
-      _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()));
+      _GLIBCXX_STD_P::merge(_GLIBCXX_MAKE_MOVE_ITERATOR(__first),
+                           _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
+                                                       __step_size),
+                           _GLIBCXX_MAKE_MOVE_ITERATOR(__first +
+                                                       __step_size),
+                           _GLIBCXX_MAKE_MOVE_ITERATOR(__last),
+                           __result, __comp);
     }
 
-  /**
-   *  @brief Sort the elements of a sequence using a predicate for comparison,
-   *         preserving the relative order of equivalent elements.
-   *  @param  first   An iterator.
-   *  @param  last    Another iterator.
-   *  @param  comp    A comparison functor.
-   *  @return  Nothing.
-   *
-   *  Sorts the elements in the range @p [first,last) in ascending order,
-   *  such that @p comp(*(i+1),*i) is false for each iterator @p i in the
-   *  range @p [first,last-1).
-   *
-   *  The relative ordering of equivalent elements is preserved, so any two
-   *  elements @p x and @p y in the range @p [first,last) such that
-   *  @p comp(x,y) is false and @p comp(y,x) is false will have the same
-   *  relative ordering after calling @p stable_sort().
-  */
-  template<typename _RandomAccessIterator, typename _Compare>
-    inline void
-    stable_sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
+  template<typename _RandomAccessIterator, typename _Distance>
+    void
+    __chunk_insertion_sort(_RandomAccessIterator __first,
+                          _RandomAccessIterator __last,
+                          _Distance __chunk_size)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
-      typedef typename iterator_traits<_RandomAccessIterator>::difference_type _DistanceType;
-
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
-           _RandomAccessIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-                                                         _ValueType, _ValueType>)
-      __glibcxx_requires_valid_range(__first, __last);
-
-      _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);
+      while (__last - __first >= __chunk_size)
+       {
+         std::__insertion_sort(__first, __first + __chunk_size);
+         __first += __chunk_size;
+       }
+      std::__insertion_sort(__first, __last);
     }
 
-  /**
-   *  @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 _RandomAccessIterator, typename _Distance,
+          typename _Compare>
     void
-    nth_element(_RandomAccessIterator __first,
-               _RandomAccessIterator __nth,
-               _RandomAccessIterator __last)
+    __chunk_insertion_sort(_RandomAccessIterator __first,
+                          _RandomAccessIterator __last,
+                          _Distance __chunk_size, _Compare __comp)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
-
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>)
-      __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
-      __glibcxx_requires_valid_range(__first, __nth);
-      __glibcxx_requires_valid_range(__nth, __last);
-
-      while (__last - __first > 3) {
-       _RandomAccessIterator __cut =
-         std::__unguarded_partition(__first, __last,
-                                    _ValueType(std::__median(*__first,
-                                                             *(__first + (__last - __first)/2),
-                                                             *(__last - 1))));
-       if (__cut <= __nth)
-         __first = __cut;
-       else
-         __last = __cut;
-      }
-      std::__insertion_sort(__first, __last);
+      while (__last - __first >= __chunk_size)
+       {
+         std::__insertion_sort(__first, __first + __chunk_size, __comp);
+         __first += __chunk_size;
+       }
+      std::__insertion_sort(__first, __last, __comp);
     }
 
-  /**
-   *  @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>
+  enum { _S_chunk_size = 7 };
+
+  template<typename _RandomAccessIterator, typename _Pointer>
     void
-    nth_element(_RandomAccessIterator __first,
-               _RandomAccessIterator __nth,
-               _RandomAccessIterator __last,
-                           _Compare __comp)
+    __merge_sort_with_buffer(_RandomAccessIterator __first,
+                            _RandomAccessIterator __last,
+                             _Pointer __buffer)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type _ValueType;
+      typedef typename iterator_traits<_RandomAccessIterator>::difference_type
+       _Distance;
 
-      // concept requirements
-      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<_RandomAccessIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-                                 _ValueType, _ValueType>)
-      __glibcxx_requires_valid_range(__first, __nth);
-      __glibcxx_requires_valid_range(__nth, __last);
+      const _Distance __len = __last - __first;
+      const _Pointer __buffer_last = __buffer + __len;
 
-      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);
+      _Distance __step_size = _S_chunk_size;
+      std::__chunk_insertion_sort(__first, __last, __step_size);
+
+      while (__step_size < __len)
+       {
+         std::__merge_sort_loop(__first, __last, __buffer, __step_size);
+         __step_size *= 2;
+         std::__merge_sort_loop(__buffer, __buffer_last, __first, __step_size);
+         __step_size *= 2;
+       }
     }
 
-  /**
-   *  @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 _ForwardIterator, typename _Tp>
-    pair<_ForwardIterator, _ForwardIterator>
-    equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val)
+  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
-      // See comments on lower_bound.
-      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_SameTypeConcept<_Tp, _ValueType>)
-      __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
-      __glibcxx_requires_partitioned(__first, __last, __val);
+      const _Distance __len = __last - __first;
+      const _Pointer __buffer_last = __buffer + __len;
 
-      _DistanceType __len = std::distance(__first, __last);
-      _DistanceType __half;
-      _ForwardIterator __middle, __left, __right;
+      _Distance __step_size = _S_chunk_size;
+      std::__chunk_insertion_sort(__first, __last, __step_size, __comp);
 
-      while (__len > 0) {
-       __half = __len >> 1;
-       __middle = __first;
-       std::advance(__middle, __half);
-       if (*__middle < __val) {
-         __first = __middle;
-         ++__first;
-         __len = __len - __half - 1;
+      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;
        }
-       else if (__val < *__middle)
-         __len = __half;
-       else {
-         __left = std::lower_bound(__first, __middle, __val);
-         std::advance(__first, __len);
-         __right = std::upper_bound(++__middle, __first, __val);
-         return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
-       }
-      }
-      return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
     }
 
-  /**
-   *  @brief Finds the largest subrange in which @a val could be inserted
-   *         at any place in it without changing the ordering.
-   *  @param  first   An iterator.
-   *  @param  last    Another iterator.
-   *  @param  val     The search term.
-   *  @param  comp    A functor to use for comparisons.
-   *  @return  An pair of iterators defining the subrange.
-   *  @ingroup binarysearch
-   *
-   *  This is equivalent to
-   *  @code
-   *    std::make_pair(lower_bound(first, last, val, comp),
-   *                   upper_bound(first, last, val, comp))
-   *  @endcode
-   *  but does not actually call those functions.
-  */
-  template<typename _ForwardIterator, typename _Tp, typename _Compare>
-    pair<_ForwardIterator, _ForwardIterator>
-    equal_range(_ForwardIterator __first, _ForwardIterator __last, const _Tp& __val,
-               _Compare __comp)
+  template<typename _RandomAccessIterator, typename _Pointer,
+          typename _Distance>
+    void
+    __stable_sort_adaptive(_RandomAccessIterator __first,
+                          _RandomAccessIterator __last,
+                           _Pointer __buffer, _Distance __buffer_size)
     {
-      typedef typename iterator_traits<_ForwardIterator>::value_type _ValueType;
-      typedef typename iterator_traits<_ForwardIterator>::difference_type _DistanceType;
-
-      // concept requirements
-      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType, _Tp>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _Tp, _ValueType>)
-      __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
-
-      _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;
+      const _Distance __len = (__last - __first + 1) / 2;
+      const _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 if (__comp(__val, *__middle))
-         __len = __half;
-       else {
-         __left = std::lower_bound(__first, __middle, __val, __comp);
-         std::advance(__first, __len);
-         __right = std::upper_bound(++__middle, __first, __val, __comp);
-         return pair<_ForwardIterator, _ForwardIterator>(__left, __right);
+      else
+       {
+         std::__merge_sort_with_buffer(__first, __middle, __buffer);
+         std::__merge_sort_with_buffer(__middle, __last, __buffer);
        }
-      }
-      return pair<_ForwardIterator, _ForwardIterator>(__first, __first);
+      std::__merge_adaptive(__first, __middle, __last,
+                           _Distance(__middle - __first),
+                           _Distance(__last - __middle),
+                           __buffer, __buffer_size);
     }
 
-  /**
-   *  @brief Determines whether an element exists in a range.
-   *  @param  first   An iterator.
-   *  @param  last    Another iterator.
-   *  @param  val     The search term.
-   *  @return  True if @a val (or its equivelent) is in [@a first,@a last ].
-   *  @ingroup binarysearch
-   *
-   *  Note that this does not actually return an iterator to @a val.  For
-   *  that, use std::find or a container's specialized find member functions.
-  */
-  template<typename _ForwardIterator, typename _Tp>
-    bool
-    binary_search(_ForwardIterator __first, _ForwardIterator __last,
-                  const _Tp& __val)
+  template<typename _RandomAccessIterator, typename _Pointer,
+          typename _Distance, typename _Compare>
+    void
+    __stable_sort_adaptive(_RandomAccessIterator __first,
+                          _RandomAccessIterator __last,
+                           _Pointer __buffer, _Distance __buffer_size,
+                           _Compare __comp)
     {
-      // concept requirements
-      // See comments on lower_bound.
-      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_SameTypeConcept<_Tp,
-               typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
-      __glibcxx_requires_partitioned(__first, __last, __val);
-
-      _ForwardIterator __i = std::lower_bound(__first, __last, __val);
-      return __i != __last && !(__val < *__i);
+      const _Distance __len = (__last - __first + 1) / 2;
+      const _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 Determines whether an element exists in a range.
-   *  @param  first   An iterator.
-   *  @param  last    Another iterator.
-   *  @param  val     The search term.
-   *  @param  comp    A functor to use for comparisons.
-   *  @return  True if @a val (or its equivelent) is in [@a first,@a last ].
-   *  @ingroup binarysearch
-   *
-   *  Note that this does not actually return an iterator to @a val.  For
-   *  that, use std::find or a container's specialized find member functions.
-   *
-   *  The comparison function should have the same effects on ordering as
-   *  the function used for the initial sort.
-  */
-  template<typename _ForwardIterator, typename _Tp, typename _Compare>
-    bool
-    binary_search(_ForwardIterator __first, _ForwardIterator __last,
-                  const _Tp& __val, _Compare __comp)
+  /// This is a helper function for the stable sorting routines.
+  template<typename _RandomAccessIterator>
+    void
+    __inplace_stable_sort(_RandomAccessIterator __first,
+                         _RandomAccessIterator __last)
     {
-      // concept requirements
-      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-               typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _Tp,
-               typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_requires_partitioned_pred(__first, __last, __val, __comp);
+      if (__last - __first < 15)
+       {
+         std::__insertion_sort(__first, __last);
+         return;
+       }
+      _RandomAccessIterator __middle = __first + (__last - __first) / 2;
+      std::__inplace_stable_sort(__first, __middle);
+      std::__inplace_stable_sort(__middle, __last);
+      std::__merge_without_buffer(__first, __middle, __last,
+                                 __middle - __first,
+                                 __last - __middle);
+    }
 
-      _ForwardIterator __i = std::lower_bound(__first, __last, __val, __comp);
-      return __i != __last && !__comp(__val, *__i);
+  /// This is a helper function for the stable sorting routines.
+  template<typename _RandomAccessIterator, typename _Compare>
+    void
+    __inplace_stable_sort(_RandomAccessIterator __first,
+                         _RandomAccessIterator __last, _Compare __comp)
+    {
+      if (__last - __first < 15)
+       {
+         std::__insertion_sort(__first, __last, __comp);
+         return;
+       }
+      _RandomAccessIterator __middle = __first + (__last - __first) / 2;
+      std::__inplace_stable_sort(__first, __middle, __comp);
+      std::__inplace_stable_sort(__middle, __last, __comp);
+      std::__merge_without_buffer(__first, __middle, __last,
+                                 __middle - __first,
+                                 __last - __middle,
+                                 __comp);
     }
 
+  // stable_sort
+
   // Set algorithms: includes, set_union, set_intersection, set_difference,
   // set_symmetric_difference.  All of these algorithms have the precondition
   // that their input ranges are sorted and the postcondition that their output
@@ -3691,7 +3401,7 @@ namespace std
    *  @param  last2   End of sequence.
    *  @return  True if each element in [first2,last2) is contained in order
    *  within [first1,last1).  False otherwise.
-   *  @ingroup setoperations
+   *  @ingroup set_algorithms
    *
    *  This operation expects both [first1,last1) and [first2,last2) to be
    *  sorted.  Searches for the presence of each element in [first2,last2)
@@ -3704,16 +3414,18 @@ namespace std
     includes(_InputIterator1 __first1, _InputIterator1 __last1,
             _InputIterator2 __first2, _InputIterator2 __last2)
     {
+      typedef typename iterator_traits<_InputIterator1>::value_type
+       _ValueType1;
+      typedef typename iterator_traits<_InputIterator2>::value_type
+       _ValueType2;
+
       // concept requirements
       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcxx_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_function_requires(_LessThanComparableConcept<
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcxx_requires_sorted(__first1, __last1);
-      __glibcxx_requires_sorted(__first2, __last2);
+      __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
+      __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
+      __glibcxx_requires_sorted_set(__first1, __last1, __first2);
+      __glibcxx_requires_sorted_set(__first2, __last2, __first1);
 
       while (__first1 != __last1 && __first2 != __last2)
        if (*__first2 < *__first1)
@@ -3729,6 +3441,7 @@ namespace std
   /**
    *  @brief Determines whether all elements of a sequence exists in a range
    *  using comparison.
+   *  @ingroup set_algorithms
    *  @param  first1  Start of search range.
    *  @param  last1   End of search range.
    *  @param  first2  Start of sequence
@@ -3736,7 +3449,7 @@ namespace std
    *  @param  comp    Comparison function to use.
    *  @return  True if each element in [first2,last2) is contained in order
    *  within [first1,last1) according to comp.  False otherwise.
-   *  @ingroup setoperations
+   *  @ingroup set_algorithms
    *
    *  This operation expects both [first1,last1) and [first2,last2) to be
    *  sorted.  Searches for the presence of each element in [first2,last2)
@@ -3745,22 +3458,27 @@ namespace std
    *  in [first2,last2) is not found before the search iterator reaches @a
    *  last2, false is returned.
   */
-  template<typename _InputIterator1, typename _InputIterator2, typename _Compare>
+  template<typename _InputIterator1, typename _InputIterator2,
+          typename _Compare>
     bool
     includes(_InputIterator1 __first1, _InputIterator1 __last1,
-            _InputIterator2 __first2, _InputIterator2 __last2, _Compare __comp)
+            _InputIterator2 __first2, _InputIterator2 __last2,
+            _Compare __comp)
     {
+      typedef typename iterator_traits<_InputIterator1>::value_type
+       _ValueType1;
+      typedef typename iterator_traits<_InputIterator2>::value_type
+       _ValueType2;
+
       // concept requirements
       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcxx_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
-      __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
+                                 _ValueType1, _ValueType2>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType2, _ValueType1>)
+      __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
+      __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
 
       while (__first1 != __last1 && __first2 != __last2)
        if (__comp(*__first2, *__first1))
@@ -3773,506 +3491,453 @@ namespace std
       return __first2 == __last2;
     }
 
+  // nth_element
+  // merge
+  // set_difference
+  // set_intersection
+  // set_union
+  // stable_sort
+  // set_symmetric_difference
+  // min_element
+  // max_element
+
   /**
-   *  @brief Return the union of two sorted ranges.
-   *  @param  first1  Start of first range.
-   *  @param  last1   End of first range.
-   *  @param  first2  Start of second range.
-   *  @param  last2   End of second range.
-   *  @return  End of the output range.
-   *  @ingroup setoperations
+   *  @brief  Permute range into the next @a dictionary ordering.
+   *  @ingroup sorting_algorithms
+   *  @param  first  Start of range.
+   *  @param  last   End of range.
+   *  @return  False if wrapped to first permutation, true otherwise.
    *
-   *  This operation iterates over both ranges, copying elements present in
-   *  each range in order to the output range.  Iterators increment for each
-   *  range.  When the current element of one range is less than the other,
-   *  that element is copied and the iterator advanced.  If an element is
-   *  contained in both ranges, the element from the first range is copied and
-   *  both ranges advance.  The output range may not overlap either input
-   *  range.
+   *  Treats all permutations of the range as a set of @a dictionary sorted
+   *  sequences.  Permutes the current sequence into the next one of this set.
+   *  Returns true if there are more sequences to generate.  If the sequence
+   *  is the largest of the set, the smallest is generated and false returned.
   */
-  template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
-    _OutputIterator
-    set_union(_InputIterator1 __first1, _InputIterator1 __last1,
-             _InputIterator2 __first2, _InputIterator2 __last2,
-             _OutputIterator __result)
+  template<typename _BidirectionalIterator>
+    bool
+    next_permutation(_BidirectionalIterator __first,
+                    _BidirectionalIterator __last)
     {
       // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcxx_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
+      __glibcxx_function_requires(_BidirectionalIteratorConcept<
+                                 _BidirectionalIterator>)
       __glibcxx_function_requires(_LessThanComparableConcept<
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcxx_requires_sorted(__first1, __last1);
-      __glibcxx_requires_sorted(__first2, __last2);
+           typename iterator_traits<_BidirectionalIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
 
-      while (__first1 != __last1 && __first2 != __last2) {
-       if (*__first1 < *__first2) {
-         *__result = *__first1;
-         ++__first1;
-       }
-       else if (*__first2 < *__first1) {
-         *__result = *__first2;
-         ++__first2;
-       }
-       else {
-         *__result = *__first1;
-         ++__first1;
-         ++__first2;
+      if (__first == __last)
+       return false;
+      _BidirectionalIterator __i = __first;
+      ++__i;
+      if (__i == __last)
+       return false;
+      __i = __last;
+      --__i;
+
+      for(;;)
+       {
+         _BidirectionalIterator __ii = __i;
+         --__i;
+         if (*__i < *__ii)
+           {
+             _BidirectionalIterator __j = __last;
+             while (!(*__i < *--__j))
+               {}
+             std::iter_swap(__i, __j);
+             std::reverse(__ii, __last);
+             return true;
+           }
+         if (__i == __first)
+           {
+             std::reverse(__first, __last);
+             return false;
+           }
        }
-       ++__result;
-      }
-      return std::copy(__first2, __last2, std::copy(__first1, __last1, __result));
     }
 
   /**
-   *  @brief Return the union of two sorted ranges using a comparison functor.
-   *  @param  first1  Start of first range.
-   *  @param  last1   End of first range.
-   *  @param  first2  Start of second range.
-   *  @param  last2   End of second range.
-   *  @param  comp    The comparison functor.
-   *  @return  End of the output range.
-   *  @ingroup setoperations
+   *  @brief  Permute range into the next @a dictionary ordering using
+   *          comparison functor.
+   *  @ingroup sorting_algorithms
+   *  @param  first  Start of range.
+   *  @param  last   End of range.
+   *  @param  comp   A comparison functor.
+   *  @return  False if wrapped to first permutation, true otherwise.
    *
-   *  This operation iterates over both ranges, copying elements present in
-   *  each range in order to the output range.  Iterators increment for each
-   *  range.  When the current element of one range is less than the other
-   *  according to @a comp, that element is copied and the iterator advanced.
-   *  If an equivalent element according to @a comp is contained in both
-   *  ranges, the element from the first range is copied and both ranges
-   *  advance.  The output range may not overlap either input range.
+   *  Treats all permutations of the range [first,last) as a set of
+   *  @a dictionary sorted sequences ordered by @a comp.  Permutes the current
+   *  sequence into the next one of this set.  Returns true if there are more
+   *  sequences to generate.  If the sequence is the largest of the set, the
+   *  smallest is generated and false returned.
   */
-  template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
-          typename _Compare>
-    _OutputIterator
-    set_union(_InputIterator1 __first1, _InputIterator1 __last1,
-             _InputIterator2 __first2, _InputIterator2 __last2,
-             _OutputIterator __result, _Compare __comp)
+  template<typename _BidirectionalIterator, typename _Compare>
+    bool
+    next_permutation(_BidirectionalIterator __first,
+                    _BidirectionalIterator __last, _Compare __comp)
     {
       // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcxx_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator1>::value_type>)
+      __glibcxx_function_requires(_BidirectionalIteratorConcept<
+                                 _BidirectionalIterator>)
       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
-      __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
-
-      while (__first1 != __last1 && __first2 != __last2) {
-       if (__comp(*__first1, *__first2)) {
-         *__result = *__first1;
-         ++__first1;
-       }
-       else if (__comp(*__first2, *__first1)) {
-         *__result = *__first2;
-         ++__first2;
-       }
-       else {
-         *__result = *__first1;
-         ++__first1;
-         ++__first2;
+           typename iterator_traits<_BidirectionalIterator>::value_type,
+           typename iterator_traits<_BidirectionalIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__first == __last)
+       return false;
+      _BidirectionalIterator __i = __first;
+      ++__i;
+      if (__i == __last)
+       return false;
+      __i = __last;
+      --__i;
+
+      for(;;)
+       {
+         _BidirectionalIterator __ii = __i;
+         --__i;
+         if (__comp(*__i, *__ii))
+           {
+             _BidirectionalIterator __j = __last;
+             while (!bool(__comp(*__i, *--__j)))
+               {}
+             std::iter_swap(__i, __j);
+             std::reverse(__ii, __last);
+             return true;
+           }
+         if (__i == __first)
+           {
+             std::reverse(__first, __last);
+             return false;
+           }
        }
-       ++__result;
-      }
-      return std::copy(__first2, __last2, std::copy(__first1, __last1, __result));
     }
 
   /**
-   *  @brief Return the intersection of two sorted ranges.
-   *  @param  first1  Start of first range.
-   *  @param  last1   End of first range.
-   *  @param  first2  Start of second range.
-   *  @param  last2   End of second range.
-   *  @return  End of the output range.
-   *  @ingroup setoperations
+   *  @brief  Permute range into the previous @a dictionary ordering.
+   *  @ingroup sorting_algorithms
+   *  @param  first  Start of range.
+   *  @param  last   End of range.
+   *  @return  False if wrapped to last permutation, true otherwise.
    *
-   *  This operation iterates over both ranges, copying elements present in
-   *  both ranges in order to the output range.  Iterators increment for each
-   *  range.  When the current element of one range is less than the other,
-   *  that iterator advances.  If an element is contained in both ranges, the
-   *  element from the first range is copied and both ranges advance.  The
-   *  output range may not overlap either input range.
+   *  Treats all permutations of the range as a set of @a dictionary sorted
+   *  sequences.  Permutes the current sequence into the previous one of this
+   *  set.  Returns true if there are more sequences to generate.  If the
+   *  sequence is the smallest of the set, the largest is generated and false
+   *  returned.
   */
-  template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
-    _OutputIterator
-    set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
-                    _InputIterator2 __first2, _InputIterator2 __last2,
-                    _OutputIterator __result)
+  template<typename _BidirectionalIterator>
+    bool
+    prev_permutation(_BidirectionalIterator __first,
+                    _BidirectionalIterator __last)
     {
       // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcxx_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
+      __glibcxx_function_requires(_BidirectionalIteratorConcept<
+                                 _BidirectionalIterator>)
       __glibcxx_function_requires(_LessThanComparableConcept<
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcxx_requires_sorted(__first1, __last1);
-      __glibcxx_requires_sorted(__first2, __last2);
-
-      while (__first1 != __last1 && __first2 != __last2)
-       if (*__first1 < *__first2)
-         ++__first1;
-       else if (*__first2 < *__first1)
-         ++__first2;
-       else {
-         *__result = *__first1;
-         ++__first1;
-         ++__first2;
-         ++__result;
-       }
-      return __result;
-    }
+           typename iterator_traits<_BidirectionalIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
 
-  /**
-   *  @brief Return the intersection of two sorted ranges using comparison
-   *  functor. 
-   *  @param  first1  Start of first range.
-   *  @param  last1   End of first range.
-   *  @param  first2  Start of second range.
-   *  @param  last2   End of second range.
-   *  @param  comp    The comparison functor.
-   *  @return  End of the output range.
-   *  @ingroup setoperations
+      if (__first == __last)
+       return false;
+      _BidirectionalIterator __i = __first;
+      ++__i;
+      if (__i == __last)
+       return false;
+      __i = __last;
+      --__i;
+
+      for(;;)
+       {
+         _BidirectionalIterator __ii = __i;
+         --__i;
+         if (*__ii < *__i)
+           {
+             _BidirectionalIterator __j = __last;
+             while (!(*--__j < *__i))
+               {}
+             std::iter_swap(__i, __j);
+             std::reverse(__ii, __last);
+             return true;
+           }
+         if (__i == __first)
+           {
+             std::reverse(__first, __last);
+             return false;
+           }
+       }
+    }
+
+  /**
+   *  @brief  Permute range into the previous @a dictionary ordering using
+   *          comparison functor.
+   *  @ingroup sorting_algorithms
+   *  @param  first  Start of range.
+   *  @param  last   End of range.
+   *  @param  comp   A comparison functor.
+   *  @return  False if wrapped to last permutation, true otherwise.
    *
-   *  This operation iterates over both ranges, copying elements present in
-   *  both ranges in order to the output range.  Iterators increment for each
-   *  range.  When the current element of one range is less than the other
-   *  according to @a comp, that iterator advances.  If an element is
-   *  contained in both ranges according to @a comp, the element from the
-   *  first range is copied and both ranges advance.  The output range may not
-   *  overlap either input range.
+   *  Treats all permutations of the range [first,last) as a set of
+   *  @a dictionary sorted sequences ordered by @a comp.  Permutes the current
+   *  sequence into the previous one of this set.  Returns true if there are
+   *  more sequences to generate.  If the sequence is the smallest of the set,
+   *  the largest is generated and false returned.
   */
-  template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
-          typename _Compare>
-    _OutputIterator
-    set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
-                    _InputIterator2 __first2, _InputIterator2 __last2,
-                    _OutputIterator __result, _Compare __comp)
+  template<typename _BidirectionalIterator, typename _Compare>
+    bool
+    prev_permutation(_BidirectionalIterator __first,
+                    _BidirectionalIterator __last, _Compare __comp)
     {
       // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcxx_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator1>::value_type>)
+      __glibcxx_function_requires(_BidirectionalIteratorConcept<
+                                 _BidirectionalIterator>)
       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
-      __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
+           typename iterator_traits<_BidirectionalIterator>::value_type,
+           typename iterator_traits<_BidirectionalIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
 
-      while (__first1 != __last1 && __first2 != __last2)
-       if (__comp(*__first1, *__first2))
-         ++__first1;
-       else if (__comp(*__first2, *__first1))
-         ++__first2;
-       else {
-         *__result = *__first1;
-         ++__first1;
-         ++__first2;
-         ++__result;
+      if (__first == __last)
+       return false;
+      _BidirectionalIterator __i = __first;
+      ++__i;
+      if (__i == __last)
+       return false;
+      __i = __last;
+      --__i;
+
+      for(;;)
+       {
+         _BidirectionalIterator __ii = __i;
+         --__i;
+         if (__comp(*__ii, *__i))
+           {
+             _BidirectionalIterator __j = __last;
+             while (!bool(__comp(*--__j, *__i)))
+               {}
+             std::iter_swap(__i, __j);
+             std::reverse(__ii, __last);
+             return true;
+           }
+         if (__i == __first)
+           {
+             std::reverse(__first, __last);
+             return false;
+           }
        }
-      return __result;
     }
 
+  // replace
+  // replace_if
+
   /**
-   *  @brief Return the difference of two sorted ranges.
-   *  @param  first1  Start of first range.
-   *  @param  last1   End of first range.
-   *  @param  first2  Start of second range.
-   *  @param  last2   End of second range.
-   *  @return  End of the output range.
-   *  @ingroup setoperations
+   *  @brief Copy a sequence, replacing each element of one value with another
+   *         value.
+   *  @param  first      An input iterator.
+   *  @param  last       An input iterator.
+   *  @param  result     An output iterator.
+   *  @param  old_value  The value to be replaced.
+   *  @param  new_value  The replacement value.
+   *  @return   The end of the output sequence, @p result+(last-first).
    *
-   *  This operation iterates over both ranges, copying elements present in
-   *  the first range but not the second in order to the output range.
-   *  Iterators increment for each range.  When the current element of the
-   *  first range is less than the second, that element is copied and the
-   *  iterator advances.  If the current element of the second range is less,
-   *  the iterator advances, but no element is copied.  If an element is
-   *  contained in both ranges, no elements are copied and both ranges
-   *  advance.  The output range may not overlap either input range.
+   *  Copies each element in the input range @p [first,last) to the
+   *  output range @p [result,result+(last-first)) replacing elements
+   *  equal to @p old_value with @p new_value.
   */
-  template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
+  template<typename _InputIterator, typename _OutputIterator, typename _Tp>
     _OutputIterator
-    set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
-                  _InputIterator2 __first2, _InputIterator2 __last2,
-                  _OutputIterator __result)
+    replace_copy(_InputIterator __first, _InputIterator __last,
+                _OutputIterator __result,
+                const _Tp& __old_value, const _Tp& __new_value)
     {
       // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcxx_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_function_requires(_LessThanComparableConcept<
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcxx_requires_sorted(__first1, __last1);
-      __glibcxx_requires_sorted(__first2, __last2);
+           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_function_requires(_EqualOpConcept<
+           typename iterator_traits<_InputIterator>::value_type, _Tp>)
+      __glibcxx_requires_valid_range(__first, __last);
 
-      while (__first1 != __last1 && __first2 != __last2)
-       if (*__first1 < *__first2) {
-         *__result = *__first1;
-         ++__first1;
-         ++__result;
-       }
-       else if (*__first2 < *__first1)
-         ++__first2;
-       else {
-         ++__first1;
-         ++__first2;
-       }
-      return std::copy(__first1, __last1, __result);
+      for (; __first != __last; ++__first, ++__result)
+       if (*__first == __old_value)
+         *__result = __new_value;
+       else
+         *__result = *__first;
+      return __result;
     }
 
   /**
-   *  @brief  Return the difference of two sorted ranges using comparison
-   *  functor.
-   *  @param  first1  Start of first range.
-   *  @param  last1   End of first range.
-   *  @param  first2  Start of second range.
-   *  @param  last2   End of second range.
-   *  @param  comp    The comparison functor.
-   *  @return  End of the output range.
-   *  @ingroup setoperations
+   *  @brief Copy a sequence, replacing each value for which a predicate
+   *         returns true with another value.
+   *  @ingroup mutating_algorithms
+   *  @param  first      An input iterator.
+   *  @param  last       An input iterator.
+   *  @param  result     An output iterator.
+   *  @param  pred       A predicate.
+   *  @param  new_value  The replacement value.
+   *  @return   The end of the output sequence, @p result+(last-first).
    *
-   *  This operation iterates over both ranges, copying elements present in
-   *  the first range but not the second in order to the output range.
-   *  Iterators increment for each range.  When the current element of the
-   *  first range is less than the second according to @a comp, that element
-   *  is copied and the iterator advances.  If the current element of the
-   *  second range is less, no element is copied and the iterator advances.
-   *  If an element is contained in both ranges according to @a comp, no
-   *  elements are copied and both ranges advance.  The output range may not
-   *  overlap either input range.
+   *  Copies each element in the range @p [first,last) to the range
+   *  @p [result,result+(last-first)) replacing elements for which
+   *  @p pred returns true with @p new_value.
   */
-  template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
-          typename _Compare>
+  template<typename _InputIterator, typename _OutputIterator,
+          typename _Predicate, typename _Tp>
     _OutputIterator
-    set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
-                  _InputIterator2 __first2, _InputIterator2 __last2,
-                  _OutputIterator __result, _Compare __comp)
+    replace_copy_if(_InputIterator __first, _InputIterator __last,
+                   _OutputIterator __result,
+                   _Predicate __pred, const _Tp& __new_value)
     {
       // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcxx_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
       __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
-      __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
+           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
+           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
 
-      while (__first1 != __last1 && __first2 != __last2)
-       if (__comp(*__first1, *__first2)) {
-         *__result = *__first1;
-         ++__first1;
-         ++__result;
-       }
-       else if (__comp(*__first2, *__first1))
-         ++__first2;
-       else {
-         ++__first1;
-         ++__first2;
-       }
-      return std::copy(__first1, __last1, __result);
+      for (; __first != __last; ++__first, ++__result)
+       if (__pred(*__first))
+         *__result = __new_value;
+       else
+         *__result = *__first;
+      return __result;
     }
 
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
   /**
-   *  @brief  Return the symmetric difference of two sorted ranges.
-   *  @param  first1  Start of first range.
-   *  @param  last1   End of first range.
-   *  @param  first2  Start of second range.
-   *  @param  last2   End of second range.
-   *  @return  End of the output range.
-   *  @ingroup setoperations
-   *
-   *  This operation iterates over both ranges, copying elements present in
-   *  one range but not the other in order to the output range.  Iterators
-   *  increment for each range.  When the current element of one range is less
-   *  than the other, that element is copied and the iterator advances.  If an
-   *  element is contained in both ranges, no elements are copied and both
-   *  ranges advance.  The output range may not overlap either input range.
+   *  @brief  Determines whether the elements of a sequence are sorted.
+   *  @ingroup sorting_algorithms
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @return  True if the elements are sorted, false otherwise.
   */
-  template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator>
-    _OutputIterator
-    set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
-                            _InputIterator2 __first2, _InputIterator2 __last2,
-                            _OutputIterator __result)
+  template<typename _ForwardIterator>
+    inline bool
+    is_sorted(_ForwardIterator __first, _ForwardIterator __last)
+    { return std::is_sorted_until(__first, __last) == __last; }
+
+  /**
+   *  @brief  Determines whether the elements of a sequence are sorted
+   *          according to a comparison functor.
+   *  @ingroup sorting_algorithms
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @param  comp    A comparison functor.
+   *  @return  True if the elements are sorted, false otherwise.
+  */
+  template<typename _ForwardIterator, typename _Compare>
+    inline bool
+    is_sorted(_ForwardIterator __first, _ForwardIterator __last,
+             _Compare __comp)
+    { return std::is_sorted_until(__first, __last, __comp) == __last; }
+
+  /**
+   *  @brief  Determines the end of a sorted sequence.
+   *  @ingroup sorting_algorithms
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @return  An iterator pointing to the last iterator i in [first, last)
+   *           for which the range [first, i) is sorted.
+  */
+  template<typename _ForwardIterator>
+    _ForwardIterator
+    is_sorted_until(_ForwardIterator __first, _ForwardIterator __last)
     {
       // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcxx_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
       __glibcxx_function_requires(_LessThanComparableConcept<
-           typename iterator_traits<_InputIterator1>::value_type>)
-      __glibcxx_requires_sorted(__first1, __last1);
-      __glibcxx_requires_sorted(__first2, __last2);
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
 
-      while (__first1 != __last1 && __first2 != __last2)
-       if (*__first1 < *__first2) {
-         *__result = *__first1;
-         ++__first1;
-         ++__result;
-       }
-       else if (*__first2 < *__first1) {
-         *__result = *__first2;
-         ++__first2;
-         ++__result;
-       }
-       else {
-         ++__first1;
-         ++__first2;
-       }
-      return std::copy(__first2, __last2, std::copy(__first1, __last1, __result));
+      if (__first == __last)
+       return __last;
+
+      _ForwardIterator __next = __first;
+      for (++__next; __next != __last; __first = __next, ++__next)
+       if (*__next < *__first)
+         return __next;
+      return __next;
     }
 
   /**
-   *  @brief  Return the symmetric difference of two sorted ranges using
-   *  comparison functor.
-   *  @param  first1  Start of first range.
-   *  @param  last1   End of first range.
-   *  @param  first2  Start of second range.
-   *  @param  last2   End of second range.
-   *  @param  comp    The comparison functor.
-   *  @return  End of the output range.
-   *  @ingroup setoperations
-   *
-   *  This operation iterates over both ranges, copying elements present in
-   *  one range but not the other in order to the output range.  Iterators
-   *  increment for each range.  When the current element of one range is less
-   *  than the other according to @a comp, that element is copied and the
-   *  iterator advances.  If an element is contained in both ranges according
-   *  to @a comp, no elements are copied and both ranges advance.  The output
-   *  range may not overlap either input range.
+   *  @brief  Determines the end of a sorted sequence using comparison functor.
+   *  @ingroup sorting_algorithms
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @param  comp    A comparison functor.
+   *  @return  An iterator pointing to the last iterator i in [first, last)
+   *           for which the range [first, i) is sorted.
   */
-  template<typename _InputIterator1, typename _InputIterator2, typename _OutputIterator,
-          typename _Compare>
-    _OutputIterator
-    set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
-                            _InputIterator2 __first2, _InputIterator2 __last2,
-                            _OutputIterator __result,
-                            _Compare __comp)
+  template<typename _ForwardIterator, typename _Compare>
+    _ForwardIterator
+    is_sorted_until(_ForwardIterator __first, _ForwardIterator __last,
+                   _Compare __comp)
     {
       // concept requirements
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
-      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
-      __glibcxx_function_requires(_SameTypeConcept<
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
-           typename iterator_traits<_InputIterator1>::value_type>)
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
       __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-           typename iterator_traits<_InputIterator1>::value_type,
-           typename iterator_traits<_InputIterator2>::value_type>)
-      __glibcxx_requires_sorted_pred(__first1, __last1, __comp);
-      __glibcxx_requires_sorted_pred(__first2, __last2, __comp);
+           typename iterator_traits<_ForwardIterator>::value_type,
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
 
-      while (__first1 != __last1 && __first2 != __last2)
-       if (__comp(*__first1, *__first2)) {
-         *__result = *__first1;
-         ++__first1;
-         ++__result;
-       }
-       else if (__comp(*__first2, *__first1)) {
-         *__result = *__first2;
-         ++__first2;
-         ++__result;
-       }
-       else {
-         ++__first1;
-         ++__first2;
-       }
-      return std::copy(__first2, __last2, std::copy(__first1, __last1, __result));
-    }
+      if (__first == __last)
+       return __last;
 
-  // min_element and max_element, with and without an explicitly supplied
-  // comparison function.
+      _ForwardIterator __next = __first;
+      for (++__next; __next != __last; __first = __next, ++__next)
+       if (__comp(*__next, *__first))
+         return __next;
+      return __next;
+    }
 
   /**
-   *  @brief  Return the maximum element in a range.
-   *  @param  first  Start of range.
-   *  @param  last   End of range.
-   *  @return  Iterator referencing the first instance of the largest value.
+   *  @brief  Determines min and max at once as an ordered pair.
+   *  @ingroup sorting_algorithms
+   *  @param  a  A thing of arbitrary type.
+   *  @param  b  Another thing of arbitrary type.
+   *  @return  A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
   */
-  template<typename _ForwardIterator>
-    _ForwardIterator
-    max_element(_ForwardIterator __first, _ForwardIterator __last)
+  template<typename _Tp>
+    inline pair<const _Tp&, const _Tp&>
+    minmax(const _Tp& __a, const _Tp& __b)
     {
       // concept requirements
-      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_LessThanComparableConcept<
-           typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
+      __glibcxx_function_requires(_LessThanComparableConcept<_Tp>)
 
-      if (__first == __last) return __first;
-      _ForwardIterator __result = __first;
-      while (++__first != __last)
-       if (*__result < *__first)
-         __result = __first;
-      return __result;
+      return __b < __a ? pair<const _Tp&, const _Tp&>(__b, __a)
+                      : pair<const _Tp&, const _Tp&>(__a, __b);
     }
 
   /**
-   *  @brief  Return the maximum element in a range using comparison functor.
-   *  @param  first  Start of range.
-   *  @param  last   End of range.
-   *  @param  comp   Comparison functor.
-   *  @return  Iterator referencing the first instance of the largest value
-   *  according to comp.
+   *  @brief  Determines min and max at once as an ordered pair.
+   *  @ingroup sorting_algorithms
+   *  @param  a  A thing of arbitrary type.
+   *  @param  b  Another thing of arbitrary type.
+   *  @param  comp  A @link comparison_functor comparison functor@endlink.
+   *  @return  A pair(b, a) if b is smaller than a, pair(a, b) otherwise.
   */
-  template<typename _ForwardIterator, typename _Compare>
-    _ForwardIterator
-    max_element(_ForwardIterator __first, _ForwardIterator __last,
-               _Compare __comp)
+  template<typename _Tp, typename _Compare>
+    inline pair<const _Tp&, const _Tp&>
+    minmax(const _Tp& __a, const _Tp& __b, _Compare __comp)
     {
-      // concept requirements
-      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-           typename iterator_traits<_ForwardIterator>::value_type,
-           typename iterator_traits<_ForwardIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
-
-      if (__first == __last) return __first;
-      _ForwardIterator __result = __first;
-      while (++__first != __last)
-       if (__comp(*__result, *__first)) __result = __first;
-      return __result;
+      return __comp(__b, __a) ? pair<const _Tp&, const _Tp&>(__b, __a)
+                             : pair<const _Tp&, const _Tp&>(__a, __b);
     }
 
   /**
-   *  @brief  Return the minimum element in a range.
+   *  @brief  Return a pair of iterators pointing to the minimum and maximum
+   *          elements in a range.
+   *  @ingroup sorting_algorithms
    *  @param  first  Start of range.
    *  @param  last   End of range.
-   *  @return  Iterator referencing the first instance of the smallest value.
+   *  @return  make_pair(m, M), where m is the first iterator i in 
+   *           [first, last) such that no other element in the range is
+   *           smaller, and where M is the last iterator i in [first, last)
+   *           such that no other element in the range is larger.
   */
   template<typename _ForwardIterator>
-    _ForwardIterator
-    min_element(_ForwardIterator __first, _ForwardIterator __last)
+    pair<_ForwardIterator, _ForwardIterator>
+    minmax_element(_ForwardIterator __first, _ForwardIterator __last)
     {
       // concept requirements
       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
@@ -4280,26 +3945,76 @@ namespace std
            typename iterator_traits<_ForwardIterator>::value_type>)
       __glibcxx_requires_valid_range(__first, __last);
 
-      if (__first == __last) return __first;
-      _ForwardIterator __result = __first;
-      while (++__first != __last)
-       if (*__first < *__result)
-         __result = __first;
-      return __result;
+      _ForwardIterator __next = __first;
+      if (__first == __last
+         || ++__next == __last)
+       return std::make_pair(__first, __first);
+
+      _ForwardIterator __min, __max;
+      if (*__next < *__first)
+       {
+         __min = __next;
+         __max = __first;
+       }
+      else
+       {
+         __min = __first;
+         __max = __next;
+       }
+
+      __first = __next;
+      ++__first;
+
+      while (__first != __last)
+       {
+         __next = __first;
+         if (++__next == __last)
+           {
+             if (*__first < *__min)
+               __min = __first;
+             else if (!(*__first < *__max))
+               __max = __first;
+             break;
+           }
+
+         if (*__next < *__first)
+           {
+             if (*__next < *__min)
+               __min = __next;
+             if (!(*__first < *__max))
+               __max = __first;
+           }
+         else
+           {
+             if (*__first < *__min)
+               __min = __first;
+             if (!(*__next < *__max))
+               __max = __next;
+           }
+
+         __first = __next;
+         ++__first;
+       }
+
+      return std::make_pair(__min, __max);
     }
 
   /**
-   *  @brief  Return the minimum element in a range using comparison functor.
+   *  @brief  Return a pair of iterators pointing to the minimum and maximum
+   *          elements in a range.
+   *  @ingroup sorting_algorithms
    *  @param  first  Start of range.
    *  @param  last   End of range.
    *  @param  comp   Comparison functor.
-   *  @return  Iterator referencing the first instance of the smallest value
-   *  according to comp.
+   *  @return  make_pair(m, M), where m is the first iterator i in 
+   *           [first, last) such that no other element in the range is
+   *           smaller, and where M is the last iterator i in [first, last)
+   *           such that no other element in the range is larger.
   */
   template<typename _ForwardIterator, typename _Compare>
-    _ForwardIterator
-    min_element(_ForwardIterator __first, _ForwardIterator __last,
-               _Compare __comp)
+    pair<_ForwardIterator, _ForwardIterator>
+    minmax_element(_ForwardIterator __first, _ForwardIterator __last,
+                  _Compare __comp)
     {
       // concept requirements
       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
@@ -4308,231 +4023,226 @@ namespace std
            typename iterator_traits<_ForwardIterator>::value_type>)
       __glibcxx_requires_valid_range(__first, __last);
 
-      if (__first == __last) return __first;
-      _ForwardIterator __result = __first;
-      while (++__first != __last)
-       if (__comp(*__first, *__result))
-         __result = __first;
-      return __result;
-    }
+      _ForwardIterator __next = __first;
+      if (__first == __last
+         || ++__next == __last)
+       return std::make_pair(__first, __first);
+
+      _ForwardIterator __min, __max;
+      if (__comp(*__next, *__first))
+       {
+         __min = __next;
+         __max = __first;
+       }
+      else
+       {
+         __min = __first;
+         __max = __next;
+       }
 
-  // next_permutation and prev_permutation, with and without an explicitly
-  // supplied comparison function.
+      __first = __next;
+      ++__first;
 
-  /**
-   *  @brief  Permute range into the next "dictionary" ordering.
-   *  @param  first  Start of range.
-   *  @param  last   End of range.
-   *  @return  False if wrapped to first permutation, true otherwise.
-   *
-   *  Treats all permutations of the range as a set of "dictionary" sorted
-   *  sequences.  Permutes the current sequence into the next one of this set.
-   *  Returns true if there are more sequences to generate.  If the sequence
-   *  is the largest of the set, the smallest is generated and false returned.
-  */
-  template<typename _BidirectionalIterator>
-    bool
-    next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
-    {
-      // concept requirements
-      __glibcxx_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>)
-      __glibcxx_function_requires(_LessThanComparableConcept<
-           typename iterator_traits<_BidirectionalIterator>::value_type>)
-      __glibcxx_requires_valid_range(__first, __last);
+      while (__first != __last)
+       {
+         __next = __first;
+         if (++__next == __last)
+           {
+             if (__comp(*__first, *__min))
+               __min = __first;
+             else if (!__comp(*__first, *__max))
+               __max = __first;
+             break;
+           }
 
-      if (__first == __last)
-       return false;
-      _BidirectionalIterator __i = __first;
-      ++__i;
-      if (__i == __last)
-       return false;
-      __i = __last;
-      --__i;
+         if (__comp(*__next, *__first))
+           {
+             if (__comp(*__next, *__min))
+               __min = __next;
+             if (!__comp(*__first, *__max))
+               __max = __first;
+           }
+         else
+           {
+             if (__comp(*__first, *__min))
+               __min = __first;
+             if (!__comp(*__next, *__max))
+               __max = __next;
+           }
 
-      for(;;) {
-       _BidirectionalIterator __ii = __i;
-       --__i;
-       if (*__i < *__ii) {
-         _BidirectionalIterator __j = __last;
-         while (!(*__i < *--__j))
-           {}
-         std::iter_swap(__i, __j);
-         std::reverse(__ii, __last);
-         return true;
-       }
-       if (__i == __first) {
-         std::reverse(__first, __last);
-         return false;
+         __first = __next;
+         ++__first;
        }
-      }
+
+      return std::make_pair(__min, __max);
+    }
+
+  // N2722 + DR 915.
+  template<typename _Tp>
+    inline _Tp
+    min(initializer_list<_Tp> __l)
+    { return *std::min_element(__l.begin(), __l.end()); }
+
+  template<typename _Tp, typename _Compare>
+    inline _Tp
+    min(initializer_list<_Tp> __l, _Compare __comp)
+    { return *std::min_element(__l.begin(), __l.end(), __comp); }
+
+  template<typename _Tp>
+    inline _Tp
+    max(initializer_list<_Tp> __l)
+    { return *std::max_element(__l.begin(), __l.end()); }
+
+  template<typename _Tp, typename _Compare>
+    inline _Tp
+    max(initializer_list<_Tp> __l, _Compare __comp)
+    { return *std::max_element(__l.begin(), __l.end(), __comp); }
+
+  template<typename _Tp>
+    inline pair<_Tp, _Tp>
+    minmax(initializer_list<_Tp> __l)
+    {
+      pair<const _Tp*, const _Tp*> __p =
+       std::minmax_element(__l.begin(), __l.end());
+      return std::make_pair(*__p.first, *__p.second);
+    }
+
+  template<typename _Tp, typename _Compare>
+    inline pair<_Tp, _Tp>
+    minmax(initializer_list<_Tp> __l, _Compare __comp)
+    {
+      pair<const _Tp*, const _Tp*> __p =
+       std::minmax_element(__l.begin(), __l.end(), __comp);
+      return std::make_pair(*__p.first, *__p.second);
     }
 
+#ifdef _GLIBCXX_USE_C99_STDINT_TR1
   /**
-   *  @brief  Permute range into the next "dictionary" ordering using
-   *  comparison functor.
-   *  @param  first  Start of range.
-   *  @param  last   End of range.
-   *  @param  comp   
-   *  @return  False if wrapped to first permutation, true otherwise.
+   *  @brief Shuffle the elements of a sequence using a uniform random
+   *         number generator.
+   *  @ingroup mutating_algorithms
+   *  @param  first   A forward iterator.
+   *  @param  last    A forward iterator.
+   *  @param  g       A UniformRandomNumberGenerator (26.5.1.3).
+   *  @return  Nothing.
    *
-   *  Treats all permutations of the range [first,last) as a set of
-   *  "dictionary" sorted sequences ordered by @a comp.  Permutes the current
-   *  sequence into the next one of this set.  Returns true if there are more
-   *  sequences to generate.  If the sequence is the largest of the set, the
-   *  smallest is generated and false returned.
+   *  Reorders the elements in the range @p [first,last) using @p g to
+   *  provide random numbers.
   */
-  template<typename _BidirectionalIterator, typename _Compare>
-    bool
-    next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last,
-                    _Compare __comp)
+  template<typename _RandomAccessIterator,
+          typename _UniformRandomNumberGenerator>
+    void
+    shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
+           _UniformRandomNumberGenerator& __g)
     {
       // concept requirements
-      __glibcxx_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-           typename iterator_traits<_BidirectionalIterator>::value_type,
-           typename iterator_traits<_BidirectionalIterator>::value_type>)
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
       __glibcxx_requires_valid_range(__first, __last);
 
       if (__first == __last)
-       return false;
-      _BidirectionalIterator __i = __first;
-      ++__i;
-      if (__i == __last)
-       return false;
-      __i = __last;
-      --__i;
+       return;
 
-      for(;;) {
-       _BidirectionalIterator __ii = __i;
-       --__i;
-       if (__comp(*__i, *__ii)) {
-         _BidirectionalIterator __j = __last;
-         while (!__comp(*__i, *--__j))
-           {}
-         std::iter_swap(__i, __j);
-         std::reverse(__ii, __last);
-         return true;
-       }
-       if (__i == __first) {
-         std::reverse(__first, __last);
-         return false;
-       }
-      }
+      typedef typename iterator_traits<_RandomAccessIterator>::difference_type
+       _DistanceType;
+
+      typedef typename std::make_unsigned<_DistanceType>::type __ud_type;
+      typedef typename std::uniform_int_distribution<__ud_type> __distr_type;
+      typedef typename __distr_type::param_type __p_type;
+      __distr_type __d;
+
+      for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
+       std::iter_swap(__i, __first + __d(__g, __p_type(0, __i - __first)));
     }
+#endif
+
+#endif // __GXX_EXPERIMENTAL_CXX0X__
+
+_GLIBCXX_END_NAMESPACE
+
+_GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
-   *  @brief  Permute range into the previous "dictionary" ordering.
-   *  @param  first  Start of range.
-   *  @param  last   End of range.
-   *  @return  False if wrapped to last permutation, true otherwise.
+   *  @brief Apply a function to every element of a sequence.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first  An input iterator.
+   *  @param  last   An input iterator.
+   *  @param  f      A unary function object.
+   *  @return   @p f (std::move(@p f) in C++0x).
    *
-   *  Treats all permutations of the range as a set of "dictionary" sorted
-   *  sequences.  Permutes the current sequence into the previous one of this
-   *  set.  Returns true if there are more sequences to generate.  If the
-   *  sequence is the smallest of the set, the largest is generated and false
-   *  returned.
+   *  Applies the function object @p f to each element in the range
+   *  @p [first,last).  @p f must not modify the order of the sequence.
+   *  If @p f has a return value it is ignored.
   */
-  template<typename _BidirectionalIterator>
-    bool
-    prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last)
+  template<typename _InputIterator, typename _Function>
+    _Function
+    for_each(_InputIterator __first, _InputIterator __last, _Function __f)
     {
       // concept requirements
-      __glibcxx_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>)
-      __glibcxx_function_requires(_LessThanComparableConcept<
-           typename iterator_traits<_BidirectionalIterator>::value_type>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
       __glibcxx_requires_valid_range(__first, __last);
-
-      if (__first == __last)
-       return false;
-      _BidirectionalIterator __i = __first;
-      ++__i;
-      if (__i == __last)
-       return false;
-      __i = __last;
-      --__i;
-
-      for(;;) {
-       _BidirectionalIterator __ii = __i;
-       --__i;
-       if (*__ii < *__i) {
-         _BidirectionalIterator __j = __last;
-         while (!(*--__j < *__i))
-           {}
-         std::iter_swap(__i, __j);
-         std::reverse(__ii, __last);
-         return true;
-       }
-       if (__i == __first) {
-         std::reverse(__first, __last);
-         return false;
-       }
-      }
+      for (; __first != __last; ++__first)
+       __f(*__first);
+      return _GLIBCXX_MOVE(__f);
     }
 
   /**
-   *  @brief  Permute range into the previous "dictionary" ordering using
-   *  comparison functor.
-   *  @param  first  Start of range.
-   *  @param  last   End of range.
-   *  @param  comp   
-   *  @return  False if wrapped to last permutation, true otherwise.
-   *
-   *  Treats all permutations of the range [first,last) as a set of
-   *  "dictionary" sorted sequences ordered by @a comp.  Permutes the current
-   *  sequence into the previous one of this set.  Returns true if there are
-   *  more sequences to generate.  If the sequence is the smallest of the set,
-   *  the largest is generated and false returned.
+   *  @brief Find the first occurrence of a value in a sequence.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first  An input iterator.
+   *  @param  last   An input iterator.
+   *  @param  val    The value to find.
+   *  @return   The first iterator @c i in the range @p [first,last)
+   *  such that @c *i == @p val, or @p last if no such iterator exists.
   */
-  template<typename _BidirectionalIterator, typename _Compare>
-    bool
-    prev_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last,
-                    _Compare __comp)
+  template<typename _InputIterator, typename _Tp>
+    inline _InputIterator
+    find(_InputIterator __first, _InputIterator __last,
+        const _Tp& __val)
     {
       // concept requirements
-      __glibcxx_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator>)
-      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
-           typename iterator_traits<_BidirectionalIterator>::value_type,
-           typename iterator_traits<_BidirectionalIterator>::value_type>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcxx_function_requires(_EqualOpConcept<
+               typename iterator_traits<_InputIterator>::value_type, _Tp>)
       __glibcxx_requires_valid_range(__first, __last);
-
-      if (__first == __last)
-       return false;
-      _BidirectionalIterator __i = __first;
-      ++__i;
-      if (__i == __last)
-       return false;
-      __i = __last;
-      --__i;
-
-      for(;;) {
-       _BidirectionalIterator __ii = __i;
-       --__i;
-       if (__comp(*__ii, *__i)) {
-         _BidirectionalIterator __j = __last;
-         while (!__comp(*--__j, *__i))
-           {}
-         std::iter_swap(__i, __j);
-         std::reverse(__ii, __last);
-         return true;
-       }
-       if (__i == __first) {
-         std::reverse(__first, __last);
-         return false;
-       }
-      }
+      return std::__find(__first, __last, __val,
+                        std::__iterator_category(__first));
     }
 
-  // find_first_of, with and without an explicitly supplied comparison function.
+  /**
+   *  @brief Find the first element in a sequence for which a
+   *         predicate is true.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first  An input iterator.
+   *  @param  last   An input iterator.
+   *  @param  pred   A predicate.
+   *  @return   The first iterator @c i in the range @p [first,last)
+   *  such that @p pred(*i) is true, or @p last if no such iterator exists.
+  */
+  template<typename _InputIterator, typename _Predicate>
+    inline _InputIterator
+    find_if(_InputIterator __first, _InputIterator __last,
+           _Predicate __pred)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
+             typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+      return std::__find_if(__first, __last, __pred,
+                           std::__iterator_category(__first));
+    }
 
   /**
    *  @brief  Find element from a set in a sequence.
+   *  @ingroup non_mutating_algorithms
    *  @param  first1  Start of range to search.
    *  @param  last1   End of range to search.
    *  @param  first2  Start of match candidates.
    *  @param  last2   End of match candidates.
    *  @return   The first iterator @c i in the range
    *  @p [first1,last1) such that @c *i == @p *(i2) such that i2 is an
-   *  interator in [first2,last2), or @p last1 if no such iterator exists.
+   *  iterator in [first2,last2), or @p last1 if no such iterator exists.
    *
    *  Searches the range @p [first1,last1) for an element that is equal to
    *  some element in the range [first2,last2).  If found, returns an iterator
@@ -4552,7 +4262,7 @@ namespace std
       __glibcxx_requires_valid_range(__first1, __last1);
       __glibcxx_requires_valid_range(__first2, __last2);
 
-      for ( ; __first1 != __last1; ++__first1)
+      for (; __first1 != __last1; ++__first1)
        for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
          if (*__first1 == *__iter)
            return __first1;
@@ -4561,6 +4271,7 @@ namespace std
 
   /**
    *  @brief  Find element from a set in a sequence using a predicate.
+   *  @ingroup non_mutating_algorithms
    *  @param  first1  Start of range to search.
    *  @param  last1   End of range to search.
    *  @param  first2  Start of match candidates.
@@ -4568,13 +4279,16 @@ namespace std
    *  @param  comp    Predicate to use.
    *  @return   The first iterator @c i in the range
    *  @p [first1,last1) such that @c comp(*i, @p *(i2)) is true and i2 is an
-   *  interator in [first2,last2), or @p last1 if no such iterator exists.
+   *  iterator in [first2,last2), or @p last1 if no such iterator exists.
    *
-   *  Searches the range @p [first1,last1) for an element that is equal to
-   *  some element in the range [first2,last2).  If found, returns an iterator in
-   *  the range [first1,last1), otherwise returns @p last1.
+
+   *  Searches the range @p [first1,last1) for an element that is
+   *  equal to some element in the range [first2,last2).  If found,
+   *  returns an iterator in the range [first1,last1), otherwise
+   *  returns @p last1.
   */
-  template<typename _InputIterator, typename _ForwardIterator, typename _BinaryPredicate>
+  template<typename _InputIterator, typename _ForwardIterator,
+          typename _BinaryPredicate>
     _InputIterator
     find_first_of(_InputIterator __first1, _InputIterator __last1,
                  _ForwardIterator __first2, _ForwardIterator __last2,
@@ -4583,146 +4297,141 @@ namespace std
       // concept requirements
       __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_EqualOpConcept<
-           typename iterator_traits<_InputIterator>::value_type,
-           typename iterator_traits<_ForwardIterator>::value_type>)
       __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
            typename iterator_traits<_InputIterator>::value_type,
            typename iterator_traits<_ForwardIterator>::value_type>)
       __glibcxx_requires_valid_range(__first1, __last1);
       __glibcxx_requires_valid_range(__first2, __last2);
 
-      for ( ; __first1 != __last1; ++__first1)
+      for (; __first1 != __last1; ++__first1)
        for (_ForwardIterator __iter = __first2; __iter != __last2; ++__iter)
          if (__comp(*__first1, *__iter))
            return __first1;
       return __last1;
     }
 
-
-  // find_end, with and without an explicitly supplied comparison function.
-  // Search [first2, last2) as a subsequence in [first1, last1), and return
-  // the *last* possible match.  Note that find_end for bidirectional iterators
-  // is much faster than for forward iterators.
-
-  // find_end for forward iterators.
-  template<typename _ForwardIterator1, typename _ForwardIterator2>
-    _ForwardIterator1
-    __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
-              _ForwardIterator2 __first2, _ForwardIterator2 __last2,
-              forward_iterator_tag, forward_iterator_tag)
+  /**
+   *  @brief Find two adjacent values in a sequence that are equal.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first  A forward iterator.
+   *  @param  last   A forward iterator.
+   *  @return   The first iterator @c i such that @c i and @c i+1 are both
+   *  valid iterators in @p [first,last) and such that @c *i == @c *(i+1),
+   *  or @p last if no such iterator exists.
+  */
+  template<typename _ForwardIterator>
+    _ForwardIterator
+    adjacent_find(_ForwardIterator __first, _ForwardIterator __last)
     {
-      if (__first2 == __last2)
-       return __last1;
-      else {
-       _ForwardIterator1 __result = __last1;
-       while (1) {
-         _ForwardIterator1 __new_result
-           = std::search(__first1, __last1, __first2, __last2);
-         if (__new_result == __last1)
-           return __result;
-         else {
-           __result = __new_result;
-           __first1 = __new_result;
-           ++__first1;
-         }
+      // concept requirements
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_EqualityComparableConcept<
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+      if (__first == __last)
+       return __last;
+      _ForwardIterator __next = __first;
+      while(++__next != __last)
+       {
+         if (*__first == *__next)
+           return __first;
+         __first = __next;
        }
-      }
+      return __last;
     }
 
-  template<typename _ForwardIterator1, typename _ForwardIterator2,
-          typename _BinaryPredicate>
-    _ForwardIterator1
-    __find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
-              _ForwardIterator2 __first2, _ForwardIterator2 __last2,
-              forward_iterator_tag, forward_iterator_tag,
-              _BinaryPredicate __comp)
+  /**
+   *  @brief Find two adjacent values in a sequence using a predicate.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first         A forward iterator.
+   *  @param  last          A forward iterator.
+   *  @param  binary_pred   A binary predicate.
+   *  @return   The first iterator @c i such that @c i and @c i+1 are both
+   *  valid iterators in @p [first,last) and such that
+   *  @p binary_pred(*i,*(i+1)) is true, or @p last if no such iterator
+   *  exists.
+  */
+  template<typename _ForwardIterator, typename _BinaryPredicate>
+    _ForwardIterator
+    adjacent_find(_ForwardIterator __first, _ForwardIterator __last,
+                 _BinaryPredicate __binary_pred)
     {
-      if (__first2 == __last2)
-       return __last1;
-      else {
-       _ForwardIterator1 __result = __last1;
-       while (1) {
-         _ForwardIterator1 __new_result
-           = std::search(__first1, __last1, __first2, __last2, __comp);
-         if (__new_result == __last1)
-           return __result;
-         else {
-           __result = __new_result;
-           __first1 = __new_result;
-           ++__first1;
-         }
+      // concept requirements
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
+           typename iterator_traits<_ForwardIterator>::value_type,
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+      if (__first == __last)
+       return __last;
+      _ForwardIterator __next = __first;
+      while(++__next != __last)
+       {
+         if (__binary_pred(*__first, *__next))
+           return __first;
+         __first = __next;
        }
-      }
+      return __last;
     }
 
-  // find_end for bidirectional iterators.  Requires partial specialization.
-  template<typename _BidirectionalIterator1, typename _BidirectionalIterator2>
-    _BidirectionalIterator1
-    __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
-              _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2,
-              bidirectional_iterator_tag, bidirectional_iterator_tag)
+  /**
+   *  @brief Count the number of copies of a value in a sequence.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first  An input iterator.
+   *  @param  last   An input iterator.
+   *  @param  value  The value to be counted.
+   *  @return   The number of iterators @c i in the range @p [first,last)
+   *  for which @c *i == @p value
+  */
+  template<typename _InputIterator, typename _Tp>
+    typename iterator_traits<_InputIterator>::difference_type
+    count(_InputIterator __first, _InputIterator __last, const _Tp& __value)
     {
       // concept requirements
-      __glibcxx_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator1>)
-      __glibcxx_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator2>)
-
-      typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
-      typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
-
-      _RevIterator1 __rlast1(__first1);
-      _RevIterator2 __rlast2(__first2);
-      _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
-                                           _RevIterator2(__last2), __rlast2);
-
-      if (__rresult == __rlast1)
-       return __last1;
-      else {
-       _BidirectionalIterator1 __result = __rresult.base();
-       std::advance(__result, -std::distance(__first2, __last2));
-       return __result;
-      }
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcxx_function_requires(_EqualOpConcept<
+       typename iterator_traits<_InputIterator>::value_type, _Tp>)
+      __glibcxx_requires_valid_range(__first, __last);
+      typename iterator_traits<_InputIterator>::difference_type __n = 0;
+      for (; __first != __last; ++__first)
+       if (*__first == __value)
+         ++__n;
+      return __n;
     }
 
-  template<typename _BidirectionalIterator1, typename _BidirectionalIterator2,
-          typename _BinaryPredicate>
-    _BidirectionalIterator1
-    __find_end(_BidirectionalIterator1 __first1, _BidirectionalIterator1 __last1,
-              _BidirectionalIterator2 __first2, _BidirectionalIterator2 __last2,
-              bidirectional_iterator_tag, bidirectional_iterator_tag,
-              _BinaryPredicate __comp)
+  /**
+   *  @brief Count the elements of a sequence for which a predicate is true.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first  An input iterator.
+   *  @param  last   An input iterator.
+   *  @param  pred   A predicate.
+   *  @return   The number of iterators @c i in the range @p [first,last)
+   *  for which @p pred(*i) is true.
+  */
+  template<typename _InputIterator, typename _Predicate>
+    typename iterator_traits<_InputIterator>::difference_type
+    count_if(_InputIterator __first, _InputIterator __last, _Predicate __pred)
     {
       // concept requirements
-      __glibcxx_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator1>)
-      __glibcxx_function_requires(_BidirectionalIteratorConcept<_BidirectionalIterator2>)
-
-      typedef reverse_iterator<_BidirectionalIterator1> _RevIterator1;
-      typedef reverse_iterator<_BidirectionalIterator2> _RevIterator2;
-
-      _RevIterator1 __rlast1(__first1);
-      _RevIterator2 __rlast2(__first2);
-      _RevIterator1 __rresult = std::search(_RevIterator1(__last1), __rlast1,
-                                           _RevIterator2(__last2), __rlast2,
-                                           __comp);
-
-      if (__rresult == __rlast1)
-       return __last1;
-      else {
-       _BidirectionalIterator1 __result = __rresult.base();
-       std::advance(__result, -std::distance(__first2, __last2));
-       return __result;
-      }
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
+           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+      typename iterator_traits<_InputIterator>::difference_type __n = 0;
+      for (; __first != __last; ++__first)
+       if (__pred(*__first))
+         ++__n;
+      return __n;
     }
 
-  // Dispatching functions for find_end.
-
   /**
-   *  @brief  Find last matching subsequence in a sequence.
-   *  @param  first1  Start of range to search.
-   *  @param  last1   End of range to search.
-   *  @param  first2  Start of sequence to match.
-   *  @param  last2   End of sequence to match.
-   *  @return   The last iterator @c i in the range
+   *  @brief Search a sequence for a matching sub-sequence.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first1  A forward iterator.
+   *  @param  last1   A forward iterator.
+   *  @param  first2  A forward iterator.
+   *  @param  last2   A forward iterator.
+   *  @return   The first iterator @c i in the range
    *  @p [first1,last1-(last2-first2)) such that @c *(i+N) == @p *(first2+N)
    *  for each @c N in the range @p [0,last2-first2), or @p last1 if no
    *  such iterator exists.
@@ -4730,8 +4439,7 @@ namespace std
    *  Searches the range @p [first1,last1) for a sub-sequence that compares
    *  equal value-by-value with the sequence given by @p [first2,last2) and
    *  returns an iterator to the first element of the sub-sequence, or
-   *  @p last1 if the sub-sequence is not found.  The sub-sequence will be the
-   *  last such subsequence contained in [first,last1).
+   *  @p last1 if the sub-sequence is not found.
    *
    *  Because the sub-sequence must lie completely within the range
    *  @p [first1,last1) it must start at a position less than
@@ -4741,9 +4449,9 @@ namespace std
    *  @p [first1,last1-(last2-first2))
   */
   template<typename _ForwardIterator1, typename _ForwardIterator2>
-    inline _ForwardIterator1
-    find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
-            _ForwardIterator2 __first2, _ForwardIterator2 __last2)
+    _ForwardIterator1
+    search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
+          _ForwardIterator2 __first2, _ForwardIterator2 __last2)
     {
       // concept requirements
       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
@@ -4754,43 +4462,69 @@ namespace std
       __glibcxx_requires_valid_range(__first1, __last1);
       __glibcxx_requires_valid_range(__first2, __last2);
 
-      return std::__find_end(__first1, __last1, __first2, __last2,
-                            std::__iterator_category(__first1),
-                            std::__iterator_category(__first2));
+      // Test for empty ranges
+      if (__first1 == __last1 || __first2 == __last2)
+       return __first1;
+
+      // Test for a pattern of length 1.
+      _ForwardIterator2 __p1(__first2);
+      if (++__p1 == __last2)
+       return _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
+
+      // General case.
+      _ForwardIterator2 __p;
+      _ForwardIterator1 __current = __first1;
+
+      for (;;)
+       {
+         __first1 = _GLIBCXX_STD_P::find(__first1, __last1, *__first2);
+         if (__first1 == __last1)
+           return __last1;
+
+         __p = __p1;
+         __current = __first1;
+         if (++__current == __last1)
+           return __last1;
+
+         while (*__current == *__p)
+           {
+             if (++__p == __last2)
+               return __first1;
+             if (++__current == __last1)
+               return __last1;
+           }
+         ++__first1;
+       }
+      return __first1;
     }
 
   /**
-   *  @brief  Find last matching subsequence in a sequence using a predicate.
-   *  @param  first1  Start of range to search.
-   *  @param  last1   End of range to search.
-   *  @param  first2  Start of sequence to match.
-   *  @param  last2   End of sequence to match.
-   *  @param  comp    The predicate to use.
-   *  @return   The last iterator @c i in the range
-   *  @p [first1,last1-(last2-first2)) such that @c predicate(*(i+N), @p
-   *  (first2+N)) is true for each @c N in the range @p [0,last2-first2), or
-   *  @p last1 if no such iterator exists.
+   *  @brief Search a sequence for a matching sub-sequence using a predicate.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first1     A forward iterator.
+   *  @param  last1      A forward iterator.
+   *  @param  first2     A forward iterator.
+   *  @param  last2      A forward iterator.
+   *  @param  predicate  A binary predicate.
+   *  @return   The first iterator @c i in the range
+   *  @p [first1,last1-(last2-first2)) such that
+   *  @p predicate(*(i+N),*(first2+N)) is true for each @c N in the range
+   *  @p [0,last2-first2), or @p last1 if no such iterator exists.
    *
    *  Searches the range @p [first1,last1) for a sub-sequence that compares
-   *  equal value-by-value with the sequence given by @p [first2,last2) using
-   *  comp as a predicate and returns an iterator to the first element of the
-   *  sub-sequence, or @p last1 if the sub-sequence is not found.  The
-   *  sub-sequence will be the last such subsequence contained in
-   *  [first,last1).
+   *  equal value-by-value with the sequence given by @p [first2,last2),
+   *  using @p predicate to determine equality, and returns an iterator
+   *  to the first element of the sub-sequence, or @p last1 if no such
+   *  iterator exists.
    *
-   *  Because the sub-sequence must lie completely within the range
-   *  @p [first1,last1) it must start at a position less than
-   *  @p last1-(last2-first2) where @p last2-first2 is the length of the
-   *  sub-sequence.
-   *  This means that the returned iterator @c i will be in the range
-   *  @p [first1,last1-(last2-first2))
+   *  @see search(_ForwardIter1, _ForwardIter1, _ForwardIter2, _ForwardIter2)
   */
   template<typename _ForwardIterator1, typename _ForwardIterator2,
           typename _BinaryPredicate>
-    inline _ForwardIterator1
-    find_end(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
-            _ForwardIterator2 __first2, _ForwardIterator2 __last2,
-            _BinaryPredicate __comp)
+    _ForwardIterator1
+    search(_ForwardIterator1 __first1, _ForwardIterator1 __last1,
+          _ForwardIterator2 __first2, _ForwardIterator2 __last2,
+          _BinaryPredicate  __predicate)
     {
       // concept requirements
       __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator1>)
@@ -4801,13 +4535,1549 @@ namespace std
       __glibcxx_requires_valid_range(__first1, __last1);
       __glibcxx_requires_valid_range(__first2, __last2);
 
-      return std::__find_end(__first1, __last1, __first2, __last2,
-                            std::__iterator_category(__first1),
-                            std::__iterator_category(__first2),
-                            __comp);
+      // Test for empty ranges
+      if (__first1 == __last1 || __first2 == __last2)
+       return __first1;
+
+      // Test for a pattern of length 1.
+      _ForwardIterator2 __p1(__first2);
+      if (++__p1 == __last2)
+       {
+         while (__first1 != __last1
+                && !bool(__predicate(*__first1, *__first2)))
+           ++__first1;
+         return __first1;
+       }
+
+      // General case.
+      _ForwardIterator2 __p;
+      _ForwardIterator1 __current = __first1;
+
+      for (;;)
+       {
+         while (__first1 != __last1
+                && !bool(__predicate(*__first1, *__first2)))
+           ++__first1;
+         if (__first1 == __last1)
+           return __last1;
+
+         __p = __p1;
+         __current = __first1;
+         if (++__current == __last1)
+           return __last1;
+
+         while (__predicate(*__current, *__p))
+           {
+             if (++__p == __last2)
+               return __first1;
+             if (++__current == __last1)
+               return __last1;
+           }
+         ++__first1;
+       }
+      return __first1;
+    }
+
+
+  /**
+   *  @brief Search a sequence for a number of consecutive values.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first  A forward iterator.
+   *  @param  last   A forward iterator.
+   *  @param  count  The number of consecutive values.
+   *  @param  val    The value to find.
+   *  @return   The first iterator @c i in the range @p [first,last-count)
+   *  such that @c *(i+N) == @p val for each @c N in the range @p [0,count),
+   *  or @p last if no such iterator exists.
+   *
+   *  Searches the range @p [first,last) for @p count consecutive elements
+   *  equal to @p val.
+  */
+  template<typename _ForwardIterator, typename _Integer, typename _Tp>
+    _ForwardIterator
+    search_n(_ForwardIterator __first, _ForwardIterator __last,
+            _Integer __count, const _Tp& __val)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_EqualOpConcept<
+       typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__count <= 0)
+       return __first;
+      if (__count == 1)
+       return _GLIBCXX_STD_P::find(__first, __last, __val);
+      return std::__search_n(__first, __last, __count, __val,
+                            std::__iterator_category(__first));
+    }
+
+
+  /**
+   *  @brief Search a sequence for a number of consecutive values using a
+   *         predicate.
+   *  @ingroup non_mutating_algorithms
+   *  @param  first        A forward iterator.
+   *  @param  last         A forward iterator.
+   *  @param  count        The number of consecutive values.
+   *  @param  val          The value to find.
+   *  @param  binary_pred  A binary predicate.
+   *  @return   The first iterator @c i in the range @p [first,last-count)
+   *  such that @p binary_pred(*(i+N),val) is true for each @c N in the
+   *  range @p [0,count), or @p last if no such iterator exists.
+   *
+   *  Searches the range @p [first,last) for @p count consecutive elements
+   *  for which the predicate returns true.
+  */
+  template<typename _ForwardIterator, typename _Integer, typename _Tp,
+           typename _BinaryPredicate>
+    _ForwardIterator
+    search_n(_ForwardIterator __first, _ForwardIterator __last,
+            _Integer __count, const _Tp& __val,
+            _BinaryPredicate __binary_pred)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_BinaryPredicate,
+           typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__count <= 0)
+       return __first;
+      if (__count == 1)
+       {
+         while (__first != __last && !bool(__binary_pred(*__first, __val)))
+           ++__first;
+         return __first;
+       }
+      return std::__search_n(__first, __last, __count, __val, __binary_pred,
+                            std::__iterator_category(__first));
+    }
+
+
+  /**
+   *  @brief Perform an operation on a sequence.
+   *  @ingroup mutating_algorithms
+   *  @param  first     An input iterator.
+   *  @param  last      An input iterator.
+   *  @param  result    An output iterator.
+   *  @param  unary_op  A unary operator.
+   *  @return   An output iterator equal to @p result+(last-first).
+   *
+   *  Applies the operator to each element in the input range and assigns
+   *  the results to successive elements of the output sequence.
+   *  Evaluates @p *(result+N)=unary_op(*(first+N)) for each @c N in the
+   *  range @p [0,last-first).
+   *
+   *  @p unary_op must not alter its argument.
+  */
+  template<typename _InputIterator, typename _OutputIterator,
+          typename _UnaryOperation>
+    _OutputIterator
+    transform(_InputIterator __first, _InputIterator __last,
+             _OutputIterator __result, _UnaryOperation __unary_op)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+            // "the type returned by a _UnaryOperation"
+            __typeof__(__unary_op(*__first))>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      for (; __first != __last; ++__first, ++__result)
+       *__result = __unary_op(*__first);
+      return __result;
+    }
+
+  /**
+   *  @brief Perform an operation on corresponding elements of two sequences.
+   *  @ingroup mutating_algorithms
+   *  @param  first1     An input iterator.
+   *  @param  last1      An input iterator.
+   *  @param  first2     An input iterator.
+   *  @param  result     An output iterator.
+   *  @param  binary_op  A binary operator.
+   *  @return   An output iterator equal to @p result+(last-first).
+   *
+   *  Applies the operator to the corresponding elements in the two
+   *  input ranges and assigns the results to successive elements of the
+   *  output sequence.
+   *  Evaluates @p *(result+N)=binary_op(*(first1+N),*(first2+N)) for each
+   *  @c N in the range @p [0,last1-first1).
+   *
+   *  @p binary_op must not alter either of its arguments.
+  */
+  template<typename _InputIterator1, typename _InputIterator2,
+          typename _OutputIterator, typename _BinaryOperation>
+    _OutputIterator
+    transform(_InputIterator1 __first1, _InputIterator1 __last1,
+             _InputIterator2 __first2, _OutputIterator __result,
+             _BinaryOperation __binary_op)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+            // "the type returned by a _BinaryOperation"
+            __typeof__(__binary_op(*__first1,*__first2))>)
+      __glibcxx_requires_valid_range(__first1, __last1);
+
+      for (; __first1 != __last1; ++__first1, ++__first2, ++__result)
+       *__result = __binary_op(*__first1, *__first2);
+      return __result;
+    }
+
+  /**
+   *  @brief Replace each occurrence of one value in a sequence with another
+   *         value.
+   *  @ingroup mutating_algorithms
+   *  @param  first      A forward iterator.
+   *  @param  last       A forward iterator.
+   *  @param  old_value  The value to be replaced.
+   *  @param  new_value  The replacement value.
+   *  @return   replace() returns no value.
+   *
+   *  For each iterator @c i in the range @p [first,last) if @c *i ==
+   *  @p old_value then the assignment @c *i = @p new_value is performed.
+  */
+  template<typename _ForwardIterator, typename _Tp>
+    void
+    replace(_ForwardIterator __first, _ForwardIterator __last,
+           const _Tp& __old_value, const _Tp& __new_value)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
+                                 _ForwardIterator>)
+      __glibcxx_function_requires(_EqualOpConcept<
+           typename iterator_traits<_ForwardIterator>::value_type, _Tp>)
+      __glibcxx_function_requires(_ConvertibleConcept<_Tp,
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      for (; __first != __last; ++__first)
+       if (*__first == __old_value)
+         *__first = __new_value;
+    }
+
+  /**
+   *  @brief Replace each value in a sequence for which a predicate returns
+   *         true with another value.
+   *  @ingroup mutating_algorithms
+   *  @param  first      A forward iterator.
+   *  @param  last       A forward iterator.
+   *  @param  pred       A predicate.
+   *  @param  new_value  The replacement value.
+   *  @return   replace_if() returns no value.
+   *
+   *  For each iterator @c i in the range @p [first,last) if @p pred(*i)
+   *  is true then the assignment @c *i = @p new_value is performed.
+  */
+  template<typename _ForwardIterator, typename _Predicate, typename _Tp>
+    void
+    replace_if(_ForwardIterator __first, _ForwardIterator __last,
+              _Predicate __pred, const _Tp& __new_value)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
+                                 _ForwardIterator>)
+      __glibcxx_function_requires(_ConvertibleConcept<_Tp,
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      for (; __first != __last; ++__first)
+       if (__pred(*__first))
+         *__first = __new_value;
+    }
+
+  /**
+   *  @brief Assign the result of a function object to each value in a
+   *         sequence.
+   *  @ingroup mutating_algorithms
+   *  @param  first  A forward iterator.
+   *  @param  last   A forward iterator.
+   *  @param  gen    A function object taking no arguments and returning
+   *                 std::iterator_traits<_ForwardIterator>::value_type
+   *  @return   generate() returns no value.
+   *
+   *  Performs the assignment @c *i = @p gen() for each @c i in the range
+   *  @p [first,last).
+  */
+  template<typename _ForwardIterator, typename _Generator>
+    void
+    generate(_ForwardIterator __first, _ForwardIterator __last,
+            _Generator __gen)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_GeneratorConcept<_Generator,
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      for (; __first != __last; ++__first)
+       *__first = __gen();
     }
 
-} // namespace std
+  /**
+   *  @brief Assign the result of a function object to each value in a
+   *         sequence.
+   *  @ingroup mutating_algorithms
+   *  @param  first  A forward iterator.
+   *  @param  n      The length of the sequence.
+   *  @param  gen    A function object taking no arguments and returning
+   *                 std::iterator_traits<_ForwardIterator>::value_type
+   *  @return   The end of the sequence, @p first+n
+   *
+   *  Performs the assignment @c *i = @p gen() for each @c i in the range
+   *  @p [first,first+n).
+   *
+   *  _GLIBCXX_RESOLVE_LIB_DEFECTS
+   *  DR 865. More algorithms that throw away information
+  */
+  template<typename _OutputIterator, typename _Size, typename _Generator>
+    _OutputIterator
+    generate_n(_OutputIterator __first, _Size __n, _Generator __gen)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+            // "the type returned by a _Generator"
+            __typeof__(__gen())>)
+
+      for (__decltype(__n + 0) __niter = __n;
+          __niter > 0; --__niter, ++__first)
+       *__first = __gen();
+      return __first;
+    }
+
+
+  /**
+   *  @brief Copy a sequence, removing consecutive duplicate values.
+   *  @ingroup mutating_algorithms
+   *  @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.
+   *
+   *  _GLIBCXX_RESOLVE_LIB_DEFECTS
+   *  DR 241. Does unique_copy() require CopyConstructible and Assignable?
+   *  
+   *  _GLIBCXX_RESOLVE_LIB_DEFECTS
+   *  DR 538. 241 again: Does unique_copy() require CopyConstructible and 
+   *  Assignable?
+  */
+  template<typename _InputIterator, typename _OutputIterator>
+    inline _OutputIterator
+    unique_copy(_InputIterator __first, _InputIterator __last,
+               _OutputIterator __result)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_function_requires(_EqualityComparableConcept<
+           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__first == __last)
+       return __result;
+      return std::__unique_copy(__first, __last, __result,
+                               std::__iterator_category(__first),
+                               std::__iterator_category(__result));
+    }
+
+  /**
+   *  @brief Copy a sequence, removing consecutive values using a predicate.
+   *  @ingroup mutating_algorithms
+   *  @param  first        An input iterator.
+   *  @param  last         An input iterator.
+   *  @param  result       An output iterator.
+   *  @param  binary_pred  A binary predicate.
+   *  @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 for which @p binary_pred returns
+   *  true.
+   *  unique_copy() is stable, so the relative order of elements that are
+   *  copied is unchanged.
+   *
+   *  _GLIBCXX_RESOLVE_LIB_DEFECTS
+   *  DR 241. Does unique_copy() require CopyConstructible and Assignable?
+  */
+  template<typename _InputIterator, typename _OutputIterator,
+          typename _BinaryPredicate>
+    inline _OutputIterator
+    unique_copy(_InputIterator __first, _InputIterator __last,
+               _OutputIterator __result,
+               _BinaryPredicate __binary_pred)
+    {
+      // concept requirements -- predicates checked later
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+           typename iterator_traits<_InputIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__first == __last)
+       return __result;
+      return std::__unique_copy(__first, __last, __result, __binary_pred,
+                               std::__iterator_category(__first),
+                               std::__iterator_category(__result));
+    }
+
+
+  /**
+   *  @brief Randomly shuffle the elements of a sequence.
+   *  @ingroup mutating_algorithms
+   *  @param  first   A forward iterator.
+   *  @param  last    A forward iterator.
+   *  @return  Nothing.
+   *
+   *  Reorder the elements in the range @p [first,last) using a random
+   *  distribution, so that every possible ordering of the sequence is
+   *  equally likely.
+  */
+  template<typename _RandomAccessIterator>
+    inline void
+    random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__first != __last)
+       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
+         std::iter_swap(__i, __first + (std::rand() % ((__i - __first) + 1)));
+    }
+
+  /**
+   *  @brief Shuffle the elements of a sequence using a random number
+   *         generator.
+   *  @ingroup mutating_algorithms
+   *  @param  first   A forward iterator.
+   *  @param  last    A forward iterator.
+   *  @param  rand    The RNG functor or function.
+   *  @return  Nothing.
+   *
+   *  Reorders the elements in the range @p [first,last) using @p rand to
+   *  provide a random distribution. Calling @p rand(N) for a positive
+   *  integer @p N should return a randomly chosen integer from the
+   *  range [0,N).
+  */
+  template<typename _RandomAccessIterator, typename _RandomNumberGenerator>
+    void
+    random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+                  _RandomNumberGenerator&& __rand)
+#else
+                  _RandomNumberGenerator& __rand)
+#endif
+    {
+      // concept requirements
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__first == __last)
+       return;
+      for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
+       std::iter_swap(__i, __first + __rand((__i - __first) + 1));
+    }
+
+
+  /**
+   *  @brief Move elements for which a predicate is true to the beginning
+   *         of a sequence.
+   *  @ingroup mutating_algorithms
+   *  @param  first   A forward iterator.
+   *  @param  last    A forward iterator.
+   *  @param  pred    A predicate functor.
+   *  @return  An iterator @p middle such that @p pred(i) is true for each
+   *  iterator @p i in the range @p [first,middle) and false for each @p i
+   *  in the range @p [middle,last).
+   *
+   *  @p pred must not modify its operand. @p partition() does not preserve
+   *  the relative ordering of elements in each group, use
+   *  @p stable_partition() if this is needed.
+  */
+  template<typename _ForwardIterator, typename _Predicate>
+    inline _ForwardIterator
+    partition(_ForwardIterator __first, _ForwardIterator __last,
+             _Predicate   __pred)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_Mutable_ForwardIteratorConcept<
+                                 _ForwardIterator>)
+      __glibcxx_function_requires(_UnaryPredicateConcept<_Predicate,
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      return std::__partition(__first, __last, __pred,
+                             std::__iterator_category(__first));
+    }
+
+
+
+  /**
+   *  @brief Sort the smallest elements of a sequence.
+   *  @ingroup sorting_algorithms
+   *  @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>
+    inline void
+    partial_sort(_RandomAccessIterator __first,
+                _RandomAccessIterator __middle,
+                _RandomAccessIterator __last)
+    {
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type
+       _ValueType;
+
+      // concept requirements
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
+      __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
+      __glibcxx_requires_valid_range(__first, __middle);
+      __glibcxx_requires_valid_range(__middle, __last);
+
+      std::__heap_select(__first, __middle, __last);
+      std::sort_heap(__first, __middle);
+    }
+
+  /**
+   *  @brief Sort the smallest elements of a sequence using a predicate
+   *         for comparison.
+   *  @ingroup sorting_algorithms
+   *  @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>
+    inline void
+    partial_sort(_RandomAccessIterator __first,
+                _RandomAccessIterator __middle,
+                _RandomAccessIterator __last,
+                _Compare __comp)
+    {
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type
+       _ValueType;
+
+      // concept requirements
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType, _ValueType>)
+      __glibcxx_requires_valid_range(__first, __middle);
+      __glibcxx_requires_valid_range(__middle, __last);
+
+      std::__heap_select(__first, __middle, __last, __comp);
+      std::sort_heap(__first, __middle, __comp);
+    }
+
+  /**
+   *  @brief Sort a sequence just enough to find a particular position.
+   *  @ingroup sorting_algorithms
+   *  @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>
+    inline void
+    nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
+               _RandomAccessIterator __last)
+    {
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type
+       _ValueType;
+
+      // concept requirements
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+                                 _RandomAccessIterator>)
+      __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
+      __glibcxx_requires_valid_range(__first, __nth);
+      __glibcxx_requires_valid_range(__nth, __last);
+
+      if (__first == __last || __nth == __last)
+       return;
+
+      std::__introselect(__first, __nth, __last,
+                        std::__lg(__last - __first) * 2);
+    }
+
+  /**
+   *  @brief Sort a sequence just enough to find a particular position
+   *         using a predicate for comparison.
+   *  @ingroup sorting_algorithms
+   *  @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>
+    inline void
+    nth_element(_RandomAccessIterator __first, _RandomAccessIterator __nth,
+               _RandomAccessIterator __last, _Compare __comp)
+    {
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type
+       _ValueType;
+
+      // concept requirements
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+                                 _RandomAccessIterator>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType, _ValueType>)
+      __glibcxx_requires_valid_range(__first, __nth);
+      __glibcxx_requires_valid_range(__nth, __last);
+
+      if (__first == __last || __nth == __last)
+       return;
+
+      std::__introselect(__first, __nth, __last,
+                        std::__lg(__last - __first) * 2, __comp);
+    }
+
+
+  /**
+   *  @brief Sort the elements of a sequence.
+   *  @ingroup sorting_algorithms
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @return  Nothing.
+   *
+   *  Sorts the elements in the range @p [first,last) in ascending order,
+   *  such that @p *(i+1)<*i is false for each iterator @p i in the range
+   *  @p [first,last-1).
+   *
+   *  The relative ordering of equivalent elements is not preserved, use
+   *  @p stable_sort() if this is needed.
+  */
+  template<typename _RandomAccessIterator>
+    inline void
+    sort(_RandomAccessIterator __first, _RandomAccessIterator __last)
+    {
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type
+       _ValueType;
+
+      // concept requirements
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
+      __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__first != __last)
+       {
+         std::__introsort_loop(__first, __last,
+                               std::__lg(__last - __first) * 2);
+         std::__final_insertion_sort(__first, __last);
+       }
+    }
+
+  /**
+   *  @brief Sort the elements of a sequence using a predicate for comparison.
+   *  @ingroup sorting_algorithms
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @param  comp    A comparison functor.
+   *  @return  Nothing.
+   *
+   *  Sorts the elements in the range @p [first,last) in ascending order,
+   *  such that @p comp(*(i+1),*i) is false for every iterator @p i in the
+   *  range @p [first,last-1).
+   *
+   *  The relative ordering of equivalent elements is not preserved, use
+   *  @p stable_sort() if this is needed.
+  */
+  template<typename _RandomAccessIterator, typename _Compare>
+    inline void
+    sort(_RandomAccessIterator __first, _RandomAccessIterator __last,
+        _Compare __comp)
+    {
+      typedef typename iterator_traits<_RandomAccessIterator>::value_type
+       _ValueType;
+
+      // concept requirements
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
+                                 _ValueType>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__first != __last)
+       {
+         std::__introsort_loop(__first, __last,
+                               std::__lg(__last - __first) * 2, __comp);
+         std::__final_insertion_sort(__first, __last, __comp);
+       }
+    }
+
+  /**
+   *  @brief Merges two sorted ranges.
+   *  @ingroup sorting_algorithms
+   *  @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 <em>not less
+   *                  than</em> @a val.
+   *
+   *  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 _InputIterator1, typename _InputIterator2,
+          typename _OutputIterator>
+    _OutputIterator
+    merge(_InputIterator1 __first1, _InputIterator1 __last1,
+         _InputIterator2 __first2, _InputIterator2 __last2,
+         _OutputIterator __result)
+    {
+      typedef typename iterator_traits<_InputIterator1>::value_type
+       _ValueType1;
+      typedef typename iterator_traits<_InputIterator2>::value_type
+       _ValueType2;
+
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType1>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType2>)
+      __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)        
+      __glibcxx_requires_sorted_set(__first1, __last1, __first2);
+      __glibcxx_requires_sorted_set(__first2, __last2, __first1);
+
+      while (__first1 != __last1 && __first2 != __last2)
+       {
+         if (*__first2 < *__first1)
+           {
+             *__result = *__first2;
+             ++__first2;
+           }
+         else
+           {
+             *__result = *__first1;
+             ++__first1;
+           }
+         ++__result;
+       }
+      return std::copy(__first2, __last2, std::copy(__first1, __last1,
+                                                   __result));
+    }
+
+  /**
+   *  @brief Merges two sorted ranges.
+   *  @ingroup sorting_algorithms
+   *  @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.
+   *
+   *  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 _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<_InputIterator1>::value_type
+       _ValueType1;
+      typedef typename iterator_traits<_InputIterator2>::value_type
+       _ValueType2;
+
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType1>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType2>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType2, _ValueType1>)
+      __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
+      __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
+
+      while (__first1 != __last1 && __first2 != __last2)
+       {
+         if (__comp(*__first2, *__first1))
+           {
+             *__result = *__first2;
+             ++__first2;
+           }
+         else
+           {
+             *__result = *__first1;
+             ++__first1;
+           }
+         ++__result;
+       }
+      return std::copy(__first2, __last2, std::copy(__first1, __last1,
+                                                   __result));
+    }
+
+
+  /**
+   *  @brief Sort the elements of a sequence, preserving the relative order
+   *         of equivalent elements.
+   *  @ingroup sorting_algorithms
+   *  @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
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
+      __glibcxx_function_requires(_LessThanComparableConcept<_ValueType>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
+                                                                __last);
+      if (__buf.begin() == 0)
+       std::__inplace_stable_sort(__first, __last);
+      else
+       std::__stable_sort_adaptive(__first, __last, __buf.begin(),
+                                   _DistanceType(__buf.size()));
+    }
+
+  /**
+   *  @brief Sort the elements of a sequence using a predicate for comparison,
+   *         preserving the relative order of equivalent elements.
+   *  @ingroup sorting_algorithms
+   *  @param  first   An iterator.
+   *  @param  last    Another iterator.
+   *  @param  comp    A comparison functor.
+   *  @return  Nothing.
+   *
+   *  Sorts the elements in the range @p [first,last) in ascending order,
+   *  such that @p comp(*(i+1),*i) is false for each iterator @p i in the
+   *  range @p [first,last-1).
+   *
+   *  The relative ordering of equivalent elements is preserved, so any two
+   *  elements @p x and @p y in the range @p [first,last) such that
+   *  @p comp(x,y) is false and @p comp(y,x) is false will have the same
+   *  relative ordering after calling @p stable_sort().
+  */
+  template<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
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType,
+                                 _ValueType>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      _Temporary_buffer<_RandomAccessIterator, _ValueType> __buf(__first,
+                                                                __last);
+      if (__buf.begin() == 0)
+       std::__inplace_stable_sort(__first, __last, __comp);
+      else
+       std::__stable_sort_adaptive(__first, __last, __buf.begin(),
+                                   _DistanceType(__buf.size()), __comp);
+    }
+
+
+  /**
+   *  @brief Return the union of two sorted ranges.
+   *  @ingroup set_algorithms
+   *  @param  first1  Start of first range.
+   *  @param  last1   End of first range.
+   *  @param  first2  Start of second range.
+   *  @param  last2   End of second range.
+   *  @return  End of the output range.
+   *  @ingroup set_algorithms
+   *
+   *  This operation iterates over both ranges, copying elements present in
+   *  each range in order to the output range.  Iterators increment for each
+   *  range.  When the current element of one range is less than the other,
+   *  that element is copied and the iterator advanced.  If an element is
+   *  contained in both ranges, the element from the first range is copied and
+   *  both ranges advance.  The output range may not overlap either input
+   *  range.
+  */
+  template<typename _InputIterator1, typename _InputIterator2,
+          typename _OutputIterator>
+    _OutputIterator
+    set_union(_InputIterator1 __first1, _InputIterator1 __last1,
+             _InputIterator2 __first2, _InputIterator2 __last2,
+             _OutputIterator __result)
+    {
+      typedef typename iterator_traits<_InputIterator1>::value_type
+       _ValueType1;
+      typedef typename iterator_traits<_InputIterator2>::value_type
+       _ValueType2;
+
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType1>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType2>)
+      __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
+      __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
+      __glibcxx_requires_sorted_set(__first1, __last1, __first2);
+      __glibcxx_requires_sorted_set(__first2, __last2, __first1);
+
+      while (__first1 != __last1 && __first2 != __last2)
+       {
+         if (*__first1 < *__first2)
+           {
+             *__result = *__first1;
+             ++__first1;
+           }
+         else if (*__first2 < *__first1)
+           {
+             *__result = *__first2;
+             ++__first2;
+           }
+         else
+           {
+             *__result = *__first1;
+             ++__first1;
+             ++__first2;
+           }
+         ++__result;
+       }
+      return std::copy(__first2, __last2, std::copy(__first1, __last1,
+                                                   __result));
+    }
+
+  /**
+   *  @brief Return the union of two sorted ranges using a comparison functor.
+   *  @ingroup set_algorithms
+   *  @param  first1  Start of first range.
+   *  @param  last1   End of first range.
+   *  @param  first2  Start of second range.
+   *  @param  last2   End of second range.
+   *  @param  comp    The comparison functor.
+   *  @return  End of the output range.
+   *  @ingroup set_algorithms
+   *
+   *  This operation iterates over both ranges, copying elements present in
+   *  each range in order to the output range.  Iterators increment for each
+   *  range.  When the current element of one range is less than the other
+   *  according to @a comp, that element is copied and the iterator advanced.
+   *  If an equivalent element according to @a comp is contained in both
+   *  ranges, the element from the first range is copied and both ranges
+   *  advance.  The output range may not overlap either input range.
+  */
+  template<typename _InputIterator1, typename _InputIterator2,
+          typename _OutputIterator, typename _Compare>
+    _OutputIterator
+    set_union(_InputIterator1 __first1, _InputIterator1 __last1,
+             _InputIterator2 __first2, _InputIterator2 __last2,
+             _OutputIterator __result, _Compare __comp)
+    {
+      typedef typename iterator_traits<_InputIterator1>::value_type
+       _ValueType1;
+      typedef typename iterator_traits<_InputIterator2>::value_type
+       _ValueType2;
+
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType1>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType2>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType1, _ValueType2>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType2, _ValueType1>)
+      __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
+      __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
+
+      while (__first1 != __last1 && __first2 != __last2)
+       {
+         if (__comp(*__first1, *__first2))
+           {
+             *__result = *__first1;
+             ++__first1;
+           }
+         else if (__comp(*__first2, *__first1))
+           {
+             *__result = *__first2;
+             ++__first2;
+           }
+         else
+           {
+             *__result = *__first1;
+             ++__first1;
+             ++__first2;
+           }
+         ++__result;
+       }
+      return std::copy(__first2, __last2, std::copy(__first1, __last1,
+                                                   __result));
+    }
+
+  /**
+   *  @brief Return the intersection of two sorted ranges.
+   *  @ingroup set_algorithms
+   *  @param  first1  Start of first range.
+   *  @param  last1   End of first range.
+   *  @param  first2  Start of second range.
+   *  @param  last2   End of second range.
+   *  @return  End of the output range.
+   *  @ingroup set_algorithms
+   *
+   *  This operation iterates over both ranges, copying elements present in
+   *  both ranges in order to the output range.  Iterators increment for each
+   *  range.  When the current element of one range is less than the other,
+   *  that iterator advances.  If an element is contained in both ranges, the
+   *  element from the first range is copied and both ranges advance.  The
+   *  output range may not overlap either input range.
+  */
+  template<typename _InputIterator1, typename _InputIterator2,
+          typename _OutputIterator>
+    _OutputIterator
+    set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
+                    _InputIterator2 __first2, _InputIterator2 __last2,
+                    _OutputIterator __result)
+    {
+      typedef typename iterator_traits<_InputIterator1>::value_type
+       _ValueType1;
+      typedef typename iterator_traits<_InputIterator2>::value_type
+       _ValueType2;
+
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType1>)
+      __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
+      __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)
+      __glibcxx_requires_sorted_set(__first1, __last1, __first2);
+      __glibcxx_requires_sorted_set(__first2, __last2, __first1);
+
+      while (__first1 != __last1 && __first2 != __last2)
+       if (*__first1 < *__first2)
+         ++__first1;
+       else if (*__first2 < *__first1)
+         ++__first2;
+       else
+         {
+           *__result = *__first1;
+           ++__first1;
+           ++__first2;
+           ++__result;
+         }
+      return __result;
+    }
+
+  /**
+   *  @brief Return the intersection of two sorted ranges using comparison
+   *  functor.
+   *  @ingroup set_algorithms
+   *  @param  first1  Start of first range.
+   *  @param  last1   End of first range.
+   *  @param  first2  Start of second range.
+   *  @param  last2   End of second range.
+   *  @param  comp    The comparison functor.
+   *  @return  End of the output range.
+   *  @ingroup set_algorithms
+   *
+   *  This operation iterates over both ranges, copying elements present in
+   *  both ranges in order to the output range.  Iterators increment for each
+   *  range.  When the current element of one range is less than the other
+   *  according to @a comp, that iterator advances.  If an element is
+   *  contained in both ranges according to @a comp, the element from the
+   *  first range is copied and both ranges advance.  The output range may not
+   *  overlap either input range.
+  */
+  template<typename _InputIterator1, typename _InputIterator2,
+          typename _OutputIterator, typename _Compare>
+    _OutputIterator
+    set_intersection(_InputIterator1 __first1, _InputIterator1 __last1,
+                    _InputIterator2 __first2, _InputIterator2 __last2,
+                    _OutputIterator __result, _Compare __comp)
+    {
+      typedef typename iterator_traits<_InputIterator1>::value_type
+       _ValueType1;
+      typedef typename iterator_traits<_InputIterator2>::value_type
+       _ValueType2;
+
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType1>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType1, _ValueType2>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType2, _ValueType1>)
+      __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
+      __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
+
+      while (__first1 != __last1 && __first2 != __last2)
+       if (__comp(*__first1, *__first2))
+         ++__first1;
+       else if (__comp(*__first2, *__first1))
+         ++__first2;
+       else
+         {
+           *__result = *__first1;
+           ++__first1;
+           ++__first2;
+           ++__result;
+         }
+      return __result;
+    }
+
+  /**
+   *  @brief Return the difference of two sorted ranges.
+   *  @ingroup set_algorithms
+   *  @param  first1  Start of first range.
+   *  @param  last1   End of first range.
+   *  @param  first2  Start of second range.
+   *  @param  last2   End of second range.
+   *  @return  End of the output range.
+   *  @ingroup set_algorithms
+   *
+   *  This operation iterates over both ranges, copying elements present in
+   *  the first range but not the second in order to the output range.
+   *  Iterators increment for each range.  When the current element of the
+   *  first range is less than the second, that element is copied and the
+   *  iterator advances.  If the current element of the second range is less,
+   *  the iterator advances, but no element is copied.  If an element is
+   *  contained in both ranges, no elements are copied and both ranges
+   *  advance.  The output range may not overlap either input range.
+  */
+  template<typename _InputIterator1, typename _InputIterator2,
+          typename _OutputIterator>
+    _OutputIterator
+    set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
+                  _InputIterator2 __first2, _InputIterator2 __last2,
+                  _OutputIterator __result)
+    {
+      typedef typename iterator_traits<_InputIterator1>::value_type
+       _ValueType1;
+      typedef typename iterator_traits<_InputIterator2>::value_type
+       _ValueType2;
+
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType1>)
+      __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
+      __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)        
+      __glibcxx_requires_sorted_set(__first1, __last1, __first2);
+      __glibcxx_requires_sorted_set(__first2, __last2, __first1);
+
+      while (__first1 != __last1 && __first2 != __last2)
+       if (*__first1 < *__first2)
+         {
+           *__result = *__first1;
+           ++__first1;
+           ++__result;
+         }
+       else if (*__first2 < *__first1)
+         ++__first2;
+       else
+         {
+           ++__first1;
+           ++__first2;
+         }
+      return std::copy(__first1, __last1, __result);
+    }
+
+  /**
+   *  @brief  Return the difference of two sorted ranges using comparison
+   *  functor.
+   *  @ingroup set_algorithms
+   *  @param  first1  Start of first range.
+   *  @param  last1   End of first range.
+   *  @param  first2  Start of second range.
+   *  @param  last2   End of second range.
+   *  @param  comp    The comparison functor.
+   *  @return  End of the output range.
+   *  @ingroup set_algorithms
+   *
+   *  This operation iterates over both ranges, copying elements present in
+   *  the first range but not the second in order to the output range.
+   *  Iterators increment for each range.  When the current element of the
+   *  first range is less than the second according to @a comp, that element
+   *  is copied and the iterator advances.  If the current element of the
+   *  second range is less, no element is copied and the iterator advances.
+   *  If an element is contained in both ranges according to @a comp, no
+   *  elements are copied and both ranges advance.  The output range may not
+   *  overlap either input range.
+  */
+  template<typename _InputIterator1, typename _InputIterator2,
+          typename _OutputIterator, typename _Compare>
+    _OutputIterator
+    set_difference(_InputIterator1 __first1, _InputIterator1 __last1,
+                  _InputIterator2 __first2, _InputIterator2 __last2,
+                  _OutputIterator __result, _Compare __comp)
+    {
+      typedef typename iterator_traits<_InputIterator1>::value_type
+       _ValueType1;
+      typedef typename iterator_traits<_InputIterator2>::value_type
+       _ValueType2;
+
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType1>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType1, _ValueType2>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType2, _ValueType1>)
+      __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
+      __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
+
+      while (__first1 != __last1 && __first2 != __last2)
+       if (__comp(*__first1, *__first2))
+         {
+           *__result = *__first1;
+           ++__first1;
+           ++__result;
+         }
+       else if (__comp(*__first2, *__first1))
+         ++__first2;
+       else
+         {
+           ++__first1;
+           ++__first2;
+         }
+      return std::copy(__first1, __last1, __result);
+    }
+
+  /**
+   *  @brief  Return the symmetric difference of two sorted ranges.
+   *  @ingroup set_algorithms
+   *  @param  first1  Start of first range.
+   *  @param  last1   End of first range.
+   *  @param  first2  Start of second range.
+   *  @param  last2   End of second range.
+   *  @return  End of the output range.
+   *  @ingroup set_algorithms
+   *
+   *  This operation iterates over both ranges, copying elements present in
+   *  one range but not the other in order to the output range.  Iterators
+   *  increment for each range.  When the current element of one range is less
+   *  than the other, that element is copied and the iterator advances.  If an
+   *  element is contained in both ranges, no elements are copied and both
+   *  ranges advance.  The output range may not overlap either input range.
+  */
+  template<typename _InputIterator1, typename _InputIterator2,
+          typename _OutputIterator>
+    _OutputIterator
+    set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
+                            _InputIterator2 __first2, _InputIterator2 __last2,
+                            _OutputIterator __result)
+    {
+      typedef typename iterator_traits<_InputIterator1>::value_type
+       _ValueType1;
+      typedef typename iterator_traits<_InputIterator2>::value_type
+       _ValueType2;
+
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType1>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType2>)
+      __glibcxx_function_requires(_LessThanOpConcept<_ValueType1, _ValueType2>)
+      __glibcxx_function_requires(_LessThanOpConcept<_ValueType2, _ValueType1>)        
+      __glibcxx_requires_sorted_set(__first1, __last1, __first2);
+      __glibcxx_requires_sorted_set(__first2, __last2, __first1);
+
+      while (__first1 != __last1 && __first2 != __last2)
+       if (*__first1 < *__first2)
+         {
+           *__result = *__first1;
+           ++__first1;
+           ++__result;
+         }
+       else if (*__first2 < *__first1)
+         {
+           *__result = *__first2;
+           ++__first2;
+           ++__result;
+         }
+       else
+         {
+           ++__first1;
+           ++__first2;
+         }
+      return std::copy(__first2, __last2, std::copy(__first1,
+                                                   __last1, __result));
+    }
+
+  /**
+   *  @brief  Return the symmetric difference of two sorted ranges using
+   *  comparison functor.
+   *  @ingroup set_algorithms
+   *  @param  first1  Start of first range.
+   *  @param  last1   End of first range.
+   *  @param  first2  Start of second range.
+   *  @param  last2   End of second range.
+   *  @param  comp    The comparison functor.
+   *  @return  End of the output range.
+   *  @ingroup set_algorithms
+   *
+   *  This operation iterates over both ranges, copying elements present in
+   *  one range but not the other in order to the output range.  Iterators
+   *  increment for each range.  When the current element of one range is less
+   *  than the other according to @a comp, that element is copied and the
+   *  iterator advances.  If an element is contained in both ranges according
+   *  to @a comp, no elements are copied and both ranges advance.  The output
+   *  range may not overlap either input range.
+  */
+  template<typename _InputIterator1, typename _InputIterator2,
+          typename _OutputIterator, typename _Compare>
+    _OutputIterator
+    set_symmetric_difference(_InputIterator1 __first1, _InputIterator1 __last1,
+                            _InputIterator2 __first2, _InputIterator2 __last2,
+                            _OutputIterator __result,
+                            _Compare __comp)
+    {
+      typedef typename iterator_traits<_InputIterator1>::value_type
+       _ValueType1;
+      typedef typename iterator_traits<_InputIterator2>::value_type
+       _ValueType2;
+
+      // concept requirements
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator1>)
+      __glibcxx_function_requires(_InputIteratorConcept<_InputIterator2>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType1>)
+      __glibcxx_function_requires(_OutputIteratorConcept<_OutputIterator,
+                                 _ValueType2>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType1, _ValueType2>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+                                 _ValueType2, _ValueType1>)
+      __glibcxx_requires_sorted_set_pred(__first1, __last1, __first2, __comp);
+      __glibcxx_requires_sorted_set_pred(__first2, __last2, __first1, __comp);
+
+      while (__first1 != __last1 && __first2 != __last2)
+       if (__comp(*__first1, *__first2))
+         {
+           *__result = *__first1;
+           ++__first1;
+           ++__result;
+         }
+       else if (__comp(*__first2, *__first1))
+         {
+           *__result = *__first2;
+           ++__first2;
+           ++__result;
+         }
+       else
+         {
+           ++__first1;
+           ++__first2;
+         }
+      return std::copy(__first2, __last2, 
+                      std::copy(__first1, __last1, __result));
+    }
+
+
+  /**
+   *  @brief  Return the minimum element in a range.
+   *  @ingroup sorting_algorithms
+   *  @param  first  Start of range.
+   *  @param  last   End of range.
+   *  @return  Iterator referencing the first instance of the smallest value.
+  */
+  template<typename _ForwardIterator>
+    _ForwardIterator
+    min_element(_ForwardIterator __first, _ForwardIterator __last)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_LessThanComparableConcept<
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__first == __last)
+       return __first;
+      _ForwardIterator __result = __first;
+      while (++__first != __last)
+       if (*__first < *__result)
+         __result = __first;
+      return __result;
+    }
+
+  /**
+   *  @brief  Return the minimum element in a range using comparison functor.
+   *  @ingroup sorting_algorithms
+   *  @param  first  Start of range.
+   *  @param  last   End of range.
+   *  @param  comp   Comparison functor.
+   *  @return  Iterator referencing the first instance of the smallest value
+   *  according to comp.
+  */
+  template<typename _ForwardIterator, typename _Compare>
+    _ForwardIterator
+    min_element(_ForwardIterator __first, _ForwardIterator __last,
+               _Compare __comp)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+           typename iterator_traits<_ForwardIterator>::value_type,
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__first == __last)
+       return __first;
+      _ForwardIterator __result = __first;
+      while (++__first != __last)
+       if (__comp(*__first, *__result))
+         __result = __first;
+      return __result;
+    }
+
+  /**
+   *  @brief  Return the maximum element in a range.
+   *  @ingroup sorting_algorithms
+   *  @param  first  Start of range.
+   *  @param  last   End of range.
+   *  @return  Iterator referencing the first instance of the largest value.
+  */
+  template<typename _ForwardIterator>
+    _ForwardIterator
+    max_element(_ForwardIterator __first, _ForwardIterator __last)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_LessThanComparableConcept<
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__first == __last)
+       return __first;
+      _ForwardIterator __result = __first;
+      while (++__first != __last)
+       if (*__result < *__first)
+         __result = __first;
+      return __result;
+    }
+
+  /**
+   *  @brief  Return the maximum element in a range using comparison functor.
+   *  @ingroup sorting_algorithms
+   *  @param  first  Start of range.
+   *  @param  last   End of range.
+   *  @param  comp   Comparison functor.
+   *  @return  Iterator referencing the first instance of the largest value
+   *  according to comp.
+  */
+  template<typename _ForwardIterator, typename _Compare>
+    _ForwardIterator
+    max_element(_ForwardIterator __first, _ForwardIterator __last,
+               _Compare __comp)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
+      __glibcxx_function_requires(_BinaryPredicateConcept<_Compare,
+           typename iterator_traits<_ForwardIterator>::value_type,
+           typename iterator_traits<_ForwardIterator>::value_type>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__first == __last) return __first;
+      _ForwardIterator __result = __first;
+      while (++__first != __last)
+       if (__comp(*__result, *__first))
+         __result = __first;
+      return __result;
+    }
 
-#endif /* _ALGO_H */
+_GLIBCXX_END_NESTED_NAMESPACE
 
+#endif /* _STL_ALGO_H */