SeqAn3 3.2.0-rc.1
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
19
20// ----------------------------------------------------------------------------
21// seqan3::list_traits::detail
22// ----------------------------------------------------------------------------
23
24namespace seqan3::list_traits::detail
25{
26
32template <ptrdiff_t idx, typename ...pack_t>
33std::type_identity<seqan3::pack_traits::at<idx, pack_t...>> at(type_list<pack_t...>);
34
39template <typename ...pack_t>
40std::type_identity<seqan3::pack_traits::front<pack_t...>> front(type_list<pack_t...>);
41
46template <typename ...pack_t>
47std::type_identity<seqan3::pack_traits::back<pack_t...>> back(type_list<pack_t...>);
48
54template <typename ...pack1_t,
55 typename ...pack2_t>
56type_list<pack1_t..., pack2_t...> concat(type_list<pack1_t...>, type_list<pack2_t...>);
57
64template <typename ...pack1_t,
65 typename ...pack2_t,
66 typename ...more_lists_t>
67auto concat(type_list<pack1_t...>, type_list<pack2_t...>, more_lists_t ...)
68{
69 return concat(type_list<pack1_t..., pack2_t...>{}, more_lists_t{}...);
70}
71
76template <typename ...pack_t>
77pack_traits::drop_front<pack_t...> drop_front(type_list<pack_t...>);
78
84template <template <typename> typename trait_t, typename ...pack_t>
85pack_traits::transform<trait_t, pack_t...> transform(type_list<pack_t...>);
86
92template <ptrdiff_t idx,
93 typename ...pack1_t>
94pack_traits::split_after<idx, pack1_t...> split_after(type_list<pack1_t...>);
95
101template <size_t count, typename t>
102auto repeat()
103{
104 if constexpr (count == 0)
105 return type_list<>{};
106 else if constexpr (count == 1)
107 return type_list<t>{};
108 else if constexpr (count == 2)
109 return type_list<t,t>{};
110 else if constexpr (count == 3)
111 return type_list<t,t,t>{};
112 else if constexpr (count == 4)
113 return type_list<t,t,t,t>{};
114 else if constexpr (count == 5)
115 return type_list<t,t,t,t,t>{};
116 else
117 return concat(repeat<5, t>(), repeat<count - 5, t>());
118}
119
126template <typename replace_t,
127 ptrdiff_t idx,
128 typename ...pack_t>
129pack_traits::replace_at<replace_t, idx, pack_t...> replace_at(type_list<pack_t...>);
130
132inline constexpr type_list<> reverse(type_list<>) { return {}; }
133
135template <typename head_t, typename ...pack_t>
136auto reverse(type_list<head_t, pack_t...>)
137{
138 return concat(reverse(type_list<pack_t...>{}), type_list<head_t>{});
139}
140
142template <typename ...current_list_t>
143constexpr seqan3::type_list<current_list_t...> type_list_difference(seqan3::type_list<current_list_t...>, seqan3::type_list<>)
144{ return {}; }
145
147template <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
175namespace seqan3::list_traits
176{
177
183template <typename list_t>
184 requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
185inline constexpr size_t size = 0;
187
194template <typename ...pack_t>
195inline constexpr size_t size<type_list<pack_t...>> = sizeof...(pack_t);
196
198template <typename query_t, typename list_t>
199 requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
200inline constexpr ptrdiff_t count = -1;
202
209template <typename query_t, typename ...pack_t>
210inline constexpr ptrdiff_t count<query_t, type_list<pack_t...>> =
211 seqan3::pack_traits::count<query_t, pack_t...>;
212
214template <typename query_t, typename list_t>
215 requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
216inline constexpr ptrdiff_t find = -1;
218
225template <typename query_t, typename ...pack_t>
226inline constexpr ptrdiff_t find<query_t, type_list<pack_t...>> =
227 seqan3::pack_traits::detail::find<query_t, pack_t...>();
228
230template <template <typename> typename pred_t, typename list_t>
231 requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
232inline constexpr ptrdiff_t find_if = -1;
234
241template <template <typename> typename pred_t, typename ...pack_t>
242inline constexpr ptrdiff_t find_if<pred_t, type_list<pack_t...>> =
243 seqan3::pack_traits::detail::find_if<pred_t, pack_t...>();
244
251template <typename query_t, typename list_t>
253 requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
255inline constexpr bool contains = (find<query_t, list_t> != -1);
256
258
279template <ptrdiff_t idx, typename list_t>
281 requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) &&
282 ((idx >= 0 && idx < size<list_t>) || (-idx <= size<list_t>))
284using at = typename decltype(detail::at<idx>(list_t{}))::type;
285
299template <typename list_t>
301 requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (size<list_t> > 0)
303using front = typename decltype(detail::front(list_t{}))::type;
304
321template <typename list_t>
323 requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (size<list_t> > 0)
325using back = typename decltype(detail::back(list_t{}))::type;
326
328
349template <typename ...lists_t>
351 requires (seqan3::detail::template_specialisation_of<lists_t, seqan3::type_list> && ...)
353using concat = decltype(detail::concat(lists_t{}...));
354
368template <typename list_t>
370 requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (size<list_t> > 0)
372using drop_front = decltype(detail::drop_front(list_t{}));
373
388template <ptrdiff_t i, typename list_t>
390 requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
392using take = typename decltype(detail::split_after<i>(list_t{}))::first_type;
393
408template <ptrdiff_t i, typename list_t>
410 requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
412using drop = typename decltype(detail::split_after<i>(list_t{}))::second_type;
413
428template <ptrdiff_t i, typename list_t>
430 requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
432using take_last = drop<size<list_t> - i, list_t>;
433
448template <ptrdiff_t i, typename list_t>
450 requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
452using drop_last = take<size<list_t> - i, list_t>;
453
468template <ptrdiff_t i, typename list_t>
470 requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
472using split_after = decltype(detail::split_after<i>(list_t{}));
473
491template <template <typename> typename trait_t, typename list_t>
493 requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
495using transform = decltype(detail::transform<trait_t>(list_t{}));
496
512template <typename replace_t, std::ptrdiff_t i, typename list_t>
514 requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i < size<list_t>)
516using replace_at = decltype(detail::replace_at<replace_t, i>(list_t{}));
517
530template <size_t count, typename t>
531using repeat = decltype(detail::repeat<count, t>());
533
534} // namespace seqan3::list_traits
decltype(detail::split_after< i >(list_t{})) split_after
Split a seqan3::type_list into two parts returned as a pair of seqan3::type_list.
Definition: traits.hpp:472
typename decltype(detail::split_after< i >(list_t{}))::first_type take
Return a seqan3::type_list of the first n types in the input type list.
Definition: traits.hpp:392
decltype(detail::concat(lists_t{}...)) concat
Join two seqan3::type_list s into one.
Definition: traits.hpp:353
constexpr bool contains
Whether a type occurs in a type list or not.
Definition: traits.hpp:255
drop< size< list_t > - i, list_t > take_last
Return a seqan3::type_list of the last n types in the input type list.
Definition: traits.hpp:432
typename decltype(detail::front(list_t{}))::type front
Return the first type from the type list.
Definition: traits.hpp:303
take< size< list_t > - i, list_t > drop_last
Return a seqan3::type_list of the types the input type list, except the last n.
Definition: traits.hpp:452
decltype(detail::replace_at< replace_t, i >(list_t{})) replace_at
Replace the type at the given index with the given type.
Definition: traits.hpp:516
decltype(detail::repeat< count, t >()) repeat
Create a type list with the given type repeated count times..
Definition: traits.hpp:531
typename decltype(detail::split_after< i >(list_t{}))::second_type drop
Return a seqan3::type_list of the types in the input type list, except the first n.
Definition: traits.hpp:412
typename decltype(detail::at< idx >(list_t{}))::type at
Return the type at given index from the type list.
Definition: traits.hpp:284
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: traits.hpp:495
typename decltype(detail::back(list_t{}))::type back
Return the last type from the type list.
Definition: traits.hpp:325
decltype(detail::drop_front(list_t{})) drop_front
Return a seqan3::type_list of all the types in the type list, except the first.
Definition: traits.hpp:372
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
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
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:151
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: traits.hpp:342
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.
The <type_traits> header from C++20's standard library.