indirect_iterator.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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>Indirect Iterator</title>
  8. <meta name="author" content="David Abrahams, Jeremy Siek, Thomas Witt" />
  9. <meta name="organization" content="Boost Consulting, Indiana University Open Systems Lab, University of Hanover Institute for Transport Railway Operation and Construction" />
  10. <meta name="date" content="2006-09-11" />
  11. <meta name="copyright" content="Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003." />
  12. <link rel="stylesheet" href="../../../rst.css" type="text/css" />
  13. </head>
  14. <body>
  15. <div class="document" id="indirect-iterator">
  16. <h1 class="title">Indirect 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, Jeremy Siek, Thomas Witt</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="reference external" href="mailto:jsiek&#64;osl.iu.edu">jsiek&#64;osl.iu.edu</a>, <a class="last reference external" href="mailto:witt&#64;ive.uni-hannover.de">witt&#64;ive.uni-hannover.de</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="reference external" href="http://www.osl.iu.edu">Open Systems
  27. Lab</a>, University of Hanover <a class="last reference external" href="http://www.ive.uni-hannover.de">Institute for Transport
  28. Railway Operation and Construction</a></td></tr>
  29. <tr><th class="docinfo-name">Date:</th>
  30. <td>2006-09-11</td></tr>
  31. <tr><th class="docinfo-name">Copyright:</th>
  32. <td>Copyright David Abrahams, Jeremy Siek, and Thomas Witt 2003.</td></tr>
  33. </tbody>
  34. </table>
  35. <!-- Distributed under the Boost -->
  36. <!-- Software License, Version 1.0. (See accompanying -->
  37. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  38. <table class="docutils field-list" frame="void" rules="none">
  39. <col class="field-name" />
  40. <col class="field-body" />
  41. <tbody valign="top">
  42. <tr class="field"><th class="field-name">abstract:</th><td class="field-body"><!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  43. <!-- Software License, Version 1.0. (See accompanying -->
  44. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  45. <tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> adapts an iterator by applying an
  46. <em>extra</em> dereference inside of <tt class="docutils literal"><span class="pre">operator*()</span></tt>. For example, this
  47. iterator adaptor makes it possible to view a container of pointers
  48. (e.g. <tt class="docutils literal"><span class="pre">list&lt;foo*&gt;</span></tt>) as if it were a container of the pointed-to type
  49. (e.g. <tt class="docutils literal"><span class="pre">list&lt;foo&gt;</span></tt>). <tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> depends on two
  50. auxiliary traits, <tt class="docutils literal"><span class="pre">pointee</span></tt> and <tt class="docutils literal"><span class="pre">indirect_reference</span></tt>, to
  51. provide support for underlying iterators whose <tt class="docutils literal"><span class="pre">value_type</span></tt> is
  52. not an iterator.</td>
  53. </tr>
  54. </tbody>
  55. </table>
  56. <div class="contents topic" id="table-of-contents">
  57. <p class="topic-title first">Table of Contents</p>
  58. <ul class="simple">
  59. <li><a class="reference internal" href="#indirect-iterator-synopsis" id="id2"><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> synopsis</a></li>
  60. <li><a class="reference internal" href="#indirect-iterator-requirements" id="id3"><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> requirements</a></li>
  61. <li><a class="reference internal" href="#indirect-iterator-models" id="id4"><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> models</a></li>
  62. <li><a class="reference internal" href="#indirect-iterator-operations" id="id5"><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> operations</a></li>
  63. <li><a class="reference internal" href="#example" id="id6">Example</a></li>
  64. </ul>
  65. </div>
  66. <div class="section" id="indirect-iterator-synopsis">
  67. <h1><a class="toc-backref" href="#id2"><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> synopsis</a></h1>
  68. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  69. <!-- Software License, Version 1.0. (See accompanying -->
  70. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  71. <pre class="literal-block">
  72. template &lt;
  73. class Iterator
  74. , class Value = use_default
  75. , class CategoryOrTraversal = use_default
  76. , class Reference = use_default
  77. , class Difference = use_default
  78. &gt;
  79. class indirect_iterator
  80. {
  81. public:
  82. typedef /* see below */ value_type;
  83. typedef /* see below */ reference;
  84. typedef /* see below */ pointer;
  85. typedef /* see below */ difference_type;
  86. typedef /* see below */ iterator_category;
  87. indirect_iterator();
  88. indirect_iterator(Iterator x);
  89. template &lt;
  90. class Iterator2, class Value2, class Category2
  91. , class Reference2, class Difference2
  92. &gt;
  93. indirect_iterator(
  94. indirect_iterator&lt;
  95. Iterator2, Value2, Category2, Reference2, Difference2
  96. &gt; const&amp; y
  97. , typename enable_if_convertible&lt;Iterator2, Iterator&gt;::type* = 0 // exposition
  98. );
  99. Iterator const&amp; base() const;
  100. reference operator*() const;
  101. indirect_iterator&amp; operator++();
  102. indirect_iterator&amp; operator--();
  103. private:
  104. Iterator m_iterator; // exposition
  105. };
  106. </pre>
  107. <p>The member types of <tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> are defined according to
  108. the following pseudo-code, where <tt class="docutils literal"><span class="pre">V</span></tt> is
  109. <tt class="docutils literal"><span class="pre">iterator_traits&lt;Iterator&gt;::value_type</span></tt></p>
  110. <pre class="literal-block">
  111. if (Value is use_default) then
  112. typedef remove_const&lt;pointee&lt;V&gt;::type&gt;::type value_type;
  113. else
  114. typedef remove_const&lt;Value&gt;::type value_type;
  115. if (Reference is use_default) then
  116. if (Value is use_default) then
  117. typedef indirect_reference&lt;V&gt;::type reference;
  118. else
  119. typedef Value&amp; reference;
  120. else
  121. typedef Reference reference;
  122. if (Value is use_default) then
  123. typedef pointee&lt;V&gt;::type* pointer;
  124. else
  125. typedef Value* pointer;
  126. if (Difference is use_default)
  127. typedef iterator_traits&lt;Iterator&gt;::difference_type difference_type;
  128. else
  129. typedef Difference difference_type;
  130. if (CategoryOrTraversal is use_default)
  131. typedef <em>iterator-category</em> (
  132. iterator_traversal&lt;Iterator&gt;::type,``reference``,``value_type``
  133. ) iterator_category;
  134. else
  135. typedef <em>iterator-category</em> (
  136. CategoryOrTraversal,``reference``,``value_type``
  137. ) iterator_category;
  138. </pre>
  139. </div>
  140. <div class="section" id="indirect-iterator-requirements">
  141. <h1><a class="toc-backref" href="#id3"><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> requirements</a></h1>
  142. <p>The expression <tt class="docutils literal"><span class="pre">*v</span></tt>, where <tt class="docutils literal"><span class="pre">v</span></tt> is an object of
  143. <tt class="docutils literal"><span class="pre">iterator_traits&lt;Iterator&gt;::value_type</span></tt>, shall be valid
  144. expression and convertible to <tt class="docutils literal"><span class="pre">reference</span></tt>. <tt class="docutils literal"><span class="pre">Iterator</span></tt> shall
  145. model the traversal concept indicated by <tt class="docutils literal"><span class="pre">iterator_category</span></tt>.
  146. <tt class="docutils literal"><span class="pre">Value</span></tt>, <tt class="docutils literal"><span class="pre">Reference</span></tt>, and <tt class="docutils literal"><span class="pre">Difference</span></tt> shall be chosen so
  147. that <tt class="docutils literal"><span class="pre">value_type</span></tt>, <tt class="docutils literal"><span class="pre">reference</span></tt>, and <tt class="docutils literal"><span class="pre">difference_type</span></tt> meet
  148. the requirements indicated by <tt class="docutils literal"><span class="pre">iterator_category</span></tt>.</p>
  149. <p>[Note: there are further requirements on the
  150. <tt class="docutils literal"><span class="pre">iterator_traits&lt;Iterator&gt;::value_type</span></tt> if the <tt class="docutils literal"><span class="pre">Value</span></tt>
  151. parameter is not <tt class="docutils literal"><span class="pre">use_default</span></tt>, as implied by the algorithm for
  152. deducing the default for the <tt class="docutils literal"><span class="pre">value_type</span></tt> member.]</p>
  153. </div>
  154. <div class="section" id="indirect-iterator-models">
  155. <h1><a class="toc-backref" href="#id4"><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> models</a></h1>
  156. <p>In addition to the concepts indicated by <tt class="docutils literal"><span class="pre">iterator_category</span></tt>
  157. and by <tt class="docutils literal"><span class="pre">iterator_traversal&lt;indirect_iterator&gt;::type</span></tt>, a
  158. specialization of <tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> models the following
  159. concepts, Where <tt class="docutils literal"><span class="pre">v</span></tt> is an object of
  160. <tt class="docutils literal"><span class="pre">iterator_traits&lt;Iterator&gt;::value_type</span></tt>:</p>
  161. <blockquote>
  162. <ul class="simple">
  163. <li>Readable Iterator if <tt class="docutils literal"><span class="pre">reference(*v)</span></tt> is convertible to
  164. <tt class="docutils literal"><span class="pre">value_type</span></tt>.</li>
  165. <li>Writable Iterator if <tt class="docutils literal"><span class="pre">reference(*v)</span> <span class="pre">=</span> <span class="pre">t</span></tt> is a valid
  166. expression (where <tt class="docutils literal"><span class="pre">t</span></tt> is an object of type
  167. <tt class="docutils literal"><span class="pre">indirect_iterator::value_type</span></tt>)</li>
  168. <li>Lvalue Iterator if <tt class="docutils literal"><span class="pre">reference</span></tt> is a reference type.</li>
  169. </ul>
  170. </blockquote>
  171. <p><tt class="docutils literal"><span class="pre">indirect_iterator&lt;X,V1,C1,R1,D1&gt;</span></tt> is interoperable with
  172. <tt class="docutils literal"><span class="pre">indirect_iterator&lt;Y,V2,C2,R2,D2&gt;</span></tt> if and only if <tt class="docutils literal"><span class="pre">X</span></tt> is
  173. interoperable with <tt class="docutils literal"><span class="pre">Y</span></tt>.</p>
  174. </div>
  175. <div class="section" id="indirect-iterator-operations">
  176. <h1><a class="toc-backref" href="#id5"><tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> operations</a></h1>
  177. <p>In addition to the operations required by the concepts described
  178. above, specializations of <tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> provide the
  179. following operations.</p>
  180. <p><tt class="docutils literal"><span class="pre">indirect_iterator();</span></tt></p>
  181. <table class="docutils field-list" frame="void" rules="none">
  182. <col class="field-name" />
  183. <col class="field-body" />
  184. <tbody valign="top">
  185. <tr class="field"><th class="field-name">Requires:</th><td class="field-body"><tt class="docutils literal"><span class="pre">Iterator</span></tt> must be Default Constructible.</td>
  186. </tr>
  187. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Constructs an instance of <tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> with
  188. a default-constructed <tt class="docutils literal"><span class="pre">m_iterator</span></tt>.</td>
  189. </tr>
  190. </tbody>
  191. </table>
  192. <p><tt class="docutils literal"><span class="pre">indirect_iterator(Iterator</span> <span class="pre">x);</span></tt></p>
  193. <table class="docutils field-list" frame="void" rules="none">
  194. <col class="field-name" />
  195. <col class="field-body" />
  196. <tbody valign="top">
  197. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Constructs an instance of <tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> with
  198. <tt class="docutils literal"><span class="pre">m_iterator</span></tt> copy constructed from <tt class="docutils literal"><span class="pre">x</span></tt>.</td>
  199. </tr>
  200. </tbody>
  201. </table>
  202. <pre class="literal-block">
  203. template &lt;
  204. class Iterator2, class Value2, unsigned Access, class Traversal
  205. , class Reference2, class Difference2
  206. &gt;
  207. indirect_iterator(
  208. indirect_iterator&lt;
  209. Iterator2, Value2, Access, Traversal, Reference2, Difference2
  210. &gt; const&amp; y
  211. , typename enable_if_convertible&lt;Iterator2, Iterator&gt;::type* = 0 // exposition
  212. );
  213. </pre>
  214. <table class="docutils field-list" frame="void" rules="none">
  215. <col class="field-name" />
  216. <col class="field-body" />
  217. <tbody valign="top">
  218. <tr class="field"><th class="field-name">Requires:</th><td class="field-body"><tt class="docutils literal"><span class="pre">Iterator2</span></tt> is implicitly convertible to <tt class="docutils literal"><span class="pre">Iterator</span></tt>.</td>
  219. </tr>
  220. <tr class="field"><th class="field-name">Effects:</th><td class="field-body">Constructs an instance of <tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> whose
  221. <tt class="docutils literal"><span class="pre">m_iterator</span></tt> subobject is constructed from <tt class="docutils literal"><span class="pre">y.base()</span></tt>.</td>
  222. </tr>
  223. </tbody>
  224. </table>
  225. <p><tt class="docutils literal"><span class="pre">Iterator</span> <span class="pre">const&amp;</span> <span class="pre">base()</span> <span class="pre">const;</span></tt></p>
  226. <table class="docutils field-list" frame="void" rules="none">
  227. <col class="field-name" />
  228. <col class="field-body" />
  229. <tbody valign="top">
  230. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">m_iterator</span></tt></td>
  231. </tr>
  232. </tbody>
  233. </table>
  234. <p><tt class="docutils literal"><span class="pre">reference</span> <span class="pre">operator*()</span> <span class="pre">const;</span></tt></p>
  235. <table class="docutils field-list" frame="void" rules="none">
  236. <col class="field-name" />
  237. <col class="field-body" />
  238. <tbody valign="top">
  239. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">**m_iterator</span></tt></td>
  240. </tr>
  241. </tbody>
  242. </table>
  243. <p><tt class="docutils literal"><span class="pre">indirect_iterator&amp;</span> <span class="pre">operator++();</span></tt></p>
  244. <table class="docutils field-list" frame="void" rules="none">
  245. <col class="field-name" />
  246. <col class="field-body" />
  247. <tbody valign="top">
  248. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><tt class="docutils literal"><span class="pre">++m_iterator</span></tt></td>
  249. </tr>
  250. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*this</span></tt></td>
  251. </tr>
  252. </tbody>
  253. </table>
  254. <p><tt class="docutils literal"><span class="pre">indirect_iterator&amp;</span> <span class="pre">operator--();</span></tt></p>
  255. <table class="docutils field-list" frame="void" rules="none">
  256. <col class="field-name" />
  257. <col class="field-body" />
  258. <tbody valign="top">
  259. <tr class="field"><th class="field-name">Effects:</th><td class="field-body"><tt class="docutils literal"><span class="pre">--m_iterator</span></tt></td>
  260. </tr>
  261. <tr class="field"><th class="field-name">Returns:</th><td class="field-body"><tt class="docutils literal"><span class="pre">*this</span></tt></td>
  262. </tr>
  263. </tbody>
  264. </table>
  265. <!-- Copyright David Abrahams 2006. Distributed under the Boost -->
  266. <!-- Software License, Version 1.0. (See accompanying -->
  267. <!-- file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -->
  268. </div>
  269. <div class="section" id="example">
  270. <h1><a class="toc-backref" href="#id6">Example</a></h1>
  271. <p>This example prints an array of characters, using
  272. <tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> to access the array of characters through an
  273. array of pointers. Next <tt class="docutils literal"><span class="pre">indirect_iterator</span></tt> is used with the
  274. <tt class="docutils literal"><span class="pre">transform</span></tt> algorithm to copy the characters (incremented by one) to
  275. another array. A constant indirect iterator is used for the source and
  276. a mutable indirect iterator is used for the destination. The last part
  277. of the example prints the original array of characters, but this time
  278. using the <tt class="docutils literal"><span class="pre">make_indirect_iterator</span></tt> helper function.</p>
  279. <pre class="literal-block">
  280. char characters[] = &quot;abcdefg&quot;;
  281. const int N = sizeof(characters)/sizeof(char) - 1; // -1 since characters has a null char
  282. char* pointers_to_chars[N]; // at the end.
  283. for (int i = 0; i &lt; N; ++i)
  284. pointers_to_chars[i] = &amp;characters[i];
  285. // Example of using indirect_iterator
  286. boost::indirect_iterator&lt;char**, char&gt;
  287. indirect_first(pointers_to_chars), indirect_last(pointers_to_chars + N);
  288. std::copy(indirect_first, indirect_last, std::ostream_iterator&lt;char&gt;(std::cout, &quot;,&quot;));
  289. std::cout &lt;&lt; std::endl;
  290. // Example of making mutable and constant indirect iterators
  291. char mutable_characters[N];
  292. char* pointers_to_mutable_chars[N];
  293. for (int j = 0; j &lt; N; ++j)
  294. pointers_to_mutable_chars[j] = &amp;mutable_characters[j];
  295. boost::indirect_iterator&lt;char* const*&gt; mutable_indirect_first(pointers_to_mutable_chars),
  296. mutable_indirect_last(pointers_to_mutable_chars + N);
  297. boost::indirect_iterator&lt;char* const*, char const&gt; const_indirect_first(pointers_to_chars),
  298. const_indirect_last(pointers_to_chars + N);
  299. std::transform(const_indirect_first, const_indirect_last,
  300. mutable_indirect_first, std::bind1st(std::plus&lt;char&gt;(), 1));
  301. std::copy(mutable_indirect_first, mutable_indirect_last,
  302. std::ostream_iterator&lt;char&gt;(std::cout, &quot;,&quot;));
  303. std::cout &lt;&lt; std::endl;
  304. // Example of using make_indirect_iterator()
  305. std::copy(boost::make_indirect_iterator(pointers_to_chars),
  306. boost::make_indirect_iterator(pointers_to_chars + N),
  307. std::ostream_iterator&lt;char&gt;(std::cout, &quot;,&quot;));
  308. std::cout &lt;&lt; std::endl;
  309. </pre>
  310. <p>The output is:</p>
  311. <pre class="literal-block">
  312. a,b,c,d,e,f,g,
  313. b,c,d,e,f,g,h,
  314. a,b,c,d,e,f,g,
  315. </pre>
  316. <p>The source code for this example can be found <a class="reference external" href="../example/indirect_iterator_example.cpp">here</a>.</p>
  317. </div>
  318. </div>
  319. <div class="footer">
  320. <hr class="footer" />
  321. <a class="reference external" href="indirect_iterator.rst">View document source</a>.
  322. 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.
  323. </div>
  324. </body>
  325. </html>