RTC Toolkit 4.0.1
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
38 constexpr MatrixBuffer(const MatrixBuffer& other)
39 : std::vector<T, A>(other), m_nrows(other.m_nrows), m_ncols(other.m_ncols) {
40 }
41
42 constexpr MatrixBuffer& operator=(const MatrixBuffer& other) {
43 std::vector<T, A>::operator=(other);
44 m_nrows = other.m_nrows;
45 m_ncols = other.m_ncols;
46 return *this;
47 }
48
49 constexpr MatrixBuffer(MatrixBuffer&& other) noexcept
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
105private:
106 size_type m_nrows;
107 size_type m_ncols;
108};
109
114template <typename T, typename A>
115constexpr bool operator==(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
116 return lhs.GetNrows() == rhs.GetNrows() and lhs.GetNcols() == rhs.GetNcols() and
117 static_cast<std::vector<T, A>>(lhs) == static_cast<std::vector<T, A>>(rhs);
118}
119
124template <typename T, typename A>
125constexpr bool operator!=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
126 return lhs.GetNrows() != rhs.GetNrows() or lhs.GetNcols() != rhs.GetNcols() or
127 static_cast<std::vector<T, A>>(lhs) != static_cast<std::vector<T, A>>(rhs);
128}
129
138template <typename T, typename A>
139constexpr bool operator<(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
140 if (lhs.size() < rhs.size()) {
141 return true;
142 } else if (lhs.size() == rhs.size()) {
143 if (lhs.GetNrows() < rhs.GetNrows()) {
144 return true;
145 } else if (lhs.GetNrows() > rhs.GetNrows()) {
146 return false;
147 }
148 }
149 return static_cast<std::vector<T, A>>(lhs) < static_cast<std::vector<T, A>>(rhs);
150}
151
156template <typename T, typename A>
157constexpr bool operator<=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
158 if (lhs.size() < rhs.size()) {
159 return true;
160 } else if (lhs.size() == rhs.size()) {
161 if (lhs.GetNrows() < rhs.GetNrows()) {
162 return true;
163 } else if (lhs.GetNrows() > rhs.GetNrows()) {
164 return false;
165 }
166 }
167 return static_cast<std::vector<T, A>>(lhs) <= static_cast<std::vector<T, A>>(rhs);
168}
169
178template <typename T, typename A>
179constexpr bool operator>(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
180 if (lhs.size() > rhs.size()) {
181 return true;
182 } else if (lhs.size() == rhs.size()) {
183 if (lhs.GetNrows() > rhs.GetNrows()) {
184 return true;
185 } else if (lhs.GetNrows() < rhs.GetNrows()) {
186 return false;
187 }
188 }
189 return static_cast<std::vector<T, A>>(lhs) > static_cast<std::vector<T, A>>(rhs);
190}
191
196template <typename T, typename A>
197constexpr bool operator>=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
198 if (lhs.size() > rhs.size()) {
199 return true;
200 } else if (lhs.size() == rhs.size()) {
201 if (lhs.GetNrows() > rhs.GetNrows()) {
202 return true;
203 } else if (lhs.GetNrows() < rhs.GetNrows()) {
204 return false;
205 }
206 }
207 return static_cast<std::vector<T, A>>(lhs) >= static_cast<std::vector<T, A>>(rhs);
208}
209
210} // namespace rtctk::componentFramework
211
212#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
Definition: commandReplier.cpp:22
bool operator<(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:354
bool operator>=(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:366
bool operator<=(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:358
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:125
bool operator==(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:350
bool operator>(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:362