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