mirandaconrado/product-iterator

Working with empty container(s)?

misuo opened this issue · 0 comments

misuo commented

First, let me thank you the for this. Seems exactly what I'm looking for.

Have you considered what happens/should-happen if one of the containers are empty?

I get a crash when trying to work with that, e.g. as illustrated:

#include "product_iterator.hpp"
#include <iostream>
#include <vector>

using namespace std;

int main()
{
  vector<int> c1({1,2});
  vector<char> c2;         // NOTE THIS IS EMPTY!

  auto it = make_product_iterator(c1, c2);
  auto end = it.get_end();
  for(; it != end; ++it)
  {
    cout << "{" << it.get<0>() << ", " << it.get<1>() << "}\n";
  }
}

To allow client to use/test for that you could e.g. introduce a new method - similar to the get<>() - that tests for whether the container iterator is at its end, e.g. something like:

template <size_t I>
bool not_end() {
    return std::get<I>(current_)!=std::get<I>(end_);
}

I've tried to add that (se snippet code below for usage), but it still crashes al beit after output the first expected combination. I haven't examined (yet) what is causing the crash, but likely it is related to how you advance iterators in your implementation.

  for(; it != end; ++it)
  {
    cout << "{" << it.get<0>();
    if( it.not_end<1>() ) cout << ", " << it.get<1>();
    cout << "}\n";
 }

Expected output:

{1}
{2}

Regards,
/Michael