permutation_iterator.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.5: http://docutils.sourceforge.net/" />
  7. <title>Permutation Iterator</title>
  8. <meta name="author" content="Toon Knapen, David Abrahams, Roland Richter, Jeremy Siek" />
  9. <meta name="organization" content="Boost Consulting, Indiana University Open Systems Lab" />
  10. <meta name="date" content="2006-09-11" />
  11. <meta name="copyright" content="Copyright Toon Knapen, David Abrahams, Roland Richter, and Jeremy Siek 2003." />
  12. <link rel="stylesheet" href="../../../rst.css" type="text/css" />
  13. </head>
  14. <body>
  15. <div class="document" id="permutation-iterator">
  16. <h1 class="title">Permutation Iterator</h1>
  17. <table class="docinfo" frame="void" rules="none">
  18. <col class="docinfo-name" />
  19. <col class="docinfo-content" />
  20. <tbody valign="top">
  21. <tr><th class="docinfo-name">Author:</th>
  22. <td>Toon Knapen, David Abrahams, Roland Richter, Jeremy Siek</td></tr>
  23. <tr><th class="docinfo-name">Contact:</th>
  24. <td><a class="first reference external" href="mailto:dave&#64;boost-consulting.com">dave&#64;boost-consulting.com</a>, <a class="last reference external" href="mailto:jsiek&#64;osl.iu.edu">jsiek&#64;osl.iu.edu</a></td></tr>
  25. <tr><th class="docinfo-name">Organization:</th>
  26. <td><a class="first reference external" href="http://www.boost-consulting.com">Boost Consulting</a>, Indiana University <a class="last reference external" href="http://www.osl.iu.edu">Open Systems
  27. Lab</a></td></tr>
  28. <tr><th class="docinfo-name">Date:</th>
  29. <td>2006-09-11</td></tr>
  30. <tr><th class="docinfo-name">Copyright:</th>
  31. <td>Copyright Toon Knapen, David Abrahams, Roland Richter, and Jeremy Siek 2003.</td></tr>
  32. </tbody>
  33. </table>
  34. <!-- Distributed under the Boost -->
  35. <!-- Software License, Version 1.0. (See accompanying -->
  36. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  37. <table class="docutils field-list" frame="void" rules="none">
  38. <col class="field-name" />
  39. <col class="field-body" />
  40. <tbody valign="top">
  41. <tr class="field"><th class="field-name">abstract:</th><td class="field-body"><!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  42. <!-- Software License, Version 1.0. (See accompanying -->
  43. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  44. The permutation iterator adaptor provides a permuted view of a given
  45. range. That is, the view includes every element of the given range but
  46. in a potentially different order.</td>
  47. </tr>
  48. </tbody>
  49. </table>
  50. <div class="contents topic" id="table-of-contents">
  51. <p class="topic-title first">Table of Contents</p>
  52. <ul class="simple">
  53. <li><a class="reference internal" href="#introduction" id="id2">Introduction</a></li>
  54. <li><a class="reference internal" href="#reference" id="id3">Reference</a><ul>
  55. <li><a class="reference internal" href="#permutation-iterator-requirements" id="id4"><tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> requirements</a></li>
  56. <li><a class="reference internal" href="#permutation-iterator-models" id="id5"><tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> models</a></li>
  57. <li><a class="reference internal" href="#permutation-iterator-operations" id="id6"><tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> operations</a></li>
  58. </ul>
  59. </li>
  60. <li><a class="reference internal" href="#example" id="id7">Example</a></li>
  61. </ul>
  62. </div>
  63. <div class="section" id="introduction">
  64. <h1><a class="toc-backref" href="#id2">Introduction</a></h1>
  65. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  66. <!-- Software License, Version 1.0. (See accompanying -->
  67. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  68. <p>The adaptor takes two arguments:</p>
  69. <blockquote>
  70. <ul class="simple">
  71. <li>an iterator to the range V on which the permutation
  72. will be applied</li>
  73. <li>the reindexing scheme that defines how the
  74. elements of V will be permuted.</li>
  75. </ul>
  76. </blockquote>
  77. <p>Note that the permutation iterator is not limited to strict
  78. permutations of the given range V. The distance between begin and end
  79. of the reindexing iterators is allowed to be smaller compared to the
  80. size of the range V, in which case the permutation iterator only
  81. provides a permutation of a subrange of V. The indexes neither need
  82. to be unique. In this same context, it must be noted that the past the
  83. end permutation iterator is completely defined by means of the
  84. past-the-end iterator to the indices.</p>
  85. </div>
  86. <div class="section" id="reference">
  87. <h1><a class="toc-backref" href="#id3">Reference</a></h1>
  88. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  89. <!-- Software License, Version 1.0. (See accompanying -->
  90. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  91. <pre class="literal-block">
  92. template&lt; class ElementIterator
  93. , class IndexIterator
  94. , class ValueT = use_default
  95. , class CategoryT = use_default
  96. , class ReferenceT = use_default
  97. , class DifferenceT = use_default &gt;
  98. class permutation_iterator
  99. {
  100. public:
  101. permutation_iterator();
  102. explicit permutation_iterator(ElementIterator x, IndexIterator y);
  103. template&lt; class OEIter, class OIIter, class V, class C, class R, class D &gt;
  104. permutation_iterator(
  105. permutation_iterator&lt;OEIter, OIIter, V, C, R, D&gt; const&amp; r
  106. , typename enable_if_convertible&lt;OEIter, ElementIterator&gt;::type* = 0
  107. , typename enable_if_convertible&lt;OIIter, IndexIterator&gt;::type* = 0
  108. );
  109. reference operator*() const;
  110. permutation_iterator&amp; operator++();
  111. ElementIterator const&amp; base() const;
  112. private:
  113. ElementIterator m_elt; // exposition only
  114. IndexIterator m_order; // exposition only
  115. };
  116. template &lt;class ElementIterator, class IndexIterator&gt;
  117. permutation_iterator&lt;ElementIterator, IndexIterator&gt;
  118. make_permutation_iterator( ElementIterator e, IndexIterator i);
  119. </pre>
  120. <div class="section" id="permutation-iterator-requirements">
  121. <h2><a class="toc-backref" href="#id4"><tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> requirements</a></h2>
  122. <p><tt class="docutils literal"><span class="pre">ElementIterator</span></tt> shall model Random Access Traversal Iterator.
  123. <tt class="docutils literal"><span class="pre">IndexIterator</span></tt> shall model Readable Iterator. The value type of
  124. the <tt class="docutils literal"><span class="pre">IndexIterator</span></tt> must be convertible to the difference type of
  125. <tt class="docutils literal"><span class="pre">ElementIterator</span></tt>.</p>
  126. </div>
  127. <div class="section" id="permutation-iterator-models">
  128. <h2><a class="toc-backref" href="#id5"><tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> models</a></h2>
  129. <p><tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> models the same iterator traversal concepts
  130. as <tt class="docutils literal"><span class="pre">IndexIterator</span></tt> and the same iterator access concepts as
  131. <tt class="docutils literal"><span class="pre">ElementIterator</span></tt>.</p>
  132. <p>If <tt class="docutils literal"><span class="pre">IndexIterator</span></tt> models Single Pass Iterator and
  133. <tt class="docutils literal"><span class="pre">ElementIterator</span></tt> models Readable Iterator then
  134. <tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> models Input Iterator.</p>
  135. <p>If <tt class="docutils literal"><span class="pre">IndexIterator</span></tt> models Forward Traversal Iterator and
  136. <tt class="docutils literal"><span class="pre">ElementIterator</span></tt> models Readable Lvalue Iterator then
  137. <tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> models Forward Iterator.</p>
  138. <p>If <tt class="docutils literal"><span class="pre">IndexIterator</span></tt> models Bidirectional Traversal Iterator and
  139. <tt class="docutils literal"><span class="pre">ElementIterator</span></tt> models Readable Lvalue Iterator then
  140. <tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> models Bidirectional Iterator.</p>
  141. <p>If <tt class="docutils literal"><span class="pre">IndexIterator</span></tt> models Random Access Traversal Iterator and
  142. <tt class="docutils literal"><span class="pre">ElementIterator</span></tt> models Readable Lvalue Iterator then
  143. <tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> models Random Access Iterator.</p>
  144. <p><tt class="docutils literal"><span class="pre">permutation_iterator&lt;E1,</span> <span class="pre">X,</span> <span class="pre">V1,</span> <span class="pre">C2,</span> <span class="pre">R1,</span> <span class="pre">D1&gt;</span></tt> is interoperable
  145. with <tt class="docutils literal"><span class="pre">permutation_iterator&lt;E2,</span> <span class="pre">Y,</span> <span class="pre">V2,</span> <span class="pre">C2,</span> <span class="pre">R2,</span> <span class="pre">D2&gt;</span></tt> if and only if
  146. <tt class="docutils literal"><span class="pre">X</span></tt> is interoperable with <tt class="docutils literal"><span class="pre">Y</span></tt> and <tt class="docutils literal"><span class="pre">E1</span></tt> is convertible
  147. to <tt class="docutils literal"><span class="pre">E2</span></tt>.</p>
  148. </div>
  149. <div class="section" id="permutation-iterator-operations">
  150. <h2><a class="toc-backref" href="#id6"><tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> operations</a></h2>
  151. <p>In addition to those operations required by the concepts that
  152. <tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> models, <tt class="docutils literal"><span class="pre">permutation_iterator</span></tt> provides the
  153. following operations.</p>
  154. <p><tt class="docutils literal"><span class="pre">permutation_iterator();</span></tt></p>
  155. <table class="docutils field-list" frame="void" rules="none">
  156. <col class="field-name" />
  157. <col class="field-body" />
  158. <tbody valign="top">
  159. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Default constructs <tt class="docutils literal"><span class="pre">m_elt</span></tt> and <tt class="docutils literal"><span class="pre">m_order</span></tt>.</td>
  160. </tr>
  161. </tbody>
  162. </table>
  163. <p><tt class="docutils literal"><span class="pre">explicit</span> <span class="pre">permutation_iterator(ElementIterator</span> <span class="pre">x,</span> <span class="pre">IndexIterator</span> <span class="pre">y);</span></tt></p>
  164. <table class="docutils field-list" frame="void" rules="none">
  165. <col class="field-name" />
  166. <col class="field-body" />
  167. <tbody valign="top">
  168. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Constructs <tt class="docutils literal"><span class="pre">m_elt</span></tt> from <tt class="docutils literal"><span class="pre">x</span></tt> and <tt class="docutils literal"><span class="pre">m_order</span></tt> from <tt class="docutils literal"><span class="pre">y</span></tt>.</td>
  169. </tr>
  170. </tbody>
  171. </table>
  172. <pre class="literal-block">
  173. template&lt; class OEIter, class OIIter, class V, class C, class R, class D &gt;
  174. permutation_iterator(
  175. permutation_iterator&lt;OEIter, OIIter, V, C, R, D&gt; const&amp; r
  176. , typename enable_if_convertible&lt;OEIter, ElementIterator&gt;::type* = 0
  177. , typename enable_if_convertible&lt;OIIter, IndexIterator&gt;::type* = 0
  178. );
  179. </pre>
  180. <table class="docutils field-list" frame="void" rules="none">
  181. <col class="field-name" />
  182. <col class="field-body" />
  183. <tbody valign="top">
  184. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Constructs <tt class="docutils literal"><span class="pre">m_elt</span></tt> from <tt class="docutils literal"><span class="pre">r.m_elt</span></tt> and
  185. <tt class="docutils literal"><span class="pre">m_order</span></tt> from <tt class="docutils literal"><span class="pre">y.m_order</span></tt>.</td>
  186. </tr>
  187. </tbody>
  188. </table>
  189. <p><tt class="docutils literal"><span class="pre">reference</span> <span class="pre">operator*()</span> <span class="pre">const;</span></tt></p>
  190. <table class="docutils field-list" frame="void" rules="none">
  191. <col class="field-name" />
  192. <col class="field-body" />
  193. <tbody valign="top">
  194. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*(m_elt</span> <span class="pre">+</span> <span class="pre">*m_order)</span></tt></td>
  195. </tr>
  196. </tbody>
  197. </table>
  198. <p><tt class="docutils literal"><span class="pre">permutation_iterator&amp;</span> <span class="pre">operator++();</span></tt></p>
  199. <table class="docutils field-list" frame="void" rules="none">
  200. <col class="field-name" />
  201. <col class="field-body" />
  202. <tbody valign="top">
  203. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><tt class="docutils literal"><span class="pre">++m_order</span></tt></td>
  204. </tr>
  205. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*this</span></tt></td>
  206. </tr>
  207. </tbody>
  208. </table>
  209. <p><tt class="docutils literal"><span class="pre">ElementIterator</span> <span class="pre">const&amp;</span> <span class="pre">base()</span> <span class="pre">const;</span></tt></p>
  210. <table class="docutils field-list" frame="void" rules="none">
  211. <col class="field-name" />
  212. <col class="field-body" />
  213. <tbody valign="top">
  214. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">m_order</span></tt></td>
  215. </tr>
  216. </tbody>
  217. </table>
  218. <pre class="literal-block">
  219. template &lt;class ElementIterator, class IndexIterator&gt;
  220. permutation_iterator&lt;ElementIterator, IndexIterator&gt;
  221. make_permutation_iterator(ElementIterator e, IndexIterator i);
  222. </pre>
  223. <table class="docutils field-list" frame="void" rules="none">
  224. <col class="field-name" />
  225. <col class="field-body" />
  226. <tbody valign="top">
  227. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">permutation_iterator&lt;ElementIterator,</span> <span class="pre">IndexIterator&gt;(e,</span> <span class="pre">i)</span></tt></td>
  228. </tr>
  229. </tbody>
  230. </table>
  231. </div>
  232. </div>
  233. <div class="section" id="example">
  234. <h1><a class="toc-backref" href="#id7">Example</a></h1>
  235. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  236. <!-- Software License, Version 1.0. (See accompanying -->
  237. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  238. <pre class="literal-block">
  239. using namespace boost;
  240. int i = 0;
  241. typedef std::vector&lt; int &gt; element_range_type;
  242. typedef std::list&lt; int &gt; index_type;
  243. static const int element_range_size = 10;
  244. static const int index_size = 4;
  245. element_range_type elements( element_range_size );
  246. for(element_range_type::iterator el_it = elements.begin() ; el_it != elements.end() ; ++el_it)
  247. *el_it = std::distance(elements.begin(), el_it);
  248. index_type indices( index_size );
  249. for(index_type::iterator i_it = indices.begin() ; i_it != indices.end() ; ++i_it )
  250. *i_it = element_range_size - index_size + std::distance(indices.begin(), i_it);
  251. std::reverse( indices.begin(), indices.end() );
  252. typedef permutation_iterator&lt; element_range_type::iterator, index_type::iterator &gt; permutation_type;
  253. permutation_type begin = make_permutation_iterator( elements.begin(), indices.begin() );
  254. permutation_type it = begin;
  255. permutation_type end = make_permutation_iterator( elements.begin(), indices.end() );
  256. std::cout &lt;&lt; &quot;The original range is : &quot;;
  257. std::copy( elements.begin(), elements.end(), std::ostream_iterator&lt; int &gt;( std::cout, &quot; &quot; ) );
  258. std::cout &lt;&lt; &quot;\n&quot;;
  259. std::cout &lt;&lt; &quot;The reindexing scheme is : &quot;;
  260. std::copy( indices.begin(), indices.end(), std::ostream_iterator&lt; int &gt;( std::cout, &quot; &quot; ) );
  261. std::cout &lt;&lt; &quot;\n&quot;;
  262. std::cout &lt;&lt; &quot;The permutated range is : &quot;;
  263. std::copy( begin, end, std::ostream_iterator&lt; int &gt;( std::cout, &quot; &quot; ) );
  264. std::cout &lt;&lt; &quot;\n&quot;;
  265. std::cout &lt;&lt; &quot;Elements at even indices in the permutation : &quot;;
  266. it = begin;
  267. for(i = 0; i &lt; index_size / 2 ; ++i, it+=2 ) std::cout &lt;&lt; *it &lt;&lt; &quot; &quot;;
  268. std::cout &lt;&lt; &quot;\n&quot;;
  269. std::cout &lt;&lt; &quot;Permutation backwards : &quot;;
  270. it = begin + (index_size);
  271. assert( it != begin );
  272. for( ; it-- != begin ; ) std::cout &lt;&lt; *it &lt;&lt; &quot; &quot;;
  273. std::cout &lt;&lt; &quot;\n&quot;;
  274. std::cout &lt;&lt; &quot;Iterate backward with stride 2 : &quot;;
  275. it = begin + (index_size - 1);
  276. for(i = 0 ; i &lt; index_size / 2 ; ++i, it-=2 ) std::cout &lt;&lt; *it &lt;&lt; &quot; &quot;;
  277. std::cout &lt;&lt; &quot;\n&quot;;
  278. </pre>
  279. <p>The output is:</p>
  280. <pre class="literal-block">
  281. The original range is : 0 1 2 3 4 5 6 7 8 9
  282. The reindexing scheme is : 9 8 7 6
  283. The permutated range is : 9 8 7 6
  284. Elements at even indices in the permutation : 9 7
  285. Permutation backwards : 6 7 8 9
  286. Iterate backward with stride 2 : 6 8
  287. </pre>
  288. <p>The source code for this example can be found <a class="reference external" href="../example/permutation_iter_example.cpp">here</a>.</p>
  289. </div>
  290. </div>
  291. <div class="footer">
  292. <hr class="footer" />
  293. <a class="reference external" href="permutation_iterator.rst">View document source</a>.
  294. Generated by <a class="reference external" href="http://docutils.sourceforge.net/">Docutils</a> from <a class="reference external" href="http://docutils.sourceforge.net/rst.html">reStructuredText</a> source.
  295. </div>
  296. </body>
  297. </html>