ginkgo/core/base/range_accessors.hpp Source File

ginkgo/core/base/range_accessors.hpp Source File#

Reference API: ginkgo/core/base/range_accessors.hpp Source File
Reference API
range_accessors.hpp
1// SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2//
3// SPDX-License-Identifier: BSD-3-Clause
4
5#ifndef GKO_PUBLIC_CORE_BASE_RANGE_ACCESSORS_HPP_
6#define GKO_PUBLIC_CORE_BASE_RANGE_ACCESSORS_HPP_
7
8
9#include <array>
10
11#include <ginkgo/core/base/range.hpp>
12#include <ginkgo/core/base/types.hpp>
13
14
15namespace gko {
21namespace accessor {
22
23
39template <typename ValueType, size_type Dimensionality>
40class row_major {
41public:
42 friend class range<row_major>;
43
44 static_assert(Dimensionality == 2,
45 "This accessor is only implemented for matrices");
46
50 using value_type = ValueType;
51
56
60 static constexpr size_type dimensionality = 2;
61
62protected:
73 constexpr GKO_ATTRIBUTES explicit row_major(data_type data,
74 size_type num_rows,
75 size_type num_cols,
77 : data{data}, lengths{num_rows, num_cols}, stride{stride}
78 {}
79
80public:
89 constexpr GKO_ATTRIBUTES value_type& operator()(size_type row,
90 size_type col) const
91 {
92 return GKO_ASSERT(row < lengths[0]), GKO_ASSERT(col < lengths[1]),
93 data[row * stride + col];
94 }
95
104 constexpr GKO_ATTRIBUTES range<row_major> operator()(const span& rows,
105 const span& cols) const
106 {
107 return GKO_ASSERT(rows.is_valid()), GKO_ASSERT(cols.is_valid()),
108 GKO_ASSERT(rows <= span{lengths[0]}),
109 GKO_ASSERT(cols <= span{lengths[1]}),
110 range<row_major>(data + rows.begin * stride + cols.begin,
111 rows.end - rows.begin, cols.end - cols.begin,
112 stride);
113 }
114
122 constexpr GKO_ATTRIBUTES size_type length(size_type dimension) const
123 {
124 return dimension < 2 ? lengths[dimension] : 1;
125 }
126
139 template <typename OtherAccessor>
140 GKO_ATTRIBUTES void copy_from(const OtherAccessor& other) const
141 {
142 for (size_type i = 0; i < lengths[0]; ++i) {
143 for (size_type j = 0; j < lengths[1]; ++j) {
144 (*this)(i, j) = other(i, j);
145 }
146 }
147 }
148
153
157 const std::array<const size_type, dimensionality> lengths;
158
163};
164
165
166} // namespace accessor
167} // namespace gko
168
169
170#endif // GKO_PUBLIC_CORE_BASE_RANGE_ACCESSORS_HPP_
Definition range_accessors.hpp:40
constexpr range< row_major > operator()(const span &rows, const span &cols) const
Definition range_accessors.hpp:104
constexpr size_type length(size_type dimension) const
Definition range_accessors.hpp:122
const std::array< const size_type, dimensionality > lengths
Definition range_accessors.hpp:157
constexpr row_major(data_type data, size_type num_rows, size_type num_cols, size_type stride)
Definition range_accessors.hpp:73
const size_type stride
Definition range_accessors.hpp:162
ValueType value_type
Definition range_accessors.hpp:50
void copy_from(const OtherAccessor &other) const
Definition range_accessors.hpp:140
const data_type data
Definition range_accessors.hpp:152
static constexpr size_type dimensionality
Definition range_accessors.hpp:60
constexpr value_type & operator()(size_type row, size_type col) const
Definition range_accessors.hpp:89
value_type * data_type
Definition range_accessors.hpp:55
Definition range.hpp:297
The Ginkgo namespace.
Definition abstract_factory.hpp:20
std::size_t size_type
Definition types.hpp:89
Definition range.hpp:46
constexpr bool is_valid() const
Definition range.hpp:73
const size_type begin
Definition range.hpp:85
const size_type end
Definition range.hpp:90