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