BioC++ core-0.7.0
The Modern C++ libraries for Bioinformatics.
 
Loading...
Searching...
No Matches
to.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2022 deCODE Genetics
3// Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
4// Copyright (c) 2016-2022, 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 <algorithm>
17#include <ranges>
18
20
21namespace bio::ranges::detail
22{
23
26template <typename container_t>
27struct to_fn
28{
29private:
35 auto impl(std::ranges::range auto && rng, container_t & container) const
36 {
38 }
39
45 auto impl(std::ranges::range auto && rng, container_t & container) const
46 requires std::ranges::range<std::decay_t<decltype(*rng.begin())>>
47 {
48 auto adapter = to_fn<typename container_t::value_type>{};
49 auto inner_rng = rng | std::views::transform(adapter);
50 std::ranges::copy(inner_rng, std::back_inserter(container));
51 }
52
53public:
60 template <std::ranges::range rng_t, typename... args_t>
61 constexpr auto operator()(rng_t && rng, args_t &&... args) const
62 {
63 auto r = container_t(std::forward<args_t>(args)...);
64
65 // reserve memory if functionality is available
66 if constexpr (std::ranges::sized_range<rng_t> && requires(container_t c) { c.reserve(std::size_t{}); })
67 {
68 r.reserve(std::ranges::size(rng));
69 }
70
71 impl(std::forward<rng_t>(rng), r);
72 return r;
73 }
74};
75
80template <template <typename...> typename container_t>
81struct to_template_template_fn
82{
89 template <std::ranges::range rng_t, typename... args_t>
90 constexpr auto operator()(rng_t && rng, args_t &&... args) const
91 {
92 using value_t = std::decay_t<decltype(*rng.begin())>;
93 auto adapter = to_fn<container_t<value_t>>{};
94 return adapter(std::forward<rng_t>(rng), std::forward<args_t>(args)...);
95 }
96};
97
98} //namespace bio::ranges::detail
99
100namespace bio::ranges
101{
102
113template <typename container_t, typename... args_t>
114constexpr auto to(args_t &&... args)
115{
116 return detail::adaptor_from_functor{detail::to_fn<container_t>{}, std::forward<args_t>(args)...};
117}
118
120template <template <typename...> typename container_t, typename... args_t>
121constexpr auto to(args_t &&... args)
122{
123 return detail::adaptor_from_functor{detail::to_template_template_fn<container_t>{}, std::forward<args_t>(args)...};
124}
125
127template <typename container_t, std::ranges::range rng_t, typename... args_t>
128constexpr auto to(rng_t && rng, args_t &&... args)
129{
130 return detail::adaptor_from_functor{detail::to_fn<container_t>{},
131 std::forward<args_t>(args)...}(std::forward<rng_t>(rng));
132}
133
135template <template <typename...> typename container_t, std::ranges::range rng_t, typename... args_t>
136constexpr auto to(rng_t && rng, args_t &&... args)
137{
138 return detail::adaptor_from_functor{detail::to_template_template_fn<container_t>{},
139 std::forward<args_t>(args)...}(std::forward<rng_t>(rng));
140}
141
142} // namespace bio::ranges
T back_inserter(T... args)
T copy(T... args)
constexpr auto to(args_t &&... args)
Converts a range to a container.
Definition: to.hpp:114
The ranges module's namespace.
Auxiliary header for the views submodule .