BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
type_reduce.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2022 deCODE Genetics
3// Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
4// Copyright (c) 2016-2020, Knut Reinert & MPI für molekulare Genetik
5// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
6// shipped with this file and also available at: https://github.com/biocpp/biocpp-core/blob/main/LICENSE.md
7// -----------------------------------------------------------------------------------------------------
8
14#pragma once
15
16#include <concepts>
17#include <ranges>
18#include <span>
19#include <string_view>
20
24
25namespace bio::ranges::detail
26{
27
28// ============================================================================
29// type_reduce_fn (adaptor definition)
30// ============================================================================
31
34class type_reduce_fn : public adaptor_base<type_reduce_fn>
35{
36private:
38 using base_t = adaptor_base<type_reduce_fn>;
39
40public:
42 using base_t::base_t;
43
44private:
46 friend base_t;
47
51 template <std::ranges::range urng_t>
52 static constexpr auto impl(urng_t && urange)
53 {
54 static_assert(std::ranges::viewable_range<urng_t>,
55 "The views::type_reduce adaptor can only be passed viewable_ranges, i.e. Views or "
56 "&-to-non-View.");
57
58 // string const &
59 if constexpr (std::ranges::borrowed_range<urng_t> && std::ranges::contiguous_range<urng_t> &&
60 std::ranges::sized_range<urng_t> &&
61 std::same_as<std::ranges::range_reference_t<urng_t>, char const &>)
62 {
63 return std::basic_string_view{std::ranges::data(urange), std::ranges::size(urange)};
64 }
65 // contiguous
66 else if constexpr (std::ranges::borrowed_range<urng_t> && std::ranges::contiguous_range<urng_t> &&
67 std::ranges::sized_range<urng_t>)
68 {
69 return std::span{std::ranges::data(urange), std::ranges::size(urange)};
70 }
71 // random_access
72 else if constexpr (std::ranges::borrowed_range<urng_t> && std::ranges::random_access_range<urng_t> &&
73 std::ranges::sized_range<urng_t>)
74 {
75 return std::ranges::subrange<std::ranges::iterator_t<urng_t>, std::ranges::iterator_t<urng_t>>{
76 std::ranges::begin(urange),
77 std::ranges::begin(urange) + std::ranges::size(urange),
78 std::ranges::size(urange)};
79 }
80 // pass to std::views::all (will return ref-view)
81 else
82 {
83 return std::views::all(std::forward<urng_t>(urange));
84 }
85 }
86};
87
88} // namespace bio::ranges::detail
89
90// ============================================================================
91// views::type_reduce (adaptor instance definition)
92// ============================================================================
93
94namespace bio::ranges::views
95{
96
152inline constexpr auto type_reduce = detail::type_reduce_fn{};
153
155
156} // namespace bio::ranges::views
157
158namespace bio::ranges
159{
161template <typename t>
162using type_reduce_view = decltype(views::type_reduce(std::declval<t>()));
163} // namespace bio::ranges
T begin(T... args)
constexpr auto type_reduce
A view adaptor that behaves like std::views::all, but type erases certain ranges.
Definition: type_reduce.hpp:152
The BioC++ namespace for views.
The ranges module's namespace.
decltype(views::type_reduce(std::declval< t >())) type_reduce_view
Deduces the return value of bio::views::type_reduce.
Definition: type_reduce.hpp:162
Additional non-standard concepts for ranges.
Auxiliary header for the views submodule .
Provides various traits to inspect templates.