Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] add list_traits::repeat #2899

Merged
merged 1 commit into from
Nov 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions include/seqan3/utility/type_list/traits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ template <ptrdiff_t idx,
typename ...pack1_t>
pack_traits::split_after<idx, pack1_t...> split_after(type_list<pack1_t...>);

/*!\brief Implementation for seqan3::list_traits::repeat.
* \tparam count The number of repititions.
* \tparam t The type to repeat
* \ingroup utility_type_list
*/
template <size_t count, typename t>
auto repeat()
{
if constexpr (count == 0)
return type_list<>{};
else if constexpr (count == 1)
return type_list<t>{};
else if constexpr (count == 2)
return type_list<t,t>{};
else if constexpr (count == 3)
return type_list<t,t,t>{};
else if constexpr (count == 4)
return type_list<t,t,t,t>{};
else if constexpr (count == 5)
return type_list<t,t,t,t,t>{};
else
return concat(repeat<5, t>(), repeat<count - 5, t>());
}

/*!\brief Implementation for seqan3::list_traits::replace_at.
* \tparam replace_t The type replacing the old one.
* \tparam idx The index of the type to replace.
Expand Down Expand Up @@ -491,6 +515,20 @@ template <typename replace_t, std::ptrdiff_t i, typename list_t>
//!\endcond
using replace_at = decltype(detail::replace_at<replace_t, i>(list_t{}));

/*!\brief Create a type list with the given type repeated `count` times..
* \tparam count The number of repititions.
* \tparam t The type to repeat
* \ingroup utility_type_list
*
* ### (Compile-time) Complexity
*
* * Number of template instantiations: O(n)
* * Other operations: O(n)
*
* However, with a constant of 0.2, e.g. repeat<10,int> results in 2-3 instantiations.
*/
template <size_t count, typename t>
using repeat = decltype(detail::repeat<count, t>());
//!\}

} // namespace seqan3::list_traits
8 changes: 8 additions & 0 deletions test/unit/utility/type_list/type_list_traits_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ TEST(list_traits, replace_at)
(seqan3::type_list<int, float, double>));
}

TEST(list_traits, repeat)
{
EXPECT_SAME_TYPE((seqan3::list_traits::repeat<0, int>), (seqan3::type_list<>));
EXPECT_SAME_TYPE((seqan3::list_traits::repeat<1, int>), (seqan3::type_list<int>));
EXPECT_SAME_TYPE((seqan3::list_traits::repeat<5, int>), (seqan3::type_list<int, int, int, int, int>));
EXPECT_SAME_TYPE((seqan3::list_traits::repeat<7, int>), (seqan3::type_list<int, int, int, int, int, int, int>));
}

TEST(list_traits_detail, reverse)
{
auto reverse = [] (auto && type_list)
Expand Down