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