zip_iterator.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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>Zip Iterator</title>
  8. <meta name="author" content="David Abrahams, Thomas Becker" />
  9. <meta name="organization" content="Boost Consulting, Zephyr Associates, Inc." />
  10. <meta name="date" content="2006-09-11" />
  11. <meta name="copyright" content="Copyright David Abrahams and Thomas Becker 2003." />
  12. <link rel="stylesheet" href="../../../rst.css" type="text/css" />
  13. </head>
  14. <body>
  15. <div class="document" id="zip-iterator">
  16. <h1 class="title">Zip 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>David Abrahams, Thomas Becker</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:thomas&#64;styleadvisor.com">thomas&#64;styleadvisor.com</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>, <a class="last reference external" href="http://www.styleadvisor.com">Zephyr Associates, Inc.</a></td></tr>
  27. <tr><th class="docinfo-name">Date:</th>
  28. <td>2006-09-11</td></tr>
  29. <tr><th class="docinfo-name">Copyright:</th>
  30. <td>Copyright David Abrahams and Thomas Becker 2003.</td></tr>
  31. </tbody>
  32. </table>
  33. <!-- Distributed under the Boost -->
  34. <!-- Software License, Version 1.0. (See accompanying -->
  35. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  36. <table class="docutils field-list" frame="void" rules="none">
  37. <col class="field-name" />
  38. <col class="field-body" />
  39. <tbody valign="top">
  40. <tr class="field"><th class="field-name">abstract:</th><td class="field-body"><!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  41. <!-- Software License, Version 1.0. (See accompanying -->
  42. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  43. The zip iterator provides the ability to parallel-iterate
  44. over several controlled sequences simultaneously. A zip
  45. iterator is constructed from a tuple of iterators. Moving
  46. the zip iterator moves all the iterators in parallel.
  47. Dereferencing the zip iterator returns a tuple that contains
  48. the results of dereferencing the individual iterators.</td>
  49. </tr>
  50. </tbody>
  51. </table>
  52. <div class="contents topic" id="table-of-contents">
  53. <p class="topic-title first">Table of Contents</p>
  54. <ul class="simple">
  55. <li><a class="reference internal" href="#zip-iterator-synopsis" id="id1"><tt class="docutils literal"><span class="pre">zip_iterator</span></tt> synopsis</a></li>
  56. <li><a class="reference internal" href="#zip-iterator-requirements" id="id2"><tt class="docutils literal"><span class="pre">zip_iterator</span></tt> requirements</a></li>
  57. <li><a class="reference internal" href="#zip-iterator-models" id="id3"><tt class="docutils literal"><span class="pre">zip_iterator</span></tt> models</a></li>
  58. <li><a class="reference internal" href="#zip-iterator-operations" id="id4"><tt class="docutils literal"><span class="pre">zip_iterator</span></tt> operations</a></li>
  59. <li><a class="reference internal" href="#examples" id="id5">Examples</a></li>
  60. </ul>
  61. </div>
  62. <div class="section" id="zip-iterator-synopsis">
  63. <h1><a class="toc-backref" href="#id1"><tt class="docutils literal"><span class="pre">zip_iterator</span></tt> synopsis</a></h1>
  64. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  65. <!-- Software License, Version 1.0. (See accompanying -->
  66. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  67. <pre class="literal-block">
  68. template&lt;typename IteratorTuple&gt;
  69. class zip_iterator
  70. {
  71. public:
  72. typedef /* see below */ reference;
  73. typedef reference value_type;
  74. typedef value_type* pointer;
  75. typedef /* see below */ difference_type;
  76. typedef /* see below */ iterator_category;
  77. zip_iterator();
  78. zip_iterator(IteratorTuple iterator_tuple);
  79. template&lt;typename OtherIteratorTuple&gt;
  80. zip_iterator(
  81. const zip_iterator&lt;OtherIteratorTuple&gt;&amp; other
  82. , typename enable_if_convertible&lt;
  83. OtherIteratorTuple
  84. , IteratorTuple&gt;::type* = 0 // exposition only
  85. );
  86. const IteratorTuple&amp; get_iterator_tuple() const;
  87. private:
  88. IteratorTuple m_iterator_tuple; // exposition only
  89. };
  90. template&lt;typename IteratorTuple&gt;
  91. zip_iterator&lt;IteratorTuple&gt;
  92. make_zip_iterator(IteratorTuple t);
  93. </pre>
  94. <p>The <tt class="docutils literal"><span class="pre">reference</span></tt> member of <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> is the type of the tuple
  95. made of the reference types of the iterator types in the <tt class="docutils literal"><span class="pre">IteratorTuple</span></tt>
  96. argument.</p>
  97. <p>The <tt class="docutils literal"><span class="pre">difference_type</span></tt> member of <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> is the <tt class="docutils literal"><span class="pre">difference_type</span></tt>
  98. of the first of the iterator types in the <tt class="docutils literal"><span class="pre">IteratorTuple</span></tt> argument.</p>
  99. <p>The <tt class="docutils literal"><span class="pre">iterator_category</span></tt> member of <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> is convertible to the
  100. minimum of the traversal categories of the iterator types in the <tt class="docutils literal"><span class="pre">IteratorTuple</span></tt>
  101. argument. For example, if the <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> holds only vector
  102. iterators, then <tt class="docutils literal"><span class="pre">iterator_category</span></tt> is convertible to
  103. <tt class="docutils literal"><span class="pre">boost::random_access_traversal_tag</span></tt>. If you add a list iterator, then
  104. <tt class="docutils literal"><span class="pre">iterator_category</span></tt> will be convertible to <tt class="docutils literal"><span class="pre">boost::bidirectional_traversal_tag</span></tt>,
  105. but no longer to <tt class="docutils literal"><span class="pre">boost::random_access_traversal_tag</span></tt>.</p>
  106. </div>
  107. <div class="section" id="zip-iterator-requirements">
  108. <h1><a class="toc-backref" href="#id2"><tt class="docutils literal"><span class="pre">zip_iterator</span></tt> requirements</a></h1>
  109. <p>All iterator types in the argument <tt class="docutils literal"><span class="pre">IteratorTuple</span></tt> shall model Readable Iterator.</p>
  110. </div>
  111. <div class="section" id="zip-iterator-models">
  112. <h1><a class="toc-backref" href="#id3"><tt class="docutils literal"><span class="pre">zip_iterator</span></tt> models</a></h1>
  113. <p>The resulting <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> models Readable Iterator.</p>
  114. <p>The fact that the <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> models only Readable Iterator does not
  115. prevent you from modifying the values that the individual iterators point
  116. to. The tuple returned by the <tt class="docutils literal"><span class="pre">zip_iterator</span></tt>'s <tt class="docutils literal"><span class="pre">operator*</span></tt> is a tuple
  117. constructed from the reference types of the individual iterators, not
  118. their value types. For example, if <tt class="docutils literal"><span class="pre">zip_it</span></tt> is a <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> whose
  119. first member iterator is an <tt class="docutils literal"><span class="pre">std::vector&lt;double&gt;::iterator</span></tt>, then the
  120. following line will modify the value which the first member iterator of
  121. <tt class="docutils literal"><span class="pre">zip_it</span></tt> currently points to:</p>
  122. <pre class="literal-block">
  123. zip_it-&gt;get&lt;0&gt;() = 42.0;
  124. </pre>
  125. <p>Consider the set of standard traversal concepts obtained by taking
  126. the most refined standard traversal concept modeled by each individual
  127. iterator type in the <tt class="docutils literal"><span class="pre">IteratorTuple</span></tt> argument.The <tt class="docutils literal"><span class="pre">zip_iterator</span></tt>
  128. models the least refined standard traversal concept in this set.</p>
  129. <p><tt class="docutils literal"><span class="pre">zip_iterator&lt;IteratorTuple1&gt;</span></tt> is interoperable with
  130. <tt class="docutils literal"><span class="pre">zip_iterator&lt;IteratorTuple2&gt;</span></tt> if and only if <tt class="docutils literal"><span class="pre">IteratorTuple1</span></tt>
  131. is interoperable with <tt class="docutils literal"><span class="pre">IteratorTuple2</span></tt>.</p>
  132. </div>
  133. <div class="section" id="zip-iterator-operations">
  134. <h1><a class="toc-backref" href="#id4"><tt class="docutils literal"><span class="pre">zip_iterator</span></tt> operations</a></h1>
  135. <p>In addition to the operations required by the concepts modeled by
  136. <tt class="docutils literal"><span class="pre">zip_iterator</span></tt>, <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> provides the following
  137. operations.</p>
  138. <p><tt class="docutils literal"><span class="pre">zip_iterator();</span></tt></p>
  139. <table class="docutils field-list" frame="void" rules="none">
  140. <col class="field-name" />
  141. <col class="field-body" />
  142. <tbody valign="top">
  143. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">An instance of <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> with <tt class="docutils literal"><span class="pre">m_iterator_tuple</span></tt>
  144. default constructed.</td>
  145. </tr>
  146. </tbody>
  147. </table>
  148. <p><tt class="docutils literal"><span class="pre">zip_iterator(IteratorTuple</span> <span class="pre">iterator_tuple);</span></tt></p>
  149. <table class="docutils field-list" frame="void" rules="none">
  150. <col class="field-name" />
  151. <col class="field-body" />
  152. <tbody valign="top">
  153. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">An instance of <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> with <tt class="docutils literal"><span class="pre">m_iterator_tuple</span></tt>
  154. initialized to <tt class="docutils literal"><span class="pre">iterator_tuple</span></tt>.</td>
  155. </tr>
  156. </tbody>
  157. </table>
  158. <pre class="literal-block">
  159. template&lt;typename OtherIteratorTuple&gt;
  160. zip_iterator(
  161. const zip_iterator&lt;OtherIteratorTuple&gt;&amp; other
  162. , typename enable_if_convertible&lt;
  163. OtherIteratorTuple
  164. , IteratorTuple&gt;::type* = 0 // exposition only
  165. );
  166. </pre>
  167. <table class="docutils field-list" frame="void" rules="none">
  168. <col class="field-name" />
  169. <col class="field-body" />
  170. <tbody valign="top">
  171. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">An instance of <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> that is a copy of <tt class="docutils literal"><span class="pre">other</span></tt>.</td>
  172. </tr>
  173. <tr class="field"><th class="field-name">Requires:</th><td class="field-body"><tt class="docutils literal"><span class="pre">OtherIteratorTuple</span></tt> is implicitly convertible to <tt class="docutils literal"><span class="pre">IteratorTuple</span></tt>.</td>
  174. </tr>
  175. </tbody>
  176. </table>
  177. <p><tt class="docutils literal"><span class="pre">const</span> <span class="pre">IteratorTuple&amp;</span> <span class="pre">get_iterator_tuple()</span> <span class="pre">const;</span></tt></p>
  178. <table class="docutils field-list" frame="void" rules="none">
  179. <col class="field-name" />
  180. <col class="field-body" />
  181. <tbody valign="top">
  182. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">m_iterator_tuple</span></tt></td>
  183. </tr>
  184. </tbody>
  185. </table>
  186. <p><tt class="docutils literal"><span class="pre">reference</span> <span class="pre">operator*()</span> <span class="pre">const;</span></tt></p>
  187. <table class="docutils field-list" frame="void" rules="none">
  188. <col class="field-name" />
  189. <col class="field-body" />
  190. <tbody valign="top">
  191. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">A tuple consisting of the results of dereferencing all iterators in
  192. <tt class="docutils literal"><span class="pre">m_iterator_tuple</span></tt>.</td>
  193. </tr>
  194. </tbody>
  195. </table>
  196. <p><tt class="docutils literal"><span class="pre">zip_iterator&amp;</span> <span class="pre">operator++();</span></tt></p>
  197. <table class="docutils field-list" frame="void" rules="none">
  198. <col class="field-name" />
  199. <col class="field-body" />
  200. <tbody valign="top">
  201. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Increments each iterator in <tt class="docutils literal"><span class="pre">m_iterator_tuple</span></tt>.</td>
  202. </tr>
  203. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*this</span></tt></td>
  204. </tr>
  205. </tbody>
  206. </table>
  207. <p><tt class="docutils literal"><span class="pre">zip_iterator&amp;</span> <span class="pre">operator--();</span></tt></p>
  208. <table class="docutils field-list" frame="void" rules="none">
  209. <col class="field-name" />
  210. <col class="field-body" />
  211. <tbody valign="top">
  212. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Decrements each iterator in <tt class="docutils literal"><span class="pre">m_iterator_tuple</span></tt>.</td>
  213. </tr>
  214. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*this</span></tt></td>
  215. </tr>
  216. </tbody>
  217. </table>
  218. <pre class="literal-block">
  219. template&lt;typename IteratorTuple&gt;
  220. zip_iterator&lt;IteratorTuple&gt;
  221. make_zip_iterator(IteratorTuple t);
  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">An instance of <tt class="docutils literal"><span class="pre">zip_iterator&lt;IteratorTuple&gt;</span></tt> with <tt class="docutils literal"><span class="pre">m_iterator_tuple</span></tt>
  228. initialized to <tt class="docutils literal"><span class="pre">t</span></tt>.</td>
  229. </tr>
  230. </tbody>
  231. </table>
  232. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  233. <!-- Software License, Version 1.0. (See accompanying -->
  234. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  235. <pre class="literal-block">
  236. template&lt;typename IteratorTuple&gt;
  237. zip_iterator&lt;IteratorTuple&gt;
  238. make_zip_iterator(IteratorTuple t);
  239. </pre>
  240. <table class="docutils field-list" frame="void" rules="none">
  241. <col class="field-name" />
  242. <col class="field-body" />
  243. <tbody valign="top">
  244. <tr class="field"><th class="field-name">Returns:</th><td class="field-body">An instance of <tt class="docutils literal"><span class="pre">zip_iterator&lt;IteratorTuple&gt;</span></tt> with <tt class="docutils literal"><span class="pre">m_iterator_tuple</span></tt>
  245. initialized to <tt class="docutils literal"><span class="pre">t</span></tt>.</td>
  246. </tr>
  247. </tbody>
  248. </table>
  249. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  250. <!-- Software License, Version 1.0. (See accompanying -->
  251. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  252. </div>
  253. <div class="section" id="examples">
  254. <h1><a class="toc-backref" href="#id5">Examples</a></h1>
  255. <p>There are two main types of applications of the <tt class="docutils literal"><span class="pre">zip_iterator</span></tt>. The first
  256. one concerns runtime efficiency: If one has several controlled sequences
  257. of the same length that must be somehow processed, e.g., with the
  258. <tt class="docutils literal"><span class="pre">for_each</span></tt> algorithm, then it is more efficient to perform just
  259. one parallel-iteration rather than several individual iterations. For an
  260. example, assume that <tt class="docutils literal"><span class="pre">vect_of_doubles</span></tt> and <tt class="docutils literal"><span class="pre">vect_of_ints</span></tt>
  261. are two vectors of equal length containing doubles and ints, respectively,
  262. and consider the following two iterations:</p>
  263. <pre class="literal-block">
  264. std::vector&lt;double&gt;::const_iterator beg1 = vect_of_doubles.begin();
  265. std::vector&lt;double&gt;::const_iterator end1 = vect_of_doubles.end();
  266. std::vector&lt;int&gt;::const_iterator beg2 = vect_of_ints.begin();
  267. std::vector&lt;int&gt;::const_iterator end2 = vect_of_ints.end();
  268. std::for_each(beg1, end1, func_0());
  269. std::for_each(beg2, end2, func_1());
  270. </pre>
  271. <p>These two iterations can now be replaced with a single one as follows:</p>
  272. <pre class="literal-block">
  273. std::for_each(
  274. boost::make_zip_iterator(
  275. boost::make_tuple(beg1, beg2)
  276. ),
  277. boost::make_zip_iterator(
  278. boost::make_tuple(end1, end2)
  279. ),
  280. zip_func()
  281. );
  282. </pre>
  283. <p>A non-generic implementation of <tt class="docutils literal"><span class="pre">zip_func</span></tt> could look as follows:</p>
  284. <pre class="literal-block">
  285. struct zip_func :
  286. public std::unary_function&lt;const boost::tuple&lt;const double&amp;, const int&amp;&gt;&amp;, void&gt;
  287. {
  288. void operator()(const boost::tuple&lt;const double&amp;, const int&amp;&gt;&amp; t) const
  289. {
  290. m_f0(t.get&lt;0&gt;());
  291. m_f1(t.get&lt;1&gt;());
  292. }
  293. private:
  294. func_0 m_f0;
  295. func_1 m_f1;
  296. };
  297. </pre>
  298. <p>The second important application of the <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> is as a building block
  299. to make combining iterators. A combining iterator is an iterator
  300. that parallel-iterates over several controlled sequences and, upon
  301. dereferencing, returns the result of applying a functor to the values of the
  302. sequences at the respective positions. This can now be achieved by using the
  303. <tt class="docutils literal"><span class="pre">zip_iterator</span></tt> in conjunction with the <tt class="docutils literal"><span class="pre">transform_iterator</span></tt>.</p>
  304. <p>Suppose, for example, that you have two vectors of doubles, say
  305. <tt class="docutils literal"><span class="pre">vect_1</span></tt> and <tt class="docutils literal"><span class="pre">vect_2</span></tt>, and you need to expose to a client
  306. a controlled sequence containing the products of the elements of
  307. <tt class="docutils literal"><span class="pre">vect_1</span></tt> and <tt class="docutils literal"><span class="pre">vect_2</span></tt>. Rather than placing these products
  308. in a third vector, you can use a combining iterator that calculates the
  309. products on the fly. Let us assume that <tt class="docutils literal"><span class="pre">tuple_multiplies</span></tt> is a
  310. functor that works like <tt class="docutils literal"><span class="pre">std::multiplies</span></tt>, except that it takes
  311. its two arguments packaged in a tuple. Then the two iterators
  312. <tt class="docutils literal"><span class="pre">it_begin</span></tt> and <tt class="docutils literal"><span class="pre">it_end</span></tt> defined below delimit a controlled
  313. sequence containing the products of the elements of <tt class="docutils literal"><span class="pre">vect_1</span></tt> and
  314. <tt class="docutils literal"><span class="pre">vect_2</span></tt>:</p>
  315. <pre class="literal-block">
  316. typedef boost::tuple&lt;
  317. std::vector&lt;double&gt;::const_iterator,
  318. std::vector&lt;double&gt;::const_iterator
  319. &gt; the_iterator_tuple;
  320. typedef boost::zip_iterator&lt;
  321. the_iterator_tuple
  322. &gt; the_zip_iterator;
  323. typedef boost::transform_iterator&lt;
  324. tuple_multiplies&lt;double&gt;,
  325. the_zip_iterator
  326. &gt; the_transform_iterator;
  327. the_transform_iterator it_begin(
  328. the_zip_iterator(
  329. the_iterator_tuple(
  330. vect_1.begin(),
  331. vect_2.begin()
  332. )
  333. ),
  334. tuple_multiplies&lt;double&gt;()
  335. );
  336. the_transform_iterator it_end(
  337. the_zip_iterator(
  338. the_iterator_tuple(
  339. vect_1.end(),
  340. vect_2.end()
  341. )
  342. ),
  343. tuple_multiplies&lt;double&gt;()
  344. );
  345. </pre>
  346. </div>
  347. </div>
  348. <div class="footer">
  349. <hr class="footer" />
  350. <a class="reference external" href="zip_iterator.rst">View document source</a>.
  351. 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.
  352. </div>
  353. </body>
  354. </html>