SeqAn3 3.1.0
The Modern C++ library for sequence analysis.
traits.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2021, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2021, 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
16#include <utility>
17
19
20// ----------------------------------------------------------------------------
21// seqan3::pack_traits::detail
22// ----------------------------------------------------------------------------
23
24namespace seqan3::pack_traits::detail
25{
26
33template <typename query_t, typename ...pack_t>
34constexpr 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
46template <template <typename> typename pred_t, typename ...pack_t>
47constexpr ptrdiff_t find_if()
48{
49 ptrdiff_t i = 0;
50 return ((pred_t<pack_t>::value ? false : ++i) && ...) ? -1 : i;
51}
52
59template <ptrdiff_t idx, typename head_t, typename ...tail_t>
60auto at()
61{
62 if constexpr (idx == 0)
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
79template <typename head_t, typename ...tail_t>
81
87template <typename head_t, typename ...tail_t>
88type_list<tail_t...> drop_front();
89
97template <ptrdiff_t idx,
98 typename head_t, typename ...pack2_t,
99 typename ...pack1_t>
100auto split_after(type_list<pack1_t...>)
101{
102 if constexpr (idx == sizeof...(pack2_t) + 1)
103 return std::pair<type_list<pack1_t..., head_t, pack2_t...>, type_list<>>{};
104 else if constexpr (idx == 0)
105 return std::pair<type_list<pack1_t...>, type_list<head_t, pack2_t...>>{};
106 else
107 return split_after<idx - 1, pack2_t...>(type_list<pack1_t..., head_t>{});
108}
109
117template <typename replace_t,
118 ptrdiff_t idx,
119 typename ...pack_t,
120 size_t ...i>
121auto replace_at(std::index_sequence<i...>) -> type_list<std::conditional_t<i == idx, replace_t, pack_t>...>;
122
123} // namespace seqan3::pack_traits::detail
124
125// ----------------------------------------------------------------------------
126// seqan3::pack_traits
127// ----------------------------------------------------------------------------
128
129namespace seqan3::pack_traits
130{
131
150template <typename ...pack_t>
151inline constexpr size_t size = sizeof...(pack_t);
152
168template <typename query_t, typename ...pack_t>
169inline constexpr ptrdiff_t count = (std::is_same_v<query_t, pack_t> + ... + 0);
170
186template <typename query_t, typename ...pack_t>
187inline constexpr ptrdiff_t find = seqan3::pack_traits::detail::find<query_t, pack_t...>();
188
209template <template <typename> typename pred_t, typename ...pack_t>
210inline constexpr ptrdiff_t find_if = seqan3::pack_traits::detail::find_if<pred_t, pack_t...>();
211
227template <typename query_t, typename ...pack_t>
228inline constexpr bool contains = (find<query_t, pack_t...> != -1);
230
251template <ptrdiff_t idx, typename ...pack_t>
253 requires (idx >= 0 && idx < sizeof...(pack_t)) ||
254 (-idx <= sizeof...(pack_t))
256using at = typename decltype(detail::at<idx, pack_t...>())::type;
257
271template <typename ...pack_t>
273 requires (sizeof...(pack_t) > 0)
275using front = typename decltype(detail::front<pack_t...>())::type;
276
293template <typename ...pack_t>
295 requires (sizeof...(pack_t) > 0)
297using back = typename decltype((std::type_identity<pack_t>{}, ...))::type; // use comma operator
298
300
318template <typename ...pack_t>
320 requires (sizeof...(pack_t) > 0)
322using drop_front = typename decltype(detail::drop_front<pack_t...>())::type;
323
341template <template <typename> typename trait_t, typename ...pack_t>
343
345
364template <ptrdiff_t i, typename ...pack_t>
366 requires (i >= 0 && i <= size<pack_t...>)
368using take = typename decltype(detail::split_after<i, pack_t...>(type_list<>{}))::first_type;
369
384template <ptrdiff_t i, typename ...pack_t>
386 requires (i >= 0 && i <= size<pack_t...>)
388using drop = typename decltype(detail::split_after<i, pack_t...>(type_list<>{}))::second_type;
389
404template <ptrdiff_t i, typename ...pack_t>
406 requires (i >= 0 && i <= size<pack_t...>)
408using take_last = drop<size<pack_t...> - i, pack_t...>;
409
424template <ptrdiff_t i, typename ...pack_t>
426 requires (i >= 0 && i <= size<pack_t...>)
428using drop_last = take<size<pack_t...> - i, pack_t...>;
429
444template <ptrdiff_t i, typename ...pack_t>
446 requires (i >= 0 && i <= size<pack_t...>)
448using split_after = decltype(detail::split_after<i, pack_t...>(type_list<>{}));
449
450
466template <typename replace_t, std::ptrdiff_t i, typename ...pack_t>
468 requires (i >= 0 && i < size<pack_t...>)
470using replace_at = decltype(detail::replace_at<replace_t, i, pack_t...>(std::make_index_sequence<size<pack_t...>>{}));
471
473
474} // namespace seqan3::pack_traits
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: traits.hpp:470
take< size< pack_t... > - i, pack_t... > drop_last
Return a seqan3::type_list of the types the type pack, except the last n.
Definition: traits.hpp:428
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: traits.hpp:322
typename decltype((std::type_identity< pack_t >{},...))::type back
Return the last type from the type pack.
Definition: traits.hpp:297
constexpr ptrdiff_t find
Get the index of the first occurrence of a type in a pack.
Definition: traits.hpp:187
constexpr ptrdiff_t count
Count the occurrences of a type in a pack.
Definition: traits.hpp:169
typename decltype(detail::at< idx, pack_t... >())::type at
Return the type at given index from the type pack.
Definition: traits.hpp:256
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: traits.hpp:448
typename decltype(detail::front< pack_t... >())::type front
Return the first type from the type pack.
Definition: traits.hpp:275
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: traits.hpp:388
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:151
drop< size< pack_t... > - i, pack_t... > take_last
Return a seqan3::type_list of the last n types in the type pack.
Definition: traits.hpp:408
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: traits.hpp:368
constexpr ptrdiff_t find_if
Get the index of the first type in a pack that satisfies the given predicate.
Definition: traits.hpp:210
constexpr bool contains
Whether a type occurs in a pack or not.
Definition: traits.hpp:228
Namespace containing traits for working on type packs.
Type that contains multiple types.
Definition: type_list.hpp:29
Provides seqan3::type_list.
The <type_traits> header from C++20's standard library.