LocARNA-1.9.2
|
00001 #ifndef LOCARNA_MATRIX_HH 00002 #define LOCARNA_MATRIX_HH 00003 00004 #ifdef HAVE_CONFIG_H 00005 #include <config.h> 00006 #endif 00007 00008 /* @file Define simple, generic matrix class (with templated element 00009 type) 00010 */ 00011 00012 #include <iostream> 00013 #include <vector> 00014 #include <assert.h> 00015 00016 #include <algorithm> 00017 00018 namespace LocARNA { 00019 00020 /* 00021 Define classes for the dynamic programming 00022 matrices. 00023 00024 The structures support offsets and moving 00025 of the offset, while maintaining the entries 00026 in the overlapping sub-matrix 00027 */ 00028 00030 template <class T> 00031 class Matrix { 00032 public: 00033 typedef T elem_t; 00034 typedef typename std::vector<elem_t>::size_type 00035 size_type; 00036 00037 typedef std::pair<size_type, size_type> 00038 size_pair_type; 00039 00040 protected: 00041 std::vector<elem_t> mat_; 00042 size_type xdim_; 00043 size_type ydim_; 00044 00054 size_type 00055 addr(size_type i, size_type j) const { 00056 assert(0 <= i && i < this->xdim_); 00057 assert(0 <= j && j < this->ydim_); 00058 return i * ydim_ + j; 00059 } 00060 00061 public: 00066 Matrix() : mat_(), xdim_(0), ydim_(0) {} 00067 00078 Matrix(size_type xdim, size_type ydim, const elem_t *from = 0L) 00079 : mat_(xdim * ydim), xdim_(xdim), ydim_(ydim) { 00080 if (from != 0L) { 00081 for (size_type i = 0; i < xdim_; i++) { 00082 for (size_type j = 0; j < ydim_; j++) { 00083 (*this)(i, j) = from[i * ydim + j]; 00084 } 00085 } 00086 } 00087 } 00088 00094 size_pair_type 00095 sizes() const { 00096 return size_pair_type(xdim_, ydim_); 00097 } 00098 00105 void 00106 resize(size_type xdim, size_type ydim) { 00107 xdim_ = xdim; 00108 ydim_ = ydim; 00109 00110 mat_.resize(xdim_ * ydim_); 00111 } 00112 00121 const elem_t & 00122 operator()(size_type i, size_type j) const { 00123 return mat_[addr(i, j)]; 00124 } 00125 00134 elem_t & 00135 operator()(size_type i, size_type j) { 00136 return mat_[addr(i, j)]; 00137 } 00138 00147 const elem_t 00148 get(size_type i, size_type j) const { 00149 return mat_[addr(i, j)]; 00150 } 00151 00160 void 00161 set(size_type i, size_type j, const elem_t &x) { 00162 mat_[addr(i, j)] = x; 00163 } 00164 00171 void 00172 fill(const elem_t &val) { 00173 for (size_type i = 0; i < xdim_ * ydim_; ++i) 00174 mat_[i] = val; 00175 } 00176 00182 void 00183 clear() { 00184 resize(0, 0); 00185 mat_.clear(); 00186 } 00187 00197 template <class UnaryOperator> 00198 void 00199 transform(UnaryOperator f) { 00200 std::transform(mat_.begin(), mat_.end(), mat_.begin(), f); 00201 } 00202 }; 00203 00212 template <class T> 00213 std::ostream & 00214 operator<<(std::ostream &out, Matrix<T> mat) { 00215 typename Matrix<T>::size_pair_type sizes = mat.sizes(); 00216 00217 for (typename Matrix<T>::size_type i = 0; i < sizes.first; i++) { 00218 for (typename Matrix<T>::size_type j = 0; j < sizes.second; j++) { 00219 out << mat(i, j) << " "; 00220 } 00221 out << std::endl; 00222 } 00223 return out; 00224 } 00225 00234 template <class T> 00235 std::istream & 00236 operator>>(std::istream &in, Matrix<T> &mat) { 00237 typename Matrix<T>::size_pair_type sizes = mat.sizes(); 00238 for (typename Matrix<T>::size_type i = 0; i <= mat.sizes().first; i++) { 00239 for (typename Matrix<T>::size_type j = 0; j <= mat.sizes().second; 00240 j++) { 00241 in >> mat(i, j); 00242 } 00243 } 00244 return in; 00245 } 00246 00247 } // end namespace LocARNA 00248 00249 #endif // LOCARNA_MATRIX_HH