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
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082 #include <memory>
00083 #include <vector>
00084
00085 template <class T, class _ALLOC = std::allocator<T> >
00086 class MATRIX
00087 {
00088 private:
00089
00090 INT _rows;
00091 INT _cols;
00092 std::vector<T, _ALLOC> _v;
00093
00094 INT _d2_to_d1(INT at_row, INT at_col) const
00095 {
00096 Is_True(at_row >= 0 && at_row < _rows && at_col >= 0 && at_col < _cols,
00097 ("Invalid idx (%d,%d) for MATRIX(%d,%d)!",
00098 at_row, at_col, _rows, _cols));
00099 return at_row*_cols + at_col;
00100 }
00101
00102 INT _it_offset(INT at_row, INT at_col) const
00103 {
00104
00105
00106 return _d2_to_d1(at_row, at_col);
00107 }
00108
00109 INT _rit_offset(INT at_row, INT at_col) const
00110 {
00111
00112
00113 return _rows*_cols - _d2_to_d1(at_row, at_col) - 1;
00114 }
00115
00116 public:
00117
00118 typedef typename std::vector<T, _ALLOC>::reference reference;
00119 typedef typename std::vector<T, _ALLOC>::const_reference const_reference;
00120 typedef typename std::vector<T, _ALLOC>::allocator_type allocator_type;
00121 typedef typename std::vector<T, _ALLOC>::iterator iterator;
00122 typedef typename std::vector<T, _ALLOC>::const_iterator const_iterator;
00123 typedef typename std::vector<T, _ALLOC>::reverse_iterator reverse_iterator;
00124 typedef typename std::vector<T, _ALLOC>::const_reverse_iterator
00125 const_reverse_iterator;
00126
00127 MATRIX(INT rows,
00128 INT cols,
00129 const T& value,
00130 const _ALLOC &alloc = _ALLOC()):
00131 _rows(rows), _cols(cols), _v(rows*cols, value, alloc) {}
00132
00133 reference operator() (INT i, INT j)
00134 {
00135 return _v[_d2_to_d1(i,j)];
00136 }
00137
00138 const_reference operator() (INT i, INT j) const
00139 {
00140 return _v[_d2_to_d1(i,j)];
00141 }
00142
00143 INT rows() const {return _rows;}
00144 INT cols() const {return _cols;}
00145
00146 iterator begin() {return _v.begin();}
00147 iterator end() {return _v.end();}
00148 const_iterator begin() const {return _v.begin();}
00149 const_iterator end() const {return _v.end();}
00150
00151 reverse_iterator rbegin() {return _v.rbegin();}
00152 reverse_iterator rend() {return _v.rend();}
00153 const_reverse_iterator rbegin() const {return _v.rbegin();}
00154 const_reverse_iterator rend() const {return _v.rend();}
00155
00156 iterator pos(INT i, INT j) {return begin() + _it_offset(i,j);}
00157 const_iterator pos(INT i, INT j) const {return begin() + _it_offset(i,j);}
00158
00159 reverse_iterator rpos(INT i, INT j)
00160 {
00161 return rbegin() + _rit_offset(i,j);
00162 }
00163 const_reverse_iterator rpos(INT i, INT j) const
00164 {
00165 return rbegin() + _rit_offset(i,j);
00166 }
00167 };
00168