Home > Mobile >  Vector product of multiple vectors using meta function
Vector product of multiple vectors using meta function

Time:10-11

Given a vector like:

  template<int... elements>
  struct vec;

How is it possible to create a metafunction which can do multiply element by element all provided vectors. For example

   template<typename ...AllVecs>
    struct multiVecs
   {
     using type = .... 
   }

where type would execute all products element by element. For example given three vecs:

   multiVecs< vec<0,1,2>, vec<1,2,3>, vec<2,3,4> >

i should get a vec<0*1*2, 1*2*3, 2*3*4>

CodePudding user response:

Let's start with the two vector product, and go from there.

template <typename lhs, typename rhs>
struct multiplies;

template <int... lhs, int... rhs>
struct multiplies<vec<lhs...>, vec<rhs...>> {
    static_assert(sizeof...(lhs) == sizeof...(rhs), "Vector arity mismatch");
    using type = vec<(lhs * rhs)...>
};

template <typename lhs, typename rhs>
using multiplies_t = typename multiplies<lhs, rhs>::type;

This works fine, so now we just fold it over the pack.

Unfortunately C 14 doesn't have fold expressions, so we have to do it longhand.

template <typename...>
struct multiVecs;

template <typename... Ts>
using multiVecs_t = typename multiVecs<Ts...>::type;

template <typename result>
struct multiVecs<result> {
    using type = result;
};

template <typename first, typename second, typename... rest>
struct multiVecs<first, second, rest...> {
    using type = multiVecs_t<multiplies<first, second>, rest...>;
};

This is also fine

CodePudding user response:

C 17 solution:

template<int... Elements>
struct vec;

template<int Element, class... Vec>
struct vec_impl { };

template<class Vec>
struct to_vec_impl;

template<int First, int... Rest>
struct to_vec_impl<vec<First, Rest...>> {
  using type = vec_impl<First, vec<Rest...>>;
};

template<>
struct to_vec_impl<vec<>> {
  using type = vec_impl<-1>;
};

template<class Result_vec, class... Vecs>
struct gen_vec_impl;

template<int... Products, template<int, class...> class... VecImpls, 
         int... First, class... Vecs>
struct gen_vec_impl<vec<Products...>, VecImpls<First, Vecs>...>
     : gen_vec_impl<vec<Products..., (First * ... * 1)>, 
                    typename to_vec_impl<Vecs>::type...> 
{ };

template<int... Products, template<int, class...> class... VecImpls>
struct gen_vec_impl<vec<Products...>, VecImpls<-1>...> {
  using type = vec<Products...>;
};

template<class... AllVecs>
struct multiVecs {
  using type = typename gen_vec_impl<
    vec<>, typename to_vec_impl<AllVecs>::type...>::type;
};

Demo.

  • Related