SeqAn3  3.2.0-rc.1
The Modern C++ library for sequence analysis.
type_list/traits.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2022, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
15 #include <type_traits>
16 
19 
20 // ----------------------------------------------------------------------------
21 // seqan3::list_traits::detail
22 // ----------------------------------------------------------------------------
23 
24 namespace seqan3::list_traits::detail
25 {
26 
32 template <ptrdiff_t idx, typename... pack_t>
33 std::type_identity<seqan3::pack_traits::at<idx, pack_t...>> at(type_list<pack_t...>);
34 
39 template <typename... pack_t>
40 std::type_identity<seqan3::pack_traits::front<pack_t...>> front(type_list<pack_t...>);
41 
46 template <typename... pack_t>
47 std::type_identity<seqan3::pack_traits::back<pack_t...>> back(type_list<pack_t...>);
48 
54 template <typename... pack1_t, typename... pack2_t>
55 type_list<pack1_t..., pack2_t...> concat(type_list<pack1_t...>, type_list<pack2_t...>);
56 
63 template <typename... pack1_t, typename... pack2_t, typename... more_lists_t>
64 auto concat(type_list<pack1_t...>, type_list<pack2_t...>, more_lists_t...)
65 {
66  return concat(type_list<pack1_t..., pack2_t...>{}, more_lists_t{}...);
67 }
68 
73 template <typename... pack_t>
74 pack_traits::drop_front<pack_t...> drop_front(type_list<pack_t...>);
75 
81 template <template <typename> typename trait_t, typename... pack_t>
82 pack_traits::transform<trait_t, pack_t...> transform(type_list<pack_t...>);
83 
89 template <ptrdiff_t idx, typename... pack1_t>
90 pack_traits::split_after<idx, pack1_t...> split_after(type_list<pack1_t...>);
91 
97 template <size_t count, typename t>
98 auto repeat()
99 {
100  if constexpr (count == 0)
101  return type_list<>{};
102  else if constexpr (count == 1)
103  return type_list<t>{};
104  else if constexpr (count == 2)
105  return type_list<t, t>{};
106  else if constexpr (count == 3)
107  return type_list<t, t, t>{};
108  else if constexpr (count == 4)
109  return type_list<t, t, t, t>{};
110  else if constexpr (count == 5)
111  return type_list<t, t, t, t, t>{};
112  else
113  return concat(repeat<5, t>(), repeat<count - 5, t>());
114 }
115 
122 template <typename replace_t, ptrdiff_t idx, typename... pack_t>
123 pack_traits::replace_at<replace_t, idx, pack_t...> replace_at(type_list<pack_t...>);
124 
126 inline constexpr type_list<> reverse(type_list<>)
127 {
128  return {};
129 }
130 
132 template <typename head_t, typename... pack_t>
133 auto reverse(type_list<head_t, pack_t...>)
134 {
135  return concat(reverse(type_list<pack_t...>{}), type_list<head_t>{});
136 }
137 
139 template <typename... current_list_t>
140 constexpr seqan3::type_list<current_list_t...> type_list_difference(seqan3::type_list<current_list_t...>,
142 {
143  return {};
144 }
145 
147 template <typename... current_list_t, typename remove_t, typename... remove_list_t>
149 {
150  constexpr auto pos = seqan3::pack_traits::find<remove_t, current_list_t...>;
151  if constexpr (pos >= 0)
152  {
153  using split_list_t = seqan3::pack_traits::split_after<pos, current_list_t...>;
154 
155  using split_list1_t = typename split_list_t::first_type;
156  using split_list2_t = decltype(drop_front(typename split_list_t::second_type{}));
157  using filtered_list_t = decltype(concat(split_list1_t{}, split_list2_t{}));
158  return type_list_difference(filtered_list_t{}, seqan3::type_list<remove_t, remove_list_t...>{});
159  }
160  else
161  {
162  // remove_t not contained in current_list_t
163  using filtered_list_t = seqan3::type_list<current_list_t...>;
164  return type_list_difference(filtered_list_t{}, seqan3::type_list<remove_list_t...>{});
165  }
166 }
167 
168 } // namespace seqan3::list_traits::detail
169 
170 // ----------------------------------------------------------------------------
171 // seqan3::list_traits
172 // ----------------------------------------------------------------------------
173 
175 namespace seqan3::list_traits
176 {
177 
183 template <typename list_t>
184  requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
185 inline constexpr size_t size = 0;
187 
194 template <typename... pack_t>
195 inline constexpr size_t size<type_list<pack_t...>> = sizeof...(pack_t);
196 
198 template <typename query_t, typename list_t>
199  requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
200 inline constexpr ptrdiff_t count = -1;
202 
209 template <typename query_t, typename... pack_t>
210 inline constexpr ptrdiff_t count<query_t, type_list<pack_t...>> = seqan3::pack_traits::count<query_t, pack_t...>;
211 
213 template <typename query_t, typename list_t>
214  requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
215 inline constexpr ptrdiff_t find = -1;
217 
224 template <typename query_t, typename... pack_t>
225 inline constexpr ptrdiff_t find<query_t, type_list<pack_t...>> =
226  seqan3::pack_traits::detail::find<query_t, pack_t...>();
227 
229 template <template <typename> typename pred_t, typename list_t>
230  requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
231 inline constexpr ptrdiff_t find_if = -1;
233 
240 template <template <typename> typename pred_t, typename... pack_t>
241 inline constexpr ptrdiff_t find_if<pred_t, type_list<pack_t...>> =
242  seqan3::pack_traits::detail::find_if<pred_t, pack_t...>();
243 
250 template <typename query_t, typename list_t>
251  requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
252 inline constexpr bool contains = (find<query_t, list_t> != -1);
253 
255 
276 template <ptrdiff_t idx, typename list_t>
277  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>)
278  && ((idx >= 0 && idx < size<list_t>) || (-idx <= size<list_t>))
279 using at = typename decltype(detail::at<idx>(list_t{}))::type;
280 
294 template <typename list_t>
295  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (size<list_t> > 0)
296 using front = typename decltype(detail::front(list_t{}))::type;
297 
314 template <typename list_t>
315  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (size<list_t> > 0)
316 using back = typename decltype(detail::back(list_t{}))::type;
317 
319 
340 template <typename... lists_t>
341  requires (seqan3::detail::template_specialisation_of<lists_t, seqan3::type_list> && ...)
342 using concat = decltype(detail::concat(lists_t{}...));
343 
357 template <typename list_t>
358  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (size<list_t> > 0)
359 using drop_front = decltype(detail::drop_front(list_t{}));
360 
375 template <ptrdiff_t i, typename list_t>
376  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
377 using take = typename decltype(detail::split_after<i>(list_t{}))::first_type;
378 
393 template <ptrdiff_t i, typename list_t>
394  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
395 using drop = typename decltype(detail::split_after<i>(list_t{}))::second_type;
396 
411 template <ptrdiff_t i, typename list_t>
412  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
413 using take_last = drop<size<list_t> - i, list_t>;
414 
429 template <ptrdiff_t i, typename list_t>
430  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
431 using drop_last = take<size<list_t> - i, list_t>;
432 
447 template <ptrdiff_t i, typename list_t>
448  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
449 using split_after = decltype(detail::split_after<i>(list_t{}));
450 
468 template <template <typename> typename trait_t, typename list_t>
469  requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
470 using transform = decltype(detail::transform<trait_t>(list_t{}));
471 
487 template <typename replace_t, std::ptrdiff_t i, typename list_t>
488  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i < size<list_t>)
489 using replace_at = decltype(detail::replace_at<replace_t, i>(list_t{}));
490 
503 template <size_t count, typename t>
504 using repeat = decltype(detail::repeat<count, t>());
506 
507 } // namespace seqan3::list_traits
list_t
Apply a transformation trait to every type in the list and return a seqan3::type_list of the results.
Definition: type_list/traits.hpp:412
requires constexpr seqan3::detail::template_specialisation_of< list_t, seqan3::type_list > bool contains
Whether a type occurs in a type list or not.
Definition: type_list/traits.hpp:252
decltype(detail::repeat< count, t >()) repeat
Create a type list with the given type repeated count times..
Definition: type_list/traits.hpp:503
decltype(detail::transform< trait_t >(list_t{})) transform
Apply a transformation trait to every type in the list and return a seqan3::type_list of the results.
Definition: type_list/traits.hpp:469
requires(seqan3::detail::template_specialisation_of< list_t, seqan3::type_list >) &&((idx >=0 &&idx< size< list_t >)||(-idx<
Return the type at given index from the type list.
constexpr ptrdiff_t find
Get the index of the first occurrence of a type in a pack.
Definition: type_pack/traits.hpp:182
constexpr ptrdiff_t count
Count the occurrences of a type in a pack.
Definition: type_pack/traits.hpp:164
constexpr size_t size
The size of a type pack.
Definition: type_pack/traits.hpp:146
seqan3::type_list< trait_t< pack_t >... > transform
Apply a transformation trait to every type in the pack and return a seqan3::type_list of the results.
Definition: type_pack/traits.hpp:328
constexpr ptrdiff_t find_if
Get the index of the first type in a pack that satisfies the given predicate.
Definition: type_pack/traits.hpp:205
Namespace containing traits for working on seqan3::type_list.
T reverse(T... args)
Type that contains multiple types.
Definition: type_list.hpp:29
Provides seqan3::type_list.
Provides various traits for template packs.