LocARNA-2.0.0
matrices.hh
1 #ifndef LOCARNA_MATRICES_HH
2 #define LOCARNA_MATRICES_HH
3 
4 #ifdef HAVE_CONFIG_H
5 #include <config.h>
6 #endif
7 
8 /* @file Define various generic matrix classes (with templated element
9  type): simple matrix, matrix with range restriction, matrix with
10  offset, rotatable matrix.
11  */
12 
13 #include <iostream>
14 #include <vector>
15 #include <assert.h>
16 
17 #include <algorithm>
18 
19 #include "matrix.hh"
20 
21 namespace LocARNA {
22 
23  // ----------------------------------------
36  template <class elem_t>
37  class RMatrix : public Matrix<elem_t> {
38  typedef typename Matrix<elem_t>::size_type size_type;
39  protected:
40  size_type xl_;
41  size_type xr_;
42  size_type yl_;
43  size_type yr_;
44 
45  size_type xfactor_;
46  size_type offset_;
47 
57  size_type
58  addr(size_type i, size_type j) const {
59  assert(xl_ <= i && i <= xr_);
60  assert(yl_ <= j && j <= yr_);
61 
62  return i * xfactor_ + j - offset_;
63  }
64 
65  public:
70  RMatrix() : Matrix<elem_t>() {}
71 
81  void
82  resize(size_type xdim, size_type ydim) {
83  this->xdim_ = xdim;
84  this->ydim_ = ydim;
85  this->mat_.resize(xdim * ydim);
86 
87  restrict(0, xdim - 1, 0, ydim - 1);
88  }
89 
98  void
99  restrict(size_type xl, size_type xr, size_type yl, size_type yr) {
100  assert(xl >= 0);
101  assert(yl >= 0);
102  assert(xr < this->xdim_);
103  assert(yr < this->ydim_);
104  assert(xl <= xr);
105  assert(yl <= yr);
106 
107  this->xl_ = xl;
108  this->xr_ = xr;
109  this->yl_ = yl;
110  this->yr_ = yr;
111 
112  this->xfactor_ = (yr - yl) + 1;
113  this->offset_ = 0;
114  this->offset_ = addr(xl, yl);
115  }
116 
126  const elem_t &
127  operator()(size_type i, size_type j) const {
128  return this->mat_[addr(i, j)];
129  }
130 
140  elem_t &
141  operator()(size_type i, size_type j) {
142  return this->mat_[addr(i, j)];
143  }
144 
154  const elem_t
155  get(size_type i, size_type j) const {
156  return this->mat_[addr(i, j)];
157  }
158 
167  void
168  set(size_type i, size_type j, const elem_t &x) {
169  this->mat_[addr(i, j)] = x;
170  }
171 
178  void
179  fill(const elem_t &val) {
180  for (size_type i = xl_; i < xr_; ++i)
181  for (size_type j = yl_; j < yr_; ++j)
182  this->mat_(i, j) = val;
183  }
184  };
185 
186  // ----------------------------------------
188  template <class elem_t>
189  class OMatrix : public Matrix<elem_t> {
190  protected:
191  size_t off_;
192  size_t xoff_;
193  size_t yoff_;
194 
206  size_t
207  addr(size_t i, size_t j) const {
208  assert(xoff_ <= i && i < xoff_ + this->xdim_);
209  assert(yoff_ <= j && j < yoff_ + this->ydim_);
210  return i * this->ydim_ + j - off_;
211  }
212 
213  public:
219  OMatrix() : Matrix<elem_t>(), off_(0), xoff_(0), yoff_(0) {}
220 
229  void
230  resize(size_t xdim, size_t ydim, size_t xoff = 0, size_t yoff = 0) {
231  xoff_ = xoff;
232  yoff_ = yoff;
233  off_ = xoff * ydim + yoff;
234  this->xdim_ = xdim;
235  this->ydim_ = ydim;
236  this->mat_.resize(xdim * ydim);
237  }
238 
248  const elem_t &
249  operator()(size_t i, size_t j) const {
250  return this->mat_[addr(i, j)];
251  }
252 
262  elem_t &
263  operator()(size_t i, size_t j) {
264  return this->mat_[addr(i, j)];
265  }
266 
276  const elem_t
277  get(size_t i, size_t j) const {
278  return this->mat_[addr(i, j)];
279  }
280 
289  void
290  set(size_t i, size_t j, const elem_t &x) {
291  this->mat_[addr(i, j)] = x;
292  }
293  };
294 
295  // ----------------------------------------
298  template <class elem_t>
299  class RotMatrix : public Matrix<elem_t> {
300  protected:
301  size_t xrot_;
302  size_t yrot_;
303 
313  size_t
314  rot(size_t x, size_t r, size_t d) {
315  assert(r < d);
316  return (x + d - r) % d;
317  }
318 
330  size_t
331  addr(size_t i, size_t j) const {
332  assert(xrot_ <= i && i < xrot_ + this->xdim_);
333  assert(yrot_ <= j && j < yrot_ + this->ydim_);
334  return rot(i, xrot_, this->xdim_) * this->xdim_ +
335  rot(j, yrot_, this->ydim_);
336  }
337 
338  public:
344  RotMatrix() : Matrix<elem_t>(0), xrot_(0), yrot_(0) {}
345 
354  void
355  resize(size_t xdim, size_t ydim, size_t xrot = 0, size_t yrot = 0) {
356  xrot_ = xrot;
357  yrot_ = yrot;
358  this->xdim_ = xdim;
359  this->ydim_ = ydim;
360  this->mat_.resize(xdim * ydim);
361  }
362 
370  void
371  move(size_t xrot, size_t yrot) {
372  xrot_ = xrot;
373  yrot_ = yrot;
374  }
375 
385  const elem_t &
386  operator()(size_t i, size_t j) const {
387  return this->mat_[addr(i, j)];
388  }
389 
399  elem_t &
400  operator()(size_t i, size_t j) {
401  return this->mat_[addr(i, j)];
402  }
403 
413  const elem_t
414  get(size_t i, size_t j) const {
415  return this->mat_[addr(i, j)];
416  }
417 
427  void
428  set(size_t i, size_t j, const elem_t &x) {
429  this->mat_[addr(i, j)] = x;
430  }
431  };
432 
433 } // end namespace LocARNA
434 
435 #endif // LOCARNA_MATRICES_HH
simple 2D matrix class, provides access via operator (int,int)
Definition: matrix.hh:31
elem_t elem_t
type of elements
Definition: matrix.hh:33
size_type ydim_
second dimension
Definition: matrix.hh:43
std::vector< elem_t > mat_
vector storing the matrix entries
Definition: matrix.hh:41
size_type xdim_
first dimension
Definition: matrix.hh:42
Simple matrix class with offset.
Definition: matrices.hh:189
size_t addr(size_t i, size_t j) const
Computes address/index in 1D vector from 2D matrix indices.
Definition: matrices.hh:207
size_t yoff_
offset in second dimension
Definition: matrices.hh:193
void set(size_t i, size_t j, const elem_t &x)
Definition: matrices.hh:290
OMatrix()
Definition: matrices.hh:219
elem_t & operator()(size_t i, size_t j)
Definition: matrices.hh:263
void resize(size_t xdim, size_t ydim, size_t xoff=0, size_t yoff=0)
Definition: matrices.hh:230
const elem_t & operator()(size_t i, size_t j) const
Definition: matrices.hh:249
size_t xoff_
offset in first dimension
Definition: matrices.hh:192
const elem_t get(size_t i, size_t j) const
Definition: matrices.hh:277
size_t off_
combined offset for vector access
Definition: matrices.hh:191
Simple matrix class with restriction to a range.
Definition: matrices.hh:37
size_type offset_
offset for index calculation
Definition: matrices.hh:46
elem_t & operator()(size_type i, size_type j)
Definition: matrices.hh:141
size_type yl_
left end of restriction in second dimension
Definition: matrices.hh:42
void resize(size_type xdim, size_type ydim)
Definition: matrices.hh:82
void fill(const elem_t &val)
Fill the whole matrix with the given value.
Definition: matrices.hh:179
size_type xr_
right end of restriction in first dimension
Definition: matrices.hh:41
size_type xfactor_
factor for index calculation
Definition: matrices.hh:45
const elem_t get(size_type i, size_type j) const
Definition: matrices.hh:155
void restrict(size_type xl, size_type xr, size_type yl, size_type yr)
Set new restricted range.
Definition: matrices.hh:99
size_type xl_
left end of restriction in first dimension
Definition: matrices.hh:40
size_type addr(size_type i, size_type j) const
Definition: matrices.hh:58
RMatrix()
Definition: matrices.hh:70
void set(size_type i, size_type j, const elem_t &x)
Definition: matrices.hh:168
size_type yr_
right end of restriction in second dimension
Definition: matrices.hh:43
const elem_t & operator()(size_type i, size_type j) const
Definition: matrices.hh:127
A matrix class with rotation.
Definition: matrices.hh:299
void move(size_t xrot, size_t yrot)
Definition: matrices.hh:371
const elem_t & operator()(size_t i, size_t j) const
Definition: matrices.hh:386
RotMatrix()
Definition: matrices.hh:344
elem_t & operator()(size_t i, size_t j)
Definition: matrices.hh:400
size_t rot(size_t x, size_t r, size_t d)
Definition: matrices.hh:314
void resize(size_t xdim, size_t ydim, size_t xrot=0, size_t yrot=0)
Definition: matrices.hh:355
const elem_t get(size_t i, size_t j) const
Definition: matrices.hh:414
size_t xrot_
rotation in dimension 1
Definition: matrices.hh:301
size_t yrot_
rotation in dimension 2
Definition: matrices.hh:302
size_t addr(size_t i, size_t j) const
Definition: matrices.hh:331
void set(size_t i, size_t j, const elem_t &x)
Definition: matrices.hh:428
Definition: aligner.cc:15