SeqAn3  3.0.3
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 
15 #include <seqan3/std/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,
55  typename ...pack2_t>
56 type_list<pack1_t..., pack2_t...> concat(type_list<pack1_t...>, type_list<pack2_t...>);
57 
64 template <typename ...pack1_t,
65  typename ...pack2_t,
66  typename ...more_lists_t>
67 auto 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 
76 template <typename ...pack_t>
77 pack_traits::drop_front<pack_t...> drop_front(type_list<pack_t...>);
78 
84 template <template <typename> typename trait_t, typename ...pack_t>
85 pack_traits::transform<trait_t, pack_t...> transform(type_list<pack_t...>);
86 
92 template <ptrdiff_t idx,
93  typename ...pack1_t>
94 pack_traits::split_after<idx, pack1_t...> split_after(type_list<pack1_t...>);
95 
102 template <typename replace_t,
103  ptrdiff_t idx,
104  typename ...pack_t>
105 pack_traits::replace_at<replace_t, idx, pack_t...> replace_at(type_list<pack_t...>);
106 
108 inline constexpr type_list<> reverse(type_list<>) { return {}; }
109 
111 template <typename head_t, typename ...pack_t>
112 auto reverse(type_list<head_t, pack_t...>)
113 {
114  return concat(reverse(type_list<pack_t...>{}), type_list<head_t>{});
115 }
116 
118 template <typename ...current_list_t>
119 constexpr seqan3::type_list<current_list_t...> type_list_difference(seqan3::type_list<current_list_t...>, seqan3::type_list<>)
120 { return {}; }
121 
123 template <typename ...current_list_t, typename remove_t, typename ...remove_list_t>
125 {
126  constexpr auto pos = seqan3::pack_traits::find<remove_t, current_list_t...>;
127  if constexpr (pos >= 0)
128  {
129  using split_list_t = seqan3::pack_traits::split_after<pos, current_list_t...>;
130 
131  using split_list1_t = typename split_list_t::first_type;
132  using split_list2_t = decltype(drop_front(typename split_list_t::second_type{}));
133  using filtered_list_t = decltype(concat(split_list1_t{}, split_list2_t{}));
134  return type_list_difference(filtered_list_t{}, seqan3::type_list<remove_t, remove_list_t...>{});
135  }
136  else
137  {
138  // remove_t not contained in current_list_t
139  using filtered_list_t = seqan3::type_list<current_list_t...>;
140  return type_list_difference(filtered_list_t{}, seqan3::type_list<remove_list_t...>{});
141  }
142 }
143 
144 } // namespace seqan3::list_traits::detail
145 
146 // ----------------------------------------------------------------------------
147 // seqan3::list_traits
148 // ----------------------------------------------------------------------------
149 
151 namespace seqan3::list_traits
152 {
153 
159 template <typename list_t>
160  requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
161 inline constexpr size_t size = 0;
163 
170 template <typename ...pack_t>
171 inline constexpr size_t size<type_list<pack_t...>> = sizeof...(pack_t);
172 
174 template <typename query_t, typename list_t>
175  requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
176 inline constexpr ptrdiff_t count = -1;
178 
185 template <typename query_t, typename ...pack_t>
186 inline constexpr ptrdiff_t count<query_t, type_list<pack_t...>> =
187  seqan3::pack_traits::count<query_t, pack_t...>;
188 
190 template <typename query_t, typename list_t>
191  requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
192 inline constexpr ptrdiff_t find = -1;
194 
201 template <typename query_t, typename ...pack_t>
202 inline constexpr ptrdiff_t find<query_t, type_list<pack_t...>> =
203  seqan3::pack_traits::detail::find<query_t, pack_t...>();
204 
206 template <template <typename> typename pred_t, typename list_t>
207  requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
208 inline constexpr ptrdiff_t find_if = -1;
210 
217 template <template <typename> typename pred_t, typename ...pack_t>
218 inline constexpr ptrdiff_t find_if<pred_t, type_list<pack_t...>> =
219  seqan3::pack_traits::detail::find_if<pred_t, pack_t...>();
220 
227 template <typename query_t, typename list_t>
229  requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
231 inline constexpr bool contains = (find<query_t, list_t> != -1);
232 
234 
255 template <ptrdiff_t idx, typename list_t>
257  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) &&
258  ((idx >= 0 && idx < size<list_t>) || (-idx <= size<list_t>))
260 using at = typename decltype(detail::at<idx>(list_t{}))::type;
261 
275 template <typename list_t>
277  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (size<list_t> > 0)
279 using front = typename decltype(detail::front(list_t{}))::type;
280 
297 template <typename list_t>
299  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (size<list_t> > 0)
301 using back = typename decltype(detail::back(list_t{}))::type;
302 
304 
325 template <typename ...lists_t>
327  requires (seqan3::detail::template_specialisation_of<lists_t, seqan3::type_list> && ...)
329 using concat = decltype(detail::concat(lists_t{}...));
330 
344 template <typename list_t>
346  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (size<list_t> > 0)
348 using drop_front = decltype(detail::drop_front(list_t{}));
349 
364 template <ptrdiff_t i, typename list_t>
366  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
368 using take = typename decltype(detail::split_after<i>(list_t{}))::first_type;
369 
384 template <ptrdiff_t i, typename list_t>
386  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
388 using drop = typename decltype(detail::split_after<i>(list_t{}))::second_type;
389 
404 template <ptrdiff_t i, typename list_t>
406  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
408 using take_last = drop<size<list_t> - i, list_t>;
409 
424 template <ptrdiff_t i, typename list_t>
426  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
428 using drop_last = take<size<list_t> - i, list_t>;
429 
444 template <ptrdiff_t i, typename list_t>
446  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i <= size<list_t>)
448 using split_after = decltype(detail::split_after<i>(list_t{}));
449 
467 template <template <typename> typename trait_t, typename list_t>
469  requires seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>
471 using transform = decltype(detail::transform<trait_t>(list_t{}));
472 
488 template <typename replace_t, std::ptrdiff_t i, typename list_t>
490  requires (seqan3::detail::template_specialisation_of<list_t, seqan3::type_list>) && (i >= 0 && i < size<list_t>)
492 using replace_at = decltype(detail::replace_at<replace_t, i>(list_t{}));
493 
495 
496 } // 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:448
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:368
decltype(detail::concat(lists_t{}...)) concat
Join two seqan3::type_list s into one.
Definition: traits.hpp:329
constexpr bool contains
Whether a type occurs in a type list or not.
Definition: traits.hpp:231
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:408
typename decltype(detail::front(list_t{}))::type front
Return the first type from the type list.
Definition: traits.hpp:279
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:428
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:492
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:388
typename decltype(detail::at< idx >(list_t{}))::type at
Return the type at given index from the type list.
Definition: traits.hpp:260
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:471
typename decltype(detail::back(list_t{}))::type back
Return the last type from the type list.
Definition: traits.hpp:301
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:348
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 C++20 additions to the type_traits header.
Provides seqan3::type_list.
Provides various traits for template packs.