RTC Toolkit  1.0.0
matrixSpan.hpp
Go to the documentation of this file.
1 
12 #ifndef RTCTK_COMPONENTFRAMEWORK_MATRIXSPAN_HPP
13 #define RTCTK_COMPONENTFRAMEWORK_MATRIXSPAN_HPP
14 
15 #include <array>
16 #include <cassert>
17 
18 #include <gsl/span>
19 
20 namespace rtctk::componentFramework {
21 
26 template <typename T>
27 class MatrixSpan : public gsl::span<T> {
28 public:
29  using typename gsl::span<T>::element_type;
30  using typename gsl::span<T>::value_type;
31  using typename gsl::span<T>::size_type;
32  using typename gsl::span<T>::pointer;
33  using const_pointer = const T*;
34  using typename gsl::span<T>::reference;
35  using const_reference = const T&;
36  using typename gsl::span<T>::iterator;
37  using typename gsl::span<T>::reverse_iterator;
38 
39  constexpr MatrixSpan() noexcept : gsl::span<T>(), m_nrows(0), m_ncols(0) {
40  }
41 
42  constexpr MatrixSpan(const MatrixSpan& other)
43  : gsl::span<T>(other), m_nrows(other.m_nrows), m_ncols(other.m_ncols) {
44  }
45 
46  constexpr MatrixSpan& operator=(const MatrixSpan& other) {
47  gsl::span<T>::operator=(other);
48  m_nrows = other.m_nrows;
49  m_ncols = other.m_ncols;
50  return *this;
51  }
52 
53  constexpr MatrixSpan(MatrixSpan&& other) noexcept
54  : gsl::span<T>(std::forward<MatrixSpan>(other))
55  , m_nrows(std::move(other.m_nrows))
56  , m_ncols(std::move(other.m_ncols)) {
57  }
58 
59  constexpr MatrixSpan& operator=(MatrixSpan&& other) noexcept {
60  gsl::span<T>::operator=(std::forward<MatrixSpan>(other));
61  m_nrows = std::move(other.m_nrows);
62  m_ncols = std::move(other.m_ncols);
63  return *this;
64  }
65 
66  constexpr explicit MatrixSpan(size_type n, size_type m, const gsl::span<T>& data)
67  : gsl::span<T>(data), m_nrows(n), m_ncols(m) {
68  assert(n * m == data.size());
69  }
70 
71  template <class I>
72  constexpr explicit MatrixSpan(size_type n, size_type m, I first, size_type count)
73  : gsl::span<T>(first, count), m_nrows(n), m_ncols(m) {
74  assert(n * m == count);
75  }
76 
77  template <class I>
78  constexpr explicit MatrixSpan(size_type n, size_type m, I first, I last)
79  : gsl::span<T>(first, last), m_nrows(n), m_ncols(m) {
80  assert(n * m == std::distance(first, last));
81  }
82 
83  template <std::size_t N>
84  constexpr explicit MatrixSpan(size_type n, size_type m, element_type (&array)[N]) noexcept
85  : gsl::span<T>(array), m_nrows(n), m_ncols(m) {
86  assert(n * m == N);
87  }
88 
89  template <std::size_t N>
90  constexpr explicit MatrixSpan(size_type n, size_type m, std::array<T, N>& array) noexcept
91  : gsl::span<T>(array), m_nrows(n), m_ncols(m) {
92  assert(n * m == N);
93  }
94 
95  template <std::size_t N>
96  constexpr explicit MatrixSpan(size_type n, size_type m, const std::array<T, N>& array) noexcept
97  : gsl::span<T>(array), m_nrows(n), m_ncols(m) {
98  assert(n * m == N);
99  }
100 
101  template <typename R>
102  constexpr explicit MatrixSpan(size_type n, size_type m, R&& range) noexcept
103  : gsl::span<T>(std::forward<R>(range)), m_nrows(n), m_ncols(m) {
104  assert(n * m == range.size());
105  }
106 
107  template <typename A>
108  constexpr explicit MatrixSpan(MatrixBuffer<T, A>& buffer) noexcept
109  : gsl::span<T>(buffer.data(), buffer.size())
110  , m_nrows(buffer.GetNrows())
111  , m_ncols(buffer.GetNcols()) {
112  }
113 
114  constexpr reference operator()(size_type n, size_type m) {
115  assert(0 <= n and n < m_nrows);
116  assert(0 <= m and m < m_ncols);
117  return gsl::span<T>::operator[](n* m_ncols + m);
118  }
119 
120  constexpr const_reference operator()(size_type n, size_type m) const {
121  assert(0 <= n and n < m_nrows);
122  assert(0 <= m and m < m_ncols);
123  return gsl::span<T>::operator[](n* m_ncols + m);
124  }
125 
126  inline size_type GetNrows() const {
127  return m_nrows;
128  }
129 
130  inline size_type GetNcols() const {
131  return m_ncols;
132  }
133 
134 private:
135  size_type m_nrows;
136  size_type m_ncols;
137 };
138 
139 } // namespace rtctk::componentFramework
140 
141 #endif // RTCTK_COMPONENTFRAMEWORK_MATRIXSPAN_HPP
rtctk::componentFramework::MatrixSpan::MatrixSpan
constexpr MatrixSpan(MatrixBuffer< T, A > &buffer) noexcept
Definition: matrixSpan.hpp:108
rtctk::componentFramework::MatrixSpan::const_pointer
const T * const_pointer
Definition: matrixSpan.hpp:33
rtctk::componentFramework::MatrixSpan::operator=
constexpr MatrixSpan & operator=(MatrixSpan &&other) noexcept
Definition: matrixSpan.hpp:59
rtctk::componentFramework::MatrixSpan::operator()
constexpr reference operator()(size_type n, size_type m)
Definition: matrixSpan.hpp:114
rtctk::componentFramework::MatrixSpan::MatrixSpan
constexpr MatrixSpan() noexcept
Definition: matrixSpan.hpp:39
rtctk::componentFramework
Definition: commandReplier.cpp:20
rtctk::componentFramework::MatrixSpan::GetNcols
size_type GetNcols() const
Definition: matrixSpan.hpp:130
rtctk::componentFramework::MatrixSpan::MatrixSpan
constexpr MatrixSpan(MatrixSpan &&other) noexcept
Definition: matrixSpan.hpp:53
rtctk::componentFramework::MatrixSpan::MatrixSpan
constexpr MatrixSpan(size_type n, size_type m, element_type(&array)[N]) noexcept
Definition: matrixSpan.hpp:84
rtctk::componentFramework::MatrixSpan::const_reference
const T & const_reference
Definition: matrixSpan.hpp:35
rtctk::componentFramework::MatrixSpan::operator=
constexpr MatrixSpan & operator=(const MatrixSpan &other)
Definition: matrixSpan.hpp:46
rtctk::componentFramework::MatrixSpan::MatrixSpan
constexpr MatrixSpan(size_type n, size_type m, I first, size_type count)
Definition: matrixSpan.hpp:72
rtctk::componentFramework::MatrixSpan::MatrixSpan
constexpr MatrixSpan(const MatrixSpan &other)
Definition: matrixSpan.hpp:42
rtctk::componentFramework::MatrixSpan::GetNrows
size_type GetNrows() const
Definition: matrixSpan.hpp:126
rtctk::componentFramework::MatrixSpan::operator()
constexpr const_reference operator()(size_type n, size_type m) const
Definition: matrixSpan.hpp:120
rtctk::componentFramework::MatrixSpan::MatrixSpan
constexpr MatrixSpan(size_type n, size_type m, R &&range) noexcept
Definition: matrixSpan.hpp:102
rtctk::componentFramework::MatrixSpan::MatrixSpan
constexpr MatrixSpan(size_type n, size_type m, std::array< T, N > &array) noexcept
Definition: matrixSpan.hpp:90
rtctk::componentFramework::MatrixSpan::MatrixSpan
constexpr MatrixSpan(size_type n, size_type m, const gsl::span< T > &data)
Definition: matrixSpan.hpp:66
rtctk::componentFramework::MatrixSpan::MatrixSpan
constexpr MatrixSpan(size_type n, size_type m, I first, I last)
Definition: matrixSpan.hpp:78
rtctk::componentFramework::MatrixSpan::MatrixSpan
constexpr MatrixSpan(size_type n, size_type m, const std::array< T, N > &array) noexcept
Definition: matrixSpan.hpp:96
rtctk::componentFramework::MatrixBuffer
Definition: matrixBuffer.hpp:22
rtctk::componentFramework::MatrixSpan
A span class that simply extends gsl::span to record the number of rows and columns of a matrix and c...
Definition: matrixSpan.hpp:27