RTC Toolkit 6.0.0-pre2
Loading...
Searching...
No Matches
matrixSpan.hpp
Go to the documentation of this file.
1
12
13#ifndef RTCTK_COMPONENTFRAMEWORK_MATRIXSPAN_HPP
14#define RTCTK_COMPONENTFRAMEWORK_MATRIXSPAN_HPP
15
17
18#include <gsl/span>
19
20#include <array>
21#include <cassert>
22#include <type_traits>
23
25
35template <typename T>
36class MatrixSpan : public gsl::span<T> {
37public:
38 using typename gsl::span<T>::element_type;
39 using typename gsl::span<T>::value_type;
40 using typename gsl::span<T>::size_type;
41 using typename gsl::span<T>::pointer;
42 using const_pointer = const T*;
43 using typename gsl::span<T>::reference;
44 using const_reference = const T&;
45 using typename gsl::span<T>::iterator;
46 using typename gsl::span<T>::reverse_iterator;
47
48 constexpr MatrixSpan() noexcept : gsl::span<T>(), m_nrows(0), m_ncols(0) {
49 }
50
51 constexpr MatrixSpan(const MatrixSpan& other)
52 : gsl::span<T>(other), m_nrows(other.m_nrows), m_ncols(other.m_ncols) {
53 }
54
55 constexpr MatrixSpan& operator=(const MatrixSpan& other) {
56 gsl::span<T>::operator=(other);
57 m_nrows = other.m_nrows;
58 m_ncols = other.m_ncols;
59 return *this;
60 }
61
62 constexpr MatrixSpan(MatrixSpan&& other) noexcept
63 : gsl::span<T>(std::forward<MatrixSpan>(other))
64 , m_nrows(std::move(other.m_nrows))
65 , m_ncols(std::move(other.m_ncols)) {
66 }
67
68 constexpr MatrixSpan& operator=(MatrixSpan&& other) noexcept {
69 gsl::span<T>::operator=(std::forward<MatrixSpan>(other));
70 m_nrows = std::move(other.m_nrows);
71 m_ncols = std::move(other.m_ncols);
72 return *this;
73 }
74
75 constexpr explicit MatrixSpan(size_type n, size_type m, const gsl::span<T>& data)
76 : gsl::span<T>(data), m_nrows(n), m_ncols(m) {
77 assert(n * m == data.size());
78 }
79
80 template <class I>
81 constexpr explicit MatrixSpan(size_type n, size_type m, I first, size_type count)
82 : gsl::span<T>(first, count), m_nrows(n), m_ncols(m) {
83 assert(n * m == count);
84 }
85
86 template <class I>
87 constexpr explicit MatrixSpan(size_type n, size_type m, I first, I last)
88 : gsl::span<T>(first, last), m_nrows(n), m_ncols(m) {
89 assert(n * m == static_cast<size_type>(std::distance(first, last)));
90 }
91
92 // We provide C-style array support in the API on purpose.
93 // NOLINTBEGIN(modernize-avoid-c-arrays)
94 template <std::size_t N>
95 constexpr explicit MatrixSpan(size_type n, size_type m, element_type (&array)[N]) noexcept
96 : gsl::span<T>(array), m_nrows(n), m_ncols(m) {
97 assert(n * m == N);
98 }
99 // NOLINTEND(modernize-avoid-c-arrays)
100
101 template <std::size_t N>
102 constexpr explicit MatrixSpan(size_type n, size_type m, std::array<T, N>& array) noexcept
103 : gsl::span<T>(array), m_nrows(n), m_ncols(m) {
104 assert(n * m == N);
105 }
106
107 template <std::size_t N>
108 constexpr explicit MatrixSpan(size_type n, size_type m, const std::array<T, N>& array) noexcept
109 : gsl::span<T>(array), m_nrows(n), m_ncols(m) {
110 assert(n * m == N);
111 }
112
113 template <typename R>
114 constexpr explicit MatrixSpan(size_type n, size_type m, R&& range) noexcept
115 : gsl::span<T>(std::forward<R>(range)), m_nrows(n), m_ncols(m) {
116 assert(n * m == range.size());
117 }
118
119 template <typename A>
120 constexpr explicit MatrixSpan(MatrixBuffer<T, A>& buffer) noexcept
121 : gsl::span<T>(buffer.data(), buffer.size())
122 , m_nrows(buffer.GetNrows())
123 , m_ncols(buffer.GetNcols()) {
124 }
125
126 template <typename A>
127 requires std::is_const_v<T> or std::is_volatile_v<T>
128 constexpr explicit MatrixSpan(MatrixBuffer<std::remove_cv_t<T>, A>& buffer) noexcept
129 : gsl::span<T>(buffer.data(), buffer.size())
130 , m_nrows(buffer.GetNrows())
131 , m_ncols(buffer.GetNcols()) {
132 }
133
134 template <typename U>
135 requires std::is_same_v<std::remove_cv_t<T>, std::remove_cv_t<U>>
136 bool operator==(const MatrixSpan<U>& rhs) const {
137 return std::equal(this->begin(), this->end(), rhs.begin(), rhs.end());
138 }
139
140 constexpr reference operator()(size_type n, size_type m) {
141 // cppcheck-suppress unsignedPositive
142 assert(0 <= n and n < m_nrows);
143 // cppcheck-suppress unsignedPositive
144 assert(0 <= m and m < m_ncols);
145 return gsl::span<T>::operator[](n * m_ncols + m);
146 }
147
148 constexpr const_reference operator()(size_type n, size_type m) const {
149 // cppcheck-suppress unsignedPositive
150 assert(0 <= n and n < m_nrows);
151 // cppcheck-suppress unsignedPositive
152 assert(0 <= m and m < m_ncols);
153 return gsl::span<T>::operator[](n * m_ncols + m);
154 }
155
156 inline size_type GetNrows() const {
157 return m_nrows;
158 }
159
160 inline size_type GetNcols() const {
161 return m_ncols;
162 }
163
164 inline size_t GetElementIndex(size_t row, size_t col) {
165 return row * m_ncols + col;
166 }
167
168private:
169 size_type m_nrows;
170 size_type m_ncols;
171};
172
173} // namespace rtctk::componentFramework
174
175#endif // RTCTK_COMPONENTFRAMEWORK_MATRIXSPAN_HPP
A buffer class representing 2D matrix data.
Definition matrixBuffer.hpp:28
bool operator==(const MatrixSpan< U > &rhs) const
Definition matrixSpan.hpp:136
const T & const_reference
Definition matrixSpan.hpp:44
constexpr MatrixSpan(MatrixBuffer< std::remove_cv_t< T >, A > &buffer) noexcept
Definition matrixSpan.hpp:128
constexpr MatrixSpan(size_type n, size_type m, R &&range) noexcept
Definition matrixSpan.hpp:114
constexpr MatrixSpan(size_type n, size_type m, I first, I last)
Definition matrixSpan.hpp:87
size_type GetNrows() const
Definition matrixSpan.hpp:156
constexpr MatrixSpan(size_type n, size_type m, const gsl::span< T > &data)
Definition matrixSpan.hpp:75
constexpr MatrixSpan(const MatrixSpan &other)
Definition matrixSpan.hpp:51
constexpr MatrixSpan(MatrixBuffer< T, A > &buffer) noexcept
Definition matrixSpan.hpp:120
constexpr MatrixSpan & operator=(const MatrixSpan &other)
Definition matrixSpan.hpp:55
constexpr MatrixSpan(size_type n, size_type m, I first, size_type count)
Definition matrixSpan.hpp:81
constexpr reference operator()(size_type n, size_type m)
Definition matrixSpan.hpp:140
size_type GetNcols() const
Definition matrixSpan.hpp:160
constexpr MatrixSpan(size_type n, size_type m, element_type(&array)[N]) noexcept
Definition matrixSpan.hpp:95
constexpr MatrixSpan() noexcept
Definition matrixSpan.hpp:48
constexpr MatrixSpan(size_type n, size_type m, std::array< T, N > &array) noexcept
Definition matrixSpan.hpp:102
constexpr const_reference operator()(size_type n, size_type m) const
Definition matrixSpan.hpp:148
size_t GetElementIndex(size_t row, size_t col)
Definition matrixSpan.hpp:164
constexpr MatrixSpan(MatrixSpan &&other) noexcept
Definition matrixSpan.hpp:62
constexpr MatrixSpan & operator=(MatrixSpan &&other) noexcept
Definition matrixSpan.hpp:68
const T * const_pointer
Definition matrixSpan.hpp:42
constexpr MatrixSpan(size_type n, size_type m, const std::array< T, N > &array) noexcept
Definition matrixSpan.hpp:108
Declaration of the MatrixBuffer template class used in APIs.
Definition commandReplier.cpp:22