Boost logo

Boost :

From: Leo Goodstadt (leo.goodstadt_at_[hidden])
Date: 2005-07-26 09:22:12


The zip iterator documentation says:
"The zip iterator provides the ability to parallel-iterate over several
controlled sequences simultaneously. ...
Moving the zip iterator moves all the iterators in parallel."

What is not specified is what happens when the several different
sequences are of unequal size.
At the moment, everything just blows up.

python zip, if I remember correctly, just stops at the end of the
shortest sequence in the tuple. (python map works differently).
This seems a sensible thing to do.

This requires just a modification to zip_iterator::equal.
I guess distance_to might need to be changed as well.

Leo Goodstadt

The diff is attached at the end.

Here is the example modified:
#include <boost/iterator/zip_iterator.hpp>
#include <boost/tuple/tuple.hpp>
#include <iostream>
#include <vector>

int main() {
    typedef std::vector<int> IntSeq;
  
  // a = {1,2,3}
  IntSeq a;
  a.push_back(1);
  a.push_back(2);
  a.push_back(3);

  // b = {10,20,30,40}
  IntSeq b;
  b.push_back(10);
  b.push_back(20);
  b.push_back(30);
  b.push_back(40);
  
  // b = {100,200,300,400,500}
  IntSeq c;
  c.push_back(100);
  c.push_back(200);
  c.push_back(300);
  c.push_back(400);
  c.push_back(500);

  

  typedef boost::zip_iterator<
    boost::tuples::tuple<
      IntSeq::const_iterator,
      IntSeq::const_iterator,
      IntSeq::const_iterator> > ZIt;

  
  ZIt cur(boost::make_tuple(a.begin(), b.begin(), c.begin()));
  ZIt end(boost::make_tuple(a.end(), b.end(), c.end()));

  for ( ; cur != end; ++cur ) {
    std::cout << "{ " << boost::tuples::get<0>(*cur) << ", "
                      << boost::tuples::get<1>(*cur) << ", "
                      << boost::tuples::get<2>(*cur) << " }" << std::endl;

  }

  
  return 0;
}

diff old_boost/iterator/zip_iterator.hpp new_boost/iterator/zip_iterator.hpp
329a330,347
>
> // Returns true if any tuples equal. NOTE: "==" for tuples currently (7/2003)
> // has problems under some compilers, so I just do my own.
> // No point in bringing in a bunch of #ifdefs here. This is
> // going to go away with the next tuple implementation anyway.
> //
> bool tuple_any_equal(tuples::null_type, tuples::null_type)
> { return false; }
>
> template<typename Tuple1, typename Tuple2>
> bool tuple_any_equal(
> Tuple1 const& t1,
> Tuple2 const& t2
> )
> {
> return t1.get_head() == t2.get_head() ||
> tuple_any_equal(t1.get_tail(), t2.get_tail());
> }
537c555
< return detail::tuple_impl_specific::tuple_equal(

---
>       return detail::tuple_impl_specific::tuple_any_equal(

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk