SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
edit_distance_trace_matrix_full.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 
13 #pragma once
14 
15 #include <bitset>
16 
20 
21 namespace seqan3::detail
22 {
23 
31 template <typename word_t, bool is_semi_global, bool use_max_errors>
32 class edit_distance_trace_matrix_full
33 {
34 public:
36  template <std::ranges::viewable_range database_t,
37  std::ranges::viewable_range query_t,
38  typename align_config_t,
39  typename edit_traits>
40  friend class edit_distance_unbanded;
41 
45  edit_distance_trace_matrix_full() = default;
46  edit_distance_trace_matrix_full(edit_distance_trace_matrix_full const &) = default;
47  edit_distance_trace_matrix_full(edit_distance_trace_matrix_full &&) = default;
48  edit_distance_trace_matrix_full & operator=(edit_distance_trace_matrix_full const &) = default;
49  edit_distance_trace_matrix_full & operator=(edit_distance_trace_matrix_full &&) = default;
50  ~edit_distance_trace_matrix_full() = default;
51 
52  protected:
54  template <typename derived_t, typename edit_traits>
55  friend class edit_distance_unbanded_trace_matrix_policy;
56 
60  edit_distance_trace_matrix_full(size_t const rows_size)
61  : rows_size{rows_size}, columns{}
62  {}
64 
65 public:
67  using word_type = word_t;
68 
70  static constexpr auto word_size = sizeof_bits<word_type>;
71 
73  using value_type = detail::trace_directions;
74 
76  using reference = value_type;
77 
79  using size_type = size_t;
80 
89  void reserve(size_t const new_capacity)
90  {
91  columns.reserve(new_capacity);
92  }
93 
94 public:
96  reference at(matrix_coordinate const & coordinate) const noexcept
97  {
98  size_t row = coordinate.row;
99  size_t col = coordinate.col;
100 
101  assert(row < rows());
102  assert(col < cols());
103 
104  column_type const & column = columns[col];
105 
106  if constexpr(use_max_errors)
107  if (!(row < column.max_rows))
109 
110  if (row == 0u)
111  {
112  if constexpr(is_semi_global)
114 
115  if (col == 0u)
117 
118  return detail::trace_directions::left;
119  }
120 
121  size_t const idx = (row - 1u) / word_size;
122  size_t const offset = (row - 1u) % word_size;
123 
124  bool const left = std::bitset<word_size>(column.left[idx])[offset];
125  bool const diagonal = std::bitset<word_size>(column.diagonal[idx])[offset];
126  bool const up = std::bitset<word_size>(column.up[idx])[offset];
127 
128  auto const dir = (left ? detail::trace_directions::left : detail::trace_directions::none) |
129  (diagonal ? detail::trace_directions::diagonal : detail::trace_directions::none) |
130  (up ? detail::trace_directions::up : detail::trace_directions::none);
131 
132  return dir;
133  }
134 
136  size_t rows() const noexcept
137  {
138  return rows_size;
139  }
140 
142  size_t cols() const noexcept
143  {
144  return columns.size();
145  }
146 
147 protected:
149  struct max_errors_state
150  {
153  size_t max_rows{};
154  };
155 
157  struct trace_matrix_state
158  {
162  std::vector<word_type> diagonal{};
165  };
166 
168  struct column_type :
169  enable_state_t<true, trace_matrix_state>,
170  enable_state_t<use_max_errors, max_errors_state>
171  {};
172 
178  void add_column(std::vector<word_type> left, std::vector<word_type> diagonal, std::vector<word_type> up)
180  requires (!use_max_errors)
182  {
183  column_type column{};
184  column.left = std::move(left);
185  column.diagonal = std::move(diagonal);
186  column.up = std::move(up);
187 
188  columns.push_back(std::move(column));
189  }
190 
197  void add_column(std::vector<word_type> left, std::vector<word_type> diagonal, std::vector<word_type> up,
198  size_t const max_rows)
200  requires use_max_errors
202  {
203  column_type column{};
204  column.left = std::move(left);
205  column.diagonal = std::move(diagonal);
206  column.up = std::move(up);
207  column.max_rows = max_rows;
208 
209  columns.push_back(std::move(column));
210  }
211 
212 private:
214  size_t rows_size{};
216  std::vector<column_type> columns{};
217 };
218 
219 } // namespace seqan3::detail
bitset
bit_manipulation.hpp
Provides utility functions for bit twiddling.
std::vector
std::bitset::size
T size(T... args)
trace_directions.hpp
Provides the declaration of seqan3::detail::trace_directions.
seqan3::views::move
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
std::left
T left(T... args)
edit_distance_fwd.hpp
Forwards for seqan3::edit_distance_unbanded related types.
seqan3::none
@ none
No flag is set.
Definition: debug_stream_type.hpp:30
seqan3::pack_traits::at
typename decltype(detail::at< idx, pack_t... >())::type at
Return the type at given index from the type pack.
Definition: traits.hpp:221