23namespace seqan3::detail
28struct number_cols : strong_type<size_t, number_cols>
31 using strong_type<size_t, number_cols>::strong_type;
36struct number_rows : strong_type<size_t, number_rows>
39 using strong_type<size_t, number_rows>::strong_type;
58template <
typename value_t,
60 matrix_major_order order = matrix_major_order::row>
61class two_dimensional_matrix
71 template <
bool const_range>
80 using value_type =
typename storage_type::value_type;
81 using reference =
typename storage_type::reference;
82 using const_reference =
typename storage_type::const_reference;
83 using pointer =
typename storage_type::pointer;
84 using const_pointer =
typename storage_type::const_pointer;
85 using difference_type =
typename storage_type::difference_type;
86 using size_type =
typename storage_type::size_type;
87 using iterator = basic_iterator<false>;
88 using const_iterator = basic_iterator<true>;
94 two_dimensional_matrix() =
default;
95 two_dimensional_matrix(two_dimensional_matrix
const &) =
default;
96 two_dimensional_matrix(two_dimensional_matrix &&) =
default;
97 two_dimensional_matrix & operator=(two_dimensional_matrix
const &) =
default;
98 two_dimensional_matrix & operator=(two_dimensional_matrix &&) =
default;
99 ~two_dimensional_matrix() =
default;
105 two_dimensional_matrix(number_rows
const row_dim, number_cols
const col_dim) :
106 row_dim{row_dim.
get()},
107 col_dim{col_dim.
get()}
109 storage.resize(row_dim.get() * col_dim.get());
118 template <std::ranges::forward_range entries_t>
119 requires (std::convertible_to<std::ranges::range_value_t<entries_t>, value_type>)
120 two_dimensional_matrix(number_rows
const row_dim, number_cols
const col_dim, entries_t entries) :
121 row_dim{row_dim.get()},
122 col_dim{col_dim.get()}
124 static_assert(std::move_constructible<std::ranges::range_value_t<entries_t>>,
125 "The value type must be moveable.");
127 assert(
static_cast<size_t>(std::ranges::distance(entries)) == (row_dim.get() * col_dim.get()));
128 storage.resize(row_dim.get() * col_dim.get());
133 two_dimensional_matrix(number_rows
const row_dim, number_cols
const col_dim, storage_type entries) :
134 row_dim{row_dim.
get()},
135 col_dim{col_dim.
get()}
137 assert(
static_cast<size_t>(std::ranges::distance(entries)) == (row_dim.get() * col_dim.get()));
138 storage = std::move(entries);
166 template <
typename other_value_t,
typename other_allocator_t, matrix_major_order other_order>
167 requires std::assignable_from<other_value_t &, value_t &>
168 explicit constexpr two_dimensional_matrix(
169 two_dimensional_matrix<other_value_t, other_allocator_t, other_order>
const & matrix) :
170 two_dimensional_matrix{number_rows{matrix.rows()}, number_cols{matrix.cols()}}
172 for (
size_t i = 0; i < cols(); ++i)
174 for (
size_t j = 0; j < rows(); ++j)
176 matrix_coordinate coord{row_index_type{j}, column_index_type{i}};
177 (*this)[coord] = matrix[coord];
186 constexpr reference operator[](matrix_coordinate
const & coordinate)
noexcept
188 assert(coordinate.col < cols());
189 assert(coordinate.row < rows());
192 + matrix_offset{row_index_type{
static_cast<std::ptrdiff_t>(coordinate.row)},
197 constexpr const_reference operator[](matrix_coordinate
const & coordinate)
const noexcept
199 assert(coordinate.col < cols());
200 assert(coordinate.row < rows());
203 + matrix_offset{row_index_type{
static_cast<std::ptrdiff_t>(coordinate.row)},
208 constexpr reference
at(matrix_coordinate
const & coordinate)
210 if (coordinate.col >= cols())
211 throw std::invalid_argument{
"Column index is out of range. Please check the dimensions of the matrix."};
212 if (coordinate.row >= rows())
213 throw std::invalid_argument{
"Row index is out of range. Please check the dimensions of the matrix."};
215 return (*
this)[coordinate];
219 constexpr const_reference
at(matrix_coordinate
const & coordinate)
const
221 if (coordinate.col >= cols())
222 throw std::invalid_argument{
"Column index is out of range. Please check the dimensions of the matrix."};
223 if (coordinate.row >= rows())
224 throw std::invalid_argument{
"Row index is out of range. Please check the dimensions of the matrix."};
226 return (*
this)[coordinate];
234 void resize(number_rows
const row_dim, number_cols
const col_dim)
236 this->row_dim = row_dim.get();
237 this->col_dim = col_dim.get();
238 storage.resize(this->row_dim * this->col_dim);
242 size_t rows() const noexcept
248 size_t cols() const noexcept
254 constexpr pointer
data() noexcept
256 return storage.data();
260 constexpr const_pointer
data() const noexcept
262 return storage.data();
270 constexpr iterator
begin() noexcept
272 return {*
this, storage.begin()};
275 constexpr const_iterator
begin() const noexcept
277 return {*
this, storage.begin()};
281 constexpr const_iterator
cbegin() const noexcept
287 constexpr iterator
end() noexcept
289 return {*
this, storage.end()};
293 constexpr const_iterator
end() const noexcept
295 return {*
this, storage.end()};
299 constexpr const_iterator
cend() const noexcept
306 storage_type storage;
319template <
typename value_t,
typename allocator_t, matrix_major_order order>
320template <
bool const_range>
321class two_dimensional_matrix<value_t, allocator_t, order>::basic_iterator :
322 public two_dimensional_matrix_iterator_base<basic_iterator<const_range>, order>
326 using parent_t = detail::maybe_const_range_t<const_range, two_dimensional_matrix>;
329 using base_t = two_dimensional_matrix_iterator_base<basic_iterator, order>;
332 template <
typename derived_t, matrix_major_order other_order>
333 friend class two_dimensional_matrix_iterator_base;
336 template <
bool other_const_range>
337 friend class basic_iterator;
340 using storage_iterator = detail::maybe_const_iterator_t<const_range, storage_type>;
351 using pointer =
typename storage_iterator::pointer;
361 constexpr basic_iterator() =
default;
362 constexpr basic_iterator(basic_iterator
const &) =
default;
363 constexpr basic_iterator(basic_iterator &&) =
default;
364 constexpr basic_iterator & operator=(basic_iterator
const &) =
default;
365 constexpr basic_iterator & operator=(basic_iterator &&) =
default;
366 ~basic_iterator() =
default;
372 constexpr basic_iterator(parent_t & matrix, storage_iterator iter) : matrix_ptr{&matrix}, host_iter{iter}
376 constexpr basic_iterator(basic_iterator<!const_range>
const & other)
noexcept
378 : matrix_ptr{other.matrix_ptr}, host_iter{other.host_iter}
383 using base_t::operator+=;
386 constexpr basic_iterator & operator+=(matrix_offset
const &
offset)
noexcept
388 assert(matrix_ptr !=
nullptr);
390 if constexpr (order == matrix_major_order::column)
392 host_iter += (
offset.col * matrix_ptr->rows());
398 host_iter += (
offset.row * matrix_ptr->cols());
404 matrix_coordinate coordinate() const noexcept
406 assert(matrix_ptr !=
nullptr);
408 auto diff = *
this - matrix_ptr->begin();
409 if constexpr (order == matrix_major_order::column)
410 return {row_index_type{diff % matrix_ptr->rows()}, column_index_type{diff / matrix_ptr->rows()}};
412 return {row_index_type{diff / matrix_ptr->cols()}, column_index_type{diff % matrix_ptr->cols()}};
416 parent_t * matrix_ptr{
nullptr};
417 storage_iterator host_iter{};
Provides various transformation traits used by the range module.
Provides seqan3::detail::deferred_crtp_base.
@ 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:276
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:412
Provides type traits for working with templates.
Provides seqan3::detail::two_dimensional_matrix_iterator_base.