zip_iterator_eg.rst 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. .. Copyright David Abrahams 2006. Distributed under the Boost
  2. .. Software License, Version 1.0. (See accompanying
  3. .. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  4. Examples
  5. ........
  6. There are two main types of applications of the ``zip_iterator``. The first
  7. one concerns runtime efficiency: If one has several controlled sequences
  8. of the same length that must be somehow processed, e.g., with the
  9. ``for_each`` algorithm, then it is more efficient to perform just
  10. one parallel-iteration rather than several individual iterations. For an
  11. example, assume that ``vect_of_doubles`` and ``vect_of_ints``
  12. are two vectors of equal length containing doubles and ints, respectively,
  13. and consider the following two iterations:
  14. ::
  15. std::vector<double>::const_iterator beg1 = vect_of_doubles.begin();
  16. std::vector<double>::const_iterator end1 = vect_of_doubles.end();
  17. std::vector<int>::const_iterator beg2 = vect_of_ints.begin();
  18. std::vector<int>::const_iterator end2 = vect_of_ints.end();
  19. std::for_each(beg1, end1, func_0());
  20. std::for_each(beg2, end2, func_1());
  21. These two iterations can now be replaced with a single one as follows:
  22. ::
  23. std::for_each(
  24. boost::make_zip_iterator(
  25. boost::make_tuple(beg1, beg2)
  26. ),
  27. boost::make_zip_iterator(
  28. boost::make_tuple(end1, end2)
  29. ),
  30. zip_func()
  31. );
  32. A non-generic implementation of ``zip_func`` could look as follows:
  33. ::
  34. struct zip_func
  35. {
  36. void operator()(const boost::tuple<const double&, const int&>& t) const
  37. {
  38. m_f0(t.get<0>());
  39. m_f1(t.get<1>());
  40. }
  41. private:
  42. func_0 m_f0;
  43. func_1 m_f1;
  44. };
  45. The second important application of the ``zip_iterator`` is as a building block
  46. to make combining iterators. A combining iterator is an iterator
  47. that parallel-iterates over several controlled sequences and, upon
  48. dereferencing, returns the result of applying a functor to the values of the
  49. sequences at the respective positions. This can now be achieved by using the
  50. ``zip_iterator`` in conjunction with the ``transform_iterator``.
  51. Suppose, for example, that you have two vectors of doubles, say
  52. ``vect_1`` and ``vect_2``, and you need to expose to a client
  53. a controlled sequence containing the products of the elements of
  54. ``vect_1`` and ``vect_2``. Rather than placing these products
  55. in a third vector, you can use a combining iterator that calculates the
  56. products on the fly. Let us assume that ``tuple_multiplies`` is a
  57. functor that works like ``std::multiplies``, except that it takes
  58. its two arguments packaged in a tuple. Then the two iterators
  59. ``it_begin`` and ``it_end`` defined below delimit a controlled
  60. sequence containing the products of the elements of ``vect_1`` and
  61. ``vect_2``:
  62. ::
  63. typedef boost::tuple<
  64. std::vector<double>::const_iterator,
  65. std::vector<double>::const_iterator
  66. > the_iterator_tuple;
  67. typedef boost::zip_iterator<
  68. the_iterator_tuple
  69. > the_zip_iterator;
  70. typedef boost::transform_iterator<
  71. tuple_multiplies<double>,
  72. the_zip_iterator
  73. > the_transform_iterator;
  74. the_transform_iterator it_begin(
  75. the_zip_iterator(
  76. the_iterator_tuple(
  77. vect_1.begin(),
  78. vect_2.begin()
  79. )
  80. ),
  81. tuple_multiplies<double>()
  82. );
  83. the_transform_iterator it_end(
  84. the_zip_iterator(
  85. the_iterator_tuple(
  86. vect_1.end(),
  87. vect_2.end()
  88. )
  89. ),
  90. tuple_multiplies<double>()
  91. );