SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
trace_iterator_base.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <concepts>
13#include <ranges>
14
18
19namespace seqan3::detail
20{
21
59template <typename derived_t, two_dimensional_matrix_iterator matrix_iter_t>
61{
62private:
63 static_assert(std::same_as<std::iter_value_t<matrix_iter_t>, trace_directions>,
64 "Value type of the underlying iterator must be seqan3::detail::trace_directions.");
65
67 template <typename other_derived_t, two_dimensional_matrix_iterator other_matrix_iter_t>
68 friend class trace_iterator_base;
69
71 friend derived_t;
72
76 constexpr trace_iterator_base() = default;
77 constexpr trace_iterator_base(trace_iterator_base const &) = default;
78 constexpr trace_iterator_base(trace_iterator_base &&) = default;
79 constexpr trace_iterator_base & operator=(trace_iterator_base const &) = default;
82
86 constexpr trace_iterator_base(matrix_iter_t const matrix_iter) noexcept : matrix_iter{matrix_iter}
87 {
89 }
90
101 template <typename other_derived_t, two_dimensional_matrix_iterator other_matrix_iter_t>
102 requires std::constructible_from<matrix_iter_t, other_matrix_iter_t>
107
108public:
112 // Doxygen: https://github.com/seqan/product_backlog/issues/424
115 using reference = trace_directions const &;
116 using pointer = value_type const *;
120
125 reference operator*() const noexcept
126 {
127 return current_direction;
128 }
129
131 pointer operator->() const noexcept
132 {
133 return &current_direction;
134 }
135
137 [[nodiscard]] constexpr matrix_coordinate coordinate() const noexcept
138 {
139 return matrix_iter.coordinate();
140 }
142
147 constexpr derived_t & operator++() noexcept
148 {
149 trace_directions old_dir = *matrix_iter;
150
151 assert(old_dir != trace_directions::none);
152
154 {
155 derived().go_up(matrix_iter);
156 // Set new trace direction if last position was up_open.
157 if (static_cast<bool>(old_dir & trace_directions::carry_up_open))
159 }
161 {
162 derived().go_left(matrix_iter);
163 // Set new trace direction if last position was left_open.
164 if (static_cast<bool>(old_dir & trace_directions::carry_left_open))
166 }
167 else
168 {
170
171 derived().go_diagonal(matrix_iter);
173 }
174 return derived();
175 }
176
178 constexpr derived_t operator++(int) noexcept
179 {
180 derived_t tmp{derived()};
181 ++(*this);
182 return tmp;
183 }
185
190 constexpr friend bool operator==(derived_t const & lhs, derived_t const & rhs) noexcept
191 {
192 return lhs.matrix_iter == rhs.matrix_iter;
193 }
194
196 constexpr friend bool operator==(derived_t const & lhs, std::default_sentinel_t const &) noexcept
197 {
198 return *lhs.matrix_iter == trace_directions::none;
199 }
200
202 constexpr friend bool operator==(std::default_sentinel_t const &, derived_t const & rhs) noexcept
203 {
204 return rhs == std::default_sentinel;
205 }
206
208 constexpr friend bool operator!=(derived_t const & lhs, derived_t const & rhs) noexcept
209 {
210 return !(lhs == rhs);
211 }
212
214 constexpr friend bool operator!=(derived_t const & lhs, std::default_sentinel_t const &) noexcept
215 {
216 return !(lhs == std::default_sentinel);
217 }
218
220 constexpr friend bool operator!=(std::default_sentinel_t const &, derived_t const & rhs) noexcept
221 {
222 return !(rhs == std::default_sentinel);
223 }
225
226private:
232 constexpr void go_left(matrix_iter_t & iter) const noexcept
233 {
235 }
236
238 constexpr void go_up(matrix_iter_t & iter) const noexcept
239 {
241 }
242
244 constexpr void go_diagonal(matrix_iter_t & iter) const noexcept
245 {
247 }
249
251 void set_trace_direction(trace_directions const dir) noexcept
252 {
253 if (static_cast<bool>(dir & trace_directions::diagonal))
254 {
256 }
257 else if (static_cast<bool>(dir & trace_directions::up))
258 {
260 }
261 else if (static_cast<bool>(dir & trace_directions::left))
262 {
264 }
265 else
266 {
268 }
269 }
270
272 constexpr derived_t & derived() noexcept
273 {
274 return static_cast<derived_t &>(*this);
275 }
276
278 constexpr derived_t const & derived() const noexcept
279 {
280 return static_cast<derived_t const &>(*this);
281 }
282
283 matrix_iter_t matrix_iter{};
285};
286
287} // namespace seqan3::detail
A CRTP-base class for trace iterator implementations for the alignment algorithms.
Definition trace_iterator_base.hpp:61
constexpr friend bool operator!=(std::default_sentinel_t const &, derived_t const &rhs) noexcept
copydoc operator!=()
Definition trace_iterator_base.hpp:220
constexpr friend bool operator==(derived_t const &lhs, derived_t const &rhs) noexcept
Returns true if both iterators are equal, false otherwise.
Definition trace_iterator_base.hpp:190
constexpr friend bool operator==(std::default_sentinel_t const &, derived_t const &rhs) noexcept
copydoc operator==()
Definition trace_iterator_base.hpp:202
matrix_iter_t matrix_iter
The underlying matrix iterator.
Definition trace_iterator_base.hpp:283
constexpr derived_t & derived() noexcept
Cast this object to its derived type.
Definition trace_iterator_base.hpp:272
trace_directions const & reference
The reference type.
Definition trace_iterator_base.hpp:115
constexpr trace_iterator_base & operator=(trace_iterator_base const &)=default
Defaulted.
constexpr trace_iterator_base(trace_iterator_base< other_derived_t, other_matrix_iter_t > const &other) noexcept
Constructs from the underlying trace matrix iterator indicating the start of the trace path.
Definition trace_iterator_base.hpp:103
constexpr trace_iterator_base(trace_iterator_base const &)=default
Defaulted.
constexpr derived_t operator++(int) noexcept
Returns an iterator advanced by one.
Definition trace_iterator_base.hpp:178
friend derived_t
Befriend the derived iterator class to allow calling the private constructors.
Definition trace_iterator_base.hpp:71
constexpr friend bool operator==(derived_t const &lhs, std::default_sentinel_t const &) noexcept
Returns true if the pointed-to-element is seqan3::detail::trace_directions::none.
Definition trace_iterator_base.hpp:196
constexpr void go_left(matrix_iter_t &iter) const noexcept
Moves iterator to previous left cell.
Definition trace_iterator_base.hpp:232
constexpr trace_iterator_base()=default
Defaulted.
void set_trace_direction(trace_directions const dir) noexcept
Updates the current trace direction.
Definition trace_iterator_base.hpp:251
constexpr matrix_coordinate coordinate() const noexcept
Returns the current coordinate in two-dimensional space.
Definition trace_iterator_base.hpp:137
value_type const * pointer
The pointer type.
Definition trace_iterator_base.hpp:116
constexpr trace_iterator_base(matrix_iter_t const matrix_iter) noexcept
Constructs from the underlying trace matrix iterator indicating the start of the trace path.
Definition trace_iterator_base.hpp:86
constexpr friend bool operator!=(derived_t const &lhs, std::default_sentinel_t const &) noexcept
Returns true if the pointed-to-element is not seqan3::detail::trace_directions::none.
Definition trace_iterator_base.hpp:214
trace_directions current_direction
The current trace direction.
Definition trace_iterator_base.hpp:284
constexpr trace_iterator_base(trace_iterator_base &&)=default
Defaulted.
constexpr derived_t & operator++() noexcept
Advances the iterator by one.
Definition trace_iterator_base.hpp:147
constexpr void go_up(matrix_iter_t &iter) const noexcept
Moves iterator to previous up cell.
Definition trace_iterator_base.hpp:238
pointer operator->() const noexcept
Returns a pointer to the current trace direction.
Definition trace_iterator_base.hpp:131
constexpr void go_diagonal(matrix_iter_t &iter) const noexcept
Moves iterator to previous diagonal cell.
Definition trace_iterator_base.hpp:244
constexpr trace_iterator_base & operator=(trace_iterator_base &&)=default
Defaulted.
~trace_iterator_base()=default
Defaulted.
constexpr derived_t const & derived() const noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition trace_iterator_base.hpp:278
reference operator*() const noexcept
Returns the current trace direction.
Definition trace_iterator_base.hpp:125
constexpr friend bool operator!=(derived_t const &lhs, derived_t const &rhs) noexcept
Returns true if both iterators are not equal, false otherwise.
Definition trace_iterator_base.hpp:208
trace_directions
The possible directions a trace can have. The values can be combined by the logical |-operator.
Definition trace_directions.hpp:26
@ carry_left_open
Carry bit for the last left open even if it is not the maximum value.
@ up
Trace comes from the above entry.
@ left
Trace comes from the left entry.
@ diagonal
Trace comes from the diagonal entry.
@ carry_up_open
Carry bit for the last up open even if it is not the maximum value.
The internal SeqAn3 namespace.
Definition aligned_sequence_concept.hpp:26
A strong type for designated initialisation of the column index of a matrix.
Definition matrix_coordinate.hpp:29
A representation of a location or offset within a two-dimensional matrix.
Definition matrix_coordinate.hpp:87
A strong type for designated initialisation of the row index of a matrix.
Definition matrix_coordinate.hpp:58
Provides the declaration of seqan3::detail::trace_directions.
Provides seqan3::detail::two_dimensional_matrix_iterator_base.
Provides seqan3::detail::two_dimensional_matrix_iterator.
Hide me