Toolbox Qt
Loading...
Searching...
No Matches
array2d.h
1#ifndef TBQ_CONTAINER_ARRAY2D_H
2#define TBQ_CONTAINER_ARRAY2D_H
3
4#include "toolboxqt/toolboxqt_global.h"
5#include <QVector>
6
7/*****************************/
8/* Start namespace */
9/*****************************/
10
11namespace tbq
12{
13
14/*****************************/
15/* Define template interface */
16/*****************************/
17
22template <typename T>
23class TOOLBOXQT_EXPORT Array2D
24{
25
26public:
27 explicit Array2D();
28 explicit Array2D(size_t nbRows, size_t nbCols);
29
30public:
31 size_t getRows() const;
32 size_t getCols() const;
33 size_t 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(m_rows, m_cols);
82}
83
87template<typename T>
89 : m_rows(0), m_cols(0)
90{
91 /* Nothing to do */
92}
93
102template<typename T>
104{
105 return m_rows;
106}
107
116template<typename T>
118{
119 return m_cols;
120}
121
131template<typename T>
133{
134 return m_rows * m_cols;
135}
136
149template<typename T>
151{
152 m_rows = 0;
153 m_cols = 0;
154 m_data.clear();
155}
156
169template<typename T>
170void Array2D<T>::resize(size_t nbRows, size_t nbCols)
171{
172 m_rows = nbRows;
173 m_cols = nbCols;
174 m_data.resize(m_rows * m_cols);
175}
176
190template<typename T>
191void Array2D<T>::insert(size_t row, size_t col, const T &value)
192{
193 (*this)(row, col) = value;
194}
195
209template<typename T>
210T& Array2D<T>::operator()(size_t row, size_t col)
211{
212 return m_data[row * m_cols + col];
213}
214
228template<typename T>
229const T& Array2D<T>::operator()(size_t row, size_t col) const
230{
231 return m_data[row * m_cols + col];
232}
233
234} // namespace tbq
235
236#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:191
Array2D()
Construct an empty 2D array.
Definition: array2d.h:88
size_t getSize() const
Get total size of 2D array (a.k.a number of elements)
Definition: array2d.h:132
void resize(size_t nbRows, size_t nbCols)
Resize array to specified size.
Definition: array2d.h:170
size_t getRows() const
Get number of rows.
Definition: array2d.h:103
T & operator()(size_t row, size_t col)
Get modifiable reference to an element.
Definition: array2d.h:210
size_t getCols() const
Get number of columns.
Definition: array2d.h:117
void clear()
Clear content of 2D array.
Definition: array2d.h:150