<html>
<head>
<title>Rvalue Reference Recommendations for Chapter 25</title>
<style>
p {text-align:justify}
li {text-align:justify}
blockquote.note
{
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
}
ins {background-color:#FFFFA0}
del {background-color:#FFFFA0}
</style>
</head>

<body>

<address align=right>
Document number: N1860=05-0120<br>
<br>
<a href="mailto:hinnant@twcny.rr.com">Howard E. Hinnant</a><br>
2005-08-26
</address>
<hr>

<h1 align=center>Rvalue Reference Recommendations for Chapter 25</h1>

<h2>Contents</h2>

<ul>
<li><a href="#Introduction">Introduction</a></li>
<li>
<a href="#25 - Algorithms library">25 - Algorithms library</a>
	<ul>
	<li>
	<a href="#25.2 - Mutating sequence operations">25.2 - Mutating sequence
operations</a>
		<ul>
		<li><a href="#25.2.2 - Move">25.2.2 - Move</a></li>
		<li><a href="#25.2.3 - Swap">25.2.3 - Swap</a></li>
		<li><a href="#25.2.8 - Remove">25.2.8 - Remove</a></li>
		<li><a href="#25.2.9 - Unique">25.2.9 - Unique</a></li>
		<li><a href="#25.2.10 - Rotate">25.2.10 - Rotate</a></li>
		<li><a href="#25.2.12 - Random shuffle">25.2.12 - Random shuffle</a></li>
		<li><a href="#25.2.13 - Partitions">25.2.13 - Partitions</a></li>
		</ul>
	</li>
	<li>
	<a href="#25.3 - Sorting and related operations">25.3 - Sorting and related
operations</a>
		<ul>
		<li>
		<a href="#25.3.1 - Sorting">25.3.1 - Sorting</a>
			<ul>
			<li><a href="#25.3.1.1 - sort">25.3.1.1 - sort</a></li>
			<li><a href="#25.3.1.2 - stable_sort">25.3.1.2 - stable_sort</a></li>
			<li><a href="#25.3.1.3 - partial_sort">25.3.1.3 - partial_sort</a></li>
			<li><a href="#25.3.1.4 - partial_sort_copy">25.3.1.4 - partial_sort_copy</a></li>
			</ul>
		</li>
		<li><a href="#25.3.2 - Nth element">25.3.2 - Nth element</a></li>
		<li><a href="#25.3.4 - Merge">25.3.4 - Merge</a></li>
		<li>
		<a href="#25.3.6 - Heap operations">25.3.6 - Heap operations</a>
			<ul>
			<li><a href="#25.3.6.1 - push_heap">25.3.6.1 - push_heap</a></li>
			<li><a href="#25.3.6.2 - pop_heap">25.3.6.2 - pop_heap</a></li>
			<li><a href="#25.3.6.3 - make_heap">25.3.6.3 - make_heap</a></li>
			<li><a href="#25.3.6.4 - sort_heap">25.3.6.4 - sort_heap</a></li>
			</ul>
		</li>
		</ul> 	</li> 	</ul>
</li>
</ul>

<h2>Related papers</h2>

<p>
<a href="n1856.html">Rvalue Reference Recommendations for Chapter 20</a><br>
<a href="n1857.html">Rvalue Reference Recommendations for Chapter 21</a><br>
<a href="n1858.html">Rvalue Reference Recommendations for Chapter 23</a><br>
<a href="n1859.html">Rvalue Reference Recommendations for Chapter 24</a><br>
<a href="n1861.html">Rvalue Reference Recommendations for Chapter 26</a><br>
<a href="n1862.html">Rvalue Reference Recommendations for Chapter 27</a>
</p>

<h2><a name="Introduction"></a>Introduction</h2>

<p>
This paper recommends proposed wording with respect to the
<a
href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2004/n1690.html">rvalue
reference</a> for the
<a
href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1804.pdf">C++0X
working draft</a>.
This paper restricts its scope to Chapter 25 "Algorithms library" for the
purpose of breaking the library work associated with the rvalue reference up
into manageable chunks. This paper largely follows the lead of
<a
href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1771.html">N1771:
Impact of the rvalue reference on the Standard Library</a>, but adds more detail
as appropriate.  Refer to N1771 for detailed motivation for these changes.
</p>

<p>
With the exception of this introduction, all non-proposed wording will have a
background color and formatting that
</p>
<blockquote class="note">
looks like this, so that motivation and description is more easily distinguished
from proposed wording.
</blockquote>
<p>
In the proposed wording below, text to be inserted is formatted like
<ins>this</ins>, while wording to be deleted is formatted like <del>this</del>.
</p>

<p>
The proposed wording in this paper accomplishes three tasks:
</p>

<ol>
<li>New algorithms <tt>move</tt> and <tt>move_backward</tt> are introduced.</li>
<li>The <tt>random_shuffle</tt> signature is altered to accept rvalue generators.</li>
<li>The requirements on the <tt>value_type</tt> of several algorithms are
reduced from <tt>CopyConstructible</tt> and <tt>CopyAssignable</tt> to
<tt>MoveConstructible</tt>
and <tt>MoveAssignable</tt>.</li>
</ol>

<p>
This third action is the most important as it allows clients to use these
algorithms with sequences of movable but non-copyable types.  For example:
</p>

<blockquote><pre>
vector&lt;unique_ptr&lt;T&gt; &gt; v;
...
sort(v.begin(), v.end(), indirect_less());  // ok to sort unique_ptr's
</pre></blockquote>

<h2><a name="25 - Algorithms library"></a>25 - Algorithms library</h2>

<p>
<b>Header <tt>&lt;algorithm&gt;</tt> synopsis</b>
</p>

<blockquote class="note">
<p>
The synopsis is updated with two new functions:  <tt>move</tt> and
<tt>move_backward</tt>.  These two functions are merely convenience functions as
any copying style algorithm can be turned into a moving style algorithm with the
use of <tt>move_iterator</tt>.  For example: </p>

<blockquote><pre>
copy(make_move_iterator(first), make_move_iterator(last), result);
</pre></blockquote>

<p>
is equivalent to: </p>

<blockquote><pre>
move(first, last, result);
</pre></blockquote>

<p>
However the anticipated frequency of use of <tt>move</tt> and
<tt>move_backward</tt> warrant the special treatment. </p>

<p>
Additionally the signature (though not the description) of
<tt>random_shuffle</tt> is modified so as to accept lvalue and rvalue random
number generators. </p>

</blockquote>

<blockquote><pre>
namespace std {

  ...

  //<i>  <a href="lib-algorithms.html#lib.alg.modifying.operations">lib.alg.modifying.operations</a>, modifying sequence operations:</i>
  //<i>  <a href="lib-algorithms.html#lib.alg.copy">lib.alg.copy</a>, copy:</i>
  template&lt;class InputIterator, class OutputIterator&gt;
    OutputIterator copy(InputIterator <i>first</i>, InputIterator <i>last</i>,
			OutputIterator <i>result</i>);
  template&lt;class BidirectionalIterator1, class BidirectionalIterator2&gt;
    BidirectionalIterator2
      copy_backward
	(BidirectionalIterator1 <i>first</i>, BidirectionalIterator1 <i>last</i>,
	 BidirectionalIterator2 <i>result</i>);

  <ins>template&lt;class InputIterator, class OutputIterator&gt;
    OutputIterator move(InputIterator <i>first</i>, InputIterator <i>last</i>,
			OutputIterator <i>result</i>);
  template&lt;class BidirectionalIterator1, class BidirectionalIterator2&gt;
    BidirectionalIterator2
      move_backward
	(BidirectionalIterator1 <i>first</i>, BidirectionalIterator1 <i>last</i>,
	 BidirectionalIterator2 <i>result</i>);</ins>

  ...

  template&lt;class RandomAccessIterator&gt;
    void random_shuffle(RandomAccessIterator <i>first</i>,
			RandomAccessIterator <i>last</i>);
  template&lt;class RandomAccessIterator, class RandomNumberGenerator&gt;
    void random_shuffle(RandomAccessIterator <i>first</i>,
			RandomAccessIterator <i>last</i>,
			RandomNumberGenerator&amp;<ins>&amp;</ins> <i>rand</i>);

  ...

}
</pre></blockquote>

<h3><a name="25.2 - Mutating sequence operations"></a>25.2 - Mutating sequence operations</h3>

<blockquote class="note">
<p>
Insert new section for <tt>move</tt> and <tt>move_backward</tt>. </p>
</blockquote>

<h4><a name="25.2.2 - Move"></a><ins>25.2.2 - Move</ins></h4>

<blockquote><pre><ins>
template&lt;class InputIterator, class OutputIterator&gt;
  OutputIterator move(InputIterator <i>first</i>, InputIterator <i>last</i>,
                      OutputIterator <i>result</i>);
</ins></pre></blockquote>

<p><ins>
-1- <b>Effects:</b>
Moves elements in the range <tt>[<i>first</i>, <i>last</i>)</tt> to the range
<tt>[<i>result</i>, <i>result</i> + (<i>last</i> - <i>first</i>))</tt> starting
from <tt><i>first</i></tt> and proceeding to <tt><i>last</i></tt>. For each
non-negative integer <tt><i>n</i> &lt; (<i>last</i>-<i>first</i>)</tt>, performs
<tt>*(<i>result</i> + <i>n</i>) = std::move(*(<i>first</i> + <i>n</i>))</tt>.
</ins></p>

<p><ins>
-2- <b>Returns:</b>
<tt><i>result</i> + (<i>last</i> - <i>first</i>)</tt>.
</ins></p>

<p><ins>
-3- <b>Requires:</b>
<tt>result</tt> shall not be in the range <tt>[<i>first</i>, <i>last</i>)</tt>.
</ins></p>

<p><ins>
-4- <b>Complexity:</b>
Exactly <tt><i>last</i> - <i>first</i></tt> move assignments.
</ins></p>

<blockquote><pre><ins>
template&lt;class BidirectionalIterator1, class BidirectionalIterator2&gt;
  BidirectionalIterator2
    move_backward(BidirectionalIterator1 <i>first</i>,
                  BidirectionalIterator1 <i>last</i>,
                  BidirectionalIterator2 <i>result</i>);
</ins></pre></blockquote>

<p><ins>
-5- <b>Effects:</b>
Moves elements in the range <tt>[<i>first</i>, <i>last</i>)</tt> to the range
<tt>[<i>result</i> - (<i>last</i> - <i>first</i>), <i>result</i>)</tt> starting
from <tt><i>last</i> - 1</tt> and proceeding to <tt><i>first</i></tt> . *
</ins></p>

<blockquote><ins>
[<i>Footnote:</i> <tt>move_backward</tt> should be used instead of <tt>move</tt>
when <tt><i>last</i></tt> is in the range <tt>[<i>result</i> - (<i>last</i> -
<i>first</i>), <i>result</i>)</tt>. <i>-- end footnote</i>]
</ins></blockquote>

<p><ins>
For each positive integer <tt>n &lt;= (<i>last</i> - <i>first</i>)</tt>,
performs <tt>*(<i>result</i> - n) = std::move(*(<i>last</i> - n))</tt>.
</ins></p>

<p><ins>
-6- <b>Requires:</b>
<tt><i>result</i></tt> shall not be in the range <tt>[<i>first</i>,
<i>last</i>)</tt>.
</ins></p>

<p><ins>
-7- <b>Returns:</b>
<tt><i>result</i> - (<i>last</i> - <i>first</i>)</tt>.
</ins></p>

<p><ins>
-8- <b>Complexity:</b>
Exactly <tt><i>last</i> - <i>first</i></tt> move assignments.
</ins></p>

<h4><a name="25.2.3 - Swap"></a>25.2.<del>2</del><ins>3</ins> - Swap</h4>

<blockquote class="note">
<p>
Reduce requirements for <tt>swap</tt> to <tt>MoveConstructible</tt> and
<tt>MoveAssignable</tt>. </p>
</blockquote>

<blockquote><pre>
template&lt;class T&gt; void swap(T&amp; <i>a</i>, T&amp; <i>b</i>);
</pre></blockquote>

<p>
-1- <b>Requires:</b>
Type <tt>T</tt> is <del><tt>CopyConstructible</tt>
(<a
href="lib-utilities.html#lib.copyconstructible">lib.copyconstructible</a>)</del>
<ins><tt>MoveConstructible</tt></ins> and <del><tt>Assignable</tt>
(<a
href="lib-containers.html#lib.container.requirements">lib.container.requirements</a>)</del>
<ins><tt>MoveAssignable</tt></ins>.
</p>

<h4><a name="25.2.8 - Remove"></a>25.2.<del>7</del><ins>8</ins> - Remove</h4>

<blockquote class="note">
<p>
Reduce requirements for <tt>remove</tt> and <tt>remove_if</tt> to
<tt>MoveConstructible</tt> and <tt>MoveAssignable</tt>. </p>
</blockquote>

<blockquote><pre>
template&lt;class ForwardIterator, class T&gt;
  ForwardIterator remove(ForwardIterator <i>first</i>, ForwardIterator <i>last</i>,
                         const T&amp; <i>value</i>);

template&lt;class ForwardIterator, class Predicate&gt;
  ForwardIterator remove_if(ForwardIterator <i>first</i>, ForwardIterator <i>last</i>,
                            Predicate <i>pred</i>);
</pre></blockquote>

<p>
-1- <b>Requires:</b>
The type of <tt>*<i>first</i></tt> shall satisfy the
<del><tt>Assignable</tt></del> <ins><tt>MoveAssignable</tt></ins> requirements.
</p>

<p>
-2- <b>Effects:</b> Eliminates all the elements referred to by iterator
<tt>i</tt> in the range <tt>[<i>first</i>, <i>last</i>)</tt> for which the
following corresponding conditions hold: <tt>*i == <i>value</i>, <i>pred</i>(*i)
!= false</tt>.
</p>

<p>
-3- <b>Returns:</b>
The end of the resulting range.
</p>

<p>
-4- <b>Remarks:</b> Stable.
</p>

<p>
-5- <b>Complexity:</b>
Exactly <tt><i>last</i> - <i>first</i></tt> applications of the corresponding
predicate.
</p>

<h4><a name="25.2.9 - Unique"></a>25.2.<del>8</del><ins>9</ins> - Unique</h4>

<blockquote class="note">
<p>
Reduce requirements for <tt>unique</tt> to <tt>MoveConstructible</tt> and
<tt>MoveAssignable</tt>. </p>
</blockquote>

<blockquote><pre>
template&lt;class ForwardIterator&gt;
  ForwardIterator unique(ForwardIterator <i>first</i>, ForwardIterator <i>last</i>);

template&lt;class ForwardIterator, class BinaryPredicate&gt;
  ForwardIterator unique(ForwardIterator <i>first</i>, ForwardIterator <i>last</i>,
                         BinaryPredicate <i>pred</i>);
</pre></blockquote>

<p>
-1- <b>Effects:</b>
Eliminates all but the first element from every consecutive group of equal
elements referred to by the iterator <tt>i</tt> in the range <tt>[<i>first</i>,
<i>last</i>)</tt> for which the following corresponding conditions hold: <tt>*i
== *(i - 1)</tt> or <tt><i>pred</i>(*i, *(i - 1)) != false</tt>
</p>

<p>
-2- <b>Requires:</b> The comparison function shall be an equivalence relation.
<ins>The type of <tt>*<i>first</i></tt> shall satisfy the
<tt>MoveAssignable</tt> requirements.</ins>
</p>

<p>
-3- <b>Returns:</b>
The end of the resulting range.
</p>

<p>
-4- <b>Complexity:</b>
If the range <tt>(<i>last</i> - <i>first</i>)</tt> is not empty, exactly
<tt>(<i>last</i> - <i>first</i>) - 1</tt> applications of the corresponding
predicate, otherwise no applications of the predicate.
</p>

<h4><a name="25.2.10 - Rotate"></a>25.2.<del>9</del><ins>10</ins> - Rotate</h4>

<blockquote class="note">
<p>
Reduce requirements for <tt>rotate</tt> to <tt>MoveConstructible</tt> and
<tt>MoveAssignable</tt>.  Note that we already have <tt>Swappable</tt> as well.
</p>
</blockquote>

<blockquote><pre>
template&lt;class ForwardIterator&gt;
  void rotate(ForwardIterator <i>first</i>, ForwardIterator <i>middle</i>,
              ForwardIterator <i>last</i>);
</pre></blockquote>

<p>
-1- <b>Effects:</b>
For each non-negative integer <tt>i &lt; (<i>last</i> - <i>first</i>)</tt>,
places the element from the position <tt><i>first</i> + i</tt> into position
<tt><i>first</i> + (i + (<i>last</i> - <i>middle</i>)) % (<i>last</i> -
<i>first</i>)</tt>.
</p>

<p>
-2- <b>Remarks:</b>
This is a left rotate.
</p>

<p>
-3- <b>Requires:</b>
<tt>[<i>first</i>, <i>middle</i>)</tt> and <tt>[<i>middle</i>, <i>last</i>)</tt>
are valid ranges. The type of <tt>*<i>first</i></tt> shall satisfy the
<tt>Swappable</tt> requirements (20.1.4)<ins>, the <tt>MoveConstructible</tt>
requirements, and the <tt>MoveAssignable</tt> requirements</ins>.
</p>

<p>
-4- <b>Complexity:</b>
At most <tt><i>last</i> - <i>first</i></tt> swaps.
</p>

<h4><a name="25.2.12 - Random shuffle"></a>25.2.<del>11</del><ins>12</ins> - Random shuffle</h4>

<blockquote class="note">
<p>
Change the signature of <tt>random_shuffle</tt>.  No other change is needed for
the specification of <tt>random_shuffle</tt>. </p>
</blockquote>

<blockquote><pre>
template&lt;class RandomAccessIterator&gt;
  void random_shuffle(RandomAccessIterator <i>first</i>,
                      RandomAccessIterator <i>last</i>);

template&lt;class RandomAccessIterator, class RandomNumberGenerator&gt;
  void random_shuffle(RandomAccessIterator <i>first</i>,
                      RandomAccessIterator <i>last</i>,
                      RandomNumberGenerator&amp;<ins>&amp;</ins> <i>rand</i>);
</pre></blockquote>

<h4><a name="25.2.13 - Partitions"></a>25.2.<del>12</del><ins>13</ins> - Partitions</h4>

<blockquote class="note">
<p>
Reduce requirements for <tt>stable_partition</tt> to <tt>MoveConstructible</tt>
and <tt>MoveAssignable</tt>.  Note that we already have <tt>Swappable</tt> as
well. </p>
</blockquote>

<blockquote><pre>
template&lt;class BidirectionalIterator, class Predicate&gt;
  BidirectionalIterator
    stable_partition(BidirectionalIterator <i>first</i>,
                     BidirectionalIterator <i>last</i>, Predicate <i>pred</i>);
</pre></blockquote>

<p>
-5- <b>Effects:</b>
Places all the elements in the range <tt>[<i>first</i>, <i>last</i>)</tt> that
satisfy <tt><i>pred</i></tt> before all the elements that do not satisfy it.
</p>

<p>
-6- <b>Returns:</b>
An iterator <tt>i</tt> such that for any iterator <tt>j</tt> in the range
<tt>[<i>first</i>, i)</tt>, <tt><i>pred</i>(*j) != false</tt>, and for any
iterator <tt>k</tt> in the range <tt>[i, <i>last</i>)</tt>, <tt><i>pred</i>(*j)
== false</tt>. The relative order of the elements in both groups is preserved.
</p>

<p>
-7- <b>Requires:</b> The type of <tt>*<i>first</i></tt> shall satisfy the
<tt>Swappable</tt> requirements (20.1.4)<ins>, the <tt>MoveConstructible</tt>
requirements, and the <tt>MoveAssignable</tt> requirements</ins>.
</p>

<p>
-8- <b>Complexity:</b>
At most <tt>(<tt><i>last</i></tt> - <tt><i>first</i></tt>) *
log(<tt><i>last</i></tt> - <tt><i>first</i></tt>)</tt> swaps, but only linear
number of swaps if there is enough extra memory. Exactly
<tt><tt><i>last</i></tt> - <tt><i>first</i></tt></tt> applications of the
predicate.
</p>

<h3><a name="25.3 - Sorting and related operations"></a>25.3 - Sorting and related operations</h3>

<h4><a name="25.3.1 - Sorting"></a>25.3.1 - Sorting</h4>

<h5><a name="25.3.1.1 - sort"></a>25.3.1.1 - sort</h5>

<blockquote class="note">
<p>
Reduce requirements for <tt>sort</tt> to <tt>MoveConstructible</tt> and
<tt>MoveAssignable</tt>.  Note that we already have <tt>Swappable</tt> as well.
</p>
</blockquote>

<blockquote><pre>
template&lt;class RandomAccessIterator&gt;
  void sort(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>last</i>);

template&lt;class RandomAccessIterator, class Compare&gt;
  void sort(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>last</i>,
            Compare <i>comp</i>);
</pre></blockquote>

<p>
-1- <b>Effects:</b>
Sorts the elements in the range <tt>[<i>first</i>, <i>last</i>)</tt>.
</p>

<p>
-2- <b>Requires:</b> The type of <tt>*<i>first</i></tt> shall satisfy the
<tt>Swappable</tt> requirements (20.1.4)<ins>, the <tt>MoveConstructible</tt>
requirements, and the <tt>MoveAssignable</tt> requirements</ins>.
</p>

<p>
-3- <b>Complexity:</b>
Approximately <tt>N  <i>log</i>  N</tt> (where <tt>N == <i>last</i> -
<i>first</i></tt>) comparisons on the average.*
</p>

<h5><a name="25.3.1.2 - stable_sort"></a>25.3.1.2 - stable_sort</h5>

<blockquote class="note">
<p>
Reduce requirements for <tt>stable_sort</tt> to <tt>MoveConstructible</tt> and
<tt>MoveAssignable</tt>.  Note that we already have <tt>Swappable</tt> as well.
</p>
</blockquote>

<blockquote><pre>
template&lt;class RandomAccessIterator&gt;
  void stable_sort(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>last</i>);

template&lt;class RandomAccessIterator, class Compare&gt;
  void stable_sort(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>last</i>,
                   Compare <i>comp</i>);
</pre></blockquote>

<p>
-1- <b>Effects:</b>
Sorts the elements in the range <tt>[<i>first</i>, <i>last</i>)</tt>.
</p>

<p>
-2- <b>Requires:</b> The type of <tt>*<i>first</i></tt> shall satisfy the
<tt>Swappable</tt> requirements (20.1.4)<ins>, the <tt>MoveConstructible</tt>
requirements, and the <tt>MoveAssignable</tt> requirements</ins>.
</p>

<p>
-3- <b>Complexity:</b>
It does at most
<tt>N(<i>log  </i>N)<sup>2</sup></tt>
(where <tt>N == <i>last</i> - <i>first</i></tt>) comparisons; if enough extra
memory is available, it is <tt>N  <i>log</i>  N</tt>.
</p>

<p>
-4- <b>Remarks:</b> Stable.
</p>

<h5><a name="25.3.1.3 - partial_sort"></a>25.3.1.3 - partial_sort</h5>

<blockquote class="note">
<p>
Reduce requirements for <tt>partial_sort</tt> to <tt>MoveConstructible</tt> and
<tt>MoveAssignable</tt>.  Note that we already have <tt>Swappable</tt> as well.
</p>
</blockquote>

<blockquote><pre>
template&lt;class RandomAccessIterator&gt;
  void partial_sort(RandomAccessIterator <i>first</i>,
                    RandomAccessIterator <i>middle</i>,
                    RandomAccessIterator <i>last</i>);

template&lt;class RandomAccessIterator, class Compare&gt;
  void partial_sort(RandomAccessIterator <i>first</i>,
                    RandomAccessIterator <i>middle</i>,
                    RandomAccessIterator <i>last</i>,
                    Compare <i>comp</i>);
</pre></blockquote>

<p>
-1- <b>Effects:</b>
Places the first <tt><i>middle</i> - <i>first</i></tt> sorted elements from the
range <tt>[<i>first</i>, <i>last</i>)</tt> into the range <tt>[<i>first</i>,
<i>middle</i>)</tt>. The rest of the elements in the range <tt>[<i>middle</i>,
<i>last</i>)</tt> are placed in an unspecified order.
</p>

<p>
-2- <b>Requires:</b> The type of <tt>*<i>first</i></tt> shall satisfy the
<tt>Swappable</tt> requirements (20.1.4)<ins>, the <tt>MoveConstructible</tt>
requirements, and the <tt>MoveAssignable</tt> requirements</ins>.
</p>

<p>
-3- <b>Complexity:</b>
It takes approximately <tt>(<i>last</i> - <i>first</i>) * log(<i>middle</i> -
<i>first</i>)</tt> comparisons.
</p>

<h5><a name="25.3.1.4 - partial_sort_copy"></a>25.3.1.4 - partial_sort_copy</h5>

<blockquote class="note">
<p>
Reduce requirements for <tt>partial_sort</tt> to <tt>MoveConstructible</tt> and
<tt>CopyAssignable</tt>.  Note that we already have <tt>Swappable</tt> as well.
Also note that while <tt>CopyAssignable</tt> is required,
<tt>CopyConstructible</tt> is not. </p>
</blockquote>

<blockquote><pre>
template&lt;class InputIterator, class RandomAccessIterator&gt;
  RandomAccessIterator
    partial_sort_copy(InputIterator <i>first</i>, InputIterator <i>last</i>,
                      RandomAccessIterator <i>result_first</i>,
                      RandomAccessIterator <i>result_last</i>);

template&lt;class InputIterator, class RandomAccessIterator,
         class Compare&gt;
  RandomAccessIterator
    partial_sort_copy(InputIterator <i>first</i>, InputIterator <i>last</i>,
                      RandomAccessIterator <i>result_first</i>,
                      RandomAccessIterator <i>result_last</i>,
                      Compare <i>comp</i>);
</pre></blockquote>

<p>
-1- <b>Effects:</b>
Places the first <tt>min(<i>last</i> - <i>first</i>, <i>result_last</i> -
<i>result_first</i>)</tt> sorted elements into the range
<tt>[<i>result_first</i>, <i>result_first</i> + min(<i>last</i> - <i>first</i>,
<i>result_last</i> - <i>result_first</i>))</tt>.
</p>

<p>
-2- <b>Returns:</b>
The smaller of: <tt><i>result_last</i></tt> or <tt><i>result_first</i> +
(<i>last</i> - <i>first</i>)</tt>
</p>

<p>
-3- <b>Requires:</b> The type of <tt>*<i>result_first</i></tt> shall satisfy the
<tt>Swappable</tt> requirements (20.1.4)<ins>, the <tt>MoveConstructible</tt>
requirements, and the <tt>CopyAssignable</tt> requirements</ins>.
</p>

<p>
-4- <b>Complexity:</b>
Approximately <tt>(<i>last</i> - <i>first</i>) * log(min(<i>last</i> -
<i>first</i>, <i>result_last</i> - <i>result_first</i>))</tt> comparisons.
</p>

<h4><a name="25.3.2 - Nth element"></a>25.3.2 - Nth element</h4>

<blockquote class="note">
<p>
Reduce requirements for <tt>nth_element</tt> to <tt>MoveConstructible</tt> and
<tt>MoveAssignable</tt>.  Note that we already have <tt>Swappable</tt> as well.
</p>
</blockquote>

<blockquote><pre>
template&lt;class RandomAccessIterator&gt;
  void nth_element(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>nth</i>,
                   RandomAccessIterator <i>last</i>);

template&lt;class RandomAccessIterator, class Compare&gt;
  void nth_element(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>nth</i>,
                   RandomAccessIterator <i>last</i>,  Compare <i>comp</i>);
</pre></blockquote>

<p>
-1- After <tt>nth_element</tt> the element in the position pointed to by
<tt><i>nth</i></tt> is the element that would be in that position if the whole
range were sorted. Also for any iterator <tt>i</tt> in the range
<tt>[<i>first</i>, <i>nth</i>)</tt> and any iterator <tt>j</tt> in the range
<tt>[<i>nth</i>, <i>last</i>)</tt> it holds that: <tt>!(*i &gt; *j)</tt> or
<tt><i>comp</i>(*j, *i) == false</tt>.
</p>

<p>
-2- <b>Requires:</b> The type of <tt>*<i>first</i></tt> shall satisfy the
<tt>Swappable</tt> requirements (20.1.4)<ins>, the <tt>MoveConstructible</tt>
requirements, and the <tt>MoveAssignable</tt> requirements</ins>.
</p>

<p>
-3- <b>Complexity:</b>
Linear on average.
</p>

<h4><a name="25.3.4 - Merge"></a>25.3.4 - Merge</h4>

<blockquote class="note">
<p>
Reduce requirements for <tt>inplace_merge</tt> to <tt>MoveConstructible</tt> and
<tt>MoveAssignable</tt>.  Note that we already have <tt>Swappable</tt> as well.
</p>
</blockquote>

<blockquote><pre>
template&lt;class BidirectionalIterator&gt;
  void inplace_merge(BidirectionalIterator <i>first</i>,
                     BidirectionalIterator <i>middle</i>,
                     BidirectionalIterator <i>last</i>);

template&lt;class BidirectionalIterator, class Compare&gt;
  void inplace_merge(BidirectionalIterator <i>first</i>,
                     BidirectionalIterator <i>middle</i>,
                     BidirectionalIterator <i>last</i>, Compare <i>comp</i>);
</pre></blockquote>

<p>
-6- <b>Effects:</b>
Merges two sorted consecutive ranges <tt>[<i>first</i>, <i>middle</i>)</tt> and
<tt>[middle, <i>last</i>)</tt>, putting the result of the merge into the range
<tt>[<i>first</i>, <i>last</i>)</tt>. The resulting range will be in
non-decreasing order; that is, for every iterator <tt>i</tt> in
<tt>[<i>first</i>, <i>last</i>)</tt> other than <tt><i>first</i></tt>, the
condition <tt>*i &lt; *(i - 1)</tt> or, respectively, <tt><i>comp</i>(*i, *(i -
1))</tt> will be false.
</p>

<p>
-7- <b>Requires:</b> The type of <tt>*<i>first</i></tt> shall satisfy the
<tt>Swappable</tt> requirements (20.1.4)<ins>, the <tt>MoveConstructible</tt>
requirements, and the <tt>MoveAssignable</tt> requirements</ins>.
</p>

<p>
-8- <b>Complexity:</b>
When enough additional memory is available, <tt>(<i>last</i> - <i>first</i>) -
1</tt> comparisons. If no additional memory is available, an algorithm with
complexity <tt>N  <i>log</i>  N</tt> (where <tt>N</tt> is equal to
<tt><i>last</i> - <i>first</i></tt>) may be used.
</p>

<p>
-9- <b>Remarks:</b> Stable.
</p>

<h4><a name="25.3.6 - Heap operations"></a>25.3.6 - Heap operations</h4>

<blockquote class="note">
<p>
Reduce requirements for the heap operations to <tt>MoveConstructible</tt> and
<tt>MoveAssignable</tt> (no longer requiring <tt>CopyConstructible</tt> and
<tt>CopyAssignable</tt>).  Note that we already have <tt>Swappable</tt> as well
for <tt>pop_heap</tt> and <tt>sort_heap</tt>. </p>
</blockquote>

<h5><a name="25.3.6.1 - push_heap"></a>25.3.6.1 - push_heap</h5>

<blockquote><pre>
template&lt;class RandomAccessIterator&gt;
  void push_heap(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>last</i>);

template&lt;class RandomAccessIterator, class Compare&gt;
  void push_heap(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>last</i>,
                 Compare <i>comp</i>);
</pre></blockquote>

<p>
-1- <b>Effects:</b>
Places the value in the location <tt><i>last</i> - 1</tt> into the resulting
heap <tt>[<i>first</i>, <i>last</i>)</tt>.
</p>

<p>
-2- <b>Requires:</b>
The range <tt>[<i>first</i>, last - 1)</tt> shall be a valid heap.  <ins>The
type of <tt>*<i>first</i></tt> shall satisfy the <tt>MoveConstructible</tt>
requirements, and the <tt>MoveAssignable</tt> requirements</ins>.
</p>

<p>
-3- <b>Complexity:</b>
At most <tt>log(<i>last</i> - <i>first</i>)</tt> comparisons.
</p>

<h5><a name="25.3.6.2 - pop_heap"></a>25.3.6.2 - pop_heap</h5>

<blockquote><pre>
template&lt;class RandomAccessIterator&gt;
  void pop_heap(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>last</i>);

template&lt;class RandomAccessIterator, class Compare&gt;
  void pop_heap(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>last</i>,
                Compare <i>comp</i>);
</pre></blockquote>

<p>
-1- <b>Effects:</b>
Swaps the value in the location <tt><i>first</i></tt> with the value in the
location <tt><i>last</i> - 1</tt> and makes <tt>[<i>first</i>, last - 1)</tt>
into a heap.
</p>

<p>
-2- <b>Requires:</b>
The range <tt>[<i>first</i>, <i>last</i>)</tt> shall be a valid heap.  The type
of <tt>*<i>first</i></tt> shall satisfy the <tt>Swappable</tt> requirements
(20.1.4)<ins>, the <tt>MoveConstructible</tt> requirements, and the
<tt>MoveAssignable</tt> requirements</ins>.
</p>

<p>
-3- <b>Complexity:</b>
At most <tt>2 * log(<i>last</i> - <i>first</i>)</tt> comparisons.
</p>

<h5><a name="25.3.6.3 - make_heap"></a>25.3.6.3 - make_heap</h5>

<blockquote><pre>
template&lt;class RandomAccessIterator&gt;
  void make_heap(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>last</i>);

template&lt;class RandomAccessIterator, class Compare&gt;
  void make_heap(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>last</i>,
                 Compare <i>comp</i>);
</pre></blockquote>

<p>
-1- <b>Effects:</b>
Constructs a heap out of the range <tt>[<i>first</i>, <i>last</i>)</tt>.
</p>

<p><ins>
-2- <b>Requires:</b> The type of <tt>*<i>first</i></tt> shall satisfy the
<tt>MoveConstructible</tt> requirements, and the <tt>MoveAssignable</tt>
requirements.
</ins></p>

<p>
-3- <b>Complexity:</b>
At most <tt>3 * (<i>last</i> - <i>first</i>)</tt> comparisons.
</p>

<h5><a name="25.3.6.4 - sort_heap"></a>25.3.6.4 - sort_heap</h5>

<blockquote><pre>
template&lt;class RandomAccessIterator&gt;
  void sort_heap(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>last</i>);

template&lt;class RandomAccessIterator, class Compare&gt;
  void sort_heap(RandomAccessIterator <i>first</i>, RandomAccessIterator <i>last</i>,
                 Compare <i>comp</i>);
</pre></blockquote>

<p>
-1- <b>Effects:</b>
Sorts elements in the heap <tt>[<i>first</i>, <i>last</i>)</tt>.
</p>

<p>
-2- <b>Requires:</b>
<ins>The range <tt>[<i>first</i>, <i>last</i>)</tt> shall be a valid heap.</ins>
 The type of <tt>*<i>first</i></tt> shall satisfy the <tt>Swappable</tt>
requirements (20.1.4)<ins>, the <tt>MoveConstructible</tt> requirements, and the
<tt>MoveAssignable</tt> requirements</ins>.
</p>

<p>
-3- <b>Complexity:</b>
At most <tt>N  <i>log</i>  N</tt> comparisons (where <tt>N == <i>last</i> -
<i>first</i></tt>).
</p>

</body>
</html>
