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 #include <algorithm>
00042
00043 namespace Gecode {
00044
00045 template<class A>
00046 inline
00047 Matrix<A>::Slice::Slice(Matrix<A>& a,
00048 int fc, int tc, int fr, int tr)
00049 : _r(0), _fc(fc), _tc(tc), _fr(fr), _tr(tr) {
00050 if (tc > a.width() || tr > a.height())
00051 throw MiniModel::ArgumentOutOfRange("Matrix::Slice::Slice");
00052 if (fc >= tc || fr >= tr)
00053 throw MiniModel::ArgumentOutOfRange("Matrix::Slice::Slice");
00054
00055 _r = args_type((tc-fc)*(tr-fr));
00056
00057 int i = 0;
00058 for (int h = fr; h < tr; h++)
00059 for (int w = fc; w < tc; w++)
00060 _r[i++] = a(w, h);
00061 }
00062
00063 template<class A>
00064 typename Matrix<A>::Slice&
00065 Matrix<A>::Slice::reverse(void) {
00066 for (int i = 0; i < _r.size()/2; i++)
00067 std::swap(_r[i], _r[_r.size()-i-1]);
00068 return *this;
00069 }
00070
00071 template<class A>
00072 forceinline
00073 Matrix<A>::Slice::operator typename Matrix<A>::args_type(void) {
00074 return _r;
00075 }
00076 template<class A>
00077 forceinline
00078 Matrix<A>::Slice::operator Matrix<typename Matrix<A>::args_type>(void) {
00079 return Matrix<args_type>(_r, _tc-_fc, _tr-_fr);
00080 }
00081
00082
00083 template<class A>
00084 forceinline
00085 Matrix<A>::Matrix(A a, int w, int h)
00086 : _a(a), _w(w), _h(h) {
00087 if ((_w * _h) != _a.size())
00088 throw MiniModel::ArgumentSizeMismatch("Matrix::Matrix(A, w, h)");
00089 }
00090
00091 template<class A>
00092 forceinline
00093 Matrix<A>::Matrix(A a, int n)
00094 : _a(a), _w(n), _h(n) {
00095 if (n*n != _a.size())
00096 throw MiniModel::ArgumentSizeMismatch("Matrix::Matrix(A, n)");
00097 }
00098
00099 template<class A>
00100 forceinline int
00101 Matrix<A>::width(void) const { return _w; }
00102 template<class A>
00103 forceinline int
00104 Matrix<A>::height(void) const { return _h; }
00105 template<class A>
00106 inline typename Matrix<A>::args_type const
00107 Matrix<A>::get_array(void) const {
00108 return args_type(_a);
00109 }
00110
00111 template<class A>
00112 forceinline typename Matrix<A>::value_type&
00113 Matrix<A>::operator ()(int c, int r) {
00114 if ((c >= _w) || (r >= _h))
00115 throw MiniModel::ArgumentOutOfRange("Matrix::operator ()");
00116 return _a[r*_w + c];
00117 }
00118
00119 template<class A>
00120 forceinline typename Matrix<A>::Slice
00121 Matrix<A>::slice(int fc, int tc, int fr, int tr) {
00122 return Slice(*this, fc, tc, fr, tr);
00123 }
00124
00125 template<class A>
00126 forceinline typename Matrix<A>::Slice
00127 Matrix<A>::row(int r) {
00128 return slice(0, width(), r, r+1);
00129 }
00130
00131 template<class A>
00132 forceinline typename Matrix<A>::Slice
00133 Matrix<A>::col(int c) {
00134 return slice(c, c+1, 0, height());
00135 }
00136
00137
00138 forceinline void
00139 element(Home home, const Matrix<IntArgs>& m, IntVar x, IntVar y,
00140 IntVar z, IntConLevel icl) {
00141 element(home, m.get_array(), x, m.width(), y, m.height(), z, icl);
00142 }
00143 forceinline void
00144 element(Home home, const Matrix<IntArgs>& m, IntVar x, IntVar y,
00145 BoolVar z, IntConLevel icl) {
00146 element(home, m.get_array(), x, m.width(), y, m.height(), z, icl);
00147 }
00148 forceinline void
00149 element(Home home, const Matrix<IntVarArgs>& m, IntVar x, IntVar y,
00150 IntVar z, IntConLevel icl) {
00151 element(home, m.get_array(), x, m.width(), y, m.height(), z, icl);
00152 }
00153 forceinline void
00154 element(Home home, const Matrix<BoolVarArgs>& m, IntVar x, IntVar y,
00155 BoolVar z, IntConLevel icl) {
00156 element(home, m.get_array(), x, m.width(), y, m.height(), z, icl);
00157 }
00158
00159 #ifdef GECODE_HAS_SET_VARS
00160 forceinline void
00161 element(Home home, const Matrix<IntSetArgs>& m, IntVar x, IntVar y,
00162 SetVar z) {
00163 element(home, m.get_array(), x, m.width(), y, m.height(), z);
00164 }
00165 forceinline void
00166 element(Home home, const Matrix<SetVarArgs>& m, IntVar x, IntVar y,
00167 SetVar z) {
00168 element(home, m.get_array(), x, m.width(), y, m.height(), z);
00169 }
00170 #endif
00171
00172 }
00173
00174