Generated on Mon Nov 30 23:53:33 2009 for Gecode by doxygen 1.6.1

matrix.hpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Mikael Lagerkvist, 2005
00008  *
00009  *  Bugfixes provided by:
00010  *     Olof Sivertsson <olof@olofsivertsson.com>
00011  *
00012  *  Last modified:
00013  *     $Date: 2009-10-13 21:19:13 +0200 (Tue, 13 Oct 2009) $ by $Author: schulte $
00014  *     $Revision: 9898 $
00015  *
00016  *  This file is part of Gecode, the generic constraint
00017  *  development environment:
00018  *     http://www.gecode.org
00019  *
00020  *  Permission is hereby granted, free of charge, to any person obtaining
00021  *  a copy of this software and associated documentation files (the
00022  *  "Software"), to deal in the Software without restriction, including
00023  *  without limitation the rights to use, copy, modify, merge, publish,
00024  *  distribute, sublicense, and/or sell copies of the Software, and to
00025  *  permit persons to whom the Software is furnished to do so, subject to
00026  *  the following conditions:
00027  *
00028  *  The above copyright notice and this permission notice shall be
00029  *  included in all copies or substantial portions of the Software.
00030  *
00031  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00032  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00033  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00034  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00035  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00036  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00037  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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 // STATISTICS: minimodel-any