Generated on Wed Nov 1 15:04:43 2006 for Gecode by doxygen 1.4.5

matrix.icc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Mikael Lagerkvist, 2005
00007  *
00008  *  Bugfixes provided by:
00009  *     Olof Sivertsson <olof@olofsivertsson.com>
00010  *
00011  *  Last modified:
00012  *     $Date: 2006-08-04 16:05:26 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $
00013  *     $Revision: 3513 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  See the file "LICENSE" for information on usage and
00020  *  redistribution of this file, and for a
00021  *     DISCLAIMER OF ALL WARRANTIES.
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 // STATISTICS: minimodel-any