c++ - deduce entire vector space at compile time -


inspired question : c++ generate (xyz) points in range

i began wonder whether there form of template code can, statement:

using x = axis_limits<-10, +10>; using y = axis_limits<-10, +10>; using z = axis_limits<-10, +10>;  auto space = std::vector<point>{ generate_point_space<x, y, z> }; 

construct @ compile time vector called space contains 1 point each x, y , z begin(x) <= x < end(x)... etc y , z.

order not important.

the return type of generate_point_space<> should std::initializer_list<int> or compile-time-constructed sequence. i'm not looking generate sequence of calls push_back(). easy :)

struct point have constructor of form:

point::point(int x, int y, int z) 

a single dimension of ints straightforward (code below). multi-dimensional aspect of problem beyond me today

#include <utility> #include <iostream> #include <vector>  template<int begin, int end> struct axis_limits {     static constexpr int first = begin;     static constexpr int last = end; };  namespace details {     template<typename int, typename, int begin, bool increasing>     struct integer_range_impl;      template<typename int, int... n, int begin>     struct integer_range_impl<int, std::integer_sequence<int, n...>, begin, true> {         using type = std::integer_sequence<int, n+begin...>;     };      template<typename int, int... n, int begin>     struct integer_range_impl<int, std::integer_sequence<int, n...>, begin, false> {         using type = std::integer_sequence<int, begin-n...>;     }; }  template<typename int, int begin, int end> using integer_range = typename details::integer_range_impl< int, std::make_integer_sequence<int, (begin<end) ? end-begin : begin-end>, begin, (begin<end) >::type;  template<int...is> std::vector<int> make_vector(std::integer_sequence<int, is...>) {     return std::vector<int> { is... }; }  template<int begin, int end> struct axis_range {     using sequence_type = integer_range<int, begin, end>;     static constexpr int size = sequence_type::size();     static std::vector<int> as_vector()     {         return make_vector(sequence_type {});     } };  template< int begin, int end > std::vector<int> make_axis(const axis_limits<begin, end> &) {     return axis_range<begin, end>::as_vector(); }  template<class t> void dump_vector(std::ostream& os, const std::vector<t>& v) {     const char* sep = "{ ";     for(const auto& : v) {         os << sep << i;         sep = ", ";     }     os << " }"; }  template<class t> std::ostream& operator<<(std::ostream& os, const std::vector<t>& vec) {     dump_vector(os, vec);     return os; }  using namespace std;  int main() {      using x = axis_limits<-5, +5>;     auto space = std::vector<int>(make_axis(x{}));     cout << space << endl;     return 0; } 

current output:

{ -5, -4, -3, -2, -1, 0, 1, 2, 3, 4 } 

what looking for:

{ { -10, -10, -10 }, { -10, -10, -9 } .... { 9, 9, 8 }, { 9, 9, 9 } } 

some metaprogramming helpers work lists of types:

template<class t>struct tag{using type=t;}; template<class tag>using type=typename tag::type;  template<class...>struct types{using type=types;};  template<class...ts> struct cat; template<class...ts> using cat_t=type<cat<ts...>>;  template<class...as, class...bs, class...ts> struct cat< types<as...>, types<bs...>, ts... >:   cat< types<as...,bs...>, ts... > {}; template<class...ts> struct cat< types<ts...> >:   types<ts...> {}; template<> struct cat<>:   types<> {}; 

a way map between sequences of values, , sequences of types. find types easier work with:

template<class seq> struct seq_to_types; template<class seq> using seq_to_types_t=type<seq_to_types<seq>>; template<class t, t...ts> struct seq_to_types< std::integer_sequence<t,ts...> >:   tag< types< std::integral_constant<t,ts>... > > {}; template<class t, class rhs> struct types_to_seq:tag<rhs>{}; template<class t, class types> using types_to_seq_t=type<types_to_seq<t,types>>; template<class t, t...ts> struct types_to_seq<t, types<std::integral_constant<t, ts>...>>:   tag<std::integer_sequence<t, ts...>> {}; template<class t, class...ts> struct types_to_seq<t, types<ts...>>:   types< types_to_seq_t<t, ts>... > {}; 

now can take std::integer_sequence<int, 1,2,3> , produce types< std::integral_constant<int,1>, std::integral_constant<int,2>, std::integral_constant<int,3> > in opinion far easier work with. can map back.

this takes types<ts...> , function on types, , map:

template<template<class...>class m, class seq> struct mapper; template<template<class...>class m, class seq> using mapper_t=type<mapper<m,seq>>; template<template<class...>class m, class...ts> struct mapper<m, types<ts...>>:   types<m<ts>...> {}; 

mapper_t< some_metafunction, types<blah...>> map each blah through some_metafunction produce new list of types.

next, way take type function, , bind first argument x:

template<template<class...>class f, class x> struct bind_1st {   template<class...ts>   using apply=f<x,ts...>; }; 

which can use cross product (together cat_t , mapper_t):

template<class...ts> struct cross_product:types<types<>>{}; template<class...ts> using cross_product_t=type<cross_product<ts...>>;  template<class...t0s, class...ts> struct cross_product<types<t0s...>, ts...>:cat<   mapper_t<     bind_1st<cat_t, types<t0s>>::template apply,     cross_product_t<ts...>   >... >{}; 

now work on next problem. have set of points, , want generate cross product.

template<class...seq> struct coords; template<class...seq> using coords_t=type<coords<seq...>>; template<class t, t...ts, class...ts> struct coords< std::integer_sequence<t,ts...>, ts... >:   types_to_seq<     t,     cross_product_t<       seq_to_types_t<std::integer_sequence<t,ts...>>,       seq_to_types_t<ts>...     >   > {}; 

should explode nicely.

live example.

the next step build syntax.

template<class t, t t0, class seq> struct offset_sequence; template<class t, t t0, class seq> using offset_sequence_t=type<offset_sequence<t, t0, seq>>; template<class t, t t0, t...ts> struct offset_sequence<t, t0, std::integer_sequence<t, ts...>>:   tag<std::integer_sequence<t, (t0+ts)...>> {}; template<int start, int finish> using axis_limits = offset_sequence_t<int, start,   std::make_integer_sequence<finish-start> >;  template<class t> using point = std::vector<t>;  template<class t, t...is> point<t> make_point( std::integer_sequence<t, is...> ) {   return {is...}; } template<class...pts> std::vector<point<int>> make_space( types<pts...> ) {   return { make_point( pts{} )... }; } template<class...ts> std::vector<point<int>> generate_point_space() {   return make_space( coords_t<ts...>{} ); } 

and have syntax want.

we can make things arrays , constexpr if want. change make_point return array of sizeof...(is) , like.


Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -