-
Notifications
You must be signed in to change notification settings - Fork 2
/
matrix.hpp
62 lines (48 loc) · 1.48 KB
/
matrix.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// A 2D matrix class with the functionality I need for PSSMs
/* Written by Martin C Frith */
/* I intend that anyone who finds this code useful be free to use,
modify, or redistribute it without any restrictions. Naturally, I
hope it will not be used for evil purposes. */
#ifndef MCF_MATRIX_H
#define MCF_MATRIX_H
#include "MCFgen.hpp" // is_reverse
#include <algorithm>
#include <vector>
namespace mcf {
template <class T> class matrix {
private:
unsigned nrows;
unsigned ncols;
std::vector<T> data;
public:
matrix() : nrows(0), ncols(0) {}
matrix(unsigned r, unsigned c) : nrows(r), ncols(c), data(r * c) {}
unsigned rows() const { return nrows; }
unsigned cols() const { return ncols; }
// assumes i+ncols is a valid iterator:
template <class It> void push_row(It i) {
data.insert(data.end(), i, i + ncols);
++nrows;
}
// Not sure why "typename" is needed:
typename std::vector<T>::iterator operator[](unsigned r) {
return data.begin() + r * ncols;
}
// T * operator[] (unsigned r)
// { return &data[0] + r * ncols; }
typename std::vector<T>::const_iterator operator[](unsigned r) const {
return data.begin() + r * ncols;
}
// const T * operator[] (unsigned r) const
// { return &data[0] + r * ncols; }
void rotate180() // same as reverse complement
{
std::reverse(data.begin(), data.end());
}
bool is_rotate180() const // is it palindromic?
{
return mcf::is_reverse(data.begin(), data.end());
}
};
}
#endif