matrix.icc
Go to the documentation of this file.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 namespace Gecode { namespace MiniModel {
00026
00027 template <class A>
00028 inline
00029 Matrix<A>::Slice::Slice(Matrix<A>& a,
00030 unsigned int fc, unsigned int tc,
00031 unsigned int fr, unsigned int tr)
00032 : _r(0), _fc(fc), _tc(tc), _fr(fr), _tr(tr) {
00033 if (tc > a.width() || tr > a.height())
00034 throw ArgumentOutOfRange("MiniModel::Matrix::Slice::Slice");
00035 if (fc >= tc || fr >= tr)
00036 throw ArgumentOutOfRange("MiniModel::Matrix::Slice::Slice");
00037
00038 _r = args_type((tc-fc)*(tr-fr));
00039
00040 int i = 0;
00041 for (unsigned int h = fr; h < tr; ++h) {
00042 for (unsigned int w = fc; w < tc; ++w) {
00043 _r[i++] = a(w, h);
00044 }
00045 }
00046 }
00047
00048 template <class A>
00049 inline
00050 Matrix<A>::Slice::operator typename Matrix<A>::args_type(void) {
00051 return _r;
00052 }
00053 template <class A>
00054 inline
00055 Matrix<A>::Slice::operator Matrix<typename Matrix<A>::args_type>(void) {
00056 return Matrix<args_type>(_r, _tc-_fc, _tr-_fr);
00057 }
00058
00059
00060 template <class A>
00061 inline
00062 Matrix<A>::Matrix(A a, unsigned int w, unsigned int h)
00063 : _a(a), _w(w), _h(h) {
00064 if (_w * _h != static_cast<unsigned int>(_a.size()))
00065 throw ArgumentSizeMismatch("MiniModel::Matrix::Matrix(A, w, h)");
00066 }
00067
00068 template <class A>
00069 inline
00070 Matrix<A>::Matrix(A a, unsigned int n)
00071 : _a(a), _w(n), _h(n) {
00072 if (n*n != static_cast<unsigned int>(_a.size()))
00073 throw ArgumentSizeMismatch("MiniModel::Matrix::Matrix(A, n)");
00074 }
00075
00076 template <class A>
00077 inline unsigned int
00078 Matrix<A>::width(void) const { return _w; }
00079 template <class A>
00080 inline unsigned int
00081 Matrix<A>::height(void) const { return _h; }
00082 template <class A>
00083 inline typename Matrix<A>::args_type const
00084 Matrix<A>::get_array(void) {
00085 return args_type(_a);
00086 }
00087
00088 template <class A>
00089 inline typename Matrix<A>::value_type&
00090 Matrix<A>::operator()(unsigned int c, unsigned int r) {
00091 if (c >= _w || r >= _h)
00092 throw ArgumentOutOfRange("MiniModel::Matrix::operator()");
00093
00094 return _a[r*_w + c];
00095 }
00096
00097 template <class A>
00098 inline typename Matrix<A>::Slice
00099 Matrix<A>::slice(unsigned int fc, unsigned int tc,
00100 unsigned int fr, unsigned int tr) {
00101 return Slice(*this, fc, tc, fr, tr);
00102 }
00103
00104 template <class A>
00105 inline typename Matrix<A>::args_type
00106 Matrix<A>::row(int r) {
00107 return slice(0, width(), r, r+1);
00108 }
00109
00110 template <class A>
00111 inline typename Matrix<A>::args_type
00112 Matrix<A>::col(int c) {
00113 return slice(c, c+1, 0, height());
00114 }
00115
00116 }}
00117
00118