From 650c363f034aea5d8f6ac0b4e0f71790add10910 Mon Sep 17 00:00:00 2001 From: szdytom Date: Wed, 19 Jul 2023 17:08:59 +0800 Subject: [PATCH] [libvmake]add gourp() --- libvmake/examples/grouping.cpp | 9 ++++++ libvmake/vmake.hpp | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 libvmake/examples/grouping.cpp diff --git a/libvmake/examples/grouping.cpp b/libvmake/examples/grouping.cpp new file mode 100644 index 0000000..801e49d --- /dev/null +++ b/libvmake/examples/grouping.cpp @@ -0,0 +1,9 @@ +#include "../vmake.hpp" +using namespace std; + +int main() { + vmake::outputln_n(cout, 5, " ", vmake::transform(vmake::group<2>(vmake::iota(1)), [](auto x) { + return get<0>(x) * get<1>(x); + })); + return 0; +} diff --git a/libvmake/vmake.hpp b/libvmake/vmake.hpp index a5d1037..7a48697 100644 --- a/libvmake/vmake.hpp +++ b/libvmake/vmake.hpp @@ -401,6 +401,64 @@ inline filteror::type, typename std::decay::type> return {std::forward(g), std::forward(p)}; } +namespace details { + +template +struct repeat_tuple { + static_assert(n > 0, ""); + using type = decltype(std::tuple_cat(std::declval>() + , std::declval::type>())); +}; + +template +struct repeat_tuple { + using type = std::tuple; +}; + +template +struct group_helper { + static auto load(Gen &g) { + auto x = g(); + auto y = group_helper::load(g); + return std::tuple_cat(std::make_tuple(std::move(x)), std::move(y)); + } +}; + +template +struct group_helper { + static auto load(Gen &g) { + return std::make_tuple(g()); + } +}; + +template +struct grouper { + static_assert(n > 0, ""); + using result = typename repeat_tuple::type; + using core = Gen; + + Gen g; + + grouper(grouper&& g) = default; + grouper(const grouper& g) = default; + grouper(Gen &&g) : g(std::forward(g)) {} + + bool is_terminated() noexcept { + return g.is_terminated(); + } + + result operator()() { + return group_helper::load(g); + } +}; + +} + +template +inline auto group(Gen &&g) { + return details::grouper::type, n>(std::forward(g)); +} + namespace rng { inline auto make_seed() {