SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
type_reduce.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 <string_view>
16 
18 #include <seqan3/range/concept.hpp>
20 #include <seqan3/std/concepts>
21 #include <seqan3/std/ranges>
22 #include <seqan3/std/span>
23 
24 namespace seqan3::detail
25 {
26 
27 // ============================================================================
28 // type_reduce_fn (adaptor definition)
29 // ============================================================================
30 
33 class type_reduce_fn : public adaptor_base<type_reduce_fn>
34 {
35 private:
37  using base_t = adaptor_base<type_reduce_fn>;
38 
39 public:
41  using base_t::base_t;
42 
43 private:
45  friend base_t;
46 
50  template <std::ranges::range urng_t>
51  static constexpr auto impl(urng_t && urange)
52  {
53  static_assert(std::ranges::viewable_range<urng_t>,
54  "The views::type_reduce adaptor can only be passed viewable_ranges, i.e. Views or &-to-non-View.");
55 
56  // views are always passed as-is
57  if constexpr (std::ranges::view<std::remove_cvref_t<urng_t>>)
58  {
59  return std::views::all(std::forward<urng_t>(urange));
60  }
61  // string const &
62  else if constexpr (is_type_specialisation_of_v<std::remove_cvref_t<urng_t>, std::basic_string> &&
64  {
65  return std::basic_string_view{std::ranges::data(urange), std::ranges::size(urange)};
66  }
67  // contiguous
68  else if constexpr (std::ranges::borrowed_range<urng_t> &&
69  std::ranges::contiguous_range<urng_t> &&
70  std::ranges::sized_range<urng_t>)
71  {
72  return std::span{std::ranges::data(urange), std::ranges::size(urange)};
73  }
74  // random_access
75  else if constexpr (std::ranges::borrowed_range<urng_t> &&
76  std::ranges::random_access_range<urng_t> &&
77  std::ranges::sized_range<urng_t>)
78  {
79  return std::ranges::subrange<std::ranges::iterator_t<urng_t>, std::ranges::iterator_t<urng_t>>
80  {
81  std::ranges::begin(urange),
82  std::ranges::begin(urange) + std::ranges::size(urange),
83  std::ranges::size(urange)
84  };
85  }
86  // pass to std::views::all (will return ref-view)
87  else
88  {
89  return std::views::all(std::forward<urng_t>(urange));
90  }
91  }
92 };
93 
94 } // namespace seqan3::detail
95 
96 // ============================================================================
97 // views::type_reduce (adaptor instance definition)
98 // ============================================================================
99 
100 namespace seqan3::views
101 {
102 
158 inline constexpr auto type_reduce = detail::type_reduce_fn{};
159 
161 
162 } // namespace seqan3::views
163 
164 namespace seqan3
165 {
167 template <typename t>
168 using type_reduce_view = decltype(views::type_reduce(std::declval<t>()));
169 }
seqan3::views
The SeqAn namespace for views.
Definition: view_iota_simd.hpp:218
span
Provides std::span from the C++20 standard library.
std::basic_string
std::basic_string_view
template_inspection.hpp
Provides seqan3::type_list and auxiliary type traits.
seqan3::views::type_reduce
constexpr auto type_reduce
A view adaptor that behaves like std::views::all, but type erases certain ranges.
Definition: type_reduce.hpp:158
concepts
The Concepts library.
concept.hpp
Additional non-standard concepts for ranges.
seqan3
The main SeqAn3 namespace.
Definition: aligned_sequence_concept.hpp:29
seqan3::pack_traits::size
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:116
ranges
Adaptations of concepts from the Ranges TS.
std::remove_reference_t
std::is_const_v
T is_const_v
std::ranges::begin
T begin(T... args)
std::remove_cvref_t
seqan3::type_reduce_view
decltype(views::type_reduce(std::declval< t >())) type_reduce_view
Deduces the return value of seqan3::views::type_reduce.
Definition: type_reduce.hpp:168
detail.hpp
Auxiliary header for the views submodule .
string_view