OSDN Git Service

2010-05-21 Paolo Carlini <paolo.carlini@oracle.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stl_algo.h
index 64905fb..fe2edb9 100644 (file)
@@ -1,12 +1,12 @@
 // Algorithm implementation -*- C++ -*-
 
-// Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
+// 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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
-// 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/>.
 
 /*
  *
 #include <bits/algorithmfwd.h>
 #include <bits/stl_heap.h>
 #include <bits/stl_tempbuf.h>  // for _Temporary_buffer
-#include <debug/debug.h>
-#include <initializer_list>
+
+#ifdef __GXX_EXPERIMENTAL_CXX0X__
+#include <random> // for std::uniform_int_distribution
+#endif
 
 // See concept_check.h for the __glibcxx_*_requires macros.
 
 _GLIBCXX_BEGIN_NAMESPACE(std)
 
-  /**
-   * @addtogroup algorithms Algorithms
-   * @{
-   */
-
-  /**
-   *  @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)
+  /// 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;
+           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);
     }
 
   // for_each
@@ -645,6 +619,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -688,6 +663,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -738,6 +714,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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.
@@ -754,6 +731,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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.
@@ -770,6 +748,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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.
@@ -786,6 +765,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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.
@@ -808,6 +788,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @brief  Checks whether the sequence is partitioned.
+   *  @ingroup mutating_algorithms
    *  @param  first  An input iterator.
    *  @param  last   An input iterator.
    *  @param  pred   A predicate.
@@ -826,6 +807,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @brief  Find the partition point of a partitioned range.
+   *  @ingroup mutating_algorithms
    *  @param  first   An iterator.
    *  @param  last    Another iterator.
    *  @param  pred    A predicate.
@@ -873,6 +855,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -908,6 +891,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -946,6 +930,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 #ifdef __GXX_EXPERIMENTAL_CXX0X__
   /**
    *  @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.
@@ -1005,6 +990,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -1031,6 +1017,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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.
@@ -1077,6 +1064,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -1119,6 +1107,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @brief Remove elements from a sequence using a predicate.
+   *  @ingroup mutating_algorithms
    *  @param  first  A forward iterator.
    *  @param  last   A forward iterator.
    *  @param  pred   A predicate.
@@ -1161,6 +1150,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -1199,6 +1189,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -1429,6 +1420,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @brief Reverse a sequence.
+   *  @ingroup mutating_algorithms
    *  @param  first  A bidirectional iterator.
    *  @param  last   A bidirectional iterator.
    *  @return   reverse() returns no value.
@@ -1451,6 +1443,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -1588,58 +1581,70 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       typedef typename iterator_traits<_RandomAccessIterator>::value_type
        _ValueType;
 
-      const _Distance __n = __last   - __first;
-      const _Distance __k = __middle - __first;
-      const _Distance __l = __n - __k;
+      _Distance __n = __last   - __first;
+      _Distance __k = __middle - __first;
 
-      if (__k == __l)
+      if (__k == __n - __k)
        {
          std::swap_ranges(__first, __middle, __middle);
          return;
        }
 
-      const _Distance __d = std::__gcd(__n, __k);
+      _RandomAccessIterator __p = __first;
 
-      for (_Distance __i = 0; __i < __d; __i++)
+      for (;;)
        {
-         _ValueType __tmp = _GLIBCXX_MOVE(*__first);
-         _RandomAccessIterator __p = __first;
-
-         if (__k < __l)
+         if (__k < __n - __k)
            {
-             for (_Distance __j = 0; __j < __l / __d; __j++)
+             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)
                {
-                 if (__p > __first + __l)
-                   {
-                     *__p = _GLIBCXX_MOVE(*(__p - __l));
-                     __p -= __l;
-                   }
-
-                 *__p = _GLIBCXX_MOVE(*(__p + __k));
-                 __p += __k;
+                 std::iter_swap(__p, __q);
+                 ++__p;
+                 ++__q;
                }
+             __n %= __k;
+             if (__n == 0)
+               return;
+             std::swap(__n, __k);
+             __k = __n - __k;
            }
          else
            {
-             for (_Distance __j = 0; __j < __k / __d - 1; __j ++)
+             __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)
                {
-                 if (__p < __last - __k)
-                   {
-                     *__p = _GLIBCXX_MOVE(*(__p + __k));
-                     __p += __k;
-                   }
-                 *__p = _GLIBCXX_MOVE(*(__p - __l));
-                 __p -= __l;
+                 --__p;
+                 --__q;
+                 std::iter_swap(__p, __q);
                }
+             __n %= __k;
+             if (__n == 0)
+               return;
+             std::swap(__n, __k);
            }
-
-         *__p = _GLIBCXX_MOVE(__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.
@@ -1674,6 +1679,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -1801,15 +1807,15 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
          for (; __first != __last; ++__first)
            if (__pred(*__first))
              {
-               *__result1 = *__first;
+               *__result1 = _GLIBCXX_MOVE(*__first);
                ++__result1;
              }
            else
              {
-               *__result2 = *__first;
+               *__result2 = _GLIBCXX_MOVE(*__first);
                ++__result2;
              }
-         std::copy(__buffer, __result2, __result1);
+         _GLIBCXX_MOVE3(__buffer, __result2, __result1);
          return __result1;
        }
       else
@@ -1833,6 +1839,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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.
@@ -1912,6 +1919,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -1976,6 +1984,7 @@ _GLIBCXX_BEGIN_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.
@@ -2044,36 +2053,40 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     }
 
   /// This is a helper function for the sort routine.
-  template<typename _RandomAccessIterator, typename _Tp>
+  template<typename _RandomAccessIterator>
     void
-    __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val)
+    __unguarded_linear_insert(_RandomAccessIterator __last)
     {
+      typename iterator_traits<_RandomAccessIterator>::value_type
+       __val = _GLIBCXX_MOVE(*__last);
       _RandomAccessIterator __next = __last;
       --__next;
       while (__val < *__next)
        {
-         *__last = *__next;
+         *__last = _GLIBCXX_MOVE(*__next);
          __last = __next;
          --__next;
        }
-      *__last = __val;
+      *__last = _GLIBCXX_MOVE(__val);
     }
 
   /// This is a helper function for the sort routine.
-  template<typename _RandomAccessIterator, typename _Tp, typename _Compare>
+  template<typename _RandomAccessIterator, typename _Compare>
     void
-    __unguarded_linear_insert(_RandomAccessIterator __last, _Tp __val,
+    __unguarded_linear_insert(_RandomAccessIterator __last,
                              _Compare __comp)
     {
+      typename iterator_traits<_RandomAccessIterator>::value_type
+       __val = _GLIBCXX_MOVE(*__last);
       _RandomAccessIterator __next = __last;
       --__next;
       while (__comp(__val, *__next))
        {
-         *__last = *__next;
+         *__last = _GLIBCXX_MOVE(*__next);
          __last = __next;
          --__next;
        }
-      *__last = __val;
+      *__last = _GLIBCXX_MOVE(__val);
     }
 
   /// This is a helper function for the sort routine.
@@ -2087,15 +2100,15 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
        {
-         typename iterator_traits<_RandomAccessIterator>::value_type
-           __val = *__i;
-         if (__val < *__first)
+         if (*__i < *__first)
            {
-             std::copy_backward(__first, __i, __i + 1);
-             *__first = __val;
+             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, __val);
+           std::__unguarded_linear_insert(__i);
        }
     }
 
@@ -2109,15 +2122,15 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
       for (_RandomAccessIterator __i = __first + 1; __i != __last; ++__i)
        {
-         typename iterator_traits<_RandomAccessIterator>::value_type
-           __val = *__i;
-         if (__comp(__val, *__first))
+         if (__comp(*__i, *__first))
            {
-             std::copy_backward(__first, __i, __i + 1);
-             *__first = __val;
+             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, __val, __comp);
+           std::__unguarded_linear_insert(__i, __comp);
        }
     }
 
@@ -2131,7 +2144,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        _ValueType;
 
       for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
-       std::__unguarded_linear_insert(__i, _ValueType(*__i));
+       std::__unguarded_linear_insert(__i);
     }
 
   /// This is a helper function for the sort routine.
@@ -2144,7 +2157,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        _ValueType;
 
       for (_RandomAccessIterator __i = __first; __i != __last; ++__i)
-       std::__unguarded_linear_insert(__i, _ValueType(*__i), __comp);
+       std::__unguarded_linear_insert(__i, __comp);
     }
 
   /**
@@ -2188,7 +2201,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   template<typename _RandomAccessIterator, typename _Tp>
     _RandomAccessIterator
     __unguarded_partition(_RandomAccessIterator __first,
-                         _RandomAccessIterator __last, _Tp __pivot)
+                         _RandomAccessIterator __last, const _Tp& __pivot)
     {
       while (true)
        {
@@ -2209,7 +2222,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     _RandomAccessIterator
     __unguarded_partition(_RandomAccessIterator __first,
                          _RandomAccessIterator __last,
-                         _Tp __pivot, _Compare __comp)
+                         const _Tp& __pivot, _Compare __comp)
     {
       while (true)
        {
@@ -2225,6 +2238,29 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        }
     }
 
+  /// 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
@@ -2232,9 +2268,6 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
                     _RandomAccessIterator __last,
                     _Size __depth_limit)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type
-       _ValueType;
-
       while (__last - __first > int(_S_threshold))
        {
          if (__depth_limit == 0)
@@ -2244,14 +2277,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
            }
          --__depth_limit;
          _RandomAccessIterator __cut =
-           std::__unguarded_partition(__first, __last,
-                                      _ValueType(std::__median(*__first,
-                                                               *(__first
-                                                                 + (__last
-                                                                    - __first)
-                                                                 / 2),
-                                                               *(__last
-                                                                 - 1))));
+           std::__unguarded_partition_pivot(__first, __last);
          std::__introsort_loop(__cut, __last, __depth_limit);
          __last = __cut;
        }
@@ -2264,9 +2290,6 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
                     _RandomAccessIterator __last,
                     _Size __depth_limit, _Compare __comp)
     {
-      typedef typename iterator_traits<_RandomAccessIterator>::value_type
-       _ValueType;
-
       while (__last - __first > int(_S_threshold))
        {
          if (__depth_limit == 0)
@@ -2276,43 +2299,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
            }
          --__depth_limit;
          _RandomAccessIterator __cut =
-           std::__unguarded_partition(__first, __last,
-                                      _ValueType(std::__median(*__first,
-                                                               *(__first
-                                                                 + (__last
-                                                                    - __first)
-                                                                 / 2),
-                                                               *(__last - 1),
-                                                               __comp)),
-                                      __comp);
+           std::__unguarded_partition_pivot(__first, __last, __comp);
          std::__introsort_loop(__cut, __last, __depth_limit, __comp);
          __last = __cut;
        }
     }
 
-  /// This is a helper function for the sort routines.  Precondition: __n > 0.
-  template<typename _Size>
-    inline _Size
-    __lg(_Size __n)
-    {
-      _Size __k;
-      for (__k = 0; __n != 0; __n >>= 1)
-       ++__k;
-      return __k - 1;
-    }
-
-  inline int
-  __lg(int __n)
-  { return sizeof(int) * __CHAR_BIT__  - 1 - __builtin_clz(__n); }
-
-  inline long
-  __lg(long __n)
-  { return sizeof(long) * __CHAR_BIT__ - 1 - __builtin_clzl(__n); }
-
-  inline long long
-  __lg(long long __n)
-  { return sizeof(long long) * __CHAR_BIT__ - 1 - __builtin_clzll(__n); }
-
   // sort
 
   template<typename _RandomAccessIterator, typename _Size>
@@ -2335,14 +2327,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
            }
          --__depth_limit;
          _RandomAccessIterator __cut =
-           std::__unguarded_partition(__first, __last,
-                                      _ValueType(std::__median(*__first,
-                                                               *(__first
-                                                                 + (__last
-                                                                    - __first)
-                                                                 / 2),
-                                                               *(__last
-                                                                 - 1))));
+           std::__unguarded_partition_pivot(__first, __last);
          if (__cut <= __nth)
            __first = __cut;
          else
@@ -2371,15 +2356,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
            }
          --__depth_limit;
          _RandomAccessIterator __cut =
-           std::__unguarded_partition(__first, __last,
-                                      _ValueType(std::__median(*__first,
-                                                               *(__first
-                                                                 + (__last
-                                                                    - __first)
-                                                                 / 2),
-                                                               *(__last - 1),
-                                                               __comp)),
-                                      __comp);
+           std::__unguarded_partition_pivot(__first, __last, __comp);
          if (__cut <= __nth)
            __first = __cut;
          else
@@ -2390,62 +2367,19 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   // nth_element
 
-  /**
-   *  @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 binary_search_algorithms
-  */
-  template<typename _ForwardIterator, typename _Tp>
-    _ForwardIterator
-    lower_bound(_ForwardIterator __first, _ForwardIterator __last,
-               const _Tp& __val)
-    {
-      typedef typename iterator_traits<_ForwardIterator>::value_type
-       _ValueType;
-      typedef typename iterator_traits<_ForwardIterator>::difference_type
-       _DistanceType;
-
-      // concept requirements
-      __glibcxx_function_requires(_ForwardIteratorConcept<_ForwardIterator>)
-      __glibcxx_function_requires(_LessThanOpConcept<_ValueType, _Tp>)
-      __glibcxx_requires_partitioned_lower(__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 (*__middle < __val)
-           {
-             __first = __middle;
-             ++__first;
-             __len = __len - __half - 1;
-           }
-         else
-           __len = __half;
-       }
-      return __first;
-    }
+  // 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.
+   *  @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
@@ -2492,6 +2426,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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.
@@ -2538,6 +2473,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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.
@@ -2590,6 +2526,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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.
@@ -2717,11 +2654,11 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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 ].
-   *  @ingroup binary_search_algorithms
    *
    *  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.
