LocARNA-1.8.11
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 size_type;
35 
36  typedef std::pair<size_type,size_type> size_pair_type;
37 
38  protected:
39  std::vector<elem_t> mat_;
40  size_type xdim_;
41  size_type ydim_;
42 
52  size_type addr(size_type i, size_type j) const {
53  assert(0<=i && i<this->xdim_);
54  assert(0<=j && j<this->ydim_);
55  return i*ydim_+j;
56  }
57 
58  public:
63  Matrix()
64  : mat_(),xdim_(0),ydim_(0) {
65  }
66 
77  Matrix(size_type xdim, size_type ydim, const elem_t *from=0L)
78  : mat_(xdim*ydim),xdim_(xdim),ydim_(ydim) {
79  if (from!=0L) {
80  for (size_type i=0; i<xdim_; i++) {
81  for (size_type j=0; j<ydim_; j++) {
82  (*this)(i,j)=from[i*ydim+j];
83  }
84  }
85  }
86  }
87 
93  size_pair_type sizes() const {
94  return size_pair_type(xdim_,ydim_);
95  }
96 
103  void
104  resize(size_type xdim, size_type ydim) {
105  xdim_=xdim;
106  ydim_=ydim;
107 
108  mat_.resize(xdim_*ydim_);
109  }
110 
119  const elem_t & operator() (size_type i,size_type j) const {
120  return mat_[addr(i,j)];
121  }
122 
131  elem_t & operator() (size_type i,size_type j) {
132  return mat_[addr(i,j)];
133  }
134 
143  const elem_t get(size_type i,size_type j) const {
144  return mat_[addr(i,j)];
145  }
146 
155  void
156  set(size_type i,size_type j, const elem_t &x) {
157  mat_[addr(i,j)]=x;
158  }
159 
166  void
167  fill(const elem_t &val) {
168  for (size_type i=0; i<xdim_*ydim_; ++i)
169  mat_[i]=val;
170  }
171 
177  void
178  clear() {
179  resize(0,0);
180  mat_.clear();
181  }
182 
191  template<class UnaryOperator>
192  void transform(UnaryOperator f) {
193  std::transform(mat_.begin(),mat_.end(),mat_.begin(),f);
194  }
195  };
196 
205  template <class T>
206  std::ostream & operator << (std::ostream &out, Matrix<T> mat) {
207  typename Matrix<T>::size_pair_type sizes = mat.sizes();
208 
209  for (typename Matrix<T>::size_type i=0; i<sizes.first; i++) {
210  for (typename Matrix<T>::size_type j=0; j<sizes.second; j++) {
211  out << mat(i,j) << " ";
212  }
213  out << std::endl;
214  }
215  return out;
216  }
217 
226  template <class T>
227  std::istream & operator >> (std::istream &in, Matrix<T> &mat) {
228  typename Matrix<T>::size_pair_type sizes = mat.sizes();
229  for (typename Matrix<T>::size_type i=0; i<=mat.sizes().first; i++) {
230  for (typename Matrix<T>::size_type j=0; j<=mat.sizes().second; j++) {
231  in >> mat(i,j);
232  }
233  }
234  return in;
235  }
236 
237 
238 
239 } // end namespace LocARNA
240 
241 #endif // LOCARNA_MATRIX_HH
std::vector< elem_t > mat_
vector storing the matrix entries
Definition: matrix.hh:39
void transform(UnaryOperator f)
Definition: matrix.hh:192
size_type xdim_
first dimension
Definition: matrix.hh:40
Matrix()
Definition: matrix.hh:63
Matrix(size_type xdim, size_type ydim, const elem_t *from=0L)
Definition: matrix.hh:77
void resize(size_type xdim, size_type ydim)
Definition: matrix.hh:104
Definition: aligner.cc:17
size_type ydim_
second dimension
Definition: matrix.hh:41
std::pair< size_type, size_type > size_pair_type
type for pair of sizes
Definition: matrix.hh:36
T elem_t
type of elements
Definition: matrix.hh:33
void fill(const elem_t &val)
Fill the whole matrix with the given value.
Definition: matrix.hh:167
void clear()
Definition: matrix.hh:178
simple 2D matrix class, provides access via operator (int,int)
Definition: aligner_impl.hh:19
std::vector< elem_t >::size_type size_type
size type (from underlying vector)
Definition: matrix.hh:34
std::istream & operator>>(std::istream &in, Matrix< T > &mat)
Definition: matrix.hh:227
const elem_t & operator()(size_type i, size_type j) const
Definition: matrix.hh:119
size_pair_type sizes() const
Definition: matrix.hh:93
size_type addr(size_type i, size_type j) const
Definition: matrix.hh:52