Toolbox Qt
Loading...
Searching...
No Matches
array2d.h
1#ifndef TBQ_CONTAINER_ARRAY2D_H
2#define TBQ_CONTAINER_ARRAY2D_H
3
4#include <QSize>
5#include <QVector>
6
7/*****************************/
8/* Namespace instructions */
9/*****************************/
10
11namespace tbq
12{
13
14/*****************************/
15/* Define template interface */
16/*****************************/
17
22template <typename T>
24{
25public:
26 Array2D();
27 explicit Array2D(size_t nbRows, size_t nbCols);
28
29public:
30 size_t getRows() const;
31 size_t getCols() const;
32 size_t getNbElements() const;
33 QSize getSize() const;
34
35public:
36 void clear();
37 void resize(size_t nbRows, size_t nbCols);
38
39 void insert(size_t row, size_t col, const T &value);
40
41public:
42 T& operator()(size_t row, size_t col);
43 const T& operator()(size_t row, size_t col) const;
44
45public: // Friends operator defined using "Making new friends" idiom (https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Making_New_Friends)
46 friend bool operator==(const Array2D<T> &left, const Array2D<T> &right)
47 {
48 return left.m_rows == right.m_rows
49 && left.m_cols == right.m_cols
50 && left.m_data == right.m_data;
51 }
52
53 friend bool operator!=(const Array2D<T> &left, const Array2D<T> &right)
54 {
55 return !(left == right);
56 }
57
58private:
59 size_t m_rows;
60 size_t m_cols;
61 QVector<T> m_data;
62};
63
64/*****************************/
65/* Define template
66 * implementation */
67/*****************************/
68
78template<typename T>
79Array2D<T>::Array2D(size_t nbRows, size_t nbCols)
80{
81 resize(nbRows, nbCols);
82}
83
87template<typename T>
89 : m_rows(0), m_cols(0)
90{
91 /* Nothing to do */
92}
93
103template<typename T>
105{
106 return m_rows;
107}
108
118template<typename T>
120{
121 return m_cols;
122}
123
133template<typename T>
135{
136 return m_rows * m_cols;
137}
138
148template<typename T>
150{
151 return QSize(m_cols, m_rows);
152}
153
166template<typename T>
168{
169 m_rows = 0;
170 m_cols = 0;
171 m_data.clear();
172}
173
186template<typename T>
187void Array2D<T>::resize(size_t nbRows, size_t nbCols)
188{
189 m_rows = nbRows;
190 m_cols = nbCols;
191 m_data.resize(m_rows * m_cols);
192}
193
207template<typename T>
208void Array2D<T>::insert(size_t row, size_t col, const T &value)
209{
210 (*this)(row, col) = value;
211}
212
226template<typename T>
227T& Array2D<T>::operator()(size_t row, size_t col)
228{
229 return m_data[row * m_cols + col];
230}
231
245template<typename T>
246const T& Array2D<T>::operator()(size_t row, size_t col) const
247{
248 return m_data[row * m_cols + col];
249}
250
251/*****************************/
252/* Qt specific methods */
253/*****************************/
254
255/*****************************/
256/* End namespaces */
257/*****************************/
258
259} // namespace tbq
260
261/*****************************/
262/* Qt specific meta-system */
263/*****************************/
264
265#endif // TBQ_CONTAINER_ARRAY2D_H
Use to manage a 2-dimensional array.
Definition array2d.h:24
void insert(size_t row, size_t col, const T &value)
Use to insert a value at specified indexes.
Definition array2d.h:208
Array2D()
Construct an empty 2D array.
Definition array2d.h:88
QSize getSize() const
Get matrix size.
Definition array2d.h:149
void resize(size_t nbRows, size_t nbCols)
Resize array to specified size.
Definition array2d.h:187
size_t getRows() const
Get number of rows.
Definition array2d.h:104
T & operator()(size_t row, size_t col)
Get modifiable reference to an element.
Definition array2d.h:227
size_t getNbElements() const
Get number of elements of 2D array.
Definition array2d.h:134
size_t getCols() const
Get number of columns.
Definition array2d.h:119
void clear()
Clear content of 2D array.
Definition array2d.h:167
Namespace used for ToolBoxQt library.
Definition array2d.h:12