SeqAn3 3.4.3-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
algorithm_executor_blocking.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2026 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2026 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <functional>
13#include <optional>
14#include <ranges>
15#include <type_traits>
16
19
20namespace seqan3::detail
21{
22
51template <std::ranges::viewable_range resource_t,
52 std::semiregular algorithm_t,
53 std::semiregular algorithm_result_t,
54 typename execution_handler_t = execution_handler_sequential>
55 requires std::ranges::forward_range<resource_t>
56 && std::invocable<algorithm_t,
57 std::ranges::range_reference_t<resource_t>,
58 std::function<void(algorithm_result_t)>>
60{
61private:
66 using resource_type = std::views::all_t<resource_t>;
68 using resource_iterator_type = std::ranges::iterator_t<resource_type>;
72
79 using bucket_iterator_type = std::ranges::iterator_t<bucket_type>;
83 using buffer_iterator_type = std::ranges::iterator_t<buffer_type>;
85
93
94public:
103
124
127
131 {
132 auto old_resource_position = other.resource_position();
133
134 resource = std::move(other.resource);
135 move_initialise(std::move(other), old_resource_position);
136 return *this;
137 }
138
141
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{}) :
161 resource{std::forward<resource_t>(resource)},
162 resource_it{std::ranges::begin(this->resource)},
163 algorithm{std::move(algorithm)}
164 {
165 if constexpr (std::same_as<execution_handler_t, execution_handler_parallel>)
166 buffer_size = static_cast<size_t>(std::ranges::distance(this->resource));
167
169 buffer_it = buffer.end();
171 }
173
190 {
191 fill_status status;
192 // Each invocation of the algorithm might produce zero results (e.g. a search might not find a query)
193 // this repeats the algorithm until it produces the first result or the input resource was consumed.
194 do
195 {
196 status = fill_buffer();
197 }
198 while (status == fill_status::empty_buffer);
199
200 std::optional<algorithm_result_t> result{std::nullopt};
201 if (status == fill_status::end_of_resource)
202 return result;
203
204 assert(status == fill_status::non_empty_buffer);
205 assert(bucket_it != buffer_it->end());
206
207 result = std::ranges::iter_move(bucket_it);
208 go_to_next_result(); // Go to next buffered result.
209 return result;
210 }
211
213 bool is_eof() noexcept
214 {
215 return resource_it == std::ranges::end(resource);
216 }
217
218private:
225 resource_difference_type old_resource_position) noexcept :
226 resource{std::move(other.resource)}
227 {
228 move_initialise(std::move(other), old_resource_position);
229 }
230
238 {
239 // Get the old resource position.
240 auto position = std::ranges::distance(std::ranges::begin(resource), resource_it);
241 assert(position >= 0);
242 return position;
243 }
244
247 {
248 if (!is_buffer_empty()) // Not everything consumed yet.
250
251 if (is_eof()) // Case: reached end of resource.
253
254 // Reset the buckets and the buffer iterator.
255 reset_buffer();
256
257 // Execute the algorithm (possibly asynchronous) and fill the buckets in this pre-assigned order.
259 {
260 exec_handler.execute(algorithm,
262 [target_buffer_it = buffer_end_it](auto && algorithm_result)
263 {
264 target_buffer_it->push_back(std::move(algorithm_result));
265 });
266 }
267
268 exec_handler.wait();
269
270 // Move the results iterator to the next available result. (This skips empty results of the algorithm)
272
273 if (is_buffer_empty())
275
277 }
278
282 bool is_buffer_empty() const
283 {
284 return buffer_it == buffer_end_it;
285 }
286
296 {
297 // Clear all buckets
298 for (auto & bucket : buffer)
299 bucket.clear();
300
301 // Reset the iterator over the buckets.
303 }
304
314 {
315 assert(buffer_it <= buffer_end_it);
316 // find first buffered bucket that contains at least one element
319 [](auto const & buffer)
320 {
321 return !buffer.empty();
322 });
323
325 bucket_it = buffer_it->begin();
326 }
327
336 {
337 if (++bucket_it == buffer_it->end())
338 {
339 ++buffer_it;
341 }
342 }
343
345 void move_initialise(algorithm_executor_blocking && other, resource_difference_type old_resource_position) noexcept
346 {
347 algorithm = std::move(other.algorithm);
348 buffer_size = std::move(other.buffer_size);
349 exec_handler = std::move(other.exec_handler);
350 // Move the resource and set the iterator state accordingly.
351 resource_it = std::ranges::next(std::ranges::begin(resource), old_resource_position);
352
353 // Get the old buffer and bucket iterator positions.
354 auto buffer_it_position = other.buffer_it - other.buffer.begin();
355 auto buffer_end_it_position = other.buffer_end_it - other.buffer.begin();
356
357 std::ptrdiff_t bucket_it_position = 0;
358 if (buffer_it_position != buffer_end_it_position)
359 bucket_it_position = other.bucket_it - other.buffer_it->begin();
360
361 // Move the buffer and set the buffer and bucket iterator accordingly.
362 buffer = std::move(other.buffer);
363 buffer_it = buffer.begin() + buffer_it_position;
364 buffer_end_it = buffer.begin() + buffer_end_it_position;
365
366 if (buffer_it_position != buffer_end_it_position)
367 bucket_it = buffer_it->begin() + bucket_it_position;
368 }
369
371 execution_handler_t exec_handler{};
372
374 resource_type resource; // a std::ranges::view
378 algorithm_t algorithm{};
379
389 size_t buffer_size{1};
390};
391
398template <typename resource_rng_t, std::semiregular algorithm_t, std::semiregular algorithm_result_t>
399algorithm_executor_blocking(resource_rng_t &&, algorithm_t, algorithm_result_t const &)
402} // namespace seqan3::detail
T begin(T... args)
A blocking algorithm executor for algorithms.
Definition algorithm_executor_blocking.hpp:60
buffer_type buffer
The buffer storing the algorithm results in buckets.
Definition algorithm_executor_blocking.hpp:381
algorithm_executor_blocking & operator=(algorithm_executor_blocking &&other)
Move assigns from the resource of another executor.
Definition algorithm_executor_blocking.hpp:130
void reset_buffer()
Resets the buckets.
Definition algorithm_executor_blocking.hpp:295
bucket_iterator_type bucket_it
The bucket iterator pointing to the next result within the current bucket.
Definition algorithm_executor_blocking.hpp:387
fill_status fill_buffer()
Fills the buffer by storing the results of an algorithm invocation into a pre-assigned bucket.
Definition algorithm_executor_blocking.hpp:246
resource_type resource
The underlying resource.
Definition algorithm_executor_blocking.hpp:374
algorithm_executor_blocking(resource_rng_t &&, algorithm_t, algorithm_result_t const &) -> algorithm_executor_blocking< resource_rng_t, algorithm_t, algorithm_result_t, execution_handler_sequential >
Deduce the type from the provided arguments and set the sequential execution handler.
void move_initialise(algorithm_executor_blocking &&other, resource_difference_type old_resource_position) noexcept
Helper function to move initialise this from other.
Definition algorithm_executor_blocking.hpp:345
std::ranges::iterator_t< resource_type > resource_iterator_type
The iterator over the underlying resource.
Definition algorithm_executor_blocking.hpp:68
buffer_iterator_type buffer_it
The iterator pointing to the current bucket in the buffer.
Definition algorithm_executor_blocking.hpp:383
std::optional< algorithm_result_t > next_result()
}
Definition algorithm_executor_blocking.hpp:189
algorithm_executor_blocking()=delete
Deleted default constructor because this class manages an external resource.
std::ranges::iterator_t< buffer_type > buffer_iterator_type
The iterator type of the buffer.
Definition algorithm_executor_blocking.hpp:83
algorithm_executor_blocking(algorithm_executor_blocking &&other) noexcept
Move constructs the resource of the other executor.
Definition algorithm_executor_blocking.hpp:121
algorithm_executor_blocking(algorithm_executor_blocking const &)=delete
This class provides unique ownership over the managed resource and is therefor not copyable.
bool is_eof() noexcept
Checks whether the end of the input resource was reached.
Definition algorithm_executor_blocking.hpp:213
algorithm_executor_blocking(resource_t resource, algorithm_t algorithm, algorithm_result_t const result=algorithm_result_t{}, execution_handler_t &&exec_handler=execution_handler_t{})
Constructs this executor with the given resource range.
Definition algorithm_executor_blocking.hpp:156
size_t buffer_size
The end get pointer in the buffer.
Definition algorithm_executor_blocking.hpp:389
execution_handler_t exec_handler
The execution policy.
Definition algorithm_executor_blocking.hpp:371
void find_next_non_empty_bucket()
Finds the first non-empty bucket starting from the current position of the buffer iterator.
Definition algorithm_executor_blocking.hpp:313
algorithm_executor_blocking & operator=(algorithm_executor_blocking const &)=delete
This class provides unique ownership over the managed resource and is therefor not copyable.
buffer_iterator_type buffer_end_it
The iterator pointing behind the last bucket (must not be the end of the buffer).
Definition algorithm_executor_blocking.hpp:385
resource_iterator_type resource_it
The iterator over the resource that stores the current state of the executor.
Definition algorithm_executor_blocking.hpp:376
resource_difference_type resource_position()
Number of times the resource_it was incremented.
Definition algorithm_executor_blocking.hpp:237
algorithm_t algorithm
The algorithm to invoke.
Definition algorithm_executor_blocking.hpp:378
fill_status
Return status for seqan3::detail::algorithm_executor_blocking::fill_buffer.
Definition algorithm_executor_blocking.hpp:88
@ end_of_resource
The end of the resource was reached.
Definition algorithm_executor_blocking.hpp:91
@ empty_buffer
The buffer is empty after calling fill_buffer.
Definition algorithm_executor_blocking.hpp:90
@ non_empty_buffer
The buffer is not fully consumed yet and contains at least one element.
Definition algorithm_executor_blocking.hpp:89
std::ranges::iterator_t< bucket_type > bucket_iterator_type
The iterator type of a bucket.
Definition algorithm_executor_blocking.hpp:79
void go_to_next_result()
Moves the bucket iterator to the next available result.
Definition algorithm_executor_blocking.hpp:335
std::views::all_t< resource_t > resource_type
The underlying resource type.
Definition algorithm_executor_blocking.hpp:66
algorithm_executor_blocking(algorithm_executor_blocking &&other, resource_difference_type old_resource_position) noexcept
This constructor is needed to ensure initialisation order for the move construction.
Definition algorithm_executor_blocking.hpp:224
bool is_buffer_empty() const
Whether the internal buffer is empty.
Definition algorithm_executor_blocking.hpp:282
T empty(T... args)
T end(T... args)
Provides seqan3::detail::execution_handler_parallel.
Provides seqan3::detail::execution_handler_sequential.
T find_if(T... args)
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
SeqAn specific customisations in the standard namespace.
T resize(T... args)
Hide me