zip_iterator.qbk 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. [section:zip Zip Iterator]
  2. The zip iterator provides the ability to parallel-iterate
  3. over several controlled sequences simultaneously. A zip
  4. iterator is constructed from a tuple of iterators. Moving
  5. the zip iterator moves all the iterators in parallel.
  6. Dereferencing the zip iterator returns a tuple that contains
  7. the results of dereferencing the individual iterators.
  8. The tuple of iterators is now implemented in terms of a Boost fusion sequence.
  9. Because of this the 'tuple' may be any Boost fusion sequence and, for backwards
  10. compatibility through a Boost fusion sequence adapter, a Boost tuple. Because the
  11. 'tuple' may be any boost::fusion sequence the 'tuple' may also be any type for which a
  12. Boost fusion adapter exists. This includes, among others, a std::tuple and a std::pair.
  13. Just remember to include the appropriate Boost fusion adapter header files for these
  14. other Boost fusion adapters. The zip_iterator header file already includes the
  15. Boost fusion adapter header file for Boost tuple, so you need not include it yourself
  16. to use a Boost tuple as your 'tuple'.
  17. [section:zip_example Example]
  18. There are two main types of applications of the `zip_iterator`. The first
  19. one concerns runtime efficiency: If one has several controlled sequences
  20. of the same length that must be somehow processed, e.g., with the
  21. `for_each` algorithm, then it is more efficient to perform just
  22. one parallel-iteration rather than several individual iterations. For an
  23. example, assume that `vect_of_doubles` and `vect_of_ints`
  24. are two vectors of equal length containing doubles and ints, respectively,
  25. and consider the following two iterations:
  26. std::vector<double>::const_iterator beg1 = vect_of_doubles.begin();
  27. std::vector<double>::const_iterator end1 = vect_of_doubles.end();
  28. std::vector<int>::const_iterator beg2 = vect_of_ints.begin();
  29. std::vector<int>::const_iterator end2 = vect_of_ints.end();
  30. std::for_each(beg1, end1, func_0());
  31. std::for_each(beg2, end2, func_1());
  32. These two iterations can now be replaced with a single one as follows:
  33. std::for_each(
  34. boost::make_zip_iterator(
  35. boost::make_tuple(beg1, beg2)
  36. ),
  37. boost::make_zip_iterator(
  38. boost::make_tuple(end1, end2)
  39. ),
  40. zip_func()
  41. );
  42. A non-generic implementation of `zip_func` could look as follows:
  43. struct zip_func :
  44. public std::unary_function<const boost::tuple<const double&, const int&>&, void>
  45. {
  46. void operator()(const boost::tuple<const double&, const int&>& t) const
  47. {
  48. m_f0(t.get<0>());
  49. m_f1(t.get<1>());
  50. }
  51. private:
  52. func_0 m_f0;
  53. func_1 m_f1;
  54. };
  55. The second important application of the `zip_iterator` is as a building block
  56. to make combining iterators. A combining iterator is an iterator
  57. that parallel-iterates over several controlled sequences and, upon
  58. dereferencing, returns the result of applying a functor to the values of the
  59. sequences at the respective positions. This can now be achieved by using the
  60. `zip_iterator` in conjunction with the `transform_iterator`.
  61. Suppose, for example, that you have two vectors of doubles, say
  62. `vect_1` and `vect_2`, and you need to expose to a client
  63. a controlled sequence containing the products of the elements of
  64. `vect_1` and `vect_2`. Rather than placing these products
  65. in a third vector, you can use a combining iterator that calculates the
  66. products on the fly. Let us assume that `tuple_multiplies` is a
  67. functor that works like `std::multiplies`, except that it takes
  68. its two arguments packaged in a tuple. Then the two iterators
  69. `it_begin` and `it_end` defined below delimit a controlled
  70. sequence containing the products of the elements of `vect_1` and
  71. `vect_2`:
  72. typedef boost::tuple<
  73. std::vector<double>::const_iterator,
  74. std::vector<double>::const_iterator
  75. > the_iterator_tuple;
  76. typedef boost::zip_iterator<
  77. the_iterator_tuple
  78. > the_zip_iterator;
  79. typedef boost::transform_iterator<
  80. tuple_multiplies<double>,
  81. the_zip_iterator
  82. > the_transform_iterator;
  83. the_transform_iterator it_begin(
  84. the_zip_iterator(
  85. the_iterator_tuple(
  86. vect_1.begin(),
  87. vect_2.begin()
  88. )
  89. ),
  90. tuple_multiplies<double>()
  91. );
  92. the_transform_iterator it_end(
  93. the_zip_iterator(
  94. the_iterator_tuple(
  95. vect_1.end(),
  96. vect_2.end()
  97. )
  98. ),
  99. tuple_multiplies<double>()
  100. );
  101. [endsect]
  102. [section:zip_reference Reference]
  103. [h2 Synopsis]
  104. template<typename IteratorTuple>
  105. class zip_iterator
  106. {
  107. public:
  108. typedef /* see below */ reference;
  109. typedef reference value_type;
  110. typedef value_type* pointer;
  111. typedef /* see below */ difference_type;
  112. typedef /* see below */ iterator_category;
  113. zip_iterator();
  114. zip_iterator(IteratorTuple iterator_tuple);
  115. template<typename OtherIteratorTuple>
  116. zip_iterator(
  117. const zip_iterator<OtherIteratorTuple>& other
  118. , typename enable_if_convertible<
  119. OtherIteratorTuple
  120. , IteratorTuple>::type* = 0 // exposition only
  121. );
  122. const IteratorTuple& get_iterator_tuple() const;
  123. private:
  124. IteratorTuple m_iterator_tuple; // exposition only
  125. };
  126. template<typename IteratorTuple>
  127. zip_iterator<IteratorTuple>
  128. make_zip_iterator(IteratorTuple t);
  129. The `reference` member of `zip_iterator` is the type of the tuple
  130. made of the reference types of the iterator types in the `IteratorTuple`
  131. argument.
  132. The `difference_type` member of `zip_iterator` is the `difference_type`
  133. of the first of the iterator types in the `IteratorTuple` argument.
  134. The `iterator_category` member of `zip_iterator` is convertible to the
  135. minimum of the traversal categories of the iterator types in the `IteratorTuple`
  136. argument. For example, if the `zip_iterator` holds only vector
  137. iterators, then `iterator_category` is convertible to
  138. `boost::random_access_traversal_tag`. If you add a list iterator, then
  139. `iterator_category` will be convertible to `boost::bidirectional_traversal_tag`,
  140. but no longer to `boost::random_access_traversal_tag`.
  141. [h2 Requirements]
  142. All iterator types in the argument `IteratorTuple` shall model Readable Iterator.
  143. [h2 Concepts]
  144. The resulting `zip_iterator` models Readable Iterator.
  145. The fact that the `zip_iterator` models only Readable Iterator does not
  146. prevent you from modifying the values that the individual iterators point
  147. to. The tuple returned by the `zip_iterator`'s `operator*` is a tuple
  148. constructed from the reference types of the individual iterators, not
  149. their value types. For example, if `zip_it` is a `zip_iterator` whose
  150. first member iterator is an `std::vector<double>::iterator`, then the
  151. following line will modify the value which the first member iterator of
  152. `zip_it` currently points to:
  153. zip_it->get<0>() = 42.0;
  154. Consider the set of standard traversal concepts obtained by taking
  155. the most refined standard traversal concept modeled by each individual
  156. iterator type in the `IteratorTuple` argument.The `zip_iterator`
  157. models the least refined standard traversal concept in this set.
  158. `zip_iterator<IteratorTuple1>` is interoperable with
  159. `zip_iterator<IteratorTuple2>` if and only if `IteratorTuple1`
  160. is interoperable with `IteratorTuple2`.
  161. [h2 Operations]
  162. In addition to the operations required by the concepts modeled by
  163. `zip_iterator`, `zip_iterator` provides the following
  164. operations.
  165. zip_iterator();
  166. [*Returns:] An instance of `zip_iterator` with `m_iterator_tuple`
  167. default constructed.
  168. zip_iterator(IteratorTuple iterator_tuple);
  169. [*Returns:] An instance of `zip_iterator` with `m_iterator_tuple`
  170. initialized to `iterator_tuple`.
  171. template<typename OtherIteratorTuple>
  172. zip_iterator(
  173. const zip_iterator<OtherIteratorTuple>& other
  174. , typename enable_if_convertible<
  175. OtherIteratorTuple
  176. , IteratorTuple>::type* = 0 // exposition only
  177. );
  178. [*Returns:] An instance of `zip_iterator` that is a copy of `other`.[br]
  179. [*Requires:] `OtherIteratorTuple` is implicitly convertible to `IteratorTuple`.
  180. const IteratorTuple& get_iterator_tuple() const;
  181. [*Returns:] `m_iterator_tuple`
  182. reference operator*() const;
  183. [*Returns:] A tuple consisting of the results of dereferencing all iterators in
  184. `m_iterator_tuple`.
  185. zip_iterator& operator++();
  186. [*Effects:] Increments each iterator in `m_iterator_tuple`.[br]
  187. [*Returns:] `*this`
  188. zip_iterator& operator--();
  189. [*Effects:] Decrements each iterator in `m_iterator_tuple`.[br]
  190. [*Returns:] `*this`
  191. template<typename IteratorTuple>
  192. zip_iterator<IteratorTuple>
  193. make_zip_iterator(IteratorTuple t);
  194. [*Returns:] An instance of `zip_iterator<IteratorTuple>` with `m_iterator_tuple`
  195. initialized to `t`.
  196. [endsect]
  197. [endsect]