SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
type_reduce.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 <concepts>
13#include <ranges>
14#include <span>
15#include <string_view>
16
19
20namespace seqan3::detail
21{
22
23// ============================================================================
24// type_reduce_fn (adaptor definition)
25// ============================================================================
26
29class type_reduce_fn : public adaptor_base<type_reduce_fn>
30{
31private:
33 using base_t = adaptor_base<type_reduce_fn>;
34
35public:
37 using base_t::base_t;
38
39private:
41 friend base_t;
42
46 template <std::ranges::range urng_t>
47 static constexpr auto impl(urng_t && urange)
48 {
49 static_assert(
50 std::ranges::viewable_range<urng_t>,
51 "The views::type_reduce adaptor can only be passed viewable_ranges, i.e. Views or &-to-non-View.");
52
53 // string_view
54 if constexpr (is_type_specialisation_of_v<std::remove_cvref_t<urng_t>, std::basic_string_view>)
55 {
56 return std::views::all(std::forward<urng_t>(urange));
57 }
58 // string const &
59 else if constexpr (is_type_specialisation_of_v<std::remove_cvref_t<urng_t>, std::basic_string>
60 && std::is_const_v<std::remove_reference_t<urng_t>>)
61 {
62 return std::basic_string_view{std::ranges::data(urange), std::ranges::size(urange)};
63 }
64 // contiguous
65 else if constexpr (std::ranges::borrowed_range<urng_t> && std::ranges::contiguous_range<urng_t>
66 && std::ranges::sized_range<urng_t>)
67 {
68 return std::span{std::ranges::data(urange), std::ranges::size(urange)};
69 }
70 // random_access
71 else if constexpr (std::ranges::borrowed_range<urng_t> && std::ranges::random_access_range<urng_t>
72 && std::ranges::sized_range<urng_t>)
73 {
74 return std::ranges::subrange<std::ranges::iterator_t<urng_t>, std::ranges::iterator_t<urng_t>>{
75 std::ranges::begin(urange),
76 std::ranges::begin(urange) + std::ranges::size(urange),
77 std::ranges::size(urange)};
78 }
79 // pass to std::views::all (will return a view or ref-view)
80 else
81 {
82 return std::views::all(std::forward<urng_t>(urange));
83 }
84 }
85};
86
87} // namespace seqan3::detail
88
89// ============================================================================
90// views::type_reduce (adaptor instance definition)
91// ============================================================================
92
93namespace seqan3::views
94{
147inline constexpr auto type_reduce = detail::type_reduce_fn{};
148
149} // namespace seqan3::views
150
151namespace seqan3
152{
158template <typename t>
159using type_reduce_t = decltype(views::type_reduce(std::declval<t>()));
160} // namespace seqan3
Provides seqan3::detail::adaptor_base and seqan3::detail::combined_adaptor.
T begin(T... args)
The SeqAn namespace for views.
Definition char_strictly_to.hpp:19
The main SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
Provides type traits for working with templates.
Hide me