23namespace seqan3::detail
54template <std::ranges::viewable_range resource_t,
55 std::semiregular algorithm_t,
56 std::semiregular algorithm_result_t,
57 typename execution_handler_t = execution_handler_sequential>
59 requires std::ranges::forward_range<resource_t> &&
60 std::invocable<algorithm_t, std::ranges::range_reference_t<resource_t>,
63class algorithm_executor_blocking
70 using resource_type = std::views::all_t<resource_t>;
72 using resource_iterator_type = std::ranges::iterator_t<resource_type>;
81 using bucket_iterator_type = std::ranges::iterator_t<bucket_type>;
85 using buffer_iterator_type = std::ranges::iterator_t<buffer_type>;
102 algorithm_executor_blocking() =
delete;
104 algorithm_executor_blocking(algorithm_executor_blocking
const &) =
delete;
123 algorithm_executor_blocking(algorithm_executor_blocking && other)
noexcept
125 move_initialise(std::move(other));
129 algorithm_executor_blocking & operator=(algorithm_executor_blocking
const &) =
delete;
133 algorithm_executor_blocking & operator=(algorithm_executor_blocking && other)
135 move_initialise(std::move(other));
140 ~algorithm_executor_blocking() =
default;
156 algorithm_executor_blocking(resource_t resource,
157 algorithm_t algorithm,
158 algorithm_result_t
const SEQAN3_DOXYGEN_ONLY(result) = algorithm_result_t{},
159 execution_handler_t && exec_handler = execution_handler_t{}) :
160 exec_handler{std::move(exec_handler)},
161 resource{std::views::all(resource)},
163 algorithm{std::move(algorithm)}
165 if constexpr (std::same_as<execution_handler_t, execution_handler_parallel>)
166 buffer_size =
static_cast<size_t>(std::ranges::distance(resource));
168 buffer.resize(buffer_size);
169 buffer_it = buffer.end();
170 buffer_end_it = buffer_it;
194 do {
status = fill_buffer(); }
while (status == fill_status::empty_buffer);
196 if (status == fill_status::end_of_resource)
197 return {std::nullopt};
199 assert(status == fill_status::non_empty_buffer);
200 assert(bucket_it != buffer_it->end());
210 return resource_it == std::ranges::end(resource);
215 fill_status fill_buffer()
217 if (!is_buffer_empty())
218 return fill_status::non_empty_buffer;
221 return fill_status::end_of_resource;
227 for (buffer_end_it = buffer_it; buffer_end_it != buffer.end() && !
is_eof(); ++buffer_end_it, ++resource_it)
229 exec_handler.execute(algorithm, *resource_it, [target_buffer_it = buffer_end_it] (
auto && algorithm_result)
231 target_buffer_it->push_back(std::move(algorithm_result));
238 find_next_non_empty_bucket();
240 if (is_buffer_empty())
241 return fill_status::empty_buffer;
243 return fill_status::non_empty_buffer;
249 bool is_buffer_empty()
const
251 return buffer_it == buffer_end_it;
265 for (
auto & bucket : buffer)
269 buffer_it = buffer.begin();
280 void find_next_non_empty_bucket()
282 assert(buffer_it <= buffer_end_it);
284 buffer_it =
std::find_if(buffer_it, buffer_end_it, [] (
auto const & buffer)
286 return !buffer.empty();
289 if (buffer_it != buffer_end_it)
290 bucket_it = buffer_it->begin();
300 void go_to_next_result()
302 if (++bucket_it == buffer_it->end())
305 find_next_non_empty_bucket();
311 void move_initialise(algorithm_executor_blocking && other)
noexcept
313 algorithm = std::move(other.algorithm);
314 buffer_size = std::move(other.buffer_size);
315 exec_handler = std::move(other.exec_handler);
317 auto old_resource_position = std::ranges::distance(std::ranges::begin(other.resource),
320 resource = std::move(other.resource);
321 resource_it = std::ranges::next(std::ranges::begin(resource), old_resource_position);
324 auto buffer_it_position = other.buffer_it - other.buffer.begin();
325 auto buffer_end_it_position = other.buffer_end_it - other.buffer.begin();
328 if (buffer_it_position != buffer_end_it_position)
329 bucket_it_position = other.bucket_it - other.buffer_it->begin();
332 buffer = std::move(other.buffer);
333 buffer_it = buffer.begin() + buffer_it_position;
334 buffer_end_it = buffer.begin() + buffer_end_it_position;
336 if (buffer_it_position != buffer_end_it_position)
337 bucket_it = buffer_it->begin() + bucket_it_position;
341 execution_handler_t exec_handler{};
344 resource_type resource{};
346 resource_iterator_type resource_it{};
348 algorithm_t algorithm{};
351 buffer_type buffer{};
353 buffer_iterator_type buffer_it{};
355 buffer_iterator_type buffer_end_it{};
357 bucket_iterator_type bucket_it{};
359 size_t buffer_size{1};
368template <
typename resource_rng_t, std::semiregular algorithm_t, std::semiregular algorithm_result_t>
369algorithm_executor_blocking(resource_rng_t &&, algorithm_t, algorithm_result_t
const &) ->
370 algorithm_executor_blocking<resource_rng_t, algorithm_t, algorithm_result_t, execution_handler_sequential>;
Provides seqan3::detail::execution_handler_parallel.
Provides seqan3::detail::execution_handler_sequential.
constexpr auto is_eof
Checks whether a given letter is equal to the EOF constant defined in <cstdio>.
Definition: predicate.hpp:77
The <ranges> header from C++20's standard library.