RTC Toolkit 5.0.0
Loading...
Searching...
No Matches
matrixBuffer.hpp
Go to the documentation of this file.
1
13#ifndef RTCTK_COMPONENTFRAMEWORK_MATRIXBUFFER_HPP
14#define RTCTK_COMPONENTFRAMEWORK_MATRIXBUFFER_HPP
15
16#include <cassert>
17#include <memory>
18#include <vector>
19
21
27template <typename T, typename A = std::allocator<T>>
28class MatrixBuffer : public std::vector<T, A> {
29public:
30 using typename std::vector<T, A>::size_type;
31 using typename std::vector<T, A>::value_type;
32 using typename std::vector<T, A>::reference;
33 using typename std::vector<T, A>::const_reference;
34
35 constexpr MatrixBuffer() noexcept(noexcept(A())) : std::vector<T, A>(), m_nrows(0), m_ncols(0) {
36 }
37
39 : std::vector<T, A>(other), m_nrows(other.m_nrows), m_ncols(other.m_ncols) {
40 }
41
43 std::vector<T, A>::operator=(other);
44 m_nrows = other.m_nrows;
45 m_ncols = other.m_ncols;
46 return *this;
47 }
48
50 : std::vector<T, A>(std::forward<MatrixBuffer>(other))
51 , m_nrows(std::move(other.m_nrows))
52 , m_ncols(std::move(other.m_ncols)) {
53 }
54
55 constexpr MatrixBuffer& operator=(MatrixBuffer&& other) noexcept(
56 std::allocator_traits<A>::propagate_on_container_move_assignment::value or
57 std::allocator_traits<A>::is_always_equal::value) {
58 std::vector<T, A>::operator=(std::forward<MatrixBuffer>(other));
59 m_nrows = std::move(other.m_nrows);
60 m_ncols = std::move(other.m_ncols);
61 return *this;
62 }
63
64 constexpr MatrixBuffer(size_type n, size_type m, const std::vector<T, A>& data)
65 : std::vector<T, A>(data), m_nrows(n), m_ncols(m) {
66 assert(n * m == data.size());
67 }
68
69 // The resize method is inherited from the base class, therefore the following warning is a
70 // false positive.
71 // NOLINTNEXTLINE(readability-identifier-naming)
72 constexpr void resize(size_type n, size_type m) {
73 std::vector<T, A>::resize(n * m);
74 m_nrows = n;
75 m_ncols = m;
76 }
77
78 // NOLINTNEXTLINE(readability-identifier-naming)
79 constexpr void resize(size_type n, size_type m, const value_type& value) {
80 std::vector<T, A>::resize(n * m, value);
81 m_nrows = n;
82 m_ncols = m;
83 }
84
85 constexpr reference operator()(size_type n, size_type m) {
86 assert(0 <= n and n < m_nrows);
87 assert(0 <= m and m < m_ncols);
88 return std::vector<T, A>::operator[](n * m_ncols + m);
89 }
90
91 constexpr const_reference operator()(size_type n, size_type m) const {
92 assert(0 <= n and n < m_nrows);
93 assert(0 <= m and m < m_ncols);
94 return std::vector<T, A>::operator[](n * m_ncols + m);
95 }
96
97 inline size_type GetNrows() const {
98 return m_nrows;
99 }
100
101 inline size_type GetNcols() const {
102 return m_ncols;
103 }
104
105 inline size_t GetElementIndex(size_t row, size_t col) {
106 return row * m_ncols + col;
107 }
108
109private:
110 size_type m_nrows;
111 size_type m_ncols;
112};
113
118template <typename T, typename A>
119constexpr bool operator==(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
120 return lhs.GetNrows() == rhs.GetNrows() and lhs.GetNcols() == rhs.GetNcols() and
121 static_cast<std::vector<T, A>>(lhs) == static_cast<std::vector<T, A>>(rhs);
122}
123
128template <typename T, typename A>
129constexpr bool operator!=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
130 return lhs.GetNrows() != rhs.GetNrows() or lhs.GetNcols() != rhs.GetNcols() or
131 static_cast<std::vector<T, A>>(lhs) != static_cast<std::vector<T, A>>(rhs);
132}
133
142template <typename T, typename A>
143constexpr bool operator<(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
144 if (lhs.size() < rhs.size()) {
145 return true;
146 } else if (lhs.size() == rhs.size()) {
147 if (lhs.GetNrows() < rhs.GetNrows()) {
148 return true;
149 } else if (lhs.GetNrows() > rhs.GetNrows()) {
150 return false;
151 }
152 }
153 return static_cast<std::vector<T, A>>(lhs) < static_cast<std::vector<T, A>>(rhs);
154}
155
160template <typename T, typename A>
161constexpr bool operator<=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
162 if (lhs.size() < rhs.size()) {
163 return true;
164 } else if (lhs.size() == rhs.size()) {
165 if (lhs.GetNrows() < rhs.GetNrows()) {
166 return true;
167 } else if (lhs.GetNrows() > rhs.GetNrows()) {
168 return false;
169 }
170 }
171 return static_cast<std::vector<T, A>>(lhs) <= static_cast<std::vector<T, A>>(rhs);
172}
173
182template <typename T, typename A>
183constexpr bool operator>(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
184 if (lhs.size() > rhs.size()) {
185 return true;
186 } else if (lhs.size() == rhs.size()) {
187 if (lhs.GetNrows() > rhs.GetNrows()) {
188 return true;
189 } else if (lhs.GetNrows() < rhs.GetNrows()) {
190 return false;
191 }
192 }
193 return static_cast<std::vector<T, A>>(lhs) > static_cast<std::vector<T, A>>(rhs);
194}
195
200template <typename T, typename A>
201constexpr bool operator>=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
202 if (lhs.size() > rhs.size()) {
203 return true;
204 } else if (lhs.size() == rhs.size()) {
205 if (lhs.GetNrows() > rhs.GetNrows()) {
206 return true;
207 } else if (lhs.GetNrows() < rhs.GetNrows()) {
208 return false;
209 }
210 }
211 return static_cast<std::vector<T, A>>(lhs) >= static_cast<std::vector<T, A>>(rhs);
212}
213
214} // namespace rtctk::componentFramework
215
216#endif // RTCTK_COMPONENTFRAMEWORK_MATRIXBUFFER_HPP
A buffer class representing 2D matrix data.
Definition matrixBuffer.hpp:28
constexpr reference operator()(size_type n, size_type m)
Definition matrixBuffer.hpp:85
constexpr void resize(size_type n, size_type m, const value_type &value)
Definition matrixBuffer.hpp:79
constexpr MatrixBuffer(size_type n, size_type m, const std::vector< T, A > &data)
Definition matrixBuffer.hpp:64
size_type GetNcols() const
Definition matrixBuffer.hpp:101
constexpr MatrixBuffer(const MatrixBuffer &other)
Definition matrixBuffer.hpp:38
size_type GetNrows() const
Definition matrixBuffer.hpp:97
constexpr const_reference operator()(size_type n, size_type m) const
Definition matrixBuffer.hpp:91
constexpr MatrixBuffer(MatrixBuffer &&other) noexcept
Definition matrixBuffer.hpp:49
constexpr MatrixBuffer() noexcept(noexcept(A()))
Definition matrixBuffer.hpp:35
constexpr void resize(size_type n, size_type m)
Definition matrixBuffer.hpp:72
constexpr MatrixBuffer & operator=(MatrixBuffer &&other) noexcept(std::allocator_traits< A >::propagate_on_container_move_assignment::value or std::allocator_traits< A >::is_always_equal::value)
Definition matrixBuffer.hpp:55
constexpr MatrixBuffer & operator=(const MatrixBuffer &other)
Definition matrixBuffer.hpp:42
size_t GetElementIndex(size_t row, size_t col)
Definition matrixBuffer.hpp:105
Definition commandReplier.cpp:22
bool operator<(const DataPointPath &lhs, const char *rhs) noexcept
Definition dataPointPath.hpp:362
bool operator>=(const DataPointPath &lhs, const char *rhs) noexcept
Definition dataPointPath.hpp:374
bool operator<=(const DataPointPath &lhs, const char *rhs) noexcept
Definition dataPointPath.hpp:366
constexpr bool operator!=(const MatrixBuffer< T, A > &lhs, const MatrixBuffer< T, A > &rhs) noexcept
Compares two MatrixBuffer objects and returns true if they do not have the same shape or the elements...
Definition matrixBuffer.hpp:129
elt::mal::future< std::string > InjectReqRepEvent(StateMachineEngine &engine)
Definition malEventInjector.hpp:23
bool operator==(const DataPointPath &lhs, const char *rhs) noexcept
Definition dataPointPath.hpp:358
bool operator>(const DataPointPath &lhs, const char *rhs) noexcept
Definition dataPointPath.hpp:370