-
Notifications
You must be signed in to change notification settings - Fork 10
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
5 changed files
with
124 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// | ||
// Copyright (C) 2014-2017 Sebastian Jeckel. | ||
// Copyright (C) 2020-2023 Krylov Yaroslav. | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
|
||
#ifndef UREACT_ADAPTOR_ZIP_TRANSFORM_HPP | ||
#define UREACT_ADAPTOR_ZIP_TRANSFORM_HPP | ||
|
||
#include <ureact/detail/adaptor.hpp> | ||
#include <ureact/detail/zip_base.hpp> | ||
|
||
UREACT_BEGIN_NAMESPACE | ||
|
||
namespace detail | ||
{ | ||
|
||
struct ZipTransformAdaptor : Adaptor | ||
{ | ||
/*! | ||
* @brief Emit an invoke result for func(e1,…,eN) for each complete set of values for sources 1...N | ||
* | ||
* Each source slot has its own unbounded buffer queue that persistently stores incoming events. | ||
* For as long as all queues are not empty, one value is popped from each and emitted together as a tuple. | ||
* | ||
* Semantically equivalent of ranges::zip_transform | ||
*/ | ||
template <typename F, typename Source, typename... Sources> | ||
UREACT_WARN_UNUSED_RESULT constexpr auto operator()( | ||
F&& func, const events<Source>& source1, const events<Sources>&... sources ) const | ||
{ | ||
static_assert( sizeof...( Sources ) >= 1, "zip: 2+ arguments are required" ); | ||
|
||
using node_type = event_zip_node<F, Source, Sources...>; | ||
using E = typename node_type::E; | ||
|
||
const context& context = source1.get_context(); | ||
return detail::create_wrapped_node<events<E>, node_type>( | ||
context, std::forward<F>( func ), source1, sources... ); | ||
} | ||
}; | ||
|
||
} // namespace detail | ||
|
||
inline constexpr detail::ZipTransformAdaptor zip_transform; | ||
|
||
UREACT_END_NAMESPACE | ||
|
||
#endif //UREACT_ADAPTOR_ZIP_TRANSFORM_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
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,37 @@ | ||
// | ||
// Copyright (C) 2020-2023 Krylov Yaroslav. | ||
// | ||
// Distributed under the Boost Software License, Version 1.0. | ||
// (See accompanying file LICENSE_1_0.txt or copy at | ||
// http://www.boost.org/LICENSE_1_0.txt) | ||
// | ||
#include "ureact/adaptor/zip_transform.hpp" | ||
|
||
#include "catch2_extra.hpp" | ||
#include "ureact/adaptor/collect.hpp" | ||
#include "ureact/events.hpp" | ||
|
||
// zip 3 event sources into 1 | ||
// based on example https://en.cppreference.com/w/cpp/ranges/zip_transform_view | ||
TEST_CASE( "ureact::zip_transform" ) | ||
{ | ||
ureact::context ctx; | ||
|
||
auto v1 = ureact::make_source<float>( ctx ); | ||
auto v2 = ureact::make_source<short>( ctx ); | ||
auto v3 = ureact::make_source<int>( ctx ); | ||
|
||
const auto add = []( auto a, auto b, auto c ) { return a + b + c; }; | ||
|
||
ureact::events<float> src = ureact::zip_transform( add, v1, v2, v3 ); | ||
|
||
const auto result = ureact::collect<std::vector>( src ); | ||
|
||
// clang-format off | ||
v1 << 1 << 2 << 3; | ||
v2 << 1 << 2 << 3 << 4; | ||
v3 << 1 << 2 << 3 << 4 << 5; | ||
// clang-format on | ||
|
||
CHECK( result.get() == std::vector<float>{ 3, 6, 9 } ); | ||
} |