SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
type_pack/traits.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <type_traits>
13#include <utility>
14
16
17// ----------------------------------------------------------------------------
18// seqan3::pack_traits::detail
19// ----------------------------------------------------------------------------
20
21namespace seqan3::pack_traits::detail
22{
23
30template <typename query_t, typename... pack_t>
31constexpr ptrdiff_t find()
32{
33 ptrdiff_t i = 0;
34 return ((std::is_same_v<query_t, pack_t> ? false : ++i) && ...) ? -1 : i;
35}
36
43template <template <typename> typename pred_t, typename... pack_t>
44constexpr ptrdiff_t find_if()
45{
46 ptrdiff_t i = 0;
47 return ((pred_t<pack_t>::value ? false : ++i) && ...) ? -1 : i;
48}
49
56template <ptrdiff_t idx, typename head_t, typename... tail_t>
57auto at()
58{
59 if constexpr (idx == 0)
61 else if constexpr (idx > 0)
64#else
65 return at<idx - 1, tail_t...>();
66#endif // __clang__
67 else
68 return at<sizeof...(tail_t) + 1 + idx, head_t, tail_t...>();
69}
70
76template <typename head_t, typename... tail_t>
78
84template <typename head_t, typename... tail_t>
86
94template <ptrdiff_t idx, typename head_t, typename... pack2_t, typename... pack1_t>
96{
97 if constexpr (idx == sizeof...(pack2_t) + 1)
99 else if constexpr (idx == 0)
101 else
102 return split_after<idx - 1, pack2_t...>(type_list<pack1_t..., head_t>{});
103}
104
112template <typename replace_t, ptrdiff_t idx, typename... pack_t, size_t... i>
114
115} // namespace seqan3::pack_traits::detail
116
117// ----------------------------------------------------------------------------
118// seqan3::pack_traits
119// ----------------------------------------------------------------------------
120
121namespace seqan3::pack_traits
122{
123
142template <typename... pack_t>
143inline constexpr size_t size = sizeof...(pack_t);
144
160template <typename query_t, typename... pack_t>
161inline constexpr ptrdiff_t count = (std::is_same_v<query_t, pack_t> + ... + 0);
162
178template <typename query_t, typename... pack_t>
179inline constexpr ptrdiff_t find = seqan3::pack_traits::detail::find<query_t, pack_t...>();
180
201template <template <typename> typename pred_t, typename... pack_t>
202inline constexpr ptrdiff_t find_if = seqan3::pack_traits::detail::find_if<pred_t, pack_t...>();
203
219template <typename query_t, typename... pack_t>
220inline constexpr bool contains = (find<query_t, pack_t...> != -1);
222
243template <ptrdiff_t idx, typename... pack_t>
244 requires (idx >= 0 && idx < sizeof...(pack_t)) || (-idx <= sizeof...(pack_t))
245using at = typename decltype(detail::at<idx, pack_t...>())::type;
246
260template <typename... pack_t>
261 requires (sizeof...(pack_t) > 0)
262using front = typename decltype(detail::front<pack_t...>())::type;
263
280template <typename... pack_t>
281 requires (sizeof...(pack_t) > 0)
282using back = typename decltype((std::type_identity<pack_t>{}, ...))::type; // use comma operator
283
285
303template <typename... pack_t>
304 requires (sizeof...(pack_t) > 0)
305using drop_front = typename decltype(detail::drop_front<pack_t...>())::type;
306
324template <template <typename> typename trait_t, typename... pack_t>
326
328
347template <ptrdiff_t i, typename... pack_t>
348 requires (i >= 0 && i <= size<pack_t...>)
349using take = typename decltype(detail::split_after<i, pack_t...>(type_list<>{}))::first_type;
350
365template <ptrdiff_t i, typename... pack_t>
366 requires (i >= 0 && i <= size<pack_t...>)
367using drop = typename decltype(detail::split_after<i, pack_t...>(type_list<>{}))::second_type;
368
383template <ptrdiff_t i, typename... pack_t>
384 requires (i >= 0 && i <= size<pack_t...>)
386
401template <ptrdiff_t i, typename... pack_t>
402 requires (i >= 0 && i <= size<pack_t...>)
404
419template <ptrdiff_t i, typename... pack_t>
420 requires (i >= 0 && i <= size<pack_t...>)
421using split_after = decltype(detail::split_after<i, pack_t...>(type_list<>{}));
422
438template <typename replace_t, std::ptrdiff_t i, typename... pack_t>
439 requires (i >= 0 && i < size<pack_t...>)
440using replace_at = decltype(detail::replace_at<replace_t, i, pack_t...>(std::make_index_sequence<size<pack_t...>>{}));
441
443
444} // namespace seqan3::pack_traits
A "pretty printer" for most SeqAn data structures and related types.
Definition debug_stream_type.hpp:79
decltype(detail::replace_at< replace_t, i, pack_t... >(std::make_index_sequence< size< pack_t... > >{})) replace_at
Replace the type at the given index with the given type.
Definition type_pack/traits.hpp:440
type_list< tail_t... > drop_front()
Implementation for seqan3::pack_traits::drop_front.
typename decltype(detail::drop_front< pack_t... >())::type drop_front
Return a seqan3::type_list of all the types in the type pack, except the first.
Definition type_pack/traits.hpp:305
typename decltype((std::type_identity< pack_t >{},...))::type back
Return the last type from the type pack.
Definition type_pack/traits.hpp:282
constexpr ptrdiff_t find
Get the index of the first occurrence of a type in a pack.
Definition type_pack/traits.hpp:179
constexpr ptrdiff_t count
Count the occurrences of a type in a pack.
Definition type_pack/traits.hpp:161
typename decltype(detail::at< idx, pack_t... >())::type at
Return the type at given index from the type pack.
Definition type_pack/traits.hpp:245
constexpr ptrdiff_t find_if()
Implementation for seqan3::pack_traits::find_if.
Definition type_pack/traits.hpp:44
constexpr ptrdiff_t find()
Implementation for seqan3::pack_traits::find.
Definition type_pack/traits.hpp:31
decltype(detail::split_after< i, pack_t... >(type_list<>{})) split_after
Split a type pack into two parts returned as a pair of seqan3::type_list.
Definition type_pack/traits.hpp:421
auto at()
Implementation for seqan3::pack_traits::at.
Definition type_pack/traits.hpp:57
typename decltype(detail::front< pack_t... >())::type front
Return the first type from the type pack.
Definition type_pack/traits.hpp:262
typename decltype(detail::split_after< i, pack_t... >(type_list<>{}))::second_type drop
Return a seqan3::type_list of the types in the type pack, except the first n.
Definition type_pack/traits.hpp:367
constexpr size_t size
The size of a type pack.
Definition type_pack/traits.hpp:143
std::type_identity< head_t > front()
Implementation for seqan3::pack_traits::front.
typename decltype(detail::split_after< i, pack_t... >(type_list<>{}))::first_type take
Return a seqan3::type_list of the first n types in the type pack.
Definition type_pack/traits.hpp:349
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:202
constexpr bool contains
Whether a type occurs in a pack or not.
Definition type_pack/traits.hpp:220
Namespace containing traits for working on type packs.
Type that contains multiple types.
Definition type_list.hpp:26
Provides seqan3::type_list.
Hide me