SeqAn3 3.3.0
The Modern C++ library for sequence analysis.
Loading...
Searching...
No Matches
two_dimensional_matrix.hpp
Go to the documentation of this file.
1// -----------------------------------------------------------------------------------------------------
2// Copyright (c) 2006-2023, Knut Reinert & Freie Universität Berlin
3// Copyright (c) 2016-2023, 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 <algorithm>
16#include <memory>
17#include <ranges>
18#include <vector>
19
25
26namespace seqan3::detail
27{
28
31struct number_cols : strong_type<size_t, number_cols>
32{
34 using strong_type<size_t, number_cols>::strong_type;
35};
36
39struct number_rows : strong_type<size_t, number_rows>
40{
42 using strong_type<size_t, number_rows>::strong_type;
43};
44
61template <typename value_t,
62 typename allocator_t = std::allocator<value_t>,
63 matrix_major_order order = matrix_major_order::row>
64class two_dimensional_matrix
65{
66private:
70 using storage_type = std::vector<value_t, allocator_t>;
72
73 // Forward declaration. For definition see below.
74 template <bool const_range>
75 class basic_iterator;
76
77public:
81 // Doxygen: https://github.com/seqan/product_backlog/issues/424
83 using value_type = typename storage_type::value_type;
84 using reference = typename storage_type::reference;
85 using const_reference = typename storage_type::const_reference;
86 using pointer = typename storage_type::pointer;
87 using const_pointer = typename storage_type::const_pointer;
88 using difference_type = typename storage_type::difference_type;
89 using size_type = typename storage_type::size_type;
90 using iterator = basic_iterator<false>;
91 using const_iterator = basic_iterator<true>;
93
97 two_dimensional_matrix() = default;
98 two_dimensional_matrix(two_dimensional_matrix const &) = default;
99 two_dimensional_matrix(two_dimensional_matrix &&) = default;
100 two_dimensional_matrix & operator=(two_dimensional_matrix const &) = default;
101 two_dimensional_matrix & operator=(two_dimensional_matrix &&) = default;
102 ~two_dimensional_matrix() = default;
103
108 two_dimensional_matrix(number_rows const row_dim, number_cols const col_dim) :
109 row_dim{row_dim.get()},
110 col_dim{col_dim.get()}
111 {
112 storage.resize(row_dim.get() * col_dim.get());
113 }
114
121 template <std::ranges::forward_range entries_t>
122 requires (std::convertible_to<std::ranges::range_value_t<entries_t>, value_type>)
123 two_dimensional_matrix(number_rows const row_dim, number_cols const col_dim, entries_t entries) :
124 row_dim{row_dim.get()},
125 col_dim{col_dim.get()}
126 {
127 static_assert(std::move_constructible<std::ranges::range_value_t<entries_t>>,
128 "The value type must be moveable.");
129
130 assert(static_cast<size_t>(std::ranges::distance(entries)) == (row_dim.get() * col_dim.get()));
131 storage.resize(row_dim.get() * col_dim.get());
132 std::ranges::move(entries, storage.begin());
133 }
134
136 two_dimensional_matrix(number_rows const row_dim, number_cols const col_dim, storage_type entries) :
137 row_dim{row_dim.get()},
138 col_dim{col_dim.get()}
139 {
140 assert(static_cast<size_t>(std::ranges::distance(entries)) == (row_dim.get() * col_dim.get()));
141 storage = std::move(entries);
142 }
143
169 template <typename other_value_t, typename other_allocator_t, matrix_major_order other_order>
170 requires std::assignable_from<other_value_t &, value_t &>
171 explicit constexpr two_dimensional_matrix(
172 two_dimensional_matrix<other_value_t, other_allocator_t, other_order> const & matrix) :
173 two_dimensional_matrix{number_rows{matrix.rows()}, number_cols{matrix.cols()}}
174 {
175 for (size_t i = 0; i < cols(); ++i)
176 {
177 for (size_t j = 0; j < rows(); ++j)
178 {
179 matrix_coordinate coord{row_index_type{j}, column_index_type{i}};
180 (*this)[coord] = matrix[coord];
181 }
182 }
183 }
185
189 constexpr reference operator[](matrix_coordinate const & coordinate) noexcept
190 {
191 assert(coordinate.col < cols());
192 assert(coordinate.row < rows());
193
194 return *(begin()
195 + matrix_offset{row_index_type{static_cast<std::ptrdiff_t>(coordinate.row)},
196 column_index_type{static_cast<std::ptrdiff_t>(coordinate.col)}});
197 }
198
200 constexpr const_reference operator[](matrix_coordinate const & coordinate) const noexcept
201 {
202 assert(coordinate.col < cols());
203 assert(coordinate.row < rows());
204
205 return *(begin()
206 + matrix_offset{row_index_type{static_cast<std::ptrdiff_t>(coordinate.row)},
207 column_index_type{static_cast<std::ptrdiff_t>(coordinate.col)}});
208 }
209
211 constexpr reference at(matrix_coordinate const & coordinate)
212 {
213 if (coordinate.col >= cols())
214 throw std::invalid_argument{"Column index is out of range. Please check the dimensions of the matrix."};
215 if (coordinate.row >= rows())
216 throw std::invalid_argument{"Row index is out of range. Please check the dimensions of the matrix."};
217
218 return (*this)[coordinate];
219 }
220
222 constexpr const_reference at(matrix_coordinate const & coordinate) const
223 {
224 if (coordinate.col >= cols())
225 throw std::invalid_argument{"Column index is out of range. Please check the dimensions of the matrix."};
226 if (coordinate.row >= rows())
227 throw std::invalid_argument{"Row index is out of range. Please check the dimensions of the matrix."};
228
229 return (*this)[coordinate];
230 }
231
237 void resize(number_rows const row_dim, number_cols const col_dim)
238 {
239 this->row_dim = row_dim.get();
240 this->col_dim = col_dim.get();
241 storage.resize(this->row_dim * this->col_dim);
242 }
243
245 size_t rows() const noexcept
246 {
247 return row_dim;
248 }
249
251 size_t cols() const noexcept
252 {
253 return col_dim;
254 }
255
257 constexpr pointer data() noexcept
258 {
259 return storage.data();
260 }
261
263 constexpr const_pointer data() const noexcept
264 {
265 return storage.data();
266 }
267
273 constexpr iterator begin() noexcept
274 {
275 return {*this, storage.begin()};
276 }
278 constexpr const_iterator begin() const noexcept
279 {
280 return {*this, storage.begin()};
281 }
282
284 constexpr const_iterator cbegin() const noexcept
285 {
286 return begin();
287 }
288
290 constexpr iterator end() noexcept
291 {
292 return {*this, storage.end()};
293 }
294
296 constexpr const_iterator end() const noexcept
297 {
298 return {*this, storage.end()};
299 }
300
302 constexpr const_iterator cend() const noexcept
303 {
304 return end();
305 }
307
308private:
309 storage_type storage;
310 size_type row_dim;
311 size_type col_dim;
312};
313
322template <typename value_t, typename allocator_t, matrix_major_order order>
323template <bool const_range>
324class two_dimensional_matrix<value_t, allocator_t, order>::basic_iterator :
325 public two_dimensional_matrix_iterator_base<basic_iterator<const_range>, order>
326{
327private:
329 using parent_t = detail::maybe_const_range_t<const_range, two_dimensional_matrix>;
330
332 using base_t = two_dimensional_matrix_iterator_base<basic_iterator, order>;
333
335 template <typename derived_t, matrix_major_order other_order>
336 friend class two_dimensional_matrix_iterator_base;
337
339 template <bool other_const_range>
340 friend class basic_iterator;
341
343 using storage_iterator = detail::maybe_const_iterator_t<const_range, storage_type>;
344
345public:
350 using value_type = std::iter_value_t<storage_iterator>;
352 using reference = std::iter_reference_t<storage_iterator>;
354 using pointer = typename storage_iterator::pointer;
356 using difference_type = std::iter_difference_t<storage_iterator>;
358 using iterator_category = std::random_access_iterator_tag;
360
364 constexpr basic_iterator() = default;
365 constexpr basic_iterator(basic_iterator const &) = default;
366 constexpr basic_iterator(basic_iterator &&) = default;
367 constexpr basic_iterator & operator=(basic_iterator const &) = default;
368 constexpr basic_iterator & operator=(basic_iterator &&) = default;
369 ~basic_iterator() = default;
370
375 constexpr basic_iterator(parent_t & matrix, storage_iterator iter) : matrix_ptr{&matrix}, host_iter{iter}
376 {}
377
379 constexpr basic_iterator(basic_iterator<!const_range> const & other) noexcept
380 requires const_range
381 : matrix_ptr{other.matrix_ptr}, host_iter{other.host_iter}
382 {}
384
385 // Import advance operator from base class.
386 using base_t::operator+=;
387
389 constexpr basic_iterator & operator+=(matrix_offset const & offset) noexcept
390 {
391 assert(matrix_ptr != nullptr);
392
393 if constexpr (order == matrix_major_order::column)
394 {
395 host_iter += (offset.col * matrix_ptr->rows());
396 host_iter += offset.row;
397 }
398 else
399 {
400 host_iter += offset.col;
401 host_iter += (offset.row * matrix_ptr->cols());
402 }
403 return *this;
404 }
405
407 matrix_coordinate coordinate() const noexcept
408 {
409 assert(matrix_ptr != nullptr);
410
411 auto diff = *this - matrix_ptr->begin();
412 if constexpr (order == matrix_major_order::column)
413 return {row_index_type{diff % matrix_ptr->rows()}, column_index_type{diff / matrix_ptr->rows()}};
414 else
415 return {row_index_type{diff / matrix_ptr->cols()}, column_index_type{diff % matrix_ptr->cols()}};
416 }
417
418private:
419 parent_t * matrix_ptr{nullptr};
420 storage_iterator host_iter{};
421};
422
423} // namespace seqan3::detail
T begin(T... args)
Provides various transformation traits used by the range module.
T data(T... args)
Provides seqan3::detail::deferred_crtp_base.
T end(T... args)
@ offset
Sequence (seqan3::field::seq) relative start position (0-based), unsigned value.
typename decltype(detail::at< idx >(list_t{}))::type at
Return the type at given index from the type list.
Definition type_list/traits.hpp:279
Provides seqan3::detail::matrix_index, seqan3::detail::matrix_coordinate and associated strong types.
constexpr auto const & get(configuration< configs_t... > const &config) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition configuration.hpp:415
Provides type traits for working with templates.
Provides seqan3::detail::two_dimensional_matrix_iterator_base.