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>;
83 using bucket_iterator_type = std::ranges::iterator_t<bucket_type>;
87 using buffer_iterator_type = std::ranges::iterator_t<buffer_type>;
104 algorithm_executor_blocking() =
delete;
106 algorithm_executor_blocking(algorithm_executor_blocking
const &) =
delete;
125 algorithm_executor_blocking(algorithm_executor_blocking && other) noexcept :
126 algorithm_executor_blocking{std::move(other), other.resource_position()}
130 algorithm_executor_blocking & operator=(algorithm_executor_blocking
const &) =
delete;
134 algorithm_executor_blocking & operator=(algorithm_executor_blocking && other)
136 auto old_resource_position = other.resource_position();
138 resource = std::move(other.resource);
139 move_initialise(std::move(other), old_resource_position);
144 ~algorithm_executor_blocking() =
default;
160 algorithm_executor_blocking(resource_t resource,
161 algorithm_t algorithm,
162 algorithm_result_t
const SEQAN3_DOXYGEN_ONLY(result) = algorithm_result_t{},
163 execution_handler_t && exec_handler = execution_handler_t{}) :
164 exec_handler{std::move(exec_handler)},
165 resource{std::views::all(resource)},
167 algorithm{std::move(algorithm)}
169 if constexpr (std::same_as<execution_handler_t, execution_handler_parallel>)
170 buffer_size =
static_cast<size_t>(std::ranges::distance(this->resource));
172 buffer.resize(buffer_size);
173 buffer_it = buffer.end();
174 buffer_end_it = buffer_it;
198 do {
status = fill_buffer(); }
while (status == fill_status::empty_buffer);
200 if (status == fill_status::end_of_resource)
201 return {std::nullopt};
203 assert(status == fill_status::non_empty_buffer);
204 assert(bucket_it != buffer_it->end());
214 return resource_it == std::ranges::end(resource);
223 algorithm_executor_blocking(algorithm_executor_blocking && other,
224 resource_difference_type old_resource_position) noexcept :
225 resource{std::move(other.resource)}
227 move_initialise(std::move(other), old_resource_position);
236 resource_difference_type resource_position()
239 auto position = std::ranges::distance(std::ranges::begin(resource), resource_it);
240 assert (position >= 0);
245 fill_status fill_buffer()
247 if (!is_buffer_empty())
248 return fill_status::non_empty_buffer;
251 return fill_status::end_of_resource;
257 for (buffer_end_it = buffer_it; buffer_end_it != buffer.end() && !
is_eof(); ++buffer_end_it, ++resource_it)
259 exec_handler.execute(algorithm, *resource_it, [target_buffer_it = buffer_end_it] (
auto && algorithm_result)
261 target_buffer_it->push_back(std::move(algorithm_result));
268 find_next_non_empty_bucket();
270 if (is_buffer_empty())
271 return fill_status::empty_buffer;
273 return fill_status::non_empty_buffer;
279 bool is_buffer_empty()
const
281 return buffer_it == buffer_end_it;
295 for (
auto & bucket : buffer)
299 buffer_it = buffer.begin();
310 void find_next_non_empty_bucket()
312 assert(buffer_it <= buffer_end_it);
314 buffer_it =
std::find_if(buffer_it, buffer_end_it, [] (
auto const & buffer)
316 return !buffer.empty();
319 if (buffer_it != buffer_end_it)
320 bucket_it = buffer_it->begin();
330 void go_to_next_result()
332 if (++bucket_it == buffer_it->end())
335 find_next_non_empty_bucket();
340 void move_initialise(algorithm_executor_blocking && other, resource_difference_type old_resource_position)
noexcept
342 algorithm = std::move(other.algorithm);
343 buffer_size = std::move(other.buffer_size);
344 exec_handler = std::move(other.exec_handler);
346 resource_it = std::ranges::next(std::ranges::begin(resource), old_resource_position);
349 auto buffer_it_position = other.buffer_it - other.buffer.begin();
350 auto buffer_end_it_position = other.buffer_end_it - other.buffer.begin();
353 if (buffer_it_position != buffer_end_it_position)
354 bucket_it_position = other.bucket_it - other.buffer_it->begin();
357 buffer = std::move(other.buffer);
358 buffer_it = buffer.begin() + buffer_it_position;
359 buffer_end_it = buffer.begin() + buffer_end_it_position;
361 if (buffer_it_position != buffer_end_it_position)
362 bucket_it = buffer_it->begin() + bucket_it_position;
366 execution_handler_t exec_handler{};
369 resource_type resource;
371 resource_iterator_type resource_it{};
373 algorithm_t algorithm{};
376 buffer_type buffer{};
378 buffer_iterator_type buffer_it{};
380 buffer_iterator_type buffer_end_it{};
382 bucket_iterator_type bucket_it{};
384 size_t buffer_size{1};
393template <
typename resource_rng_t, std::semiregular algorithm_t, std::semiregular algorithm_result_t>
394algorithm_executor_blocking(resource_rng_t &&, algorithm_t, algorithm_result_t
const &) ->
395 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.
auto constexpr 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.