RTC Toolkit  0.1.0-alpha
matrixBuffer.hpp
Go to the documentation of this file.
1 
9 #ifndef RTCTK_COMPONENTFRAMEWORK_MATRIXBUFFER_HPP
10 #define RTCTK_COMPONENTFRAMEWORK_MATRIXBUFFER_HPP
11 
12 #include <cassert>
13 #include <memory>
14 #include <vector>
15 
16 namespace rtctk::componentFramework {
17 
18 template <typename T, typename A = std::allocator<T>>
19 class MatrixBuffer : public std::vector<T, A> {
20 public:
21  using typename std::vector<T, A>::size_type;
22  using typename std::vector<T, A>::value_type;
23  using typename std::vector<T, A>::reference;
24  using typename std::vector<T, A>::const_reference;
25 
26  constexpr MatrixBuffer() noexcept(noexcept(A())) :
27  std::vector<T, A>(), m_nrows(0), m_ncols(0) {
28  }
29 
30  constexpr MatrixBuffer(const MatrixBuffer& other) :
31  std::vector<T, A>(other),
32  m_nrows(other.m_nrows),
33  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)
50  noexcept(std::allocator_traits<A>::propagate_on_container_move_assignment::value
51  or 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  constexpr void resize(size_type n, size_type m) {
64  std::vector<T, A>::resize(n*m);
65  m_nrows = n;
66  m_ncols = m;
67  }
68 
69  constexpr void resize(size_type n, size_type m, const value_type& value) {
70  std::vector<T, A>::resize(n*m, value);
71  m_nrows = n;
72  m_ncols = m;
73  }
74 
75  constexpr reference operator()(size_type n, size_type m) {
76  assert(0 <= n and n < m_nrows);
77  assert(0 <= m and m < m_ncols);
78  return std::vector<T, A>::operator[](n*m_ncols + m);
79  }
80 
81  constexpr const_reference operator()(size_type n, size_type m) const {
82  assert(0 <= n and n < m_nrows);
83  assert(0 <= m and m < m_ncols);
84  return std::vector<T, A>::operator[](n*m_ncols + m);
85  }
86 
87  inline size_type get_nrows() const {
88  return m_nrows;
89  }
90 
91  inline size_type get_ncols() const {
92  return m_ncols;
93  }
94 
95 private:
96  size_type m_nrows;
97  size_type m_ncols;
98 };
99 
104 template <typename T, typename A>
105 constexpr bool operator==(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
106  return lhs.get_nrows() == rhs.get_nrows() and
107  lhs.get_ncols() == rhs.get_ncols() and
108  static_cast<std::vector<T, A>>(lhs) == static_cast<std::vector<T, A>>(rhs);
109 }
110 
115 template <typename T, typename A>
116 constexpr bool operator!=(const MatrixBuffer<T, A>& lhs, const MatrixBuffer<T, A>& rhs) noexcept {
117  return lhs.get_nrows() != rhs.get_nrows() or
118  lhs.get_ncols() != rhs.get_ncols() or
119  static_cast<std::vector<T, A>>(lhs) != static_cast<std::vector<T, A>>(rhs);
120 }
121 
130 template <typename T, typename A>
131 constexpr 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.get_nrows() < rhs.get_nrows()) {
136  return true;
137  } else if (lhs.get_nrows() > rhs.get_nrows()) {
138  return false;
139  }
140  }
141  return static_cast<std::vector<T, A>>(lhs) < static_cast<std::vector<T, A>>(rhs);
142 }
143 
148 template <typename T, typename A>
149 constexpr 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.get_nrows() < rhs.get_nrows()) {
154  return true;
155  } else if (lhs.get_nrows() > rhs.get_nrows()) {
156  return false;
157  }
158  }
159  return static_cast<std::vector<T, A>>(lhs) <= static_cast<std::vector<T, A>>(rhs);
160 }
161 
170 template <typename T, typename A>
171 constexpr 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.get_nrows() > rhs.get_nrows()) {
176  return true;
177  } else if (lhs.get_nrows() < rhs.get_nrows()) {
178  return false;
179  }
180  }
181  return static_cast<std::vector<T, A>>(lhs) > static_cast<std::vector<T, A>>(rhs);
182 }
183 
188 template <typename T, typename A>
189 constexpr 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.get_nrows() > rhs.get_nrows()) {
194  return true;
195  } else if (lhs.get_nrows() < rhs.get_nrows()) {
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
rtctk::componentFramework::operator>=
constexpr bool operator>=(const MatrixBuffer< T, A > &lhs, const MatrixBuffer< T, A > &rhs) noexcept
Definition: matrixBuffer.hpp:189
rtctk::componentFramework::operator>
constexpr bool operator>(const MatrixBuffer< T, A > &lhs, const MatrixBuffer< T, A > &rhs) noexcept
Definition: matrixBuffer.hpp:171
rtctk::componentFramework::operator<
constexpr bool operator<(const MatrixBuffer< T, A > &lhs, const MatrixBuffer< T, A > &rhs) noexcept
Definition: matrixBuffer.hpp:131
rtctk::componentFramework::MatrixBuffer::resize
constexpr void resize(size_type n, size_type m)
Definition: matrixBuffer.hpp:63
rtctk::componentFramework::operator<=
constexpr bool operator<=(const MatrixBuffer< T, A > &lhs, const MatrixBuffer< T, A > &rhs) noexcept
Definition: matrixBuffer.hpp:149
rtctk::componentFramework::MatrixBuffer::MatrixBuffer
constexpr MatrixBuffer() noexcept(noexcept(A()))
Definition: matrixBuffer.hpp:26
rtctk::componentFramework::MatrixBuffer::MatrixBuffer
constexpr MatrixBuffer(const MatrixBuffer &other)
Definition: matrixBuffer.hpp:30
rtctk::componentFramework
Definition: rtcComponent.hpp:17
rtctk::componentFramework::operator!=
constexpr bool operator!=(const MatrixBuffer< T, A > &lhs, const MatrixBuffer< T, A > &rhs) noexcept
Definition: matrixBuffer.hpp:116
rtctk::componentFramework::MatrixBuffer::resize
constexpr void resize(size_type n, size_type m, const value_type &value)
Definition: matrixBuffer.hpp:69
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:109
rtctk::componentFramework::MatrixBuffer::get_nrows
size_type get_nrows() const
Definition: matrixBuffer.hpp:87
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:81
rtctk::componentFramework::MatrixBuffer::get_ncols
size_type get_ncols() const
Definition: matrixBuffer.hpp:91
rtctk::componentFramework::operator==
bool operator==(const DataPointPath &lhs, const char *rhs) noexcept
Definition: dataPointPath.hpp:174
rtctk::componentFramework::MatrixBuffer::operator()
constexpr reference operator()(size_type n, size_type m)
Definition: matrixBuffer.hpp:75
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::MatrixBuffer
Definition: matrixBuffer.hpp:19