/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Mikael Lagerkvist * * Copyright: * Mikael Lagerkvist, 2005 * * Bugfixes provided by: * Olof Sivertsson * * Last modified: * $Date: 2007-08-09 15:30:21 +0200 (Thu, 09 Aug 2007) $ by $Author: tack $ * $Revision: 4790 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ #include namespace Gecode { namespace MiniModel { template inline Matrix::Slice::Slice(Matrix& a, unsigned int fc, unsigned int tc, unsigned int fr, unsigned int tr) : _r(0), _fc(fc), _tc(tc), _fr(fr), _tr(tr) { if (tc > a.width() || tr > a.height()) throw ArgumentOutOfRange("MiniModel::Matrix::Slice::Slice"); if (fc >= tc || fr >= tr) throw ArgumentOutOfRange("MiniModel::Matrix::Slice::Slice"); _r = args_type((tc-fc)*(tr-fr)); int i = 0; for (unsigned int h = fr; h < tr; ++h) { for (unsigned int w = fc; w < tc; ++w) { _r[i++] = a(w, h); } } } template typename Matrix::Slice& Matrix::Slice::reverse(void) { for (int i = 0; i < _r.size()/2; ++i) std::swap(_r[i], _r[_r.size()-i-1]); return *this; } template inline Matrix::Slice::operator typename Matrix::args_type(void) { return _r; } template inline Matrix::Slice::operator Matrix::args_type>(void) { return Matrix(_r, _tc-_fc, _tr-_fr); } template inline Matrix::Matrix(A a, unsigned int w, unsigned int h) : _a(a), _w(w), _h(h) { if (_w * _h != static_cast(_a.size())) throw ArgumentSizeMismatch("MiniModel::Matrix::Matrix(A, w, h)"); } template inline Matrix::Matrix(A a, unsigned int n) : _a(a), _w(n), _h(n) { if (n*n != static_cast(_a.size())) throw ArgumentSizeMismatch("MiniModel::Matrix::Matrix(A, n)"); } template inline unsigned int Matrix::width(void) const { return _w; } template inline unsigned int Matrix::height(void) const { return _h; } template inline typename Matrix::args_type const Matrix::get_array(void) { return args_type(_a); } template inline typename Matrix::value_type& Matrix::operator()(unsigned int c, unsigned int r) { if (c >= _w || r >= _h) throw ArgumentOutOfRange("MiniModel::Matrix::operator()"); return _a[r*_w + c]; } template inline typename Matrix::Slice Matrix::slice(unsigned int fc, unsigned int tc, unsigned int fr, unsigned int tr) { return Slice(*this, fc, tc, fr, tr); } template inline typename Matrix::Slice Matrix::row(int r) { return slice(0, width(), r, r+1); } template inline typename Matrix::Slice Matrix::col(int c) { return slice(c, c+1, 0, height()); } }} // STATISTICS: minimodel-any