glbarcode++
 
Loading...
Searching...
No Matches
Matrix.hpp
Go to the documentation of this file.
1// Matrix.hpp
2//
3// Copyright (C) 2013-2026 Jaye Evins <evins@snaught.com>
4//
5// This file is part of glbarcode++.
6//
7// glbarcode++ is free software: you can redistribute it and/or modify
8// it under the terms of the GNU Lesser General Public License as published by
9// the Free Software Foundation, either version 3 of the License, or
10// (at your option) any later version.
11//
12// glbarcode++ is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU Lesser General Public License for more details.
16//
17// You should have received a copy of the GNU Lesser General Public License
18// along with glbarcode++. If not, see <http://www.gnu.org/licenses/>.
19//
20
21#ifndef glbarcode_Matrix_hpp
22#define glbarcode_Matrix_hpp
23
24
25namespace glbarcode
26{
27
33 template <class T> class Matrix
34 {
35
36 public:
40 Matrix() : mNx(0), mNy(0), mData(nullptr) { }
41
42
46 Matrix( int nx, int ny ) : mNx(nx),
47 mNy(ny),
48 mData((nx > 0 && ny > 0) ? new T[nx * ny] : nullptr) { }
49
50
54 Matrix( const Matrix<T>& src ) : mNx(src.mNx),
55 mNy(src.mNy),
56 mData((src.mNx > 0 && src.mNy > 0) ? new T[src.mNx * src.mNy] : nullptr)
57 {
58 for ( int iy = 0; iy < mNy; iy++ )
59 {
60 for ( int ix = 0; ix < mNx; ix++ )
61 {
62 (*this)[iy][ix] = src[iy][ix];
63 }
64 }
65 }
66
67
71 Matrix( const Matrix<T>& src,
72 int x0,
73 int y0,
74 int nx,
75 int ny ) : mNx(nx),
76 mNy(ny),
77 mData((nx > 0 && ny > 0) ? new T[nx * ny] : nullptr)
78 {
79 for ( int iy = 0; iy < mNy; iy++ )
80 {
81 if ( (y0+iy) < src.ny() )
82 {
83 for ( int ix = 0; ix < mNx; ix++ )
84 {
85 if ( (x0+ix) < src.nx() )
86 {
87 (*this)[iy][ix] = src[y0+iy][x0+ix];
88 }
89 }
90 }
91 }
92 }
93
94
99 {
100 if ( mData != nullptr )
101 {
102 delete[] mData;
103 }
104 }
105
106
110 inline Matrix & operator=( const Matrix & src )
111 {
112 return Matrix( src );
113 }
114
115
119 inline T* operator[]( int i ) const
120 {
121 return (mData + (mNx * i));
122 }
123
124
128 inline void resize( int nx, int ny )
129 {
130 if ( mData != nullptr )
131 {
132 delete[] mData;
133 }
134 mNx = nx;
135 mNy = ny;
136 mData = (nx > 0 && ny > 0) ? new T[nx * ny] : nullptr;
137 }
138
139
145 inline int nx() const
146 {
147 return mNx;
148 }
149
150
156 inline int ny() const
157 {
158 return mNy;
159 }
160
161
165 inline Matrix<T> subMatrix( int x0, int y0, int nx, int ny )
166 {
167 return Matrix<T>( *this, x0, y0, nx, ny );
168 }
169
170
174 inline void setSubMatrix( int x0, int y0, Matrix<T> & a )
175 {
176 for ( int iy = 0; iy < a.ny(); iy++ )
177 {
178 if ( (y0 + iy) < mNy )
179 {
180 for ( int ix = 0; ix < a.nx(); ix++ )
181 {
182 if ( (x0 + ix) < mNx )
183 {
184 (*this)[y0+iy][x0+ix] = a[iy][ix];
185 }
186 }
187 }
188 }
189 }
190
191
195 inline void fill( T val )
196 {
197 for ( int iy = 0; iy < mNy; iy++ )
198 {
199 for ( int ix = 0; ix < mNx; ix++ )
200 {
201 (*this)[iy][ix] = val;
202 }
203 }
204 }
205
206
207 private:
211 int mNx;
212 int mNy;
213 T* mData;
214
215 };
216
217}
218
219
220#endif // glbarcode_Matrix_hpp
Definition Matrix.hpp:34
Matrix< T > subMatrix(int x0, int y0, int nx, int ny)
Definition Matrix.hpp:165
Matrix(const Matrix< T > &src)
Definition Matrix.hpp:54
int nx() const
Definition Matrix.hpp:145
Matrix()
Definition Matrix.hpp:40
Matrix & operator=(const Matrix &src)
Definition Matrix.hpp:110
T * operator[](int i) const
Definition Matrix.hpp:119
void setSubMatrix(int x0, int y0, Matrix< T > &a)
Definition Matrix.hpp:174
void fill(T val)
Definition Matrix.hpp:195
~Matrix()
Definition Matrix.hpp:98
Matrix(int nx, int ny)
Definition Matrix.hpp:46
void resize(int nx, int ny)
Definition Matrix.hpp:128
int ny() const
Definition Matrix.hpp:156
Matrix(const Matrix< T > &src, int x0, int y0, int nx, int ny)
Definition Matrix.hpp:71
Definition Barcode.hpp:38