Loading...
Searching...
No Matches
policy_search_result_builder.hpp
Go to the documentation of this file.
// SPDX-FileCopyrightText: 2006-2025 Knut Reinert & Freie Universität Berlin
// SPDX-FileCopyrightText: 2016-2025 Knut Reinert & MPI für molekulare Genetik
// SPDX-License-Identifier: BSD-3-Clause
#pragma once
#include <seqan3/core/detail/template_inspection.hpp>
#include <seqan3/search/detail/search_traits.hpp>
#include <seqan3/search/fm_index/concept.hpp>
#include <seqan3/search/search_result.hpp>
namespace seqan3::detail
{
template <typename search_configuration_t>
requires seqan3::detail::is_type_specialisation_of_v<search_configuration_t, configuration>
struct policy_search_result_builder
{
protected:
using search_traits_type = detail::search_traits<search_configuration_t>;
using search_result_type = typename search_traits_type::search_result_type;
static_assert(!std::same_as<search_result_type, typename search_traits_type::empty_search_result_type>,
"The search result type was not configured properly.");
policy_search_result_builder() = default;
policy_search_result_builder(policy_search_result_builder &&) = default;
policy_search_result_builder(policy_search_result_builder const &) = default;
policy_search_result_builder & operator=(policy_search_result_builder &&) = default;
policy_search_result_builder & operator=(policy_search_result_builder const &) = default;
~policy_search_result_builder() = default;
explicit policy_search_result_builder(search_configuration_t const &)
{}
template <typename index_cursor_t, typename query_index_t, typename callback_t>
void make_results(std::vector<index_cursor_t> internal_hits, query_index_t idx, callback_t && callback)
{
return make_results_impl(std::move(internal_hits), idx, std::forward<callback_t>(callback));
}
template <typename index_cursor_t, typename query_index_t, typename callback_t>
requires search_traits_type::output_requires_locate_call && (!search_traits_type::search_single_best_hit)
void make_results(std::vector<index_cursor_t> internal_hits, query_index_t idx, callback_t && callback)
{
std::vector<search_result_type> results{};
results.reserve(internal_hits.size()); // expect at least as many text positions as cursors, possibly more
make_results_impl(std::move(internal_hits),
idx,
[&results](auto && search_result)
{
results.push_back(std::move(search_result));
});
// sort by reference id or by reference position if both have the same reference id.
std::sort(results.begin(),
results.end(),
[](auto const & r1, auto const & r2)
{
return (r1.reference_id() == r2.reference_id())
? (r1.reference_begin_position() < r2.reference_begin_position())
: (r1.reference_id() < r2.reference_id());
});
results.erase(std::unique(results.begin(), results.end()), results.end());
for (auto && search_result : results)
callback(std::move(search_result));
}
private:
template <typename index_cursor_t, typename query_index_t, typename callback_t>
void make_results_impl(std::vector<index_cursor_t> internal_hits,
[[maybe_unused]] query_index_t idx,
callback_t && callback)
{
auto maybe_locate = [](auto const & cursor)
{
if constexpr (search_traits_type::output_requires_locate_call)
return cursor.lazy_locate();
else
return std::views::single(std::tuple{0, 0});
};
for (auto const & cursor : internal_hits)
{
for (auto && [ref_id, ref_pos] : maybe_locate(cursor))
{
search_result_type result{};
if constexpr (search_traits_type::output_query_id)
result.query_id_ = std::move(idx);
if constexpr (search_traits_type::output_index_cursor)
result.cursor_ = cursor;
if constexpr (search_traits_type::output_reference_id)
result.reference_id_ = std::move(ref_id);
if constexpr (search_traits_type::output_reference_begin_position)
result.reference_begin_position_ = std::move(ref_pos);
callback(result);
if constexpr (search_traits_type::search_single_best_hit)
return;
}
}
}
};
} // namespace seqan3::detail
std::vector::begin
T begin(T... args)
std::vector::end
T end(T... args)
std::vector::erase
T erase(T... args)
std::forward
T forward(T... args)
seqan3::field::ref_id
@ ref_id
The identifier of the (reference) sequence that seqan3::field::seq was aligned to.
Definition record.hpp:79
std::vector::push_back
T push_back(T... args)
std::vector::reserve
T reserve(T... args)
concept.hpp
Provides the concept for seqan3::detail::sdsl_index.
search_result.hpp
Provides seqan3::search_result.
search_traits.hpp
Provides seqan3::detail::search_traits.
std::vector::size
T size(T... args)
std::sort
T sort(T... args)
template_inspection.hpp
Provides type traits for working with templates.
std::unique
T unique(T... args)
Hide me