SeqAn3  3.2.0-rc.1
The Modern C++ library for sequence analysis.
alignment_trace_matrix_full.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 <iterator>
16 #include <ranges>
17 
23 
24 namespace seqan3::detail
25 {
26 
49 template <typename trace_t, bool coordinate_only = false>
50 class alignment_trace_matrix_full :
51  protected alignment_trace_matrix_base<trace_t>,
52  public alignment_matrix_column_major_range_base<alignment_trace_matrix_full<trace_t, coordinate_only>>
53 {
54 private:
55  static_assert(std::same_as<trace_t, trace_directions> || simd_concept<trace_t>,
56  "Value type must either be a trace_directions object or a simd vector.");
57 
59  using matrix_base_t = alignment_trace_matrix_base<trace_t>;
61  using range_base_t =
62  alignment_matrix_column_major_range_base<alignment_trace_matrix_full<trace_t, coordinate_only>>;
63 
65  friend range_base_t;
66 
67 protected:
68  using typename matrix_base_t::coordinate_type;
69  using typename matrix_base_t::element_type;
70  using typename range_base_t::alignment_column_type;
72  using column_data_view_type =
73  std::conditional_t<coordinate_only,
74  decltype(std::views::iota(coordinate_type{}, coordinate_type{})),
75  decltype(views::zip(std::declval<std::span<element_type>>(),
76  std::declval<std::span<element_type>>(),
77  std::views::iota(coordinate_type{}, coordinate_type{})))>;
78 
79 public:
84  using value_type =
85  alignment_trace_matrix_proxy<coordinate_type,
88  using reference = value_type;
90  using iterator = typename range_base_t::iterator;
92  using sentinel = typename range_base_t::sentinel;
93  using typename matrix_base_t::size_type;
95 
99  constexpr alignment_trace_matrix_full() = default;
100  constexpr alignment_trace_matrix_full(alignment_trace_matrix_full const &) = default;
101  constexpr alignment_trace_matrix_full(alignment_trace_matrix_full &&) = default;
102  constexpr alignment_trace_matrix_full & operator=(alignment_trace_matrix_full const &) = default;
103  constexpr alignment_trace_matrix_full & operator=(alignment_trace_matrix_full &&) = default;
104  ~alignment_trace_matrix_full() = default;
105 
119  template <std::ranges::forward_range first_sequence_t, std::ranges::forward_range second_sequence_t>
120  constexpr alignment_trace_matrix_full(first_sequence_t && first,
121  second_sequence_t && second,
122  [[maybe_unused]] trace_t const initial_value = trace_t{})
123  {
124  matrix_base_t::num_cols = static_cast<size_type>(std::ranges::distance(first) + 1);
125  matrix_base_t::num_rows = static_cast<size_type>(std::ranges::distance(second) + 1);
126 
127  if constexpr (!coordinate_only)
128  {
129  // Allocate the matrix here.
130  matrix_base_t::data = typename matrix_base_t::pool_type{number_rows{matrix_base_t::num_rows},
131  number_cols{matrix_base_t::num_cols}};
132  matrix_base_t::cache_left.resize(matrix_base_t::num_rows, initial_value);
133  }
134  }
136 
143  auto trace_path(matrix_coordinate const & trace_begin)
144  {
145  static_assert(!coordinate_only, "Requested trace but storing the trace was disabled!");
146 
147  using matrix_iter_t = std::ranges::iterator_t<typename matrix_base_t::pool_type>;
148  using trace_iterator_t = trace_iterator<matrix_iter_t>;
149  using path_t = std::ranges::subrange<trace_iterator_t, std::default_sentinel_t>;
150 
151  if (trace_begin.row >= matrix_base_t::num_rows || trace_begin.col >= matrix_base_t::num_cols)
152  throw std::invalid_argument{"The given coordinate exceeds the matrix in vertical or horizontal direction."};
153 
154  return path_t{trace_iterator_t{matrix_base_t::data.begin() + matrix_offset{trace_begin}},
155  std::default_sentinel};
156  }
157 
158 private:
160  constexpr alignment_column_type initialise_column(size_type const column_index) noexcept
161  {
162  coordinate_type row_begin{column_index_type{column_index}, row_index_type{0u}};
163  coordinate_type row_end{column_index_type{column_index}, row_index_type{matrix_base_t::num_rows}};
164  if constexpr (coordinate_only)
165  {
166  return alignment_column_type{
167  *this,
168  column_data_view_type{std::views::iota(std::move(row_begin), std::move(row_end))}};
169  }
170  else
171  {
172  matrix_coordinate current_position{row_index_type{0u}, column_index_type{column_index}};
173  auto col = views::zip(
174  std::span<element_type>{std::addressof(matrix_base_t::data[current_position]), matrix_base_t::num_rows},
175  std::span<element_type>{matrix_base_t::cache_left},
176  std::views::iota(std::move(row_begin), std::move(row_end)));
177  return alignment_column_type{*this, column_data_view_type{col}};
178  }
179  }
180 
182  template <std::random_access_iterator iter_t>
183  constexpr value_type make_proxy(iter_t host_iter) noexcept
184  {
185  if constexpr (coordinate_only)
186  {
187  return {*host_iter, std::ignore, std::ignore, std::ignore, std::ignore};
188  }
189  else
190  {
191  return {
192  std::get<2>(*host_iter), // the coordinate.
193  std::get<0>(*host_iter), // the current entry.
194  std::get<1>(*host_iter), // the last left cell to read from.
195  std::get<1>(*host_iter), // the next left cell to write to.
196  matrix_base_t::cache_up, // the last up cell to read/write from/to.
197  };
198  }
199  }
200 };
201 
202 } // namespace seqan3::detail
T addressof(T... args)
Provides seqan3::detail::alignment_matrix_column_major_range_base.
Provides seqan3::detail::alignment_trace_matrix_base.
Provides seqan3::detail::alignment_trace_matrix_proxy.
T declval(T... args)
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
matrix_index< size_t > matrix_coordinate
A coordinate type to access an element inside of a two-dimensional matrix.
Definition: matrix_coordinate.hpp:178
The <ranges> header from C++20's standard library.
Provides seqan3::detail::trace_iterator.
Provides seqan3::views::zip.