00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef OPENMS_DATASTRUCTURES_MATRIX_H
00028 #define OPENMS_DATASTRUCTURES_MATRIX_H
00029
00030 #include <OpenMS/CONCEPT/Macros.h>
00031 #include <OpenMS/DATASTRUCTURES/String.h>
00032
00033 #include <cmath>
00034 #include <iomanip>
00035 #include <vector>
00036
00037 namespace OpenMS
00038 {
00039
00068 template <typename Value>
00069 class Matrix : protected std::vector < Value >
00070 {
00071 protected:
00072 typedef std::vector < Value > Base;
00073
00074 public:
00075
00077
00078 typedef Base container_type;
00079
00080 typedef typename Base::difference_type difference_type;
00081 typedef typename Base::size_type size_type;
00082
00083 typedef typename Base::const_iterator const_iterator;
00084 typedef typename Base::const_reverse_iterator const_reverse_iterator;
00085 typedef typename Base::iterator iterator;
00086 typedef typename Base::reverse_iterator reverse_iterator;
00087
00088 typedef typename Base::const_reference const_reference;
00089 typedef typename Base::pointer pointer;
00090 typedef typename Base::reference reference;
00091 typedef typename Base::value_type value_type;
00092
00093 typedef typename Base::allocator_type allocator_type;
00095
00097
00098 typedef Base ContainerType;
00099 typedef difference_type DifferenceType;
00100 typedef size_type SizeType;
00101
00102 typedef const_iterator ConstIterator;
00103 typedef const_reverse_iterator ConstReverseIterator;
00104 typedef iterator Iterator;
00105 typedef reverse_iterator ReverseIterator;
00106
00107 typedef const_reference ConstReference;
00108 typedef pointer Pointer;
00109 typedef reference Reference;
00110 typedef value_type ValueType;
00111
00112 typedef allocator_type AllocatorType;
00114
00116
00117 Matrix ()
00118 : Base(),
00119 rows_(0),
00120 cols_(0)
00121 {}
00122
00123 Matrix (SizeType rows, SizeType cols, ValueType value = ValueType())
00124 : Base(rows*cols,value),
00125 rows_(rows),
00126 cols_(cols)
00127 {}
00128
00129 Matrix (const Matrix & source)
00130 : Base(source),
00131 rows_(source.rows_),
00132 cols_(source.cols_)
00133 {}
00134
00135 Matrix & operator = (const Matrix & rhs)
00136 {
00137 Base::operator=(rhs);
00138 rows_ = rhs.rows_;
00139 cols_ = rhs.cols_;
00140 return *this;
00141 }
00142
00143 ~Matrix() {}
00145
00147
00148 const_reference operator() (size_type const i, size_type const j) const
00149 {
00150 return getValue(i,j);
00151 }
00152
00153 reference operator() (size_type const i, size_type const j)
00154 {
00155 return getValue(i,j);
00156 }
00157
00158 const_reference getValue(size_type const i, size_type const j) const
00159 {
00160 return Base::operator[](index(i,j));
00161 }
00162
00163 reference getValue(size_type const i, size_type const j)
00164 {
00165 return Base::operator[](index(i,j));
00166 }
00167
00168 void setValue(size_type const i, size_type const j, value_type value)
00169 {
00170 Base::operator[](index(i,j)) = value;
00171 }
00172
00174
00175
00182 public:
00183
00184 Base::begin;
00185 Base::end;
00186 Base::rbegin;
00187 Base::rend;
00188
00189 Base::front;
00190 Base::back;
00191 Base::assign;
00192
00193 Base::empty;
00194 Base::size;
00195
00196 Base::capacity;
00197 Base::max_size;
00198
00200
00201 void clear()
00202 {
00203 Base::clear();
00204 rows_ = 0;
00205 cols_ = 0;
00206 }
00207
00208 void resize(size_type i, size_type j, value_type value = value_type())
00209 {
00210 rows_ = i;
00211 cols_ = j;
00212 Base::resize(rows_*cols_, value);
00213 }
00214
00215 void resize(std::pair<UInt,UInt> const & size_pair, value_type value = value_type())
00216 {
00217 rows_ = size_pair.first;
00218 cols_ = size_pair.second;
00219 Base::resize(rows_*cols_, value);
00220 }
00221
00223 SizeType rows() const throw()
00224 {
00225 return rows_;
00226 }
00227
00229 SizeType cols() const throw()
00230 {
00231 return cols_;
00232 }
00233
00234 std::pair<UInt,UInt> sizePair() const
00235 {
00236 return std::pair<UInt,UInt>(rows_,cols_);
00237 }
00238
00243 SizeType const index(SizeType row, SizeType col) const
00244 {
00245 #ifdef OPENMS_DEBUG
00246 if ( row >= rows_ ) throw Exception::IndexOverflow(__FILE__,__LINE__,__PRETTY_FUNCTION__,row, rows_);
00247 if ( col >= cols_ ) throw Exception::IndexOverflow(__FILE__,__LINE__,__PRETTY_FUNCTION__,col, cols_);
00248 #endif
00249 return row * cols_ + col;
00250 }
00251
00256 std::pair<UInt,UInt> const indexPair(UInt index) const
00257 {
00258 #ifdef OPENMS_DEBUG
00259 if ( index >= size() ) throw Exception::IndexOverflow(__FILE__,__LINE__,__PRETTY_FUNCTION__,index,size()-1);
00260 #endif
00261 return std::pair<SizeType,SizeType>(index/cols_,index%cols_);
00262 }
00263
00268 SizeType colIndex(SizeType index) const
00269 {
00270 #ifdef OPENMS_DEBUG
00271 if ( index >= size() ) throw Exception::IndexOverflow(__FILE__,__LINE__,__PRETTY_FUNCTION__,index,size()-1);
00272 #endif
00273 return index%cols_;
00274 }
00275
00280 SizeType rowIndex(SizeType index) const
00281 {
00282 #ifdef OPENMS_DEBUG
00283 if ( index >= size() ) throw Exception::IndexOverflow(__FILE__,__LINE__,__PRETTY_FUNCTION__,index,size()-1);
00284 #endif
00285
00286 return index/cols_;
00287 }
00288
00293 bool operator == ( Matrix const & rhs ) const
00294 throw (Exception::Precondition)
00295 {
00296 OPENMS_PRECONDITION(cols_ == rhs.cols_,
00297 "Matrices have different row sizes.");
00298 OPENMS_PRECONDITION(rows_ == rhs.rows_,
00299 "Matrices have different column sizes.");
00300 return static_cast < typename Matrix<Value>::Base const &>(*this) == static_cast < typename Matrix<Value>::Base const &>(rhs);
00301 }
00302
00307 bool operator < (Matrix const & rhs) const
00308 throw (Exception::Precondition)
00309 {
00310 OPENMS_PRECONDITION(cols_ == rhs.cols_,
00311 "Matrices have different row sizes.");
00312 OPENMS_PRECONDITION(rows_ == rhs.rows_,
00313 "Matrices have different column sizes.");
00314 return static_cast < typename Matrix<Value>::Base const &>(*this) < static_cast < typename Matrix<Value>::Base const &>(rhs);
00315 }
00316
00317 protected:
00318
00320
00321
00322 SizeType rows_;
00324 SizeType cols_;
00326
00327 };
00328
00333 template <typename Value>
00334 std::ostream& operator << (std::ostream& os, const Matrix<Value>& matrix)
00335 {
00336 typedef typename Matrix<Value>::size_type size_type;
00337 for ( size_type i = 0; i < matrix.rows(); ++i ) {
00338 for ( size_type j = 0; j < matrix.cols(); ++j ) {
00339 os << std::setprecision(6) << std::setw(6) << matrix(i,j) << ' ';
00340 }
00341 os << std::endl;
00342 }
00343 return os;
00344 }
00345
00346
00347 }
00348
00349 #endif // OPENMS_DATASTRUCTURES_MATRIX_H