SeqAn3  3.2.0-rc.1
The Modern C++ library for sequence analysis.
combined_score_and_trace_matrix.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 
15 #include <ranges>
16 
22 
23 namespace seqan3::detail
24 {
44 template <std::ranges::input_range score_matrix_t, std::ranges::input_range trace_matrix_t>
45  requires (std::ranges::input_range<std::ranges::range_reference_t<score_matrix_t>>
46  && std::ranges::input_range<std::ranges::range_reference_t<trace_matrix_t>>)
47 class combined_score_and_trace_matrix
48 {
49 private:
51  using score_type = std::remove_cvref_t<std::tuple_element_t<0, range_innermost_value_t<score_matrix_t>>>;
52 
53  class iterator;
54  class sentinel;
55 
57  score_matrix_t score_matrix{};
59  trace_matrix_t trace_matrix{};
60 
61 public:
65  combined_score_and_trace_matrix() = default;
66  combined_score_and_trace_matrix(combined_score_and_trace_matrix const &) = default;
67  combined_score_and_trace_matrix(combined_score_and_trace_matrix &&) = default;
68  combined_score_and_trace_matrix & operator=(combined_score_and_trace_matrix const &) = default;
69  combined_score_and_trace_matrix & operator=(combined_score_and_trace_matrix &&) = default;
70  ~combined_score_and_trace_matrix() = default;
71 
73 
94  template <std::integral column_index_t, std::integral row_index_t>
95  void resize(column_index_type<column_index_t> const column_count,
96  row_index_type<row_index_t> const row_count,
97  score_type const initial_score = score_type{})
98  {
99  score_matrix_t tmp_score_matrix{};
100  tmp_score_matrix.resize(column_count, row_count, initial_score);
101 
102  trace_matrix_t tmp_trace_matrix{};
103  tmp_trace_matrix.resize(column_count, row_count);
104 
105  score_matrix = std::move(tmp_score_matrix);
106  trace_matrix = std::move(tmp_trace_matrix);
107  }
108 
113  iterator begin()
114  {
115  return iterator{score_matrix.begin(), trace_matrix.begin()};
116  }
117 
119  iterator begin() const = delete;
120 
122  sentinel end()
123  {
124  return sentinel{score_matrix.end()};
125  }
126 
128  sentinel end() const = delete;
130 
139  auto trace_path(matrix_coordinate const & from_coordinate) const
140  {
141  return trace_matrix.trace_path(from_coordinate);
142  }
143 };
144 
156 template <std::ranges::input_range score_matrix_t, std::ranges::input_range trace_matrix_t>
157  requires (std::ranges::input_range<std::ranges::range_reference_t<score_matrix_t>>
158  && std::ranges::input_range<std::ranges::range_reference_t<trace_matrix_t>>)
159 class combined_score_and_trace_matrix<score_matrix_t, trace_matrix_t>::iterator
160 {
161 private:
163  using score_matrix_reference_type = std::ranges::range_reference_t<score_matrix_t>;
165  using trace_matrix_reference_type = std::ranges::range_reference_t<trace_matrix_t>;
166 
167  static_assert(std::ranges::viewable_range<score_matrix_reference_type>);
168  static_assert(std::ranges::viewable_range<trace_matrix_reference_type>);
169 
171  using combined_column_type =
172  decltype(views::zip(std::declval<score_matrix_reference_type>(), std::declval<trace_matrix_reference_type>()));
174  using score_matrix_iter_type = std::ranges::iterator_t<score_matrix_t>;
176  using trace_matrix_iter_type = std::ranges::iterator_t<trace_matrix_t>;
177 
178  // Befriend the base class.
179  template <std::ranges::input_range other_score_matrix_t, std::ranges::input_range other_trace_matrix_t>
180  requires (std::ranges::input_range<std::ranges::range_reference_t<other_score_matrix_t>>
181  && std::ranges::input_range<std::ranges::range_reference_t<other_trace_matrix_t>>)
182  friend class combined_score_and_trace_matrix;
183 
185  static constexpr auto transform_to_combined_matrix_cell = std::views::transform(
186  [](auto && tpl) -> affine_cell_proxy<std::remove_cvref_t<decltype(tpl)>>
187  {
188  using fwd_tuple_t = decltype(tpl);
189  return affine_cell_proxy<std::remove_cvref_t<fwd_tuple_t>>{std::forward<fwd_tuple_t>(tpl)};
190  });
191 
193  score_matrix_iter_type score_matrix_it;
195  trace_matrix_iter_type trace_matrix_it;
196 
197 public:
202  using value_type = decltype(std::declval<combined_column_type>() | transform_to_combined_matrix_cell);
204  using reference = value_type;
206  using pointer = void;
208  using difference_type = std::ptrdiff_t;
210  using iterator_concept = std::input_iterator_tag;
212 
216  iterator() = default;
217  iterator(iterator const &) = default;
218  iterator(iterator &&) = default;
219  iterator & operator=(iterator const &) = default;
220  iterator & operator=(iterator &&) = default;
221  ~iterator() = default;
222 
228  explicit iterator(score_matrix_iter_type score_matrix_it, trace_matrix_iter_type trace_matrix_it) noexcept :
229  score_matrix_it{std::move(score_matrix_it)},
230  trace_matrix_it{std::move(trace_matrix_it)}
231  {}
233 
238  reference operator*() const
239  {
240  return views::zip(*score_matrix_it, *trace_matrix_it) | transform_to_combined_matrix_cell;
241  }
243 
248  iterator & operator++()
249  {
250  ++score_matrix_it;
251  ++trace_matrix_it;
252  return *this;
253  }
254 
256  void operator++(int)
257  {
258  ++(*this);
259  }
261 };
262 
270 template <std::ranges::input_range score_matrix_t, std::ranges::input_range trace_matrix_t>
271  requires (std::ranges::input_range<std::ranges::range_reference_t<score_matrix_t>>
272  && std::ranges::input_range<std::ranges::range_reference_t<trace_matrix_t>>)
273 class combined_score_and_trace_matrix<score_matrix_t, trace_matrix_t>::sentinel
274 {
275 private:
277  using score_matrix_sentinel_type = std::ranges::sentinel_t<score_matrix_t>;
278 
280  score_matrix_sentinel_type score_matrix_sentinel{};
281 
282 public:
286  sentinel() = default;
287  sentinel(sentinel const &) = default;
288  sentinel(sentinel &&) = default;
289  sentinel & operator=(sentinel const &) = default;
290  sentinel & operator=(sentinel &&) = default;
291  ~sentinel() = default;
292 
297  explicit sentinel(score_matrix_sentinel_type score_matrix_sentinel) noexcept :
298  score_matrix_sentinel{std::move(score_matrix_sentinel)}
299  {}
301 
306  friend bool operator==(iterator const & lhs, sentinel const & rhs) noexcept
307  {
308  return rhs.equal(lhs);
309  }
310 
312  friend bool operator==(sentinel const & lhs, iterator const & rhs) noexcept
313  {
314  return rhs == lhs;
315  }
316 
318  friend bool operator!=(iterator const & lhs, sentinel const & rhs) noexcept
319  {
320  return !(lhs == rhs);
321  }
322 
324  friend bool operator!=(sentinel const & lhs, iterator const & rhs) noexcept
325  {
326  return !(lhs == rhs);
327  }
329 
330 private:
340  constexpr bool equal(iterator const & iter) const noexcept
341  {
342  return iter.score_matrix_it == score_matrix_sentinel;
343  }
344 };
345 
346 } // namespace seqan3::detail
Provides seqan3::detail::affine_cell_proxy.
T begin(T... args)
Provides various transformation traits used by the range module.
T end(T... args)
T equal(T... args)
requires requires
The rank_type of the semi-alphabet; defined as the return type of seqan3::to_rank....
Definition: alphabet/concept.hpp:164
decltype(detail::transform< trait_t >(list_t{})) transform
Apply a transformation trait to every type in the list and return a seqan3::type_list of the results.
Definition: type_list/traits.hpp:469
constexpr auto zip
A view adaptor that takes several views and returns tuple-like values from every i-th element of each...
Definition: zip.hpp:573
Provides seqan3::detail::matrix_index, seqan3::detail::matrix_coordinate and associated strong types.
matrix_index< size_t > matrix_coordinate
A coordinate type to access an element inside of a two-dimensional matrix.
Definition: matrix_coordinate.hpp:178
SeqAn specific customisations in the standard namespace.
T operator!=(T... args)
The <ranges> header from C++20's standard library.
Provides concepts that do not have equivalents in C++20.
Provides seqan3::views::zip.