25namespace seqan3::detail
34template <
typename configuration_t,
typename index_t,
typename ...policies_t>
36 requires (template_specialisation_of<typename index_t::cursor_type, bi_fm_index_cursor>)
38class search_scheme_algorithm :
protected policies_t...
42 using traits_t = search_traits<configuration_t>;
44 using search_result_type =
typename traits_t::search_result_type;
46 static_assert(!std::same_as<search_result_type, empty_type>,
"The search result type was not configured.");
52 search_scheme_algorithm() =
default;
53 search_scheme_algorithm(search_scheme_algorithm
const &) =
default;
54 search_scheme_algorithm(search_scheme_algorithm &&) =
default;
55 search_scheme_algorithm & operator=(search_scheme_algorithm
const &) =
default;
56 search_scheme_algorithm & operator=(search_scheme_algorithm &&) =
default;
57 ~search_scheme_algorithm() =
default;
70 search_scheme_algorithm(configuration_t
const & cfg, index_t
const & index) : policies_t{cfg}...
72 stratum = cfg.get_or(search_cfg::hit_strata{0}).stratum;
97 template <tuple_like indexed_query_t,
typename callback_t>
99 requires (std::tuple_size_v<indexed_query_t> == 2) &&
100 std::ranges::forward_range<std::tuple_element_t<1, indexed_query_t>> &&
101 std::invocable<callback_t, search_result_type>
103 void operator()(indexed_query_t && indexed_query, callback_t && callback)
105 auto && [query_idx, query] = indexed_query;
106 auto error_state = this->max_error_counts(query);
110 auto on_hit_delegate = [&internal_hits] (
auto const & it)
115 perform_search_by_hit_strategy(internal_hits, query, error_state, on_hit_delegate);
118 this->make_results(std::move(internal_hits), query_idx, callback);
123 index_t
const * index_ptr{
nullptr};
129 template <
bool abort_on_hit,
typename query_t,
typename delegate_t>
130 inline void search_algo_bi(query_t & query, search_param
const error_left, delegate_t && delegate);
139 template <
typename query_t,
typename delegate_t>
142 search_param error_state,
143 delegate_t
const & on_hit_delegate)
145 if constexpr (!traits_t::search_all_hits)
147 auto max_total = error_state.total;
148 error_state.total = 0;
149 while (internal_hits.
empty() && error_state.total <= max_total)
157 constexpr bool abort_on_hit = !traits_t::search_all_best_hits;
158 search_algo_bi<abort_on_hit>(query, error_state, on_hit_delegate);
161 if constexpr (traits_t::search_strata_hits)
163 if (!internal_hits.
empty())
165 internal_hits.
clear();
166 error_state.total += stratum - 1;
167 search_algo_bi<false>(query, error_state, on_hit_delegate);
175 search_algo_bi<false>(query, error_state, on_hit_delegate);
221template <
typename search_scheme_t>
222inline auto search_scheme_block_info(search_scheme_t
const & search_scheme,
size_t const query_length)
224 using blocks_length_type =
typename search_scheme_t::value_type::blocks_length_type;
226 bool constexpr is_dyn_scheme = std::same_as<search_scheme_t, search_scheme_dyn_type>;
232 transformation_trait_or_t<std::tuple_size<search_scheme_t>,
236 if constexpr (is_dyn_scheme)
237 result.resize(search_scheme.size());
239 uint8_t
const blocks {search_scheme[0].blocks()};
240 size_t const block_length{query_length / blocks};
241 uint8_t
const rest {
static_cast<uint8_t
>(query_length % blocks)};
243 blocks_length_type blocks_length;
246 if constexpr (is_dyn_scheme)
247 blocks_length.resize(blocks, block_length);
249 blocks_length.fill(block_length);
251 for (uint8_t block_id = 0; block_id < rest; ++block_id)
252 ++blocks_length[block_id];
254 for (uint8_t search_id = 0; search_id < search_scheme.size(); ++search_id)
256 auto const &
search = search_scheme[search_id];
258 auto & [search_blocks_length, start_pos] = result[search_id];
262 if constexpr (is_dyn_scheme)
263 search_blocks_length.resize(blocks);
264 search_blocks_length[0] = blocks_length[
search.pi[0] - 1];
265 for (uint8_t i = 1; i < blocks; ++i)
267 search_blocks_length[i] = blocks_length[
search.pi[i] - 1] + search_blocks_length[i - 1];
269 start_pos += search_blocks_length[i] - search_blocks_length[i - 1];
278template <
bool abort_on_hit,
typename cursor_t,
typename query_t,
typename search_t,
typename blocks_length_t,
280inline bool search_ss(cursor_t cur, query_t & query,
281 typename cursor_t::size_type
const lb,
typename cursor_t::size_type
const rb,
282 uint8_t
const errors_spent, uint8_t
const block_id,
bool const go_right, search_t
const &
search,
283 blocks_length_t
const & blocks_length, search_param
const error_left, delegate_t && delegate);
317template <
bool abort_on_hit,
typename cursor_t,
typename query_t,
typename search_t,
typename blocks_length_t,
319inline bool search_ss_exact(cursor_t cur, query_t & query,
320 typename cursor_t::size_type
const lb,
typename cursor_t::size_type
const rb,
321 uint8_t
const errors_spent, uint8_t
const block_id,
bool const go_right,
322 search_t
const &
search, blocks_length_t
const & blocks_length,
323 search_param
const error_left, delegate_t && delegate)
325 using size_type =
typename cursor_t::size_type;
327 uint8_t
const block_id2 = std::min<uint8_t>(block_id + 1,
search.blocks() - 1);
328 bool const go_right2 = (block_id <
search.blocks() - 1) && (
search.pi[block_id + 1] >
search.pi[block_id]);
332 size_type
const infix_lb = rb - 1;
333 size_type
const infix_rb = lb + blocks_length[block_id] - 1;
335 if (!cur.extend_right(query |
views::slice(infix_lb, infix_rb + 1)))
338 if (search_ss<abort_on_hit>(cur, query, lb, infix_rb + 2, errors_spent, block_id2, go_right2,
search,
339 blocks_length, error_left, delegate) && abort_on_hit)
346 size_type
const infix_lb = rb - blocks_length[block_id] - 1;
347 size_type
const infix_rb = lb - 1;
349 if (!cur.extend_left(query |
views::slice(infix_lb, infix_rb + 1)))
352 if (search_ss<abort_on_hit>(cur, query, infix_lb, rb, errors_spent, block_id2, go_right2,
search, blocks_length,
353 error_left, delegate) && abort_on_hit)
367template <
bool abort_on_hit,
typename cursor_t,
typename query_t,
typename search_t,
typename blocks_length_t,
369inline bool search_ss_deletion(cursor_t cur, query_t & query,
370 typename cursor_t::size_type
const lb,
typename cursor_t::size_type
const rb,
371 uint8_t
const errors_spent, uint8_t
const block_id,
bool const go_right,
372 search_t
const &
search, blocks_length_t
const & blocks_length,
373 search_param
const error_left, delegate_t && delegate)
375 uint8_t
const max_error_left_in_block =
search.u[block_id] - errors_spent;
376 uint8_t
const min_error_left_in_block =
std::max(
search.l[block_id] - errors_spent, 0);
379 if (min_error_left_in_block == 0)
381 uint8_t
const block_id2 = std::min<uint8_t>(block_id + 1,
search.blocks() - 1);
382 bool const go_right2 = block_id2 == 0 ? true :
search.pi[block_id2] >
search.pi[block_id2 - 1];
384 if (search_ss<abort_on_hit>(cur, query, lb, rb, errors_spent, block_id2, go_right2,
search, blocks_length,
385 error_left, delegate) && abort_on_hit)
394 if (!(
search.pi[block_id] == 1 && !go_right) &&
395 !(
search.pi[block_id] ==
search.blocks() && go_right) &&
396 max_error_left_in_block > 0 && error_left.total > 0 && error_left.deletion > 0 &&
397 ((go_right && cur.extend_right()) || (!go_right && cur.extend_left())))
399 search_param error_left2{error_left};
401 error_left2.deletion--;
404 if (search_ss_deletion<abort_on_hit>(cur, query, lb, rb, errors_spent + 1, block_id, go_right,
search,
405 blocks_length, error_left2, delegate) && abort_on_hit)
409 }
while ((go_right && cur.cycle_back()) || (!go_right && cur.cycle_front()));
422template <
bool abort_on_hit,
typename cursor_t,
typename query_t,
typename search_t,
typename blocks_length_t,
424inline bool search_ss_children(cursor_t cur, query_t & query,
425 typename cursor_t::size_type
const lb,
typename cursor_t::size_type
const rb,
426 uint8_t
const errors_spent, uint8_t
const block_id,
bool const go_right,
427 uint8_t
const min_error_left_in_block, search_t
const &
search,
428 blocks_length_t
const & blocks_length, search_param
const error_left,
429 delegate_t && delegate)
431 using size_type =
typename cursor_t::size_type;
432 if ((go_right && cur.extend_right()) || (!go_right && cur.extend_left()))
434 size_type
const chars_left = blocks_length[block_id] - (rb - lb - 1);
436 size_type lb2 = lb - !go_right;
437 size_type rb2 = rb + go_right;
441 bool const delta = cur.last_rank() !=
to_rank(query[(go_right ? rb : lb) - 1]);
447 if (error_left.deletion == 0 && chars_left + delta < min_error_left_in_block + 1u)
450 if (!delta || error_left.substitution > 0)
452 search_param error_left2{error_left};
453 error_left2.total -= delta;
454 error_left2.substitution -= delta;
457 if (rb - lb == blocks_length[block_id])
461 if (error_left.deletion > 0)
463 if (search_ss_deletion<abort_on_hit>(cur, query, lb2, rb2, errors_spent + delta, block_id,
464 go_right,
search, blocks_length, error_left2, delegate) &&
472 uint8_t
const block_id2 = std::min<uint8_t>(block_id + 1,
search.blocks() - 1);
473 bool const go_right2 = block_id2 == 0 ? true :
search.pi[block_id2] >
search.pi[block_id2 - 1];
475 if (search_ss<abort_on_hit>(cur, query, lb2, rb2, errors_spent + delta, block_id2, go_right2,
476 search, blocks_length, error_left2, delegate) &&
485 if (search_ss<abort_on_hit>(cur, query, lb2, rb2, errors_spent + delta, block_id, go_right,
search,
486 blocks_length, error_left2, delegate) && abort_on_hit)
497 if (error_left.deletion > 0 &&
501 search_param error_left3{error_left};
503 error_left3.deletion--;
504 search_ss<abort_on_hit>(cur, query, lb, rb, errors_spent + 1, block_id, go_right,
search, blocks_length,
505 error_left3, delegate);
507 }
while ((go_right && cur.cycle_back()) || (!go_right && cur.cycle_front()));
517template <
bool abort_on_hit,
typename cursor_t,
typename query_t,
typename search_t,
518 typename blocks_length_t,
typename delegate_t>
519inline bool search_ss(cursor_t cur, query_t & query,
520 typename cursor_t::size_type
const lb,
typename cursor_t::size_type
const rb,
521 uint8_t
const errors_spent, uint8_t
const block_id,
bool const go_right, search_t
const &
search,
522 blocks_length_t
const & blocks_length, search_param
const error_left, delegate_t && delegate)
524 uint8_t
const max_error_left_in_block =
search.u[block_id] - errors_spent;
525 uint8_t
const min_error_left_in_block =
std::max(
search.l[block_id] - errors_spent, 0);
528 if (min_error_left_in_block == 0 && lb == 0 && rb ==
std::ranges::size(query) + 1)
534 else if (((max_error_left_in_block == 0) && (rb - lb - 1 != blocks_length[block_id])) ||
535 (error_left.total == 0 && min_error_left_in_block == 0))
537 if (search_ss_exact<abort_on_hit>(cur, query, lb, rb, errors_spent, block_id, go_right,
search, blocks_length,
538 error_left, delegate) && abort_on_hit)
545 else if (error_left.total > 0)
548 if (error_left.insertion > 0)
550 using size_type =
typename cursor_t::size_type;
552 size_type
const lb2 = lb - !go_right;
553 size_type
const rb2 = rb + go_right;
555 search_param error_left2{error_left};
557 error_left2.insertion--;
559 if (rb - lb == blocks_length[block_id])
566 if (search_ss_deletion<abort_on_hit>(cur, query, lb2, rb2, errors_spent + 1, block_id, go_right,
search,
567 blocks_length, error_left2, delegate) && abort_on_hit)
574 if (search_ss<abort_on_hit>(cur, query, lb2, rb2, errors_spent + 1, block_id, go_right,
search,
575 blocks_length, error_left2, delegate) && abort_on_hit)
581 if (search_ss_children<abort_on_hit>(cur, query, lb, rb, errors_spent, block_id, go_right,
582 min_error_left_in_block,
search, blocks_length, error_left, delegate) &&
614template <
bool abort_on_hit,
typename index_t,
typename query_t,
typename search_scheme_t,
typename delegate_t>
615inline void search_ss(index_t
const & index, query_t & query, search_param
const error_left,
616 search_scheme_t
const & search_scheme, delegate_t && delegate)
619 auto const block_info = search_scheme_block_info(search_scheme,
std::ranges::size(query));
621 for (uint8_t search_id = 0; search_id < search_scheme.size(); ++search_id)
623 auto const &
search = search_scheme[search_id];
624 auto const & [blocks_length, start_pos] = block_info[search_id];
626 bool const hit = search_ss<abort_on_hit>(
629 start_pos, start_pos + 1,
639 if (abort_on_hit && hit)
662template <
typename configuration_t,
typename index_t,
typename ...policies_t>
663template <
bool abort_on_hit,
typename query_t,
typename delegate_t>
664inline void search_scheme_algorithm<configuration_t, index_t, policies_t...>::search_algo_bi(
666 search_param
const error_left,
667 delegate_t && delegate)
669 switch (error_left.total)
672 search_ss<abort_on_hit>(*index_ptr, query, error_left, optimum_search_scheme<0, 0>, delegate);
675 search_ss<abort_on_hit>(*index_ptr, query, error_left, optimum_search_scheme<0, 1>, delegate);
678 search_ss<abort_on_hit>(*index_ptr, query, error_left, optimum_search_scheme<0, 2>, delegate);
681 search_ss<abort_on_hit>(*index_ptr, query, error_left, optimum_search_scheme<0, 3>, delegate);
684 auto const & search_scheme{compute_ss(0, error_left.total)};
685 search_ss<abort_on_hit>(*index_ptr, query, error_left, search_scheme, delegate);
Provides the bidirectional seqan3::bi_fm_index.
constexpr auto to_rank
Return the rank representation of a (semi-)alphabet object.
Definition: concept.hpp:155
auto search(queries_t &&queries, index_t const &index, configuration_t const &cfg=search_cfg::default_configuration)
Search a query or a range of queries in an index.
Definition: search.hpp:104
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:151
constexpr auto slice
A view adaptor that returns a half-open interval on the underlying range.
Definition: slice.hpp:183
Provides the concept for seqan3::detail::sdsl_index.
Provides data structures used by different search algorithms.
Provides the data structures and precomputed instances for (optimum) search schemes.
Provides seqan3::detail::search_traits.
Provides seqan3::views::slice.