reversible-algorithm.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  4. <head>
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  6. <meta name="generator" content="Docutils 0.7: http://docutils.sourceforge.net/" />
  7. <title>The MPL Reference Manual: Reversible Algorithm</title>
  8. <link rel="stylesheet" href="../style.css" type="text/css" />
  9. </head>
  10. <body class="docframe refmanual">
  11. <table class="header"><tr class="header"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./inserter.html" class="navigation-link">Prev</a>&nbsp;<a href="./inserters.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./inserter.html" class="navigation-link">Back</a>&nbsp;Along</span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./algorithms-concepts.html" class="navigation-link">Up</a>&nbsp;<a href="../refmanual.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./refmanual_toc.html" class="navigation-link">Full TOC</a></span></td>
  12. <td class="header-group page-location"><a href="../refmanual.html" class="navigation-link">Front Page</a> / <a href="./algorithms.html" class="navigation-link">Algorithms</a> / <a href="./algorithms-concepts.html" class="navigation-link">Concepts</a> / <a href="./reversible-algorithm.html" class="navigation-link">Reversible Algorithm</a></td>
  13. </tr></table><div class="header-separator"></div>
  14. <div class="section" id="reversible-algorithm">
  15. <h1><a class="toc-backref" href="./algorithms-concepts.html#id1463">Reversible Algorithm</a></h1>
  16. <div class="section" id="id459">
  17. <h3><a class="subsection-title" href="#description" name="description">Description</a></h3>
  18. <p>A <a class="reference internal" href="./reversible-algorithm.html">Reversible Algorithm</a> is a member of a pair of
  19. transformation algorithms that iterate over their input sequence(s)
  20. in opposite directions. For each reversible
  21. algorithm <tt class="literal"><span class="pre">x</span></tt> there exists a <em>counterpart</em> algorithm <tt class="literal"><span class="pre">reverse_x</span></tt>,
  22. that exhibits the exact semantics of <tt class="literal"><span class="pre">x</span></tt> except that the elements
  23. of its input sequence argument(s) are processed in the reverse
  24. order.</p>
  25. </div>
  26. <div class="section" id="id460">
  27. <h3><a class="subsection-title" href="#expression-requirements" name="expression-requirements">Expression requirements</a></h3>
  28. <p>In the following table and subsequent specifications, <tt class="literal"><span class="pre">x</span></tt> is a placeholder token for the actual
  29. <a class="reference internal" href="./reversible-algorithm.html">Reversible Algorithm</a>'s name, <em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub> are
  30. <a class="reference internal" href="./forward-sequence.html">Forward Sequence</a>s, and <tt class="literal"><span class="pre">in</span></tt> is an <a class="reference internal" href="./inserter.html">Inserter</a>.</p>
  31. <table border="1" class="docutils table">
  32. <colgroup>
  33. <col width="48%" />
  34. <col width="28%" />
  35. <col width="23%" />
  36. </colgroup>
  37. <thead valign="bottom">
  38. <tr><th class="head">Expression</th>
  39. <th class="head">Type</th>
  40. <th class="head">Complexity</th>
  41. </tr>
  42. </thead>
  43. <tbody valign="top">
  44. <tr><td><tt class="literal"><span class="pre">x&lt;</span></tt><em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub>, ...<tt class="literal"><span class="pre">&gt;::type</span></tt></td>
  45. <td><a class="reference internal" href="./forward-sequence.html">Forward Sequence</a></td>
  46. <td>Unspecified.</td>
  47. </tr>
  48. <tr><td><tt class="literal"><span class="pre">x&lt;</span></tt><em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub>, ... <tt class="literal"><span class="pre">in&gt;::type</span></tt></td>
  49. <td>Any type</td>
  50. <td>Unspecified.</td>
  51. </tr>
  52. <tr><td><tt class="literal"><span class="pre">reverse_x&lt;</span></tt><em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub>, ...<tt class="literal"><span class="pre">&gt;::type</span></tt></td>
  53. <td><a class="reference internal" href="./forward-sequence.html">Forward Sequence</a></td>
  54. <td>Unspecified.</td>
  55. </tr>
  56. <tr><td><tt class="literal"><span class="pre">reverse_x&lt;</span></tt><em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub>, ... <tt class="literal"><span class="pre">in&gt;::type</span></tt></td>
  57. <td>Any type</td>
  58. <td>Unspecified.</td>
  59. </tr>
  60. </tbody>
  61. </table>
  62. </div>
  63. <div class="section" id="id461">
  64. <h3><a class="subsection-title" href="#expression-semantics" name="expression-semantics">Expression semantics</a></h3>
  65. <pre class="literal-block">
  66. typedef x&lt;<em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub>,...&gt;::type t;
  67. </pre>
  68. <table class="docutils field-list" frame="void" rules="none">
  69. <col class="field-name" />
  70. <col class="field-body" />
  71. <tbody valign="top">
  72. <tr class="field"><th class="field-name">Precondition:</th><td class="field-body"><p class="first"><em>s</em><sub>1</sub> is an <a class="reference internal" href="./extensible-sequence.html">Extensible Sequence</a>.</p>
  73. </td>
  74. </tr>
  75. <tr class="field"><th class="field-name">Semantics:</th><td class="field-body"><p class="first"><tt class="literal"><span class="pre">t</span></tt> is equivalent to</p>
  76. <pre class="literal-block">
  77. x&lt;
  78. <em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub>,...
  79. , <a href="./back-inserter.html" class="identifier">back_inserter</a>&lt; <a href="./clear.html" class="identifier">clear</a>&lt;<em>s</em><sub>1</sub>&gt;::type &gt;
  80. &gt;::type
  81. </pre>
  82. <p>if <tt class="literal"><span class="pre">has_push_back&lt;</span></tt><em>s</em><sub>1</sub><tt class="literal"><span class="pre">&gt;::value</span> <span class="pre">==</span> <span class="pre">true</span></tt> and</p>
  83. <pre class="literal-block">
  84. reverse_x&lt;
  85. <em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub>,...
  86. , <a href="./front-inserter.html" class="identifier">front_inserter</a>&lt; <a href="./clear.html" class="identifier">clear</a>&lt;<em>s</em><sub>1</sub>&gt;::type &gt;
  87. &gt;::type
  88. </pre>
  89. <p class="last">otherwise.</p>
  90. </td>
  91. </tr>
  92. </tbody>
  93. </table>
  94. <!-- .......................................................................... -->
  95. <pre class="literal-block">
  96. typedef x&lt;<em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub>,...in&gt;::type t;
  97. </pre>
  98. <table class="docutils field-list" frame="void" rules="none">
  99. <col class="field-name" />
  100. <col class="field-body" />
  101. <tbody valign="top">
  102. <tr class="field"><th class="field-name">Semantics:</th><td class="field-body"><tt class="literal"><span class="pre">t</span></tt> is the result of an <tt class="literal"><span class="pre">x</span></tt> invocation with arguments
  103. <em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,... <em>s</em><sub>n</sub>,...<tt class="literal"><span class="pre">in</span></tt>.</td>
  104. </tr>
  105. </tbody>
  106. </table>
  107. <!-- .......................................................................... -->
  108. <pre class="literal-block">
  109. typedef reverse_x&lt;<em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,... <em>s</em><sub>n</sub>,... &gt;::type t;
  110. </pre>
  111. <table class="docutils field-list" frame="void" rules="none">
  112. <col class="field-name" />
  113. <col class="field-body" />
  114. <tbody valign="top">
  115. <tr class="field"><th class="field-name">Precondition:</th><td class="field-body"><p class="first"><em>s</em><sub>1</sub> is an <a class="reference internal" href="./extensible-sequence.html">Extensible Sequence</a>.</p>
  116. </td>
  117. </tr>
  118. <tr class="field"><th class="field-name">Semantics:</th><td class="field-body"><p class="first"><tt class="literal"><span class="pre">t</span></tt> is equivalent to</p>
  119. <pre class="literal-block">
  120. x&lt;
  121. <em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub>,...
  122. , <a href="./front-inserter.html" class="identifier">front_inserter</a>&lt; <a href="./clear.html" class="identifier">clear</a>&lt;<em>s</em><sub>1</sub>&gt;::type &gt;
  123. &gt;::type
  124. </pre>
  125. <p>if <tt class="literal"><span class="pre">has_push_front&lt;</span></tt><em>s</em><sub>1</sub><tt class="literal"><span class="pre">&gt;::value</span> <span class="pre">==</span> <span class="pre">true</span></tt> and</p>
  126. <pre class="literal-block">
  127. reverse_x&lt;
  128. <em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub>,...
  129. , <a href="./back-inserter.html" class="identifier">back_inserter</a>&lt; <a href="./clear.html" class="identifier">clear</a>&lt;<em>s</em><sub>1</sub>&gt;::type &gt;
  130. &gt;::type
  131. </pre>
  132. <p class="last">otherwise.</p>
  133. </td>
  134. </tr>
  135. </tbody>
  136. </table>
  137. <!-- .......................................................................... -->
  138. <pre class="literal-block">
  139. typedef reverse_x&lt;<em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub>,... in&gt;::type t;
  140. </pre>
  141. <table class="docutils field-list" frame="void" rules="none">
  142. <col class="field-name" />
  143. <col class="field-body" />
  144. <tbody valign="top">
  145. <tr class="field"><th class="field-name">Semantics:</th><td class="field-body"><tt class="literal"><span class="pre">t</span></tt> is the result of a <tt class="literal"><span class="pre">reverse_x</span></tt> invocation with arguments
  146. <em>s</em><sub>1</sub>,<em>s</em><sub>2</sub>,...<em>s</em><sub>n</sub>,...<tt class="literal"><span class="pre">in</span></tt>.</td>
  147. </tr>
  148. </tbody>
  149. </table>
  150. </div>
  151. <div class="section" id="id462">
  152. <h3><a class="subsection-title" href="#example" name="example">Example</a></h3>
  153. <pre class="literal-block">
  154. typedef <a href="./transform.html" class="identifier">transform</a>&lt;
  155. <a href="./range-c.html" class="identifier">range_c</a>&lt;int,0,10&gt;
  156. , <a href="./plus.html" class="identifier">plus</a>&lt;<a href="./placeholders.html" class="identifier">_1</a>,<a href="./int.html" class="identifier">int_</a>&lt;7&gt; &gt;
  157. , <a href="./back-inserter.html" class="identifier">back_inserter</a>&lt; vector0&lt;&gt; &gt;
  158. &gt;::type r1;
  159. typedef <a href="./transform.html" class="identifier">transform</a>&lt; r1, <a href="./minus.html" class="identifier">minus</a>&lt;<a href="./placeholders.html" class="identifier">_1</a>,<a href="./int.html" class="identifier">int_</a>&lt;2&gt; &gt; &gt;::type r2;
  160. typedef <a href="./reverse-transform.html" class="identifier">reverse_transform</a>&lt;
  161. r2
  162. , <a href="./minus.html" class="identifier">minus</a>&lt;<a href="./placeholders.html" class="identifier">_1</a>,5&gt;
  163. , <a href="./front-inserter.html" class="identifier">front_inserter</a>&lt; vector0&lt;&gt; &gt;
  164. &gt;::type r3;
  165. <a href="./assert.html" class="identifier">BOOST_MPL_ASSERT</a>(( <a href="./equal.html" class="identifier">equal</a>&lt;r1, <a href="./range-c.html" class="identifier">range_c</a>&lt;int,7,17&gt; &gt; ));
  166. <a href="./assert.html" class="identifier">BOOST_MPL_ASSERT</a>(( <a href="./equal.html" class="identifier">equal</a>&lt;r2, <a href="./range-c.html" class="identifier">range_c</a>&lt;int,5,15&gt; &gt; ));
  167. <a href="./assert.html" class="identifier">BOOST_MPL_ASSERT</a>(( <a href="./equal.html" class="identifier">equal</a>&lt;r3, <a href="./range-c.html" class="identifier">range_c</a>&lt;int,0,10&gt; > ));
  168. </pre>
  169. </div>
  170. <div class="section" id="id463">
  171. <h3><a class="subsection-title" href="#models" name="models">Models</a></h3>
  172. <ul class="simple">
  173. <li><a class="reference internal" href="./transform.html">transform</a></li>
  174. <li><a class="reference internal" href="./remove.html">remove</a></li>
  175. <li><a class="reference internal" href="./replace.html">replace</a></li>
  176. </ul>
  177. </div>
  178. <div class="section" id="id464">
  179. <h3><a class="subsection-title" href="#see-also" name="see-also">See also</a></h3>
  180. <p><a class="reference internal" href="./transformation-algorithms.html">Transformation Algorithms</a>, <a class="reference internal" href="./inserter.html">Inserter</a></p>
  181. </div>
  182. </div>
  183. <div class="footer-separator"></div>
  184. <table class="footer"><tr class="footer"><td class="header-group navigation-bar"><span class="navigation-group"><a href="./inserter.html" class="navigation-link">Prev</a>&nbsp;<a href="./inserters.html" class="navigation-link">Next</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./inserter.html" class="navigation-link">Back</a>&nbsp;Along</span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./algorithms-concepts.html" class="navigation-link">Up</a>&nbsp;<a href="../refmanual.html" class="navigation-link">Home</a></span><span class="navigation-group-separator">&nbsp;|&nbsp;</span><span class="navigation-group"><a href="./refmanual_toc.html" class="navigation-link">Full TOC</a></span></td>
  185. <td><div class="copyright-footer"><div class="copyright">Copyright © 2001-2009 Aleksey Gurtovoy and David Abrahams</div>
  186. Distributed under the Boost Software License, Version 1.0. (See accompanying
  187. file LICENSE_1_0.txt or copy at <a class="reference external" href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)</div></td></tr></table></body>
  188. </html>