如何使用笛卡尔积将一组向量转换为元组向量?

时间:2013-05-15 18:02:25

标签: c++ c++11 vector tuples cartesian-product

出于使用std::tuple构造函数的单元测试类的目的,我想生成特殊情况的示例以及构造函数参数的随机值。假设我std::tuple std::vector<T1>std::vector<Tn>,(每个Ti都不同)我怎样才能将其转换为std::vector的{​​{1}}所有std::tuple<T1, ..., Tn>组合的完整笛卡尔积?

具体来说,我想要一个可变参数函数模板,如下所示:

template<typename... Args>
std::vector<std::tuple<Args...> cartesian_product(std::vector<Args>...)
{
    // template magic or fat mulitple loops?
}

可以像这样使用:

// some type to be tested
class MyType
{
    MyType(std::tuple<int, bool, std::string>);
    // bla
};

// test values for each constructor argument
std::tuple< std::vector<int>, std::vector<bool>, std::vector<std::string> > input {
    { 1, 2, 3}, { false, true}, { "Hello", "World"}
};

// should become 3 x 2 x 2 = 12 cases { {1, false, "Hello"}, ... , {3, true, "World"} } 
std::vector< std::tuple<int, bool, std::string> > test_cases = cartesian_product( input );

// can write flat single loop over all cases
for (auto t: test_cases) {
    BOOST_CHECK(MyType(t).my_test());
}

是否有任何(Boost)库可以开箱即用?如何参与为此编写可变参数模板?

2 个答案:

答案 0 :(得分:3)

http://ideone.com/ThhAoa

不是那么棘手:

#include <cstddef>
#include <utility>
#include <vector>
#include <tuple>
#include <string>
#include <iostream>

using std::size_t;

template<size_t...> struct seq {};
template<size_t Min, size_t Max, size_t... s>
struct make_seq:make_seq< Min, Max-1, Max-1, s... > {};
template<size_t Min, size_t... s>
struct make_seq< Min, Min, s... > {
    typedef seq<s...> type;
};
template<size_t Max, size_t Min=0>
using MakeSeq = typename make_seq<Min, Max>::type;

size_t product_size() {
  return 1;
}
template<typename... Sizes>
size_t product_size( size_t x, Sizes... tail ) {
  return x * product_size(tail...);
}
namespace details {
  template<typename max_iterator, typename Lambda>
  void for_each_index( max_iterator mbegin, max_iterator mend, Lambda&& f, std::vector<size_t>& idx ) {
    if (mbegin == mend) {
      f(idx);
    } else {
      for (size_t i = 0; i < *mbegin; ++i) {
        idx.push_back(i);
        for_each_index(mbegin+1, mend, f, idx);
        idx.pop_back();
      }
    }
  }
  template<typename Lambda>
  void for_each_index( std::vector<size_t> const& maxes, Lambda&& f ) {
    std::vector<size_t> idx;
    details::for_each_index( maxes.begin(), maxes.end(), f, idx );
  }
  template<size_t... s, typename... Ts>
  std::vector< std::tuple<Ts...> > does_it_blend( seq<s...>, std::tuple< std::vector<Ts>... >const& input ) {
    std::vector< std::tuple<Ts...> > retval;
    retval.reserve( product_size( std::get<s>(input).size()... ) );
    std::vector<size_t> maxes = {
      (std::get<s>(input).size())...
    };
    for_each_index( maxes, [&](std::vector<size_t> const& idx){
      retval.emplace_back( std::get<s>(input)[idx[s]]... );
    });
    return retval;
  }
}
template<typename... Ts>
std::vector< std::tuple<Ts...> > does_it_blend( std::tuple< std::vector<Ts>... >const& input ) {
  return details::does_it_blend( MakeSeq< sizeof...(Ts) >(), input );
}

int main() {
  std::tuple< std::vector<int>, std::vector<bool>, std::vector<std::string> > input {
    { 1, 2, 3}, { false, true}, { "Hello", "World"}
  };

  // should become 3 x 2 x 2 = 12 cases { {1, false, "Hello"}, ... , {3, true, "World"} } 
  std::vector< std::tuple<int, bool, std::string> > test_cases = does_it_blend( input );

  for( auto&& x:test_cases ) {
    std::cout << std::get<0>(x) << "," << std::get<1>(x) << "," << std::get<2>(x) << "\n";
  }
}

这里我创建了一个函数,它执行可能索引的笛卡尔积,然后直接在输出容器中创建元组。

我也很省心保留输出尺寸。

现在代码更少。

答案 1 :(得分:1)

这有帮助吗?

#include <vector>
#include <tuple>
#include <type_traits>

template <typename... T>
struct transpose {};

template <typename... T>
struct transpose<std::tuple<std::vector<T>...>>
{
    using type = std::vector<std::tuple<T...>>;
};

template <typename... T>
struct transpose<std::vector<std::tuple<T...>>>
{
    using type = std::tuple<std::vector<T>...>;
};

int main()
{
    std::tuple<std::vector<int>, std::vector<bool>> var;

    static_assert(
        std::is_same<
            transpose<decltype(var)>::type,
            std::vector<std::tuple<int, bool>>
       >::value, ""
    );
}