Awali
Another Weighted Automata library
sub_tuple.hh
Go to the documentation of this file.
1 // This file is part of Awali.
2 // Copyright 2016-2022 Sylvain Lombardy, Victor Marsault, Jacques Sakarovitch
3 //
4 // Awali is a free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef AWALI_MISC_SUB_TUPLE_HH
18 #define AWALI_MISC_SUB_TUPLE_HH
19 
20 #include <awali/common/tuple.hh>
21 
22 namespace awali {
23  namespace sttc {
24  namespace internal {
25  /* sub_tuple<I...>::get(t)
26  * builds the tuple formed with the components at indices I...
27  * of t
28  *
29  * The type is obtained from the type T of the tuple by
30  * sub_tuple<I...>::template type<T>
31  */
32 
33  template<size_t Head, size_t... Tail>
34  struct sub_tuple {
35 
36  template<typename Tuple>
38  template<typename Tuple>
39  static
40  auto get(const Tuple& t) -> type<Tuple> {
41  return std::tuple_cat(std::make_tuple(std::get<Head>(t)), sub_tuple<Tail...>::get(t));
42  }
43  };
44 
45  template<size_t I>
46  struct sub_tuple<I> {
47 
48  template<typename Tuple>
49  using type = std::tuple<typename std::tuple_element<I,Tuple>::type>;
50  template<typename Tuple>
51  static
52  auto
53  get(const Tuple& t) -> type<Tuple> {
54  return std::make_tuple(std::get<I>(t));
55  }
56  };
57 
58  /* tuple_tail (t)
59  * returns the tail of the tuple t
60  *
61  * The type is obtained from the type T of the tuple by
62  * tail_tuple<T>::type
63  */
64 
65  template<typename T> struct tail_tuple;
66  template<typename Head, typename... Tail>
67  struct tail_tuple<std::tuple<Head, Tail...>> {
68  using type = std::tuple<Tail...>;
69  };
70 
71  template<size_t I, size_t N>
72  struct comp_tail_tuple {
73  template<typename Tuple, typename... Values>
74  static
75  auto
76  get(const Tuple& t, Values & ... values)
77  -> typename tail_tuple<Tuple>::type
78  {
79  return comp_tail_tuple<I+1, N>::get(t, values..., std::get<I>(t));
80  }
81  };
82 
83  template<size_t N>
84  struct comp_tail_tuple<N, N> {
85  template<typename Tuple, typename... Values>
86  static
87  auto
88  get(const Tuple&, Values & ... values)
89  -> typename tail_tuple<Tuple>::type
90  {
91  return std::make_tuple(values...);
92  }
93  };
94 
95  template<typename Tuple>
96  auto tuple_tail(const Tuple& t )
97  -> typename tail_tuple<Tuple>::type
98  {
100  }
101 
102  /* rem_in_tuple<I>::get(t)
103  * builds the tuple formed with the components all components
104  * of t except component at index I
105  *
106  * The type is obtained from the type T of the tuple by
107  * rem_in_tuple<I>::template type<T>
108  */
109 
110  template<size_t N>
111  struct rem_in_tuple{
112  template<typename Tuple>
113  using type = typename std::cons_tuple<
114  typename std::tuple_element<0,Tuple>::type,
116 
117  template<typename Tuple>
118  static
119  auto get(const Tuple& t) -> type<Tuple>
120  {
121  return std::tuple_cat(std::make_tuple(std::get<0>(t)), rem_in_tuple<N-1>::get(tuple_tail(t)));
122  }
123  };
124 
125  template<>
126  struct rem_in_tuple<0u>{
127  template<typename Tuple>
128  using type = typename tail_tuple<Tuple>::type;
129 
130  template<typename Tuple>
131  static
132  auto get(const Tuple& t) -> type<Tuple> {
133  return tuple_tail(t);
134  }
135  };
136 
137  /* replace_in_tuple<I,T>::get(t,x)
138  * builds the tuple formed with the components
139  * of t except, with the component at index I replace by x of type T
140  *
141  */
142 
143  template<size_t N, typename T>
145  template<typename Tuple>
146  using type = typename std::cons_tuple<
147  typename std::tuple_element<0,Tuple>::type,
149 
150  template<typename Tuple>
151  static
152  auto get(const Tuple& t, const T& x) -> type<Tuple>
153  {
154  return std::tuple_cat(std::make_tuple(std::get<0>(t)), replace_in_tuple<N-1,T>::get(tuple_tail(t),x));
155  }
156  };
157 
158  template<typename T>
159  struct replace_in_tuple<0u,T>{
160  template<typename Tuple>
162 
163  template<typename Tuple>
164  static
165  auto get(const Tuple& t, const T& x) -> type<Tuple> {
166  return std::tuple_cat(std::make_tuple(x), tuple_tail(t));
167  }
168  };
169 
170 
171  }
172  }
173 }//end of ns awali::stc
174 
175 #endif
auto tuple_tail(const Tuple &t) -> typename tail_tuple< Tuple >::type
Definition: sub_tuple.hh:96
std::tuple< Tail... > type
Definition: sub_tuple.hh:68
Definition: sub_tuple.hh:65
Main namespace of Awali.
Definition: ato.hh:22
static auto get(const Tuple &, Values &... values) -> typename tail_tuple< Tuple >::type
Definition: sub_tuple.hh:88
Definition: sub_tuple.hh:72
static auto get(const Tuple &t, Values &... values) -> typename tail_tuple< Tuple >::type
Definition: sub_tuple.hh:76
static auto get(const Tuple &t) -> type< Tuple >
Definition: sub_tuple.hh:132
typename tail_tuple< Tuple >::type type
Definition: sub_tuple.hh:128
Definition: sub_tuple.hh:111
typename std::cons_tuple< typename std::tuple_element< 0, Tuple >::type, typename rem_in_tuple< N-1 >::template type< typename tail_tuple< Tuple >::type > >::type type
Definition: sub_tuple.hh:115
static auto get(const Tuple &t) -> type< Tuple >
Definition: sub_tuple.hh:119
static auto get(const Tuple &t, const T &x) -> type< Tuple >
Definition: sub_tuple.hh:165
typename std::cons_tuple< T, typename tail_tuple< Tuple >::type >::type type
Definition: sub_tuple.hh:161
Definition: sub_tuple.hh:144
static auto get(const Tuple &t, const T &x) -> type< Tuple >
Definition: sub_tuple.hh:152
typename std::cons_tuple< typename std::tuple_element< 0, Tuple >::type, typename replace_in_tuple< N-1, T >::template type< typename tail_tuple< Tuple >::type > >::type type
Definition: sub_tuple.hh:148
static auto get(const Tuple &t) -> type< Tuple >
Definition: sub_tuple.hh:53
std::tuple< typename std::tuple_element< I, Tuple >::type > type
Definition: sub_tuple.hh:49
Definition: sub_tuple.hh:34
static auto get(const Tuple &t) -> type< Tuple >
Definition: sub_tuple.hh:40
typename std::cons_tuple< typename std::tuple_element< Head, Tuple >::type, typename sub_tuple< Tail... >::template type< Tuple > >::type type
Definition: sub_tuple.hh:37
Definition: tuple.hh:359