tinympl  0.2
mini MPL library for C++11
transpose.hpp
1 // Copyright (C) 2013, Ennio Barbaro.
2 //
3 // Use, modification, and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://sbabbi.github.io/tinympl for documentation.
8 //
9 // You are welcome to contact the author at:
10 // enniobarbaro@gmail.com
11 //
12 
13 #ifndef TINYMPL_TRANSPOSE_HPP
14 #define TINYMPL_TRANSPOSE_HPP
15 
16 #include <tinympl/variadic/all_of.hpp>
17 #include <tinympl/variadic/at.hpp>
18 #include <tinympl/as_sequence.hpp>
19 #include <tinympl/is_sequence.hpp>
20 #include <tinympl/sequence.hpp>
21 #include <tinympl/variadic/all_of.hpp>
22 #include <tinympl/size.hpp>
23 #include <tinympl/bind.hpp>
24 #include <tinympl/equal_to.hpp>
25 #include <tinympl/int.hpp>
26 #include <tinympl/at.hpp>
27 
28 namespace tinympl {
29 
39 template < class SequenceOfSequences,
40  template<class ...> class OutOuter =
41  as_sequence<SequenceOfSequences>::template rebind,
42  template<class ...> class OutInner =
43  as_sequence<typename at<0,SequenceOfSequences>::type>::
44  template rebind >
45 struct transpose :
46  transpose< as_sequence_t<SequenceOfSequences>, OutOuter, OutInner > {};
47 
48 template< class ... Sequences,
49  template<class ...> class OutOuter,
50  template<class ...> class OutInner>
51 class transpose< sequence<Sequences...>, OutOuter, OutInner> {
53  "transpose: not all the elements of the main sequence are sequences" );
54 
55  static_assert( sizeof...( Sequences ) > 0,
56  "transpose is undefined on empty sequences" );
57 
58  enum {size = tinympl::size<
59  typename variadic::at<0,Sequences...>::type>::value};
60 
61  static_assert( variadic::all_of <
62  bind < equal_to, int_<size>,
63  bind<tinympl::size, arg1> >::
64  template eval, Sequences...>::value,
65  "transpose: all the sequences must have the same size" );
66 
67  template<std::size_t i, class ... Bound>
68  struct impl {
69  typedef OutInner< typename at<i-1, Sequences>::type ... > cur_t;
70  typedef typename impl < i - 1, cur_t, Bound...>::type type;
71  };
72 
73  template<class ... Bound>
74  struct impl<0, Bound...> {
75  typedef OutOuter<Bound...> type;
76  };
77 
78 public:
79  typedef typename impl<size>::type type;
80 };
81 
82 } // namespace tinympl
83 
84 #endif // TINYMPL_TRANSPOSE_HPP
Determines whether every element in the sequence satisfies the given predicate.
Definition: all_of.hpp:31
Extract the i-th element of a variadic template.
Definition: at.hpp:27
Transpose a sequence of sequences.
Definition: transpose.hpp:45
Get the number of elements of a sequence.
Definition: size.hpp:27
The main sequence type.
Definition: sequence.hpp:28