MDA
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
Matrix.hpp
1 /*
2  * Matrix.h
3  *
4  * Created on: Apr 12, 2012
5  * Author: Carsten Kemena
6  *
7  * This file is part of MDAT.
8  *
9  * MDAT is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * MDAT is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with MDAT. If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23 
24 
25 #ifndef MATRIX_HPP_
26 #define MATRIX_HPP_
27 
28 
29 // C header
30 #include <cstdlib>
31 #include <cstdio>
32 
33 // C++ header
34 #include <vector>
35 
36 
37 
38 namespace MDAT {
39 
43 template <typename DataType>
44 class Matrix {
45 
46 
47 private:
48  std::vector<std::vector<DataType> > _matrix;
49 public:
50  Matrix():_matrix()
51  {}
52 
58  Matrix(size_t dim1, size_t dim2);
59 
66  Matrix(size_t dim1, size_t dim2, DataType init);
67  Matrix(const Matrix&) = default;
68  Matrix & operator=(const Matrix&) = default;
72  virtual ~Matrix()
73  {
74 
75  }
76 
82  std::vector<DataType> &operator[](unsigned int index)
83  {
84  return _matrix[index];
85  }
86 
92  const std::vector<DataType> &operator[](unsigned int index) const
93  {
94  return _matrix[index];
95  }
96 
97 
103  void resize(size_t dim_1, size_t dim_2)
104  {
105  _matrix.resize(dim_1);
106  for (size_t i=0; i<dim_1; ++i)
107  _matrix[i].resize(dim_2);
108  }
109 
110  void ensure(size_t dim_1, size_t dim_2)
111  {
112  if (dim_1 > _matrix.size())
113  _matrix.resize(dim_1);
114  for (size_t i=0; i<dim_1; ++i)
115  {
116  if (_matrix[i].size() <dim_2)
117  _matrix[i].resize(dim_2);
118  }
119  }
120 
121 
126  size_t dim1() const
127  {
128  return _matrix.size();
129  }
130 
135  size_t dim2() const
136  {
137  return (_matrix.size() > 0) ? _matrix[0].size() : 0;
138  }
139 
140  void
141  fill(const DataType &value)
142  {
143  size_t dim_1 = _matrix.size();
144  size_t dim_2= (dim_1>0) ? _matrix[0].size() : 0;
145  size_t j;
146  for(size_t i=0; i<dim_1; ++i)
147  {
148  std::vector<DataType> &vec=_matrix[i];
149  for (j=0; j<dim_2; ++j)
150  vec[j] = value;
151  }
152  }
153 };
154 
155 
156 template <typename DataType>
157 Matrix<DataType>::Matrix(size_t dim_1, size_t dim_2, DataType init):_matrix(dim_1, std::vector<DataType>(dim_2, init))
158 {}
159 
160 
161 template <typename DataType>
162 Matrix<DataType>::Matrix(size_t dim_1, size_t dim_2):_matrix(dim_1, std::vector<DataType>(dim_2))
163 {}
164 
165 
166 
167 } /* namespace MDAT */
168 
169 #endif /* MATRIX_HPP_ */