SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
policy_optimum_tracker.hpp
Go to the documentation of this file.
1 // -----------------------------------------------------------------------------------------------------
2 // Copyright (c) 2006-2020, Knut Reinert & Freie Universität Berlin
3 // Copyright (c) 2016-2020, 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 
14 #pragma once
15 
16 #include <limits>
17 
24 
25 namespace seqan3::detail
26 {
27 
36 struct max_score_updater
37 {
53  template <typename score_t, typename coordinate_t>
55  requires (std::totally_ordered<score_t> && std::assignable_from<score_t &, score_t const &> &&
56  std::assignable_from<coordinate_t &, coordinate_t const &>)
58  void operator()(score_t & optimal_score,
59  coordinate_t & optimal_coordinate,
60  score_t current_score,
61  coordinate_t current_coordinate) const noexcept
62  {
63  bool const is_better_score = current_score >= optimal_score;
64  optimal_score = (is_better_score) ? std::move(current_score) : optimal_score;
65  optimal_coordinate = (is_better_score) ? std::move(current_coordinate) : optimal_coordinate;
66  }
67 };
68 
88 template <typename alignment_configuration_t, std::semiregular optimum_updater_t>
90  requires is_type_specialisation_of_v<alignment_configuration_t, configuration> &&
91  std::invocable<optimum_updater_t,
92  typename alignment_configuration_traits<alignment_configuration_t>::score_type &,
93  typename alignment_configuration_traits<alignment_configuration_t>::matrix_coordinate_type &,
94  typename alignment_configuration_traits<alignment_configuration_t>::score_type,
95  typename alignment_configuration_traits<alignment_configuration_t>::matrix_coordinate_type>
97 class policy_optimum_tracker
98 {
99 protected:
101  using traits_type = alignment_configuration_traits<alignment_configuration_t>;
103  using score_type = typename traits_type::score_type;
105  using matrix_coordinate_type = typename traits_type::matrix_coordinate_type;
106 
108  score_type optimal_score{};
110  matrix_coordinate_type optimal_coordinate{};
112  optimum_updater_t compare_and_set_optimum{};
113 
115  bool test_every_cell{false};
117  bool test_last_row_cell{false};
119  bool test_last_column_cell{false};
120 
124  policy_optimum_tracker() = default;
125  policy_optimum_tracker(policy_optimum_tracker const &) = default;
126  policy_optimum_tracker(policy_optimum_tracker &&) = default;
127  policy_optimum_tracker & operator=(policy_optimum_tracker const &) = default;
128  policy_optimum_tracker & operator=(policy_optimum_tracker &&) = default;
129  ~policy_optimum_tracker() = default;
130 
139  policy_optimum_tracker(alignment_configuration_t const & config)
140  {
141  auto method_global_config = config.get_or(align_cfg::method_global{});
142  test_last_row_cell = method_global_config.free_end_gaps_sequence1_trailing;
143  test_last_column_cell = method_global_config.free_end_gaps_sequence2_trailing;
144  }
146 
161  template <typename cell_t>
162  decltype(auto) track_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
163  {
164  if (test_every_cell)
165  invoke_comparator(cell, std::move(coordinate));
166 
167  return std::forward<cell_t>(cell);
168  }
169 
184  template <typename cell_t>
185  decltype(auto) track_last_row_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
186  {
187  if (test_last_row_cell && !test_every_cell)
188  invoke_comparator(cell, std::move(coordinate));
189  }
204  template <typename cell_t>
205  decltype(auto) track_last_column_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
206  {
207  if (test_last_column_cell && !test_every_cell)
208  invoke_comparator(cell, std::move(coordinate));
209 
210  return std::forward<cell_t>(cell);
211  }
212 
227  template <typename cell_t>
228  decltype(auto) track_final_cell(cell_t && cell, matrix_coordinate_type coordinate) noexcept
229  {
230  if (!(test_every_cell || test_last_row_cell || test_last_column_cell))
231  invoke_comparator(cell, std::move(coordinate));
232  }
234  void reset_optimum() noexcept
235  {
236  optimal_score = std::numeric_limits<score_type>::lowest();
237  optimal_coordinate = {};
238  }
239 
251  template <typename cell_t>
252  void invoke_comparator(cell_t && cell, matrix_coordinate_type coordinate) noexcept
253  {
254  compare_and_set_optimum(optimal_score, optimal_coordinate, cell.best_score(), std::move(coordinate));
255  }
256 };
257 } // namespace seqan3::detail
matrix_coordinate.hpp
Provides seqan3::detail::alignment_coordinate and associated strong types.
type_traits.hpp
Provides helper type traits for the configuration and execution of the alignment algorithm.
tuple.hpp
Provides seqan3::tuple_like.
configuration.hpp
Provides seqan3::detail::configuration and utility functions.
template_inspection.hpp
Provides seqan3::type_list and auxiliary type traits.
std::numeric_limits::lowest
T lowest(T... args)
seqan3::views::move
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
coordinate_matrix.hpp
Provides seqan3::detail::coordinate_matrix.
limits