SeqAn3  3.2.0-rc.1
The Modern C++ library for sequence analysis.
type_pack/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 #include <utility>
17 
19 
20 // ----------------------------------------------------------------------------
21 // seqan3::pack_traits::detail
22 // ----------------------------------------------------------------------------
23 
24 namespace seqan3::pack_traits::detail
25 {
26 
33 template <typename query_t, typename... pack_t>
34 constexpr ptrdiff_t find()
35 {
36  ptrdiff_t i = 0;
37  return ((std::is_same_v<query_t, pack_t> ? false : ++i) && ...) ? -1 : i;
38 }
39 
46 template <template <typename> typename pred_t, typename... pack_t>
47 constexpr ptrdiff_t find_if()
48 {
49  ptrdiff_t i = 0;
50  return ((pred_t<pack_t>::value ? false : ++i) && ...) ? -1 : i;
51 }
52 
59 template <ptrdiff_t idx, typename head_t, typename... tail_t>
60 auto at()
61 {
62  if constexpr (idx == 0)
63  return std::type_identity<head_t>{};
64  else if constexpr (idx > 0)
65 #ifdef __clang__
66  return std::type_identity<__type_pack_element<idx - 1, tail_t...>>{};
67 #else
68  return at<idx - 1, tail_t...>();
69 #endif // __clang__
70  else
71  return at<sizeof...(tail_t) + 1 + idx, head_t, tail_t...>();
72 }
73 
79 template <typename head_t, typename... tail_t>
80 std::type_identity<head_t> front();
81 
87 template <typename head_t, typename... tail_t>
88 type_list<tail_t...> drop_front();
89 
97 template <ptrdiff_t idx, typename head_t, typename... pack2_t, typename... pack1_t>
98 auto split_after(type_list<pack1_t...>)
99 {
100  if constexpr (idx == sizeof...(pack2_t) + 1)
101  return std::pair<type_list<pack1_t..., head_t, pack2_t...>, type_list<>>{};
102  else if constexpr (idx == 0)
103  return std::pair<type_list<pack1_t...>, type_list<head_t, pack2_t...>>{};
104  else
105  return split_after<idx - 1, pack2_t...>(type_list<pack1_t..., head_t>{});
106 }
107 
115 template <typename replace_t, ptrdiff_t idx, typename... pack_t, size_t... i>
116 auto replace_at(std::index_sequence<i...>) -> type_list<std::conditional_t<i == idx, replace_t, pack_t>...>;
117 
118 } // namespace seqan3::pack_traits::detail
119 
120 // ----------------------------------------------------------------------------
121 // seqan3::pack_traits
122 // ----------------------------------------------------------------------------
123 
124 namespace seqan3::pack_traits
125 {
126 
145 template <typename... pack_t>
146 inline constexpr size_t size = sizeof...(pack_t);
147 
163 template <typename query_t, typename... pack_t>
164 inline constexpr ptrdiff_t count = (std::is_same_v<query_t, pack_t> + ... + 0);
165 
181 template <typename query_t, typename... pack_t>
182 inline constexpr ptrdiff_t find = seqan3::pack_traits::detail::find<query_t, pack_t...>();
183 
204 template <template <typename> typename pred_t, typename... pack_t>
205 inline constexpr ptrdiff_t find_if = seqan3::pack_traits::detail::find_if<pred_t, pack_t...>();
206 
222 template <typename query_t, typename... pack_t>
223 inline constexpr bool contains = (find<query_t, pack_t...> != -1);
225 
246 template <ptrdiff_t idx, typename... pack_t>
247  requires (idx >= 0 && idx < sizeof...(pack_t)) || (-idx <= sizeof...(pack_t))
248 using at = typename decltype(detail::at<idx, pack_t...>())::type;
249 
263 template <typename... pack_t>
264  requires (sizeof...(pack_t) > 0)
265 using front = typename decltype(detail::front<pack_t...>())::type;
266 
283 template <typename... pack_t>
284  requires (sizeof...(pack_t) > 0)
285 using back = typename decltype((std::type_identity<pack_t>{}, ...))::type; // use comma operator
286 
288 
306 template <typename... pack_t>
307  requires (sizeof...(pack_t) > 0)
308 using drop_front = typename decltype(detail::drop_front<pack_t...>())::type;
309 
327 template <template <typename> typename trait_t, typename... pack_t>
328 using transform = seqan3::type_list<trait_t<pack_t>...>;
329 
331 
350 template <ptrdiff_t i, typename... pack_t>
351  requires (i >= 0 && i <= size<pack_t...>)
352 using take = typename decltype(detail::split_after<i, pack_t...>(type_list<>{}))::first_type;
353 
368 template <ptrdiff_t i, typename... pack_t>
369  requires (i >= 0 && i <= size<pack_t...>)
370 using drop = typename decltype(detail::split_after<i, pack_t...>(type_list<>{}))::second_type;
371 
386 template <ptrdiff_t i, typename... pack_t>
387  requires (i >= 0 && i <= size<pack_t...>)
388 using take_last = drop<size<pack_t...> - i, pack_t...>;
389 
404 template <ptrdiff_t i, typename... pack_t>
405  requires (i >= 0 && i <= size<pack_t...>)
406 using drop_last = take<size<pack_t...> - i, pack_t...>;
407 
422 template <ptrdiff_t i, typename... pack_t>
423  requires (i >= 0 && i <= size<pack_t...>)
424 using split_after = decltype(detail::split_after<i, pack_t...>(type_list<>{}));
425 
441 template <typename replace_t, std::ptrdiff_t i, typename... pack_t>
442  requires (i >= 0 && i < size<pack_t...>)
443 using replace_at = decltype(detail::replace_at<replace_t, i, pack_t...>(std::make_index_sequence<size<pack_t...>>{}));
444 
446 
447 } // namespace seqan3::pack_traits
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
requires(idx >=0 &&idx< sizeof...(pack_t))||(-idx<
Return the type at given index from the type pack.
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
constexpr bool contains
Whether a type occurs in a pack or not.
Definition: type_pack/traits.hpp:223
Namespace containing traits for working on type packs.
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
SeqAn specific customisations in the standard namespace.
Type that contains multiple types.
Definition: type_list.hpp:29
Provides seqan3::type_list.