SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 <seqan3/std/ranges>
16 #include <seqan3/std/span>
17 #include <vector>
18 
27 
28 namespace seqan3::detail
29 {
30 
49 template <typename trace_t>
51  requires std::same_as<trace_t, trace_directions>
53 class trace_matrix_full
54 {
55 private:
57  using matrix_t = two_dimensional_matrix<trace_t,
58  aligned_allocator<trace_t, sizeof(trace_t)>,
59  matrix_major_order::column>;
61  using physical_column_t = std::vector<trace_t>;
63  using virtual_column_t = decltype(views::repeat_n(trace_t{}, 1));
64 
65  class iterator;
66 
68  matrix_t complete_matrix{};
70  physical_column_t horizontal_column{};
72  virtual_column_t vertical_column{};
74  size_t column_count{};
76  size_t row_count{};
77 
78 public:
82  trace_matrix_full() = default;
83  trace_matrix_full(trace_matrix_full const &) = default;
84  trace_matrix_full(trace_matrix_full &&) = default;
85  trace_matrix_full & operator=(trace_matrix_full const &) = default;
86  trace_matrix_full & operator=(trace_matrix_full &&) = default;
87  ~trace_matrix_full() = default;
88 
112  template <std::integral column_index_t, std::integral row_index_t>
113  void resize(column_index_type<column_index_t> const column_count,
114  row_index_type<row_index_t> const row_count)
115  {
116  this->column_count = column_count.get();
117  this->row_count = row_count.get();
118  complete_matrix.resize(number_rows{this->row_count}, number_cols{this->column_count});
119  horizontal_column.resize(this->row_count);
120  vertical_column = views::repeat_n(trace_t{}, this->row_count);
121  }
122 
126  iterator begin()
128  {
129  return iterator{*this, 0u};
130  }
131 
133  iterator begin() const = delete;
134 
136  iterator end()
137  {
138  return iterator{*this, column_count};
139  }
140 
142  iterator end() const = delete;
144 };
145 
155 template <typename trace_t>
157  requires std::same_as<trace_t, trace_directions>
159 class trace_matrix_full<trace_t>::iterator
160 {
161 private:
163  using single_trace_column_type = std::span<trace_t>;
165  using matrix_column_type = decltype(views::zip(std::declval<single_trace_column_type>(),
166  std::declval<physical_column_t &>(),
167  std::declval<virtual_column_t &>()));
169  using matrix_column_value_t = std::vector<std::ranges::range_value_t<matrix_column_type>>;
170 
171  // Defines a proxy that can be converted to the value type.
172  class column_proxy;
173 
175  trace_matrix_full * host_ptr{nullptr};
177  size_t current_column_id{};
178 
179 public:
183  using value_type = matrix_column_value_t;
186  using reference = column_proxy;
188  using pointer = void;
190  using difference_type = std::ptrdiff_t;
192  using iterator_category = std::input_iterator_tag;
194 
198  iterator() noexcept = default;
199  iterator(iterator const &) noexcept = default;
200  iterator(iterator &&) noexcept = default;
201  iterator & operator=(iterator const &) noexcept = default;
202  iterator & operator=(iterator &&) noexcept = default;
203  ~iterator() = default;
204 
210  explicit iterator(trace_matrix_full & host_matrix, size_t const initial_column_id) noexcept :
211  host_ptr{std::addressof(host_matrix)},
212  current_column_id{initial_column_id}
213  {}
215 
219  reference operator*() const
221  {
222  auto column_begin = host_ptr->complete_matrix.data() + current_column_id * host_ptr->row_count;
223  single_trace_column_type single_trace_column{column_begin, column_begin + host_ptr->row_count};
224 
225  return column_proxy{views::zip(std::move(single_trace_column),
226  host_ptr->horizontal_column,
227  host_ptr->vertical_column)};
228  }
230 
234  iterator & operator++()
236  {
237  ++current_column_id;
238  return *this;
239  }
240 
242  void operator++(int)
243  {
244  ++(*this);
245  }
247 
251  friend bool operator==(iterator const & lhs, iterator const & rhs) noexcept
253  {
254  return lhs.current_column_id == rhs.current_column_id;
255  }
256 
258  friend bool operator!=(iterator const & lhs, iterator const & rhs) noexcept
259  {
260  return !(lhs == rhs);
261  }
263 };
264 
273 template <typename trace_t>
275  requires std::same_as<trace_t, trace_directions>
277 class trace_matrix_full<trace_t>::iterator::column_proxy : public std::ranges::view_interface<column_proxy>
278 {
279 private:
281  matrix_column_type column;
282 
283 public:
287  column_proxy() noexcept = default;
288  column_proxy(column_proxy const &) noexcept = default;
289  column_proxy(column_proxy &&) noexcept = default;
290  column_proxy & operator=(column_proxy const &) noexcept = default;
291  column_proxy & operator=(column_proxy &&) noexcept = default;
292  ~column_proxy() = default;
293 
298  explicit column_proxy(matrix_column_type && column) noexcept : column{std::move(column)}
299  {}
301 
305  std::ranges::iterator_t<matrix_column_type> begin()
307  {
308  return column.begin();
309  }
311  std::ranges::iterator_t<matrix_column_type> begin() const = delete;
312 
314  std::ranges::sentinel_t<matrix_column_type> end()
315  {
316  return column.end();
317  }
318 
320  std::ranges::sentinel_t<matrix_column_type> end() const = delete;
322 
324  constexpr operator matrix_column_value_t() const
325  {
326  matrix_column_value_t target{};
327  std::ranges::copy(column, std::cpp20::back_inserter(target));
328  return target;
329  }
330 };
331 
332 } // namespace seqan3::detail
matrix_coordinate.hpp
Provides seqan3::detail::alignment_coordinate and associated strong types.
zip.hpp
Provides seqan3::views::zip.
span
Provides std::span from the C++20 standard library.
std::rel_ops::operator!=
T operator!=(T... args)
vector
std::input_iterator_tag
template_inspection.hpp
Provides seqan3::type_list and auxiliary type traits.
two_dimensional_matrix.hpp
Provides seqan3::detail::two_dimensional_matrix.
trace_directions.hpp
Provides the declaration of seqan3::detail::trace_directions.
std::addressof
T addressof(T... args)
repeat_n.hpp
Provides seqan3::views::repeat_n.
seqan3::views::move
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
core_language.hpp
Provides concepts for core language types and relations that don't have concepts in C++20 (yet).
aligned_allocator.hpp
Provides seqan3::aligned_allocator.
ranges
Adaptations of concepts from the Ranges TS.
std::begin
T begin(T... args)
std::ptrdiff_t
seqan3::views::repeat_n
constexpr auto repeat_n
A view factory that repeats a given value n times.
Definition: repeat_n.hpp:94
std::end
T end(T... args)