RTC Toolkit 4.0.1
Loading...
Searching...
No Matches
matrixSpan.hpp
Go to the documentation of this file.
1
13#ifndef RTCTK_COMPONENTFRAMEWORK_MATRIXSPAN_HPP
14#define RTCTK_COMPONENTFRAMEWORK_MATRIXSPAN_HPP
15
16#include <array>
17#include <cassert>
18
19#include <gsl/span>
20
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 == std::distance(first, last));
89 }
90
91 template <std::size_t N>
92 constexpr explicit MatrixSpan(size_type n, size_type m, element_type (&array)[N]) noexcept
93 : gsl::span<T>(array), m_nrows(n), m_ncols(m) {
94 assert(n * m == N);
95 }
96
97 template <std::size_t N>
98 constexpr explicit MatrixSpan(size_type n, size_type m, std::array<T, N>& array) noexcept
99 : gsl::span<T>(array), m_nrows(n), m_ncols(m) {
100 assert(n * m == N);
101 }
102
103 template <std::size_t N>
104 constexpr explicit MatrixSpan(size_type n, size_type m, const std::array<T, N>& array) noexcept
105 : gsl::span<T>(array), m_nrows(n), m_ncols(m) {
106 assert(n * m == N);
107 }
108
109 template <typename R>
110 constexpr explicit MatrixSpan(size_type n, size_type m, R&& range) noexcept
111 : gsl::span<T>(std::forward<R>(range)), m_nrows(n), m_ncols(m) {
112 assert(n * m == range.size());
113 }
114
115 template <typename A>
116 constexpr explicit MatrixSpan(MatrixBuffer<T, A>& buffer) noexcept
117 : gsl::span<T>(buffer.data(), buffer.size())
118 , m_nrows(buffer.GetNrows())
119 , m_ncols(buffer.GetNcols()) {
120 }
121
122 constexpr reference operator()(size_type n, size_type m) {
123 // cppcheck-suppress unsignedPositive
124 assert(0 <= n and n < m_nrows);
125 // cppcheck-suppress unsignedPositive
126 assert(0 <= m and m < m_ncols);
127 return gsl::span<T>::operator[](n* m_ncols + m);
128 }
129
130 constexpr const_reference operator()(size_type n, size_type m) const {
131 // cppcheck-suppress unsignedPositive
132 assert(0 <= n and n < m_nrows);
133 // cppcheck-suppress unsignedPositive
134 assert(0 <= m and m < m_ncols);
135 return gsl::span<T>::operator[](n* m_ncols + m);
136 }
137
138 inline size_type GetNrows() const {
139 return m_nrows;
140 }
141
142 inline size_type GetNcols() const {
143 return m_ncols;
144 }
145
146private:
147 size_type m_nrows;
148 size_type m_ncols;
149};
150
151} // namespace rtctk::componentFramework
152
153#endif // RTCTK_COMPONENTFRAMEWORK_MATRIXSPAN_HPP
A buffer class representing 2D matrix data.
Definition: matrixBuffer.hpp:28
A span referencing a 2D matrix buffer.
Definition: matrixSpan.hpp:35
const T & const_reference
Definition: matrixSpan.hpp:43
constexpr MatrixSpan(size_type n, size_type m, R &&range) noexcept
Definition: matrixSpan.hpp:110
constexpr MatrixSpan(size_type n, size_type m, I first, I last)
Definition: matrixSpan.hpp:86
size_type GetNrows() const
Definition: matrixSpan.hpp:138
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:116
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:122
size_type GetNcols() const
Definition: matrixSpan.hpp:142
constexpr MatrixSpan(size_type n, size_type m, element_type(&array)[N]) noexcept
Definition: matrixSpan.hpp:92
constexpr MatrixSpan() noexcept
Definition: matrixSpan.hpp:47
constexpr MatrixSpan(size_type n, size_type m, std::array< T, N > &array) noexcept
Definition: matrixSpan.hpp:98
constexpr const_reference operator()(size_type n, size_type m) const
Definition: matrixSpan.hpp:130
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:104
Declaration of the MatrixBuffer template class used in APIs.
Definition: commandReplier.cpp:22