Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
matrix.h
Go to the documentation of this file.
1#ifndef LIBS_CORE_MATRIX_H_
2#define LIBS_CORE_MATRIX_H_
3
4#include <algorithm>
5#include <ostream>
6#include <vector>
7
9{
10
11template<typename T>
12class Matrix
13{
14public:
15 using matrix_t = std::vector<T>;
16 using iterator = typename matrix_t::iterator;
17 using const_iterator = typename matrix_t::const_iterator;
18
19 Matrix() = delete;
20
21 Matrix(const size_t rows, const size_t cols) : m_rows(rows), m_cols(cols), m_data(matrix_t(rows * cols))
22 {
23 }
24
25 Matrix(const Matrix &other) : m_rows(other.m_rows), m_cols(other.m_cols), m_data(other.m_data)
26 {
27 }
28
29 Matrix &operator=(const Matrix &other)
30 {
31 m_data = other.m_data;
32 m_rows = other.m_rows;
33 m_cols = other.m_cols;
34 return *this;
35 }
36
37 Matrix(Matrix &&other) noexcept : m_rows(other.m_rows), m_cols(other.m_cols), m_data(std::move(other.m_data))
38 {
39 }
40
41 Matrix &operator=(Matrix &&other) noexcept
42 {
43 m_data.swap(std::move(other.m_data));
44 m_rows = other.m_rows;
45 m_cols = other.m_cols;
46 return *this;
47 }
48
50 {
51 m_data.clear();
52 }
53
54 T &operator()(const size_t row, const size_t column)
55 {
56 return m_data[row * m_cols + column];
57 }
58
59 T operator()(const size_t row, const size_t column) const
60 {
61 return m_data[row * m_cols + column];
62 }
63
64 template<typename U>
65 friend std::ostream &operator<<(std::ostream &os, const Matrix<U> &rhs);
66
68 {
69 return m_data.begin();
70 }
71
73 {
74 return m_data.end();
75 }
76
78 {
79 return m_data.begin();
80 }
81
83 {
84 return m_data.end();
85 }
86
88 {
89 return m_data.cbegin();
90 }
91
93 {
94 return m_data.cend();
95 }
96
97 T &at(const size_t row, const size_t column)
98 {
99 return m_data.at(row * m_cols + column);
100 }
101
102 const T &at(const size_t row, const size_t column) const
103 {
104 return m_data.at(row * m_cols + column);
105 }
106
107 void set(const size_t row, const size_t column, const T &item)
108 {
109 // if (m_nmea2kAttribute.size() <= (row * m_cols + column))
110 // m_nmea2kAttribute.resize(row * m_cols + column + 1);
111
112 m_data.at(row * m_cols + column) = item;
113 }
114
115 void size(uint64_t *rows, uint64_t *cols) const
116 {
117 if (rows != nullptr)
118 {
119 *rows = m_rows;
120 }
121 if (cols != nullptr)
122 {
123 *cols = m_cols;
124 }
125 }
126
127 template<typename TypeSearch>
128 bool exist(const TypeSearch itemSearch, bool (*predicate)(const TypeSearch &, const T &), size_t *ind) const
129 {
131 sGenericComparator<TypeSearch> search(itemSearch, predicate);
132
133 it = std::find_if(m_data.begin(), m_data.end(), search);
134
135 if (ind != nullptr)
136 {
137 *ind = std::distance(m_data.begin(), it);
138 }
139
140 return it != m_data.end();
141 }
142
143private:
144 template<typename TypeSearch>
146 {
147 sGenericComparator(const TypeSearch data, bool (*function)(const TypeSearch &, const T &))
148 : m_data(data), fptr(function)
149 {
150 }
151
152 bool operator()(const T &arg) const
153 {
154 return fptr(m_data, arg);
155 }
156
157 const TypeSearch m_data;
158 bool (*fptr)(const TypeSearch &, const T &);
159 };
160
161 size_t m_rows;
162 size_t m_cols;
164};
165
166template<typename U>
167std::ostream &operator<<(std::ostream &os, const Matrix<U> &rhs)
168{
169 for (size_t i = 0; i < rhs.m_rows; i++)
170 {
171 for (size_t j = 0; j < rhs.m_cols; j++)
172 {
173 os << rhs.at(i, j) << " ";
174 }
175 os << "\n";
176 }
177
178 return os;
179}
180
181}// namespace sedni::containers
182
183#endif /* LIBS_CORE_MATRIX_H_ */
Definition matrix.h:13
Matrix & operator=(const Matrix &other)
Definition matrix.h:29
Matrix(const Matrix &other)
Definition matrix.h:25
size_t m_rows
Definition matrix.h:161
T & at(const size_t row, const size_t column)
Definition matrix.h:97
const_iterator begin() const
Definition matrix.h:77
bool exist(const TypeSearch itemSearch, bool(*predicate)(const TypeSearch &, const T &), size_t *ind) const
Definition matrix.h:128
Matrix(const size_t rows, const size_t cols)
Definition matrix.h:21
const_iterator end() const
Definition matrix.h:82
~Matrix()
Definition matrix.h:49
iterator end()
Definition matrix.h:72
void set(const size_t row, const size_t column, const T &item)
Definition matrix.h:107
std::vector< T > matrix_t
Definition matrix.h:15
size_t m_cols
Definition matrix.h:162
friend std::ostream & operator<<(std::ostream &os, const Matrix< U > &rhs)
Definition matrix.h:167
Matrix(Matrix &&other) noexcept
Definition matrix.h:37
const T & at(const size_t row, const size_t column) const
Definition matrix.h:102
const_iterator cend() const
Definition matrix.h:92
iterator begin()
Definition matrix.h:67
Matrix & operator=(Matrix &&other) noexcept
Definition matrix.h:41
const_iterator cbegin() const
Definition matrix.h:87
void size(uint64_t *rows, uint64_t *cols) const
Definition matrix.h:115
typename matrix_t::const_iterator const_iterator
Definition matrix.h:17
T & operator()(const size_t row, const size_t column)
Definition matrix.h:54
T operator()(const size_t row, const size_t column) const
Definition matrix.h:59
typename matrix_t::iterator iterator
Definition matrix.h:16
matrix_t m_data
Definition matrix.h:163
Definition matrix.h:9
std::ostream & operator<<(std::ostream &os, const Matrix< U > &rhs)
Definition matrix.h:167
bool operator()(const T &arg) const
Definition matrix.h:152
sGenericComparator(const TypeSearch data, bool(*function)(const TypeSearch &, const T &))
Definition matrix.h:147
const TypeSearch m_data
Definition matrix.h:157
bool(* fptr)(const TypeSearch &, const T &)
Definition matrix.h:158