SeqAn3  3.2.0
The Modern C++ library for sequence analysis.
policy_search_result_builder.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2022, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2022, Knut Reinert & MPI für molekulare Genetik
4 // This file may be used, modified and/or redistributed under the terms of the 3-clause BSD-License
5 // shipped with this file and also available at: https://github.com/seqan/seqan3/blob/master/LICENSE.md
6 // -----------------------------------------------------------------------------------------------------
7 
13 #pragma once
14 
19 
20 namespace seqan3::detail
21 {
22 
25 template <typename search_configuration_t>
26  requires is_type_specialisation_of_v<search_configuration_t, configuration>
27 struct policy_search_result_builder
28 {
29 protected:
31  using search_traits_type = detail::search_traits<search_configuration_t>;
33  using search_result_type = typename search_traits_type::search_result_type;
34 
35  static_assert(!std::same_as<search_result_type, typename search_traits_type::empty_search_result_type>,
36  "The search result type was not configured properly.");
37 
41  policy_search_result_builder() = default;
42  policy_search_result_builder(policy_search_result_builder &&) = default;
43  policy_search_result_builder(policy_search_result_builder const &) = default;
44  policy_search_result_builder & operator=(policy_search_result_builder &&) = default;
45  policy_search_result_builder & operator=(policy_search_result_builder const &) = default;
46  ~policy_search_result_builder() = default;
47 
49  explicit policy_search_result_builder(search_configuration_t const &)
50  {}
52 
67  template <typename index_cursor_t, typename query_index_t, typename callback_t>
68  void make_results(std::vector<index_cursor_t> internal_hits, query_index_t idx, callback_t && callback)
69  {
70  return make_results_impl(std::move(internal_hits), idx, std::forward<callback_t>(callback));
71  }
72 
89  template <typename index_cursor_t, typename query_index_t, typename callback_t>
90  requires search_traits_type::output_requires_locate_call && (!search_traits_type::search_single_best_hit)
91  void make_results(std::vector<index_cursor_t> internal_hits, query_index_t idx, callback_t && callback)
92  {
94  results.reserve(internal_hits.size()); // expect at least as many text positions as cursors, possibly more
95 
96  make_results_impl(std::move(internal_hits),
97  idx,
98  [&results](auto && search_result)
99  {
100  results.push_back(std::move(search_result));
101  });
102 
103  // sort by reference id or by reference position if both have the same reference id.
104  std::sort(results.begin(),
105  results.end(),
106  [](auto const & r1, auto const & r2)
107  {
108  return (r1.reference_id() == r2.reference_id())
109  ? (r1.reference_begin_position() < r2.reference_begin_position())
110  : (r1.reference_id() < r2.reference_id());
111  });
112 
113  results.erase(std::unique(results.begin(), results.end()), results.end());
114 
115  for (auto && search_result : results)
116  callback(std::move(search_result));
117  }
118 
119 private:
137  template <typename index_cursor_t, typename query_index_t, typename callback_t>
138  void make_results_impl(std::vector<index_cursor_t> internal_hits,
139  [[maybe_unused]] query_index_t idx,
140  callback_t && callback)
141  {
142  auto maybe_locate = [](auto const & cursor)
143  {
144  if constexpr (search_traits_type::output_requires_locate_call)
145  return cursor.lazy_locate();
146  else
147  return std::views::single(std::tuple{0, 0});
148  };
149 
150  for (auto const & cursor : internal_hits)
151  {
152  for (auto && [ref_id, ref_pos] : maybe_locate(cursor))
153  {
154  search_result_type result{};
155 
156  if constexpr (search_traits_type::output_query_id)
157  result.query_id_ = std::move(idx);
158  if constexpr (search_traits_type::output_index_cursor)
159  result.cursor_ = cursor;
160  if constexpr (search_traits_type::output_reference_id)
161  result.reference_id_ = std::move(ref_id);
162  if constexpr (search_traits_type::output_reference_begin_position)
163  result.reference_begin_position_ = std::move(ref_pos);
164 
165  callback(result);
166 
167  if constexpr (search_traits_type::search_single_best_hit)
168  return;
169  }
170  }
171  }
172 };
173 
174 } // namespace seqan3::detail
requires requires
The rank_type of the semi-alphabet; defined as the return type of seqan3::to_rank....
Definition: alphabet/concept.hpp:164
@ single
The text is a single range.
Definition: search/fm_index/concept.hpp:93
T reserve(T... args)
Provides the concept for seqan3::detail::sdsl_index.
Provides seqan3::search_result.
Provides seqan3::detail::search_traits.
T size(T... args)
T sort(T... args)
Provides type traits for working with templates.
T unique(T... args)