RTC Toolkit  2.0.0
matrixBuffer.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_MATRIXBUFFER_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_MATRIXBUFFER_HPP
14 
15 #include <cassert>
16 #include <memory>
17 #include <vector>
18 
19 namespace rtctk::componentFramework {
20 
21 template <typename T, typename A = std::allocator<T>>
22 class MatrixBuffer : public std::vector<T, A> {
23 public:
24  using typename std::vector<T, A>::size_type;
25  using typename std::vector<T, A>::value_type;
26  using typename std::vector<T, A>::reference;
27  using typename std::vector<T, A>::const_reference;
28 
29  constexpr MatrixBuffer() noexcept(noexcept(A())) : std::vector<T, A>(), m_nrows(0), m_ncols(0) {
30  }
31 
32  constexpr MatrixBuffer(const MatrixBuffer& other)
33  : std::vector<T, A>(other), m_nrows(other.m_nrows), m_ncols(other.m_ncols) {
34  }
35 
36  constexpr MatrixBuffer& operator=(const MatrixBuffer& other) {
37  std::vector<T, A>::operator=(other);
38  m_nrows = other.m_nrows;
39  m_ncols = other.m_ncols;
40  return *this;
41  }
42 
43  constexpr MatrixBuffer(MatrixBuffer&& other) noexcept
44  : std::vector<T, A>(std::forward<MatrixBuffer>(other))
45  , m_nrows(std::move(other.m_nrows))
46  , m_ncols(std::move(other.m_ncols)) {
47  }
48 
49  constexpr MatrixBuffer& operator=(MatrixBuffer&& other) noexcept(
50  std::allocator_traits<A>::propagate_on_container_move_assignment::value or
51  std::allocator_traits<A>::is_always_equal::value) {
52  std::vector<T, A>::operator=(std::forward<MatrixBuffer>(other));
53  m_nrows = std::move(other.m_nrows);
54  m_ncols = std::move(other.m_ncols);
55  return *this;
56  }
57 
58  constexpr MatrixBuffer(size_type n, size_type m, const std::vector<T, A>& data)
59  : std::vector<T, A>(data), m_nrows(n), m_ncols(m) {
60  assert(n * m == data.size());
61  }
62 
63  // The resize method is inherited from the base class, therefore the following warning is a
64  // false positive.
65  // NOLINTNEXTLINE(readability-identifier-naming)
66  constexpr void resize(size_type n, size_type m) {
67  std::vector<T, A>::resize(n * m);
68  m_nrows = n;
69  m_ncols = m;
70  }
71 
72  // NOLINTNEXTLINE(readability-identifier-naming)
73  constexpr void resize(size_type n, size_type m, const value_type& value) {
74  std::vector<T, A>::resize(n * m, value);
75  m_nrows = n;
76  m_ncols = m;
77  }
78 
79  constexpr reference operator()(size_type n, size_type m) {
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  constexpr const_reference operator()(size_type n, size_type m) const {
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  inline size_type GetNrows() const {
92  return m_nrows;
93  }
94 
95  inline size_type GetNcols() const {
96  return m_ncols;
97  }
98 
99 private:
100  size_type m_nrows;
101  size_type m_ncols;
102 };
103 
108 template <typename T, typename A>
109 constexpr bool operator==(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
110  return lhs.GetNrows() == rhs.GetNrows() and lhs.GetNcols() == rhs.GetNcols() and
111  static_cast<std::vector<T, A>>(lhs) == static_cast<std::vector<T, A>>(rhs);
112 }
113 
118 template <typename T, typename A>
119 constexpr bool operator!=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
120  return lhs.GetNrows() != rhs.GetNrows() or lhs.GetNcols() != rhs.GetNcols() or
121  static_cast<std::vector<T, A>>(lhs) != static_cast<std::vector<T, A>>(rhs);
122 }
123 
132 template <typename T, typename A>
133 constexpr bool operator<(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
134  if (lhs.size() < rhs.size()) {
135  return true;
136  } else if (lhs.size() == rhs.size()) {
137  if (lhs.GetNrows() < rhs.GetNrows()) {
138  return true;
139  } else if (lhs.GetNrows() > rhs.GetNrows()) {
140  return false;
141  }
142  }
143  return static_cast<std::vector<T, A>>(lhs) < static_cast<std::vector<T, A>>(rhs);
144 }
145 
150 template <typename T, typename A>
151 constexpr bool operator<=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
152  if (lhs.size() < rhs.size()) {
153  return true;
154  } else if (lhs.size() == rhs.size()) {
155  if (lhs.GetNrows() < rhs.GetNrows()) {
156  return true;
157  } else if (lhs.GetNrows() > rhs.GetNrows()) {
158  return false;
159  }
160  }
161  return static_cast<std::vector<T, A>>(lhs) <= static_cast<std::vector<T, A>>(rhs);
162 }
163 
172 template <typename T, typename A>
173 constexpr bool operator>(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
174  if (lhs.size() > rhs.size()) {
175  return true;
176  } else if (lhs.size() == rhs.size()) {
177  if (lhs.GetNrows() > rhs.GetNrows()) {
178  return true;
179  } else if (lhs.GetNrows() < rhs.GetNrows()) {
180  return false;
181  }
182  }
183  return static_cast<std::vector<T, A>>(lhs) > static_cast<std::vector<T, A>>(rhs);
184 }
185 
190 template <typename T, typename A>
191 constexpr bool operator>=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
192  if (lhs.size() > rhs.size()) {
193  return true;
194  } else if (lhs.size() == rhs.size()) {
195  if (lhs.GetNrows() > rhs.GetNrows()) {
196  return true;
197  } else if (lhs.GetNrows() < rhs.GetNrows()) {
198  return false;
199  }
200  }
201  return static_cast<std::vector<T, A>>(lhs) >= static_cast<std::vector<T, A>>(rhs);
202 }
203 
204 } // namespace rtctk::componentFramework
205 
206 #endif // RTCTK_COMPONENTFRAMEWORK_MATRIXBUFFER_HPP
rtctk::componentFramework::MatrixBuffer::GetNrows
size_type GetNrows() const
Definition: matrixBuffer.hpp:91
rtctk::componentFramework::MatrixBuffer::resize
constexpr void resize(size_type n, size_type m)
Definition: matrixBuffer.hpp:66
rtctk::componentFramework::MatrixBuffer::MatrixBuffer
constexpr MatrixBuffer() noexcept(noexcept(A()))
Definition: matrixBuffer.hpp:29
rtctk::componentFramework::MatrixBuffer::MatrixBuffer
constexpr MatrixBuffer(const MatrixBuffer &other)
Definition: matrixBuffer.hpp:32
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::operator<=
bool operator<=(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:314
rtctk::componentFramework::operator!=
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:119
rtctk::componentFramework::MatrixBuffer::resize
constexpr void resize(size_type n, size_type m, const value_type &value)
Definition: matrixBuffer.hpp:73
rtctk::componentFramework::operator>
bool operator>(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:318
rtctk::componentFramework::MatrixBuffer::operator=
constexpr MatrixBuffer & operator=(const MatrixBuffer &other)
Definition: matrixBuffer.hpp:36
rtctk::componentFramework::MatrixBuffer::MatrixBuffer
constexpr MatrixBuffer(MatrixBuffer &&other) noexcept
Definition: matrixBuffer.hpp:43
std
Definition: mudpiProcessingError.hpp:119
rtctk::componentFramework::MatrixBuffer::MatrixBuffer
constexpr MatrixBuffer(size_type n, size_type m, const std::vector< T, A > &data)
Definition: matrixBuffer.hpp:58
rtctk::componentFramework::MatrixBuffer::operator()
constexpr const_reference operator()(size_type n, size_type m) const
Definition: matrixBuffer.hpp:85
rtctk::componentFramework::operator==
bool operator==(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:306
rtctk::componentFramework::MatrixBuffer::operator()
constexpr reference operator()(size_type n, size_type m)
Definition: matrixBuffer.hpp:79
rtctk::componentFramework::MatrixBuffer::operator=
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:49
rtctk::componentFramework::operator<
bool operator<(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:310
rtctk::componentFramework::MatrixBuffer::GetNcols
size_type GetNcols() const
Definition: matrixBuffer.hpp:95
rtctk::componentFramework::MatrixBuffer
Definition: matrixBuffer.hpp:22
rtctk::componentFramework::operator>=
bool operator>=(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:322