SeqAn3  3.2.0
The Modern C++ library for sequence analysis.
score_matrix_single_column.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 #include <vector>
17 
25 
26 namespace seqan3::detail
27 {
28 
51 template <typename score_t>
52  requires (arithmetic<score_t> || simd_concept<score_t>)
53 class score_matrix_single_column
54 {
55 private:
57  using physical_column_t = std::vector<score_t, aligned_allocator<score_t, alignof(score_t)>>;
59  using virtual_column_t = decltype(views::repeat_n(score_t{}, 1));
60 
61  class matrix_iterator;
62 
64  physical_column_t optimal_column{};
66  physical_column_t horizontal_column{};
68  virtual_column_t vertical_column{};
70  size_t number_of_columns{};
71 
72 public:
76  score_matrix_single_column() = default;
77  score_matrix_single_column(score_matrix_single_column const &) = default;
78  score_matrix_single_column(score_matrix_single_column &&) = default;
79  score_matrix_single_column & operator=(score_matrix_single_column const &) = default;
80  score_matrix_single_column & operator=(score_matrix_single_column &&) = default;
81  ~score_matrix_single_column() = default;
82 
84 
111  template <std::integral column_index_t, std::integral row_index_t>
112  void resize(column_index_type<column_index_t> const number_of_columns,
113  row_index_type<row_index_t> const number_of_rows,
114  score_t const initial_value = score_t{})
115  {
116  this->number_of_columns = number_of_columns.get();
117  optimal_column.clear();
118  horizontal_column.clear();
119  optimal_column.resize(number_of_rows.get(), initial_value);
120  horizontal_column.resize(number_of_rows.get(), initial_value);
121  vertical_column = views::repeat_n(initial_value, number_of_rows.get());
122  }
123 
128  matrix_iterator begin()
129  {
130  return matrix_iterator{*this, 0u};
131  }
132 
134  matrix_iterator begin() const = delete;
135 
137  matrix_iterator end()
138  {
139  return matrix_iterator{*this, number_of_columns};
140  }
141 
143  matrix_iterator end() const = delete;
145 };
146 
158 template <typename score_t>
159  requires (arithmetic<score_t> || simd_concept<score_t>)
160 class score_matrix_single_column<score_t>::matrix_iterator
161 {
162 private:
164  using matrix_column_t = decltype(views::zip(std::declval<physical_column_t &>(),
165  std::declval<physical_column_t &>(),
166  std::declval<virtual_column_t &>()));
167 
169  static constexpr auto transform_to_affine_cell = std::views::transform(
170  [](auto && tpl) -> affine_cell_proxy<std::remove_cvref_t<decltype(tpl)>>
171  {
172  using fwd_tuple_t = decltype(tpl);
173  return affine_cell_proxy<std::remove_cvref_t<fwd_tuple_t>>{std::forward<fwd_tuple_t>(tpl)};
174  });
175 
177  score_matrix_single_column * host_ptr{nullptr};
179  size_t current_column_id{};
180 
181 public:
186  using value_type = decltype(std::declval<matrix_column_t>() | transform_to_affine_cell);
188  using reference = value_type;
190  using pointer = void;
192  using difference_type = std::ptrdiff_t;
194  using iterator_category = std::input_iterator_tag;
196 
200  matrix_iterator() noexcept = default;
201  matrix_iterator(matrix_iterator const &) noexcept = default;
202  matrix_iterator(matrix_iterator &&) noexcept = default;
203  matrix_iterator & operator=(matrix_iterator const &) noexcept = default;
204  matrix_iterator & operator=(matrix_iterator &&) noexcept = default;
205  ~matrix_iterator() = default;
206 
212  explicit matrix_iterator(score_matrix_single_column & host_matrix, size_t const initial_column_id) noexcept :
213  host_ptr{std::addressof(host_matrix)},
214  current_column_id{initial_column_id}
215  {}
217 
222  reference operator*() const
223  {
224  return views::zip(host_ptr->optimal_column, host_ptr->horizontal_column, host_ptr->vertical_column)
225  | transform_to_affine_cell;
226  }
228 
233  matrix_iterator & operator++()
234  {
235  ++current_column_id;
236  return *this;
237  }
238 
240  void operator++(int)
241  {
242  ++(*this);
243  }
245 
250  friend bool operator==(matrix_iterator const & lhs, matrix_iterator const & rhs) noexcept
251  {
252  return lhs.current_column_id == rhs.current_column_id;
253  }
254 
256  friend bool operator!=(matrix_iterator const & lhs, matrix_iterator const & rhs) noexcept
257  {
258  return !(lhs == rhs);
259  }
261 };
262 
263 } // namespace seqan3::detail
T addressof(T... args)
Provides seqan3::detail::affine_cell_proxy.
Provides seqan3::aligned_allocator.
T begin(T... args)
T end(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
constexpr auto repeat_n
A view factory that repeats a given value n times.
Definition: repeat_n.hpp:91
A type that satisfies std::is_arithmetic_v<t>.
Provides seqan3::detail::matrix_index, seqan3::detail::matrix_coordinate and associated strong types.
T operator!=(T... args)
The <ranges> header from C++20's standard library.
Provides seqan3::views::repeat_n.
Provides concepts that do not have equivalents in C++20.
Provides seqan3::simd::simd_concept.
Provides seqan3::views::zip.