function_input_iterator.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.6: http://docutils.sourceforge.net/" />
  7. <title></title>
  8. <meta name="author" content="Dean Michael Berris" />
  9. <link rel="stylesheet" href="../../../rst.css" type="text/css" />
  10. </head>
  11. <body>
  12. <div class="document">
  13. <table class="docinfo" frame="void" rules="none">
  14. <col class="docinfo-name" />
  15. <col class="docinfo-content" />
  16. <tbody valign="top">
  17. <tr><th class="docinfo-name">Author:</th>
  18. <td><a class="first reference external" href="mailto:mikhailberis&#64;gmail.com">Dean Michael Berris</a></td></tr>
  19. <tr class="field"><th class="docinfo-name">License:</th><td class="field-body">Distributed under the Boost Software License, Version 1.0
  20. (See accompanying file LICENSE_1_0.txt or copy at <a class="reference external" href="http://www.boost.org/LICENSE_1_0.txt">http://www.boost.org/LICENSE_1_0.txt</a>)</td>
  21. </tr>
  22. </tbody>
  23. </table>
  24. <div class="section" id="function-input-iterator">
  25. <h1>Function Input Iterator</h1>
  26. <p>The Function Input Iterator allows for creating iterators that encapsulate
  27. a nullary function object and a state object which tracks the number of times
  28. the iterator has been incremented. A Function Input Iterator models the
  29. <a class="reference external" href="http://www.sgi.com/tech/stl/InputIterator.html">InputIterator</a> concept and is useful for creating bounded input iterators.</p>
  30. <p>Like the Generator Iterator, the Function Input Iterator takes a function
  31. that models the <a class="reference external" href="http://www.sgi.com/tech/stl/Generator.html">Generator</a> concept (which is basically a nullary or 0-arity
  32. function object). Each increment of the function Function Input Iterator
  33. invokes the generator function and stores the value in the iterator. When
  34. the iterator is dereferenced the stored value is returned.</p>
  35. <p>The Function Input Iterator encapsulates a state object which models the
  36. <a class="reference internal" href="#incrementable-concept">Incrementable Concept</a> and the <a class="reference external" href="http://www.sgi.com/tech/stl/EqualityComparable.html">EqualityComparable</a> Concept. These concepts are
  37. described below as:</p>
  38. <div class="section" id="incrementable-concept">
  39. <h2>Incrementable Concept</h2>
  40. <p>A type models the Incrementable Concept when it supports the pre- and post-
  41. increment operators. For a given object <tt class="docutils literal"><span class="pre">i</span></tt> with type <tt class="docutils literal"><span class="pre">I</span></tt>, the following
  42. constructs should be valid:</p>
  43. <table border="1" class="docutils">
  44. <colgroup>
  45. <col width="24%" />
  46. <col width="46%" />
  47. <col width="30%" />
  48. </colgroup>
  49. <tbody valign="top">
  50. <tr><td colspan="3">Construct Description Return Type</td>
  51. </tr>
  52. <tr><td>i++</td>
  53. <td>Post-increment i.</td>
  54. <td>I</td>
  55. </tr>
  56. <tr><td>++i</td>
  57. <td>Pre-increment i.</td>
  58. <td>I&amp;</td>
  59. </tr>
  60. </tbody>
  61. </table>
  62. <p>NOTE: An Incrementable type should also be <a class="reference external" href="http://www.sgi.com/tech/stl/DefaultConstructible.html">DefaultConstructible</a>.</p>
  63. </div>
  64. <div class="section" id="synopsis">
  65. <h2>Synopsis</h2>
  66. <pre class="literal-block">
  67. namespace {
  68. template &lt;class Function, class State&gt;
  69. class function_input_iterator;
  70. template &lt;class Function, class State&gt;
  71. typename function_input_iterator&lt;Function, State&gt;
  72. make_function_input_iterator(Function &amp; f);
  73. struct infinite;
  74. }
  75. </pre>
  76. </div>
  77. <div class="section" id="function-input-iterator-class">
  78. <h2>Function Input Iterator Class</h2>
  79. <p>The class Function Input Iterator class takes two template parameters
  80. <tt class="docutils literal"><span class="pre">Function</span></tt> and <tt class="docutils literal"><span class="pre">State</span></tt>. These two template parameters tell the
  81. Function Input Iterator the type of the function to encapsulate and
  82. the type of the internal state value to hold.</p>
  83. <p>The <tt class="docutils literal"><span class="pre">State</span></tt> parameter is important in cases where you want to
  84. control the type of the counter which determines whether two iterators
  85. are at the same state. This allows for creating a pair of iterators which
  86. bound the range of the invocations of the encapsulated functions.</p>
  87. </div>
  88. <div class="section" id="examples">
  89. <h2>Examples</h2>
  90. <p>The following example shows how we use the function input iterator class
  91. in cases where we want to create bounded (lazy) generated ranges.</p>
  92. <pre class="literal-block">
  93. struct generator {
  94. typedef int result_type;
  95. generator() { srand(time(0)); }
  96. result_type operator() () const {
  97. return rand();
  98. }
  99. };
  100. int main(int argc, char * argv[]) {
  101. generator f;
  102. copy(
  103. make_function_input_iterator(f, 0),
  104. make_function_input_iterator(f, 10),
  105. ostream_iterator&lt;int&gt;(cout, &quot; &quot;)
  106. );
  107. return 0;
  108. }
  109. </pre>
  110. <p>Here we can see that we've bounded the number of invocations using an <tt class="docutils literal"><span class="pre">int</span></tt>
  111. that counts from <tt class="docutils literal"><span class="pre">0</span></tt> to <tt class="docutils literal"><span class="pre">10</span></tt>. Say we want to create an endless stream
  112. of random numbers and encapsulate that in a pair of integers, we can do
  113. it with the <tt class="docutils literal"><span class="pre">boost::infinite</span></tt> helper class.</p>
  114. <pre class="literal-block">
  115. copy(
  116. make_function_input_iterator(f,infinite()),
  117. make_function_input_iterator(f,infinite()),
  118. ostream_iterator&lt;int&gt;(count, &quot; &quot;)
  119. );
  120. </pre>
  121. <p>Above, instead of creating a huge vector we rely on the STL copy algorithm
  122. to traverse the function input iterator and call the function object f
  123. as it increments the iterator. The special property of <tt class="docutils literal"><span class="pre">boost::infinite</span></tt>
  124. is that equating two instances always yield false -- and that incrementing
  125. an instance of <tt class="docutils literal"><span class="pre">boost::infinite</span></tt> doesn't do anything. This is an efficient
  126. way of stating that the iterator range provided by two iterators with an
  127. encapsulated infinite state will definitely be infinite.</p>
  128. </div>
  129. </div>
  130. </div>
  131. <div class="footer">
  132. <hr class="footer" />
  133. <a class="reference external" href="function_input_iterator.rst">View document source</a>.
  134. 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.
  135. </div>
  136. </body>
  137. </html>