SeqAn3 3.4.0-rc.1
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
coordinate_matrix.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#include <seqan3/utility/simd/algorithm.hpp>
19#include <seqan3/utility/simd/concept.hpp>
20#include <seqan3/utility/simd/simd_traits.hpp>
21#include <seqan3/utility/simd/views/iota_simd.hpp>
23
24namespace seqan3::detail
25{
26
27//------------------------------------------------------------------------------
28// coordinate_matrix
29//------------------------------------------------------------------------------
30
59template <typename index_t>
60 requires (std::integral<index_t> || simd_index<index_t>)
61class coordinate_matrix
62{
63private:
71 struct convert_to_matrix_coordinate
72 {
74 index_t column_index{};
75
82 auto operator()(index_t const row_index) noexcept
83 {
84 return matrix_index<index_t>{row_index_type{row_index}, column_index_type{column_index}};
85 }
86 };
87
89 template <typename simd_index_t>
90 using lazy_scalar_type_t = typename simd_traits<simd_index_t>::scalar_type;
91
93 using size_type = lazy_conditional_t<simd_concept<index_t>, lazy<lazy_scalar_type_t, index_t>, index_t>;
94
95 // The coordinate matrix iterator.
96 class iterator;
97
99 size_type column_count{};
101 size_type row_count{};
102
103public:
107 coordinate_matrix() = default;
108 coordinate_matrix(coordinate_matrix const &) = default;
109 coordinate_matrix(coordinate_matrix &&) = default;
110 coordinate_matrix & operator=(coordinate_matrix const &) = default;
111 coordinate_matrix & operator=(coordinate_matrix &&) = default;
112 ~coordinate_matrix() = default;
113
132 template <std::integral column_index_t, std::integral row_index_t>
133 void resize(column_index_type<column_index_t> const column_count,
134 row_index_type<row_index_t> const row_count) noexcept
135 {
136 this->column_count = column_count.get();
137 this->row_count = row_count.get();
138 }
140
145 iterator begin() const noexcept
146 {
147 return iterator{size_type{}, row_count};
148 }
149
151 iterator end() const noexcept
152 {
153 return iterator{column_count, row_count};
154 }
156};
157
158//------------------------------------------------------------------------------
159// iterator
160//------------------------------------------------------------------------------
161
171template <typename index_t>
172 requires (std::integral<index_t> || simd_index<index_t>)
173class coordinate_matrix<index_t>::iterator
174{
175private:
177 using iota_view_t = lazy_conditional_t<simd_index<index_t>,
178 lazy<iota_simd_view, index_t>,
179 decltype(std::views::iota(size_type{}, size_type{}))>;
181 index_t column_id{0};
183 size_type row_count{0};
184
185public:
190 using value_type = decltype(std::declval<iota_view_t>()
191 | std::views::transform(convert_to_matrix_coordinate{index_t{} /*column_id*/}));
193 using reference = value_type;
195 using pointer = void;
197 using difference_type = std::ptrdiff_t;
199 using iterator_category = std::forward_iterator_tag;
201
205 iterator() = default;
206 iterator(iterator const &) = default;
207 iterator(iterator &&) = default;
208 iterator & operator=(iterator const &) = default;
209 iterator & operator=(iterator &&) = default;
210 ~iterator() = default;
211
218 explicit iterator(size_type column_id, size_type row_count) noexcept : row_count{std::move(row_count)}
219 {
220 if constexpr (simd_index<index_t>)
221 this->column_id = simd::fill<index_t>(std::move(column_id));
222 else
223 this->column_id = std::move(column_id);
224 }
226
232 auto operator*() const
233 {
234 if constexpr (simd_index<index_t>)
235 {
236 return views::iota_simd<index_t>(size_type{}, row_count)
237 | std::views::transform(convert_to_matrix_coordinate{column_id});
238 }
239 else
240 {
241 return std::views::iota(size_type{}, row_count)
242 | std::views::transform(convert_to_matrix_coordinate{column_id});
243 }
244 }
246
252 iterator & operator++()
253 {
254 // clang: pre-increment of a SIMD vector does not work
255 if constexpr (simd_index<index_t>)
256 column_id = column_id + simd::fill<index_t>(1);
257 else
258 ++column_id;
259
260 return *this;
261 }
262
264 iterator operator++(int)
265 {
266 iterator tmp{*this};
267 ++(*this);
268 return tmp;
269 }
271
277 friend bool operator==(iterator const & lhs, iterator const & rhs)
278 {
279 if constexpr (simd_index<index_t>)
280 return lhs.column_id[0] == rhs.column_id[0];
281 else
282 return lhs.column_id == rhs.column_id;
283 }
284
286 friend bool operator!=(iterator const & lhs, iterator const & rhs)
287 {
288 return !(lhs == rhs);
289 }
291};
292} // namespace seqan3::detail
Provides seqan3::aligned_allocator.
T begin(T... args)
T end(T... args)
Provides lazy template instantiation traits.
Provides seqan3::detail::matrix_index, seqan3::detail::matrix_coordinate and associated strong types.
T operator!=(T... args)
Provides type traits for working with templates.
Hide me