forked from NVIDIA/stdexec
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmerge.hpp
More file actions
211 lines (180 loc) · 6.83 KB
/
merge.hpp
File metadata and controls
211 lines (180 loc) · 6.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright (c) 2023 Maikel Nadolski
* Copyright (c) 2023 NVIDIA Corporation
*
* Licensed under the Apache License Version 2.0 with LLVM Exceptions
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* https://llvm.org/LICENSE.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "../../stdexec/execution.hpp"
#include "../detail/basic_sequence.hpp"
#include "../sender_for.hpp"
#include "../sequence_senders.hpp"
#include "ignore_all_values.hpp"
#include "transform_each.hpp"
STDEXEC_PRAGMA_PUSH()
STDEXEC_PRAGMA_IGNORE_EDG(not_used_in_template_function_params)
namespace experimental::execution
{
namespace __merge
{
using namespace STDEXEC;
template <class _Receiver>
struct __operation_base
{
_Receiver __rcvr_;
};
template <class _Receiver>
struct __result_receiver
{
using receiver_concept = STDEXEC::receiver_tag;
void set_value() noexcept
{
STDEXEC::set_value(static_cast<_Receiver&&>(__op_->__rcvr_));
}
template <class _Error>
void set_error(_Error&& __error) noexcept
{
STDEXEC::set_error(static_cast<_Receiver&&>(__op_->__rcvr_),
static_cast<_Error&&>(__error));
}
void set_stopped() noexcept
{
STDEXEC::set_stopped(static_cast<_Receiver&&>(__op_->__rcvr_));
}
auto get_env() const noexcept -> env_of_t<_Receiver>
{
return STDEXEC::get_env(__op_->__rcvr_);
}
__operation_base<_Receiver>* __op_;
};
template <class _Receiver>
struct __merge_each_fn
{
template <STDEXEC::sender _Item>
auto operator()(_Item&& __item, __operation_base<_Receiver>* __op) const
noexcept(__nothrow_callable<set_next_t, _Receiver&, _Item>)
-> next_sender_of_t<_Receiver, _Item>
{
return exec::set_next(__op->__rcvr_, static_cast<_Item&&>(__item));
}
};
struct __combine
{
template <class _Receiver>
using merge_each_fn_t = __closure<__merge_each_fn<_Receiver>, __operation_base<_Receiver>*>;
template <class _Sequence, class _Receiver>
using transform_sender_t =
__call_result_t<exec::transform_each_t, _Sequence, merge_each_fn_t<_Receiver>>;
template <class _Sequence, class _Receiver>
using ignored_sender_t =
__call_result_t<exec::ignore_all_values_t, transform_sender_t<_Sequence, _Receiver>>;
template <class _Receiver, class... _Sequences>
using result_sender_t =
__call_result_t<when_all_t, ignored_sender_t<_Sequences, _Receiver>...>;
};
template <class _Receiver, class... _Sequences>
struct __operation : __operation_base<_Receiver>
{
using merge_each_fn_t = __combine::merge_each_fn_t<_Receiver>;
template <class _ReceiverDependent>
using result_sender_t = __combine::result_sender_t<_ReceiverDependent, _Sequences...>;
explicit __operation(_Receiver __rcvr, _Sequences... __sequences)
: __operation_base<_Receiver>{static_cast<_Receiver&&>(__rcvr)}
, __op_result_{
STDEXEC::connect(STDEXEC::when_all(exec::ignore_all_values(
exec::transform_each(static_cast<_Sequences&&>(__sequences),
merge_each_fn_t({}, this)))...),
__result_receiver<_Receiver>{this})}
{}
void start() & noexcept
{
STDEXEC::start(__op_result_);
}
connect_result_t<result_sender_t<_Receiver>, __result_receiver<_Receiver>> __op_result_;
};
struct merge_t
{
private:
struct __subscribe_fn
{
template <class _Receiver, class... _Sequences>
auto operator()(_Receiver& __rcvr, __ignore, __ignore, _Sequences... __sequences)
noexcept(__nothrow_decay_copyable<_Sequences...>) -> __operation<_Receiver, _Sequences...>
{
return __operation<_Receiver, _Sequences...>{static_cast<_Receiver&&>(__rcvr),
static_cast<_Sequences&&>(__sequences)...};
}
};
template <class... _Env>
static consteval auto __mk_item_transform()
{
return []<class _ItemSender>()
{
return exec::__sequence_completion_signatures_of<_ItemSender, _Env...>();
};
}
static consteval auto __mk_unique_concat_items()
{
return []<class... _ItemLists>(_ItemLists...)
{
return STDEXEC::__minvoke<
STDEXEC::__mconcat<STDEXEC::__munique<STDEXEC::__qq<exec::item_types>>>,
_ItemLists...>();
};
}
template <class... _Env>
static consteval auto __mk_get_item_types()
{
return []<class _ItemSender>()
{
return exec::get_item_types<_ItemSender, _Env...>();
};
}
public:
template <class... _Sequences>
auto operator()(_Sequences&&... __sequences) const
noexcept(__nothrow_decay_copyable<_Sequences...>) -> __well_formed_sequence_sender auto
{
return make_sequence_expr<merge_t>({}, static_cast<_Sequences&&>(__sequences)...);
}
template <class _Self, class... _Env>
static consteval auto get_completion_signatures()
{
static_assert(sender_for<_Self, merge_t>);
auto __items = STDEXEC::__children_of<_Self, STDEXEC::__qq<item_types>>();
return exec::concat_completion_signatures(
completion_signatures<set_stopped_t()>(),
__items.__transform(__mk_item_transform<_Env...>(), exec::concat_completion_signatures));
}
template <class _Self, class... _Env>
static consteval auto get_item_types()
{
static_assert(sender_for<_Self, merge_t>);
auto __items = STDEXEC::__children_of<_Self, STDEXEC::__qq<item_types>>();
return __items.__transform(__mk_get_item_types<_Env...>(), __mk_unique_concat_items());
}
template <class _Self, receiver _Receiver>
static auto subscribe(_Self&& __self, _Receiver __rcvr)
noexcept(__nothrow_applicable<__subscribe_fn, _Self, _Receiver&>)
-> __apply_result_t<__subscribe_fn, _Self, _Receiver&>
{
static_assert(sender_for<_Self, merge_t>);
return STDEXEC::__apply(__subscribe_fn{}, static_cast<_Self&&>(__self), __rcvr);
}
};
} // namespace __merge
using __merge::merge_t;
inline constexpr merge_t merge{};
} // namespace experimental::execution
namespace exec = experimental::execution;
STDEXEC_PRAGMA_POP()