Generated on Mon Jul 6 18:09:16 2009 for Gecode by doxygen 1.5.9

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: 2008-12-05 16:43:57 +0100 (Fri, 05 Dec 2008) $ by $Author: schulte $
00014  *     $Revision: 7989 $
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                           unsigned int fc, unsigned int tc,
00049                           unsigned int fr, unsigned int tr)
00050     : _r(0), _fc(fc), _tc(tc), _fr(fr), _tr(tr) {
00051     if (tc > a.width() || tr > a.height())
00052       throw MiniModel::ArgumentOutOfRange("Matrix::Slice::Slice");
00053     if (fc >= tc || fr >= tr)
00054       throw MiniModel::ArgumentOutOfRange("Matrix::Slice::Slice");
00055 
00056     _r = args_type((tc-fc)*(tr-fr));
00057 
00058     int i = 0;
00059     for (unsigned int h = fr; h < tr; ++h) {
00060       for (unsigned int w = fc; w < tc; ++w) {
00061         _r[i++] = a(w, h);
00062       }
00063     }
00064   }
00065 
00066   template <class A>
00067   typename Matrix<A>::Slice&
00068   Matrix<A>::Slice::reverse(void) {
00069     for (int i = 0; i < _r.size()/2; ++i)
00070       std::swap(_r[i], _r[_r.size()-i-1]);
00071     return *this;
00072   }
00073 
00074   template <class A>
00075   inline
00076   Matrix<A>::Slice::operator typename Matrix<A>::args_type(void) {
00077     return _r;
00078   }
00079   template <class A>
00080   inline
00081   Matrix<A>::Slice::operator Matrix<typename Matrix<A>::args_type>(void) {
00082     return Matrix<args_type>(_r, _tc-_fc, _tr-_fr);
00083   }
00084 
00085 
00086   template <class A>
00087   inline
00088   Matrix<A>::Matrix(A a, unsigned int w, unsigned int h)
00089     : _a(a), _w(w), _h(h) {
00090     if (_w * _h != static_cast<unsigned int>(_a.size()))
00091       throw MiniModel::ArgumentSizeMismatch("Matrix::Matrix(A, w, h)");
00092   }
00093 
00094   template <class A>
00095   inline
00096   Matrix<A>::Matrix(A a, unsigned int n)
00097     : _a(a), _w(n), _h(n) {
00098     if (n*n != static_cast<unsigned int>(_a.size()))
00099       throw MiniModel::ArgumentSizeMismatch("Matrix::Matrix(A, n)");
00100   }
00101 
00102   template <class A>
00103   inline unsigned int
00104   Matrix<A>::width(void) const  { return _w; }
00105   template <class A>
00106   inline unsigned int
00107   Matrix<A>::height(void) const { return _h; }
00108   template <class A>
00109   inline typename Matrix<A>::args_type const
00110   Matrix<A>::get_array(void) {
00111     return args_type(_a);
00112   }
00113 
00114   template <class A>
00115   inline typename Matrix<A>::value_type&
00116   Matrix<A>::operator ()(unsigned int c, unsigned int r) {
00117     if ((c >= _w) || (r >= _h))
00118       throw MiniModel::ArgumentOutOfRange("Matrix::operator ()");
00119     return _a[r*_w + c];
00120   }
00121 
00122   template <class A>
00123   inline typename Matrix<A>::Slice
00124   Matrix<A>::slice(unsigned int fc, unsigned int tc,
00125                    unsigned int fr, unsigned int tr) {
00126     return Slice(*this, fc, tc, fr, tr);
00127   }
00128 
00129   template <class A>
00130   inline typename Matrix<A>::Slice
00131   Matrix<A>::row(int r) {
00132     return slice(0, width(), r, r+1);
00133   }
00134 
00135   template <class A>
00136   inline typename Matrix<A>::Slice
00137   Matrix<A>::col(int c) {
00138     return slice(c, c+1, 0, height());
00139   }
00140 
00141 }
00142 
00143 // STATISTICS: minimodel-any