SeqAn3  3.0.2
The Modern C++ library for sequence analysis.
alignment_matrix_column_major_range_base.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 
17 #include <seqan3/std/iterator>
18 #include <seqan3/std/ranges>
19 #include <seqan3/std/span>
20 
21 namespace seqan3::detail
22 {
23 
59 template <typename derived_t>
60 class alignment_matrix_column_major_range_base
61 {
62 private:
64  friend derived_t;
65 
75  class alignment_column_type : public std::ranges::view_interface<alignment_column_type>
76  {
77  private:
79  using view_type = typename deferred_type<typename derived_t::column_data_view_type>::type;
80 
81  static_assert(std::ranges::random_access_range<view_type>, "Column view must support random access.");
82  static_assert(std::ranges::sized_range<view_type>, "Column view must be a sized range.");
83  static_assert(std::ranges::view<view_type>, "Column view must be a view.");
84 
86  using sentinel = std::ranges::sentinel_t<view_type>;
87 
97  class iterator_type
98  {
99  public:
100 
104  using value_type = typename deferred_type<typename derived_t::value_type>::type;
107  using reference = typename deferred_type<typename derived_t::reference>::type;
109  using pointer = void;
111  using difference_type = std::ranges::range_difference_t<view_type>;
113  using iterator_category = std::forward_iterator_tag;
115 
119  constexpr iterator_type() = default;
120  constexpr iterator_type(iterator_type const &) = default;
121  constexpr iterator_type(iterator_type &&) = default;
122  constexpr iterator_type & operator=(iterator_type const &) = default;
123  constexpr iterator_type & operator=(iterator_type &&) = default;
124  ~iterator_type() = default;
125 
129  explicit constexpr iterator_type(alignment_column_type & host) :
130  host_ptr{&host},
131  host_iter{host.ref.begin()}
132  {
133  host_ptr->me_ptr->on_column_iterator_creation(host_iter);
134  }
136 
140  constexpr reference operator*() const noexcept
142  {
143  static_assert(std::convertible_to<decltype(host_ptr->me_ptr->make_proxy(host_iter)), reference>,
144  "The returned type of make_proxy must be convertible to the reference type.");
145  assert(host_ptr != nullptr);
146  return host_ptr->me_ptr->make_proxy(host_iter);
147  }
149 
153  constexpr iterator_type & operator++() noexcept
155  {
156  assert(host_ptr != nullptr);
157  assert(host_ptr->me_ptr != nullptr);
158 
159  host_ptr->me_ptr->before_column_iterator_increment(host_iter);
160  ++host_iter;
161  host_ptr->me_ptr->after_column_iterator_increment(host_iter);
162  return *this;
163  }
164 
166  constexpr iterator_type operator++(int) noexcept
167  {
168  iterator_type tmp{*this};
169  ++(*this);
170  return tmp;
171  }
173 
177  constexpr bool operator==(sentinel const & rhs) const noexcept
179  {
180  return host_iter == rhs;
181  }
182 
184  friend constexpr bool operator==(sentinel const & lhs, iterator_type const & rhs) noexcept
185  {
186  return rhs == lhs;
187  }
188 
190  constexpr bool operator==(iterator_type const & rhs) const noexcept
191  {
192  return (host_ptr == rhs.host_ptr) && (host_iter == rhs.host_iter);
193  }
194 
196  constexpr bool operator!=(sentinel const & rhs) const noexcept
197  {
198  return !(*this == rhs);
199  }
200 
202  friend constexpr bool operator!=(sentinel const & lhs, iterator_type const & rhs) noexcept
203  {
204  return rhs != lhs;
205  }
206 
208  constexpr bool operator!=(iterator_type const & rhs) const noexcept
209  {
210  return !(*this == rhs);
211  }
213 
214  private:
216  alignment_column_type * host_ptr{nullptr};
218  std::ranges::iterator_t<view_type> host_iter{};
219  }; // class iterator_type
220 
221  public:
225  constexpr alignment_column_type() = default;
226  constexpr alignment_column_type(alignment_column_type const &) = default;
227  constexpr alignment_column_type(alignment_column_type &&) = default;
228  constexpr alignment_column_type & operator=(alignment_column_type const &) = default;
229  constexpr alignment_column_type & operator=(alignment_column_type &&) = default;
230  ~alignment_column_type() = default;
231 
240  constexpr alignment_column_type(derived_t & me, view_type ref) :
241  ref{std::move(ref)},
242  me_ptr{&me}
243  {}
245 
250  constexpr iterator_type begin() noexcept
252  {
253  assert(me_ptr != nullptr);
254  return iterator_type{*this};
255  }
256 
258  constexpr auto begin() const noexcept = delete; // Not needed by the alignment algorithm
259 
261  constexpr sentinel end() noexcept
262  {
263  return ref.end();
264  }
265 
267  constexpr sentinel end() const noexcept = delete; // Not needed by the alignment algorithm
269 
271  constexpr size_t size() const noexcept
272  {
273  return std::ranges::size(ref);
274  }
275 
276  private:
278  view_type ref{};
280  derived_t * me_ptr{};
281  }; // class alignment_column_type
282 
295  class iterator_type
296  {
297  public:
301  using value_type = alignment_column_type;
304  using reference = value_type;
306  using pointer = void;
308  using difference_type = std::ranges::range_difference_t<alignment_column_type>;
310  using iterator_category = std::input_iterator_tag;
312 
316  constexpr iterator_type() = default;
317  constexpr iterator_type(iterator_type const &) = default;
318  constexpr iterator_type(iterator_type &&) = default;
319  constexpr iterator_type & operator=(iterator_type const &) = default;
320  constexpr iterator_type & operator=(iterator_type &&) = default;
321  ~iterator_type() = default;
322 
326  explicit constexpr iterator_type(derived_t & me) :
327  me_ptr{&me},
328  column_index{0}
329  {}
331 
335  constexpr reference operator*() const noexcept
337  {
338  static_assert(std::convertible_to<decltype(me_ptr->initialise_column(column_index)), reference>,
339  "The returned type of initialise_column must be convertible to the reference type.");
340  return me_ptr->initialise_column(column_index);
341  }
343 
347  constexpr iterator_type & operator++() noexcept
349  {
350  ++column_index;
351  return *this;
352  }
353 
355  constexpr void operator++(int) noexcept
356  {
357  ++(*this);
358  }
360 
364  constexpr bool operator==(std::default_sentinel_t const &) const noexcept
366  {
367  return column_index == me_ptr->num_cols;
368  }
369 
371  friend constexpr bool operator==(std::default_sentinel_t const & lhs, iterator_type const & rhs)
372  noexcept
373  {
374  return rhs == lhs;
375  }
376 
378  constexpr bool operator!=(std::default_sentinel_t const & rhs) const noexcept
379  {
380  return !(*this == rhs);
381  }
382 
384  friend constexpr bool operator!=(std::default_sentinel_t const & lhs, iterator_type const & rhs)
385  noexcept
386  {
387  return rhs != lhs;
388  }
390 
391  private:
393  derived_t * me_ptr;
395  size_t column_index{};
396  }; // class iterator_type
397 
401  constexpr alignment_matrix_column_major_range_base() = default;
404  constexpr alignment_matrix_column_major_range_base(alignment_matrix_column_major_range_base const &) = default;
406  constexpr alignment_matrix_column_major_range_base(alignment_matrix_column_major_range_base &&) = default;
408  constexpr alignment_matrix_column_major_range_base &
409  operator=(alignment_matrix_column_major_range_base const &) = default;
411  constexpr alignment_matrix_column_major_range_base &
412  operator=(alignment_matrix_column_major_range_base &&) = default;
414  ~alignment_matrix_column_major_range_base() = default;
416 
424  SEQAN3_DOXYGEN_ONLY(typedef /*IMPLEMENTATION_DEFINED*/ value_type;)
425 
429  SEQAN3_DOXYGEN_ONLY(typedef /*IMPLEMENTATION_DEFINED*/ column_data_view_type;)
431 
441  SEQAN3_DOXYGEN_ONLY(value_type make_proxy(iter_t host_iter) noexcept {})
442 
455  SEQAN3_DOXYGEN_ONLY(alignment_column_type initialise_column(size_t column_index) {})
456 
461  template <typename iter_t>
462  constexpr void on_column_iterator_creation(iter_t SEQAN3_DOXYGEN_ONLY(host_iter)) noexcept
463  {}
464 
469  template <typename iter_t>
470  constexpr void before_column_iterator_increment(iter_t SEQAN3_DOXYGEN_ONLY(host_iter)) noexcept
471  {}
472 
477  template <typename iter_t>
478  constexpr void after_column_iterator_increment(iter_t SEQAN3_DOXYGEN_ONLY(host_iter)) noexcept
479  {}
481 
483  using iterator = iterator_type;
485  using sentinel = std::default_sentinel_t;
486 
487 public:
492  constexpr iterator begin() noexcept
494  {
495  return iterator{static_cast<derived_t &>(*this)};
496  }
497 
499  constexpr iterator begin() const noexcept = delete; // not needed for the alignment algorithm
500 
502  constexpr sentinel end() noexcept
503  {
504  return std::default_sentinel;
505  }
506 
508  constexpr sentinel end() const noexcept = delete; // not needed for the alignment algorithm
510 };
511 } // namespace seqan3::detail
span
Provides std::span from the C++20 standard library.
std::rel_ops::operator!=
T operator!=(T... args)
iterator
Provides C++20 additions to the <iterator> header.
std::forward_iterator_tag
basic.hpp
Provides various type traits on generic types.
seqan3::views::move
auto const move
A view that turns lvalue-references into rvalue-references.
Definition: move.hpp:68
range.hpp
Provides various transformation traits used by the range module.
seqan3::pack_traits::size
constexpr size_t size
The size of a type pack.
Definition: traits.hpp:116
ranges
Adaptations of concepts from the Ranges TS.
std::begin
T begin(T... args)
std::end
T end(T... args)
std::ref
T ref(T... args)