-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
33 changed files
with
511 additions
and
111 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#include <deque> | ||
#include <forward_list> | ||
#include <list> | ||
#include <vector> | ||
|
||
#include <seqan3/utility/range/to.hpp> | ||
|
||
int main() | ||
{ | ||
auto lst = std::views::iota(1, 10); // some range over the numbers 1-10 | ||
|
||
// convert range to vector using pipe syntax | ||
auto vec0 = lst | seqan3::ranges::to<std::vector<int>>(); | ||
static_assert(std::same_as<decltype(vec0), std::vector<int>>); | ||
|
||
// convert range to vector but auto deducing the element type | ||
auto vec1 = lst | seqan3::ranges::to<std::vector>(); | ||
static_assert(std::same_as<decltype(vec1), std::vector<int>>); | ||
|
||
// convert range to vector using function call syntax | ||
auto vec2 = seqan3::ranges::to<std::vector<int>>(lst); | ||
static_assert(std::same_as<decltype(vec2), std::vector<int>>); | ||
|
||
// using function call syntax and auto deducing element type | ||
auto vec3 = seqan3::ranges::to<std::vector>(lst); | ||
static_assert(std::same_as<decltype(vec3), std::vector<int>>); | ||
|
||
// convert nested ranges into nested containers | ||
auto nested_lst = std::list<std::forward_list<int>>{{1, 2, 3}, {4, 5, 6, 7}}; | ||
auto vec4 = nested_lst | seqan3::ranges::to<std::vector<std::vector<int>>>(); | ||
static_assert(std::same_as<decltype(vec4), std::vector<std::vector<int>>>); | ||
|
||
// different supported container types | ||
auto vec5 = lst | seqan3::ranges::to<std::list>(); | ||
static_assert(std::same_as<decltype(vec5), std::list<int>>); | ||
|
||
auto vec6 = lst | seqan3::ranges::to<std::deque>(); | ||
static_assert(std::same_as<decltype(vec6), std::deque<int>>); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,3 +79,4 @@ | |
#pragma once | ||
|
||
#include <seqan3/utility/range/concept.hpp> | ||
#include <seqan3/utility/range/to.hpp> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
// ----------------------------------------------------------------------------------------------------- | ||
// Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin | ||
// Copyright (c) 2016-2022, Knut Reinert & MPI für molekulare Genetik | ||
// This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License | ||
// shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md | ||
// ----------------------------------------------------------------------------------------------------- | ||
|
||
/*!\file | ||
* \author Simon Gene Gottlieb <simon.gottlieb AT fu-berlin.de> | ||
* \brief Provides seqan3::ranges::to. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <algorithm> | ||
#include <seqan3/std/ranges> | ||
|
||
#include <seqan3/core/range/detail/adaptor_from_functor.hpp> | ||
#include <seqan3/utility/concept.hpp> | ||
|
||
namespace seqan3::detail | ||
{ | ||
|
||
//!\brief Function object to convert a std::ranges::input_range to a fully defined container. | ||
template <typename container_t> | ||
requires (!std::ranges::view<container_t>) | ||
struct to_fn | ||
{ | ||
private: | ||
/*!\brief Copies a range into a container. | ||
* \tparam rng_t Type of the range. | ||
* \tparam container_t Type of the target container. | ||
*/ | ||
template <std::ranges::input_range rng_t> | ||
requires std::convertible_to<std::ranges::range_reference_t<rng_t>, std::ranges::range_value_t<container_t>> | ||
auto impl(rng_t && rng, container_t & container) const | ||
{ | ||
std::ranges::copy(rng, std::back_inserter(container)); | ||
} | ||
|
||
//!\overload | ||
template <std::ranges::input_range rng_t> | ||
auto impl(rng_t && rng, container_t & container) const | ||
requires std::ranges::input_range<std::ranges::range_value_t<rng_t>> | ||
{ | ||
auto adaptor = to_fn<std::ranges::range_value_t<container_t>>{}; | ||
auto inner_rng = rng | std::views::transform(adaptor); | ||
std::ranges::copy(inner_rng, std::back_inserter(container)); | ||
} | ||
|
||
public: | ||
/*!\brief Converts a template-template into a container. | ||
* \tparam rng_t The type of the range being processed. | ||
* \tparam args_t The types of the arguments for the constructor. | ||
* \param rng The range being processed. | ||
* \param args Arguments to pass to the constructor of the container. | ||
*/ | ||
template <std::ranges::input_range rng_t, typename... args_t> | ||
constexpr auto operator()(rng_t && rng, args_t &&... args) const | ||
{ | ||
auto new_container = container_t(std::forward<args_t>(args)...); | ||
|
||
// reserve memory if functionality is available | ||
if constexpr (std::ranges::sized_range<rng_t> && requires (container_t c) { c.reserve(size_t{}); }) | ||
new_container.reserve(std::ranges::size(rng)); | ||
|
||
impl(std::forward<rng_t>(rng), new_container); | ||
return new_container; | ||
} | ||
}; | ||
|
||
/*!\brief Similar to to_fn, but accepts a template-template as argument, | ||
* e.g.: to_fn<vector> instead of to_fn<vector<int>>. | ||
*/ | ||
template <template <typename> typename container_t> | ||
struct to_template_template_fn | ||
{ | ||
/*!\brief Converts a template-template into a container. | ||
* \tparam rng_t The type of the range being processed. | ||
* \tparam args_t The types of the arguments for the constructor. | ||
* \param rng The range being processed. | ||
* \param args Arguments to pass to the constructor of the container. | ||
*/ | ||
template <std::ranges::input_range rng_t, typename... args_t> | ||
constexpr auto operator()(rng_t && rng, args_t &&... args) const | ||
{ | ||
auto adaptor = to_fn<container_t<std::ranges::range_value_t<rng_t>>>{}; | ||
return adaptor(std::forward<rng_t>(rng), std::forward<args_t>(args)...); | ||
} | ||
}; | ||
|
||
} // namespace seqan3::detail | ||
|
||
namespace seqan3::ranges | ||
{ | ||
|
||
/*!\brief Converts a range to a container. | ||
* \ingroup utility_range | ||
* \details | ||
* To convert a range to a container, either the "pipe syntax" or the "function call" syntax can be used. | ||
* Both syntaxes support the explicit specification of the target container or | ||
* a specification with a deduced value type. | ||
* | ||
* Currently supported containers are: | ||
* - `std::vector` | ||
* - `std::list` | ||
* - `std::deque` | ||
* | ||
* \include doc/tutorial/05_ranges/to.cpp | ||
* | ||
* \noapi{This is a implementation of the C++23 ranges::to. It will be replaced with std::ranges::to.} | ||
*/ | ||
template <typename container_t, typename... args_t> | ||
constexpr auto to(args_t &&... args) | ||
{ | ||
return detail::adaptor_from_functor{detail::to_fn<container_t>{}, std::forward<args_t>(args)...}; | ||
} | ||
|
||
/*!\brief Converts a range to a container. | ||
* \ingroup utility_range | ||
* \overload | ||
*/ | ||
template <template <typename...> typename container_t, typename... args_t> | ||
constexpr auto to(args_t &&... args) | ||
{ | ||
return detail::adaptor_from_functor{detail::to_template_template_fn<container_t>{}, std::forward<args_t>(args)...}; | ||
} | ||
|
||
/*!\brief Converts a range to a container. | ||
* \ingroup utility_range | ||
* \overload | ||
*/ | ||
template <typename container_t, std::ranges::input_range rng_t, typename... args_t> | ||
constexpr auto to(rng_t && rng, args_t &&... args) | ||
{ | ||
return detail::adaptor_from_functor{detail::to_fn<container_t>{}, | ||
std::forward<args_t>(args)...}(std::forward<rng_t>(rng)); | ||
} | ||
|
||
/*!\brief Converts a range to a container. | ||
* \ingroup utility_range | ||
* \overload | ||
*/ | ||
template <template <class...> typename container_t, std::ranges::input_range rng_t, typename... args_t> | ||
constexpr auto to(rng_t && rng, args_t &&... args) | ||
{ | ||
return detail::adaptor_from_functor{detail::to_template_template_fn<container_t>{}, | ||
std::forward<args_t>(args)...}(std::forward<rng_t>(rng)); | ||
} | ||
|
||
} // namespace seqan3::ranges | ||
|
||
namespace seqan3::views | ||
{ | ||
|
||
/*!\brief Converts a range to a container. | ||
* \ingroup utility_views | ||
* \deprecated Use seqan3::ranges::to instead. | ||
*/ | ||
template <typename container_t, typename... args_t> | ||
SEQAN3_DEPRECATED_330 constexpr auto to(args_t &&... args) | ||
{ | ||
return ranges::to<container_t>(std::forward<args_t>(args)...); | ||
} | ||
|
||
/*!\brief Converts a range to a container. | ||
* \ingroup utility_views | ||
* \deprecated Use seqan3::ranges::to instead. | ||
*/ | ||
template <template <typename...> typename container_t, typename... args_t> | ||
SEQAN3_DEPRECATED_330 constexpr auto to(args_t &&... args) | ||
{ | ||
return ranges::to<container_t>(std::forward<args_t>(args)...); | ||
} | ||
|
||
/*!\brief Converts a range to a container. | ||
* \ingroup utility_views | ||
* \deprecated Use seqan3::ranges::to instead. | ||
*/ | ||
template <typename container_t, std::ranges::input_range rng_t, typename... args_t> | ||
SEQAN3_DEPRECATED_330 constexpr auto to(rng_t && rng, args_t &&... args) | ||
{ | ||
return ranges::to<container_t>(std::forward<rng_t>(rng), std::forward<args_t>(args)...); | ||
} | ||
|
||
/*!\brief Converts a range to a container. | ||
* \ingroup utility_views | ||
* \deprecated Use seqan3::ranges::to instead. | ||
*/ | ||
template <template <typename...> typename container_t, std::ranges::input_range rng_t, typename... args_t> | ||
SEQAN3_DEPRECATED_330 constexpr auto to(rng_t && rng, args_t &&... args) | ||
{ | ||
return ranges::to<container_t>(std::forward<rng_t>(rng), std::forward<args_t>(args)...); | ||
} | ||
|
||
} // namespace seqan3::views |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.