@@ -2746,12 +2683,12 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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  True if @a val (or its equivalent) is in [@a first,@a last ].
-   *  @ingroup binary_search_algorithms
    *
    *  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.
@@ -2867,15 +2804,15 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       _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);
+         __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 = std::copy(__first, __middle, __buffer);
-         std::copy(__middle, __last, __first);
-         return std::copy_backward(__buffer, __buffer_end, __last);
+         __buffer_end = _GLIBCXX_MOVE3(__first, __middle, __buffer);
+         _GLIBCXX_MOVE3(__middle, __last, __first);
+         return _GLIBCXX_MOVE_BACKWARD3(__buffer, __buffer_end, __last);
        }
       else
        {
@@ -2897,15 +2834,21 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     {
       if (__len1 <= __len2 && __len1 <= __buffer_size)
        {
-         _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
-         _GLIBCXX_STD_P::merge(__buffer, __buffer_end, __middle, __last, 
+         _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 = std::copy(__middle, __last, __buffer);
-         std::__merge_backward(__first, __middle, __buffer,
-                               __buffer_end, __last);
+         _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
        {
@@ -2954,15 +2897,21 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     {
       if (__len1 <= __len2 && __len1 <= __buffer_size)
        {
-         _Pointer __buffer_end = std::copy(__first, __middle, __buffer);
-         _GLIBCXX_STD_P::merge(__buffer, __buffer_end, __middle, __last,
+         _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 = std::copy(__middle, __last, __buffer);
-         std::__merge_backward(__first, __middle, __buffer, __buffer_end,
-                               __last, __comp);
+         _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
        {
@@ -3091,6 +3040,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @brief Merges two sorted ranges in place.
+   *  @ingroup sorting_algorithms
    *  @param  first   An iterator.
    *  @param  middle  Another iterator.
    *  @param  last    Another iterator.
@@ -3141,6 +3091,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @brief Merges two sorted ranges in place.
+   *  @ingroup sorting_algorithms
    *  @param  first   An iterator.
    *  @param  middle  Another iterator.
    *  @param  last    Another iterator.
@@ -3209,16 +3160,22 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
       while (__last - __first >= __two_step)
        {
-         __result = _GLIBCXX_STD_P::merge(__first, __first + __step_size,
-                                          __first + __step_size,
-                                          __first + __two_step,
-                                          __result);
+         __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(__first, __first + __step_size, 
-                           __first + __step_size, __last,
+      _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);
     }
 
@@ -3234,16 +3191,23 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
       while (__last - __first >= __two_step)
        {
-         __result = _GLIBCXX_STD_P::merge(__first, __first + __step_size,
-                               __first + __step_size, __first + __two_step,
-                               __result,
-                               __comp);
+         __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);
 
-      _GLIBCXX_STD_P::merge(__first, __first + __step_size,
-                           __first + __step_size, __last, __result, __comp);
+      _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);
     }
 
   template<typename _RandomAccessIterator, typename _Distance>
@@ -3477,6 +3441,7 @@ _GLIBCXX_BEGIN_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
@@ -3537,12 +3502,13 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   // max_element
 
   /**
-   *  @brief  Permute range into the next "dictionary" ordering.
+   *  @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.
    *
-   *  Treats all permutations of the range as a set of "dictionary" sorted
+   *  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.
@@ -3590,15 +3556,16 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     }
 
   /**
-   *  @brief  Permute range into the next "dictionary" ordering using
+   *  @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.
    *
    *  Treats all permutations of the range [first,last) as a set of
-   *  "dictionary" sorted sequences ordered by @a comp.  Permutes the current
+   *  @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.
@@ -3647,12 +3614,13 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     }
 
   /**
-   *  @brief  Permute range into the previous "dictionary" ordering.
+   *  @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.
    *
-   *  Treats all permutations of the range as a set of "dictionary" sorted
+   *  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
@@ -3701,15 +3669,16 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
     }
 
   /**
-   *  @brief  Permute range into the previous "dictionary" ordering using
+   *  @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.
    *
    *  Treats all permutations of the range [first,last) as a set of
-   *  "dictionary" sorted sequences ordered by @a comp.  Permutes the current
+   *  @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.
@@ -3799,6 +3768,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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.
@@ -3836,6 +3806,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 #ifdef __GXX_EXPERIMENTAL_CXX0X__
   /**
    *  @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.
@@ -3848,6 +3819,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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.
@@ -3861,6 +3833,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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)
@@ -3888,6 +3861,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -3918,6 +3892,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -3935,6 +3910,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
 
   /**
    *  @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.
@@ -3951,6 +3927,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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  make_pair(m, M), where m is the first iterator i in 
@@ -4025,6 +4002,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
   /**
    *  @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.
@@ -4099,7 +4077,7 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
       return std::make_pair(__min, __max);
     }
 
-  // N2722 + fixes.
+  // N2722 + DR 915.
   template<typename _Tp>
     inline _Tp
     min(initializer_list<_Tp> __l)
@@ -4137,25 +4115,60 @@ _GLIBCXX_BEGIN_NAMESPACE(std)
        std::minmax_element(__l.begin(), __l.end(), __comp);
       return std::make_pair(*__p.first, *__p.second);
     }
-#endif // __GXX_EXPERIMENTAL_CXX0X__
 
-  /* @} */ // group algorithms
+#ifdef _GLIBCXX_USE_C99_STDINT_TR1
+  /**
+   *  @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.
+   *
+   *  Reorders the elements in the range @p [first,last) using @p g to
+   *  provide random numbers.
+  */
+  template<typename _RandomAccessIterator,
+          typename _UniformRandomNumberGenerator>
+    void
+    shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
+           _UniformRandomNumberGenerator& __g)
+    {
+      // concept requirements
+      __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
+           _RandomAccessIterator>)
+      __glibcxx_requires_valid_range(__first, __last);
+
+      if (__first == __last)
+       return;
+
+      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)
 
   /**
-   * @addtogroup algorithms Algorithms
-   * @{
-   */
-
-  /**
    *  @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.
+   *  @return   @p f (std::move(@p f) in C++0x).
    *
    *  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.
@@ -4170,11 +4183,12 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
       __glibcxx_requires_valid_range(__first, __last);
       for (; __first != __last; ++__first)
        __f(*__first);
-      return __f;
+      return _GLIBCXX_MOVE(__f);
     }
 
   /**
    *  @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.
@@ -4198,6 +4212,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -4220,6 +4235,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -4255,6 +4271,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -4295,6 +4312,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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
@@ -4324,6 +4342,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -4357,6 +4376,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -4381,6 +4401,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -4405,6 +4426,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -4478,6 +4500,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -4558,6 +4581,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -4592,6 +4616,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -4632,6 +4657,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -4665,6 +4691,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -4703,6 +4730,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -4734,6 +4762,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -4765,6 +4794,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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
@@ -4792,6 +4822,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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
@@ -4800,6 +4831,9 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
    *
    *  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
@@ -4810,7 +4844,8 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
             // "the type returned by a _Generator"
             __typeof__(__gen())>)
 
-      for (; __n > 0; --__n, ++__first)
+      for (__decltype(__n + 0) __niter = __n;
+          __niter > 0; --__niter, ++__first)
        *__first = __gen();
       return __first;
     }
@@ -4818,6 +4853,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -4858,6 +4894,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -4897,6 +4934,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @brief Randomly shuffle the elements of a sequence.
+   *  @ingroup mutating_algorithms
    *  @param  first   A forward iterator.
    *  @param  last    A forward iterator.
    *  @return  Nothing.
@@ -4922,6 +4960,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -4935,7 +4974,11 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   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<
@@ -4952,6 +4995,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -4983,6 +5027,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @brief Sort the smallest elements of a sequence.
+   *  @ingroup sorting_algorithms
    *  @param  first   An iterator.
    *  @param  middle  Another iterator.
    *  @param  last    Another iterator.
@@ -5019,6 +5064,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -5058,6 +5104,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -5096,6 +5143,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -5135,6 +5183,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @brief Sort the elements of a sequence.
+   *  @ingroup sorting_algorithms
    *  @param  first   An iterator.
    *  @param  last    Another iterator.
    *  @return  Nothing.
@@ -5169,6 +5218,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -5206,13 +5256,14 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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 "not less
-   *                  than" @a val.
+   *  @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
@@ -5264,6 +5315,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @brief Merges two sorted ranges.
+   *  @ingroup sorting_algorithms
    *  @param  first1  An iterator.
    *  @param  first2  Another iterator.
    *  @param  last1   Another iterator.
@@ -5329,6 +5381,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -5369,6 +5422,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -5413,6 +5467,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -5478,6 +5533,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -5546,6 +5602,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -5600,6 +5657,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -5657,6 +5715,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -5715,6 +5774,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -5776,6 +5836,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -5839,6 +5900,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
   /**
    *  @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.
@@ -5907,6 +5969,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -5932,6 +5995,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -5961,6 +6025,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -5986,6 +6051,7 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
 
   /**
    *  @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.
@@ -6012,8 +6078,6 @@ _GLIBCXX_BEGIN_NESTED_NAMESPACE(std, _GLIBCXX_STD_P)
       return __result;
     }
 
-  /* @} */ // group algorithms
-
 _GLIBCXX_END_NESTED_NAMESPACE
 
 #endif /* _STL_ALGO_H */