LocARNA-2.0.0
matrix.hh
1 #ifndef LOCARNA_MATRIX_HH
2 #define LOCARNA_MATRIX_HH
3 
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7 
8 /* @file Define simple, generic matrix class (with templated element
9  type)
10  */
11 
12 #include <iostream>
13 #include <vector>
14 #include <assert.h>
15 
16 #include <algorithm>
17 
18 namespace LocARNA {
19 
20  /*
21  Define classes for the dynamic programming
22  matrices.
23 
24  The structures support offsets and moving
25  of the offset, while maintaining the entries
26  in the overlapping sub-matrix
27  */
28 
30  template <class T>
31  class Matrix {
32  public:
33  typedef T elem_t;
34  typedef typename std::vector<elem_t>::size_type
36 
37  typedef std::pair<size_type, size_type>
39 
40  protected:
41  std::vector<elem_t> mat_;
44 
54  size_type
55  addr(size_type i, size_type j) const {
56  //assert(i < this->xdim_);
57  //assert(j < this->ydim_);
58  return i * ydim_ + j;
59  }
60 
61  public:
66  Matrix() : mat_(), xdim_(0), ydim_(0) {}
67 
78  Matrix(size_type xdim, size_type ydim, const elem_t *from = nullptr)
79  : mat_(xdim * ydim), xdim_(xdim), ydim_(ydim) {
80  if (from == nullptr) return;
81 
82  std::copy(from, from + xdim_ * ydim_, mat_.begin());
83  }
84 
91  sizes() const {
92  return size_pair_type(xdim_, ydim_);
93  }
94 
101  void
102  resize(size_type xdim, size_type ydim) {
103  xdim_ = xdim;
104  ydim_ = ydim;
105 
106  mat_.resize(xdim_ * ydim_);
107  }
108 
117  const elem_t &
119  return mat_[addr(i, j)];
120  }
121 
130  elem_t &
132  return mat_[addr(i, j)];
133  }
134 
143  const elem_t &
144  get(size_type i, size_type j) const {
145  return mat_[addr(i, j)];
146  }
147 
156  void
157  set(size_type i, size_type j, const elem_t &x) {
158  mat_[addr(i, j)] = x;
159  }
160 
167  void
168  fill(const elem_t &val) {
169  for (size_type i = 0; i < xdim_ * ydim_; ++i)
170  mat_[i] = val;
171  }
172 
178  void
179  clear() {
180  resize(0, 0);
181  mat_.clear();
182  }
183 
193  template <class UnaryOperator>
194  void
195  transform(UnaryOperator f) {
196  std::transform(mat_.begin(), mat_.end(), mat_.begin(), f);
197  }
198  };
199 
208  template <class T>
209  std::ostream &
210  operator<<(std::ostream &out, Matrix<T> mat) {
211  typename Matrix<T>::size_pair_type sizes = mat.sizes();
212 
213  for (typename Matrix<T>::size_type i = 0; i < sizes.first; i++) {
214  for (typename Matrix<T>::size_type j = 0; j < sizes.second; j++) {
215  out << mat(i, j) << " ";
216  }
217  out << std::endl;
218  }
219  return out;
220  }
221 
230  template <class T>
231  std::istream &
232  operator>>(std::istream &in, Matrix<T> &mat) {
233  typename Matrix<T>::size_pair_type sizes = mat.sizes();
234  for (typename Matrix<T>::size_type i = 0; i <= mat.sizes().first; i++) {
235  for (typename Matrix<T>::size_type j = 0; j <= mat.sizes().second;
236  j++) {
237  in >> mat(i, j);
238  }
239  }
240  return in;
241  }
242 
243 } // end namespace LocARNA
244 
245 #endif // LOCARNA_MATRIX_HH
simple 2D matrix class, provides access via operator (int,int)
Definition: matrix.hh:31
void fill(const elem_t &val)
Fill the whole matrix with the given value.
Definition: matrix.hh:168
void clear()
Definition: matrix.hh:179
void resize(size_type xdim, size_type ydim)
Definition: matrix.hh:102
void set(size_type i, size_type j, const elem_t &x)
Definition: matrix.hh:157
std::vector< elem_t >::size_type size_type
size type (from underlying vector)
Definition: matrix.hh:35
T elem_t
type of elements
Definition: matrix.hh:33
void transform(UnaryOperator f)
Definition: matrix.hh:195
Matrix(size_type xdim, size_type ydim, const elem_t *from=nullptr)
Definition: matrix.hh:78
const elem_t & operator()(size_type i, size_type j) const
Definition: matrix.hh:118
std::pair< size_type, size_type > size_pair_type
type for pair of sizes
Definition: matrix.hh:38
Matrix()
Definition: matrix.hh:66
size_type ydim_
second dimension
Definition: matrix.hh:43
elem_t & operator()(size_type i, size_type j)
Definition: matrix.hh:131
size_pair_type sizes() const
Definition: matrix.hh:91
const elem_t & get(size_type i, size_type j) const
Definition: matrix.hh:144
std::vector< elem_t > mat_
vector storing the matrix entries
Definition: matrix.hh:41
size_type addr(size_type i, size_type j) const
Definition: matrix.hh:55
size_type xdim_
first dimension
Definition: matrix.hh:42
Definition: aligner.cc:15
std::ostream & operator<<(std::ostream &out, const AlignerRestriction &r)
Definition: aligner_restriction.hh:135
std::istream & operator>>(std::istream &in, Matrix< T > &mat)
Definition: matrix.hh:232