RTC Toolkit 6.0.0
Loading...
Searching...
No Matches
matrixBuffer.hpp
Go to the documentation of this file.
1
11
12#ifndef RTCTK_COMPONENTFRAMEWORK_MATRIXBUFFER_HPP
13#define RTCTK_COMPONENTFRAMEWORK_MATRIXBUFFER_HPP
14
15#include <cassert>
16#include <memory>
17#include <vector>
18
20
26template <typename T, typename A = std::allocator<T>>
27class MatrixBuffer : public std::vector<T, A> {
28public:
29 using typename std::vector<T, A>::size_type;
30 using typename std::vector<T, A>::value_type;
31 using typename std::vector<T, A>::reference;
32 using typename std::vector<T, A>::const_reference;
33
34 constexpr MatrixBuffer() noexcept(noexcept(A())) = default;
35
36 constexpr MatrixBuffer(const MatrixBuffer& other) = default;
37
38 constexpr MatrixBuffer& operator=(const MatrixBuffer& other) = default;
39
40 constexpr MatrixBuffer(MatrixBuffer&& other) noexcept = default;
41
42 constexpr MatrixBuffer& operator=(MatrixBuffer&& other) noexcept(
43 std::allocator_traits<A>::propagate_on_container_move_assignment::value or
44 std::allocator_traits<A>::is_always_equal::value) {
45 m_nrows = other.m_nrows;
46 m_ncols = other.m_ncols;
47 std::vector<T, A>::operator=(std::move(other));
48 return *this;
49 }
50
51 constexpr MatrixBuffer(size_type n, size_type m, const std::vector<T, A>& data)
52 : std::vector<T, A>(data), m_nrows(n), m_ncols(m) {
53 assert(n * m == data.size());
54 }
55
56 // The resize method is inherited from the base class, therefore the following warning is a
57 // false positive.
58 // NOLINTNEXTLINE(readability-identifier-naming)
59 constexpr void resize(size_type n, size_type m) {
60 std::vector<T, A>::resize(n * m);
61 m_nrows = n;
62 m_ncols = m;
63 }
64
65 // NOLINTNEXTLINE(readability-identifier-naming)
66 constexpr void resize(size_type n, size_type m, const value_type& value) {
67 std::vector<T, A>::resize(n * m, value);
68 m_nrows = n;
69 m_ncols = m;
70 }
71
72 constexpr reference operator()(size_type n, size_type m) {
73 assert(0 <= n and n < m_nrows);
74 assert(0 <= m and m < m_ncols);
75 return std::vector<T, A>::operator[](n * m_ncols + m);
76 }
77
78 constexpr const_reference operator()(size_type n, size_type m) const {
79 assert(0 <= n and n < m_nrows);
80 assert(0 <= m and m < m_ncols);
81 return std::vector<T, A>::operator[](n * m_ncols + m);
82 }
83
84 inline size_type GetNrows() const {
85 return m_nrows;
86 }
87
88 inline size_type GetNcols() const {
89 return m_ncols;
90 }
91
92 inline size_t GetElementIndex(size_t row, size_t col) {
93 return row * m_ncols + col;
94 }
95
96private:
97 size_type m_nrows{0};
98 size_type m_ncols{0};
99};
100
105template <typename T, typename A>
106constexpr bool operator==(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
107 return lhs.GetNrows() == rhs.GetNrows() and lhs.GetNcols() == rhs.GetNcols() and
108 static_cast<std::vector<T, A>>(lhs) == static_cast<std::vector<T, A>>(rhs);
109}
110
115template <typename T, typename A>
116constexpr bool operator!=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
117 return lhs.GetNrows() != rhs.GetNrows() or lhs.GetNcols() != rhs.GetNcols() or
118 static_cast<std::vector<T, A>>(lhs) != static_cast<std::vector<T, A>>(rhs);
119}
120
129template <typename T, typename A>
130constexpr bool operator<(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
131 if (lhs.size() < rhs.size()) {
132 return true;
133 } else if (lhs.size() == rhs.size()) {
134 if (lhs.GetNrows() < rhs.GetNrows()) {
135 return true;
136 } else if (lhs.GetNrows() > rhs.GetNrows()) {
137 return false;
138 }
139 }
140 return static_cast<std::vector<T, A>>(lhs) < static_cast<std::vector<T, A>>(rhs);
141}
142
147template <typename T, typename A>
148constexpr bool operator<=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
149 if (lhs.size() < rhs.size()) {
150 return true;
151 } else if (lhs.size() == rhs.size()) {
152 if (lhs.GetNrows() < rhs.GetNrows()) {
153 return true;
154 } else if (lhs.GetNrows() > rhs.GetNrows()) {
155 return false;
156 }
157 }
158 return static_cast<std::vector<T, A>>(lhs) <= static_cast<std::vector<T, A>>(rhs);
159}
160
169template <typename T, typename A>
170constexpr bool operator>(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
171 if (lhs.size() > rhs.size()) {
172 return true;
173 } else if (lhs.size() == rhs.size()) {
174 if (lhs.GetNrows() > rhs.GetNrows()) {
175 return true;
176 } else if (lhs.GetNrows() < rhs.GetNrows()) {
177 return false;
178 }
179 }
180 return static_cast<std::vector<T, A>>(lhs) > static_cast<std::vector<T, A>>(rhs);
181}
182
187template <typename T, typename A>
188constexpr bool operator>=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
189 if (lhs.size() > rhs.size()) {
190 return true;
191 } else if (lhs.size() == rhs.size()) {
192 if (lhs.GetNrows() > rhs.GetNrows()) {
193 return true;
194 } else if (lhs.GetNrows() < rhs.GetNrows()) {
195 return false;
196 }
197 }
198 return static_cast<std::vector<T, A>>(lhs) >= static_cast<std::vector<T, A>>(rhs);
199}
200
201} // namespace rtctk::componentFramework
202
203#endif // RTCTK_COMPONENTFRAMEWORK_MATRIXBUFFER_HPP
constexpr MatrixBuffer() noexcept(noexcept(A()))=default
A buffer class representing 2D matrix data.
Definition matrixBuffer.hpp:27
constexpr reference operator()(size_type n, size_type m)
Definition matrixBuffer.hpp:72
constexpr void resize(size_type n, size_type m, const value_type &value)
Definition matrixBuffer.hpp:66
constexpr MatrixBuffer(size_type n, size_type m, const std::vector< T, A > &data)
Definition matrixBuffer.hpp:51
size_type GetNcols() const
Definition matrixBuffer.hpp:88
constexpr MatrixBuffer() noexcept(noexcept(A()))=default
size_type GetNrows() const
Definition matrixBuffer.hpp:84
constexpr const_reference operator()(size_type n, size_type m) const
Definition matrixBuffer.hpp:78
constexpr void resize(size_type n, size_type m)
Definition matrixBuffer.hpp:59
size_t GetElementIndex(size_t row, size_t col)
Definition matrixBuffer.hpp:92
Definition commandReplier.cpp:21
bool operator<=(const DataPointPath &lhs, const char *rhs) noexcept
Definition dataPointPath.hpp:375
bool operator>(const DataPointPath &lhs, const char *rhs) noexcept
Definition dataPointPath.hpp:379
bool operator==(const DataPointPath &lhs, const char *rhs) noexcept
Definition dataPointPath.hpp:367
bool operator>=(const DataPointPath &lhs, const char *rhs) noexcept
Definition dataPointPath.hpp:383
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:116
bool operator<(const DataPointPath &lhs, const char *rhs) noexcept
Definition dataPointPath.hpp:371
Definition ddsSub.hpp:155